Skip to main content

asterust_junos/system/
mod.rs

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