etchdns 0.9.1

Caching DNS proxy and forwarding server with load balancing, failover, domain filtering, and WebAssembly plugins
Documentation
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
use crate::dns_key::DNSKey;
use log::{debug, error, info};
use std::fs::{self, File, OpenOptions};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio::sync::Mutex;

/// QueryLogger handles logging DNS queries to a file
#[derive(Debug, Clone)]
pub struct QueryLogger {
    inner: Arc<Mutex<QueryLoggerInner>>,
}

/// Inner implementation of QueryLogger
#[derive(Debug)]
struct QueryLoggerInner {
    /// The file to log to
    file: Option<File>,
    /// The path to the log file
    log_file_path: Option<PathBuf>,
    /// Whether to include the timestamp in the log
    include_timestamp: bool,
    /// Whether to include the client address in the log
    include_client_addr: bool,
    /// Whether to include the query type in the log
    include_query_type: bool,
    /// Whether to include the query class in the log
    include_query_class: bool,
    /// Maximum size in bytes before rotating log files (0 to disable size-based rotation)
    rotation_size: usize,
    /// Time interval for log rotation ("hourly", "daily", "weekly", "monthly")
    rotation_interval: String,
    /// Number of rotated log files to keep
    rotation_count: usize,
    /// Whether to compress rotated log files
    compression: bool,
    /// Last rotation time
    last_rotation_time: SystemTime,
    /// Current log file size
    current_size: usize,
}

impl QueryLogger {
    /// Check if log rotation is needed and perform rotation if necessary
    async fn check_rotation(&self) -> io::Result<()> {
        let mut inner = self.inner.lock().await;

        // Check if we need to rotate based on file size
        if inner.rotation_size > 0 && inner.current_size >= inner.rotation_size {
            debug!(
                "Log rotation triggered: file size {}/{}",
                inner.current_size, inner.rotation_size
            );
            self.rotate_log(&mut inner).await?;
            return Ok(());
        }

        // Check if we need to rotate based on time interval
        if !inner.rotation_interval.is_empty() && inner.rotation_interval != "never" {
            let now = SystemTime::now();

            // Calculate the next rotation time based on the interval
            let should_rotate = match inner.rotation_interval.as_str() {
                "hourly" => {
                    // Rotate if we've crossed an hour boundary
                    let elapsed = now
                        .duration_since(inner.last_rotation_time)
                        .unwrap_or(Duration::from_secs(0));
                    elapsed.as_secs() >= 3600 // 1 hour in seconds
                }
                "daily" => {
                    // Rotate if we've crossed a day boundary
                    let elapsed = now
                        .duration_since(inner.last_rotation_time)
                        .unwrap_or(Duration::from_secs(0));
                    elapsed.as_secs() >= 86400 // 24 hours in seconds
                }
                "weekly" => {
                    // Rotate if we've crossed a week boundary
                    let elapsed = now
                        .duration_since(inner.last_rotation_time)
                        .unwrap_or(Duration::from_secs(0));
                    elapsed.as_secs() >= 604800 // 7 days in seconds
                }
                "monthly" => {
                    // This is approximate since months vary in length
                    let elapsed = now
                        .duration_since(inner.last_rotation_time)
                        .unwrap_or(Duration::from_secs(0));
                    elapsed.as_secs() >= 2592000 // ~30 days in seconds
                }
                _ => false,
            };

            if should_rotate {
                debug!(
                    "Log rotation triggered: time interval {}",
                    inner.rotation_interval
                );
                self.rotate_log(&mut inner).await?;
                return Ok(());
            }
        }

        Ok(())
    }

    /// Rotate the log file
    async fn rotate_log(&self, inner: &mut QueryLoggerInner) -> io::Result<()> {
        // Can't rotate if we don't have a log file
        if inner.file.is_none() || inner.log_file_path.is_none() {
            return Ok(());
        }

        let log_path = inner.log_file_path.as_ref().unwrap();
        let log_path_str = log_path.to_string_lossy();

        // Manage rotation - first remove old files if we're at the limit
        if inner.rotation_count > 0 {
            // Look for existing rotated files
            let log_dir = log_path.parent().unwrap_or_else(|| Path::new("."));
            let log_name = log_path.file_name().unwrap_or_default().to_string_lossy();

            let rotated_prefix = format!("{log_name}.");
            if let Ok(entries) = fs::read_dir(log_dir) {
                let mut rotated_files: Vec<PathBuf> = entries
                    .filter_map(Result::ok)
                    .filter(|entry| {
                        let file_name = entry.file_name().to_string_lossy().to_string();
                        file_name
                            .strip_prefix(&rotated_prefix)
                            .map(|suffix| {
                                suffix.chars().all(|c| c.is_ascii_digit())
                                    || suffix
                                        .strip_suffix(".gz")
                                        .is_some_and(|s| s.chars().all(|c| c.is_ascii_digit()))
                            })
                            .unwrap_or(false)
                    })
                    .map(|entry| entry.path())
                    .collect();

                // Sort by modification time, oldest first
                rotated_files.sort_by(|a, b| {
                    let a_modified = fs::metadata(a)
                        .and_then(|m| m.modified())
                        .unwrap_or(SystemTime::UNIX_EPOCH);
                    let b_modified = fs::metadata(b)
                        .and_then(|m| m.modified())
                        .unwrap_or(SystemTime::UNIX_EPOCH);
                    a_modified.cmp(&b_modified)
                });

                // Remove oldest files if we have more than rotation_count
                while rotated_files.len() >= inner.rotation_count {
                    if let Some(oldest) = rotated_files.first() {
                        if let Err(e) = fs::remove_file(oldest) {
                            error!("Failed to remove old log file {}: {}", oldest.display(), e);
                        } else {
                            debug!("Removed old log file: {}", oldest.display());
                        }
                        rotated_files.remove(0);
                    }
                }
            }
        }

        // Generate timestamp for the rotated log file
        let now = SystemTime::now();
        let timestamp = now
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::from_secs(0))
            .as_secs();

        // Create the new filename with timestamp
        let new_filename = format!("{log_path_str}.{timestamp}");
        let new_path = PathBuf::from(&new_filename);

        // Rename the current log file
        if let Err(e) = fs::rename(log_path, &new_path) {
            error!("Failed to rename log file from {log_path_str} to {new_filename}: {e}");
            // Keep writing to the open file, which we may not be allowed to reopen, and try
            // again at the next rotation
            inner.current_size = 0;
            inner.last_rotation_time = now;
            return Ok(());
        }
        inner.file = None;

        // Compress the rotated file if compression is enabled
        if inner.compression {
            debug!("Compressing rotated log file: {new_filename}");
            // Use gzip for compression if available
            let gzip_output = Command::new("gzip").arg(&new_filename).output();

            match gzip_output {
                Ok(output) => {
                    if !output.status.success() {
                        error!(
                            "Failed to compress log file: {}",
                            String::from_utf8_lossy(&output.stderr)
                        );
                    } else {
                        debug!("Successfully compressed log file to {new_filename}.gz");
                    }
                }
                Err(e) => {
                    error!("Failed to run gzip: {e}");
                }
            }
        }

        // Create a new log file
        match OpenOptions::new().create(true).append(true).open(log_path) {
            Ok(file) => {
                inner.file = Some(file);
                inner.current_size = 0;
                inner.last_rotation_time = now;
                debug!("Rotated log file: created new file at {log_path_str}");
            }
            Err(e) => {
                error!("Failed to create new log file after rotation: {e}");
            }
        }

        Ok(())
    }

    /// Create a new QueryLogger
    pub fn new(
        log_file_path: Option<String>,
        include_timestamp: bool,
        include_client_addr: bool,
        include_query_type: bool,
        include_query_class: bool,
        rotation_size: usize,
        rotation_interval: String,
        rotation_count: usize,
        compression: bool,
    ) -> Self {
        let file = match log_file_path {
            Some(ref path) => {
                let path_buf = PathBuf::from(path);
                match OpenOptions::new().create(true).append(true).open(&path_buf) {
                    Ok(file) => {
                        info!("Query logging enabled to file: {path}");
                        Some(file)
                    }
                    Err(e) => {
                        error!("Failed to open query log file {path}: {e}");
                        None
                    }
                }
            }
            None => None,
        };

        // Get current file size if it exists
        let current_size = if let Some(ref file) = file {
            match file.metadata() {
                Ok(metadata) => metadata.len() as usize,
                Err(_) => 0,
            }
        } else {
            0
        };

        Self {
            inner: Arc::new(Mutex::new(QueryLoggerInner {
                file,
                log_file_path: log_file_path.map(PathBuf::from),
                include_timestamp,
                include_client_addr,
                include_query_type,
                include_query_class,
                rotation_size,
                rotation_interval,
                rotation_count,
                compression,
                last_rotation_time: SystemTime::now(),
                current_size,
            })),
        }
    }

    /// Log a DNS query
    pub async fn log_query(&self, key: &DNSKey, client_addr: &str) {
        // Check if we need to rotate logs
        if let Err(e) = self.check_rotation().await {
            error!("Error checking log rotation: {e}");
        }

        let mut inner = self.inner.lock().await;

        if inner.file.is_none() {
            return;
        }

        // Copy configuration values to avoid borrow issues
        let include_timestamp = inner.include_timestamp;
        let include_client_addr = inner.include_client_addr;
        let include_query_type = inner.include_query_type;
        let include_query_class = inner.include_query_class;
        let log_file_path = inner.log_file_path.clone();

        // Build the log line
        let mut log_line = String::new();

        // Add timestamp if enabled
        if include_timestamp && let Ok(time) = SystemTime::now().duration_since(UNIX_EPOCH) {
            log_line.push_str(&format!("[{}] ", time.as_secs()));
        }

        // Add client address if enabled (IP only, without port)
        if include_client_addr {
            // Extract just the IP address part without the port
            let client_ip = if let Ok(socket_addr) = client_addr.parse::<std::net::SocketAddr>() {
                socket_addr.ip().to_string()
            } else {
                // Fallback if parsing fails
                client_addr.to_string()
            };
            log_line.push_str(&format!("{client_ip} "));
        }

        // Always include the domain name
        log_line.push_str(&key.name);

        // Add query type if enabled
        if include_query_type {
            log_line.push_str(&format!(" TYPE{}", key.qtype));
        }

        // Add query class if enabled
        if include_query_class {
            log_line.push_str(&format!(" CLASS{}", key.qclass));
        }

        // Add newline
        log_line.push('\n');

        // Write to file
        if let Some(file) = &mut inner.file {
            if let Err(e) = file.write_all(log_line.as_bytes()) {
                error!("Failed to write to query log file: {e}");

                // Try to reopen the file if it was closed or moved
                if let Some(path) = &log_file_path {
                    match OpenOptions::new().create(true).append(true).open(path) {
                        Ok(new_file) => {
                            debug!("Reopened query log file: {}", path.display());
                            inner.file = Some(new_file);

                            // Try writing again
                            if let Some(file) = &mut inner.file
                                && let Err(e) = file.write_all(log_line.as_bytes())
                            {
                                error!("Failed to write to reopened query log file: {e}");
                            }
                        }
                        Err(e) => {
                            error!("Failed to reopen query log file {}: {}", path.display(), e);
                            inner.file = None;
                        }
                    }
                }
            } else {
                // Update current size for log rotation tracking
                inner.current_size += log_line.len();
            }
        }
    }

    /// Check if query logging is enabled
    #[allow(dead_code)]
    pub async fn is_enabled(&self) -> bool {
        let inner = self.inner.lock().await;
        inner.file.is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::os::unix::fs::PermissionsExt;

    #[tokio::test]
    async fn test_failed_rotation_keeps_logging() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("queries.log");
        let logger = QueryLogger::new(
            Some(path.to_string_lossy().into_owned()),
            false,
            false,
            false,
            false,
            1,
            "never".to_string(),
            7,
            false,
        );
        let key = DNSKey::new("before.example".to_string(), 1, 1, false).unwrap();
        logger.log_query(&key, "192.0.2.1:5353").await;

        // Neither renaming nor reopening the log is possible anymore
        fs::set_permissions(&path, fs::Permissions::from_mode(0o444)).unwrap();
        fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o555)).unwrap();
        if File::create(dir.path().join("probe")).is_ok() {
            fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o755)).unwrap();
            eprintln!("skipped: permissions are not enforced for this user");
            return;
        }
        let key = DNSKey::new("during.example".to_string(), 1, 1, false).unwrap();
        logger.log_query(&key, "192.0.2.1:5353").await;
        fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o755)).unwrap();
        fs::set_permissions(&path, fs::Permissions::from_mode(0o644)).unwrap();

        assert!(logger.is_enabled().await);
        assert!(
            fs::read_to_string(&path)
                .unwrap()
                .contains("during.example")
        );
    }
}