Skip to main content

asterust_junos/interfaces/
ppp_options.rs

1//! `ppp-options`
2//! `interfaces { <name> { ppp-options { ... } } }` and `interfaces { <name> { unit <n> { ppp-options { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [ppp-options](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ppp-options-edit-interfaces.html)
7//! - [peer-ip-address-optional](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/peer-ip-address-optional-edit-dynamic-profiles.html)
8//! - [initiate-ncp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/initiate-ncp-edit-ppp-options.html)
9//! - [on-demand-ip-address](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/on-demand-ip-address-edit-dynamic-profiles.html)
10//! - [chap](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/chap-edit-interfaces.html)
11//! - [pap](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pap-edit-interfaces.html)
12//! - [compression (PPP Properties)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/compression-edit-interfaces-ppp-options.html)
13//! - [no-termination-request](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-termination-request-edit-interfaces.html)
14
15use crate::{arg_of, child_block, has_leaf, list_values, secret_value, statement_words, value_after};
16use asterust::config::ConfigLine;
17
18/// The `chap` authentication of the PPP options.
19#[derive(Clone, Debug, PartialEq, Eq)]
20#[non_exhaustive]
21pub struct PppChap {
22    pub access_profile: Option<String>,
23    pub challenge_length_minimum: Option<String>,
24    pub challenge_length_maximum: Option<String>,
25    /// A `default-chap-secret` is configured.
26    pub has_default_chap_secret: bool,
27    /// The `default-chap-secret`; `None` unless the `secrets` feature is enabled.
28    pub default_chap_secret: Option<crate::Secret>,
29    pub local_name: Option<String>,
30    pub passive: bool,
31}
32
33/// The `pap` authentication of the PPP options.
34#[derive(Clone, Debug, PartialEq, Eq)]
35#[non_exhaustive]
36pub struct PppPap {
37    pub access_profile: Option<String>,
38    /// A `default-pap-password` is configured.
39    pub has_default_pap_password: bool,
40    /// The `default-pap-password`; `None` unless the `secrets` feature is enabled.
41    pub default_pap_password: Option<crate::Secret>,
42    pub local_name: Option<String>,
43    /// A `local-password` is configured.
44    pub has_local_password: bool,
45    /// The `local-password`; `None` unless the `secrets` feature is enabled.
46    pub local_password: Option<crate::Secret>,
47    pub passive: bool,
48}
49
50/// The `compression` of the PPP options.
51#[derive(Clone, Debug, PartialEq, Eq)]
52#[non_exhaustive]
53pub struct PppCompression {
54    /// Address and control field compression.
55    pub acfc: bool,
56    /// Protocol field compression.
57    pub pfc: bool,
58}
59
60/// The `ppp-options` of an interface.
61#[derive(Clone, Debug, PartialEq, Eq)]
62#[non_exhaustive]
63pub struct PppOptions {
64    /// The protocols of `authentication [ ... ]`, e.g. `chap` and `pap`.
65    pub authentication: Vec<String>,
66    pub mru: Option<String>,
67    /// A size or `use-lower-layer`.
68    pub mtu: Option<String>,
69    pub chap: Option<PppChap>,
70    pub pap: Option<PppPap>,
71    pub compression: Option<PppCompression>,
72    pub dynamic_profile: Option<String>,
73    /// `ip`, `ipv6` or `dual-stack-passive`.
74    pub initiate_ncp: Option<String>,
75    pub ipcp_suggest_dns_option: bool,
76    pub lcp_max_conf_req: Option<String>,
77    /// Milliseconds.
78    pub lcp_restart_timer: Option<String>,
79    /// Seconds.
80    pub loopback_clear_timer: Option<String>,
81    pub ncp_max_conf_req: Option<String>,
82    /// Milliseconds.
83    pub ncp_restart_timer: Option<String>,
84    pub on_demand_ip_address: bool,
85    pub peer_ip_address_optional: bool,
86    pub no_termination_request: bool,
87}
88
89fn parse_chap(chap: &ConfigLine) -> PppChap {
90    let length = statement_words(chap, "challenge-length");
91    PppChap {
92        access_profile: arg_of(chap, "access-profile").map(str::to_string),
93        challenge_length_minimum: length.as_ref().and_then(|w| value_after(w, "minimum")),
94        challenge_length_maximum: length.as_ref().and_then(|w| value_after(w, "maximum")),
95        has_default_chap_secret: arg_of(chap, "default-chap-secret").is_some(),
96        default_chap_secret: secret_value(arg_of(chap, "default-chap-secret")),
97        local_name: arg_of(chap, "local-name").map(str::to_string),
98        passive: has_leaf(chap, "passive"),
99    }
100}
101
102fn parse_pap(pap: &ConfigLine) -> PppPap {
103    PppPap {
104        access_profile: arg_of(pap, "access-profile").map(str::to_string),
105        has_default_pap_password: arg_of(pap, "default-pap-password").is_some(),
106        default_pap_password: secret_value(arg_of(pap, "default-pap-password")),
107        local_name: arg_of(pap, "local-name").map(str::to_string),
108        has_local_password: arg_of(pap, "local-password").is_some(),
109        local_password: secret_value(arg_of(pap, "local-password")),
110        passive: has_leaf(pap, "passive"),
111    }
112}
113
114pub(in crate::interfaces) fn parse_ppp_options(options: &ConfigLine) -> PppOptions {
115    let value = |keyword| arg_of(options, keyword).map(str::to_string);
116    let initiate = statement_words(options, "initiate-ncp");
117    PppOptions {
118        authentication: arg_of(options, "authentication").map(list_values).unwrap_or_default(),
119        mru: value("mru"),
120        mtu: value("mtu"),
121        chap: child_block(options, "chap").map(parse_chap),
122        pap: child_block(options, "pap").map(parse_pap),
123        compression: child_block(options, "compression").map(|c| PppCompression {
124            acfc: has_leaf(c, "acfc"),
125            pfc: has_leaf(c, "pfc"),
126        }),
127        dynamic_profile: value("dynamic-profile"),
128        initiate_ncp: initiate.as_ref().and_then(|w| w.first()).map(|w| w.to_string()),
129        ipcp_suggest_dns_option: initiate
130            .as_ref()
131            .is_some_and(|w| w.contains(&"ipcp-suggest-dns-option")),
132        lcp_max_conf_req: value("lcp-max-conf-req"),
133        lcp_restart_timer: value("lcp-restart-timer"),
134        loopback_clear_timer: value("loopback-clear-timer"),
135        ncp_max_conf_req: value("ncp-max-conf-req"),
136        ncp_restart_timer: value("ncp-restart-timer"),
137        on_demand_ip_address: has_leaf(options, "on-demand-ip-address"),
138        peer_ip_address_optional: has_leaf(options, "peer-ip-address-optional"),
139        no_termination_request: has_leaf(options, "no-termination-request"),
140    }
141}
142
143#[cfg(test)]
144mod tests {
145    use crate::interfaces::interfaces;
146    use crate::secret::expected;
147    use asterust::config::juniper;
148
149    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
150    #[test]
151    fn extracts_ppp_options() {
152        let ifaces = interfaces(&juniper::parse_braces(
153            r#"
154interfaces {
155    so-0/0/0 {
156        ppp-options {
157            authentication [ chap pap ];
158            mru 1500;
159            mtu use-lower-layer;
160            dynamic-profile ppp-prof;
161            initiate-ncp ip ipcp-suggest-dns-option;
162            lcp-max-conf-req 10;
163            lcp-restart-timer 3000;
164            loopback-clear-timer 20;
165            ncp-max-conf-req 8;
166            ncp-restart-timer 1500;
167            on-demand-ip-address;
168            no-termination-request;
169            compression {
170                acfc;
171            }
172            chap {
173                access-profile radius-prof;
174                challenge-length minimum 16 maximum 32;
175                default-chap-secret "$9$chap";
176                local-name r1;
177                passive;
178            }
179            pap {
180                access-profile pap-prof;
181                default-pap-password "$9$pap";
182                local-name r2;
183                local-password "$9$local";
184            }
185        }
186    }
187    so-0/0/1 {
188        ppp-options {
189            initiate-ncp dual-stack-passive;
190        }
191    }
192}
193"#,
194        ));
195        let ppp = ifaces[0].ppp_options.as_ref().unwrap();
196        assert_eq!(ppp.authentication, ["chap", "pap"]);
197        assert_eq!(ppp.mru.as_deref(), Some("1500"));
198        assert_eq!(ppp.mtu.as_deref(), Some("use-lower-layer"));
199        assert_eq!(ppp.dynamic_profile.as_deref(), Some("ppp-prof"));
200        assert_eq!(ppp.initiate_ncp.as_deref(), Some("ip"));
201        assert!(ppp.ipcp_suggest_dns_option);
202        assert_eq!(ppp.lcp_max_conf_req.as_deref(), Some("10"));
203        assert_eq!(ppp.lcp_restart_timer.as_deref(), Some("3000"));
204        assert_eq!(ppp.loopback_clear_timer.as_deref(), Some("20"));
205        assert_eq!(ppp.ncp_max_conf_req.as_deref(), Some("8"));
206        assert_eq!(ppp.ncp_restart_timer.as_deref(), Some("1500"));
207        assert!(ppp.on_demand_ip_address && ppp.no_termination_request);
208        let compression = ppp.compression.as_ref().unwrap();
209        assert!(compression.acfc && !compression.pfc);
210        let chap = ppp.chap.as_ref().unwrap();
211        assert_eq!(chap.access_profile.as_deref(), Some("radius-prof"));
212        assert_eq!(chap.challenge_length_minimum.as_deref(), Some("16"));
213        assert_eq!(chap.challenge_length_maximum.as_deref(), Some("32"));
214        assert!(chap.has_default_chap_secret);
215        assert_eq!(
216            chap.default_chap_secret.as_ref().map(crate::Secret::expose),
217            expected("$9$chap")
218        );
219        assert_eq!(chap.local_name.as_deref(), Some("r1"));
220        assert!(chap.passive);
221        let pap = ppp.pap.as_ref().unwrap();
222        assert_eq!(pap.access_profile.as_deref(), Some("pap-prof"));
223        assert!(pap.has_default_pap_password && pap.has_local_password);
224        assert_eq!(
225            pap.default_pap_password.as_ref().map(crate::Secret::expose),
226            expected("$9$pap")
227        );
228        assert_eq!(
229            pap.local_password.as_ref().map(crate::Secret::expose),
230            expected("$9$local")
231        );
232        assert_eq!(pap.local_name.as_deref(), Some("r2"));
233        assert!(!pap.passive);
234
235        let minimal = ifaces[1].ppp_options.as_ref().unwrap();
236        assert_eq!(minimal.initiate_ncp.as_deref(), Some("dual-stack-passive"));
237        assert!(!minimal.ipcp_suggest_dns_option && minimal.chap.is_none() && minimal.pap.is_none());
238        assert!(minimal.authentication.is_empty() && minimal.compression.is_none());
239    }
240}