view src/user.rs @ 12:0df4d067badb

move each crates
author AnaTofuZ <k198584@ie.u-ryukyu.ac.jp>
date Thu, 29 Oct 2020 16:15:52 +0900
parents
children e6dc51066703
line wrap: on
line source

use nix;

pub fn getlogin(uid: u32) -> &'static str {
    use std::ffi::CStr;
    let user_passwd = unsafe { nix::libc::getpwuid(uid) };
    let c_str = unsafe { CStr::from_ptr((*user_passwd).pw_name) };
    return c_str.to_str().unwrap();
}

pub fn getuid() -> u32 {
    let uid_struct = nix::unistd::getuid();
    return uid_struct.into();
}

pub fn getgid() -> u32 {
    let gid_struct = nix::unistd::getgid();
    return gid_struct.into();
}

pub fn set_root_id() {
    let id = 0;
    match nix::unistd::seteuid(nix::unistd::Uid::from_raw(id)) {
        Err(err) => panic!("failed seteuid {}", err),
        Ok(_) => {}
    }
    match nix::unistd::setegid(nix::unistd::Gid::from_raw(id)) {
        Err(err) => panic!("failed setedid{}", err),
        Ok(_) => {}
    }

    match nix::unistd::setuid(nix::unistd::Uid::from_raw(id)) {
        Err(err) => panic!("failed setuid {}", err),
        Ok(_) => {}
    }

    match nix::unistd::setgid(nix::unistd::Gid::from_raw(id)) {
        Err(err) => panic!("failed setdid{}", err),
        Ok(_) => {}
    }
}