logfence-daemon 0.1.0

Validating syslog filter daemon — forwards valid JSON messages to rsyslog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//! Forwarding validated syslog messages to rsyslog.
//!
//! [`Forwarder`] wraps the configured rsyslog transport and serializes
//! [`SyslogMessage`] values to the wire format expected by rsyslog.
//!
//! Two transports are supported:
//!
//! - `unix_dgram` — sends each message as a Unix datagram (default; matches
//!   rsyslog `imuxsock` on Linux/macOS).
//! - `unix_stream` — sends each message with RFC 6587 octet-count framing
//!   over a Unix stream socket.
//!
//! ## Cloning behaviour
//!
//! `Forwarder` is cheaply cloneable and safe to pass to Tokio tasks.
//!
//! For `unix_dgram` output, all clones share a single unconnected socket via
//! `Arc`; no per-message connection setup is needed and `send_to` is lock-free.
//!
//! For `unix_stream` output, each clone holds an **independent** connection
//! slot (`Mutex<None>` on construction, lazily connected on first use).
//! Session tasks and worker tasks that each hold their own cloned `Forwarder`
//! therefore maintain separate persistent connections to rsyslog, allowing
//! writes to proceed in parallel without contending on a shared mutex.

use std::{path::Path, sync::Arc, time::Duration};

use thiserror::Error;
use tokio::{io::AsyncWriteExt, net::unix::OwnedWriteHalf, net::UnixStream, sync::Mutex};
use tokio_util::sync::CancellationToken;
use tracing::{debug, error};

use logfence_proto::syslog::SyslogMessage;

use crate::config::{DgramExhausted, ForwardTransport, RsyslogConfig};

// ── Datagram retry ────────────────────────────────────────────────────────────

fn is_buffer_full(e: &std::io::Error) -> bool {
    // WouldBlock: EAGAIN/EWOULDBLOCK — socket send buffer temporarily full.
    // Raw 105: ENOBUFS on Linux (all asm-generic architectures).
    // Raw 55: ENOBUFS on macOS/BSD.
    // ErrorKind::NoBufferSpace is not yet stable in the current toolchain;
    // check the raw OS error number directly.
    matches!(e.kind(), std::io::ErrorKind::WouldBlock) || matches!(e.raw_os_error(), Some(105 | 55))
}

// Delay before the Nth attempt (1-indexed; no delay before attempt 1).
// Starts at 100 µs for attempt 2 and doubles each subsequent attempt,
// capped at 1 s.
fn dgram_attempt_delay(attempt: u32) -> Duration {
    let max = Duration::from_secs(1);
    let shift = attempt.saturating_sub(2);
    let micros = 1u64
        .checked_shl(shift)
        .map_or(u64::MAX, |v| 100u64.saturating_mul(v));
    let delay = Duration::from_micros(micros);
    if delay > max {
        max
    } else {
        delay
    }
}

// Send `data` to `path`, retrying on buffer-full errors up to `max_attempts`
// total tries.  `max_attempts = 0` means unlimited.  Non-retryable errors are
// returned immediately without consuming any retry budget.
//
// Uses `try_send_to` (non-blocking) rather than `send_to` (async, waits for
// writability) so that EAGAIN/ENOBUFS reaches `is_buffer_full` and the retry
// schedule runs under our control instead of Tokio's internal re-queue.
async fn send_dgram_with_retry(
    socket: &tokio::net::UnixDatagram,
    data: &[u8],
    path: &Path,
    max_attempts: u32,
) -> std::io::Result<()> {
    let mut last_err = match socket.try_send_to(data, path) {
        Ok(_) => return Ok(()),
        Err(e) if !is_buffer_full(&e) => return Err(e),
        Err(e) => e,
    };
    let mut attempt = 2u32;
    loop {
        if max_attempts != 0 && attempt > max_attempts {
            break;
        }
        tokio::time::sleep(dgram_attempt_delay(attempt)).await;
        match socket.try_send_to(data, path) {
            Ok(_) => return Ok(()),
            Err(e) if !is_buffer_full(&e) => return Err(e),
            Err(e) => last_err = e,
        }
        attempt = attempt.saturating_add(1);
    }
    Err(last_err)
}

// ── Error ─────────────────────────────────────────────────────────────────────

/// Errors that can occur while forwarding a message.
#[derive(Debug, Error)]
pub enum ForwardError {
    /// An I/O error occurred while sending to rsyslog.
    #[error("I/O error forwarding to rsyslog: {0}")]
    Io(#[from] std::io::Error),
}

// ── Forwarder ─────────────────────────────────────────────────────────────────

/// Sends validated syslog messages to rsyslog.
///
/// The underlying connection is managed internally and reconnected on error.
/// See the module-level documentation for cloning behaviour.
#[derive(Clone)]
pub struct Forwarder {
    inner: Inner,
    dgram_max_attempts: u32,
    dgram_exhausted: DgramExhausted,
    /// Cancelled when retries are exhausted with `dgram_exhausted = Terminate`.
    shutdown: Option<CancellationToken>,
}

// Datagram state is shared across all clones (one unconnected socket).
struct DgramConn {
    socket: tokio::net::UnixDatagram,
    path: String,
}

// Stream state is NOT shared: each clone of `Forwarder` holds its own
// independent connection slot.
struct StreamConn {
    path: String,
    /// Only the write half is retained after connecting.  The read half is
    /// dropped (and `shutdown(SHUT_RD)` issued) immediately after the OS
    /// connection is established.
    stream: Mutex<Option<OwnedWriteHalf>>,
}

impl Clone for StreamConn {
    fn clone(&self) -> Self {
        Self {
            path: self.path.clone(),
            stream: Mutex::new(None), // fresh, independent connection slot
        }
    }
}

enum Inner {
    UnixDgram(Arc<DgramConn>),
    UnixStream(StreamConn),
}

impl Clone for Inner {
    fn clone(&self) -> Self {
        match self {
            Inner::UnixDgram(arc) => Inner::UnixDgram(Arc::clone(arc)),
            Inner::UnixStream(conn) => Inner::UnixStream(conn.clone()),
        }
    }
}

impl Forwarder {
    /// Build a [`Forwarder`] from a [`RsyslogConfig`].
    ///
    /// `shutdown` is cancelled when `dgram_exhausted = "terminate"` and all
    /// send attempts are exhausted.  Pass `None` to use `"drop"` semantics
    /// regardless of the config setting.
    ///
    /// # Errors
    ///
    /// Returns [`ForwardError::Io`] if the socket cannot be created.
    pub fn from_config(
        cfg: &RsyslogConfig,
        shutdown: Option<CancellationToken>,
    ) -> Result<Self, ForwardError> {
        let inner = match cfg.transport {
            ForwardTransport::UnixDgram => {
                let socket = tokio::net::UnixDatagram::unbound()?;
                // Enforce write-only direction at OS level. macOS returns ENOTCONN
                // for unconnected datagram sockets; safe to ignore since an unbound
                // socket has no address and cannot receive unsolicited data.
                if let Err(e) = socket.shutdown(std::net::Shutdown::Read) {
                    if e.kind() != std::io::ErrorKind::NotConnected {
                        return Err(e.into());
                    }
                }
                Inner::UnixDgram(Arc::new(DgramConn {
                    socket,
                    path: cfg.socket.clone(),
                }))
            }
            ForwardTransport::UnixStream => Inner::UnixStream(StreamConn {
                path: cfg.socket.clone(),
                stream: Mutex::new(None),
            }),
        };
        Ok(Self {
            inner,
            dgram_max_attempts: cfg.dgram_max_attempts,
            dgram_exhausted: cfg.dgram_exhausted,
            shutdown,
        })
    }

    /// Forward a validated [`SyslogMessage`] to rsyslog.
    ///
    /// # Errors
    ///
    /// Returns [`ForwardError::Io`] on any I/O failure. Unix stream connections
    /// are re-established automatically on the next call after a failure.
    pub async fn forward(&self, msg: &SyslogMessage) -> Result<(), ForwardError> {
        let wire = msg.to_string();
        match &self.inner {
            Inner::UnixDgram(conn) => {
                let result = send_dgram_with_retry(
                    &conn.socket,
                    wire.as_bytes(),
                    Path::new(&conn.path),
                    self.dgram_max_attempts,
                )
                .await;
                match result {
                    Ok(()) => debug!(bytes = wire.len(), "forwarded via unix_dgram"),
                    Err(e) => {
                        if is_buffer_full(&e) && self.dgram_exhausted == DgramExhausted::Terminate {
                            error!(
                                error = %e,
                                "datagram retries exhausted; initiating graceful shutdown"
                            );
                            if let Some(token) = &self.shutdown {
                                token.cancel();
                            }
                        }
                        return Err(ForwardError::Io(e));
                    }
                }
            }
            Inner::UnixStream(conn) => {
                let frame = format!("{} {wire}", wire.len());
                let mut guard = conn.stream.lock().await;
                if guard.is_none() {
                    // Connect, then enforce write-only direction by shutting
                    // down the read half at the OS level before splitting.
                    let raw = UnixStream::connect(&conn.path).await?;
                    let std_raw = raw.into_std()?;
                    std_raw.shutdown(std::net::Shutdown::Read)?;
                    let raw = UnixStream::from_std(std_raw)?;
                    let (_, write_half) = raw.into_split();
                    *guard = Some(write_half);
                }
                let Some(s) = guard.as_mut() else {
                    return Err(ForwardError::Io(std::io::Error::other(
                        "internal: unix stream not initialised",
                    )));
                };
                if let Err(e) = s.write_all(frame.as_bytes()).await {
                    *guard = None;
                    return Err(ForwardError::Io(e));
                }
                debug!(bytes = wire.len(), "forwarded via unix_stream");
            }
        }
        Ok(())
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    reason = "unwrap is appropriate in test assertions"
)]
mod tests {
    use std::time::Duration;

    use tokio::io::AsyncReadExt;
    use tokio::net::{UnixDatagram, UnixListener};

    use logfence_proto::syslog::{Facility, Priority, Severity};

    use super::*;
    use crate::config::{ForwardTransport, RsyslogConfig};

    fn sample_msg() -> SyslogMessage {
        SyslogMessage {
            priority: Priority {
                facility: Facility::Local0,
                severity: Severity::Info,
            },
            timestamp: None,
            hostname: None,
            app_name: Some("test".into()),
            proc_id: None,
            msg_id: None,
            structured_data: "-".into(),
            msg: r#"{"k":"v"}"#.into(),
        }
    }

    fn rsyslog_cfg(transport: ForwardTransport, socket: &str) -> RsyslogConfig {
        RsyslogConfig {
            transport,
            socket: socket.to_owned(),
            ..Default::default()
        }
    }

    /// Fill a tiny receive buffer, spawn a drain thread that sleeps 200 µs
    /// before emptying it, then call `forward()`.  The first retry at 100 µs
    /// finds the buffer still full; the second retry at 600 µs (100+500) finds
    /// it drained and succeeds.  This proves the retry loop actually runs.
    #[tokio::test]
    async fn dgram_forward_retries_on_buffer_full() {
        let dir = tempfile::tempdir().unwrap();
        let sock_path = dir.path().join("rsyslog.sock");

        let receiver = std::os::unix::net::UnixDatagram::bind(&sock_path).unwrap();
        // Shrink the receive buffer to the kernel minimum so it fills quickly.
        socket2::SockRef::from(&receiver)
            .set_recv_buffer_size(4096)
            .unwrap();

        // Fill the receive buffer with 1-byte messages until ENOBUFS.
        let filler = std::os::unix::net::UnixDatagram::unbound().unwrap();
        filler.set_nonblocking(true).unwrap();
        let mut fill_count = 0usize;
        loop {
            match filler.send_to(&[0u8], &sock_path) {
                Ok(_) => {
                    fill_count += 1;
                    assert!(fill_count < 100_000, "socket buffer never filled");
                }
                Err(ref e) if is_buffer_full(e) => break,
                Err(ref e) => {
                    assert!(is_buffer_full(e), "unexpected fill error: {e}");
                    break;
                }
            }
        }
        assert!(
            fill_count > 0,
            "expected at least one fill message to succeed"
        );

        // Drain after 200 µs — longer than the first retry delay (100 µs) but
        // shorter than the second (100+500 = 600 µs), so exactly one retry fails.
        let drainer = receiver.try_clone().unwrap();
        std::thread::spawn(move || {
            std::thread::sleep(Duration::from_micros(200));
            let mut buf = vec![0u8; 65_536];
            drainer.set_nonblocking(true).unwrap();
            while drainer.recv(&mut buf).is_ok() {}
        });

        let cfg = rsyslog_cfg(ForwardTransport::UnixDgram, sock_path.to_str().unwrap());
        let forwarder = Forwarder::from_config(&cfg, None).unwrap();
        tokio::time::timeout(Duration::from_millis(200), forwarder.forward(&sample_msg()))
            .await
            .unwrap()
            .unwrap();
    }

    #[tokio::test]
    async fn unix_dgram_forward() {
        let dir = tempfile::tempdir().unwrap();
        let sock_path = dir.path().join("rsyslog.sock");

        let receiver = UnixDatagram::bind(&sock_path).unwrap();
        let cfg = rsyslog_cfg(ForwardTransport::UnixDgram, sock_path.to_str().unwrap());
        let forwarder = Forwarder::from_config(&cfg, None).unwrap();

        let msg = sample_msg();
        let expected = msg.to_string();
        forwarder.forward(&msg).await.unwrap();

        let mut buf = vec![0u8; 4096];
        let n = tokio::time::timeout(Duration::from_secs(1), receiver.recv(&mut buf))
            .await
            .unwrap()
            .unwrap();
        let received = std::str::from_utf8(&buf[..n]).unwrap();
        assert_eq!(received, expected);
    }

    #[tokio::test]
    async fn unix_stream_forward_octet_count() {
        let dir = tempfile::tempdir().unwrap();
        let sock_path = dir.path().join("rsyslog.sock");
        let listener = UnixListener::bind(&sock_path).unwrap();

        let cfg = rsyslog_cfg(ForwardTransport::UnixStream, sock_path.to_str().unwrap());
        let forwarder = Forwarder::from_config(&cfg, None).unwrap();

        let msg = sample_msg();
        let expected_wire = msg.to_string();

        let send_task = tokio::spawn(async move { forwarder.forward(&msg).await.unwrap() });
        let (mut conn, _) = tokio::time::timeout(Duration::from_secs(1), listener.accept())
            .await
            .unwrap()
            .unwrap();

        let mut buf = vec![0u8; 4096];
        let n = tokio::time::timeout(Duration::from_secs(1), conn.read(&mut buf))
            .await
            .unwrap()
            .unwrap();
        let received = std::str::from_utf8(&buf[..n]).unwrap();

        let (count_str, body) = received.split_once(' ').unwrap();
        assert_eq!(count_str.parse::<usize>().unwrap(), expected_wire.len());
        assert_eq!(body, expected_wire);

        send_task.await.unwrap();
    }

    #[tokio::test]
    async fn clone_creates_independent_stream_connection() {
        let dir = tempfile::tempdir().unwrap();
        let sock_path = dir.path().join("rsyslog.sock");
        let listener = UnixListener::bind(&sock_path).unwrap();

        let cfg = rsyslog_cfg(ForwardTransport::UnixStream, sock_path.to_str().unwrap());
        let f1 = Forwarder::from_config(&cfg, None).unwrap();
        let f2 = f1.clone();

        let msg = sample_msg();

        // Forward from both clones concurrently.
        let m1 = msg.clone();
        let m2 = msg.clone();
        let (r1, r2) = tokio::join!(
            tokio::spawn(async move { f1.forward(&m1).await.unwrap() }),
            tokio::spawn(async move { f2.forward(&m2).await.unwrap() }),
        );
        r1.unwrap();
        r2.unwrap();

        // Both clones must have established independent connections: the listener
        // should have accepted exactly two connections.
        let mut accepted = 0usize;
        for _ in 0..2 {
            tokio::time::timeout(Duration::from_secs(1), listener.accept())
                .await
                .unwrap()
                .unwrap();
            accepted += 1;
        }
        assert_eq!(accepted, 2, "expected two independent stream connections");
    }

    /// When `dgram_exhausted = Terminate` and retries are exhausted on a full
    /// buffer, `forward()` must return an error AND cancel the shutdown token.
    #[tokio::test]
    async fn dgram_exhausted_terminate_cancels_shutdown_token() {
        let dir = tempfile::tempdir().unwrap();
        let sock_path = dir.path().join("rsyslog.sock");

        let receiver = std::os::unix::net::UnixDatagram::bind(&sock_path).unwrap();
        socket2::SockRef::from(&receiver)
            .set_recv_buffer_size(4096)
            .unwrap();

        // Fill the buffer so the very first send attempt hits ENOBUFS.
        let filler = std::os::unix::net::UnixDatagram::unbound().unwrap();
        filler.set_nonblocking(true).unwrap();
        let mut fill_count = 0usize;
        loop {
            match filler.send_to(&[0u8], &sock_path) {
                Ok(_) => {
                    fill_count += 1;
                    assert!(fill_count < 100_000, "socket buffer never filled");
                }
                Err(ref e) if is_buffer_full(e) => break,
                Err(ref e) => {
                    assert!(is_buffer_full(e), "unexpected fill error: {e}");
                    break;
                }
            }
        }
        assert!(
            fill_count > 0,
            "expected at least one fill message to succeed"
        );

        let shutdown = CancellationToken::new();
        let cfg = RsyslogConfig {
            transport: ForwardTransport::UnixDgram,
            socket: sock_path.to_str().unwrap().to_owned(),
            dgram_max_attempts: 1, // exhaust after the single immediate attempt
            dgram_exhausted: DgramExhausted::Terminate,
        };
        let forwarder = Forwarder::from_config(&cfg, Some(shutdown.clone())).unwrap();

        let result =
            tokio::time::timeout(Duration::from_millis(500), forwarder.forward(&sample_msg()))
                .await
                .unwrap();

        assert!(result.is_err(), "forward() must fail when buffer is full");
        assert!(
            shutdown.is_cancelled(),
            "shutdown token must be cancelled on exhaustion with Terminate"
        );
    }
}