Skip to main content

asterust_junos/system/services/
ssh.rs

1//! `ssh`
2//! `system { services { ssh { ... } } }`.
3//!
4//! # See also
5//!
6//! - [ssh (System Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ssh-edit-system.html)
7
8use crate::{arg_of, args_of, has_leaf, list_values};
9use asterust::config::ConfigLine;
10
11/// The `system services ssh` settings; every field is `None` or empty when only `ssh;` is written.
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct SshService {
15    /// `allow`, `deny` or `deny-password`.
16    pub root_login: Option<String>,
17    pub protocol_versions: Vec<String>,
18    pub port: Option<String>,
19    pub connection_limit: Option<String>,
20    pub rate_limit: Option<String>,
21    pub ciphers: Vec<String>,
22    pub sftp_server: bool,
23    /// Methods tried in order: `password`, `radius`, `tacplus`.
24    pub authentication_order: Vec<String>,
25    pub key_exchange: Vec<String>,
26    pub macs: Vec<String>,
27    pub client_alive_count_max: Option<String>,
28    pub client_alive_interval: Option<String>,
29    /// `md5` or `sha2-256`.
30    pub fingerprint_hash: Option<String>,
31    pub max_sessions_per_connection: Option<String>,
32    pub max_pre_authentication_packets: Option<String>,
33    pub no_challenge_response: bool,
34    pub no_password_authentication: bool,
35    pub no_passwords: bool,
36    pub no_public_keys: bool,
37}
38
39pub(super) fn parse_ssh(ssh: &ConfigLine) -> SshService {
40    let list = |keyword: &str| args_of(ssh, keyword).into_iter().flat_map(list_values).collect();
41    let field = |keyword: &str| arg_of(ssh, keyword).map(str::to_string);
42    SshService {
43        root_login: field("root-login"),
44        protocol_versions: list("protocol-version"),
45        port: field("port"),
46        connection_limit: field("connection-limit"),
47        rate_limit: field("rate-limit"),
48        ciphers: list("ciphers"),
49        sftp_server: has_leaf(ssh, "sftp-server"),
50        authentication_order: list("authentication-order"),
51        key_exchange: list("key-exchange"),
52        macs: list("macs"),
53        client_alive_count_max: field("client-alive-count-max"),
54        client_alive_interval: field("client-alive-interval"),
55        fingerprint_hash: field("fingerprint-hash"),
56        max_sessions_per_connection: field("max-sessions-per-connection"),
57        max_pre_authentication_packets: field("max-pre-authentication-packets"),
58        no_challenge_response: has_leaf(ssh, "no-challenge-response"),
59        no_password_authentication: has_leaf(ssh, "no-password-authentication"),
60        no_passwords: has_leaf(ssh, "no-passwords"),
61        no_public_keys: has_leaf(ssh, "no-public-keys"),
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use crate::system::system;
68    use asterust::config::juniper;
69
70    #[test]
71    fn extracts_more_ssh_settings() {
72        let ssh = system(&juniper::parse_braces(
73            r#"
74system {
75    services {
76        ssh {
77            authentication-order [ tacplus password ];
78            key-exchange [ curve25519-sha256 ecdh-sha2-nistp256 ];
79            macs [ hmac-sha2-256 hmac-sha2-512 ];
80            client-alive-count-max 3;
81            client-alive-interval 60;
82            fingerprint-hash sha2-256;
83            max-sessions-per-connection 5;
84            max-pre-authentication-packets 64;
85            no-challenge-response;
86            no-password-authentication;
87            no-public-keys;
88        }
89    }
90}
91"#,
92        ))
93        .unwrap()
94        .services
95        .unwrap()
96        .ssh
97        .unwrap();
98        assert_eq!(ssh.authentication_order, ["tacplus", "password"]);
99        assert_eq!(ssh.key_exchange, ["curve25519-sha256", "ecdh-sha2-nistp256"]);
100        assert_eq!(ssh.macs, ["hmac-sha2-256", "hmac-sha2-512"]);
101        assert_eq!(ssh.client_alive_count_max.as_deref(), Some("3"));
102        assert_eq!(ssh.client_alive_interval.as_deref(), Some("60"));
103        assert_eq!(ssh.fingerprint_hash.as_deref(), Some("sha2-256"));
104        assert_eq!(ssh.max_sessions_per_connection.as_deref(), Some("5"));
105        assert_eq!(ssh.max_pre_authentication_packets.as_deref(), Some("64"));
106        assert!(ssh.no_challenge_response);
107        assert!(ssh.no_password_authentication);
108        assert!(ssh.no_public_keys);
109        assert!(!ssh.no_passwords);
110    }
111}