Mercurial > hg > Members > anatofuz > ie-virsh
view src/user.rs @ 41:63e77a9de3ab
...
author | AnaTofuZ <anatofuz@gmail.com> |
---|---|
date | Sun, 22 Nov 2020 16:48:05 +0900 |
parents | afec42bdd5ab |
children |
line wrap: on
line source
pub struct User { pub uid: u32, pub gid: u32, pub name: String, } pub struct UserDetail<'a> { pub user: &'a User, pub year: String, pub affilication: String, } impl<'a> UserDetail<'a> { pub fn new(user: &'a User) -> UserDetail { // e195730 -> 19 let year = user.name.chars().skip(1).take(2).collect::<String>(); // e195730 -> e19, tnal -> teacher let affilication = if year.parse::<u8>().is_ok() { user.name.chars().take(3).collect::<String>() } else { "teacher".to_string() }; UserDetail { user, year, affilication, } } pub fn getpass(self) -> String { format!("{}/{}", self.affilication, &self.user.name) } } pub fn getlogin(uid: u32) -> String { use std::ffi::CStr; let user_passwd = unsafe { nix::libc::getpwuid(uid) }; let c_str = unsafe { CStr::from_ptr((*user_passwd).pw_name) }; c_str.to_str().unwrap().to_string() } pub fn getuid() -> u32 { let uid_struct = nix::unistd::getuid(); uid_struct.into() } pub fn getgid() -> u32 { let gid_struct = nix::unistd::getgid(); gid_struct.into() } const ROOT_ID: u32 = 0; pub fn set_root_id() { if let Err(err) = nix::unistd::seteuid(nix::unistd::Uid::from_raw(ROOT_ID)) { panic!("failed seteuid {}", err) } if let Err(err) = nix::unistd::setegid(nix::unistd::Gid::from_raw(ROOT_ID)) { panic!("failed setedid{}", err) } if let Err(err) = nix::unistd::setuid(nix::unistd::Uid::from_raw(ROOT_ID)) { panic!("failed setuid {}", err) } if let Err(err) = nix::unistd::setgid(nix::unistd::Gid::from_raw(ROOT_ID)) { panic!("failed setdid{}", err) } }