From b39cf184186c77e0601d0fc96cf9aaff49e0ab3c Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Mon, 16 Jun 2014 21:04:56 +0200 Subject: [PATCH 1/4] c_types: use libc::{uid_t,gid_t} where appropriate --- common/c_types.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/common/c_types.rs b/common/c_types.rs index 1846a296c..77d5fbf92 100644 --- a/common/c_types.rs +++ b/common/c_types.rs @@ -6,6 +6,7 @@ use self::libc::{ c_char, c_int, uid_t, + gid_t, }; #[cfg(target_os = "macos")] use self::libc::time_t; use self::libc::funcs::posix88::unistd::getgroups; @@ -19,8 +20,8 @@ use std::str::raw::from_c_str; pub struct c_passwd { pub pw_name: *c_char, /* user name */ pub pw_passwd: *c_char, /* user name */ - pub pw_uid: c_int, /* user uid */ - pub pw_gid: c_int, /* user gid */ + pub pw_uid: uid_t, /* user uid */ + pub pw_gid: gid_t, /* user gid */ pub pw_change: time_t, pub pw_class: *c_char, pub pw_gecos: *c_char, @@ -33,8 +34,8 @@ pub struct c_passwd { pub struct c_passwd { pub pw_name: *c_char, /* user name */ pub pw_passwd: *c_char, /* user name */ - pub pw_uid: c_int, /* user uid */ - pub pw_gid: c_int, /* user gid */ + pub pw_uid: uid_t, /* user uid */ + pub pw_gid: gid_t, /* user gid */ pub pw_gecos: *c_char, pub pw_dir: *c_char, pub pw_shell: *c_char, @@ -76,13 +77,13 @@ pub struct c_tm { } extern { - pub fn getpwuid(uid: c_int) -> *c_passwd; + pub fn getpwuid(uid: uid_t) -> *c_passwd; pub fn getpwnam(login: *c_char) -> *c_passwd; pub fn getgrouplist(name: *c_char, - basegid: c_int, - groups: *c_int, + basegid: gid_t, + groups: *gid_t, ngroups: *mut c_int) -> c_int; - pub fn getgrgid(gid: uid_t) -> *c_group; + pub fn getgrgid(gid: gid_t) -> *c_group; } pub fn get_pw_from_args(free: &Vec) -> Option { @@ -91,8 +92,8 @@ pub fn get_pw_from_args(free: &Vec) -> Option { // Passed user as id if username.chars().all(|c| c.is_digit()) { - let id = from_str::(username).unwrap(); - let pw_pointer = unsafe { getpwuid(id) }; + let id = from_str::(username).unwrap(); + let pw_pointer = unsafe { getpwuid(id as uid_t) }; if pw_pointer.is_not_null() { Some(unsafe { read(pw_pointer) }) @@ -133,7 +134,7 @@ pub fn group(possible_pw: Option, nflag: bool) { } } else { ngroups = unsafe { - getgroups(NGROUPS, groups.as_mut_ptr() as *mut u32) + getgroups(NGROUPS, groups.as_mut_ptr() as *mut gid_t) }; } @@ -142,7 +143,7 @@ pub fn group(possible_pw: Option, nflag: bool) { for &g in groups.iter() { if nflag { - let group = unsafe { getgrgid(g as u32) }; + let group = unsafe { getgrgid(g) }; if group.is_not_null() { let name = unsafe { from_c_str(read(group).gr_name) @@ -150,7 +151,7 @@ pub fn group(possible_pw: Option, nflag: bool) { print!("{:s} ", name); } } else { - print!("{:d} ", g); + print!("{:u} ", g); } } From 68c42a6a393ade998b6095e9e1561106d524c8b2 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Mon, 16 Jun 2014 22:09:04 +0200 Subject: [PATCH 2/4] id: use libc::{uid_t,gid_t} where appropriate --- id/id.rs | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/id/id.rs b/id/id.rs index 785f6d2ec..4bd97943a 100644 --- a/id/id.rs +++ b/id/id.rs @@ -23,6 +23,7 @@ use std::ptr::read; use libc::{ c_char, c_int, + gid_t, uid_t, getgid, getuid @@ -80,8 +81,8 @@ mod audit { extern { fn getgrgid(gid: uid_t) -> *c_group; fn getgrouplist(name: *c_char, - basegid: c_int, - groups: *c_int, + basegid: gid_t, + groups: *gid_t, ngroups: *mut c_int) -> c_int; } @@ -136,11 +137,11 @@ pub fn uumain(args: Vec) -> int { possible_pw.unwrap().pw_gid } else { if rflag { - unsafe { getgid() as i32 } + unsafe { getgid() } } else { - unsafe { getegid() as i32 } + unsafe { getegid() } } - } as u32; + }; let gr = unsafe { getgrgid(id) }; if nflag && gr.is_not_null() { @@ -156,9 +157,9 @@ pub fn uumain(args: Vec) -> int { let id = if possible_pw.is_some() { possible_pw.unwrap().pw_uid } else if rflag { - unsafe { getgid() as i32 } + unsafe { getgid() } } else { - unsafe { getegid() as i32 } + unsafe { getegid() } }; let pw = unsafe { getpwuid(id) }; @@ -168,7 +169,7 @@ pub fn uumain(args: Vec) -> int { }; println!("{:s}", pw_name); } else { - println!("{:d}", id); + println!("{:u}", id); } return 0; @@ -208,7 +209,7 @@ fn pretty(possible_pw: Option) { } else { let login = unsafe { from_c_str(getlogin()) }; let rid = unsafe { getuid() }; - let pw = unsafe { getpwuid(rid as i32) }; + let pw = unsafe { getpwuid(rid) }; let is_same_user = unsafe { from_c_str(read(pw).pw_name) == login @@ -228,7 +229,7 @@ fn pretty(possible_pw: Option) { let eid = unsafe { getegid() }; if eid == rid { - let pw = unsafe { getpwuid(eid as i32) }; + let pw = unsafe { getpwuid(eid) }; if pw.is_not_null() { println!( "euid\t{:s}", @@ -288,7 +289,7 @@ fn pline(possible_pw: Option) { #[cfg(target_os = "linux")] fn pline(possible_pw: Option) { let pw = if possible_pw.is_none() { - unsafe { read(getpwuid(getuid() as i32)) } + unsafe { read(getpwuid(getuid())) } } else { possible_pw.unwrap() }; @@ -300,7 +301,7 @@ fn pline(possible_pw: Option) { let pw_shell = unsafe { from_c_str(pw.pw_shell) }; println!( - "{:s}:{:s}:{:d}:{:d}:{:s}:{:s}:{:s}", + "{:s}:{:s}:{:u}:{:u}:{:s}:{:s}:{:s}", pw_name, pw_passwd, pw.pw_uid, @@ -343,8 +344,8 @@ fn id_print(possible_pw: Option, uid = possible_pw.unwrap().pw_uid; gid = possible_pw.unwrap().pw_gid; } else { - uid = unsafe { getuid() as i32 }; - gid = unsafe { getgid() as i32 }; + uid = unsafe { getuid() }; + gid = unsafe { getgid() }; } let mut ngroups; @@ -363,15 +364,15 @@ fn id_print(possible_pw: Option, if possible_pw.is_some() { print!( - "uid={:d}({:s})", + "uid={:u}({:s})", uid, unsafe { from_c_str(possible_pw.unwrap().pw_name) }); } else { print!("uid={:u}", unsafe { getuid() }); } - print!(" gid={:d}", gid); - let gr = unsafe { getgrgid(gid as u32) }; + print!(" gid={:u}", gid); + let gr = unsafe { getgrgid(gid) }; if gr.is_not_null() { print!( "({:s})", @@ -379,9 +380,9 @@ fn id_print(possible_pw: Option, } let euid = unsafe { geteuid() }; - if p_euid && (euid != uid as u32) { + if p_euid && (euid != uid) { print!(" euid={:u}", euid); - let pw = unsafe { getpwuid(euid as i32) }; + let pw = unsafe { getpwuid(euid) }; if pw.is_not_null() { print!( "({:s})", @@ -408,7 +409,7 @@ fn id_print(possible_pw: Option, let mut first = true; for &gr in groups.iter() { if !first { print!(",") } - print!("{:d}", gr); + print!("{:u}", gr); let group = unsafe { getgrgid(gr as u32) }; if group.is_not_null() { let name = unsafe { From d24327b243db443522d82ed7259857830b7058bc Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Mon, 16 Jun 2014 22:12:36 +0200 Subject: [PATCH 3/4] whoami: use libc::uid_t where appropriate --- whoami/whoami.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/whoami/whoami.rs b/whoami/whoami.rs index cedb32778..4abe953e7 100644 --- a/whoami/whoami.rs +++ b/whoami/whoami.rs @@ -32,7 +32,7 @@ mod platform { #[path = "../../common/c_types.rs"] mod c_types; extern { - pub fn geteuid() -> libc::c_int; + pub fn geteuid() -> libc::uid_t; } pub unsafe fn getusername() -> String { From d22be169326f39961cd96bb3e4cb8ed6c2e2fbb8 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Mon, 16 Jun 2014 21:05:39 +0200 Subject: [PATCH 4/4] c_types: handle errors from getgroups()/getgrouplist() --- common/c_types.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/common/c_types.rs b/common/c_types.rs index 77d5fbf92..adbd06b10 100644 --- a/common/c_types.rs +++ b/common/c_types.rs @@ -13,6 +13,7 @@ use self::libc::funcs::posix88::unistd::getgroups; use std::vec::Vec; +use std::io::IoError; use std::ptr::read; use std::str::raw::from_c_str; @@ -138,6 +139,9 @@ pub fn group(possible_pw: Option, nflag: bool) { }; } + if ngroups < 0 { + crash!(1, "{}", IoError::last_error()); + } unsafe { groups.set_len(ngroups as uint) };