priv_sep

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

c.rs (5616B)


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