| 1 | |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "flag" |
| 10 | "fmt" |
| 11 | "github.com/google/uuid" |
| 12 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 13 | "os" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | func main() { |
| 18 | if len(os.Args) < 2 { |
| 19 | os.Exit(2) |
| 20 | } |
| 21 | flags := flag.NewFlagSet("Cage.HyperVManager", flag.ContinueOnError) |
| 22 | flags.SetOutput(os.Stderr) |
| 23 | binding := flags.String("binding", "", "") |
| 24 | vm := flags.String("vm-id", "", "") |
| 25 | hostSID := flags.String("host-sid", "", "") |
| 26 | profile := flags.String("profile", "", "") |
| 27 | state := flags.String("state", "", "") |
| 28 | installation := flags.String("installation-id", "", "") |
| 29 | pipe := flags.String("pipe", "", "") |
| 30 | parentPID := flags.Uint("parent-pid", 0, "") |
| 31 | if flags.Parse(os.Args[2:]) != nil || flags.NArg() != 0 { |
| 32 | os.Exit(2) |
| 33 | } |
| 34 | var err error |
| 35 | enrollmentFlags := *profile != "" || *state != "" || *installation != "" || *pipe != "" || *parentPID != 0 |
| 36 | switch os.Args[1] { |
| 37 | case "service": |
| 38 | if *binding == "" || *vm != "" || *hostSID != "" || enrollmentFlags { |
| 39 | os.Exit(2) |
| 40 | } |
| 41 | err = guestos.RunWindowsHostManagerService(*binding) |
| 42 | case "prepare", "binding": |
| 43 | id, parseErr := uuid.Parse(*vm) |
| 44 | if parseErr != nil || id == uuid.Nil || *hostSID == "" || *binding != "" || enrollmentFlags { |
| 45 | os.Exit(2) |
| 46 | } |
| 47 | ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 48 | defer cancel() |
| 49 | var record guestos.WindowsHostManagerBinding |
| 50 | if os.Args[1] == "binding" { |
| 51 | record, err = guestos.LoadWindowsHostManagerBinding(guestos.WindowsHostManagerBindingPath(id)) |
| 52 | if err == nil && (record.VMID != id || record.HostSID != *hostSID) { |
| 53 | err = fmt.Errorf("binding_mismatch") |
| 54 | } |
| 55 | } else { |
| 56 | record, err = guestos.PrepareWindowsHostManagerBinding(ctx, id, *hostSID) |
| 57 | } |
| 58 | if err == nil { |
| 59 | err = json.NewEncoder(os.Stdout).Encode(record) |
| 60 | } |
| 61 | case "user-enrollment": |
| 62 | id, idErr := uuid.Parse(*vm) |
| 63 | install, installErr := uuid.Parse(*installation) |
| 64 | if idErr != nil || installErr != nil || id == uuid.Nil || install == uuid.Nil || *hostSID == "" || *profile == "" || *state == "" || *pipe == "" || *parentPID == 0 || *parentPID > 0xffffffff || *binding != "" { |
| 65 | os.Exit(2) |
| 66 | } |
| 67 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Hour) |
| 68 | defer cancel() |
| 69 | err = guestos.RunWindowsHostEnrollment(ctx, guestos.WindowsHostEnrollmentOptions{VMID: id, InstallationID: install, HostSID: *hostSID, Profile: *profile, State: *state, Pipe: *pipe, ParentPID: uint32(*parentPID)}) |
| 70 | default: |
| 71 | os.Exit(2) |
| 72 | } |
| 73 | if err != nil { |
| 74 | fmt.Fprintln(os.Stderr, "Windows host manager action did not complete.") |
| 75 | var pipeError *guestos.WindowsHostEnrollmentPipeError |
| 76 | if errors.As(err, &pipeError) { |
| 77 | fmt.Fprintln(os.Stderr, pipeError) |
| 78 | } |
| 79 | os.Exit(1) |
| 80 | } |
| 81 | } |