1use crate::{active_children, arg_of, child_block, has_leaf, split_name, unquote};
4use asterust::config::ConfigLine;
5
6#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct MasterPassword {
10 pub iteration_count: Option<String>,
11 pub pseudorandom_function: Option<String>,
13}
14
15#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct RootAuthentication {
19 pub methods: Vec<String>,
21}
22
23#[derive(Clone, Debug, PartialEq, Eq)]
25#[non_exhaustive]
26pub struct RadiusServer {
27 pub address: String,
28 pub port: Option<String>,
29 pub accounting_port: Option<String>,
30 pub retry: Option<String>,
31 pub timeout: Option<String>,
32 pub source_address: Option<String>,
33 pub routing_instance: Option<String>,
34}
35
36#[derive(Clone, Debug, PartialEq, Eq)]
38#[non_exhaustive]
39pub struct TacplusServer {
40 pub address: String,
41 pub port: Option<String>,
42 pub timeout: Option<String>,
43 pub source_address: Option<String>,
44 pub routing_instance: Option<String>,
45 pub single_connection: bool,
46}
47
48fn servers<'a>(system: &'a ConfigLine, keyword: &str) -> Vec<(&'a str, &'a ConfigLine)> {
50 let mut found = Vec::new();
51 for child in active_children(system) {
52 if child.text == keyword {
53 found.extend(active_children(child).map(|server| (unquote(&server.text), server)));
54 } else if let Some(arg) = child.arg(keyword) {
55 found.push((split_name(arg).0, child));
56 }
57 }
58 found
59}
60
61pub(super) fn radius_servers(parent: &ConfigLine, keyword: &str) -> Vec<RadiusServer> {
63 servers(parent, keyword)
64 .into_iter()
65 .map(|(address, server)| RadiusServer {
66 address: address.to_string(),
67 port: arg_of(server, "port").map(str::to_string),
68 accounting_port: arg_of(server, "accounting-port").map(str::to_string),
69 retry: arg_of(server, "retry").map(str::to_string),
70 timeout: arg_of(server, "timeout").map(str::to_string),
71 source_address: arg_of(server, "source-address").map(str::to_string),
72 routing_instance: arg_of(server, "routing-instance").map(str::to_string),
73 })
74 .collect()
75}
76
77pub(super) fn tacplus_servers(parent: &ConfigLine, keyword: &str) -> Vec<TacplusServer> {
78 servers(parent, keyword)
79 .into_iter()
80 .map(|(address, server)| TacplusServer {
81 address: address.to_string(),
82 port: arg_of(server, "port").map(str::to_string),
83 timeout: arg_of(server, "timeout").map(str::to_string),
84 source_address: arg_of(server, "source-address").map(str::to_string),
85 routing_instance: arg_of(server, "routing-instance").map(str::to_string),
86 single_connection: has_leaf(server, "single-connection"),
87 })
88 .collect()
89}
90
91pub(super) fn parse_master_password(master_password: &ConfigLine) -> MasterPassword {
92 MasterPassword {
93 iteration_count: arg_of(master_password, "iteration-count").map(str::to_string),
94 pseudorandom_function: arg_of(master_password, "pseudorandom-function").map(str::to_string),
95 }
96}
97
98pub(super) fn password_methods(system: &ConfigLine, keyword: &str) -> Vec<String> {
101 let mut methods = Vec::new();
102 for child in active_children(system) {
103 if child.text == keyword {
104 methods.extend(authentication_methods(child));
105 } else if let Some(method) = child.arg(keyword).and_then(|arg| arg.split_whitespace().next()) {
106 methods.push(method.to_string());
107 }
108 }
109 methods
110}
111
112pub(super) fn root_authentication(system: &ConfigLine) -> Option<RootAuthentication> {
113 child_block(system, "root-authentication").map(|auth| RootAuthentication {
114 methods: authentication_methods(auth),
115 })
116}
117
118pub(super) fn authentication_methods(auth: &ConfigLine) -> Vec<String> {
121 active_children(auth)
122 .filter_map(|c| c.text.split_whitespace().next())
123 .map(str::to_string)
124 .collect()
125}
126
127#[cfg(test)]
128mod tests {
129 use crate::system::system;
130 use asterust::config::juniper;
131
132 #[test]
133 fn extracts_root_authentication_methods_without_secrets() {
134 let system = system(&juniper::parse_braces(
135 r#"
136system {
137 root-authentication {
138 encrypted-password "$6$salt$hash"; ## SECRET-DATA
139 ssh-rsa "ssh-rsa AAAAB3Nza root@host"; ## SECRET-DATA
140 ssh-rsa "ssh-rsa AAAAB3Nzb ops@host" {
141 from 192.0.2.0/24;
142 }
143 }
144}
145"#,
146 ))
147 .unwrap();
148 let root = system.root_authentication.unwrap();
149 assert_eq!(root.methods, ["encrypted-password", "ssh-rsa", "ssh-rsa"]);
150 }
151
152 #[test]
153 fn root_authentication_can_disable_public_keys() {
154 let system = system(&juniper::parse_braces(
155 "system {\n root-authentication {\n no-public-keys;\n }\n}\n",
156 ))
157 .unwrap();
158 assert_eq!(system.root_authentication.unwrap().methods, ["no-public-keys"]);
159 }
160
161 #[test]
162 fn extracts_master_password_settings() {
163 let master = system(&juniper::parse_braces(
164 "system {\n master-password {\n iteration-count 500;\n pseudorandom-function hmac-sha2-256;\n }\n}\n",
165 ))
166 .unwrap()
167 .master_password
168 .unwrap();
169 assert_eq!(master.iteration_count.as_deref(), Some("500"));
170 assert_eq!(master.pseudorandom_function.as_deref(), Some("hmac-sha2-256"));
171 }
172
173 #[test]
174 fn diag_port_and_pic_console_report_only_the_method() {
175 let system = system(&juniper::parse_braces(
176 r#"
177system {
178 diag-port-authentication encrypted-password "$6$salt$hash"; ## SECRET-DATA
179 pic-console-authentication {
180 encrypted-password "$6$salt$other"; ## SECRET-DATA
181 }
182}
183"#,
184 ))
185 .unwrap();
186 assert_eq!(system.diag_port_authentication, ["encrypted-password"]);
187 assert_eq!(system.pic_console_authentication, ["encrypted-password"]);
188 }
189
190 #[test]
191 fn secret_settings_are_optional() {
192 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
193 assert!(system.master_password.is_none());
194 assert!(system.diag_port_authentication.is_empty());
195 assert!(system.pic_console_authentication.is_empty());
196 }
197
198 #[test]
199 fn no_root_authentication_is_none() {
200 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
201 assert!(system.root_authentication.is_none());
202 }
203
204 #[test]
205 fn extracts_authentication_order() {
206 let system = system(&juniper::parse_braces(
207 "system {\n authentication-order [ tacplus radius password ];\n}\n",
208 ))
209 .unwrap();
210 assert_eq!(system.authentication_order, ["tacplus", "radius", "password"]);
211 }
212
213 #[test]
214 fn extracts_radius_servers_without_secrets() {
215 let system = system(&juniper::parse_braces(
216 r#"
217system {
218 radius-server 192.0.2.10 {
219 secret "$9$secret"; ## SECRET-DATA
220 port 1812;
221 accounting-port 1813;
222 retry 5;
223 timeout 10;
224 source-address 192.0.2.1;
225 routing-instance mgmt_junos;
226 }
227 radius-server 192.0.2.11 {
228 secret "$9$other"; ## SECRET-DATA
229 }
230}
231"#,
232 ))
233 .unwrap();
234 assert_eq!(system.radius_servers.len(), 2);
235
236 let first = &system.radius_servers[0];
237 assert_eq!(first.address, "192.0.2.10");
238 assert_eq!(first.port.as_deref(), Some("1812"));
239 assert_eq!(first.accounting_port.as_deref(), Some("1813"));
240 assert_eq!(first.retry.as_deref(), Some("5"));
241 assert_eq!(first.timeout.as_deref(), Some("10"));
242 assert_eq!(first.source_address.as_deref(), Some("192.0.2.1"));
243 assert_eq!(first.routing_instance.as_deref(), Some("mgmt_junos"));
244
245 assert_eq!(system.radius_servers[1].address, "192.0.2.11");
246 assert_eq!(system.radius_servers[1].port, None);
247 }
248
249 #[test]
250 fn extracts_radius_servers_in_nested_block_form() {
251 let system = system(&juniper::parse_braces(
252 "system {\n radius-server {\n 192.0.2.10 {\n port 1645;\n }\n }\n}\n",
253 ))
254 .unwrap();
255 assert_eq!(system.radius_servers[0].address, "192.0.2.10");
256 assert_eq!(system.radius_servers[0].port.as_deref(), Some("1645"));
257 }
258
259 #[test]
260 fn extracts_tacplus_servers_without_secrets() {
261 let system = system(&juniper::parse_braces(
262 r#"
263system {
264 tacplus-server 192.0.2.20 {
265 port 49;
266 secret "$9$secret"; ## SECRET-DATA
267 single-connection;
268 source-address 192.0.2.1;
269 timeout 5;
270 }
271 inactive: tacplus-server 192.0.2.21 {
272 secret "$9$other"; ## SECRET-DATA
273 }
274}
275"#,
276 ))
277 .unwrap();
278 assert_eq!(system.tacplus_servers.len(), 1);
279 let server = &system.tacplus_servers[0];
280 assert_eq!(server.address, "192.0.2.20");
281 assert_eq!(server.port.as_deref(), Some("49"));
282 assert_eq!(server.timeout.as_deref(), Some("5"));
283 assert_eq!(server.source_address.as_deref(), Some("192.0.2.1"));
284 assert!(server.single_connection);
285 assert_eq!(server.routing_instance, None);
286 }
287
288 #[test]
289 fn authentication_settings_are_optional() {
290 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
291 assert!(system.authentication_order.is_empty());
292 assert!(system.radius_servers.is_empty());
293 assert!(system.tacplus_servers.is_empty());
294 }
295}