priv_sep

Privilege separation library.
git clone https://git.philomathiclife.com/repos/priv_sep
Log | Files | Refs | README

c.rs (4434B)


      1 use super::{Gid, Uid, UserInfo};
      2 #[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "vita"))]
      3 use core::ffi::c_ushort;
      4 use core::ffi::{c_char, c_int};
      5 /// Error code when a libc call is successful.
      6 pub const SUCCESS: c_int = 0;
      7 /// `uid_t` and `gid_t`.
      8 #[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "vita"))]
      9 pub type IdT = c_ushort;
     10 /// `uid_t` and `gid_t`.
     11 #[cfg(target_os = "nto")]
     12 pub type IdT = i32;
     13 /// `uid_t` and `gid_t`.
     14 #[cfg(not(any(
     15     target_os = "espidf",
     16     target_os = "horizon",
     17     target_os = "nto",
     18     target_os = "vita"
     19 )))]
     20 pub type IdT = u32;
     21 /// `time_t`.
     22 #[cfg(all(
     23     any(
     24         target_os = "dragonfly",
     25         target_os = "freebsd",
     26         target_os = "macos",
     27         target_os = "netbsd",
     28         target_os = "openbsd"
     29     ),
     30     target_arch = "aarch64",
     31     target_pointer_width = "32"
     32 ))]
     33 type TimeT = i32;
     34 /// `time_t`.
     35 #[cfg(all(
     36     any(
     37         target_os = "dragonfly",
     38         target_os = "freebsd",
     39         target_os = "macos",
     40         target_os = "netbsd",
     41         target_os = "openbsd"
     42     ),
     43     not(all(target_arch = "aarch64", target_pointer_width = "32"))
     44 ))]
     45 type TimeT = i64;
     46 /// [`passwd`](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/basedefs/pwd.h.html).
     47 #[repr(C)]
     48 pub struct Passwd {
     49     /// Username.
     50     name: *mut c_char,
     51     /// User password.
     52     pass: *mut c_char,
     53     /// User ID.
     54     uid: IdT,
     55     /// Group ID.
     56     gid: IdT,
     57     /// Password change time.
     58     #[cfg(any(
     59         target_os = "dragonfly",
     60         target_os = "freebsd",
     61         target_os = "macos",
     62         target_os = "netbsd",
     63         target_os = "openbsd"
     64     ))]
     65     change: TimeT,
     66     /// User access class.
     67     #[cfg(any(
     68         target_os = "dragonfly",
     69         target_os = "freebsd",
     70         target_os = "macos",
     71         target_os = "netbsd",
     72         target_os = "openbsd"
     73     ))]
     74     class: *mut c_char,
     75     /// User information.
     76     gecos: *mut c_char,
     77     /// Home directory.
     78     dir: *mut c_char,
     79     /// Shell program.
     80     shell: *mut c_char,
     81     /// Account expiration.
     82     #[cfg(any(
     83         target_os = "dragonfly",
     84         target_os = "freebsd",
     85         target_os = "macos",
     86         target_os = "netbsd",
     87         target_os = "openbsd"
     88     ))]
     89     expire: TimeT,
     90     /// Internal fields.
     91     #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "macos"))]
     92     fields: c_int,
     93 }
     94 impl Passwd {
     95     /// Returns `UserInfo` based on the contained user and group IDs.
     96     pub const fn into_user_info(self) -> UserInfo {
     97         UserInfo {
     98             uid: Uid(self.uid),
     99             gid: Gid(self.gid),
    100         }
    101     }
    102 }
    103 #[expect(unsafe_code, reason = "FFI requires unsafe")]
    104 unsafe extern "C" {
    105     /// [`getuid`](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/getuid.html).
    106     pub safe fn getuid() -> IdT;
    107     /// [`geteuid`](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/geteuid.html).
    108     pub safe fn geteuid() -> IdT;
    109     /// [`getgid`](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/getgid.html).
    110     pub safe fn getgid() -> IdT;
    111     /// [`getegid`](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/getegid.html).
    112     pub safe fn getegid() -> IdT;
    113     /// [`setresuid`](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/setresuid.html).
    114     pub safe fn setresuid(ruid: IdT, euid: IdT, suid: IdT) -> c_int;
    115     /// [`setresgid`](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/setresgid.html).
    116     pub safe fn setresgid(rgid: IdT, egid: IdT, sgid: IdT) -> c_int;
    117     /// [`chroot(2)`](https://manned.org/chroot.2).
    118     pub fn chroot(path: *const c_char) -> c_int;
    119     /// [`chdir`](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/chdir.html).
    120     pub fn chdir(path: *const c_char) -> c_int;
    121     /// [`getpwnam_r`](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/getpwnam_r.html).
    122     pub fn getpwnam_r(
    123         name: *const c_char,
    124         pwd: *mut Passwd,
    125         buf: *mut c_char,
    126         size: usize,
    127         result: *mut *mut Passwd,
    128     ) -> c_int;
    129     /// [`getpwuid_r`](https://pubs.opengroup.org/onlinepubs/9799919799.2024edition/functions/getpwuid_r.html).
    130     pub fn getpwuid_r(
    131         uid: IdT,
    132         pwd: *mut Passwd,
    133         buf: *mut c_char,
    134         size: usize,
    135         result: *mut *mut Passwd,
    136     ) -> c_int;
    137 }