mod dhcp_local_server;
mod dhcp_server;
mod dns;
mod dynamic_dns;
mod extensible_subscriber_services;
mod extension_service;
mod flow_tap_dtcp;
mod ftp;
mod grpc_tunnel;
mod http_servers;
mod inband_ztp;
mod jeb;
mod limits;
mod local_policy_decision_function;
mod netconf;
mod outbound_https;
mod outbound_ssh;
mod remote_device_management;
mod resource_monitor;
mod rest;
mod reverse;
mod service_deployment;
mod ssh;
mod static_subscribers;
mod subscriber_management;
mod tcp_forwarding;
mod telnet;
mod web_management;
mod webapi;
mod xnm_clear_text;
mod xnm_ssl;
pub use dhcp_local_server::{
DhcpActiveLeasequery, DhcpActiveLeasequeryLimits, DhcpAuthentication, DhcpBfd, DhcpBulkLeasequery,
DhcpClassificationKey, DhcpDelay, DhcpDualStackGroup, DhcpDynamicProfile, DhcpDynamicServerGroup,
DhcpDynamicServerInterface, DhcpFingerprintServer, DhcpFingerprintServerGroup, DhcpGroupInterface,
DhcpInterfaceTag, DhcpLeaseTimeValidation, DhcpLivenessDetection, DhcpLocalServer, DhcpLocalServerGroup, DhcpMatch,
DhcpOptions, DhcpOverrides, DhcpReconfigure, DhcpShortCycleProtection, DhcpUsernameInclude,
};
pub use dhcp_server::{DhcpPool, DhcpServer, DhcpServerSettings, DhcpStaticBinding};
pub use dns::{Dns, DnsCacheEntry, DnsDomain, DnsProxy, DnsView, Dnssec};
pub use dynamic_dns::DynamicDnsClient;
pub use extensible_subscriber_services::ExtensibleSubscriberServices;
pub use extension_service::{ExtensionService, GrpcService, GrpcSsl, JetNotification};
pub use flow_tap_dtcp::{FlowTapDtcp, FlowTapDtcpSsh};
pub use ftp::FtpService;
pub use grpc_tunnel::{GrpcTunnel, GrpcTunnelServer};
pub use http_servers::{HttpServer, HttpServerTls};
pub use inband_ztp::InbandZtp;
pub use jeb::{Jeb, JebTls};
pub use limits::ConnectionLimits;
pub use local_policy_decision_function::{AaclStatisticsProfile, LocalPolicyDecisionFunction};
pub use netconf::{
Netconf, NetconfClientIdentity, NetconfDefaultClientIdentity, NetconfHelloMessage, NetconfMonitoring, NetconfSsh,
NetconfTls, NetconfYangModules,
};
pub use outbound_https::{OutboundHttpsAddress, OutboundHttpsClient};
pub use outbound_ssh::{OutboundSshClient, OutboundSshServer};
pub use remote_device_management::{RemoteDeviceNetconf, ServiceDevice};
pub use resource_monitor::{FpcLimit, JtreeResourceType, PicLimit, PortLimit, ResourceMonitor, SubscriberLimit};
pub use rest::{Rest, RestHttp, RestHttps};
pub use reverse::Reverse;
pub use service_deployment::{ServiceDeployment, ServiceDeploymentServer};
pub use ssh::{SshModuli, SshService};
pub use static_subscribers::{
StaticSubscriberGroup, StaticSubscriberProfile, StaticSubscriberUsername, StaticSubscribers,
};
pub use subscriber_management::{
ExternalManagement, InlineNaptPool, OverrideFamily, RedundancyInterface, Shmlog, SubscriberManagement,
SubscriberOverrides, SubscriberRedundancy,
};
pub use tcp_forwarding::TcpForwardingEntry;
pub use telnet::TelnetService;
pub use web_management::{WebManagement, WebManagementHttp, WebManagementHttps, WebManagementVirtualDomain};
pub use webapi::{Webapi, WebapiHttps, WebapiUser};
pub use xnm_clear_text::XnmClearText;
pub use xnm_ssl::XnmSsl;
use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{arg_of, child_block, has_leaf};
use asterust::config::ConfigLine;
use dhcp_local_server::parse_dhcp_local_server;
use dhcp_server::parse_dhcp_server;
use dns::parse_dns;
use dynamic_dns::parse_dynamic_dns;
use extensible_subscriber_services::parse_extensible_subscriber_services;
use extension_service::parse_extension_service;
use flow_tap_dtcp::parse_flow_tap_dtcp;
use ftp::parse_ftp;
use grpc_tunnel::parse_grpc_tunnel;
use http_servers::parse_http_servers;
use inband_ztp::parse_inband_ztp;
use jeb::parse_jeb;
use limits::parse_limits;
use local_policy_decision_function::parse_local_policy_decision_function;
use netconf::parse_netconf;
use outbound_https::parse_outbound_https;
use outbound_ssh::parse_outbound_ssh;
use remote_device_management::parse_remote_device_management;
use resource_monitor::parse_resource_monitor;
use rest::parse_rest;
use reverse::parse_reverse;
use service_deployment::parse_service_deployment;
use ssh::parse_ssh;
use static_subscribers::parse_static_subscribers;
use subscriber_management::parse_subscriber_management;
use tcp_forwarding::parse_tcp_forwarding;
use telnet::parse_telnet;
use web_management::parse_web_management;
use webapi::parse_webapi;
use xnm_clear_text::parse_xnm_clear_text;
use xnm_ssl::parse_xnm_ssl;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Services {
pub ssh: Option<SshService>,
pub telnet: bool,
pub ftp: bool,
pub netconf_ssh: bool,
pub http: bool,
pub https: bool,
pub outbound_ssh: Vec<OutboundSshClient>,
pub web_management: Option<WebManagement>,
pub netconf: Option<Netconf>,
pub xnm_clear_text: Option<XnmClearText>,
pub xnm_ssl: Option<XnmSsl>,
pub subscriber_management: Option<SubscriberManagement>,
pub static_subscribers: Option<StaticSubscribers>,
pub dhcp_local_server: Option<DhcpLocalServer>,
pub local_policy_decision_function: Option<LocalPolicyDecisionFunction>,
pub flow_tap_dtcp: Option<FlowTapDtcp>,
pub service_deployment: Option<ServiceDeployment>,
pub telnet_service: Option<TelnetService>,
pub ftp_service: Option<FtpService>,
pub finger: Option<ConnectionLimits>,
pub tftp_server: Option<ConnectionLimits>,
pub reverse: Option<Reverse>,
pub rest: Option<Rest>,
pub http_servers: Vec<HttpServer>,
pub extension_service: Option<ExtensionService>,
pub jeb: Option<Jeb>,
pub inband_ztp: Option<InbandZtp>,
pub outbound_https: Vec<OutboundHttpsClient>,
pub outbound_ssh_traceoptions: Option<Traceoptions>,
pub resource_monitor: Option<ResourceMonitor>,
pub dns: Option<Dns>,
pub dynamic_dns: Vec<DynamicDnsClient>,
pub grpc_tunnel: Option<GrpcTunnel>,
pub webapi: Option<Webapi>,
pub dhcp: Option<DhcpServer>,
pub extensible_subscriber_services: Option<ExtensibleSubscriberServices>,
pub remote_device_management: Vec<ServiceDevice>,
pub tcp_forwarding: Vec<TcpForwardingEntry>,
pub dtcp_only: bool,
pub packet_triggered_subscriber_idle_timeout: Option<String>,
pub database_replication_traceoptions: Option<Traceoptions>,
}
pub(super) fn parse_services(services: &ConfigLine) -> Services {
let web = child_block(services, "web-management");
let netconf = child_block(services, "netconf");
Services {
ssh: child_block(services, "ssh").map(parse_ssh),
telnet: child_block(services, "telnet").is_some(),
ftp: child_block(services, "ftp").is_some(),
netconf_ssh: netconf.is_some_and(|n| child_block(n, "ssh").is_some()),
http: web.is_some_and(|w| child_block(w, "http").is_some()),
https: web.is_some_and(|w| child_block(w, "https").is_some()),
outbound_ssh: child_block(services, "outbound-ssh")
.map(parse_outbound_ssh)
.unwrap_or_default(),
web_management: web.map(parse_web_management),
netconf: netconf.map(parse_netconf),
xnm_clear_text: child_block(services, "xnm-clear-text").map(parse_xnm_clear_text),
xnm_ssl: child_block(services, "xnm-ssl").map(parse_xnm_ssl),
subscriber_management: child_block(services, "subscriber-management").map(parse_subscriber_management),
static_subscribers: child_block(services, "static-subscribers").map(parse_static_subscribers),
dhcp_local_server: child_block(services, "dhcp-local-server").map(parse_dhcp_local_server),
local_policy_decision_function: child_block(services, "local-policy-decision-function")
.map(parse_local_policy_decision_function),
flow_tap_dtcp: child_block(services, "flow-tap-dtcp").map(parse_flow_tap_dtcp),
service_deployment: child_block(services, "service-deployment").map(parse_service_deployment),
telnet_service: child_block(services, "telnet").map(parse_telnet),
ftp_service: child_block(services, "ftp").map(parse_ftp),
finger: child_block(services, "finger").map(parse_limits),
tftp_server: child_block(services, "tftp-server").map(parse_limits),
reverse: child_block(services, "reverse").map(parse_reverse),
rest: child_block(services, "rest").map(parse_rest),
http_servers: child_block(services, "http")
.map(parse_http_servers)
.unwrap_or_default(),
extension_service: child_block(services, "extension-service").map(parse_extension_service),
jeb: child_block(services, "jeb").map(parse_jeb),
inband_ztp: child_block(services, "inband-ztp").map(parse_inband_ztp),
outbound_https: child_block(services, "outbound-https")
.map(parse_outbound_https)
.unwrap_or_default(),
outbound_ssh_traceoptions: child_block(services, "outbound-ssh").and_then(parse_traceoptions),
resource_monitor: child_block(services, "resource-monitor").map(parse_resource_monitor),
dns: child_block(services, "dns").map(parse_dns),
dynamic_dns: child_block(services, "dynamic-dns")
.map(parse_dynamic_dns)
.unwrap_or_default(),
grpc_tunnel: child_block(services, "grpc-tunnel").map(parse_grpc_tunnel),
webapi: child_block(services, "webapi").map(parse_webapi),
dhcp: child_block(services, "dhcp").map(parse_dhcp_server),
extensible_subscriber_services: child_block(services, "extensible-subscriber-services")
.map(parse_extensible_subscriber_services),
remote_device_management: child_block(services, "remote-device-management")
.map(parse_remote_device_management)
.unwrap_or_default(),
tcp_forwarding: child_block(services, "tcp-forwarding")
.map(parse_tcp_forwarding)
.unwrap_or_default(),
dtcp_only: has_leaf(services, "dtcp-only"),
packet_triggered_subscriber_idle_timeout: child_block(services, "packet-triggered-subscribers")
.and_then(|triggered| arg_of(triggered, "subscriber-packet-idle-timeout"))
.map(str::to_string),
database_replication_traceoptions: child_block(services, "database-replication").and_then(parse_traceoptions),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::system::system;
use asterust::config::juniper;
const SERVICES: &str = r#"
system {
services {
ssh {
root-login deny;
protocol-version v2;
port 2222;
connection-limit 10;
rate-limit 5;
ciphers [ aes256-ctr aes128-ctr ];
sftp-server;
}
netconf {
ssh;
}
web-management {
https {
system-generated-certificate;
}
}
telnet;
}
}
"#;
#[test]
fn extracts_dtcp_only_packet_triggered_subscribers_and_database_replication() {
let services = system(&juniper::parse_braces(
"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",
))
.unwrap()
.services
.unwrap();
assert!(services.dtcp_only);
assert_eq!(services.packet_triggered_subscriber_idle_timeout.as_deref(), Some("30"));
assert_eq!(
services.database_replication_traceoptions.unwrap().file.as_deref(),
Some("dbrepl.log")
);
}
#[test]
fn extracts_ssh_service_settings() {
let services = system(&juniper::parse_braces(SERVICES)).unwrap().services.unwrap();
let ssh = services.ssh.unwrap();
assert_eq!(ssh.root_login.as_deref(), Some("deny"));
assert_eq!(ssh.protocol_versions, ["v2"]);
assert_eq!(ssh.port.as_deref(), Some("2222"));
assert_eq!(ssh.connection_limit.as_deref(), Some("10"));
assert_eq!(ssh.rate_limit.as_deref(), Some("5"));
assert_eq!(ssh.ciphers, ["aes256-ctr", "aes128-ctr"]);
assert!(ssh.sftp_server);
}
#[test]
fn extracts_enabled_services() {
let services = system(&juniper::parse_braces(SERVICES)).unwrap().services.unwrap();
assert!(services.telnet);
assert!(services.netconf_ssh);
assert!(services.https);
assert!(!services.http);
assert!(!services.ftp);
}
#[test]
fn bare_service_statements_enable_with_defaults() {
let services = system(&juniper::parse_braces(
"system {\n services {\n ssh;\n ftp;\n web-management {\n http;\n }\n }\n}\n",
))
.unwrap()
.services
.unwrap();
assert_eq!(services.ssh, Some(SshService::default()));
assert!(services.ftp);
assert!(services.http);
assert!(!services.telnet);
assert!(!services.netconf_ssh);
}
#[test]
fn disabled_service_is_not_reported() {
let services = system(&juniper::parse_braces(
"system {\n services {\n inactive: telnet;\n ssh;\n }\n}\n",
))
.unwrap()
.services
.unwrap();
assert!(!services.telnet);
}
#[test]
fn extracts_netconf_and_xnm_settings() {
let services = system(&juniper::parse_braces(
r#"
system {
services {
netconf {
ssh {
port 830;
connection-limit 5;
rate-limit 10;
}
rfc-compliant;
}
xnm-clear-text {
connection-limit 3;
}
xnm-ssl {
port 3220;
local-certificate XNM;
rate-limit 7;
}
}
}
"#,
))
.unwrap()
.services
.unwrap();
let netconf = services.netconf.unwrap();
assert!(netconf.rfc_compliant);
let ssh = netconf.ssh.unwrap();
assert_eq!(ssh.port.as_deref(), Some("830"));
assert_eq!(ssh.connection_limit.as_deref(), Some("5"));
assert_eq!(ssh.rate_limit.as_deref(), Some("10"));
assert!(services.netconf_ssh);
let clear_text = services.xnm_clear_text.unwrap();
assert_eq!(clear_text.connection_limit.as_deref(), Some("3"));
assert_eq!(clear_text.rate_limit, None);
let ssl = services.xnm_ssl.unwrap();
assert_eq!(ssl.port.as_deref(), Some("3220"));
assert_eq!(ssl.local_certificate.as_deref(), Some("XNM"));
assert_eq!(ssl.rate_limit.as_deref(), Some("7"));
}
#[test]
fn no_services_is_none() {
assert!(system(&juniper::parse_braces("system {\n host-name pe1;\n}\n"))
.unwrap()
.services
.is_none());
}
const SUBSCRIBERS: &str = r#"
system {
dynamic-profile-options {
versioning;
}
services {
subscriber-management {
enable;
enforce-strict-scale-limit-license;
gres-route-flush-delay 60;
cgnat {
enable;
}
redundancy {
interface ae0.100 {
local-inet-address 192.0.2.1;
local-inet6-address 2001:db8::1;
shared-key "$9$secret"; ## SECRET-DATA
virtual-inet-address 192.0.2.254;
virtual-inet6-address 2001:db8::fe;
}
no-advertise-routes-on-backup;
protocol {
vrrp;
}
}
}
static-subscribers {
access-profile AP;
service-profile SP;
authentication {
password "$9$secret"; ## SECRET-DATA
}
group STATIC1 {
access-profile AP1;
authentication {
password "$9$other"; ## SECRET-DATA
}
dynamic-profile PROFILE1 {
aggregate-clients merge;
}
interface ge-1/0/0.100 upto ge-1/0/0.200;
interface ge-1/0/0.150 exclude;
}
}
dhcp-local-server {
group G1 {
interface ge-1/0/0.0;
interface ge-1/0/1.0 upto ge-1/0/1.9;
}
dhcpv6 {
group V6 {
interface ge-2/0/0.0;
}
}
}
local-policy-decision-function {
statistics {
aacl-statistics-profile AACL1 {
aacl-fields {
input-bytes;
output-bytes;
}
file aacl.stats;
record-mode interim-full;
report-interval 30;
}
}
}
}
}
"#;
#[test]
fn extracts_subscriber_management() {
let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
let management = system.services.unwrap().subscriber_management.unwrap();
assert!(management.enable);
assert!(management.cgnat);
assert!(management.enforce_strict_scale_limit_license);
assert_eq!(management.gres_route_flush_delay.as_deref(), Some("60"));
let redundancy = management.redundancy.unwrap();
assert!(redundancy.no_advertise_routes_on_backup);
assert_eq!(redundancy.protocols, ["vrrp"]);
let interface = &redundancy.interfaces[0];
assert_eq!(interface.name, "ae0.100");
assert_eq!(
interface.shared_key.as_ref().map(crate::Secret::expose),
crate::secret::expected("$9$secret")
);
assert_eq!(interface.local_inet_address.as_deref(), Some("192.0.2.1"));
assert_eq!(interface.local_inet6_address.as_deref(), Some("2001:db8::1"));
assert_eq!(interface.virtual_inet_address.as_deref(), Some("192.0.2.254"));
assert_eq!(interface.virtual_inet6_address.as_deref(), Some("2001:db8::fe"));
}
#[test]
fn extracts_static_subscribers() {
let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
let subscribers = system.services.unwrap().static_subscribers.unwrap();
assert_eq!(subscribers.access_profile.as_deref(), Some("AP"));
assert_eq!(subscribers.service_profile.as_deref(), Some("SP"));
assert_eq!(
subscribers.password.as_ref().map(crate::Secret::expose),
crate::secret::expected("$9$secret")
);
let group = &subscribers.groups[0];
assert_eq!(group.name, "STATIC1");
assert_eq!(group.access_profile.as_deref(), Some("AP1"));
assert_eq!(
group.password.as_ref().map(crate::Secret::expose),
crate::secret::expected("$9$other")
);
assert_eq!(group.dynamic_profiles[0].name, "PROFILE1");
assert_eq!(group.dynamic_profiles[0].aggregate_clients.as_deref(), Some("merge"));
assert_eq!(
group.interfaces,
["ge-1/0/0.100 upto ge-1/0/0.200", "ge-1/0/0.150 exclude"]
);
}
#[test]
fn extracts_dhcp_local_server_groups() {
let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
let server = system.services.unwrap().dhcp_local_server.unwrap();
assert_eq!(server.groups.len(), 1);
assert_eq!(server.groups[0].name, "G1");
assert_eq!(
server.groups[0].interfaces,
["ge-1/0/0.0", "ge-1/0/1.0 upto ge-1/0/1.9"]
);
assert_eq!(server.dhcpv6_groups[0].name, "V6");
assert_eq!(server.dhcpv6_groups[0].interfaces, ["ge-2/0/0.0"]);
}
#[test]
fn extracts_aacl_statistics_profiles() {
let system = system(&juniper::parse_braces(SUBSCRIBERS)).unwrap();
let function = system.services.unwrap().local_policy_decision_function.unwrap();
let profile = &function.statistics[0];
assert_eq!(profile.name, "AACL1");
assert_eq!(profile.aacl_fields, ["input-bytes", "output-bytes"]);
assert_eq!(profile.file.as_deref(), Some("aacl.stats"));
assert_eq!(profile.record_mode.as_deref(), Some("interim-full"));
assert_eq!(profile.report_interval.as_deref(), Some("30"));
}
#[test]
fn subscriber_settings_are_optional() {
let system = system(&juniper::parse_braces(
"system {\n services {\n ssh;\n }\n}\n",
))
.unwrap();
assert!(system.dynamic_profile_options.is_none());
let services = system.services.unwrap();
assert!(services.subscriber_management.is_none());
assert!(services.static_subscribers.is_none());
assert!(services.dhcp_local_server.is_none());
assert!(services.local_policy_decision_function.is_none());
}
}