Skip to main content

kanidmd_core/
lib.rs

1//! These contain the server "cores". These are able to startup the server
2//! (bootstrap) to a running state and then execute tasks. This is where modules
3//! are logically ordered based on their depenedncies for execution. Some of these
4//! are task-only i.e. reindexing, and some of these launch the server into a
5//! fully operational state (https, ldap, etc).
6//!
7//! Generally, this is the "entry point" where the server begins to run, and
8//! the entry point for all client traffic which is then directed to the
9//! various `actors`.
10
11#![deny(warnings)]
12#![warn(unused_extern_crates)]
13#![warn(unused_imports)]
14#![deny(clippy::todo)]
15#![deny(clippy::unimplemented)]
16#![deny(clippy::unwrap_used)]
17#![deny(clippy::expect_used)]
18#![deny(clippy::panic)]
19#![deny(clippy::unreachable)]
20#![deny(clippy::await_holding_lock)]
21#![deny(clippy::needless_pass_by_value)]
22#![deny(clippy::trivially_copy_pass_by_ref)]
23#![deny(clippy::indexing_slicing)]
24
25#[macro_use]
26extern crate tracing;
27#[macro_use]
28extern crate kanidmd_lib;
29
30mod actors;
31pub mod admin;
32pub mod config;
33mod crypto;
34mod https;
35mod interval;
36mod ldaps;
37mod repl;
38mod tcp;
39mod utils;
40
41use crate::{
42    actors::{QueryServerReadV1, QueryServerWriteV1},
43    admin::AdminActor,
44    config::{Configuration, ServerRole},
45    interval::IntervalActor,
46    repl::ReplicationServerHandles,
47    utils::touch_file_or_quit,
48};
49use crypto_glue::{
50    s256::{Sha256, Sha256Output},
51    traits::Digest,
52};
53use kanidm_proto::{
54    backup::BackupCompression, internal::OperationError, scim_v1::client::ScimAssertGeneric,
55};
56use kanidmd_lib::{
57    be::{Backend, BackendConfig, BackendTransaction},
58    idm::ldap::LdapServer,
59    prelude::*,
60    schema::Schema,
61    status::StatusActor,
62    value::CredentialType,
63};
64use regex::Regex;
65use sketching::LoggerType;
66use std::{
67    collections::BTreeSet,
68    fmt::{Display, Formatter},
69    path::{Path, PathBuf},
70    sync::{Arc, LazyLock},
71};
72use tokio::{sync::broadcast, task};
73use tokio_rustls::TlsAcceptor;
74
75#[cfg(not(target_family = "windows"))]
76use libc::umask;
77
78pub const KANIDM_PKG_VERSION: &str = env!("KANIDM_PKG_VERSION");
79
80// === internal setup helpers
81
82fn setup_backend(config: &Configuration, schema: &Schema) -> Result<Backend, OperationError> {
83    setup_backend_vacuum(config, schema, false)
84}
85
86fn setup_backend_vacuum(
87    config: &Configuration,
88    schema: &Schema,
89    vacuum: bool,
90) -> Result<Backend, OperationError> {
91    // Limit the scope of the schema txn.
92    // let schema_txn = task::block_on(schema.write());
93    let schema_txn = schema.write();
94    let idxmeta = schema_txn.reload_idxmeta();
95
96    let pool_size: u32 = config.threads as u32;
97
98    let cfg = BackendConfig::new(
99        config.db_path.as_deref(),
100        pool_size,
101        config.db_fs_type.unwrap_or_default(),
102        config.db_arc_size,
103    );
104
105    Backend::new(cfg, idxmeta, vacuum)
106}
107
108// TODO #54: We could move most of the be/schema/qs setup and startup
109// outside of this call, then pass in "what we need" in a cloneable
110// form, this way we could have separate Idm vs Qs threads, and dedicated
111// threads for write vs read
112async fn setup_qs_idms(
113    be: Backend,
114    schema: Schema,
115    config: &Configuration,
116) -> Result<(QueryServer, IdmServer, IdmServerDelayed, IdmServerAudit), OperationError> {
117    let curtime = duration_from_epoch_now();
118    // Create a query_server implementation
119    let query_server = QueryServer::new(be, schema, config.domain.clone(), curtime)?;
120
121    // TODO #62: Should the IDM parts be broken out to the IdmServer?
122    // What's important about this initial setup here is that it also triggers
123    // the schema and acp reload, so they are now configured correctly!
124    // Initialise the schema core.
125    //
126    // Now search for the schema itself, and validate that the system
127    // in memory matches the BE on disk, and that it's syntactically correct.
128    // Write it out if changes are needed.
129    query_server
130        .initialise_helper(curtime, DOMAIN_TGT_LEVEL)
131        .await?;
132
133    // We generate a SINGLE idms only!
134    let is_integration_test = config.integration_test_config.is_some();
135    let (idms, idms_delayed, idms_audit) = IdmServer::new(
136        query_server.clone(),
137        &config.origin,
138        is_integration_test,
139        curtime,
140    )
141    .await?;
142
143    Ok((query_server, idms, idms_delayed, idms_audit))
144}
145
146async fn setup_qs(
147    be: Backend,
148    schema: Schema,
149    config: &Configuration,
150) -> Result<QueryServer, OperationError> {
151    let curtime = duration_from_epoch_now();
152    // Create a query_server implementation
153    let query_server = QueryServer::new(be, schema, config.domain.clone(), curtime)?;
154
155    // TODO #62: Should the IDM parts be broken out to the IdmServer?
156    // What's important about this initial setup here is that it also triggers
157    // the schema and acp reload, so they are now configured correctly!
158    // Initialise the schema core.
159    //
160    // Now search for the schema itself, and validate that the system
161    // in memory matches the BE on disk, and that it's syntactically correct.
162    // Write it out if changes are needed.
163    query_server
164        .initialise_helper(curtime, DOMAIN_TGT_LEVEL)
165        .await?;
166
167    Ok(query_server)
168}
169
170macro_rules! dbscan_setup_be {
171    (
172        $config:expr
173    ) => {{
174        let schema = match Schema::new() {
175            Ok(s) => s,
176            Err(e) => {
177                error!("Failed to setup in memory schema: {:?}", e);
178                std::process::exit(1);
179            }
180        };
181
182        match setup_backend($config, &schema) {
183            Ok(be) => be,
184            Err(e) => {
185                error!("Failed to setup BE: {:?}", e);
186                return;
187            }
188        }
189    }};
190}
191
192pub fn dbscan_list_indexes_core(config: &Configuration) {
193    let be = dbscan_setup_be!(config);
194    let mut be_rotxn = match be.read() {
195        Ok(txn) => txn,
196        Err(err) => {
197            error!(?err, "Unable to proceed, backend read transaction failure.");
198            return;
199        }
200    };
201
202    match be_rotxn.list_indexes() {
203        Ok(mut idx_list) => {
204            idx_list.sort_unstable();
205            idx_list.iter().for_each(|idx_name| {
206                println!("{idx_name}");
207            })
208        }
209        Err(e) => {
210            error!("Failed to retrieve index list: {:?}", e);
211        }
212    };
213}
214
215pub fn dbscan_list_id2entry_core(config: &Configuration) {
216    let be = dbscan_setup_be!(config);
217    let mut be_rotxn = match be.read() {
218        Ok(txn) => txn,
219        Err(err) => {
220            error!(?err, "Unable to proceed, backend read transaction failure.");
221            return;
222        }
223    };
224
225    match be_rotxn.list_id2entry() {
226        Ok(mut id_list) => {
227            id_list.sort_unstable_by_key(|k| k.0);
228            id_list.iter().for_each(|(id, value)| {
229                println!("{id:>8}: {value}");
230            })
231        }
232        Err(e) => {
233            error!("Failed to retrieve id2entry list: {:?}", e);
234        }
235    };
236}
237
238pub fn dbscan_list_index_analysis_core(config: &Configuration) {
239    let _be = dbscan_setup_be!(config);
240    // TBD in after slopes merge.
241}
242
243pub fn dbscan_list_index_core(config: &Configuration, index_name: &str) {
244    let be = dbscan_setup_be!(config);
245    let mut be_rotxn = match be.read() {
246        Ok(txn) => txn,
247        Err(err) => {
248            error!(?err, "Unable to proceed, backend read transaction failure.");
249            return;
250        }
251    };
252
253    match be_rotxn.list_index_content(index_name) {
254        Ok(mut idx_list) => {
255            idx_list.sort_unstable_by(|a, b| a.0.cmp(&b.0));
256            idx_list.iter().for_each(|(key, value)| {
257                println!("{key:>50}: {value:?}");
258            })
259        }
260        Err(e) => {
261            error!("Failed to retrieve index list: {:?}", e);
262        }
263    };
264}
265
266pub fn dbscan_get_id2entry_core(config: &Configuration, id: u64) {
267    let be = dbscan_setup_be!(config);
268    let mut be_rotxn = match be.read() {
269        Ok(txn) => txn,
270        Err(err) => {
271            error!(?err, "Unable to proceed, backend read transaction failure.");
272            return;
273        }
274    };
275
276    match be_rotxn.get_id2entry(id) {
277        Ok((id, value)) => println!("{id:>8}: {value}"),
278        Err(e) => {
279            error!("Failed to retrieve id2entry value: {:?}", e);
280        }
281    };
282}
283
284pub fn dbscan_quarantine_id2entry_core(config: &Configuration, id: u64) {
285    let be = dbscan_setup_be!(config);
286    let mut be_wrtxn = match be.write() {
287        Ok(txn) => txn,
288        Err(err) => {
289            error!(
290                ?err,
291                "Unable to proceed, backend write transaction failure."
292            );
293            return;
294        }
295    };
296
297    match be_wrtxn
298        .quarantine_entry(id)
299        .and_then(|_| be_wrtxn.commit())
300    {
301        Ok(()) => {
302            println!("quarantined - {id:>8}")
303        }
304        Err(e) => {
305            error!("Failed to quarantine id2entry value: {:?}", e);
306        }
307    };
308}
309
310pub fn dbscan_list_quarantined_core(config: &Configuration) {
311    let be = dbscan_setup_be!(config);
312    let mut be_rotxn = match be.read() {
313        Ok(txn) => txn,
314        Err(err) => {
315            error!(?err, "Unable to proceed, backend read transaction failure.");
316            return;
317        }
318    };
319
320    match be_rotxn.list_quarantined() {
321        Ok(mut id_list) => {
322            id_list.sort_unstable_by_key(|k| k.0);
323            id_list.iter().for_each(|(id, value)| {
324                println!("{id:>8}: {value}");
325            })
326        }
327        Err(e) => {
328            error!("Failed to retrieve id2entry list: {:?}", e);
329        }
330    };
331}
332
333pub fn dbscan_restore_quarantined_core(config: &Configuration, id: u64) {
334    let be = dbscan_setup_be!(config);
335    let mut be_wrtxn = match be.write() {
336        Ok(txn) => txn,
337        Err(err) => {
338            error!(
339                ?err,
340                "Unable to proceed, backend write transaction failure."
341            );
342            return;
343        }
344    };
345
346    match be_wrtxn
347        .restore_quarantined(id)
348        .and_then(|_| be_wrtxn.commit())
349    {
350        Ok(()) => {
351            println!("restored - {id:>8}")
352        }
353        Err(e) => {
354            error!("Failed to restore quarantined id2entry value: {:?}", e);
355        }
356    };
357}
358
359pub fn backup_server_core(config: &Configuration, dst_path: Option<&Path>) {
360    let schema = match Schema::new() {
361        Ok(s) => s,
362        Err(e) => {
363            error!("Failed to setup in memory schema: {:?}", e);
364            std::process::exit(1);
365        }
366    };
367
368    let be = match setup_backend(config, &schema) {
369        Ok(be) => be,
370        Err(e) => {
371            error!("Failed to setup BE: {:?}", e);
372            return;
373        }
374    };
375
376    let mut be_ro_txn = match be.read() {
377        Ok(txn) => txn,
378        Err(err) => {
379            error!(?err, "Unable to proceed, backend read transaction failure.");
380            return;
381        }
382    };
383
384    let compression = match config.online_backup.as_ref() {
385        Some(backup_config) => backup_config.compression,
386        None => BackupCompression::default(),
387    };
388
389    if let Some(dst_path) = dst_path {
390        if dst_path.exists() {
391            error!(
392                "backup file {} already exists, will not overwrite it.",
393                dst_path.display()
394            );
395            return;
396        }
397
398        let output = match std::fs::File::create(dst_path) {
399            Ok(output) => output,
400            Err(err) => {
401                error!(?err, "File::create error creating {}", dst_path.display());
402                return;
403            }
404        };
405
406        match be_ro_txn.backup(output, compression) {
407            Ok(_) => info!("Backup success!"),
408            Err(e) => {
409                error!("Backup failed: {:?}", e);
410                std::process::exit(1);
411            }
412        };
413    } else {
414        // No path set, default to stdout
415        let stdout = std::io::stdout().lock();
416
417        match be_ro_txn.backup(stdout, compression) {
418            Ok(_) => info!("Backup success!"),
419            Err(e) => {
420                error!("Backup failed: {:?}", e);
421                std::process::exit(1);
422            }
423        };
424    };
425    // Let the txn abort, even on success.
426}
427
428pub async fn restore_server_core(config: &Configuration, dst_path: &Path) {
429    // If it's an in memory database, we don't need to touch anything
430    if let Some(db_path) = config.db_path.as_ref() {
431        touch_file_or_quit(db_path);
432    }
433
434    // First, we provide the in-memory schema so that core attrs are indexed correctly.
435    let schema = match Schema::new() {
436        Ok(s) => s,
437        Err(e) => {
438            error!("Failed to setup in memory schema: {:?}", e);
439            std::process::exit(1);
440        }
441    };
442
443    let be = match setup_backend(config, &schema) {
444        Ok(be) => be,
445        Err(e) => {
446            error!("Failed to setup backend: {:?}", e);
447            return;
448        }
449    };
450
451    let mut be_wr_txn = match be.write() {
452        Ok(txn) => txn,
453        Err(err) => {
454            error!(
455                ?err,
456                "Unable to proceed, backend write transaction failure."
457            );
458            return;
459        }
460    };
461
462    let compression = BackupCompression::identify_file(dst_path);
463
464    let input = match std::fs::File::open(dst_path) {
465        Ok(output) => output,
466        Err(err) => {
467            error!(?err, "File::open error reading {}", dst_path.display());
468            return;
469        }
470    };
471
472    let r = be_wr_txn
473        .restore(input, compression)
474        .and_then(|_| be_wr_txn.commit());
475
476    if r.is_err() {
477        error!("Failed to restore database: {:?}", r);
478        std::process::exit(1);
479    }
480    info!("Database loaded successfully");
481
482    reindex_inner(be, schema, config).await;
483
484    info!("✅ Restore Success!");
485}
486
487pub async fn reindex_server_core(config: &Configuration) {
488    // First, we provide the in-memory schema so that core attrs are indexed correctly.
489    let schema = match Schema::new() {
490        Ok(s) => s,
491        Err(e) => {
492            error!("Failed to setup in memory schema: {:?}", e);
493            std::process::exit(1);
494        }
495    };
496
497    let be = match setup_backend(config, &schema) {
498        Ok(be) => be,
499        Err(e) => {
500            error!("Failed to setup BE: {:?}", e);
501            return;
502        }
503    };
504
505    reindex_inner(be, schema, config).await;
506
507    info!("✅ Reindex Success!");
508}
509
510async fn reindex_inner(be: Backend, schema: Schema, config: &Configuration) {
511    info!("Start Index Phase 1 ...");
512    // Reindex only the core schema attributes to bootstrap the process.
513    let mut be_wr_txn = match be.write() {
514        Ok(txn) => txn,
515        Err(err) => {
516            error!(
517                ?err,
518                "Unable to proceed, backend write transaction failure."
519            );
520            return;
521        }
522    };
523
524    let r = be_wr_txn.reindex(true).and_then(|_| be_wr_txn.commit());
525
526    // Now that's done, setup a minimal qs and reindex from that.
527    if r.is_err() {
528        error!("Failed to reindex database: {:?}", r);
529        std::process::exit(1);
530    }
531    info!("Index Phase 1 Success!");
532
533    debug!("Attempting to init query server ...");
534
535    let (qs, _idms, _idms_delayed, _idms_audit) = match setup_qs_idms(be, schema, config).await {
536        Ok(t) => t,
537        Err(e) => {
538            error!("Unable to setup query server or idm server -> {:?}", e);
539            return;
540        }
541    };
542    debug!("Init Query Server Success!");
543
544    info!("Start Index Phase 2 ...");
545
546    let Ok(mut qs_write) = qs.write(duration_from_epoch_now()).await else {
547        error!("Unable to acquire write transaction");
548        return;
549    };
550    let r = qs_write.reindex(true).and_then(|_| qs_write.commit());
551
552    match r {
553        Ok(_) => info!("Index Phase 2 Success!"),
554        Err(e) => {
555            error!("Reindex failed: {:?}", e);
556            std::process::exit(1);
557        }
558    };
559}
560
561pub fn vacuum_server_core(config: &Configuration) {
562    let schema = match Schema::new() {
563        Ok(s) => s,
564        Err(e) => {
565            eprintln!("Failed to setup in memory schema: {e:?}");
566            std::process::exit(1);
567        }
568    };
569
570    // The schema doesn't matter here. Vacuum is run as part of db open to avoid
571    // locking.
572    let r = setup_backend_vacuum(config, &schema, true);
573
574    match r {
575        Ok(_) => eprintln!("Vacuum Success!"),
576        Err(e) => {
577            eprintln!("Vacuum failed: {e:?}");
578            std::process::exit(1);
579        }
580    };
581}
582
583pub async fn domain_rename_core(config: &Configuration) {
584    let schema = match Schema::new() {
585        Ok(s) => s,
586        Err(e) => {
587            eprintln!("Failed to setup in memory schema: {e:?}");
588            std::process::exit(1);
589        }
590    };
591
592    // Start the backend.
593    let be = match setup_backend(config, &schema) {
594        Ok(be) => be,
595        Err(e) => {
596            error!("Failed to setup BE: {:?}", e);
597            return;
598        }
599    };
600
601    // Setup the qs, and perform any migrations and changes we may have.
602    let qs = match setup_qs(be, schema, config).await {
603        Ok(t) => t,
604        Err(e) => {
605            error!("Unable to setup query server -> {:?}", e);
606            return;
607        }
608    };
609
610    let new_domain_name = config.domain.as_str();
611
612    // make sure we're actually changing the domain name...
613    match qs.read().await.map(|qs| qs.get_domain_name().to_string()) {
614        Ok(old_domain_name) => {
615            admin_info!(?old_domain_name, ?new_domain_name);
616            if old_domain_name == new_domain_name {
617                admin_info!("Domain name not changing, stopping.");
618                return;
619            }
620            admin_debug!(
621                "Domain name is changing from {:?} to {:?}",
622                old_domain_name,
623                new_domain_name
624            );
625        }
626        Err(e) => {
627            admin_error!("Failed to query domain name, quitting! -> {:?}", e);
628            return;
629        }
630    }
631
632    let Ok(mut qs_write) = qs.write(duration_from_epoch_now()).await else {
633        error!("Unable to acquire write transaction");
634        return;
635    };
636    let r = qs_write
637        .danger_domain_rename(new_domain_name)
638        .and_then(|_| qs_write.commit());
639
640    match r {
641        Ok(_) => info!("Domain Rename Success!"),
642        Err(e) => {
643            error!("Domain Rename Failed - Rollback has occurred: {:?}", e);
644            std::process::exit(1);
645        }
646    };
647}
648
649pub async fn verify_server_core(config: &Configuration) {
650    let curtime = duration_from_epoch_now();
651    // setup the qs - without initialise!
652    let schema_mem = match Schema::new() {
653        Ok(sc) => sc,
654        Err(e) => {
655            error!("Failed to setup in memory schema: {:?}", e);
656            return;
657        }
658    };
659    // Setup the be
660    let be = match setup_backend(config, &schema_mem) {
661        Ok(be) => be,
662        Err(e) => {
663            error!("Failed to setup BE: {:?}", e);
664            return;
665        }
666    };
667
668    let server = match QueryServer::new(be, schema_mem, config.domain.clone(), curtime) {
669        Ok(qs) => qs,
670        Err(err) => {
671            error!(?err, "Failed to setup query server");
672            return;
673        }
674    };
675
676    // Run verifications.
677    let r = server.verify().await;
678
679    if r.is_empty() {
680        eprintln!("Verification passed!");
681        std::process::exit(0);
682    } else {
683        for er in r {
684            error!("{:?}", er);
685        }
686        std::process::exit(1);
687    }
688
689    // Now add IDM server verifications?
690}
691
692pub fn cert_generate_core(config: &Configuration) {
693    // Get the cert root
694
695    let (tls_key_path, tls_chain_path) = match &config.tls_config {
696        Some(tls_config) => (tls_config.key.as_path(), tls_config.chain.as_path()),
697        None => {
698            error!("Unable to find TLS configuration");
699            std::process::exit(1);
700        }
701    };
702
703    if tls_key_path.exists() && tls_chain_path.exists() {
704        info!(
705            "TLS key and chain already exist - remove them first if you intend to regenerate these"
706        );
707        return;
708    }
709
710    let origin_domain = match config.origin.domain() {
711        Some(val) => val,
712        None => {
713            error!("origin does not contain a valid domain");
714            std::process::exit(1);
715        }
716    };
717
718    let cert_root = match tls_key_path.parent() {
719        Some(parent) => parent,
720        None => {
721            error!("Unable to find parent directory of {:?}", tls_key_path);
722            std::process::exit(1);
723        }
724    };
725
726    let ca_cert = cert_root.join("ca.pem");
727    let ca_key = cert_root.join("cakey.pem");
728    let tls_cert_path = cert_root.join("cert.pem");
729
730    let ca_handle = if !ca_cert.exists() || !ca_key.exists() {
731        // Generate the CA again.
732        let ca_handle = match crypto::build_ca() {
733            Ok(ca_handle) => ca_handle,
734            Err(e) => {
735                error!(err = ?e, "Failed to build CA");
736                std::process::exit(1);
737            }
738        };
739
740        if crypto::write_ca(ca_key, ca_cert, &ca_handle).is_err() {
741            error!("Failed to write CA");
742            std::process::exit(1);
743        }
744
745        ca_handle
746    } else {
747        match crypto::load_ca(ca_key, ca_cert) {
748            Ok(ca_handle) => ca_handle,
749            Err(_) => {
750                error!("Failed to load CA");
751                std::process::exit(1);
752            }
753        }
754    };
755
756    if !tls_key_path.exists() || !tls_chain_path.exists() || !tls_cert_path.exists() {
757        // Generate the cert from the ca.
758        let cert_handle = match crypto::build_cert(origin_domain, &ca_handle) {
759            Ok(cert_handle) => cert_handle,
760            Err(e) => {
761                error!(err = ?e, "Failed to build certificate");
762                std::process::exit(1);
763            }
764        };
765
766        if crypto::write_cert(tls_key_path, tls_chain_path, tls_cert_path, &cert_handle).is_err() {
767            error!("Failed to write certificates");
768            std::process::exit(1);
769        }
770    }
771    info!("certificate generation complete");
772}
773
774static MIGRATION_PATH_RE: LazyLock<Regex> = LazyLock::new(|| {
775    #[allow(clippy::expect_used)]
776    Regex::new("^\\d\\d-.*\\.h?json$").expect("Invalid SPN regex found")
777});
778
779struct ScimMigration {
780    path: PathBuf,
781    hash: Sha256Output,
782    assertions: ScimAssertGeneric,
783}
784
785async fn migration_reload_supervisor(
786    mut broadcast_rx: broadcast::Receiver<CoreAction>,
787    server_write_ref: &'static QueryServerWriteV1,
788    migration_path: PathBuf,
789) {
790    loop {
791        tokio::select! {
792            Ok(action) = broadcast_rx.recv() => {
793                match action {
794                    CoreAction::Shutdown => break,
795                    CoreAction::Reload => {
796                        // Read the migrations.
797                        // Apply them.
798                        let eventid = Uuid::new_v4();
799                        migration_apply(
800                            eventid,
801                            server_write_ref,
802                            migration_path.as_path(),
803                        ).await;
804
805                        info!("Migration reload complete");
806                    },
807                }
808            }
809        }
810    }
811    info!("Stopped {}", TaskName::MigrationReload);
812}
813
814#[instrument(
815    level = "info",
816    fields(uuid = ?eventid),
817    skip_all,
818)]
819async fn migration_apply(
820    eventid: Uuid,
821    server_write_ref: &'static QueryServerWriteV1,
822    migration_path: &Path,
823) {
824    if !migration_path.exists() {
825        info!(migration_path = %migration_path.display(), "Migration path does not exist - migrations will be skipped.");
826        return;
827    }
828
829    let mut dir_ents = match tokio::fs::read_dir(migration_path).await {
830        Ok(dir_ents) => dir_ents,
831        Err(err) => {
832            error!(?err, "Unable to read migration directory.");
833            let diag = kanidm_lib_file_permissions::diagnose_path(migration_path);
834            info!(%diag);
835            return;
836        }
837    };
838
839    let mut migration_paths = Vec::with_capacity(8);
840
841    loop {
842        match dir_ents.next_entry().await {
843            Ok(Some(dir_ent)) => migration_paths.push(dir_ent.path()),
844            Ok(None) => {
845                // Complete,
846                break;
847            }
848            Err(err) => {
849                error!(?err, "Unable to read directory entries.");
850                return;
851            }
852        }
853    }
854
855    // Filter these.
856
857    let mut migration_paths: Vec<_> = migration_paths.into_iter()
858        .filter(|path| {
859            if !path.is_file() {
860                info!(path = %path.display(), "ignoring path that is not a file.");
861                return false;
862            }
863
864            let Some(file_name) = path.file_name().and_then(std::ffi::OsStr::to_str) else {
865                info!(path = %path.display(), "ignoring path that has no file name, or is not a valid utf-8 file name.");
866                return false;
867            };
868
869            if !MIGRATION_PATH_RE.is_match(file_name) {
870                info!(path = %path.display(), "ignoring file that does not match naming pattern.");
871                info!("expected pattern 'XX-NAME.json' where XX are two numbers, followed by a hypen, with the file extension .json");
872                return false;
873            }
874
875            true
876        })
877        .collect();
878
879    migration_paths.sort_unstable();
880    let mut migrations = Vec::with_capacity(migration_paths.len());
881
882    for migration_path in migration_paths {
883        info!(path = %migration_path.display(), "examining migration");
884
885        let migration_content = match tokio::fs::read(&migration_path).await {
886            Ok(bytes) => bytes,
887            Err(err) => {
888                error!(?err, "Unable to read migration - it will be ignored.");
889                let diag = kanidm_lib_file_permissions::diagnose_path(&migration_path);
890                info!(%diag);
891                continue;
892            }
893        };
894
895        // Is it valid json?
896        let assertions: ScimAssertGeneric = match serde_hjson::from_slice(&migration_content) {
897            Ok(assertions) => assertions,
898            Err(err) => {
899                error!(?err, path = %migration_path.display(), "Invalid JSON SCIM Assertion");
900                continue;
901            }
902        };
903
904        // Hash the content.
905        let mut hasher = Sha256::new();
906        hasher.update(&migration_content);
907        let migration_hash: Sha256Output = hasher.finalize();
908
909        migrations.push(ScimMigration {
910            path: migration_path,
911            hash: migration_hash,
912            assertions,
913        });
914    }
915
916    let mut migration_ids = BTreeSet::new();
917    for migration in &migrations {
918        // BTreeSet returns false on duplicate value insertion.
919        if !migration_ids.insert(migration.assertions.id) {
920            error!(path = %migration.path.display(), uuid = ?migration.assertions.id, "Duplicate migration UUID found, refusing to proceed!!! All migrations must have a unique ID!!!");
921            return;
922        }
923    }
924
925    // Okay, we're setup to go - apply them all. Note that we do these
926    // separately, each migration occurs in its own transaction.
927    for ScimMigration {
928        path,
929        hash,
930        assertions,
931    } in migrations
932    {
933        if let Err(err) = server_write_ref
934            .handle_scim_migration_apply(eventid, assertions, hash)
935            .await
936        {
937            error!(?err, path = %path.display(), "Failed to apply migration");
938        };
939    }
940}
941
942#[derive(Clone, Debug)]
943pub enum CoreAction {
944    Shutdown,
945    Reload,
946}
947
948pub(crate) enum TaskName {
949    AdminSocket,
950    AuditdActor,
951    BackupActor,
952    DelayedActionActor,
953    HttpsServer,
954    IntervalActor,
955    LdapActor,
956    ReplicationSupervisor,
957    TlsAcceptorReload,
958    MigrationReload,
959}
960
961impl Display for TaskName {
962    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
963        write!(
964            f,
965            "{}",
966            match self {
967                TaskName::AdminSocket => "Admin Socket",
968                TaskName::AuditdActor => "Auditd Actor",
969                TaskName::BackupActor => "Backup Actor",
970                TaskName::DelayedActionActor => "Delayed Action Actor",
971                TaskName::HttpsServer => "HTTPS Server",
972                TaskName::IntervalActor => "Interval Actor",
973                TaskName::LdapActor => "LDAP Acceptor Actor",
974                TaskName::ReplicationSupervisor => "Replication Supervisor",
975                TaskName::TlsAcceptorReload => "TlsAcceptor Reload Monitor",
976                TaskName::MigrationReload => "Migration Reload Monitor",
977            }
978        )
979    }
980}
981
982pub struct CoreHandle {
983    clean_shutdown: bool,
984    tx: broadcast::Sender<CoreAction>,
985    /// This stores a name for the handle, and the handle itself so we can tell which failed/succeeded at the end.
986    handles: Vec<(TaskName, task::JoinHandle<()>)>,
987}
988
989impl CoreHandle {
990    pub fn subscribe(&mut self) -> broadcast::Receiver<CoreAction> {
991        self.tx.subscribe()
992    }
993
994    pub async fn shutdown(&mut self) {
995        if self.tx.send(CoreAction::Shutdown).is_err() {
996            eprintln!("No receivers acked shutdown request. Treating as unclean.");
997            return;
998        }
999
1000        // Wait on the handles.
1001        while let Some((handle_name, handle)) = self.handles.pop() {
1002            debug!("Waiting for {handle_name} ...");
1003            if let Err(error) = handle.await {
1004                eprintln!("Task {handle_name} failed to finish: {error:?}");
1005            }
1006        }
1007
1008        self.clean_shutdown = true;
1009    }
1010
1011    pub async fn reload(&mut self) {
1012        if self.tx.send(CoreAction::Reload).is_err() {
1013            eprintln!("No receivers acked reload request.");
1014        }
1015    }
1016}
1017
1018impl Drop for CoreHandle {
1019    fn drop(&mut self) {
1020        if !self.clean_shutdown {
1021            eprintln!("⚠️  UNCLEAN SHUTDOWN OCCURRED ⚠️ ");
1022        }
1023        // Can't enable yet until we clean up unix_int cache layer test
1024        // debug_assert!(self.clean_shutdown);
1025    }
1026}
1027
1028#[allow(clippy::result_unit_err)]
1029pub async fn create_server_core(
1030    config: Configuration,
1031    config_test: bool,
1032) -> Result<CoreHandle, ()> {
1033    // Until this point, we probably want to write to the log macro fns.
1034    let (mut broadcast_tx, _broadcast_rx) = broadcast::channel(4);
1035
1036    if config.integration_test_config.is_some() {
1037        warn!("RUNNING IN INTEGRATION TEST MODE.");
1038        warn!("IF YOU SEE THIS IN PRODUCTION YOU MUST CONTACT SUPPORT IMMEDIATELY.");
1039    } else if config.tls_config.is_none() {
1040        // TLS is great! We won't run without it.
1041        error!("Running without TLS is not supported! Quitting!");
1042        return Err(());
1043    }
1044
1045    info!(
1046        "Starting kanidm with {}configuration: {}",
1047        if config_test { "TEST " } else { "" },
1048        config
1049    );
1050    // Setup umask, so that every we touch or create is secure.
1051    #[cfg(not(target_family = "windows"))]
1052    unsafe {
1053        umask(0o0027)
1054    };
1055
1056    // Setup TLS (if any)
1057    let maybe_tls_acceptor = match crypto::setup_tls(&config.tls_config) {
1058        Ok(tls_acc) => tls_acc,
1059        Err(err) => {
1060            error!(?err, "Failed to configure TLS acceptor");
1061            return Err(());
1062        }
1063    };
1064
1065    let schema = match Schema::new() {
1066        Ok(s) => s,
1067        Err(e) => {
1068            error!("Failed to setup in memory schema: {:?}", e);
1069            return Err(());
1070        }
1071    };
1072
1073    // Setup the be for the qs.
1074    let be = match setup_backend(&config, &schema) {
1075        Ok(be) => be,
1076        Err(e) => {
1077            error!("Failed to setup BE -> {:?}", e);
1078            return Err(());
1079        }
1080    };
1081    // Start the IDM server.
1082    let (_qs, idms, idms_delayed, idms_audit) = match setup_qs_idms(be, schema, &config).await {
1083        Ok(t) => t,
1084        Err(e) => {
1085            error!("Unable to setup query server or idm server -> {:?}", e);
1086            return Err(());
1087        }
1088    };
1089
1090    // Any pre-start tasks here.
1091    if let Some(itc) = &config.integration_test_config {
1092        let Ok(mut idms_prox_write) = idms.proxy_write(duration_from_epoch_now()).await else {
1093            error!("Unable to acquire write transaction");
1094            return Err(());
1095        };
1096        // We need to set the admin pw.
1097        match idms_prox_write.recover_account(&itc.admin_user, Some(&itc.admin_password)) {
1098            Ok(_) => {}
1099            Err(e) => {
1100                error!(
1101                    "Unable to configure INTEGRATION TEST {} account -> {:?}",
1102                    &itc.admin_user, e
1103                );
1104                return Err(());
1105            }
1106        };
1107        // set the idm_admin account password
1108        match idms_prox_write.recover_account(&itc.idm_admin_user, Some(&itc.idm_admin_password)) {
1109            Ok(_) => {}
1110            Err(e) => {
1111                error!(
1112                    "Unable to configure INTEGRATION TEST {} account -> {:?}",
1113                    &itc.idm_admin_user, e
1114                );
1115                return Err(());
1116            }
1117        };
1118
1119        // Add admin to idm_admins to allow tests more flexibility wrt to permissions.
1120        // This way our default access controls can be stricter to prevent lateral
1121        // movement.
1122        match idms_prox_write.qs_write.internal_modify_uuid(
1123            UUID_IDM_ADMINS,
1124            &ModifyList::new_append(Attribute::Member, Value::ReferN(UUID_ADMIN)),
1125        ) {
1126            Ok(_) => {}
1127            Err(e) => {
1128                error!(
1129                    "Unable to configure INTEGRATION TEST admin as member of idm_admins -> {:?}",
1130                    e
1131                );
1132                return Err(());
1133            }
1134        };
1135
1136        match idms_prox_write.qs_write.internal_modify_uuid(
1137            UUID_IDM_ALL_PERSONS,
1138            &ModifyList::new_purge_and_set(
1139                Attribute::CredentialTypeMinimum,
1140                CredentialType::Any.into(),
1141            ),
1142        ) {
1143            Ok(_) => {}
1144            Err(e) => {
1145                error!(
1146                    "Unable to configure INTEGRATION TEST default credential policy -> {:?}",
1147                    e
1148                );
1149                return Err(());
1150            }
1151        };
1152
1153        match idms_prox_write.commit() {
1154            Ok(_) => {}
1155            Err(e) => {
1156                error!("Unable to commit INTEGRATION TEST setup -> {:?}", e);
1157                return Err(());
1158            }
1159        }
1160    }
1161
1162    let ldap = match LdapServer::new(&idms).await {
1163        Ok(l) => l,
1164        Err(e) => {
1165            error!("Unable to start LdapServer -> {:?}", e);
1166            return Err(());
1167        }
1168    };
1169
1170    // Arc the idms and ldap
1171    let idms_arc = Arc::new(idms);
1172    let ldap_arc = Arc::new(ldap);
1173
1174    // Pass it to the actor for threading.
1175    // Start the read query server with the given be path: future config
1176    let server_read_ref = QueryServerReadV1::start_static(idms_arc.clone(), ldap_arc.clone());
1177
1178    // Create the server async write entry point.
1179    let server_write_ref = QueryServerWriteV1::start_static(idms_arc.clone());
1180
1181    let mut handles: Vec<(TaskName, task::JoinHandle<()>)> = Vec::with_capacity(16);
1182
1183    let startup_success = if config_test {
1184        info!("This config rocks! 🪨 ");
1185        Ok(())
1186    } else {
1187        launch_server_tasks(
1188            &mut handles,
1189            &config,
1190            &mut broadcast_tx,
1191            idms_delayed,
1192            idms_audit,
1193            server_read_ref,
1194            server_write_ref,
1195            idms_arc,
1196            maybe_tls_acceptor,
1197        )
1198        .await
1199    };
1200
1201    let mut server_ctx = CoreHandle {
1202        clean_shutdown: false,
1203        tx: broadcast_tx,
1204        handles,
1205    };
1206
1207    if startup_success.is_ok() {
1208        Ok(server_ctx)
1209    } else {
1210        server_ctx.shutdown().await;
1211        Err(())
1212    }
1213}
1214
1215#[allow(clippy::too_many_arguments)]
1216async fn launch_server_tasks(
1217    handles: &mut Vec<(TaskName, task::JoinHandle<()>)>,
1218
1219    config: &Configuration,
1220    broadcast_tx: &mut broadcast::Sender<CoreAction>,
1221
1222    mut idms_delayed: IdmServerDelayed,
1223    mut idms_audit: IdmServerAudit,
1224
1225    server_read_ref: &'static QueryServerReadV1,
1226    server_write_ref: &'static QueryServerWriteV1,
1227
1228    idms_arc: Arc<IdmServer>,
1229
1230    maybe_tls_acceptor: Option<TlsAcceptor>,
1231) -> Result<(), ()> {
1232    // Similar, create a stats task which aggregates statistics from the
1233    // server as they come in.
1234    let status_ref = StatusActor::start();
1235
1236    // Delayed actions
1237    let mut broadcast_rx = broadcast_tx.subscribe();
1238
1239    let delayed_handle = task::spawn(async move {
1240        let mut buffer = Vec::with_capacity(DELAYED_ACTION_BATCH_SIZE);
1241        loop {
1242            tokio::select! {
1243                added = idms_delayed.recv_many(&mut buffer) => {
1244                    if added == 0 {
1245                        // Channel has closed, stop the task.
1246                        break
1247                    }
1248                    server_write_ref.handle_delayedaction(&mut buffer).await;
1249                }
1250                Ok(action) = broadcast_rx.recv() => {
1251                    match action {
1252                        CoreAction::Shutdown => break,
1253                        CoreAction::Reload => {},
1254                    }
1255                }
1256            }
1257        }
1258        info!("Stopped {}", TaskName::DelayedActionActor);
1259    });
1260
1261    handles.push((TaskName::DelayedActionActor, delayed_handle));
1262
1263    // Auditing tasks
1264    let mut broadcast_rx = broadcast_tx.subscribe();
1265
1266    let auditd_handle = task::spawn(async move {
1267        loop {
1268            tokio::select! {
1269                Ok(action) = broadcast_rx.recv() => {
1270                    match action {
1271                        CoreAction::Shutdown => break,
1272                        CoreAction::Reload => {},
1273                    }
1274                }
1275                audit_event = idms_audit.audit_rx().recv() => {
1276                    match serde_json::to_string(&audit_event) {
1277                        Ok(audit_event) => {
1278                            warn!(%audit_event);
1279                        }
1280                        Err(e) => {
1281                            error!(err=?e, "Unable to process audit event to json.");
1282                            warn!(?audit_event, json=false);
1283                        }
1284                    }
1285
1286                }
1287            }
1288        }
1289        info!("Stopped {}", TaskName::AuditdActor);
1290    });
1291
1292    handles.push((TaskName::AuditdActor, auditd_handle));
1293
1294    // Run the migrations *once*, only in production though.
1295    let migration_path = config
1296        .migration_path
1297        .clone()
1298        .unwrap_or(PathBuf::from(env!("KANIDM_SERVER_MIGRATION_PATH")));
1299
1300    if config.integration_test_config.is_none() {
1301        let eventid = Uuid::new_v4();
1302        migration_apply(eventid, server_write_ref, migration_path.as_path()).await;
1303        // Skip all these handles in integration test mode.
1304
1305        // Setup the Migration Reload Trigger.
1306        let broadcast_rx = broadcast_tx.subscribe();
1307        let migration_reload_handle = task::spawn(async move {
1308            migration_reload_supervisor(broadcast_rx, server_write_ref, migration_path).await
1309        });
1310
1311        handles.push((TaskName::MigrationReload, migration_reload_handle));
1312
1313        // Setup timed events associated to the write thread
1314        let interval_handle = IntervalActor::start(server_write_ref, broadcast_tx.subscribe());
1315
1316        handles.push((TaskName::IntervalActor, interval_handle));
1317
1318        // Setup timed events associated to the read thread
1319        match &config.online_backup {
1320            Some(online_backup_config) => {
1321                if online_backup_config.enabled {
1322                    let backup_handle = IntervalActor::start_online_backup(
1323                        server_read_ref,
1324                        online_backup_config,
1325                        broadcast_tx.subscribe(),
1326                    )?;
1327                    handles.push((TaskName::BackupActor, backup_handle));
1328                } else {
1329                    debug!("Backups disabled");
1330                }
1331            }
1332            None => {
1333                debug!("Online backup not configured, skipping");
1334            }
1335        };
1336
1337        // If we have replication configured, setup the listener with its initial replication
1338        // map (if any).
1339        let maybe_repl_ctrl_tx = match &config.repl_config {
1340            Some(rc) => {
1341                // ⚠️  only start the sockets and listeners in non-config-test modes.
1342                let repl_server_handles =
1343                    repl::create_repl_server(idms_arc.clone(), rc, broadcast_tx.subscribe())
1344                        .await?;
1345
1346                let ReplicationServerHandles {
1347                    repl_handle,
1348                    ctrl_tx,
1349                } = repl_server_handles;
1350
1351                handles.push((TaskName::ReplicationSupervisor, repl_handle));
1352
1353                Some(ctrl_tx)
1354            }
1355            None => {
1356                debug!("Replication not configured, skipping");
1357                None
1358            }
1359        };
1360
1361        let broadcast_tx_ = broadcast_tx.clone();
1362
1363        let admin_handle = AdminActor::create_admin_sock(
1364            config.adminbindpath.as_str(),
1365            server_write_ref,
1366            server_read_ref,
1367            broadcast_tx_,
1368            maybe_repl_ctrl_tx,
1369        )
1370        .await?;
1371
1372        handles.push((TaskName::AdminSocket, admin_handle));
1373    }
1374
1375    // Setup a TLS Acceptor Reload trigger.
1376
1377    let mut broadcast_rx = broadcast_tx.subscribe();
1378    let tls_config = config.tls_config.clone();
1379
1380    let (tls_acceptor_reload_tx, _tls_acceptor_reload_rx) = broadcast::channel(1);
1381    let tls_acceptor_reload_tx_c = tls_acceptor_reload_tx.clone();
1382
1383    let tls_acceptor_reload_handle = task::spawn(async move {
1384        loop {
1385            tokio::select! {
1386                Ok(action) = broadcast_rx.recv() => {
1387                    match action {
1388                        CoreAction::Shutdown => break,
1389                        CoreAction::Reload => {
1390                            let tls_acceptor = match crypto::setup_tls(&tls_config) {
1391                                Ok(Some(tls_acc)) => tls_acc,
1392                                Ok(None) => {
1393                                    warn!("TLS not configured, ignoring reload request.");
1394                                    continue;
1395                                }
1396                                Err(err) => {
1397                                    error!(?err, "Failed to configure and reload TLS acceptor");
1398                                    continue;
1399                                }
1400                            };
1401
1402                            // We don't log here as the receivers will notify when they have completed
1403                            // the reload.
1404                            if tls_acceptor_reload_tx_c.send(tls_acceptor).is_err() {
1405                                error!("TLS acceptor did not accept the reload, the server may have failed!");
1406                            };
1407                            info!("TLS acceptor reload notification sent");
1408                        },
1409                    }
1410                }
1411            }
1412        }
1413        info!("Stopped {}", TaskName::TlsAcceptorReload);
1414    });
1415
1416    handles.push((TaskName::TlsAcceptorReload, tls_acceptor_reload_handle));
1417
1418    // If we have been requested to init LDAP, configure it now.
1419    match &config.ldapbindaddress {
1420        Some(la) => {
1421            let logging_pipeline = match config.otel_grpc_endpoint {
1422                Some(_) => LoggerType::OpenTelemetry,
1423                None => LoggerType::TracingForest,
1424            };
1425            let opt_ldap_ssl_acceptor = maybe_tls_acceptor.clone();
1426
1427            let ldap_handles = ldaps::create_ldap_server(
1428                la,
1429                opt_ldap_ssl_acceptor,
1430                server_read_ref,
1431                broadcast_tx,
1432                &tls_acceptor_reload_tx,
1433                Arc::new(config.ldap_client_address_info.trusted_tcp_info()),
1434                logging_pipeline,
1435            )
1436            .await?;
1437            for ldap_handle in ldap_handles {
1438                handles.push((TaskName::LdapActor, ldap_handle));
1439            }
1440        }
1441        None => {
1442            debug!("LDAP not requested, skipping");
1443        }
1444    };
1445
1446    // Finally launch the https tasks.
1447    let http_handles: Vec<task::JoinHandle<()>> = https::create_https_server(
1448        config.clone(),
1449        status_ref,
1450        server_write_ref,
1451        server_read_ref,
1452        broadcast_tx.clone(),
1453        maybe_tls_acceptor,
1454        &tls_acceptor_reload_tx,
1455    )
1456    .await
1457    .inspect_err(|err| {
1458        error!(?err, "Failed to start HTTPS server");
1459    })?;
1460
1461    if config.role != ServerRole::WriteReplicaNoUI {
1462        admin_info!("Ready to rock! 🪨  UI available at: {}", config.origin);
1463    } else {
1464        admin_info!("Ready to rock! 🪨 ");
1465    }
1466
1467    for http_handle in http_handles {
1468        handles.push((TaskName::HttpsServer, http_handle))
1469    }
1470
1471    Ok(())
1472}