kanidm_unixd_tasks/
kanidm_unixd_tasks.rs

1#![deny(warnings)]
2#![warn(unused_extern_crates)]
3#![deny(clippy::todo)]
4#![deny(clippy::unimplemented)]
5#![deny(clippy::unwrap_used)]
6#![deny(clippy::expect_used)]
7#![deny(clippy::panic)]
8#![deny(clippy::unreachable)]
9#![deny(clippy::await_holding_lock)]
10#![deny(clippy::needless_pass_by_value)]
11#![deny(clippy::trivially_copy_pass_by_ref)]
12
13use futures::{SinkExt, StreamExt};
14use kanidm_unix_common::constants::{
15    DEFAULT_CONFIG_PATH, SYSTEM_GROUP_PATH, SYSTEM_PASSWD_PATH, SYSTEM_SHADOW_PATH,
16};
17use kanidm_unix_common::json_codec::JsonCodec;
18use kanidm_unix_common::unix_config::UnixdConfig;
19use kanidm_unix_common::unix_passwd::{parse_etc_group, parse_etc_passwd, parse_etc_shadow, EtcDb};
20use kanidm_unix_common::unix_proto::{
21    HomeDirectoryInfo, TaskRequest, TaskRequestFrame, TaskResponse,
22};
23use kanidm_utils_users::{get_effective_gid, get_effective_uid};
24use libc::{lchown, umask};
25use notify_debouncer_full::notify::RecommendedWatcher;
26use notify_debouncer_full::Debouncer;
27use notify_debouncer_full::RecommendedCache;
28use notify_debouncer_full::{new_debouncer, notify::RecursiveMode, DebouncedEvent};
29use sketching::tracing_forest::traits::*;
30use sketching::tracing_forest::util::*;
31use sketching::tracing_forest::{self};
32use std::ffi::CString;
33use std::os::unix::ffi::OsStrExt;
34use std::os::unix::fs::symlink;
35use std::path::{Path, PathBuf};
36use std::process::ExitCode;
37use std::time::Duration;
38use std::{fs, io};
39use tokio::fs::File;
40use tokio::io::AsyncReadExt;
41use tokio::net::UnixStream;
42use tokio::sync::broadcast;
43use tokio::sync::watch;
44use tokio::time;
45use tokio_util::codec::Framed;
46use tracing::instrument;
47use walkdir::WalkDir;
48
49#[cfg(all(target_family = "unix", feature = "selinux"))]
50use kanidm_unix_common::selinux_util;
51
52fn chown(path: &Path, gid: u32) -> Result<(), String> {
53    let path_os = CString::new(path.as_os_str().as_bytes())
54        .map_err(|_| "Unable to create c-string".to_string())?;
55
56    // Change the owner to the gid - remember, kanidm ONLY has gid's, the uid is implied.
57    if unsafe { lchown(path_os.as_ptr(), gid, gid) } != 0 {
58        return Err("Unable to set ownership".to_string());
59    }
60    Ok(())
61}
62
63fn create_home_directory(
64    info: &HomeDirectoryInfo,
65    home_prefix_path: &Path,
66    home_mount_prefix_path: Option<&PathBuf>,
67    use_etc_skel: bool,
68    use_selinux: bool,
69) -> Result<(), String> {
70    // Final sanity check to prevent certain classes of attacks. This should *never*
71    // be possible, but we assert this to be sure.
72    let name = info.name.trim_start_matches('.').replace(['/', '\\'], "");
73
74    debug!(?home_prefix_path, ?home_mount_prefix_path, ?info);
75
76    // This is where the users home dir "is" and aliases from here go to the true storage
77    // mounts
78    let home_prefix_path = home_prefix_path
79        .canonicalize()
80        .map_err(|e| format!("{e:?}"))?;
81
82    // This is where the storage is *mounted*. If not set, falls back to the home_prefix.
83    let home_mount_prefix_path = home_mount_prefix_path
84        .unwrap_or(&home_prefix_path)
85        .canonicalize()
86        .map_err(|e| format!("{e:?}"))?;
87
88    // Does our home_prefix actually exist?
89    if !home_prefix_path.exists() || !home_prefix_path.is_dir() || !home_prefix_path.is_absolute() {
90        return Err("Invalid home_prefix from configuration - home_prefix path must exist, must be a directory, and must be absolute (not relative)".to_string());
91    }
92
93    if !home_mount_prefix_path.exists()
94        || !home_mount_prefix_path.is_dir()
95        || !home_mount_prefix_path.is_absolute()
96    {
97        return Err("Invalid home_mount_prefix from configuration - home_prefix path must exist, must be a directory, and must be absolute (not relative)".to_string());
98    }
99
100    // This is now creating the actual home directory in the home_mount path.
101    // First we want to validate that the path is legitimate and hasn't tried
102    // to escape the home_mount prefix.
103    let hd_mount_path = Path::join(&home_mount_prefix_path, &name);
104
105    debug!(?hd_mount_path);
106
107    if let Some(pp) = hd_mount_path.parent() {
108        if pp != home_mount_prefix_path {
109            return Err("Invalid home directory name - not within home_mount_prefix".to_string());
110        }
111    } else {
112        return Err("Invalid/Corrupt home directory path - no prefix found".to_string());
113    }
114
115    // Get a handle to the SELinux labeling interface
116    debug!(?use_selinux, "selinux for home dir labeling");
117    #[cfg(all(target_family = "unix", feature = "selinux"))]
118    let labeler = if use_selinux {
119        selinux_util::SelinuxLabeler::new(info.gid, &home_mount_prefix_path)?
120    } else {
121        selinux_util::SelinuxLabeler::new_noop()
122    };
123
124    // Does the home directory exist? This is checking the *true* home mount storage.
125    if !hd_mount_path.exists() {
126        // Set the SELinux security context for file creation
127        #[cfg(all(target_family = "unix", feature = "selinux"))]
128        labeler.do_setfscreatecon_for_path()?;
129
130        // Set a umask
131        let before = unsafe { umask(0o0027) };
132
133        // Create the dir
134        if let Err(e) = fs::create_dir_all(&hd_mount_path) {
135            let _ = unsafe { umask(before) };
136            error!(err = ?e, ?hd_mount_path, "Unable to create directory");
137            return Err(format!("{e:?}"));
138        }
139        let _ = unsafe { umask(before) };
140
141        chown(&hd_mount_path, info.gid)?;
142
143        // Copy in structure from /etc/skel/ if present
144        let skel_dir = Path::new("/etc/skel/");
145        if use_etc_skel && skel_dir.exists() {
146            info!("preparing homedir using /etc/skel");
147            for entry in WalkDir::new(skel_dir).into_iter().filter_map(|e| e.ok()) {
148                let dest = &hd_mount_path.join(
149                    entry
150                        .path()
151                        .strip_prefix(skel_dir)
152                        .map_err(|e| e.to_string())?,
153                );
154
155                #[cfg(all(target_family = "unix", feature = "selinux"))]
156                {
157                    let p = entry
158                        .path()
159                        .strip_prefix(skel_dir)
160                        .map_err(|e| e.to_string())?;
161                    labeler.label_path(p)?;
162                }
163
164                if entry.path().is_dir() {
165                    fs::create_dir_all(dest).map_err(|e| {
166                        error!(err = ?e, ?dest, "Unable to create directory from /etc/skel");
167                        e.to_string()
168                    })?;
169                } else {
170                    fs::copy(entry.path(), dest).map_err(|e| {
171                        error!(err = ?e, ?dest, "Unable to copy from /etc/skel");
172                        e.to_string()
173                    })?;
174                }
175                chown(dest, info.gid)?;
176
177                // Create equivalence rule in the SELinux policy
178                #[cfg(all(target_family = "unix", feature = "selinux"))]
179                labeler.setup_equivalence_rule(&hd_mount_path)?;
180            }
181        }
182    }
183
184    // Reset object creation SELinux context to default
185    #[cfg(all(target_family = "unix", feature = "selinux"))]
186    labeler.set_default_context_for_fs_objects()?;
187
188    // Do the aliases exist?
189    for alias in info.aliases.iter() {
190        // Sanity check the alias.
191        // let alias = alias.replace(".", "").replace("/", "").replace("\\", "");
192        let alias = alias.trim_start_matches('.').replace(['/', '\\'], "");
193
194        let alias_path = Path::join(&home_prefix_path, &alias);
195
196        // Assert the resulting alias path is consistent and correct within the home_prefix.
197        if let Some(pp) = alias_path.parent() {
198            if pp != home_prefix_path {
199                return Err("Invalid home directory alias - not within home_prefix".to_string());
200            }
201        } else {
202            return Err("Invalid/Corrupt alias directory path - no prefix found".to_string());
203        }
204
205        if alias_path.exists() {
206            debug!("checking symlink {:?} -> {:?}", alias_path, hd_mount_path);
207            let attr = match fs::symlink_metadata(&alias_path) {
208                Ok(a) => a,
209                Err(e) => {
210                    error!(err = ?e, ?alias_path, "Unable to read alias path metadata");
211                    return Err(format!("{e:?}"));
212                }
213            };
214
215            if attr.file_type().is_symlink() {
216                // Probably need to update it.
217                if let Err(e) = fs::remove_file(&alias_path) {
218                    error!(err = ?e, ?alias_path, "Unable to remove existing alias path");
219                    return Err(format!("{e:?}"));
220                }
221
222                debug!("updating symlink {:?} -> {:?}", alias_path, hd_mount_path);
223                if let Err(e) = symlink(&hd_mount_path, &alias_path) {
224                    error!(err = ?e, ?alias_path, "Unable to update alias path");
225                    return Err(format!("{e:?}"));
226                }
227            } else {
228                warn!(
229                    ?alias_path,
230                    ?hd_mount_path,
231                    "home directory alias path is not a symlink, unable to update"
232                );
233            }
234        } else {
235            // Does not exist. Create.
236            debug!("creating symlink {:?} -> {:?}", alias_path, hd_mount_path);
237            if let Err(e) = symlink(&hd_mount_path, &alias_path) {
238                error!(err = ?e, ?alias_path, "Unable to create alias path");
239                return Err(format!("{e:?}"));
240            }
241        }
242    }
243    Ok(())
244}
245
246async fn shadow_reload_task(
247    shadow_data_watch_tx: watch::Sender<EtcDb>,
248    mut shadow_broadcast_rx: broadcast::Receiver<bool>,
249) {
250    debug!("shadow reload task has started ...");
251
252    while shadow_broadcast_rx.recv().await.is_ok() {
253        match process_etc_passwd_group().await {
254            Ok(etc_db) => {
255                shadow_data_watch_tx.send_replace(etc_db);
256                debug!("shadow reload task sent");
257            }
258            Err(()) => {
259                error!("Unable to process etc db");
260                continue;
261            }
262        }
263    }
264
265    debug!("shadow reload task has stopped");
266}
267
268async fn handle_shadow_reload(shadow_data_watch_rx: &mut watch::Receiver<EtcDb>) -> TaskResponse {
269    debug!("Received shadow reload event.");
270    let etc_db: EtcDb = {
271        let etc_db_ref = shadow_data_watch_rx.borrow_and_update();
272        (*etc_db_ref).clone()
273    };
274    // process etc shadow and send it here.
275    TaskResponse::NotifyShadowChange(etc_db)
276}
277
278async fn handle_unixd_request(
279    request: Option<Result<TaskRequestFrame, io::Error>>,
280    cfg: &UnixdConfig,
281) -> Result<TaskResponse, ()> {
282    debug!("Received unixd event.");
283    match request {
284        Some(Ok(TaskRequestFrame {
285            id,
286            req: TaskRequest::HomeDirectory(info),
287        })) => {
288            debug!("Received task -> HomeDirectory({:?})", info);
289
290            match create_home_directory(
291                &info,
292                cfg.home_prefix.as_ref(),
293                cfg.home_mount_prefix.as_ref(),
294                cfg.use_etc_skel,
295                cfg.selinux,
296            ) {
297                Ok(()) => Ok(TaskResponse::Success(id)),
298                Err(msg) => Ok(TaskResponse::Error(msg)),
299            }
300        }
301        other => {
302            error!("Error -> got un-handled Request Frame {other:?}");
303            Err(())
304        }
305    }
306}
307
308async fn handle_tasks(
309    stream: UnixStream,
310    ctl_broadcast_rx: &mut broadcast::Receiver<bool>,
311    shadow_data_watch_rx: &mut watch::Receiver<EtcDb>,
312    cfg: &UnixdConfig,
313) {
314    let codec: JsonCodec<TaskRequestFrame, TaskResponse> = JsonCodec::default();
315
316    let mut reqs = Framed::new(stream, codec);
317
318    // Immediately trigger that we should reload the shadow files for the new connected handler
319    shadow_data_watch_rx.mark_changed();
320
321    debug!("Task handler loop has started ...");
322
323    loop {
324        let msg = tokio::select! {
325            biased; // tell tokio to poll these in order
326            _ = ctl_broadcast_rx.recv() => {
327                // We received a shutdown signal.
328                debug!("Received shutdown signal, breaking task handler loop ...");
329                return
330            }
331            // We bias to *sending* messages in tasks.
332            Ok(_) = shadow_data_watch_rx.changed() => {
333                handle_shadow_reload(shadow_data_watch_rx).await
334            }
335            request = reqs.next() => {
336                match handle_unixd_request(request,  cfg).await {
337                    Ok(response) => {
338                       response
339                    }
340                    Err(_) => {
341                        error!("Error handling request, exiting task handler loop ...");
342                        return;
343                    }
344                }
345            }
346        };
347
348        if let Err(e) = reqs.send(msg).await {
349            error!(?e, "Error sending response to kanidm_unixd");
350            return;
351        }
352    }
353}
354
355#[instrument(level = "debug", skip_all)]
356async fn process_etc_passwd_group() -> Result<EtcDb, ()> {
357    let mut file = File::open(SYSTEM_PASSWD_PATH).await.map_err(|err| {
358        error!(?err);
359    })?;
360    let mut contents = vec![];
361    file.read_to_end(&mut contents).await.map_err(|err| {
362        error!(?err);
363    })?;
364
365    let users = parse_etc_passwd(contents.as_slice())
366        .map_err(|_| "Invalid passwd content")
367        .map_err(|err| {
368            error!(?err);
369        })?;
370
371    let mut file = File::open(SYSTEM_SHADOW_PATH).await.map_err(|err| {
372        error!(?err);
373    })?;
374    let mut contents = vec![];
375    file.read_to_end(&mut contents).await.map_err(|err| {
376        error!(?err);
377    })?;
378
379    let shadow = parse_etc_shadow(contents.as_slice())
380        .map_err(|_| "Invalid passwd content")
381        .map_err(|err| {
382            error!(?err);
383        })?;
384
385    let mut file = File::open(SYSTEM_GROUP_PATH).await.map_err(|err| {
386        error!(?err);
387    })?;
388    let mut contents = vec![];
389    file.read_to_end(&mut contents).await.map_err(|err| {
390        error!(?err);
391    })?;
392
393    let groups = parse_etc_group(contents.as_slice())
394        .map_err(|_| "Invalid group content")
395        .map_err(|err| {
396            error!(?err);
397        })?;
398
399    Ok(EtcDb {
400        users,
401        shadow,
402        groups,
403    })
404}
405
406fn setup_shadow_inotify_watcher(
407    shadow_broadcast_tx: broadcast::Sender<bool>,
408) -> Result<Debouncer<RecommendedWatcher, RecommendedCache>, ExitCode> {
409    let watcher = new_debouncer(
410        Duration::from_secs(5),
411        None,
412        move |event: Result<Vec<DebouncedEvent>, _>| {
413            let array_of_events = match event {
414                Ok(events) => events,
415                Err(array_errors) => {
416                    for err in array_errors {
417                        error!(?err, "inotify debounce error");
418                    }
419                    return;
420                }
421            };
422
423            let mut path_of_interest_was_changed = false;
424
425            for inode_event in array_of_events.iter() {
426                if !inode_event.kind.is_access()
427                    && inode_event.paths.iter().any(|path| {
428                        path == Path::new(SYSTEM_GROUP_PATH)
429                            || path == Path::new(SYSTEM_PASSWD_PATH)
430                            || path == Path::new(SYSTEM_SHADOW_PATH)
431                    })
432                {
433                    debug!(?inode_event, "Handling inotify modification event");
434
435                    path_of_interest_was_changed = true
436                }
437            }
438
439            if path_of_interest_was_changed {
440                let _ = shadow_broadcast_tx.send(true);
441            } else {
442                trace!(?array_of_events, "IGNORED");
443            }
444        },
445    )
446    .and_then(|mut debouncer| {
447        debouncer
448            .watch(Path::new("/etc"), RecursiveMode::Recursive)
449            .map(|()| debouncer)
450    });
451
452    watcher.map_err(|err| {
453        error!(?err, "Failed to setup inotify");
454        ExitCode::FAILURE
455    })
456}
457
458#[tokio::main(flavor = "current_thread")]
459async fn main() -> ExitCode {
460    // On linux when debug assertions are disabled, prevent ptrace
461    // from attaching to us.
462    #[cfg(all(target_os = "linux", not(debug_assertions)))]
463    if let Err(code) = prctl::set_dumpable(false) {
464        error!(?code, "CRITICAL: Unable to set prctl flags");
465        return ExitCode::FAILURE;
466    }
467    // let cuid = get_current_uid();
468    // let cgid = get_current_gid();
469    // We only need to check effective id
470    let ceuid = get_effective_uid();
471    let cegid = get_effective_gid();
472
473    for arg in std::env::args() {
474        if arg.contains("--version") {
475            println!("kanidm_unixd_tasks {}", env!("CARGO_PKG_VERSION"));
476            return ExitCode::SUCCESS;
477        } else if arg.contains("--help") {
478            println!("kanidm_unixd_tasks {}", env!("CARGO_PKG_VERSION"));
479            println!("Usage: kanidm_unixd_tasks");
480            println!("  --version");
481            println!("  --help");
482            return ExitCode::SUCCESS;
483        }
484    }
485
486    #[allow(clippy::expect_used)]
487    tracing_forest::worker_task()
488        .set_global(true)
489        // Fall back to stderr
490        .map_sender(|sender| sender.or_stderr())
491        .build_on(|subscriber| {
492            subscriber.with(
493                EnvFilter::try_from_default_env()
494                    .or_else(|_| EnvFilter::try_new("info"))
495                    .expect("Failed to init envfilter"),
496            )
497        })
498        .on(async {
499            if ceuid != 0 || cegid != 0 {
500                error!("Refusing to run - this process *MUST* operate as root.");
501                return ExitCode::FAILURE;
502            }
503
504            let unixd_path = Path::new(DEFAULT_CONFIG_PATH);
505            let unixd_path_str = match unixd_path.to_str() {
506                Some(cps) => cps,
507                None => {
508                    error!("Unable to turn unixd_path to str");
509                    return ExitCode::FAILURE;
510                }
511            };
512
513            let cfg = match UnixdConfig::new().read_options_from_optional_config(unixd_path) {
514                Ok(v) => v,
515                Err(_) => {
516                    error!("Failed to parse {}", unixd_path_str);
517                    return ExitCode::FAILURE;
518                }
519            };
520
521            let task_sock_path = cfg.task_sock_path.clone();
522            debug!("Attempting to use {} ...", task_sock_path);
523
524            // This is the startup/shutdown control channel
525            let (broadcast_tx, mut broadcast_rx) = broadcast::channel(4);
526            let mut d_broadcast_rx = broadcast_tx.subscribe();
527
528            // This is to broadcast when we need to reload the shadow
529            // files.
530            let (shadow_broadcast_tx, shadow_broadcast_rx) = broadcast::channel(4);
531
532            let watcher = match setup_shadow_inotify_watcher(shadow_broadcast_tx.clone()) {
533                Ok(w) => w,
534                Err(exit) => return exit,
535            };
536
537            // Setup the etcdb watch
538            let etc_db = match process_etc_passwd_group().await {
539                Ok(etc_db) => etc_db,
540                Err(err) => {
541                    warn!(?err, "unable to process {SYSTEM_PASSWD_PATH} and related files.");
542                    // Return an empty set instead.
543                    EtcDb::default()
544                }
545            };
546
547            let (shadow_data_watch_tx, mut shadow_data_watch_rx) = watch::channel(etc_db);
548
549            let _shadow_task = tokio::spawn(async move {
550                shadow_reload_task(
551                    shadow_data_watch_tx, shadow_broadcast_rx
552                ).await
553            });
554
555            let server = tokio::spawn(async move {
556                loop {
557                    info!("Attempting to connect to kanidm_unixd ...");
558
559                    tokio::select! {
560                        _ = broadcast_rx.recv() => {
561                            break;
562                        }
563                        connect_res = UnixStream::connect(&task_sock_path) => {
564                            match connect_res {
565                                Ok(stream) => {
566                                    info!("Found kanidm_unixd, waiting for tasks ...");
567
568                                    // Yep! Now let the main handler do its job.
569                                    // If it returns (dc, etc, then we loop and try again).
570                                    handle_tasks(stream, &mut d_broadcast_rx, &mut shadow_data_watch_rx, &cfg).await;
571                                    continue;
572                                }
573                                Err(e) => {
574                                    debug!("\\---> {:?}", e);
575                                    error!("Unable to find kanidm_unixd, sleeping ...");
576                                    // Back off.
577                                    time::sleep(Duration::from_millis(5000)).await;
578                                }
579                            }
580                        }
581                    } // select
582                } // loop
583            });
584
585            info!("Server started ...");
586
587            // On linux, notify systemd.
588            #[cfg(target_os = "linux")]
589            let _ = sd_notify::notify(true, &[sd_notify::NotifyState::Ready]);
590
591            loop {
592                tokio::select! {
593                    Ok(()) = tokio::signal::ctrl_c() => {
594                        break
595                    }
596                    Some(()) = async move {
597                        let sigterm = tokio::signal::unix::SignalKind::terminate();
598                        #[allow(clippy::unwrap_used)]
599                        tokio::signal::unix::signal(sigterm).unwrap().recv().await
600                    } => {
601                        break
602                    }
603                    Some(()) = async move {
604                        let sigterm = tokio::signal::unix::SignalKind::alarm();
605                        #[allow(clippy::unwrap_used)]
606                        tokio::signal::unix::signal(sigterm).unwrap().recv().await
607                    } => {
608                        // Ignore
609                    }
610                    Some(()) = async move {
611                        let sigterm = tokio::signal::unix::SignalKind::hangup();
612                        #[allow(clippy::unwrap_used)]
613                        tokio::signal::unix::signal(sigterm).unwrap().recv().await
614                    } => {
615                        // Ignore
616                    }
617                    Some(()) = async move {
618                        let sigterm = tokio::signal::unix::SignalKind::user_defined1();
619                        #[allow(clippy::unwrap_used)]
620                        tokio::signal::unix::signal(sigterm).unwrap().recv().await
621                    } => {
622                        // Ignore
623                    }
624
625                    Some(()) = async move {
626                        let sigterm = tokio::signal::unix::SignalKind::user_defined2();
627                        #[allow(clippy::unwrap_used)]
628                        tokio::signal::unix::signal(sigterm).unwrap().recv().await
629                    } => {
630                        // Ignore
631                    }
632                }
633            }
634            info!("Signal received, shutting down");
635            // Send a broadcast that we are done.
636            if let Err(e) = broadcast_tx.send(true) {
637                error!("Unable to shutdown workers {:?}", e);
638            }
639
640            debug!("Dropping inotify watcher ...");
641            drop(watcher);
642
643            let _ = server.await;
644            ExitCode::SUCCESS
645        })
646        .await
647}