priv_sep

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

c.rs (5875B)


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