| 1 | //! Configuration for gardmd |
| 2 | |
| 3 | use serde::Deserialize; |
| 4 | use std::path::PathBuf; |
| 5 | |
| 6 | /// Main configuration structure |
| 7 | #[derive(Debug, Clone, Deserialize)] |
| 8 | pub struct Config { |
| 9 | #[serde(default)] |
| 10 | pub general: GeneralConfig, |
| 11 | #[serde(default)] |
| 12 | pub greeter: GreeterConfig, |
| 13 | #[serde(default)] |
| 14 | pub security: SecurityConfig, |
| 15 | } |
| 16 | |
| 17 | impl Default for Config { |
| 18 | fn default() -> Self { |
| 19 | Self { |
| 20 | general: GeneralConfig::default(), |
| 21 | greeter: GreeterConfig::default(), |
| 22 | security: SecurityConfig::default(), |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /// General daemon settings |
| 28 | #[derive(Debug, Clone, Deserialize)] |
| 29 | pub struct GeneralConfig { |
| 30 | /// Default session if user hasn't selected one |
| 31 | #[serde(default = "default_session")] |
| 32 | pub default_session: String, |
| 33 | |
| 34 | /// Path to greeter executable |
| 35 | #[serde(default = "default_greeter")] |
| 36 | pub greeter: PathBuf, |
| 37 | |
| 38 | /// VT to use (0 = auto-select) |
| 39 | #[serde(default)] |
| 40 | pub vt: u32, |
| 41 | |
| 42 | /// X11 display to use |
| 43 | #[serde(default = "default_display")] |
| 44 | pub display: String, |
| 45 | } |
| 46 | |
| 47 | impl Default for GeneralConfig { |
| 48 | fn default() -> Self { |
| 49 | Self { |
| 50 | default_session: default_session(), |
| 51 | greeter: default_greeter(), |
| 52 | vt: 0, |
| 53 | display: default_display(), |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// Greeter visual settings |
| 59 | #[derive(Debug, Clone, Deserialize)] |
| 60 | pub struct GreeterConfig { |
| 61 | /// Blur radius for background |
| 62 | #[serde(default = "default_blur_radius")] |
| 63 | pub blur_radius: u32, |
| 64 | |
| 65 | /// Background brightness (0.0-1.0) |
| 66 | #[serde(default = "default_blur_brightness")] |
| 67 | pub blur_brightness: f32, |
| 68 | |
| 69 | /// Show power buttons |
| 70 | #[serde(default = "default_true")] |
| 71 | pub show_power_buttons: bool, |
| 72 | |
| 73 | /// Show session selector |
| 74 | #[serde(default = "default_true")] |
| 75 | pub show_session_selector: bool, |
| 76 | |
| 77 | /// Use garbg wallpaper |
| 78 | #[serde(default = "default_true")] |
| 79 | pub use_garbg_wallpaper: bool, |
| 80 | |
| 81 | /// Fallback wallpaper path |
| 82 | #[serde(default = "default_fallback_wallpaper")] |
| 83 | pub fallback_wallpaper: PathBuf, |
| 84 | } |
| 85 | |
| 86 | impl Default for GreeterConfig { |
| 87 | fn default() -> Self { |
| 88 | Self { |
| 89 | blur_radius: default_blur_radius(), |
| 90 | blur_brightness: default_blur_brightness(), |
| 91 | show_power_buttons: true, |
| 92 | show_session_selector: true, |
| 93 | use_garbg_wallpaper: true, |
| 94 | fallback_wallpaper: default_fallback_wallpaper(), |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// Security settings |
| 100 | #[derive(Debug, Clone, Deserialize)] |
| 101 | pub struct SecurityConfig { |
| 102 | /// Allow empty passwords |
| 103 | #[serde(default)] |
| 104 | pub allow_empty_password: bool, |
| 105 | |
| 106 | /// Lock after N failed attempts (0 = disabled) |
| 107 | #[serde(default = "default_lockout_attempts")] |
| 108 | pub lockout_attempts: u32, |
| 109 | |
| 110 | /// Lockout duration in seconds |
| 111 | #[serde(default = "default_lockout_duration")] |
| 112 | pub lockout_duration: u64, |
| 113 | } |
| 114 | |
| 115 | impl Default for SecurityConfig { |
| 116 | fn default() -> Self { |
| 117 | Self { |
| 118 | allow_empty_password: false, |
| 119 | lockout_attempts: default_lockout_attempts(), |
| 120 | lockout_duration: default_lockout_duration(), |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Default value functions |
| 126 | fn default_session() -> String { "gar".to_string() } |
| 127 | fn default_greeter() -> PathBuf { PathBuf::from("/usr/bin/gardm-greeter") } |
| 128 | fn default_display() -> String { ":0".to_string() } |
| 129 | fn default_blur_radius() -> u32 { 20 } |
| 130 | fn default_blur_brightness() -> f32 { 0.7 } |
| 131 | fn default_true() -> bool { true } |
| 132 | fn default_fallback_wallpaper() -> PathBuf { |
| 133 | PathBuf::from("/usr/share/gardm/backgrounds/default.jpg") |
| 134 | } |
| 135 | fn default_lockout_attempts() -> u32 { 5 } |
| 136 | fn default_lockout_duration() -> u64 { 300 } |
| 137 | |
| 138 | impl Config { |
| 139 | /// Load configuration from default path |
| 140 | pub fn load() -> anyhow::Result<Self> { |
| 141 | Self::load_from("/etc/gardm/config.toml") |
| 142 | } |
| 143 | |
| 144 | /// Load configuration from specified path |
| 145 | pub fn load_from(path: &str) -> anyhow::Result<Self> { |
| 146 | let path = PathBuf::from(path); |
| 147 | if path.exists() { |
| 148 | let content = std::fs::read_to_string(&path)?; |
| 149 | Ok(toml::from_str(&content)?) |
| 150 | } else { |
| 151 | tracing::info!("Config file not found at {}, using defaults", path.display()); |
| 152 | Ok(Config::default()) |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 |