1mod dhcp_local_server;
9mod dhcp_server;
10mod dns;
11mod dynamic_dns;
12mod extensible_subscriber_services;
13mod extension_service;
14mod flow_tap_dtcp;
15mod ftp;
16mod grpc_tunnel;
17mod http_servers;
18mod inband_ztp;
19mod jeb;
20mod limits;
21mod local_policy_decision_function;
22mod netconf;
23mod outbound_https;
24mod outbound_ssh;
25mod remote_device_management;
26mod resource_monitor;
27mod rest;
28mod reverse;
29mod service_deployment;
30mod ssh;
31mod static_subscribers;
32mod subscriber_management;
33mod tcp_forwarding;
34mod telnet;
35mod web_management;
36mod webapi;
37mod xnm_clear_text;
38mod xnm_ssl;
39
40pub use dhcp_local_server::{
41 DhcpActiveLeasequery, DhcpActiveLeasequeryLimits, DhcpAuthentication, DhcpBfd, DhcpBulkLeasequery,
42 DhcpClassificationKey, DhcpDelay, DhcpDualStackGroup, DhcpDynamicProfile, DhcpDynamicServerGroup,
43 DhcpDynamicServerInterface, DhcpFingerprintServer, DhcpFingerprintServerGroup, DhcpGroupInterface,
44 DhcpInterfaceTag, DhcpLeaseTimeValidation, DhcpLivenessDetection, DhcpLocalServer, DhcpLocalServerGroup, DhcpMatch,
45 DhcpOptions, DhcpOverrides, DhcpReconfigure, DhcpShortCycleProtection, DhcpUsernameInclude,
46};
47pub use dhcp_server::{DhcpPool, DhcpServer, DhcpServerSettings, DhcpStaticBinding};
48pub use dns::{Dns, DnsCacheEntry, DnsDomain, DnsProxy, DnsView, Dnssec};
49pub use dynamic_dns::DynamicDnsClient;
50pub use extensible_subscriber_services::ExtensibleSubscriberServices;
51pub use extension_service::{ExtensionService, GrpcService, GrpcSsl, JetNotification};
52pub use flow_tap_dtcp::{FlowTapDtcp, FlowTapDtcpSsh};
53pub use ftp::FtpService;
54pub use grpc_tunnel::{GrpcTunnel, GrpcTunnelServer};
55pub use http_servers::{HttpServer, HttpServerTls};
56pub use inband_ztp::InbandZtp;
57pub use jeb::{Jeb, JebTls};
58pub use limits::ConnectionLimits;
59pub use local_policy_decision_function::{AaclStatisticsProfile, LocalPolicyDecisionFunction};
60pub use netconf::{
61 Netconf, NetconfClientIdentity, NetconfDefaultClientIdentity, NetconfHelloMessage, NetconfMonitoring, NetconfSsh,
62 NetconfTls, NetconfYangModules,
63};
64pub use outbound_https::{OutboundHttpsAddress, OutboundHttpsClient};
65pub use outbound_ssh::{OutboundSshClient, OutboundSshServer};
66pub use remote_device_management::{RemoteDeviceNetconf, ServiceDevice};
67pub use resource_monitor::{FpcLimit, JtreeResourceType, PicLimit, PortLimit, ResourceMonitor, SubscriberLimit};
68pub use rest::{Rest, RestHttp, RestHttps};
69pub use reverse::Reverse;
70pub use service_deployment::{ServiceDeployment, ServiceDeploymentServer};
71pub use ssh::{SshModuli, SshService};
72pub use static_subscribers::{
73 StaticSubscriberGroup, StaticSubscriberProfile, StaticSubscriberUsername, StaticSubscribers,
74};
75pub use subscriber_management::{
76 ExternalManagement, InlineNaptPool, OverrideFamily, RedundancyInterface, Shmlog, SubscriberManagement,
77 SubscriberOverrides, SubscriberRedundancy,
78};
79pub use tcp_forwarding::TcpForwardingEntry;
80pub use telnet::TelnetService;
81pub use web_management::{WebManagement, WebManagementHttp, WebManagementHttps, WebManagementVirtualDomain};
82pub use webapi::{Webapi, WebapiHttps, WebapiUser};
83pub use xnm_clear_text::XnmClearText;
84pub use xnm_ssl::XnmSsl;
85
86use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
87use crate::{arg_of, child_block, has_leaf};
88use asterust::config::ConfigLine;
89use dhcp_local_server::parse_dhcp_local_server;
90use dhcp_server::parse_dhcp_server;
91use dns::parse_dns;
92use dynamic_dns::parse_dynamic_dns;
93use extensible_subscriber_services::parse_extensible_subscriber_services;
94use extension_service::parse_extension_service;
95use flow_tap_dtcp::parse_flow_tap_dtcp;
96use ftp::parse_ftp;
97use grpc_tunnel::parse_grpc_tunnel;
98use http_servers::parse_http_servers;
99use inband_ztp::parse_inband_ztp;
100use jeb::parse_jeb;
101use limits::parse_limits;
102use local_policy_decision_function::parse_local_policy_decision_function;
103use netconf::parse_netconf;
104use outbound_https::parse_outbound_https;
105use outbound_ssh::parse_outbound_ssh;
106use remote_device_management::parse_remote_device_management;
107use resource_monitor::parse_resource_monitor;
108use rest::parse_rest;
109use reverse::parse_reverse;
110use service_deployment::parse_service_deployment;
111use ssh::parse_ssh;
112use static_subscribers::parse_static_subscribers;
113use subscriber_management::parse_subscriber_management;
114use tcp_forwarding::parse_tcp_forwarding;
115use telnet::parse_telnet;
116use web_management::parse_web_management;
117use webapi::parse_webapi;
118use xnm_clear_text::parse_xnm_clear_text;
119use xnm_ssl::parse_xnm_ssl;
120
121#[derive(Clone, Debug, PartialEq, Eq)]
123#[non_exhaustive]
124pub struct Services {
125 pub ssh: Option<SshService>,
126 pub telnet: bool,
127 pub ftp: bool,
128 pub netconf_ssh: bool,
130 pub http: bool,
132 pub https: bool,
134 pub outbound_ssh: Vec<OutboundSshClient>,
135 pub web_management: Option<WebManagement>,
137 pub netconf: Option<Netconf>,
139 pub xnm_clear_text: Option<XnmClearText>,
140 pub xnm_ssl: Option<XnmSsl>,
141 pub subscriber_management: Option<SubscriberManagement>,
142 pub static_subscribers: Option<StaticSubscribers>,
143 pub dhcp_local_server: Option<DhcpLocalServer>,
144 pub local_policy_decision_function: Option<LocalPolicyDecisionFunction>,
145 pub flow_tap_dtcp: Option<FlowTapDtcp>,
146 pub service_deployment: Option<ServiceDeployment>,
147 pub telnet_service: Option<TelnetService>,
148 pub ftp_service: Option<FtpService>,
149 pub finger: Option<ConnectionLimits>,
150 pub tftp_server: Option<ConnectionLimits>,
151 pub reverse: Option<Reverse>,
152 pub rest: Option<Rest>,
153 pub http_servers: Vec<HttpServer>,
155 pub extension_service: Option<ExtensionService>,
156 pub jeb: Option<Jeb>,
157 pub inband_ztp: Option<InbandZtp>,
158 pub outbound_https: Vec<OutboundHttpsClient>,
159 pub outbound_ssh_traceoptions: Option<Traceoptions>,
161 pub resource_monitor: Option<ResourceMonitor>,
162 pub dns: Option<Dns>,
163 pub dynamic_dns: Vec<DynamicDnsClient>,
164 pub grpc_tunnel: Option<GrpcTunnel>,
165 pub webapi: Option<Webapi>,
166 pub dhcp: Option<DhcpServer>,
168 pub extensible_subscriber_services: Option<ExtensibleSubscriberServices>,
169 pub remote_device_management: Vec<ServiceDevice>,
170 pub tcp_forwarding: Vec<TcpForwardingEntry>,
171 pub dtcp_only: bool,
173 pub packet_triggered_subscriber_idle_timeout: Option<String>,
175 pub database_replication_traceoptions: Option<Traceoptions>,
177}
178
179pub(super) fn parse_services(services: &ConfigLine) -> Services {
180 let web = child_block(services, "web-management");
181 let netconf = child_block(services, "netconf");
182 Services {
183 ssh: child_block(services, "ssh").map(parse_ssh),
184 telnet: child_block(services, "telnet").is_some(),
185 ftp: child_block(services, "ftp").is_some(),
186 netconf_ssh: netconf.is_some_and(|n| child_block(n, "ssh").is_some()),
187 http: web.is_some_and(|w| child_block(w, "http").is_some()),
188 https: web.is_some_and(|w| child_block(w, "https").is_some()),
189 outbound_ssh: child_block(services, "outbound-ssh")
190 .map(parse_outbound_ssh)
191 .unwrap_or_default(),
192 web_management: web.map(parse_web_management),
193 netconf: netconf.map(parse_netconf),
194 xnm_clear_text: child_block(services, "xnm-clear-text").map(parse_xnm_clear_text),
195 xnm_ssl: child_block(services, "xnm-ssl").map(parse_xnm_ssl),
196 subscriber_management: child_block(services, "subscriber-management").map(parse_subscriber_management),
197 static_subscribers: child_block(services, "static-subscribers").map(parse_static_subscribers),
198 dhcp_local_server: child_block(services, "dhcp-local-server").map(parse_dhcp_local_server),
199 local_policy_decision_function: child_block(services, "local-policy-decision-function")
200 .map(parse_local_policy_decision_function),
201 flow_tap_dtcp: child_block(services, "flow-tap-dtcp").map(parse_flow_tap_dtcp),
202 service_deployment: child_block(services, "service-deployment").map(parse_service_deployment),
203 telnet_service: child_block(services, "telnet").map(parse_telnet),
204 ftp_service: child_block(services, "ftp").map(parse_ftp),
205 finger: child_block(services, "finger").map(parse_limits),
206 tftp_server: child_block(services, "tftp-server").map(parse_limits),
207 reverse: child_block(services, "reverse").map(parse_reverse),
208 rest: child_block(services, "rest").map(parse_rest),
209 http_servers: child_block(services, "http")
210 .map(parse_http_servers)
211 .unwrap_or_default(),
212 extension_service: child_block(services, "extension-service").map(parse_extension_service),
213 jeb: child_block(services, "jeb").map(parse_jeb),
214 inband_ztp: child_block(services, "inband-ztp").map(parse_inband_ztp),
215 outbound_https: child_block(services, "outbound-https")
216 .map(parse_outbound_https)
217 .unwrap_or_default(),
218 outbound_ssh_traceoptions: child_block(services, "outbound-ssh").and_then(parse_traceoptions),
219 resource_monitor: child_block(services, "resource-monitor").map(parse_resource_monitor),
220 dns: child_block(services, "dns").map(parse_dns),
221 dynamic_dns: child_block(services, "dynamic-dns")
222 .map(parse_dynamic_dns)
223 .unwrap_or_default(),
224 grpc_tunnel: child_block(services, "grpc-tunnel").map(parse_grpc_tunnel),
225 webapi: child_block(services, "webapi").map(parse_webapi),
226 dhcp: child_block(services, "dhcp").map(parse_dhcp_server),
227 extensible_subscriber_services: child_block(services, "extensible-subscriber-services")
228 .map(parse_extensible_subscriber_services),
229 remote_device_management: child_block(services, "remote-device-management")
230 .map(parse_remote_device_management)
231 .unwrap_or_default(),
232 tcp_forwarding: child_block(services, "tcp-forwarding")
233 .map(parse_tcp_forwarding)
234 .unwrap_or_default(),
235 dtcp_only: has_leaf(services, "dtcp-only"),
236 packet_triggered_subscriber_idle_timeout: child_block(services, "packet-triggered-subscribers")
237 .and_then(|triggered| arg_of(triggered, "subscriber-packet-idle-timeout"))
238 .map(str::to_string),
239 database_replication_traceoptions: child_block(services, "database-replication").and_then(parse_traceoptions),
240 }
241}
242
243#[cfg(test)]
244mod tests {
245 use super::*;
246 use crate::system::system;
247 use asterust::config::juniper;
248
249 const SERVICES: &str = r#"
250system {
251 services {
252 ssh {
253 root-login deny;
254 protocol-version v2;
255 port 2222;
256 connection-limit 10;
257 rate-limit 5;
258 ciphers [ aes256-ctr aes128-ctr ];
259 sftp-server;
260 }
261 netconf {
262 ssh;
263 }
264 web-management {
265 https {
266 system-generated-certificate;
267 }
268 }
269 telnet;
270 }
271}
272"#;
273
274 #[test]
276 fn extracts_dtcp_only_packet_triggered_subscribers_and_database_replication() {
277 let services = system(&juniper::parse_braces(
278 "system {\n services {\n dtcp-only;\n packet-triggered-subscribers {\n subscriber-packet-idle-timeout 30;\n }\n database-replication {\n traceoptions {\n file dbrepl.log;\n flag all;\n }\n }\n }\n}\n",
279 ))
280 .unwrap()
281 .services
282 .unwrap();
283 assert!(services.dtcp_only);
284 assert_eq!(services.packet_triggered_subscriber_idle_timeout.as_deref(), Some("30"));
285 assert_eq!(
286 services.database_replication_traceoptions.unwrap().file.as_deref(),
287 Some("dbrepl.log")
288 );
289 }
290
291 #[test]
292 fn extracts_ssh_service_settings() {
293 let services = system(&juniper::parse_braces(SERVICES)).unwrap().services.unwrap();
294 let ssh = services.ssh.unwrap();
295 assert_eq!(ssh.root_login.as_deref(), Some("deny"));
296 assert_eq!(ssh.protocol_versions, ["v2"]);
297 assert_eq!(ssh.port.as_deref(), Some("2222"));
298 assert_eq!(ssh.connection_limit.as_deref(), Some("10"));
299 assert_eq!(ssh.rate_limit.as_deref(), Some("5"));
300 assert_eq!(ssh.ciphers, ["aes256-ctr", "aes128-ctr"]);
301 assert!(ssh.sftp_server);
302 }
303
304 #[test]
305 fn extracts_enabled_services() {
306 let services = system(&juniper::parse_braces(SERVICES)).unwrap().services.unwrap();
307 assert!(services.telnet);
308 assert!(services.netconf_ssh);
309 assert!(services.https);
310 assert!(!services.http);
311 assert!(!services.ftp);
312 }
313
314 #[test]
315 fn bare_service_statements_enable_with_defaults() {
316 let services = system(&juniper::parse_braces(
317 "system {\n services {\n ssh;\n ftp;\n web-management {\n http;\n }\n }\n}\n",
318 ))
319 .unwrap()
320 .services
321 .unwrap();
322 assert_eq!(services.ssh, Some(SshService::default()));
323 assert!(services.ftp);
324 assert!(services.http);
325 assert!(!services.telnet);
326 assert!(!services.netconf_ssh);
327 }
328
329 #[test]
330 fn disabled_service_is_not_reported() {
331 let services = system(&juniper::parse_braces(
332 "system {\n services {\n inactive: telnet;\n ssh;\n }\n}\n",
333 ))
334 .unwrap()
335 .services
336 .unwrap();
337 assert!(!services.telnet);
338 }
339
340 #[test]
341 fn extracts_netconf_and_xnm_settings() {
342 let services = system(&juniper::parse_braces(
343 r#"
344system {
345 services {
346 netconf {
347 ssh {
348 port 830;
349 connection-limit 5;
350 rate-limit 10;
351 }
352 rfc-compliant;
353 }
354 xnm-clear-text {
355 connection-limit 3;
356 }
357 xnm-ssl {
358 port 3220;
359 local-certificate XNM;
360 rate-limit 7;
361 }
362 }
363}
364"#,
365 ))
366 .unwrap()
367 .services
368 .unwrap();
369 let netconf = services.netconf.unwrap();
370 assert!(netconf.rfc_compliant);
371 let ssh = netconf.ssh.unwrap();
372 assert_eq!(ssh.port.as_deref(), Some("830"));
373 assert_eq!(ssh.connection_limit.as_deref(), Some("5"));
374 assert_eq!(ssh.rate_limit.as_deref(), Some("10"));
375 assert!(services.netconf_ssh);
376
377 let clear_text = services.xnm_clear_text.unwrap();
378 assert_eq!(clear_text.connection_limit.as_deref(), Some("3"));
379 assert_eq!(clear_text.rate_limit, None);
380
381 let ssl = services.xnm_ssl.unwrap();
382 assert_eq!(ssl.port.as_deref(), Some("3220"));
383 assert_eq!(ssl.local_certificate.as_deref(), Some("XNM"));
384 assert_eq!(ssl.rate_limit.as_deref(), Some("7"));
385 }
386
387 #[test]
388 fn no_services_is_none() {
389 assert!(system(&juniper::parse_braces("system {\n host-name pe1;\n}\n"))
390 .unwrap()
391 .services
392 .is_none());
393 }
394
395 const SUBSCRIBERS: &str = r#"
397system {
398 dynamic-profile-options {
399 versioning;
400 }
401 services {
402 subscriber-management {
403 enable;
404 enforce-strict-scale-limit-license;
405 gres-route-flush-delay 60;
406 cgnat {
407 enable;
408 }
409 redundancy {
410 interface ae0.100 {
411 local-inet-address 192.0.2.1;
412 local-inet6-address 2001:db8::1;
413 shared-key "$9$secret"; ## SECRET-DATA
414 virtual-inet-address 192.0.2.254;
415 virtual-inet6-address 2001:db8::fe;
416 }
417 no-advertise-routes-on-backup;
418 protocol {
419 vrrp;
420 }
421 }
422 }
423 static-subscribers {
424 access-profile AP;
425 service-profile SP;
426 authentication {
427 password "$9$secret"; ## SECRET-DATA
428 }
429 group STATIC1 {
430 access-profile AP1;
431 authentication {
432 password "$9$other"; ## SECRET-DATA
433 }
434 dynamic-profile PROFILE1 {
435 aggregate-clients merge;
436 }
437 interface ge-1/0/0.100 upto ge-1/0/0.200;
438 interface ge-1/0/0.150 exclude;
439 }
440 }
441 dhcp-local-server {
442 group G1 {
443 interface ge-1/0/0.0;
444 interface ge-1/0/1.0 upto ge-1/0/1.9;
445 }
446 dhcpv6 {
447 group V6 {
448 interface ge-2/0/0.0;
449 }
450 }
451 }
452 local-policy-decision-function {
453 statistics {
454 aacl-statistics-profile AACL1 {
455 aacl-fields {
456 input-bytes;
457 output-bytes;
458 }
459 file aacl.stats;
460 record-mode interim-full;
461 report-interval 30;
462 }
463 }
464 }
465 }
466}
467"#;
468
469 #[test]
470 fn extracts_subscriber_management() {
471 let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
472 let management = system.services.unwrap().subscriber_management.unwrap();
473 assert!(management.enable);
474 assert!(management.cgnat);
475 assert!(management.enforce_strict_scale_limit_license);
476 assert_eq!(management.gres_route_flush_delay.as_deref(), Some("60"));
477
478 let redundancy = management.redundancy.unwrap();
479 assert!(redundancy.no_advertise_routes_on_backup);
480 assert_eq!(redundancy.protocols, ["vrrp"]);
481 let interface = &redundancy.interfaces[0];
482 assert_eq!(interface.name, "ae0.100");
483 assert_eq!(
484 interface.shared_key.as_ref().map(crate::Secret::expose),
485 crate::secret::expected("$9$secret")
486 );
487 assert_eq!(interface.local_inet_address.as_deref(), Some("192.0.2.1"));
488 assert_eq!(interface.local_inet6_address.as_deref(), Some("2001:db8::1"));
489 assert_eq!(interface.virtual_inet_address.as_deref(), Some("192.0.2.254"));
490 assert_eq!(interface.virtual_inet6_address.as_deref(), Some("2001:db8::fe"));
491 }
492
493 #[test]
494 fn extracts_static_subscribers() {
495 let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
496 let subscribers = system.services.unwrap().static_subscribers.unwrap();
497 assert_eq!(subscribers.access_profile.as_deref(), Some("AP"));
498 assert_eq!(subscribers.service_profile.as_deref(), Some("SP"));
499 assert_eq!(
500 subscribers.password.as_ref().map(crate::Secret::expose),
501 crate::secret::expected("$9$secret")
502 );
503
504 let group = &subscribers.groups[0];
505 assert_eq!(group.name, "STATIC1");
506 assert_eq!(group.access_profile.as_deref(), Some("AP1"));
507 assert_eq!(
508 group.password.as_ref().map(crate::Secret::expose),
509 crate::secret::expected("$9$other")
510 );
511 assert_eq!(group.dynamic_profiles[0].name, "PROFILE1");
512 assert_eq!(group.dynamic_profiles[0].aggregate_clients.as_deref(), Some("merge"));
513 assert_eq!(
514 group.interfaces,
515 ["ge-1/0/0.100 upto ge-1/0/0.200", "ge-1/0/0.150 exclude"]
516 );
517 }
518
519 #[test]
520 fn extracts_dhcp_local_server_groups() {
521 let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
522 let server = system.services.unwrap().dhcp_local_server.unwrap();
523 assert_eq!(server.groups.len(), 1);
524 assert_eq!(server.groups[0].name, "G1");
525 assert_eq!(
526 server.groups[0].interfaces,
527 ["ge-1/0/0.0", "ge-1/0/1.0 upto ge-1/0/1.9"]
528 );
529 assert_eq!(server.dhcpv6_groups[0].name, "V6");
530 assert_eq!(server.dhcpv6_groups[0].interfaces, ["ge-2/0/0.0"]);
531 }
532
533 #[test]
534 fn extracts_aacl_statistics_profiles() {
535 let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
536 let function = system.services.unwrap().local_policy_decision_function.unwrap();
537 let profile = &function.statistics[0];
538 assert_eq!(profile.name, "AACL1");
539 assert_eq!(profile.aacl_fields, ["input-bytes", "output-bytes"]);
540 assert_eq!(profile.file.as_deref(), Some("aacl.stats"));
541 assert_eq!(profile.record_mode.as_deref(), Some("interim-full"));
542 assert_eq!(profile.report_interval.as_deref(), Some("30"));
543 }
544
545 #[test]
546 fn subscriber_settings_are_optional() {
547 let system = system(&juniper::parse_braces(
548 "system {\n services {\n ssh;\n }\n}\n",
549 ))
550 .unwrap();
551 assert!(system.dynamic_profile_options.is_none());
552 let services = system.services.unwrap();
553 assert!(services.subscriber_management.is_none());
554 assert!(services.static_subscribers.is_none());
555 assert!(services.dhcp_local_server.is_none());
556 assert!(services.local_policy_decision_function.is_none());
557 }
558}