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
#![deny(warnings)]
#![deny(clippy::complexity)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]
#![deny(clippy::panic)]
#![deny(clippy::needless_pass_by_value)]
#![deny(clippy::trivially_copy_pass_by_ref)]


extern crate libc;
extern crate nix;

use snafu::Snafu;

pub mod ffi;
pub mod user;
pub mod group;
pub mod stdio;
pub mod daemon;

#[derive(Debug, Snafu)]
pub enum DaemonError {
    /// This feature is unavailable, or not implemented for your target os
    UnsupportedOnOS,
    /// Unable to fork
    Fork,
    /// Failed to chdir
    ChDir,
    /// Failed to open dev null
    OpenDevNull,
    /// Failed to close the file pointer of a stdio stream
    CloseFp,
    /// Invalid or nonexistent user
    InvalidUser,
    /// Invalid or nonexistent group
    InvalidGroup,
    /// Either group or user was specified but no the other
    InvalidUserGroupPair,
    /// The specified cstr is invalid
    InvalidCstr,
    /// Failed to execute initgroups
    InitGroups,
    /// Failed to set uid
    SetUid,
    /// Failed to set gid
    SetGid,
    /// Failed to chown the pid file
    ChownPid,
    /// Failed to create the pid file
    OpenPid,
    /// Failed to write to the pid file
    WritePid,
    /// Failed to redirect the standard streams
    RedirectStream,
    /// Umask bits are invalid
    InvalidUmaskBits,
    /// Failed to set sid
    SetSid,
    /// Failed to get groups record
    GetGrRecord,
    /// Failed to get passwd record
    GetPasswdRecord,
    /// Failed to set proc name
    SetProcName,
    InvalidProcName,
    #[doc(hidden)]
    __Nonexhaustive,
}

pub type Result<T> = std::result::Result<T, DaemonError>;

#[cfg(test)]
mod tests {
    // TODO: Improve testing coverage
    extern crate nix;

    use std::convert::TryFrom;
    use crate::daemon::Daemon;
    use crate::user::User;

    #[test]
    fn test_uname_to_uid_resolution() {
        let daemon = Daemon::new().user(User::try_from("root").unwrap());
        assert!(daemon.user.is_some());
        let uid = match daemon.user.unwrap() {
            User::Id(id) => id,
        };
        assert_eq!(uid, 0)
    }
}