Skip to main content

asterust_junos/system/
mod.rs

1//! `system`
2//! `system { ... }`.
3//!
4//! Each statement with children of its own lives in a submodule named after it.
5//!
6//! # See also
7//!
8//! - [`[edit system]` hierarchy](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/topic-map/hierarchy-edit-system.html)
9//! - [host-name (System)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/host-name-edit-system.html)
10//! - [domain-name (System)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/domain-name-edit-system.html)
11//! - [time-zone](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/time-zone-edit-system.html)
12//! - [domain-search](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/domain-search-edit-system.html)
13//! - [authentication-order (System)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/authentication-order-edit-system.html)
14//! - [max-configurations-on-flash (System)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/max-configurations-on-flash-edit-system.html)
15//! - [no-redirects (IPv4 Traffic)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-redirects-edit-system.html)
16//! - [default-address-selection](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/default-address-selection-edit-system.html)
17//! - [diag-port-authentication](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/diag-port-authentication-edit-system.html)
18//! - [pic-console-authentication](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pic-console-authentication-edit-system.html)
19
20mod accounting;
21mod archival;
22mod autoinstallation;
23mod backup_router;
24mod commit;
25mod dynamic_profile_options;
26mod extensions;
27mod internet_options;
28mod license;
29mod location;
30mod login;
31mod master_password;
32mod name_server;
33mod ntp;
34mod pixel_tracing;
35mod ports;
36mod processes;
37mod radius_server;
38mod root_authentication;
39mod schema;
40mod scripts;
41mod services;
42mod static_host_mapping;
43mod syslog;
44mod tacplus_server;
45
46pub use accounting::{Accounting, AccountingDestination};
47pub use archival::{Archival, ArchivalConfiguration};
48pub use autoinstallation::{Autoinstallation, AutoinstallationInterface};
49pub use backup_router::BackupRouter;
50pub use commit::Commit;
51pub use dynamic_profile_options::DynamicProfileOptions;
52pub use extensions::{ExtensionApplication, ExtensionProvider, Extensions, ProviderLicense};
53pub use internet_options::{IcmpRateLimit, InternetOptions};
54pub use license::{License, LicenseAutoupdate, LicenseRenew};
55pub use location::Location;
56pub use login::{Login, LoginClass, LoginPassword, LoginRetryOptions, LoginUser};
57pub use master_password::MasterPassword;
58pub use name_server::NameServer;
59pub use ntp::{Ntp, NtpAuthenticationKey, NtpHost};
60pub use pixel_tracing::PixelTracing;
61pub use ports::{Auxiliary, Console, Ports};
62pub use processes::Process;
63pub use radius_server::RadiusServer;
64pub use root_authentication::RootAuthentication;
65pub use schema::Schema;
66pub use scripts::Scripts;
67pub use services::{
68    AaclStatisticsProfile, DhcpLocalServer, DhcpLocalServerGroup, FlowTapDtcp, FlowTapDtcpSsh,
69    LocalPolicyDecisionFunction, Netconf, NetconfSsh, OutboundSshClient, OutboundSshServer, RedundancyInterface,
70    ServiceDeployment, ServiceDeploymentServer, Services, SshService, StaticSubscriberGroup, StaticSubscriberProfile,
71    StaticSubscribers, SubscriberManagement, SubscriberRedundancy, WebManagement, WebManagementHttp,
72    WebManagementHttps, XnmClearText, XnmSsl,
73};
74pub use static_host_mapping::StaticHostMapping;
75pub use syslog::{Syslog, SyslogArchive, SyslogHost, SyslogRule, SyslogStructuredData, SyslogTarget};
76pub use tacplus_server::TacplusServer;
77
78use crate::{active_children, arg_of, args_of, child_block, has_leaf, list_values, split_name, top_level, unquote};
79use accounting::parse_accounting;
80use archival::parse_archival;
81use asterust::config::{Config, ConfigLine};
82use autoinstallation::parse_autoinstallation;
83use backup_router::parse_backup_router;
84use commit::parse_commit;
85use dynamic_profile_options::parse_dynamic_profile_options;
86use extensions::parse_extensions;
87use internet_options::parse_internet_options;
88use license::parse_license;
89use location::parse_location;
90use login::parse_login;
91use master_password::parse_master_password;
92use name_server::name_servers;
93use ntp::parse_ntp;
94use pixel_tracing::parse_pixel_tracing;
95use ports::parse_ports;
96use processes::parse_processes;
97use radius_server::radius_servers;
98use root_authentication::root_authentication;
99use schema::parse_schema;
100use scripts::parse_scripts;
101use services::parse_services;
102use static_host_mapping::static_host_mappings;
103use syslog::parse_syslog;
104use tacplus_server::tacplus_servers;
105
106/// The top-level `system` configuration.
107#[derive(Clone, Debug, PartialEq, Eq)]
108#[non_exhaustive]
109pub struct System {
110    pub host_name: Option<String>,
111    pub domain_name: Option<String>,
112    /// e.g. `"UTC"` or `"America/New_York"`.
113    pub time_zone: Option<String>,
114    pub domain_search: Vec<String>,
115    pub static_host_mappings: Vec<StaticHostMapping>,
116    pub backup_router: Option<BackupRouter>,
117    pub inet6_backup_router: Option<BackupRouter>,
118    /// Methods in the order tried: `password`, `radius`, `tacplus`.
119    pub authentication_order: Vec<String>,
120    pub radius_servers: Vec<RadiusServer>,
121    pub tacplus_servers: Vec<TacplusServer>,
122    pub location: Option<Location>,
123    pub ports: Option<Ports>,
124    pub max_configurations_on_flash: Option<String>,
125    pub max_configuration_rollbacks: Option<String>,
126    pub no_redirects: bool,
127    /// `default-address-selection`: use the loopback as the source of locally generated packets.
128    pub default_address_selection: bool,
129    pub internet_options: Option<InternetOptions>,
130    pub commit: Option<Commit>,
131    pub processes: Vec<Process>,
132    pub dynamic_profile_options: Option<DynamicProfileOptions>,
133    pub extensions: Option<Extensions>,
134    pub autoinstallation: Option<Autoinstallation>,
135    pub schema: Option<Schema>,
136    pub pixel_tracing: Option<PixelTracing>,
137    pub scripts: Option<Scripts>,
138    pub no_multicast_echo: bool,
139    pub no_ping_record_route: bool,
140    pub no_ping_time_stamp: bool,
141    pub auto_snapshot: bool,
142    pub compress_configuration_files: bool,
143    pub mirror_flash_on_disk: bool,
144    pub switchover_on_routing_crash: bool,
145    pub use_imported_time_zones: bool,
146    pub archival: Option<Archival>,
147    pub accounting: Option<Accounting>,
148    pub root_authentication: Option<RootAuthentication>,
149    pub master_password: Option<MasterPassword>,
150    /// Method name of `diag-port-authentication`, e.g. `encrypted-password`; the password is not
151    /// extracted.
152    pub diag_port_authentication: Vec<String>,
153    /// Method names of `pic-console-authentication`; the password is not extracted.
154    pub pic_console_authentication: Vec<String>,
155    pub license: Option<License>,
156    pub name_servers: Vec<NameServer>,
157    pub ntp: Option<Ntp>,
158    pub syslog: Option<Syslog>,
159    pub login: Option<Login>,
160    pub services: Option<Services>,
161}
162
163/// Returns the top-level `system` block, or `None` if there is none.
164///
165/// # Examples
166///
167/// ```
168/// use asterust::config::juniper;
169/// use asterust_junos::system::system;
170///
171/// let config = juniper::parse_braces("system {\n    host-name pe1;\n}\n");
172/// let system = system(&config).unwrap();
173/// assert_eq!(system.host_name.as_deref(), Some("pe1"));
174/// ```
175pub fn system(config: &Config) -> Option<System> {
176    let system = top_level(config, "system")?;
177    Some(System {
178        host_name: arg_of(system, "host-name").map(str::to_string),
179        domain_name: arg_of(system, "domain-name").map(str::to_string),
180        time_zone: arg_of(system, "time-zone").map(str::to_string),
181        domain_search: args_of(system, "domain-search")
182            .into_iter()
183            .flat_map(list_values)
184            .collect(),
185        static_host_mappings: static_host_mappings(system),
186        backup_router: arg_of(system, "backup-router").map(parse_backup_router),
187        inet6_backup_router: arg_of(system, "inet6-backup-router").map(parse_backup_router),
188        authentication_order: args_of(system, "authentication-order")
189            .into_iter()
190            .flat_map(list_values)
191            .collect(),
192        radius_servers: radius_servers(system, "radius-server"),
193        tacplus_servers: tacplus_servers(system, "tacplus-server"),
194        location: child_block(system, "location").map(parse_location),
195        ports: child_block(system, "ports").map(parse_ports),
196        max_configurations_on_flash: arg_of(system, "max-configurations-on-flash").map(str::to_string),
197        max_configuration_rollbacks: arg_of(system, "max-configuration-rollbacks").map(str::to_string),
198        no_redirects: has_leaf(system, "no-redirects"),
199        default_address_selection: has_leaf(system, "default-address-selection"),
200        internet_options: child_block(system, "internet-options").map(parse_internet_options),
201        commit: child_block(system, "commit").map(parse_commit),
202        processes: child_block(system, "processes")
203            .map(parse_processes)
204            .unwrap_or_default(),
205        dynamic_profile_options: child_block(system, "dynamic-profile-options").map(parse_dynamic_profile_options),
206        extensions: child_block(system, "extensions").map(parse_extensions),
207        autoinstallation: child_block(system, "autoinstallation").map(parse_autoinstallation),
208        schema: child_block(system, "schema").map(parse_schema),
209        pixel_tracing: child_block(system, "pixel-tracing").map(parse_pixel_tracing),
210        scripts: child_block(system, "scripts").map(parse_scripts),
211        no_multicast_echo: has_leaf(system, "no-multicast-echo"),
212        no_ping_record_route: has_leaf(system, "no-ping-record-route"),
213        no_ping_time_stamp: has_leaf(system, "no-ping-time-stamp"),
214        auto_snapshot: has_leaf(system, "auto-snapshot"),
215        compress_configuration_files: has_leaf(system, "compress-configuration-files"),
216        mirror_flash_on_disk: has_leaf(system, "mirror-flash-on-disk"),
217        switchover_on_routing_crash: has_leaf(system, "switchover-on-routing-crash"),
218        use_imported_time_zones: has_leaf(system, "use-imported-time-zones"),
219        archival: child_block(system, "archival").map(parse_archival),
220        accounting: child_block(system, "accounting").map(parse_accounting),
221        root_authentication: root_authentication(system),
222        master_password: child_block(system, "master-password").map(parse_master_password),
223        diag_port_authentication: password_methods(system, "diag-port-authentication"),
224        pic_console_authentication: password_methods(system, "pic-console-authentication"),
225        license: child_block(system, "license").map(parse_license),
226        name_servers: name_servers(system),
227        ntp: child_block(system, "ntp").map(parse_ntp),
228        syslog: child_block(system, "syslog").map(parse_syslog),
229        login: child_block(system, "login").map(parse_login),
230        services: child_block(system, "services").map(parse_services),
231    })
232}
233
234/// Servers written as `<keyword> <address> { ... }`, or as `<keyword> { <address> { ... } }`, with
235/// their option lines.
236fn servers<'a>(system: &'a ConfigLine, keyword: &str) -> Vec<(&'a str, &'a ConfigLine)> {
237    let mut found = Vec::new();
238    for child in active_children(system) {
239        if child.text == keyword {
240            found.extend(active_children(child).map(|server| (unquote(&server.text), server)));
241        } else if let Some(arg) = child.arg(keyword) {
242            found.push((split_name(arg).0, child));
243        }
244    }
245    found
246}
247
248/// The method names of a password setting such as `diag-port-authentication`, written either as
249/// `<keyword> encrypted-password "...";` or as a block. The passwords are dropped.
250fn password_methods(system: &ConfigLine, keyword: &str) -> Vec<String> {
251    let mut methods = Vec::new();
252    for child in active_children(system) {
253        if child.text == keyword {
254            methods.extend(authentication_methods(child));
255        } else if let Some(method) = child.arg(keyword).and_then(|arg| arg.split_whitespace().next()) {
256            methods.push(method.to_string());
257        }
258    }
259    methods
260}
261
262/// The name of each statement in an `authentication` block. Secrets can carry a trailing `##
263/// SECRET-DATA` marker, so only the first word is kept.
264fn authentication_methods(auth: &ConfigLine) -> Vec<String> {
265    active_children(auth)
266        .filter_map(|c| c.text.split_whitespace().next())
267        .map(str::to_string)
268        .collect()
269}
270
271#[cfg(test)]
272mod tests {
273    use crate::system::system;
274    use asterust::config::juniper;
275
276    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
277    #[test]
278    fn extracts_host_name() {
279        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
280        assert_eq!(system.host_name.as_deref(), Some("pe1"));
281    }
282
283    #[test]
284    fn host_name_is_optional() {
285        let system = system(&juniper::parse_braces("system {\n    domain-name example.net;\n}\n")).unwrap();
286        assert_eq!(system.host_name, None);
287    }
288
289    #[test]
290    fn skips_inactive_host_name() {
291        let system = system(&juniper::parse_braces("system {\n    inactive: host-name pe1;\n}\n")).unwrap();
292        assert_eq!(system.host_name, None);
293    }
294
295    #[test]
296    fn extracts_domain_name() {
297        let system = system(&juniper::parse_braces(
298            "system {\n    host-name pe1;\n    domain-name example.net;\n}\n",
299        ))
300        .unwrap();
301        assert_eq!(system.domain_name.as_deref(), Some("example.net"));
302    }
303
304    #[test]
305    fn domain_name_is_optional() {
306        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
307        assert_eq!(system.domain_name, None);
308    }
309
310    #[test]
311    fn extracts_time_zone() {
312        let system = system(&juniper::parse_braces("system {\n    time-zone America/New_York;\n}\n")).unwrap();
313        assert_eq!(system.time_zone.as_deref(), Some("America/New_York"));
314    }
315
316    #[test]
317    fn time_zone_is_optional() {
318        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
319        assert_eq!(system.time_zone, None);
320    }
321
322    #[test]
323    fn extracts_configuration_limits_and_no_redirects() {
324        let system = system(&juniper::parse_braces(
325            "system {\n    max-configurations-on-flash 5;\n    max-configuration-rollbacks 20;\n    no-redirects;\n}\n",
326        ))
327        .unwrap();
328        assert_eq!(system.max_configurations_on_flash.as_deref(), Some("5"));
329        assert_eq!(system.max_configuration_rollbacks.as_deref(), Some("20"));
330        assert!(system.no_redirects);
331    }
332
333    #[test]
334    fn configuration_limits_are_optional() {
335        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
336        assert_eq!(system.max_configurations_on_flash, None);
337        assert_eq!(system.max_configuration_rollbacks, None);
338        assert!(!system.no_redirects);
339    }
340
341    #[test]
342    fn extracts_system_flags() {
343        let system = system(&juniper::parse_braces(
344            "system {\n    no-multicast-echo;\n    no-ping-record-route;\n    no-ping-time-stamp;\n    auto-snapshot;\n    compress-configuration-files;\n    mirror-flash-on-disk;\n    switchover-on-routing-crash;\n    use-imported-time-zones;\n}\n",
345        ))
346        .unwrap();
347        assert!(system.no_multicast_echo);
348        assert!(system.no_ping_record_route);
349        assert!(system.no_ping_time_stamp);
350        assert!(system.auto_snapshot);
351        assert!(system.compress_configuration_files);
352        assert!(system.mirror_flash_on_disk);
353        assert!(system.switchover_on_routing_crash);
354        assert!(system.use_imported_time_zones);
355    }
356
357    #[test]
358    fn system_flags_default_to_off() {
359        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
360        assert!(!system.no_multicast_echo);
361        assert!(!system.auto_snapshot);
362        assert!(!system.use_imported_time_zones);
363    }
364
365    #[test]
366    fn no_system_is_none() {
367        assert!(system(&juniper::parse_braces("interfaces {\n    lo0;\n}\n")).is_none());
368    }
369
370    #[test]
371    fn diag_port_and_pic_console_report_only_the_method() {
372        let system = system(&juniper::parse_braces(
373            r#"
374system {
375    diag-port-authentication encrypted-password "$6$salt$hash"; ## SECRET-DATA
376    pic-console-authentication {
377        encrypted-password "$6$salt$other"; ## SECRET-DATA
378    }
379}
380"#,
381        ))
382        .unwrap();
383        assert_eq!(system.diag_port_authentication, ["encrypted-password"]);
384        assert_eq!(system.pic_console_authentication, ["encrypted-password"]);
385    }
386
387    #[test]
388    fn secret_settings_are_optional() {
389        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
390        assert!(system.master_password.is_none());
391        assert!(system.diag_port_authentication.is_empty());
392        assert!(system.pic_console_authentication.is_empty());
393    }
394
395    #[test]
396    fn extracts_authentication_order() {
397        let system = system(&juniper::parse_braces(
398            "system {\n    authentication-order [ tacplus radius password ];\n}\n",
399        ))
400        .unwrap();
401        assert_eq!(system.authentication_order, ["tacplus", "radius", "password"]);
402    }
403
404    #[test]
405    fn authentication_settings_are_optional() {
406        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
407        assert!(system.authentication_order.is_empty());
408        assert!(system.radius_servers.is_empty());
409        assert!(system.tacplus_servers.is_empty());
410    }
411
412    #[test]
413    fn extracts_domain_search_list() {
414        let system = system(&juniper::parse_braces(
415            "system {\n    domain-search [ example.net lab.example.net ];\n}\n",
416        ))
417        .unwrap();
418        assert_eq!(system.domain_search, ["example.net", "lab.example.net"]);
419    }
420
421    #[test]
422    fn addressing_settings_are_optional() {
423        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
424        assert!(system.domain_search.is_empty());
425        assert!(system.static_host_mappings.is_empty());
426        assert!(system.backup_router.is_none());
427        assert!(system.inet6_backup_router.is_none());
428    }
429}