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//! - [moduli](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/moduli-edit-system-services-ssh.html)
8//! - [hostkey-algorithm-list](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-edit-ssh-hostkey-algorithm-list.html)
9
10use crate::{arg_of, args_of, child_block, has_leaf, list_values, statements};
11use asterust::config::ConfigLine;
12
13/// The `moduli` block: how Diffie-Hellman prime moduli are generated for group exchange.
14#[derive(Clone, Debug, Default, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct SshModuli {
17    /// `designer` or `safe`, from `type`.
18    pub modulus_type: Option<String>,
19    /// `monthly` or `weekly`.
20    pub refresh: Option<String>,
21    /// Number of moduli to generate.
22    pub count: Option<String>,
23}
24
25/// The `system services ssh` settings; every field is `None` or empty when only `ssh;` is written.
26#[derive(Clone, Debug, Default, PartialEq, Eq)]
27#[non_exhaustive]
28pub struct SshService {
29    /// `allow`, `deny` or `deny-password`.
30    pub root_login: Option<String>,
31    pub protocol_versions: Vec<String>,
32    pub port: Option<String>,
33    pub connection_limit: Option<String>,
34    pub rate_limit: Option<String>,
35    pub ciphers: Vec<String>,
36    pub sftp_server: bool,
37    /// Methods tried in order: `password`, `radius`, `tacplus`.
38    pub authentication_order: Vec<String>,
39    pub key_exchange: Vec<String>,
40    pub macs: Vec<String>,
41    pub client_alive_count_max: Option<String>,
42    pub client_alive_interval: Option<String>,
43    /// `md5` or `sha2-256`.
44    pub fingerprint_hash: Option<String>,
45    pub max_sessions_per_connection: Option<String>,
46    pub max_pre_authentication_packets: Option<String>,
47    pub no_challenge_response: bool,
48    pub no_password_authentication: bool,
49    pub no_passwords: bool,
50    pub no_public_keys: bool,
51    /// The `hostkey-algorithm` statements, e.g. `ssh-ed25519`.
52    pub hostkey_algorithms: Vec<String>,
53    /// The algorithms of `hostkey-algorithm-list { ... }`, e.g. `ed25519` or `rsa-sha2-512`.
54    pub hostkey_algorithm_list: Vec<String>,
55    pub moduli: Option<SshModuli>,
56}
57
58pub(super) fn parse_ssh(ssh: &ConfigLine) -> SshService {
59    let list = |keyword: &str| args_of(ssh, keyword).into_iter().flat_map(list_values).collect();
60    let field = |keyword: &str| arg_of(ssh, keyword).map(str::to_string);
61    SshService {
62        root_login: field("root-login"),
63        protocol_versions: list("protocol-version"),
64        port: field("port"),
65        connection_limit: field("connection-limit"),
66        rate_limit: field("rate-limit"),
67        ciphers: list("ciphers"),
68        sftp_server: has_leaf(ssh, "sftp-server"),
69        authentication_order: list("authentication-order"),
70        key_exchange: list("key-exchange"),
71        macs: list("macs"),
72        client_alive_count_max: field("client-alive-count-max"),
73        client_alive_interval: field("client-alive-interval"),
74        fingerprint_hash: field("fingerprint-hash"),
75        max_sessions_per_connection: field("max-sessions-per-connection"),
76        max_pre_authentication_packets: field("max-pre-authentication-packets"),
77        no_challenge_response: has_leaf(ssh, "no-challenge-response"),
78        no_password_authentication: has_leaf(ssh, "no-password-authentication"),
79        no_passwords: has_leaf(ssh, "no-passwords"),
80        no_public_keys: has_leaf(ssh, "no-public-keys"),
81        hostkey_algorithms: list("hostkey-algorithm"),
82        hostkey_algorithm_list: statements(ssh, "hostkey-algorithm-list")
83            .into_iter()
84            .flat_map(list_values)
85            .collect(),
86        moduli: child_block(ssh, "moduli").map(|moduli| SshModuli {
87            modulus_type: arg_of(moduli, "type").map(str::to_string),
88            refresh: arg_of(moduli, "refresh").map(str::to_string),
89            count: arg_of(moduli, "count").map(str::to_string),
90        }),
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use crate::system::system;
97    use asterust::config::juniper;
98
99    #[test]
100    fn extracts_more_ssh_settings() {
101        let ssh = system(&juniper::parse_braces(
102            r#"
103system {
104    services {
105        ssh {
106            authentication-order [ tacplus password ];
107            key-exchange [ curve25519-sha256 ecdh-sha2-nistp256 ];
108            macs [ hmac-sha2-256 hmac-sha2-512 ];
109            client-alive-count-max 3;
110            client-alive-interval 60;
111            fingerprint-hash sha2-256;
112            max-sessions-per-connection 5;
113            max-pre-authentication-packets 64;
114            no-challenge-response;
115            no-password-authentication;
116            no-public-keys;
117        }
118    }
119}
120"#,
121        ))
122        .unwrap()
123        .services
124        .unwrap()
125        .ssh
126        .unwrap();
127        assert_eq!(ssh.authentication_order, ["tacplus", "password"]);
128        assert_eq!(ssh.key_exchange, ["curve25519-sha256", "ecdh-sha2-nistp256"]);
129        assert_eq!(ssh.macs, ["hmac-sha2-256", "hmac-sha2-512"]);
130        assert_eq!(ssh.client_alive_count_max.as_deref(), Some("3"));
131        assert_eq!(ssh.client_alive_interval.as_deref(), Some("60"));
132        assert_eq!(ssh.fingerprint_hash.as_deref(), Some("sha2-256"));
133        assert_eq!(ssh.max_sessions_per_connection.as_deref(), Some("5"));
134        assert_eq!(ssh.max_pre_authentication_packets.as_deref(), Some("64"));
135        assert!(ssh.no_challenge_response);
136        assert!(ssh.no_password_authentication);
137        assert!(ssh.no_public_keys);
138        assert!(!ssh.no_passwords);
139    }
140
141    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
142    #[test]
143    fn extracts_ssh_hostkey_algorithms() {
144        let ssh = system(&juniper::parse_braces(
145            "system {\n    services {\n        ssh {\n            hostkey-algorithm ssh-ed25519;\n            hostkey-algorithm-list {\n                ed25519;\n                rsa-sha2-512;\n            }\n        }\n    }\n}\n",
146        ))
147        .unwrap()
148        .services
149        .unwrap()
150        .ssh
151        .unwrap();
152        assert_eq!(ssh.hostkey_algorithms, ["ssh-ed25519"]);
153        assert_eq!(ssh.hostkey_algorithm_list, ["ed25519", "rsa-sha2-512"]);
154    }
155
156    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
157    #[test]
158    fn extracts_ssh_moduli() {
159        let ssh = system(&juniper::parse_braces(
160            "system {\n    services {\n        ssh {\n            moduli {\n                type safe;\n                refresh weekly;\n                count 5;\n            }\n        }\n    }\n}\n",
161        ))
162        .unwrap()
163        .services
164        .unwrap()
165        .ssh
166        .unwrap();
167        let moduli = ssh.moduli.unwrap();
168        assert_eq!(moduli.modulus_type.as_deref(), Some("safe"));
169        assert_eq!(moduli.refresh.as_deref(), Some("weekly"));
170        assert_eq!(moduli.count.as_deref(), Some("5"));
171    }
172}