| 1 | package guestos |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "syscall" |
| 7 | |
| 8 | "github.com/google/uuid" |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | |
| 13 | type WindowsHostEnrollmentPipeError struct { |
| 14 | Operation string |
| 15 | Win32 syscall.Errno |
| 16 | } |
| 17 | |
| 18 | func (err *WindowsHostEnrollmentPipeError) Error() string { |
| 19 | return fmt.Sprintf("enrollment_pipe_%s_failed (win32=%d)", err.Operation, err.Win32) |
| 20 | } |
| 21 | |
| 22 | func enrollmentPipeError(operation string, err error) error { |
| 23 | var code syscall.Errno |
| 24 | errors.As(err, &code) |
| 25 | return &WindowsHostEnrollmentPipeError{Operation: operation, Win32: code} |
| 26 | } |
| 27 | |
| 28 | |
| 29 | |
| 30 | type WindowsHostEnrollmentOptions struct { |
| 31 | VMID uuid.UUID |
| 32 | InstallationID uuid.UUID |
| 33 | HostSID string |
| 34 | Profile string |
| 35 | State string |
| 36 | Pipe string |
| 37 | ParentPID uint32 |
| 38 | } |
| 39 | |
| 40 | type windowsHostEnrollmentRequest struct { |
| 41 | Version int `json:"version"` |
| 42 | Challenge string `json:"challenge"` |
| 43 | Action string `json:"action"` |
| 44 | VMID uuid.UUID `json:"vm_id"` |
| 45 | GUIUserSID string `json:"gui_user_sid,omitempty"` |
| 46 | PID uint32 `json:"pid,omitempty"` |
| 47 | ProcessHandle uint64 `json:"process_handle,omitempty"` |
| 48 | } |
| 49 | |
| 50 | type windowsHostEnrollmentReply struct { |
| 51 | Version int `json:"version"` |
| 52 | OK bool `json:"ok"` |
| 53 | Challenge string `json:"challenge,omitempty"` |
| 54 | Authorized bool `json:"authorized,omitempty"` |
| 55 | Error string `json:"error,omitempty"` |
| 56 | } |
| 57 | |
| 58 | func (request windowsHostEnrollmentRequest) validate(vm uuid.UUID, owner bool, guestSID string) error { |
| 59 | if request.Version != 1 || request.VMID != vm { |
| 60 | return errors.New("enrollment_request_invalid") |
| 61 | } |
| 62 | if owner { |
| 63 | switch request.Action { |
| 64 | case "ready", "stop": |
| 65 | if request.PID != 0 || request.ProcessHandle != 0 || request.GUIUserSID != "" { |
| 66 | return errors.New("enrollment_request_invalid") |
| 67 | } |
| 68 | case "allow-admin": |
| 69 | if request.PID == 0 || request.ProcessHandle == 0 || request.GUIUserSID != "" { |
| 70 | return errors.New("enrollment_request_invalid") |
| 71 | } |
| 72 | default: |
| 73 | return errors.New("enrollment_action_invalid") |
| 74 | } |
| 75 | } else { |
| 76 | if request.PID != 0 || request.ProcessHandle != 0 || !windowsGUISID.MatchString(request.GUIUserSID) || guestSID != "" && request.GUIUserSID != guestSID { |
| 77 | return errors.New("enrollment_request_invalid") |
| 78 | } |
| 79 | switch request.Action { |
| 80 | case "deliver-gui", "deliver-packet": |
| 81 | case "finalize": |
| 82 | if guestSID == "" { |
| 83 | return errors.New("enrollment_request_invalid") |
| 84 | } |
| 85 | default: |
| 86 | return errors.New("enrollment_action_invalid") |
| 87 | } |
| 88 | } |
| 89 | return nil |
| 90 | } |