sightingdb 0.4.1

A database designed for Sightings, a technique to count items
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
mod acl;
mod admin;
mod attribute;
mod config;
mod daemon;
mod db;
mod db_log;
mod dns;
mod error;
mod handlers;
mod ingest;
mod maintenance;
mod persistence;
mod sighting_reader;
mod sighting_writer;
mod tls;

use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::thread::JoinHandle;
use std::time::Duration;

use actix_web::{App, HttpServer, web};
use anyhow::{Context, Result, bail};
use chrono::Utc;
use clap::Parser;

use crate::acl::Acl;
use crate::config::Settings;
use crate::db::{DEFAULT_APIKEY, Database};
use crate::dns::answer::Responder;
use crate::handlers::SharedState;
use crate::maintenance::Shutdown;

#[derive(Debug, Parser)]
#[command(
    name = "sightingdb",
    version,
    about = "Sightings Database",
    author = "Sebastien Tricaud <sebastien.tricaud@devo.com>"
)]
struct Cli {
    /// Sets a custom config file
    #[arg(short, long, value_name = "FILE")]
    config: Option<PathBuf>,

    /// log4rs configuration file
    #[arg(
        short = 'l',
        long,
        value_name = "FILE",
        default_value = "etc/log4rs.yml"
    )]
    logging_config: PathBuf,

    /// Set the default API key, replacing the built-in one
    #[arg(short = 'k', long, value_name = "APIKEY")]
    apikey: Option<String>,

    /// Import STIX 2.1 bundles from a file or directory, then exit
    #[arg(long, value_name = "PATH")]
    import_stix: Option<PathBuf>,

    /// Write a self-signed certificate and key at the configured ssl_cert and
    /// ssl_key paths, then exit. For getting started; not for production.
    #[arg(long)]
    install_selfsigned_keys: bool,

    /// Sets the level of verbosity
    #[arg(short, long, action = clap::ArgAction::Count)]
    verbose: u8,
}

fn main() -> Result<()> {
    let cli = Cli::parse();

    log4rs::init_file(&cli.logging_config, Default::default()).with_context(|| {
        format!(
            "loading logging configuration {}",
            cli.logging_config.display()
        )
    })?;

    create_home_config();

    let config_path = match cli.config {
        Some(path) => path,
        None => config::locate()?,
    };
    log::info!("Using configuration file: {}", config_path.display());
    let settings = Settings::load(&config_path)?;

    // Detach before doing anything expensive, so the launcher does not restore
    // a database it is only going to throw away.
    if settings.daemonize && !daemon::is_child() {
        let pid = daemon::detach(&settings.log_out, &settings.log_err)?;
        log::info!("Running in the background as pid {pid}");
        return Ok(());
    }

    // Before anything touches the database or the network.
    if cli.install_selfsigned_keys {
        let Some(tls) = &settings.tls else {
            bail!(
                "ssl is off in {}, so there is no certificate to install. Set ssl = true first.",
                config_path.display()
            );
        };
        return tls::install_self_signed(tls);
    }

    // Persistence is only on when dbdir is set *and* usable; a database that
    // cannot save is still better than one that refuses to start.
    let snapshot = usable_snapshot_path(&settings);
    let db = open_database(&settings, snapshot.as_deref())?;

    let acl = build_acl(&settings, &db, cli.apikey.as_deref());

    // A one-shot import: load, write, save, exit. No listeners start.
    if let Some(path) = &cli.import_stix {
        let imported = import_stix(&db, &settings, path)?;
        log::info!("Imported {imported} sighting(s) from {}", path.display());
        if let Some(snapshot) = snapshot.as_deref() {
            persistence::save(&db, snapshot)
                .with_context(|| format!("saving to {}", snapshot.display()))?;
            log::info!("Saved the database to {}", snapshot.display());
        } else {
            log::warn!("No dbdir configured, so the import was not persisted");
        }
        return Ok(());
    }

    log::info!("Starting Sighting Daemon");
    if !settings.authenticate {
        log::warn!("No authentication used for the database; the ACL is not consulted.");
    }
    if settings.http_enabled && settings.tls.is_none() {
        log::warn!("TLS is disabled; serving plain HTTP.");
    }
    if snapshot.is_none() {
        log::warn!("Persistence is disabled; data will be lost when the process stops.");
    }

    if !settings.daemonize {
        log::info!(
            "Running in the foreground. Set 'daemonize = true' in {} to detach, or run under \
             a service manager (see etc/sightingdb.service).",
            config_path.display()
        );
    }

    let info = server_info(&settings, &config_path, &db, &acl);
    let state = Arc::new(SharedState {
        db,
        authenticate: settings.authenticate,
        acl: std::sync::RwLock::new(acl),
        info,
        acl_file: settings.acl_file.clone(),
    });

    let shutdown = Shutdown::new();
    let mut workers = spawn_workers(&state, &settings, snapshot.as_deref(), &shutdown)?;
    workers.extend(spawn_dns(&state, &settings, &shutdown)?);

    let result = actix_web::rt::System::new().block_on(async {
        if let Some(zmq) = settings.zmq.clone() {
            log::info!(
                "ZMQ ingest subscribing to {} ({} format)",
                zmq.endpoint,
                match zmq.format {
                    ingest::Format::Misp => "MISP",
                    ingest::Format::Native => "native",
                }
            );
            actix_web::rt::spawn(ingest::run(Arc::clone(&state), zmq, Arc::clone(&shutdown)));
        }

        if settings.http_enabled {
            serve(Arc::clone(&state), &settings).await
        } else {
            // DNS-only: the listeners are already running on their own threads,
            // so this future exists purely to hold the process open until asked
            // to stop.
            log::info!("HTTP API disabled; running listeners only");
            wait_for_signal(Arc::clone(&shutdown)).await
        }
    });

    // Stop the background workers before the final save, so nothing is writing
    // a snapshot underneath us.
    shutdown.stop();
    for worker in workers {
        let _ = worker.join();
    }

    if let Some(path) = snapshot.as_deref() {
        log::info!("Saving the database to {}", path.display());
        if let Err(e) = persistence::save(&state.db, path) {
            log::error!("Could not save the database: {e:#}");
        }
    }

    daemon::remove_pid_file();

    result
}

/// Snapshot what this server was configured to do, for the management
/// interface. Taken once at startup because configuration is read from a file
/// and does not change while running.
fn server_info(
    settings: &Settings,
    config_path: &Path,
    db: &Database,
    acl: &Acl,
) -> admin::ServerInfo {
    admin::ServerInfo {
        version: env!("CARGO_PKG_VERSION"),
        authenticate: settings.authenticate,
        http_enabled: settings.http_enabled,
        config_path: config_path.display().to_string(),
        dbdir: settings.dbdir.as_ref().map(|d| d.display().to_string()),
        snapshot_interval: settings.snapshot_interval,
        sweep_interval: settings.sweep_interval,
        stats_retention: settings.stats_retention,
        shadow_ttl: settings.shadow_ttl,
        dns: settings.dns.as_ref().map(|dns| admin::DnsInfo {
            listen: dns.listen.clone(),
            zone: dns.zone.clone(),
            ttl: dns.ttl,
            rate_limit: dns.rate_limit,
            shadow: dns.shadow,
            exposed: dns
                .exposed
                .iter()
                .map(|e| admin::ExposedInfo {
                    label: e.label.clone(),
                    namespace: e.namespace.clone(),
                    encoding: format!("{:?}", e.encoding).to_lowercase(),
                })
                .collect(),
        }),
        zmq: settings.zmq.as_ref().map(|zmq| admin::ZmqInfo {
            endpoint: zmq.endpoint.clone(),
            topics: zmq.topics.clone(),
            format: format!("{:?}", zmq.format).to_lowercase(),
            require_to_ids: zmq.mapping.require_to_ids,
            mapped_types: zmq.mapping.types.len(),
        }),
        namespaces: db.namespace_count(),
        apikeys: acl.len(),
    }
}

/// Read STIX bundles from a file, or from every `.json` in a directory.
///
/// One unreadable bundle does not abort the run: an import of a hundred files
/// should tell you which one was broken and keep the other ninety-nine.
fn import_stix(db: &Database, settings: &Settings, path: &Path) -> Result<u64> {
    let files = stix_files(path)?;
    if files.is_empty() {
        log::warn!("No .json files found at {}", path.display());
        return Ok(0);
    }
    if settings.stix.mapping.types.is_empty() && settings.stix.mapping.default_namespace.is_none() {
        log::warn!("No [stix.types] mapping is configured, so every observable will be discarded");
    }

    let mut total = 0;
    for file in files {
        let body = match fs::read_to_string(&file) {
            Ok(body) => body,
            Err(e) => {
                log::error!("Could not read {}: {e}", file.display());
                continue;
            }
        };

        let sightings = match ingest::stix::parse_bundle(&body, &settings.stix.mapping) {
            Ok(sightings) => sightings,
            Err(e) => {
                log::error!("Could not parse {}: {e}", file.display());
                continue;
            }
        };

        let mut written = 0;
        for sighting in &sightings {
            match ingest::record(db, settings.stix.ttl, sighting) {
                Ok(()) => written += sighting.count.max(1),
                Err(e) => log::warn!(
                    "Skipped {}/{} from {}: {e}",
                    sighting.namespace,
                    sighting.value,
                    file.display()
                ),
            }
        }
        log::info!(
            "{}: {} observable(s), {written} sighting(s)",
            file.display(),
            sightings.len()
        );
        total += written;
    }

    Ok(total)
}

fn stix_files(path: &Path) -> Result<Vec<PathBuf>> {
    let metadata = fs::metadata(path).with_context(|| format!("reading {}", path.display()))?;
    if metadata.is_file() {
        return Ok(vec![path.to_path_buf()]);
    }

    let mut files: Vec<PathBuf> = fs::read_dir(path)
        .with_context(|| format!("listing {}", path.display()))?
        .filter_map(|entry| entry.ok())
        .map(|entry| entry.path())
        .filter(|p| {
            p.extension()
                .is_some_and(|e| e.eq_ignore_ascii_case("json"))
        })
        .collect();
    // Deterministic order, so repeated imports log identically.
    files.sort();
    Ok(files)
}

/// Decide which API keys exist and what each may reach.
///
/// The `[acl]` section is authoritative when present. Without one we fall back
/// to the keys an older build stored in the database, granting each of them the
/// unrestricted access it used to have — upgrading must not lock a running
/// deployment out of its own data. `-k` always wins, and replaces the built-in
/// default key exactly as it always did.
fn build_acl(settings: &Settings, db: &Database, cli_apikey: Option<&str>) -> Acl {
    let legacy = db.legacy_apikeys();

    let mut acl = match &settings.acl {
        Some(acl) => {
            if !legacy.is_empty() {
                log::warn!(
                    "Ignoring {} API key(s) stored in the snapshot: the configuration has an \
                     [acl] section, which takes precedence",
                    legacy.len()
                );
            }
            log::info!("Loaded {} API key(s) from the [acl] section", acl.len());
            acl.clone()
        }
        None => {
            let mut acl = Acl::new();
            for key in &legacy {
                acl.grant_full(key);
            }
            if !legacy.is_empty() {
                log::warn!(
                    "Granting full access to {} API key(s) restored from the snapshot. Declare \
                     an [acl] section to scope them to particular namespaces.",
                    legacy.len()
                );
            }
            acl
        }
    };

    if let Some(apikey) = cli_apikey {
        acl.remove(DEFAULT_APIKEY);
        acl.grant_full(apikey);
        log::info!("API key from -k granted full access");
    }

    if acl.is_empty() {
        log::warn!(
            "No API keys configured; seeding '{DEFAULT_APIKEY}' with full access. Add an [acl] \
             section or pass -k."
        );
        acl.grant_full(DEFAULT_APIKEY);
    }

    acl
}

/// Load the database from disk when there is a snapshot, otherwise start fresh.
///
/// A snapshot that exists but cannot be read is fatal: starting empty would
/// look like catastrophic data loss, and the next save would make it real.
fn open_database(settings: &Settings, snapshot: Option<&Path>) -> Result<Database> {
    let policy = settings.database_policy();

    let Some(path) = snapshot else {
        return Ok(Database::with_policy(policy));
    };

    match persistence::load(path)? {
        Some(data) => {
            let db = Database::from_snapshot(data, policy);
            log::info!(
                "Restored {} namespaces from {}",
                db.namespace_count(),
                path.display()
            );
            Ok(db)
        }
        None => {
            log::info!("No snapshot at {}, starting empty", path.display());
            Ok(Database::with_policy(policy))
        }
    }
}

/// Where snapshots go, or `None` if persistence is off or the directory is not
/// usable.
fn usable_snapshot_path(settings: &Settings) -> Option<PathBuf> {
    let dbdir = settings.dbdir.as_ref()?;

    if let Err(e) = fs::create_dir_all(dbdir) {
        log::error!(
            "Cannot use dbdir {}: {e}. Continuing without persistence.",
            dbdir.display()
        );
        return None;
    }

    Some(persistence::snapshot_path(dbdir))
}

fn spawn_workers(
    state: &Arc<SharedState>,
    settings: &Settings,
    snapshot: Option<&Path>,
    shutdown: &Arc<Shutdown>,
) -> Result<Vec<JoinHandle<()>>> {
    let mut workers = Vec::new();

    if settings.sweep_interval > 0 {
        let state = Arc::clone(state);
        workers.push(
            maintenance::spawn_interval(
                "sightingdb-sweeper",
                Duration::from_secs(settings.sweep_interval),
                Arc::clone(shutdown),
                move || {
                    let report = state.db.sweep(Utc::now());
                    if !report.is_empty() {
                        log::info!(
                            "Swept {} expired values and {} empty namespaces",
                            report.values_removed,
                            report.namespaces_removed
                        );
                    }
                },
            )
            .context("starting the sweeper thread")?,
        );
    }

    if let Some(path) = snapshot
        && settings.snapshot_interval > 0
    {
        let state = Arc::clone(state);
        let path = path.to_path_buf();
        workers.push(
            maintenance::spawn_interval(
                "sightingdb-snapshot",
                Duration::from_secs(settings.snapshot_interval),
                Arc::clone(shutdown),
                move || {
                    if let Err(e) = persistence::save(&state.db, &path) {
                        log::error!("Snapshot failed: {e:#}");
                    }
                },
            )
            .context("starting the snapshot thread")?,
        );
    }

    Ok(workers)
}

/// Start the DNS listeners, if a `[dns]` section asked for them.
///
/// DNS has no authentication, so this deliberately answers only for the
/// namespaces named in `[dns.namespaces]` — the API-key ACL does not apply here.
fn spawn_dns(
    state: &Arc<SharedState>,
    settings: &Settings,
    shutdown: &Arc<Shutdown>,
) -> Result<Vec<JoinHandle<()>>> {
    let Some(dns) = &settings.dns else {
        return Ok(Vec::new());
    };

    let zone = hickory_proto::rr::Name::parse(&dns.zone, Some(&hickory_proto::rr::Name::root()))
        .with_context(|| format!("parsing the DNS zone '{}'", dns.zone))?;

    let responder = Arc::new(Responder::new(
        Arc::clone(state),
        zone,
        dns.exposed.clone(),
        dns.ttl,
        dns.shadow,
    ));

    let handles = dns::server::spawn(
        responder,
        &dns.listen,
        dns.threads,
        dns.rate_limit,
        Arc::clone(shutdown),
    )?;

    log::info!(
        "DNS listening on {} for zone {} ({} namespace(s) exposed, {} threads)",
        dns.listen,
        dns.zone,
        dns.exposed.len(),
        dns.threads
    );
    for exposed in &dns.exposed {
        log::info!(
            "  {}.{} -> namespace '{}' ({:?})",
            exposed.label,
            dns.zone,
            exposed.namespace,
            exposed.encoding
        );
    }
    if dns.shadow {
        log::warn!("DNS lookups will raise shadow sightings, an unauthenticated write path");
    }

    Ok(handles)
}

async fn serve(state: Arc<SharedState>, settings: &Settings) -> Result<()> {
    let state = web::Data::from(state);
    let post_limit = settings.post_limit;

    let server = HttpServer::new(move || {
        App::new()
            .app_data(state.clone())
            .app_data(handlers::json_config(post_limit))
            .app_data(handlers::query_config())
            .configure(admin::routes)
            .configure(handlers::routes)
    });

    let server = match &settings.tls {
        Some(tls) => {
            let builder = tls::acceptor(tls)?;
            server
                .bind_openssl(&settings.listen, builder)
                .with_context(|| format!("binding https://{}", settings.listen))?
        }
        None => server
            .bind(&settings.listen)
            .with_context(|| format!("binding http://{}", settings.listen))?,
    };

    let scheme = if settings.tls.is_some() {
        "https"
    } else {
        "http"
    };
    log::info!("Listening on {scheme}://{}", settings.listen);

    server.run().await.context("running the HTTP server")
}

/// Block until the process is asked to stop, for the DNS-only case where no
/// HTTP server is holding the runtime open.
///
/// Both signals feed the same `Shutdown` that the background workers watch,
/// and we poll it rather than awaiting the signals directly: `System::stop()`
/// does not interrupt `block_on`, so a task that only stopped the system would
/// leave this future waiting forever.
async fn wait_for_signal(shutdown: Arc<Shutdown>) -> Result<()> {
    #[cfg(unix)]
    {
        use actix_web::rt::signal::unix::{SignalKind, signal};

        let mut terminate = signal(SignalKind::terminate()).context("listening for SIGTERM")?;
        let shutdown = Arc::clone(&shutdown);
        actix_web::rt::spawn(async move {
            terminate.recv().await;
            log::info!("SIGTERM received, shutting down");
            shutdown.stop();
        });
    }

    {
        let shutdown = Arc::clone(&shutdown);
        actix_web::rt::spawn(async move {
            if actix_web::rt::signal::ctrl_c().await.is_ok() {
                log::info!("Interrupt received, shutting down");
                shutdown.stop();
            }
        });
    }

    while !shutdown.is_stopped() {
        actix_web::rt::time::sleep(Duration::from_millis(200)).await;
    }
    Ok(())
}

/// Make sure `~/.sightingdb` exists so a user-local config has somewhere to live.
fn create_home_config() {
    let Some(mut home_config) = dirs::home_dir() else {
        log::warn!("Cannot determine the home directory; skipping ~/.sightingdb");
        return;
    };
    home_config.push(".sightingdb");

    if let Err(e) = fs::create_dir_all(&home_config) {
        log::error!(
            "Error creating home configuration {}: {e}",
            home_config.display()
        );
    }
}