1use crate::{active_children, arg_of, child_block, has_leaf, named_children, statement_words};
4use asterust::config::ConfigLine;
5
6#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct SyslogRule {
10 pub facility: String,
12 pub severity: String,
14}
15
16#[derive(Clone, Debug, PartialEq, Eq)]
18#[non_exhaustive]
19pub struct SyslogHost {
20 pub address: String,
22 pub rules: Vec<SyslogRule>,
23 pub port: Option<String>,
24 pub source_address: Option<String>,
25 pub routing_instance: Option<String>,
26 pub facility_override: Option<String>,
27 pub log_prefix: Option<String>,
28 pub allow_duplicates: bool,
29 pub explicit_priority: bool,
30 pub exclude_hostname: bool,
31 pub match_regex: Option<String>,
33}
34
35#[derive(Clone, Debug, PartialEq, Eq)]
37#[non_exhaustive]
38pub struct SyslogStructuredData {
39 pub brief: bool,
41}
42
43#[derive(Clone, Debug, Default, PartialEq, Eq)]
45#[non_exhaustive]
46pub struct SyslogArchive {
47 pub files: Option<String>,
49 pub size: Option<String>,
51 pub world_readable: Option<bool>,
53}
54
55#[derive(Clone, Debug, PartialEq, Eq)]
57#[non_exhaustive]
58pub struct SyslogTarget {
59 pub name: String,
61 pub rules: Vec<SyslogRule>,
62 pub archive: Option<SyslogArchive>,
64 pub allow_duplicates: bool,
65 pub match_regex: Option<String>,
67 pub explicit_priority: bool,
69 pub structured_data: Option<SyslogStructuredData>,
71}
72
73#[derive(Clone, Debug, PartialEq, Eq)]
75#[non_exhaustive]
76pub struct Syslog {
77 pub hosts: Vec<SyslogHost>,
78 pub files: Vec<SyslogTarget>,
79 pub users: Vec<SyslogTarget>,
80 pub console: Vec<SyslogRule>,
81 pub source_address: Option<String>,
82 pub time_format: Option<String>,
84 pub archive: Option<SyslogArchive>,
86 pub allow_duplicates: bool,
87 pub log_rotate_frequency: Option<String>,
88}
89
90pub(super) fn parse_syslog(syslog: &ConfigLine) -> Syslog {
91 Syslog {
92 hosts: named_children(syslog, "host")
93 .map(|(address, host)| SyslogHost {
94 address: address.to_string(),
95 rules: syslog_rules(host),
96 port: arg_of(host, "port").map(str::to_string),
97 source_address: arg_of(host, "source-address").map(str::to_string),
98 routing_instance: arg_of(host, "routing-instance").map(str::to_string),
99 facility_override: arg_of(host, "facility-override").map(str::to_string),
100 log_prefix: arg_of(host, "log-prefix").map(str::to_string),
101 allow_duplicates: has_leaf(host, "allow-duplicates"),
102 explicit_priority: has_leaf(host, "explicit-priority"),
103 exclude_hostname: has_leaf(host, "exclude-hostname"),
104 match_regex: arg_of(host, "match").map(str::to_string),
105 })
106 .collect(),
107 files: syslog_targets(syslog, "file"),
108 users: syslog_targets(syslog, "user"),
109 console: child_block(syslog, "console").map(syslog_rules).unwrap_or_default(),
110 source_address: arg_of(syslog, "source-address").map(str::to_string),
111 time_format: arg_of(syslog, "time-format").map(str::to_string),
112 archive: syslog_archive(syslog),
113 allow_duplicates: has_leaf(syslog, "allow-duplicates"),
114 log_rotate_frequency: arg_of(syslog, "log-rotate-frequency").map(str::to_string),
115 }
116}
117
118fn syslog_targets(syslog: &ConfigLine, keyword: &str) -> Vec<SyslogTarget> {
119 named_children(syslog, keyword)
120 .map(|(name, target)| SyslogTarget {
121 name: name.to_string(),
122 rules: syslog_rules(target),
123 archive: syslog_archive(target),
124 allow_duplicates: has_leaf(target, "allow-duplicates"),
125 match_regex: arg_of(target, "match").map(str::to_string),
126 explicit_priority: has_leaf(target, "explicit-priority"),
127 structured_data: child_block(target, "structured-data").map(|data| SyslogStructuredData {
128 brief: has_leaf(data, "brief"),
129 }),
130 })
131 .collect()
132}
133
134fn syslog_archive(parent: &ConfigLine) -> Option<SyslogArchive> {
136 let mut words = statement_words(parent, "archive")?.into_iter();
137 let mut settings = SyslogArchive::default();
138 while let Some(word) = words.next() {
139 match word {
140 "files" => settings.files = words.next().map(str::to_string),
141 "size" => settings.size = words.next().map(str::to_string),
142 "world-readable" => settings.world_readable = Some(true),
143 "no-world-readable" => settings.world_readable = Some(false),
144 _ => {}
145 }
146 }
147 Some(settings)
148}
149
150fn syslog_rules(destination: &ConfigLine) -> Vec<SyslogRule> {
152 const SEVERITIES: [&str; 10] = [
153 "any",
154 "none",
155 "emergency",
156 "alert",
157 "critical",
158 "error",
159 "warning",
160 "notice",
161 "info",
162 "debug",
163 ];
164 active_children(destination)
165 .filter_map(|c| {
166 let (facility, severity) = c.text.split_once(' ')?;
167 SEVERITIES.contains(&severity).then(|| SyslogRule {
168 facility: facility.to_string(),
169 severity: severity.to_string(),
170 })
171 })
172 .collect()
173}
174
175#[cfg(test)]
176mod tests {
177 use crate::system::system;
178 use asterust::config::juniper;
179
180 const SYSLOG: &str = r#"
181system {
182 syslog {
183 user * {
184 any emergency;
185 }
186 host 192.0.2.5 {
187 any notice;
188 authorization info;
189 port 514;
190 source-address 192.0.2.1;
191 routing-instance mgmt_junos;
192 facility-override local3;
193 log-prefix pe1;
194 }
195 host other-routing-engine {
196 any critical;
197 }
198 file messages {
199 any notice;
200 authorization info;
201 explicit-priority;
202 }
203 file interactive-commands {
204 interactive-commands any;
205 }
206 inactive: file debug {
207 any debug;
208 }
209 console {
210 any critical;
211 }
212 source-address 192.0.2.1;
213 time-format year millisecond;
214 }
215}
216"#;
217
218 #[test]
219 fn extracts_syslog_hosts() {
220 let syslog = system(&juniper::parse_braces(SYSLOG)).unwrap().syslog.unwrap();
221 let addresses: Vec<_> = syslog.hosts.iter().map(|h| h.address.as_str()).collect();
222 assert_eq!(addresses, ["192.0.2.5", "other-routing-engine"]);
223
224 let host = &syslog.hosts[0];
225 assert_eq!(host.rules.len(), 2);
226 assert_eq!(host.rules[1].facility, "authorization");
227 assert_eq!(host.rules[1].severity, "info");
228 assert_eq!(host.port.as_deref(), Some("514"));
229 assert_eq!(host.source_address.as_deref(), Some("192.0.2.1"));
230 assert_eq!(host.routing_instance.as_deref(), Some("mgmt_junos"));
231 assert_eq!(host.facility_override.as_deref(), Some("local3"));
232 assert_eq!(host.log_prefix.as_deref(), Some("pe1"));
233 }
234
235 #[test]
236 fn extracts_syslog_files_users_and_console() {
237 let syslog = system(&juniper::parse_braces(SYSLOG)).unwrap().syslog.unwrap();
238
239 let files: Vec<_> = syslog.files.iter().map(|f| f.name.as_str()).collect();
240 assert_eq!(files, ["messages", "interactive-commands"]);
241 assert_eq!(syslog.files[0].rules.len(), 2);
243 assert_eq!(syslog.files[1].rules[0].facility, "interactive-commands");
244 assert_eq!(syslog.files[1].rules[0].severity, "any");
245
246 assert_eq!(syslog.users[0].name, "*");
247 assert_eq!(syslog.users[0].rules[0].severity, "emergency");
248 assert_eq!(syslog.console[0].facility, "any");
249 assert_eq!(syslog.console[0].severity, "critical");
250 }
251
252 #[test]
253 fn extracts_syslog_global_settings() {
254 let syslog = system(&juniper::parse_braces(SYSLOG)).unwrap().syslog.unwrap();
255 assert_eq!(syslog.source_address.as_deref(), Some("192.0.2.1"));
256 assert_eq!(syslog.time_format.as_deref(), Some("year millisecond"));
257 }
258
259 #[test]
260 fn extracts_syslog_archive_in_line_and_block_form() {
261 let syslog = system(&juniper::parse_braces(
262 r#"
263system {
264 syslog {
265 archive {
266 files 20;
267 size 5m;
268 world-readable;
269 }
270 file messages {
271 any notice;
272 archive size 100k files 10 no-world-readable;
273 }
274 file plain {
275 any info;
276 }
277 }
278}
279"#,
280 ))
281 .unwrap()
282 .syslog
283 .unwrap();
284
285 let default = syslog.archive.unwrap();
286 assert_eq!(default.files.as_deref(), Some("20"));
287 assert_eq!(default.size.as_deref(), Some("5m"));
288 assert_eq!(default.world_readable, Some(true));
289
290 let messages = syslog.files[0].archive.as_ref().unwrap();
291 assert_eq!(messages.files.as_deref(), Some("10"));
292 assert_eq!(messages.size.as_deref(), Some("100k"));
293 assert_eq!(messages.world_readable, Some(false));
294
295 assert_eq!(syslog.files[1].archive, None);
296 }
297
298 #[test]
299 fn extracts_syslog_destination_options() {
300 let syslog = system(&juniper::parse_braces(
301 r#"
302system {
303 syslog {
304 allow-duplicates;
305 log-rotate-frequency 5;
306 host 192.0.2.5 {
307 any notice;
308 allow-duplicates;
309 explicit-priority;
310 exclude-hostname;
311 match "UI_COMMIT";
312 }
313 file messages {
314 any notice;
315 allow-duplicates;
316 explicit-priority;
317 match "!(ssh)";
318 structured-data {
319 brief;
320 }
321 }
322 file plain {
323 any info;
324 structured-data;
325 }
326 user admin {
327 any alert;
328 match "commit";
329 }
330 }
331}
332"#,
333 ))
334 .unwrap()
335 .syslog
336 .unwrap();
337 assert!(syslog.allow_duplicates);
338 assert_eq!(syslog.log_rotate_frequency.as_deref(), Some("5"));
339
340 let host = &syslog.hosts[0];
341 assert!(host.allow_duplicates);
342 assert!(host.explicit_priority);
343 assert!(host.exclude_hostname);
344 assert_eq!(host.match_regex.as_deref(), Some("UI_COMMIT"));
345
346 let messages = &syslog.files[0];
347 assert!(messages.allow_duplicates);
348 assert!(messages.explicit_priority);
349 assert_eq!(messages.match_regex.as_deref(), Some("!(ssh)"));
350 assert!(messages.structured_data.as_ref().unwrap().brief);
351
352 let plain = &syslog.files[1];
353 assert!(!plain.structured_data.as_ref().unwrap().brief);
354 assert!(!plain.allow_duplicates);
355 assert_eq!(plain.match_regex, None);
356
357 assert_eq!(syslog.users[0].match_regex.as_deref(), Some("commit"));
358 }
359
360 #[test]
361 fn syslog_destination_options_default_to_off() {
362 let syslog = system(&juniper::parse_braces(
363 "system {\n syslog {\n host 192.0.2.5 {\n any notice;\n }\n }\n}\n",
364 ))
365 .unwrap()
366 .syslog
367 .unwrap();
368 assert!(!syslog.allow_duplicates);
369 assert_eq!(syslog.log_rotate_frequency, None);
370 assert!(!syslog.hosts[0].explicit_priority);
371 assert_eq!(syslog.hosts[0].match_regex, None);
372 }
373
374 #[test]
375 fn no_syslog_is_none() {
376 assert!(system(&juniper::parse_braces("system {\n host-name pe1;\n}\n"))
377 .unwrap()
378 .syslog
379 .is_none());
380 }
381}