1
|
1 use nix;
|
3
|
2 use std::process::Command;
|
|
3 //use libc;
|
1
|
4
|
0
|
5 fn main() {
|
3
|
6 let uid = getuid();
|
|
7 let gid = getgid();
|
|
8 let user_name = getlogin(uid);
|
|
9 println!("uid: {} gid: {} name: {}", uid, gid, user_name);
|
|
10 set_root_id();
|
|
11
|
|
12 list_command();
|
0
|
13 }
|
1
|
14
|
3
|
15
|
2
|
16 fn getlogin(uid: u32) -> &'static str {
|
|
17 use std::ffi::CStr;
|
1
|
18 let user_passwd = unsafe { nix::libc::getpwuid(uid)};
|
|
19 let c_str = unsafe { CStr::from_ptr((*user_passwd).pw_name)} ;
|
|
20 return c_str.to_str().unwrap();
|
|
21 }
|
|
22
|
|
23 fn getuid() -> u32 {
|
|
24 let uid_struct = nix::unistd::getuid();
|
3
|
25 return uid_struct.into();
|
1
|
26 }
|
|
27
|
|
28 fn getgid() -> u32 {
|
|
29 let gid_struct = nix::unistd::getgid();
|
3
|
30 return gid_struct.into();
|
|
31 }
|
|
32
|
|
33 fn list_command() {
|
|
34 Command::new("virsh").arg("list").arg("--all").status().expect("failed to virsh");
|
1
|
35 }
|
3
|
36
|
|
37
|
|
38 fn set_root_id(){
|
|
39 let id = 0;
|
|
40 match nix::unistd::seteuid(nix::unistd::Uid::from_raw(id)) {
|
|
41 Err(err) => panic!("failed seteuid {}", err),
|
|
42 Ok(_) => {}
|
|
43 }
|
|
44 match nix::unistd::setegid(nix::unistd::Gid::from_raw(id)) {
|
|
45 Err(err) => panic!("failed setedid{}", err),
|
|
46 Ok(_) => {}
|
|
47 }
|
|
48
|
|
49 match nix::unistd::setuid(nix::unistd::Uid::from_raw(id)) {
|
|
50 Err(err) => panic!("failed setuid {}", err),
|
|
51 Ok(_) => {}
|
|
52 }
|
|
53
|
|
54 match nix::unistd::setgid(nix::unistd::Gid::from_raw(id)) {
|
|
55 Err(err) => panic!("failed setdid{}", err),
|
|
56 Ok(_) => {}
|
|
57 }
|
|
58 } |