|
4 | 4 | package update |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "context" |
7 | 8 | "encoding/json" |
| 9 | + "errors" |
8 | 10 | "fmt" |
9 | 11 | "net/http" |
| 12 | + "os/exec" |
| 13 | + "path/filepath" |
| 14 | + "regexp" |
10 | 15 | "strings" |
11 | 16 | "syscall" |
| 17 | + "time" |
12 | 18 | "unsafe" |
13 | 19 |
|
14 | 20 | "github.com/fleetdm/fleet/v4/server/fleet" |
| 21 | + "github.com/fleetdm/fleet/v4/server/mdm/microsoft/syncml" |
15 | 22 | "github.com/rs/zerolog/log" |
16 | 23 | "golang.org/x/sys/windows" |
17 | 24 | "golang.org/x/sys/windows/registry" |
@@ -190,3 +197,92 @@ func IsRunningOnWindowsServer() (bool, error) { |
190 | 197 |
|
191 | 198 | return false, nil |
192 | 199 | } |
| 200 | + |
| 201 | +// windowsMDMSyncTriggerTimeout bounds the deviceenroller invocation. attemptSync runs it in a background goroutine that holds the receiver's |
| 202 | +// lock, so this caps how long that goroutine (and the single-flight lock) stays tied up if deviceenroller ever hangs. `deviceenroller /o /c` |
| 203 | +// returns within seconds in practice; the timeout is a generous backstop, not the expected duration. |
| 204 | +const windowsMDMSyncTriggerTimeout = 2 * time.Minute |
| 205 | + |
| 206 | +// TriggerWindowsMDMSync starts an on-demand, client-initiated OMA-DM session with the Fleet MDM server so that queued Windows MDM commands |
| 207 | +// are delivered without waiting for the device's next scheduled poll. It runs the OS deviceenroller for Fleet's enrollment in |
| 208 | +// client-initiated mode: `deviceenroller.exe /o <EnrollmentGUID> /c`. |
| 209 | +// |
| 210 | +// Microsoft does not publicly document the deviceenroller flags; `/o <GUID> /c` was established empirically and validated end-to-end on |
| 211 | +// Windows 11 24H2 (build 26100) and 25H2 (build 26200). On a warm device it reliably starts a session. On a just-booted or just-resumed |
| 212 | +// device the same call can exit 0 without actually starting a session, so a nil return here is best-effort, not a guarantee of delivery. |
| 213 | +// Delivery is ultimately guaranteed by the server continuing to request a sync while the command is queued (orbit re-fires on the next |
| 214 | +// config poll) and by the relaxed scheduled poll as a floor. |
| 215 | +// |
| 216 | +// Exported so it can be built/tested for Windows from tools/. Not meant to be called from outside this package. |
| 217 | +func TriggerWindowsMDMSync() error { |
| 218 | + guid, err := fleetMDMEnrollmentGUID() |
| 219 | + if err != nil { |
| 220 | + return fmt.Errorf("find Fleet MDM enrollment: %w", err) |
| 221 | + } |
| 222 | + |
| 223 | + // Resolve System32 via the Windows API rather than the process environment, so the path we execute as SYSTEM can't be redirected by a |
| 224 | + // tampered SystemRoot. orbit is a 64-bit process, so this is the real (64-bit) System32 that contains deviceenroller.exe; there is no |
| 225 | + // WOW64 redirection to account for. If the API ever fails, use the hardcoded default rather than trusting an environment variable. |
| 226 | + systemDir, sysDirErr := windows.GetSystemDirectory() |
| 227 | + if sysDirErr != nil || systemDir == "" { |
| 228 | + systemDir = `C:\Windows\System32` |
| 229 | + } |
| 230 | + deviceEnroller := filepath.Join(systemDir, "deviceenroller.exe") |
| 231 | + |
| 232 | + // Bound the call so a hung deviceenroller cannot block the config-receiver loop indefinitely. |
| 233 | + ctx, cancel := context.WithTimeout(context.Background(), windowsMDMSyncTriggerTimeout) |
| 234 | + defer cancel() |
| 235 | + |
| 236 | + cmd := exec.CommandContext(ctx, deviceEnroller, "/o", guid, "/c") |
| 237 | + cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} |
| 238 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 239 | + if ctx.Err() == context.DeadlineExceeded { |
| 240 | + return fmt.Errorf("run deviceenroller /o %s /c timed out after %s (output: %q)", guid, windowsMDMSyncTriggerTimeout, string(out)) |
| 241 | + } |
| 242 | + return fmt.Errorf("run deviceenroller /o %s /c: %w (output: %q)", guid, err, string(out)) |
| 243 | + } |
| 244 | + return nil |
| 245 | +} |
| 246 | + |
| 247 | +// windowsEnrollmentGUIDRe matches a standard enrollment GUID (8-4-4-4-12 hex). The matched subkey name becomes an argument to deviceenroller |
| 248 | +// while orbit runs as SYSTEM, so we validate its shape before using it, even though writing the Enrollments key already requires admin. |
| 249 | +var windowsEnrollmentGUIDRe = regexp.MustCompile(`^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$`) |
| 250 | + |
| 251 | +// fleetMDMEnrollmentGUID returns the enrollment GUID of the active Fleet Windows MDM enrollment by scanning |
| 252 | +// HKLM\SOFTWARE\Microsoft\Enrollments for the subkey whose ProviderID is Fleet's and whose EnrollmentState is active. The subkey name is |
| 253 | +// the enrollment GUID that deviceenroller's /o argument expects. |
| 254 | +func fleetMDMEnrollmentGUID() (string, error) { |
| 255 | + const enrollmentsPath = `SOFTWARE\Microsoft\Enrollments` |
| 256 | + root, err := registry.OpenKey(registry.LOCAL_MACHINE, enrollmentsPath, registry.READ) |
| 257 | + if err != nil { |
| 258 | + return "", fmt.Errorf("open enrollments registry key: %w", err) |
| 259 | + } |
| 260 | + defer root.Close() |
| 261 | + |
| 262 | + names, err := root.ReadSubKeyNames(-1) |
| 263 | + if err != nil { |
| 264 | + return "", fmt.Errorf("read enrollment subkeys: %w", err) |
| 265 | + } |
| 266 | + |
| 267 | + // EnrollmentState == 1 is the active state observed for Fleet's MDM enrollment on tested Windows builds; the registry DWORD under |
| 268 | + // Enrollments is not authoritatively documented by Microsoft. The ProviderID == "Fleet" check in the loop below scopes the match to |
| 269 | + // Fleet's own enrollment, so this never selects an unrelated (e.g. Intune) enrollment that might use a different state value. |
| 270 | + const enrollmentStateActive = 1 |
| 271 | + for _, name := range names { |
| 272 | + k, err := registry.OpenKey(registry.LOCAL_MACHINE, enrollmentsPath+`\`+name, registry.QUERY_VALUE) |
| 273 | + if err != nil { |
| 274 | + continue |
| 275 | + } |
| 276 | + providerID, _, providerErr := k.GetStringValue("ProviderID") |
| 277 | + state, _, stateErr := k.GetIntegerValue("EnrollmentState") |
| 278 | + k.Close() |
| 279 | + if providerErr == nil && stateErr == nil && providerID == syncml.DocProvisioningAppProviderID && state == enrollmentStateActive { |
| 280 | + // Don't hand a malformed subkey name to deviceenroller; skip it and keep looking for a well-formed enrollment GUID. |
| 281 | + if !windowsEnrollmentGUIDRe.MatchString(name) { |
| 282 | + continue |
| 283 | + } |
| 284 | + return name, nil |
| 285 | + } |
| 286 | + } |
| 287 | + return "", errors.New("no active Fleet MDM enrollment found in registry") |
| 288 | +} |
0 commit comments