| 1 | package layout |
| 2 | |
| 3 | import ( |
| 4 | "github.com/carlosframework/platform/internal/hostbox" |
| 5 | "github.com/carlosframework/platform/internal/provision" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | ) |
| 9 | |
| 10 | func InstanceCensusName(rel string) (string, bool) { |
| 11 | parent := filepath.Dir(rel) |
| 12 | if parent != "instances" && parent != ".instances" { |
| 13 | return "", false |
| 14 | } |
| 15 | name := filepath.Base(rel) |
| 16 | if rel != filepath.Join(parent, name) || name == "." || name == ".." { |
| 17 | return "", false |
| 18 | } |
| 19 | return name, true |
| 20 | } |
| 21 | |
| 22 | func VerifyInstanceCensusAlias(root, alias, host string) error { |
| 23 | public := filepath.Join("instances", provision.InstanceID(host)) |
| 24 | if alias != filepath.Join(".instances", provision.InstanceID(host)) { |
| 25 | return os.ErrPermission |
| 26 | } |
| 27 | a, err := hostbox.CaptureInventoryIdentity(root, alias) |
| 28 | if err != nil { |
| 29 | return err |
| 30 | } |
| 31 | b, err := hostbox.CaptureInventoryIdentity(root, public) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | ar, err := filepath.Rel(a.MountPoint, filepath.Join(root, alias)) |
| 36 | if err != nil || !filepath.IsLocal(ar) { |
| 37 | return os.ErrPermission |
| 38 | } |
| 39 | br, err := filepath.Rel(b.MountPoint, filepath.Join(root, public)) |
| 40 | if err != nil || !filepath.IsLocal(br) { |
| 41 | return os.ErrPermission |
| 42 | } |
| 43 | if !a.SameMountNamespace(b) || a.Device != b.Device || filepath.Join(a.MountRoot, ar) != filepath.Join(b.MountRoot, br) { |
| 44 | return os.ErrPermission |
| 45 | } |
| 46 | b.MountRoot, b.MountPoint, b.Mount, b.MountLineage = a.MountRoot, a.MountPoint, a.Mount, a.MountLineage |
| 47 | b.MountUnique = a.MountUnique |
| 48 | if !a.Same(b) { |
| 49 | return os.ErrPermission |
| 50 | } |
| 51 | return nil |
| 52 | } |
| 53 | |