1use crate::{active_children, arg_of, args_of, child_block, has_leaf, list_values, named_children};
4use asterust::config::ConfigLine;
5
6#[derive(Clone, Debug, Default, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct SshService {
10 pub root_login: Option<String>,
12 pub protocol_versions: Vec<String>,
13 pub port: Option<String>,
14 pub connection_limit: Option<String>,
15 pub rate_limit: Option<String>,
16 pub ciphers: Vec<String>,
17 pub sftp_server: bool,
18 pub authentication_order: Vec<String>,
20 pub key_exchange: Vec<String>,
21 pub macs: Vec<String>,
22 pub client_alive_count_max: Option<String>,
23 pub client_alive_interval: Option<String>,
24 pub fingerprint_hash: Option<String>,
26 pub max_sessions_per_connection: Option<String>,
27 pub max_pre_authentication_packets: Option<String>,
28 pub no_challenge_response: bool,
29 pub no_password_authentication: bool,
30 pub no_passwords: bool,
31 pub no_public_keys: bool,
32}
33
34#[derive(Clone, Debug, PartialEq, Eq)]
36#[non_exhaustive]
37pub struct OutboundSshServer {
38 pub address: String,
39 pub port: Option<String>,
40 pub retry: Option<String>,
41 pub timeout: Option<String>,
42}
43
44#[derive(Clone, Debug, PartialEq, Eq)]
46#[non_exhaustive]
47pub struct OutboundSshClient {
48 pub id: String,
49 pub device_id: Option<String>,
50 pub servers: Vec<OutboundSshServer>,
51 pub services: Vec<String>,
53 pub reconnect_strategy: Option<String>,
55 pub routing_instance: Option<String>,
56}
57
58#[derive(Clone, Debug, PartialEq, Eq)]
60#[non_exhaustive]
61pub struct Services {
62 pub ssh: Option<SshService>,
63 pub telnet: bool,
64 pub ftp: bool,
65 pub netconf_ssh: bool,
67 pub http: bool,
69 pub https: bool,
71 pub outbound_ssh: Vec<OutboundSshClient>,
72}
73
74pub(super) fn parse_services(services: &ConfigLine) -> Services {
75 let web = child_block(services, "web-management");
76 let netconf = child_block(services, "netconf");
77 Services {
78 ssh: child_block(services, "ssh").map(parse_ssh),
79 telnet: child_block(services, "telnet").is_some(),
80 ftp: child_block(services, "ftp").is_some(),
81 netconf_ssh: netconf.is_some_and(|n| child_block(n, "ssh").is_some()),
82 http: web.is_some_and(|w| child_block(w, "http").is_some()),
83 https: web.is_some_and(|w| child_block(w, "https").is_some()),
84 outbound_ssh: child_block(services, "outbound-ssh")
85 .map(|outbound| {
86 named_children(outbound, "client")
87 .map(|(id, client)| parse_outbound_client(id, client))
88 .collect()
89 })
90 .unwrap_or_default(),
91 }
92}
93
94fn parse_outbound_client(id: &str, client: &ConfigLine) -> OutboundSshClient {
95 OutboundSshClient {
96 id: id.to_string(),
97 device_id: arg_of(client, "device-id").map(str::to_string),
98 servers: active_children(client)
99 .filter(|c| !c.text.contains(' ') && !matches!(c.text.as_str(), "keep-alive" | "proxy-server"))
101 .map(|server| OutboundSshServer {
102 address: server.text.clone(),
103 port: arg_of(server, "port").map(str::to_string),
104 retry: arg_of(server, "retry").map(str::to_string),
105 timeout: arg_of(server, "timeout").map(str::to_string),
106 })
107 .collect(),
108 services: args_of(client, "services").into_iter().flat_map(list_values).collect(),
109 reconnect_strategy: arg_of(client, "reconnect-strategy").map(str::to_string),
110 routing_instance: arg_of(client, "routing-instance").map(str::to_string),
111 }
112}
113
114fn parse_ssh(ssh: &ConfigLine) -> SshService {
115 let list = |keyword: &str| args_of(ssh, keyword).into_iter().flat_map(list_values).collect();
116 let field = |keyword: &str| arg_of(ssh, keyword).map(str::to_string);
117 SshService {
118 root_login: field("root-login"),
119 protocol_versions: list("protocol-version"),
120 port: field("port"),
121 connection_limit: field("connection-limit"),
122 rate_limit: field("rate-limit"),
123 ciphers: list("ciphers"),
124 sftp_server: has_leaf(ssh, "sftp-server"),
125 authentication_order: list("authentication-order"),
126 key_exchange: list("key-exchange"),
127 macs: list("macs"),
128 client_alive_count_max: field("client-alive-count-max"),
129 client_alive_interval: field("client-alive-interval"),
130 fingerprint_hash: field("fingerprint-hash"),
131 max_sessions_per_connection: field("max-sessions-per-connection"),
132 max_pre_authentication_packets: field("max-pre-authentication-packets"),
133 no_challenge_response: has_leaf(ssh, "no-challenge-response"),
134 no_password_authentication: has_leaf(ssh, "no-password-authentication"),
135 no_passwords: has_leaf(ssh, "no-passwords"),
136 no_public_keys: has_leaf(ssh, "no-public-keys"),
137 }
138}
139
140#[cfg(test)]
141mod tests {
142 use super::*;
143 use crate::system::system;
144 use asterust::config::juniper;
145
146 const SERVICES: &str = r#"
147system {
148 services {
149 ssh {
150 root-login deny;
151 protocol-version v2;
152 port 2222;
153 connection-limit 10;
154 rate-limit 5;
155 ciphers [ aes256-ctr aes128-ctr ];
156 sftp-server;
157 }
158 netconf {
159 ssh;
160 }
161 web-management {
162 https {
163 system-generated-certificate;
164 }
165 }
166 telnet;
167 }
168}
169"#;
170
171 #[test]
172 fn extracts_ssh_service_settings() {
173 let services = system(&juniper::parse_braces(SERVICES)).unwrap().services.unwrap();
174 let ssh = services.ssh.unwrap();
175 assert_eq!(ssh.root_login.as_deref(), Some("deny"));
176 assert_eq!(ssh.protocol_versions, ["v2"]);
177 assert_eq!(ssh.port.as_deref(), Some("2222"));
178 assert_eq!(ssh.connection_limit.as_deref(), Some("10"));
179 assert_eq!(ssh.rate_limit.as_deref(), Some("5"));
180 assert_eq!(ssh.ciphers, ["aes256-ctr", "aes128-ctr"]);
181 assert!(ssh.sftp_server);
182 }
183
184 #[test]
185 fn extracts_enabled_services() {
186 let services = system(&juniper::parse_braces(SERVICES)).unwrap().services.unwrap();
187 assert!(services.telnet);
188 assert!(services.netconf_ssh);
189 assert!(services.https);
190 assert!(!services.http);
191 assert!(!services.ftp);
192 }
193
194 #[test]
195 fn bare_service_statements_enable_with_defaults() {
196 let services = system(&juniper::parse_braces(
197 "system {\n services {\n ssh;\n ftp;\n web-management {\n http;\n }\n }\n}\n",
198 ))
199 .unwrap()
200 .services
201 .unwrap();
202 assert_eq!(services.ssh, Some(SshService::default()));
203 assert!(services.ftp);
204 assert!(services.http);
205 assert!(!services.telnet);
206 assert!(!services.netconf_ssh);
207 }
208
209 #[test]
210 fn disabled_service_is_not_reported() {
211 let services = system(&juniper::parse_braces(
212 "system {\n services {\n inactive: telnet;\n ssh;\n }\n}\n",
213 ))
214 .unwrap()
215 .services
216 .unwrap();
217 assert!(!services.telnet);
218 }
219
220 #[test]
221 fn extracts_outbound_ssh_clients() {
222 let services = system(&juniper::parse_braces(
223 r#"
224system {
225 services {
226 outbound-ssh {
227 client mgmt {
228 device-id pe1;
229 secret "$9$secret"; ## SECRET-DATA
230 reconnect-strategy sticky;
231 routing-instance mgmt_junos;
232 services netconf;
233 192.0.2.50 {
234 port 7804;
235 retry 5;
236 timeout 20;
237 }
238 }
239 client backup {
240 192.0.2.60;
241 keep-alive {
242 retry 3;
243 timeout 15;
244 }
245 }
246 }
247 }
248}
249"#,
250 ))
251 .unwrap()
252 .services
253 .unwrap();
254 assert_eq!(services.outbound_ssh.len(), 2);
255
256 let mgmt = &services.outbound_ssh[0];
257 assert_eq!(mgmt.id, "mgmt");
258 assert_eq!(mgmt.device_id.as_deref(), Some("pe1"));
259 assert_eq!(mgmt.reconnect_strategy.as_deref(), Some("sticky"));
260 assert_eq!(mgmt.routing_instance.as_deref(), Some("mgmt_junos"));
261 assert_eq!(mgmt.services, ["netconf"]);
262 assert_eq!(mgmt.servers.len(), 1);
263 assert_eq!(mgmt.servers[0].address, "192.0.2.50");
264 assert_eq!(mgmt.servers[0].port.as_deref(), Some("7804"));
265 assert_eq!(mgmt.servers[0].retry.as_deref(), Some("5"));
266 assert_eq!(mgmt.servers[0].timeout.as_deref(), Some("20"));
267
268 let backup = &services.outbound_ssh[1];
269 assert_eq!(backup.id, "backup");
270 assert_eq!(backup.servers.len(), 1);
272 assert_eq!(backup.servers[0].address, "192.0.2.60");
273 assert_eq!(backup.servers[0].port, None);
274 }
275
276 #[test]
277 fn no_outbound_ssh_is_empty() {
278 let services = system(&juniper::parse_braces(
279 "system {\n services {\n ssh;\n }\n}\n",
280 ))
281 .unwrap()
282 .services
283 .unwrap();
284 assert!(services.outbound_ssh.is_empty());
285 }
286
287 #[test]
288 fn extracts_more_ssh_settings() {
289 let ssh = system(&juniper::parse_braces(
290 r#"
291system {
292 services {
293 ssh {
294 authentication-order [ tacplus password ];
295 key-exchange [ curve25519-sha256 ecdh-sha2-nistp256 ];
296 macs [ hmac-sha2-256 hmac-sha2-512 ];
297 client-alive-count-max 3;
298 client-alive-interval 60;
299 fingerprint-hash sha2-256;
300 max-sessions-per-connection 5;
301 max-pre-authentication-packets 64;
302 no-challenge-response;
303 no-password-authentication;
304 no-public-keys;
305 }
306 }
307}
308"#,
309 ))
310 .unwrap()
311 .services
312 .unwrap()
313 .ssh
314 .unwrap();
315 assert_eq!(ssh.authentication_order, ["tacplus", "password"]);
316 assert_eq!(ssh.key_exchange, ["curve25519-sha256", "ecdh-sha2-nistp256"]);
317 assert_eq!(ssh.macs, ["hmac-sha2-256", "hmac-sha2-512"]);
318 assert_eq!(ssh.client_alive_count_max.as_deref(), Some("3"));
319 assert_eq!(ssh.client_alive_interval.as_deref(), Some("60"));
320 assert_eq!(ssh.fingerprint_hash.as_deref(), Some("sha2-256"));
321 assert_eq!(ssh.max_sessions_per_connection.as_deref(), Some("5"));
322 assert_eq!(ssh.max_pre_authentication_packets.as_deref(), Some("64"));
323 assert!(ssh.no_challenge_response);
324 assert!(ssh.no_password_authentication);
325 assert!(ssh.no_public_keys);
326 assert!(!ssh.no_passwords);
327 }
328
329 #[test]
330 fn no_services_is_none() {
331 assert!(system(&juniper::parse_braces("system {\n host-name pe1;\n}\n"))
332 .unwrap()
333 .services
334 .is_none());
335 }
336}