| 1 | package hostfloor |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "github.com/carlosframework/platform/internal/release" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | type Running struct { |
| 12 | ReleaseTag string |
| 13 | Hash string |
| 14 | Protocol int |
| 15 | BootID string |
| 16 | FloorSHA256 string |
| 17 | GuardReady bool |
| 18 | } |
| 19 | |
| 20 | func (d Disk) Running(build, platform string, role ...string) (Running, error) { |
| 21 | component := "router" |
| 22 | if len(role) == 1 { |
| 23 | component = role[0] |
| 24 | } else if len(role) > 1 { |
| 25 | return Running{}, ErrFloor |
| 26 | } |
| 27 | _, f, err := d.Load() |
| 28 | if err != nil { |
| 29 | return Running{}, err |
| 30 | } |
| 31 | if component == "console" { |
| 32 | path, err := os.ReadFile(filepath.Join(d.Dir, "console-ready.json")) |
| 33 | if err != nil { |
| 34 | return Running{}, err |
| 35 | } |
| 36 | dropin, err := os.ReadFile("/etc/systemd/system/console.service.d/90-host-floor.conf") |
| 37 | if err != nil { |
| 38 | return Running{}, err |
| 39 | } |
| 40 | want := ConsoleGuardDropIn(string(path)) |
| 41 | if string(dropin) != want { |
| 42 | return Running{}, ErrFloor |
| 43 | } |
| 44 | } |
| 45 | hash, err := ExecutingHash() |
| 46 | if err != nil { |
| 47 | return Running{}, err |
| 48 | } |
| 49 | boot, err := os.ReadFile("/proc/sys/kernel/random/boot_id") |
| 50 | if err != nil { |
| 51 | return Running{}, err |
| 52 | } |
| 53 | entries, err := os.ReadDir(filepath.Join(d.Dir, "identities")) |
| 54 | if err != nil { |
| 55 | return Running{}, err |
| 56 | } |
| 57 | for _, entry := range entries { |
| 58 | if entry.IsDir() { |
| 59 | continue |
| 60 | } |
| 61 | raw, err := os.ReadFile(filepath.Join(d.Dir, "identities", entry.Name())) |
| 62 | if err != nil { |
| 63 | return Running{}, err |
| 64 | } |
| 65 | if CheckComponent(d.PublicKey, f, raw, build, platform, hash, component) != nil { |
| 66 | continue |
| 67 | } |
| 68 | tag := strings.TrimSuffix(entry.Name(), ".json") |
| 69 | if _, err := os.Stat(filepath.Join(d.Dir, "carlos")); err != nil { |
| 70 | return Running{}, err |
| 71 | } |
| 72 | identity, err := release.VerifySystemIdentity(d.PublicKey, raw) |
| 73 | if err != nil { |
| 74 | return Running{}, err |
| 75 | } |
| 76 | return Running{tag, hash, identity.Current.MovesProtocol, strings.TrimSpace(string(boot)), f.Digest, true}, nil |
| 77 | } |
| 78 | return Running{}, ErrFloor |
| 79 | } |
| 80 | |
| 81 | // Enrollment is installed by root from the verified roster. It is reporting |
| 82 | // configuration; readers always compare it to the fresh signed roster. |
| 83 | type Enrollment struct { |
| 84 | Box string `json:"box"` |
| 85 | Enrollment string `json:"enrollment"` |
| 86 | InstanceID string `json:"instance_id"` |
| 87 | } |
| 88 | |
| 89 | func ReadEnrollment(path string) (Enrollment, error) { |
| 90 | raw, err := os.ReadFile(path) |
| 91 | if err != nil { |
| 92 | return Enrollment{}, err |
| 93 | } |
| 94 | var e Enrollment |
| 95 | if json.Unmarshal(raw, &e) != nil || e.Box == "" || e.Enrollment == "" || e.InstanceID == "" { |
| 96 | return Enrollment{}, ErrFloor |
| 97 | } |
| 98 | return e, nil |
| 99 | } |
| 100 | |
| 101 | func ConsoleGuardDropIn(path string) string { |
| 102 | return "[Service]\nExecStart=\nExecStart=/opt/carlos/system/guard/carlos system guard-exec console " + path + " serve\n" |
| 103 | } |
| 104 | |