1use super::auth::authentication_methods;
4use crate::{arg_of, args_of, child_block, list_values, named_children};
5use asterust::config::ConfigLine;
6
7#[derive(Clone, Debug, PartialEq, Eq)]
9#[non_exhaustive]
10pub struct LoginClass {
11 pub name: String,
12 pub permissions: Vec<String>,
13 pub idle_timeout: Option<String>,
15 pub allow_commands: Option<String>,
17 pub deny_commands: Option<String>,
18 pub allow_configuration: Option<String>,
19 pub deny_configuration: Option<String>,
20 pub access_start: Option<String>,
22 pub access_end: Option<String>,
23 pub allowed_days: Vec<String>,
24 pub allow_sources: Vec<String>,
25 pub deny_sources: Vec<String>,
26 pub allow_times: Vec<String>,
27 pub deny_times: Vec<String>,
28}
29
30#[derive(Clone, Debug, PartialEq, Eq)]
32#[non_exhaustive]
33pub struct LoginUser {
34 pub name: String,
35 pub class: Option<String>,
36 pub uid: Option<String>,
37 pub full_name: Option<String>,
38 pub authentication_methods: Vec<String>,
40}
41
42#[derive(Clone, Debug, PartialEq, Eq)]
44#[non_exhaustive]
45pub struct LoginPassword {
46 pub change_type: Option<String>,
48 pub format: Option<String>,
50 pub maximum_length: Option<String>,
51 pub minimum_length: Option<String>,
52 pub maximum_lifetime: Option<String>,
53 pub minimum_lifetime: Option<String>,
54 pub minimum_changes: Option<String>,
55 pub minimum_character_changes: Option<String>,
56 pub minimum_lower_cases: Option<String>,
57 pub minimum_upper_cases: Option<String>,
58 pub minimum_numerics: Option<String>,
59 pub minimum_punctuations: Option<String>,
60 pub minimum_reuse: Option<String>,
61}
62
63#[derive(Clone, Debug, PartialEq, Eq)]
65#[non_exhaustive]
66pub struct LoginRetryOptions {
67 pub backoff_factor: Option<String>,
68 pub backoff_threshold: Option<String>,
69 pub lockout_period: Option<String>,
71 pub maximum_time: Option<String>,
72 pub minimum_time: Option<String>,
73 pub tries_before_disconnect: Option<String>,
74}
75
76#[derive(Clone, Debug, PartialEq, Eq)]
78#[non_exhaustive]
79pub struct Login {
80 pub message: Option<String>,
82 pub announcement: Option<String>,
84 pub idle_timeout: Option<String>,
86 pub deny_sources: Vec<String>,
88 pub password: Option<LoginPassword>,
89 pub retry_options: Option<LoginRetryOptions>,
90 pub classes: Vec<LoginClass>,
91 pub users: Vec<LoginUser>,
92}
93
94pub(super) fn parse_login(login: &ConfigLine) -> Login {
95 Login {
96 message: arg_of(login, "message").map(str::to_string),
97 announcement: arg_of(login, "announcement").map(str::to_string),
98 idle_timeout: arg_of(login, "idle-timeout").map(str::to_string),
99 deny_sources: child_block(login, "deny-sources")
100 .map(|sources| list(sources, "address"))
101 .unwrap_or_default(),
102 password: child_block(login, "password").map(|password| {
103 let field = |keyword: &str| arg_of(password, keyword).map(str::to_string);
104 LoginPassword {
105 change_type: field("change-type"),
106 format: field("format"),
107 maximum_length: field("maximum-length"),
108 minimum_length: field("minimum-length"),
109 maximum_lifetime: field("maximum-lifetime"),
110 minimum_lifetime: field("minimum-lifetime"),
111 minimum_changes: field("minimum-changes"),
112 minimum_character_changes: field("minimum-character-changes"),
113 minimum_lower_cases: field("minimum-lower-cases"),
114 minimum_upper_cases: field("minimum-upper-cases"),
115 minimum_numerics: field("minimum-numerics"),
116 minimum_punctuations: field("minimum-punctuations"),
117 minimum_reuse: field("minimum-reuse"),
118 }
119 }),
120 retry_options: child_block(login, "retry-options").map(|retry| {
121 let field = |keyword: &str| arg_of(retry, keyword).map(str::to_string);
122 LoginRetryOptions {
123 backoff_factor: field("backoff-factor"),
124 backoff_threshold: field("backoff-threshold"),
125 lockout_period: field("lockout-period"),
126 maximum_time: field("maximum-time"),
127 minimum_time: field("minimum-time"),
128 tries_before_disconnect: field("tries-before-disconnect"),
129 }
130 }),
131 classes: named_children(login, "class")
132 .map(|(name, class)| LoginClass {
133 name: name.to_string(),
134 permissions: args_of(class, "permissions")
135 .into_iter()
136 .flat_map(list_values)
137 .collect(),
138 idle_timeout: arg_of(class, "idle-timeout").map(str::to_string),
139 allow_commands: arg_of(class, "allow-commands").map(str::to_string),
140 deny_commands: arg_of(class, "deny-commands").map(str::to_string),
141 allow_configuration: arg_of(class, "allow-configuration").map(str::to_string),
142 deny_configuration: arg_of(class, "deny-configuration").map(str::to_string),
143 access_start: arg_of(class, "access-start").map(str::to_string),
144 access_end: arg_of(class, "access-end").map(str::to_string),
145 allowed_days: list(class, "allowed-days"),
146 allow_sources: list(class, "allow-sources"),
147 deny_sources: list(class, "deny-sources"),
148 allow_times: list(class, "allow-times"),
149 deny_times: list(class, "deny-times"),
150 })
151 .collect(),
152 users: named_children(login, "user")
153 .map(|(name, user)| LoginUser {
154 name: name.to_string(),
155 class: arg_of(user, "class").map(str::to_string),
156 uid: arg_of(user, "uid").map(str::to_string),
157 full_name: arg_of(user, "full-name").map(str::to_string),
158 authentication_methods: child_block(user, "authentication")
159 .map(authentication_methods)
160 .unwrap_or_default(),
161 })
162 .collect(),
163 }
164}
165
166fn list(line: &ConfigLine, keyword: &str) -> Vec<String> {
168 args_of(line, keyword).into_iter().flat_map(list_values).collect()
169}
170
171#[cfg(test)]
172mod tests {
173 use crate::system::system;
174 use asterust::config::juniper;
175
176 const LOGIN: &str = r#"
177system {
178 login {
179 message "Authorized access only";
180 announcement "Maintenance window Sunday";
181 class NOC {
182 permissions [ view view-configuration ];
183 idle-timeout 15;
184 allow-commands "(ping)|(traceroute)";
185 deny-configuration ".*";
186 }
187 class OPS {
188 permissions all;
189 deny-commands "(request system)";
190 allow-configuration "(interfaces)|(protocols)";
191 }
192 user admin {
193 uid 2000;
194 class super-user;
195 full-name "Network Admin";
196 authentication {
197 encrypted-password "$6$salt$hash"; ## SECRET-DATA
198 ssh-ed25519 "ssh-ed25519 AAAAC3Nza admin@host"; ## SECRET-DATA
199 }
200 }
201 user monitor {
202 class NOC;
203 }
204 }
205}
206"#;
207
208 #[test]
209 fn extracts_login_banner_and_classes() {
210 let login = system(&juniper::parse_braces(LOGIN)).unwrap().login.unwrap();
211 assert_eq!(login.message.as_deref(), Some("Authorized access only"));
212 assert_eq!(login.announcement.as_deref(), Some("Maintenance window Sunday"));
213
214 assert_eq!(login.classes.len(), 2);
215 let noc = &login.classes[0];
216 assert_eq!(noc.name, "NOC");
217 assert_eq!(noc.permissions, ["view", "view-configuration"]);
218 assert_eq!(noc.idle_timeout.as_deref(), Some("15"));
219 assert_eq!(noc.allow_commands.as_deref(), Some("(ping)|(traceroute)"));
220 assert_eq!(noc.deny_configuration.as_deref(), Some(".*"));
221 assert_eq!(noc.deny_commands, None);
222
223 let ops = &login.classes[1];
224 assert_eq!(ops.permissions, ["all"]);
225 assert_eq!(ops.deny_commands.as_deref(), Some("(request system)"));
226 assert_eq!(ops.allow_configuration.as_deref(), Some("(interfaces)|(protocols)"));
227 }
228
229 #[test]
230 fn extracts_login_users_without_secrets() {
231 let login = system(&juniper::parse_braces(LOGIN)).unwrap().login.unwrap();
232 assert_eq!(login.users.len(), 2);
233
234 let admin = &login.users[0];
235 assert_eq!(admin.name, "admin");
236 assert_eq!(admin.class.as_deref(), Some("super-user"));
237 assert_eq!(admin.uid.as_deref(), Some("2000"));
238 assert_eq!(admin.full_name.as_deref(), Some("Network Admin"));
239 assert_eq!(admin.authentication_methods, ["encrypted-password", "ssh-ed25519"]);
240
241 let monitor = &login.users[1];
242 assert_eq!(monitor.class.as_deref(), Some("NOC"));
243 assert!(monitor.authentication_methods.is_empty());
244 }
245
246 const LOGIN_OPTIONS: &str = r#"
248system {
249 login {
250 idle-timeout 30;
251 deny-sources {
252 address [ 203.0.113.0/24 198.51.100.7 ];
253 }
254 password {
255 format sha512;
256 change-type character-sets;
257 minimum-length 12;
258 maximum-length 64;
259 minimum-changes 3;
260 minimum-upper-cases 1;
261 minimum-lower-cases 1;
262 minimum-numerics 1;
263 minimum-punctuations 1;
264 minimum-reuse 5;
265 maximum-lifetime 90;
266 minimum-lifetime 1;
267 minimum-character-changes 2;
268 }
269 retry-options {
270 tries-before-disconnect 3;
271 backoff-threshold 2;
272 backoff-factor 5;
273 minimum-time 20;
274 maximum-time 60;
275 lockout-period 10;
276 }
277 class OPS {
278 access-start 08:00;
279 access-end 18:00;
280 allowed-days [ monday tuesday friday ];
281 allow-sources [ 192.0.2.0/24 ];
282 deny-sources 192.0.2.99;
283 allow-times [ 08:00-12:00 ];
284 deny-times [ 22:00-23:00 ];
285 }
286 }
287}
288"#;
289
290 #[test]
291 fn extracts_login_level_options() {
292 let login = system(&juniper::parse_braces(LOGIN_OPTIONS)).unwrap().login.unwrap();
293 assert_eq!(login.idle_timeout.as_deref(), Some("30"));
294 assert_eq!(login.deny_sources, ["203.0.113.0/24", "198.51.100.7"]);
295 }
296
297 #[test]
298 fn extracts_password_policy() {
299 let login = system(&juniper::parse_braces(LOGIN_OPTIONS)).unwrap().login.unwrap();
300 let password = login.password.unwrap();
301 assert_eq!(password.format.as_deref(), Some("sha512"));
302 assert_eq!(password.change_type.as_deref(), Some("character-sets"));
303 assert_eq!(password.minimum_length.as_deref(), Some("12"));
304 assert_eq!(password.maximum_length.as_deref(), Some("64"));
305 assert_eq!(password.minimum_changes.as_deref(), Some("3"));
306 assert_eq!(password.minimum_upper_cases.as_deref(), Some("1"));
307 assert_eq!(password.minimum_lower_cases.as_deref(), Some("1"));
308 assert_eq!(password.minimum_numerics.as_deref(), Some("1"));
309 assert_eq!(password.minimum_punctuations.as_deref(), Some("1"));
310 assert_eq!(password.minimum_reuse.as_deref(), Some("5"));
311 assert_eq!(password.maximum_lifetime.as_deref(), Some("90"));
312 assert_eq!(password.minimum_lifetime.as_deref(), Some("1"));
313 assert_eq!(password.minimum_character_changes.as_deref(), Some("2"));
314 }
315
316 #[test]
317 fn extracts_retry_options() {
318 let login = system(&juniper::parse_braces(LOGIN_OPTIONS)).unwrap().login.unwrap();
319 let retry = login.retry_options.unwrap();
320 assert_eq!(retry.tries_before_disconnect.as_deref(), Some("3"));
321 assert_eq!(retry.backoff_threshold.as_deref(), Some("2"));
322 assert_eq!(retry.backoff_factor.as_deref(), Some("5"));
323 assert_eq!(retry.minimum_time.as_deref(), Some("20"));
324 assert_eq!(retry.maximum_time.as_deref(), Some("60"));
325 assert_eq!(retry.lockout_period.as_deref(), Some("10"));
326 }
327
328 #[test]
329 fn extracts_class_access_restrictions() {
330 let login = system(&juniper::parse_braces(LOGIN_OPTIONS)).unwrap().login.unwrap();
331 let class = &login.classes[0];
332 assert_eq!(class.access_start.as_deref(), Some("08:00"));
333 assert_eq!(class.access_end.as_deref(), Some("18:00"));
334 assert_eq!(class.allowed_days, ["monday", "tuesday", "friday"]);
335 assert_eq!(class.allow_sources, ["192.0.2.0/24"]);
336 assert_eq!(class.deny_sources, ["192.0.2.99"]);
337 assert_eq!(class.allow_times, ["08:00-12:00"]);
338 assert_eq!(class.deny_times, ["22:00-23:00"]);
339 }
340
341 #[test]
342 fn login_options_are_optional() {
343 let login = system(&juniper::parse_braces(
344 "system {\n login {\n class OPS;\n }\n}\n",
345 ))
346 .unwrap()
347 .login
348 .unwrap();
349 assert_eq!(login.idle_timeout, None);
350 assert!(login.deny_sources.is_empty());
351 assert!(login.password.is_none());
352 assert!(login.retry_options.is_none());
353 }
354
355 #[test]
356 fn no_login_is_none() {
357 assert!(system(&juniper::parse_braces("system {\n host-name pe1;\n}\n"))
358 .unwrap()
359 .login
360 .is_none());
361 }
362}