diff --git a/Program.cs b/Program.cs index 201ac4a..488a9bc 100644 --- a/Program.cs +++ b/Program.cs @@ -490,8 +490,7 @@ static async Task MainAsync(string[] args) Console.WriteLine(" --dialog-title Custom title for progress dialog"); Console.WriteLine(" --dialog-message Custom message for progress dialog"); Console.WriteLine(" --pipe Named pipe for GUI output streaming"); - Console.WriteLine(" --save-settings Save GUI settings to registry"); - Console.WriteLine(" --save-settings-file Save settings from JSON file to registry"); + Console.WriteLine(" --save-settings Save settings to HKLM machine settings (administrator)"); Console.WriteLine(" --help Show this help message"); Console.WriteLine(" --version, -V Show version information"); Console.WriteLine(" --status Show current installation status"); @@ -624,12 +623,6 @@ static async Task MainAsync(string[] args) case "--save-settings": return SaveSettingsFromArgs(args); - case "--save-settings-file": - if (i + 1 < args.Length) - return SaveSettingsFromFile(args[++i]); - Console.WriteLine("ERROR: --save-settings-file requires a file path"); - return ExitFailure; - case "--headers": // The GUI passes the authorization header this way. Consume the // value - leaving it in place made the header string itself get @@ -748,36 +741,7 @@ static async Task MainAsync(string[] args) } /// - /// Saves settings from a JSON file. Called by the GUI app via elevated process. - /// - private static int SaveSettingsFromFile(string filePath) - { - try - { - var json = File.ReadAllText(filePath); - var config = System.Text.Json.JsonSerializer.Deserialize(json); - if (config is null) - { - Logger.Error("Failed to deserialize settings file."); - return 1; - } - ConfigManager.SaveUserSettings(config); - Logger.Info("Settings saved from file successfully."); - return 0; - } - catch (Exception ex) - { - Logger.Error($"Failed to save settings from file: {ex.Message}"); - return 1; - } - finally - { - try { File.Delete(filePath); } catch { } - } - } - - /// - /// Saves settings from CLI args to user registry. Called by the GUI app via elevated process. + /// Saves settings from CLI args to the HKLM machine settings key (requires administrator). /// Expected format: --save-settings --url X --dialog-title Y ... /// private static int SaveSettingsFromArgs(string[] args) @@ -818,7 +782,7 @@ private static int SaveSettingsFromArgs(string[] args) try { - ConfigManager.SaveUserSettings(config); + ConfigManager.SaveMachineSettings(config); Logger.Info("Settings saved to registry successfully."); return 0; } diff --git a/README.md b/README.md index 7ec764b..63fa3b0 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Before building, set up your environment variables: When a run completes, BootstrapMate can POST a vendor-neutral JSON run summary to an optional endpoint, turning "did this PC provision cleanly?" into a fleet-dashboard query. The payload is plain JSON and not tied to any specific backend — any service accepting a JSON POST (a custom collector, ReportMate, MunkiReport, etc.) can consume it. Both the Windows and macOS clients emit the same schema. -Configure via Intune CSP / Group Policy (the bundled ADMX), the machine/user registry, or both keys below: +Configure via Intune CSP / Group Policy (the bundled ADMX), the machine registry (`HKLM\SOFTWARE\BootstrapMate\Settings`), or both keys below: | Key | Type | Effect | |---|---|---| @@ -50,7 +50,7 @@ The POST is best-effort: it is bounded by a short timeout and never fails the ru Before any MSI or EXE installer is executed elevated, BootstrapMate verifies its Authenticode signature with `WinVerifyTrust`. A successful download only proves where the bytes came from — not who produced them. The signature gate ensures an installer carries a signature that chains to a trusted root (and, when configured, matches an expected publisher) before it runs as an elevated process. -Behaviour is controlled via Intune CSP / Group Policy (the bundled ADMX), the machine/user registry, or per-item manifest fields. +Behaviour is controlled via Intune CSP / Group Policy (the bundled ADMX), the machine registry, or per-item manifest fields. Policy / registry keys (`HKLM\SOFTWARE\Policies\BootstrapMate` for policy; `HKLM\SOFTWARE\BootstrapMate\Settings` for machine settings): @@ -743,7 +743,7 @@ Options: --dialog-title Custom progress dialog title --dialog-message Custom progress dialog message --pipe Named pipe for GUI output streaming - --save-settings Save GUI settings to the registry + --save-settings Save settings to the HKLM machine settings key (administrator) --status Show current installation status --clear-status Clear all installation status data --clear-cache Clear caches, including failed installation files diff --git a/src/BootstrapMate.App/MainWindow.xaml.cs b/src/BootstrapMate.App/MainWindow.xaml.cs index 6a07069..ea0256f 100644 --- a/src/BootstrapMate.App/MainWindow.xaml.cs +++ b/src/BootstrapMate.App/MainWindow.xaml.cs @@ -44,8 +44,11 @@ public MainWindow() // Apply Mica backdrop for modern Windows 11 look SystemBackdrop = new MicaBackdrop(); - // Select the first tab on launch - NavView.SelectedItem = NavView.MenuItems[0]; + // Open on Prefs when asked to (the elevated relaunch from Unlock passes --prefs); + // Prefs is also the first tab, so this is the default either way. + NavView.SelectedItem = BootstrapMate.Core.PrefsElevation.OpensOnPrefs(Environment.GetCommandLineArgs().Skip(1)) + ? NavView.MenuItems.OfType().First(i => (string?)i.Tag == "prefs") + : NavView.MenuItems[0]; } private void NavView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) diff --git a/src/BootstrapMate.App/ViewModels/PrefsViewModel.cs b/src/BootstrapMate.App/ViewModels/PrefsViewModel.cs index bfe7d1d..5b8f209 100644 --- a/src/BootstrapMate.App/ViewModels/PrefsViewModel.cs +++ b/src/BootstrapMate.App/ViewModels/PrefsViewModel.cs @@ -6,7 +6,9 @@ namespace BootstrapMate.App.ViewModels; /// /// ViewModel for the Prefs tab. Mirrors macOS SettingsViewModel. -/// Loads from ConfigManager, saves non-managed settings to the user registry (HKCU), shows management lock state. +/// Loads from ConfigManager and shows management lock state. Settings live in HKLM, so the tab is +/// read-only unless this process is elevated; Unlock relaunches the app elevated on this tab. +/// When elevated, non-managed settings auto-save to HKLM through ConfigManager.SaveMachineSettings. /// public partial class PrefsViewModel : ObservableObject { @@ -89,6 +91,67 @@ public double NetworkTimeoutValue // ── Policy Lock Properties (for x:Bind) ───────────────────── + // ── Elevation State ───────────────────────────────────────── + + /// True when this process can write HKLM (elevated administrator token). + public bool IsElevated { get; } = PrefsElevation.IsProcessElevated(); + + public bool IsReadOnly => !IsElevated; + + [ObservableProperty] private string _unlockError = ""; + + public bool HasUnlockError => !string.IsNullOrEmpty(UnlockError); + + partial void OnUnlockErrorChanged(string value) => OnPropertyChanged(nameof(HasUnlockError)); + + /// + /// Relaunches this app elevated through UAC, opening on the Prefs tab. Returns true when the + /// elevated instance started, so the caller can close this one. A cancelled UAC prompt + /// returns false quietly and leaves the tab read-only. + /// + public bool TryRelaunchElevated() + { + UnlockError = ""; + try + { + var exe = Environment.ProcessPath; + if (string.IsNullOrEmpty(exe)) + { + UnlockError = "Could not find the app's own executable to relaunch."; + return false; + } + + using var process = System.Diagnostics.Process.Start(PrefsElevation.BuildElevatedRelaunch(exe)); + return process is not null; + } + catch (Exception ex) when (PrefsElevation.IsElevationCancelled(ex)) + { + return false; + } + catch (Exception ex) + { + UnlockError = $"Could not relaunch as administrator: {ex.Message}"; + return false; + } + } + + // ── Editability (elevated and not managed by policy) ──────── + + private bool CanEdit(string key) => PrefsElevation.CanEdit(IsElevated, _managedKeys.Contains(key)); + + public bool CanEditManifestUrl => CanEdit("ManifestUrl"); + public bool CanEditAuthHeader => CanEdit("AuthorizationHeader"); + public bool CanEditSilentMode => CanEdit("SilentMode"); + public bool CanEditVerboseMode => CanEdit("VerboseMode"); + public bool CanEditDryRun => CanEdit("DryRun"); + public bool CanEditEnableDialog => CanEdit("EnableDialog"); + public bool CanEditDialogTitle => CanEdit("DialogTitle"); + public bool CanEditDialogMessage => CanEdit("DialogMessage"); + public bool CanEditDialogIcon => CanEdit("DialogIcon"); + public bool CanEditBlurScreen => CanEdit("BlurScreen"); + public bool CanEditCustomInstallPath => CanEdit("CustomInstallPath"); + public bool CanEditNetworkTimeout => CanEdit("NetworkTimeout"); + public bool IsManifestUrlLocked => _managedKeys.Contains("ManifestUrl"); public bool IsAuthHeaderLocked => _managedKeys.Contains("AuthorizationHeader"); public bool IsSilentModeLocked => _managedKeys.Contains("SilentMode"); @@ -164,6 +227,7 @@ public void Load() nameof(AuthPlaceholderText), nameof(NetworkTimeoutValue), nameof(VersionDisplay), nameof(IsManifestPreviewLoading), nameof(ManifestPreviewContent), nameof(ManifestPreviewError), nameof(HasManifestPreviewContent), nameof(HasManifestPreviewError), + nameof(UnlockError), nameof(HasUnlockError), "", // string.Empty from Load's bulk notify ]; @@ -171,13 +235,14 @@ protected override void OnPropertyChanged(System.ComponentModel.PropertyChangedE { base.OnPropertyChanged(e); - if (_isLoading || _nonSettingProperties.Contains(e.PropertyName ?? "")) + // Not elevated: HKLM is not writable and the tab is read-only, so never save. + if (_isLoading || !IsElevated || _nonSettingProperties.Contains(e.PropertyName ?? "")) return; _autoSaveTimer?.Dispose(); _autoSaveTimer = new System.Threading.Timer(_ => { - try { ConfigManager.SaveUserSettings(BuildConfig()); } catch { } + try { ConfigManager.SaveMachineSettings(BuildConfig(), PageKeys); } catch { } }, null, 500, System.Threading.Timeout.Infinite); } @@ -223,6 +288,17 @@ public List BuildRunArguments() // ── Private ────────────────────────────────────────────────── + /// + /// The machine settings this tab edits. Saving writes only these, so values the tab does not + /// show (reporting, signature checks, baseline interval) keep whatever HKLM already holds. + /// + private static readonly string[] PageKeys = + [ + "ManifestUrl", "AuthorizationHeader", "SilentMode", "VerboseMode", "DryRun", + "EnableDialog", "NoDialog", "DialogTitle", "DialogMessage", "DialogIcon", "BlurScreen", + "CustomInstallPath", "NetworkTimeout", + ]; + private BootstrapMateConfig BuildConfig() => new() { ManifestUrl = ManifestUrl, diff --git a/src/BootstrapMate.App/Views/PrefsPage.xaml b/src/BootstrapMate.App/Views/PrefsPage.xaml index 03540d9..074e3ab 100644 --- a/src/BootstrapMate.App/Views/PrefsPage.xaml +++ b/src/BootstrapMate.App/Views/PrefsPage.xaml @@ -40,6 +40,46 @@ HorizontalAlignment="Center" /> + + + + + + + + + + + + + + + + + + + + + + @@ -73,7 +113,7 @@ + IsEnabled="{x:Bind ViewModel.CanEditManifestUrl, Mode=OneWay}" />