use std::collections::HashMap;
use std::fs::File;
use std::io::Write as _;
use std::path::PathBuf;
use std::sync::Mutex;
use std::sync::mpsc::Sender;
use std::time::{SystemTime, UNIX_EPOCH};
#[allow(missing_docs, clippy::all)]
#[path = "dds_security_log.rs"]
pub mod idl;
pub use idl::dds::security::{
BuiltinLoggingTypeV2, LoggingLevel, NameValuePair, NameValuePairSeq, StructuredDataEntry, TimeT,
};
pub const LOG_TOPIC: &str = "DDS:Security:LogTopicV2";
pub const FACILITY: u8 = 10;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LogOptions {
pub log_level: LoggingLevel,
pub log_file: Option<PathBuf>,
pub distribute: bool,
}
impl Default for LogOptions {
fn default() -> Self {
LogOptions {
log_level: LoggingLevel::ErrorLevel,
log_file: None,
distribute: false,
}
}
}
impl LogOptions {
pub const LOGGING_LEVEL: &'static str = "dds.sec.log.builtin.DDS_LogTopic.logging_level";
pub const LOG_FILE: &'static str = "dds.sec.log.builtin.DDS_LogTopic.log_file";
pub const DISTRIBUTE: &'static str = "dds.sec.log.builtin.DDS_LogTopic.distribute";
pub fn from_properties(props: &HashMap<&str, &str>) -> Result<Option<Self>, String> {
let level = props.get(Self::LOGGING_LEVEL);
let file = props.get(Self::LOG_FILE);
let distribute = props.get(Self::DISTRIBUTE);
if level.is_none() && file.is_none() && distribute.is_none() {
return Ok(None);
}
let mut o = LogOptions::default();
if let Some(l) = level {
o.log_level = parse_level(l)
.ok_or_else(|| format!("{}: unknown level {l}", Self::LOGGING_LEVEL))?;
}
o.log_file = file.filter(|f| !f.is_empty()).map(PathBuf::from);
if let Some(d) = distribute {
o.distribute = match d.to_ascii_lowercase().as_str() {
"true" | "1" | "yes" => true,
"false" | "0" | "no" => false,
_ => return Err(format!("{}: not a boolean: {d}", Self::DISTRIBUTE)),
};
}
Ok(Some(o))
}
}
const LEVELS: [(LoggingLevel, &str); 8] = [
(LoggingLevel::EmergencyLevel, "EMERGENCY"),
(LoggingLevel::AlertLevel, "ALERT"),
(LoggingLevel::CriticalLevel, "CRITICAL"),
(LoggingLevel::ErrorLevel, "ERROR"),
(LoggingLevel::WarningLevel, "WARNING"),
(LoggingLevel::NoticeLevel, "NOTICE"),
(LoggingLevel::InformationalLevel, "INFORMATIONAL"),
(LoggingLevel::DebugLevel, "DEBUG"),
];
pub fn parse_level(s: &str) -> Option<LoggingLevel> {
let s = s.trim().to_ascii_uppercase();
if let Ok(n) = s.parse::<usize>() {
return LEVELS.get(n).map(|(l, _)| *l);
}
let name = s.strip_suffix("_LEVEL").unwrap_or(&s);
LEVELS.iter().find(|(_, n)| *n == name).map(|(l, _)| *l)
}
pub fn level_name(level: LoggingLevel) -> String {
let n = LEVELS
.iter()
.find(|(l, _)| *l == level)
.map_or("DEBUG", |(_, n)| n);
format!("{n}_LEVEL")
}
#[derive(Clone, Copy, Debug)]
pub(crate) enum Plugin {
Authentication,
AccessControl,
}
impl Plugin {
fn name(self) -> &'static str {
match self {
Plugin::Authentication => "Authentication",
Plugin::AccessControl => "AccessControl",
}
}
}
enum Sink {
Waiting(Vec<BuiltinLoggingTypeV2>),
Writer(Sender<BuiltinLoggingTypeV2>),
Closed,
}
pub(crate) struct Logger {
options: LogOptions,
domain_id: u32,
guid: Mutex<String>,
file: Option<Mutex<File>>,
sink: Mutex<Sink>,
hostname: String,
appname: String,
procid: String,
}
impl Logger {
pub(crate) fn new(options: LogOptions, domain_id: u32) -> std::io::Result<Self> {
let file = match &options.log_file {
Some(p) => Some(Mutex::new(
std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(p)?,
)),
None => None,
};
let sink = if options.distribute {
Sink::Waiting(Vec::new())
} else {
Sink::Closed
};
Ok(Logger {
options,
domain_id,
guid: Mutex::new(String::new()),
file,
sink: Mutex::new(sink),
hostname: hostname(),
appname: std::env::current_exe()
.ok()
.and_then(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
.unwrap_or_default(),
procid: std::process::id().to_string(),
})
}
pub(crate) fn options(&self) -> &LogOptions {
&self.options
}
pub(crate) fn set_guid(&self, guid: &[u8; 16]) {
*self.guid.lock().unwrap_or_else(|e| e.into_inner()) = guid_text(guid);
}
pub(crate) fn attach(&self, tx: Sender<BuiltinLoggingTypeV2>) {
let mut s = self.sink.lock().unwrap_or_else(|e| e.into_inner());
if let Sink::Waiting(kept) = std::mem::replace(&mut *s, Sink::Closed) {
for m in kept {
let _ = tx.send(m);
}
*s = Sink::Writer(tx);
}
}
pub(crate) fn close(&self) {
*self.sink.lock().unwrap_or_else(|e| e.into_inner()) = Sink::Closed;
}
pub(crate) fn log(&self, level: LoggingLevel, plugin: Plugin, method: &str, message: &str) {
if level > self.options.log_level {
return;
}
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default();
let guid = self.guid.lock().unwrap_or_else(|e| e.into_inner()).clone();
if let Some(f) = &self.file {
let line = format!(
"{} {} {}.{} guid={guid} domain_id={}: {message}\n",
rfc3339(now.as_secs() as i64, now.subsec_nanos()),
level_name(level),
plugin.name(),
method,
self.domain_id,
);
let mut f = f.lock().unwrap_or_else(|e| e.into_inner());
let _ = f.write_all(line.as_bytes());
}
let mut sink = self.sink.lock().unwrap_or_else(|e| e.into_inner());
if matches!(*sink, Sink::Closed) {
return;
}
let pair = |n: &str, v: String| NameValuePair {
name: n.into(),
value: v,
};
let m = BuiltinLoggingTypeV2 {
facility: FACILITY,
severity: level,
timestamp: TimeT {
sec: now.as_secs() as i64,
nanosec: now.subsec_nanos(),
},
hostname: self.hostname.clone(),
hostip: String::new(),
appname: self.appname.clone(),
procid: self.procid.clone(),
msgid: format!("{}.{method}", plugin.name()),
message: message.into(),
structured_data: vec![StructuredDataEntry {
key: "DDS".into(),
value: vec![
pair("guid", guid),
pair("domain_id", self.domain_id.to_string()),
pair("plugin_class", plugin.name().into()),
pair("plugin_method", method.into()),
],
}],
};
match &mut *sink {
Sink::Waiting(kept) => {
if kept.len() < 256 {
kept.push(m);
}
}
Sink::Writer(tx) => {
if tx.send(m).is_err() {
*sink = Sink::Closed;
}
}
Sink::Closed => {}
}
}
}
pub(crate) fn guid_text(g: &[u8; 16]) -> String {
g.chunks(4)
.map(|c| c.iter().map(|b| format!("{b:02x}")).collect::<String>())
.collect::<Vec<_>>()
.join(".")
}
fn hostname() -> String {
std::env::var("HOSTNAME")
.ok()
.filter(|h| !h.is_empty())
.or_else(|| {
std::fs::read_to_string("/etc/hostname")
.ok()
.map(|h| h.trim().to_string())
})
.unwrap_or_default()
}
fn rfc3339(secs: i64, nanos: u32) -> String {
let days = secs.div_euclid(86_400);
let rem = secs.rem_euclid(86_400);
let z = days + 719_468;
let era = z.div_euclid(146_097);
let doe = z.rem_euclid(146_097);
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d = doy - (153 * mp + 2) / 5 + 1;
let m = if mp < 10 { mp + 3 } else { mp - 9 };
let y = yoe + era * 400 + i64::from(m <= 2);
format!(
"{y:04}-{m:02}-{d:02}T{:02}:{:02}:{:02}.{nanos:09}Z",
rem / 3600,
rem / 60 % 60,
rem % 60
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn levels_by_name_and_number() {
assert_eq!(parse_level("ERROR_LEVEL"), Some(LoggingLevel::ErrorLevel));
assert_eq!(parse_level("debug"), Some(LoggingLevel::DebugLevel));
assert_eq!(parse_level("0"), Some(LoggingLevel::EmergencyLevel));
assert_eq!(parse_level("8"), None);
assert_eq!(parse_level("FATAL"), None);
assert_eq!(level_name(LoggingLevel::NoticeLevel), "NOTICE_LEVEL");
}
#[test]
fn options_from_properties() {
let none = HashMap::new();
assert_eq!(LogOptions::from_properties(&none), Ok(None));
let p: HashMap<&str, &str> = [
(LogOptions::LOGGING_LEVEL, "WARNING_LEVEL"),
(LogOptions::LOG_FILE, "/tmp/x.log"),
(LogOptions::DISTRIBUTE, "true"),
]
.into();
assert_eq!(
LogOptions::from_properties(&p),
Ok(Some(LogOptions {
log_level: LoggingLevel::WarningLevel,
log_file: Some("/tmp/x.log".into()),
distribute: true,
}))
);
let p: HashMap<&str, &str> = [(LogOptions::DISTRIBUTE, "maybe")].into();
assert!(LogOptions::from_properties(&p).is_err());
}
#[test]
fn times_and_guids() {
assert_eq!(rfc3339(0, 0), "1970-01-01T00:00:00.000000000Z");
assert_eq!(rfc3339(951_782_400, 5), "2000-02-29T00:00:00.000000005Z");
assert_eq!(rfc3339(1_794_787_199, 0), "2026-11-15T23:59:59.000000000Z");
let mut g = [0u8; 16];
g[0] = 0x01;
g[15] = 0xc1;
assert_eq!(guid_text(&g), "01000000.00000000.00000000.000000c1");
}
#[test]
fn messages_below_the_level_are_left_out() {
let dir = std::env::temp_dir().join(format!("rustorb-dds-log-{}", std::process::id()));
let _ = std::fs::create_dir_all(&dir);
let path = dir.join("security.log");
let _ = std::fs::remove_file(&path);
let (tx, rx) = std::sync::mpsc::channel();
let l = Logger::new(
LogOptions {
log_level: LoggingLevel::WarningLevel,
log_file: Some(path.clone()),
distribute: true,
},
3,
)
.unwrap();
l.log(
LoggingLevel::ErrorLevel,
Plugin::Authentication,
"m1",
"kept before",
);
l.set_guid(&[1; 16]);
l.attach(tx);
l.log(
LoggingLevel::InformationalLevel,
Plugin::AccessControl,
"m2",
"dropped",
);
l.log(
LoggingLevel::WarningLevel,
Plugin::AccessControl,
"m3",
"sent",
);
let got: Vec<BuiltinLoggingTypeV2> = rx.try_iter().collect();
assert_eq!(got.len(), 2);
assert_eq!(got[0].message, "kept before");
assert_eq!(got[1].severity, LoggingLevel::WarningLevel);
assert_eq!(got[1].facility, 10);
let dds = &got[1].structured_data[0];
assert_eq!(dds.key, "DDS");
assert_eq!(dds.value[0].value, "01010101.01010101.01010101.01010101");
assert_eq!(dds.value[3].value, "m3");
l.close();
l.log(
LoggingLevel::ErrorLevel,
Plugin::AccessControl,
"m4",
"file only",
);
assert_eq!(rx.try_iter().count(), 0);
let text = std::fs::read_to_string(&path).unwrap();
let lines: Vec<&str> = text.lines().collect();
assert_eq!(lines.len(), 3, "{text}");
assert!(lines[0].contains("ERROR_LEVEL Authentication.m1 guid= domain_id=3: kept before"));
assert!(lines[2].contains("AccessControl.m4"));
let _ = std::fs::remove_dir_all(&dir);
}
}