From d66679ccf893570517011e286e8f92cb246905b2 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Tue, 17 Jun 2014 12:18:17 +0200 Subject: [PATCH 1/7] c_types: rework group() Fixes #236 --- common/c_types.rs | 89 +++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 37 deletions(-) diff --git a/common/c_types.rs b/common/c_types.rs index e4a891722..a0f9c735d 100644 --- a/common/c_types.rs +++ b/common/c_types.rs @@ -13,7 +13,7 @@ use self::libc::funcs::posix88::unistd::getgroups; use std::vec::Vec; -use std::io::IoError; +use std::os; use std::ptr::read; use std::str::raw::from_c_str; @@ -85,7 +85,7 @@ extern { pub fn getpwnam(login: *c_char) -> *c_passwd; pub fn getgrouplist(name: *c_char, basegid: gid_t, - groups: *gid_t, + groups: *mut gid_t, ngroups: *mut c_int) -> c_int; pub fn getgrgid(gid: gid_t) -> *c_group; pub fn getgrnam(name: *c_char) -> *c_group; @@ -137,46 +137,61 @@ pub fn get_group(groupname: &str) -> Option { } } -static NGROUPS: i32 = 20; +fn get_group_list(name: *c_char, gid: gid_t) -> Result, int> { + let mut ngroups = 0 as c_int; + + unsafe { getgrouplist(name, gid, 0 as *mut gid_t, &mut ngroups) }; + let mut groups = Vec::from_elem(ngroups as uint, 0 as gid_t); + let err = unsafe { getgrouplist(name, gid, groups.as_mut_ptr(), &mut ngroups) }; + if err == -1 { + Err(os::errno()) + } else { + groups.truncate(ngroups as uint); + Ok(groups) + } +} + +fn get_groups() -> Result, int> { + let ngroups = unsafe { getgroups(0, 0 as *mut gid_t) }; + if ngroups == -1 { + return Err(os::errno()); + } + + let mut groups = Vec::from_elem(ngroups as uint, 0 as gid_t); + let ngroups = unsafe { getgroups(ngroups, groups.as_mut_ptr()) }; + if ngroups == -1 { + Err(os::errno()) + } else { + groups.truncate(ngroups as uint); + Ok(groups) + } +} pub fn group(possible_pw: Option, nflag: bool) { - let mut groups = Vec::with_capacity(NGROUPS as uint); - let mut ngroups; - if possible_pw.is_some() { - ngroups = NGROUPS; - unsafe { - getgrouplist( - possible_pw.unwrap().pw_name, - possible_pw.unwrap().pw_gid, - groups.as_ptr(), - &mut ngroups); - } - } else { - ngroups = unsafe { - getgroups(NGROUPS, groups.as_mut_ptr() as *mut gid_t) - }; - } + let groups = match possible_pw { + Some(pw) => get_group_list(pw.pw_name, pw.pw_gid), + None => get_groups(), + }; - if ngroups < 0 { - crash!(1, "{}", IoError::last_error()); - } - - unsafe { groups.set_len(ngroups as uint) }; - - for &g in groups.iter() { - if nflag { - let group = unsafe { getgrgid(g) }; - if group.is_not_null() { - let name = unsafe { - from_c_str(read(group).gr_name) - }; - print!("{:s} ", name); + match groups { + Err(errno) => + crash!(1, "failed to get group list (errno={:d})", errno), + Ok(groups) => { + for &g in groups.iter() { + if nflag { + let group = unsafe { getgrgid(g) }; + if group.is_not_null() { + let name = unsafe { + from_c_str(read(group).gr_name) + }; + print!("{:s} ", name); + } + } else { + print!("{:u} ", g); + } } - } else { - print!("{:u} ", g); + println!(""); } } - - println!(""); } From 65c224fbf8a1cbe51d6e2b88745a269c621e3a66 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Tue, 17 Jun 2014 14:28:19 +0200 Subject: [PATCH 2/7] id: remove unused arg --- id/id.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/id/id.rs b/id/id.rs index 0174e7c15..26cb888ae 100644 --- a/id/id.rs +++ b/id/id.rs @@ -191,9 +191,9 @@ pub fn uumain(args: Vec) -> int { } if possible_pw.is_some() { - id_print(possible_pw, true, false, false) + id_print(possible_pw, false, false) } else { - id_print(possible_pw, false, true, true) + id_print(possible_pw, true, true) } 0 @@ -333,7 +333,6 @@ fn auditid() { } fn id_print(possible_pw: Option, - use_ggl: bool, p_euid: bool, p_egid: bool) { @@ -351,7 +350,7 @@ fn id_print(possible_pw: Option, let mut ngroups; let mut groups = Vec::with_capacity(NGROUPS as uint); - if use_ggl && possible_pw.is_some() { + if possible_pw.is_some() { ngroups = NGROUPS; let pw_name = possible_pw.unwrap().pw_name; From c80fdc1a73c625e273f5df76ea9405f8aa3b9e38 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Tue, 17 Jun 2014 14:32:31 +0200 Subject: [PATCH 3/7] c_types: make get_groups/get_group_list public --- common/c_types.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/c_types.rs b/common/c_types.rs index a0f9c735d..5a9e4bb37 100644 --- a/common/c_types.rs +++ b/common/c_types.rs @@ -137,7 +137,7 @@ pub fn get_group(groupname: &str) -> Option { } } -fn get_group_list(name: *c_char, gid: gid_t) -> Result, int> { +pub fn get_group_list(name: *c_char, gid: gid_t) -> Result, int> { let mut ngroups = 0 as c_int; unsafe { getgrouplist(name, gid, 0 as *mut gid_t, &mut ngroups) }; @@ -151,7 +151,7 @@ fn get_group_list(name: *c_char, gid: gid_t) -> Result, int> { } } -fn get_groups() -> Result, int> { +pub fn get_groups() -> Result, int> { let ngroups = unsafe { getgroups(0, 0 as *mut gid_t) }; if ngroups == -1 { return Err(os::errno()); From 2b2bdc89fff5ce584ced1bcc20835a08f3204902 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Tue, 17 Jun 2014 14:45:10 +0200 Subject: [PATCH 4/7] id: rework id_print() Fixes #239 --- id/id.rs | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/id/id.rs b/id/id.rs index 26cb888ae..f7cad4328 100644 --- a/id/id.rs +++ b/id/id.rs @@ -34,6 +34,8 @@ use getopts::{getopts, optflag, usage}; use c_types::{ c_passwd, c_group, + get_groups, + get_group_list, get_pw_from_args, getpwuid, group @@ -80,10 +82,6 @@ mod audit { extern { fn getgrgid(gid: uid_t) -> *c_group; - fn getgrouplist(name: *c_char, - basegid: gid_t, - groups: *gid_t, - ngroups: *mut c_int) -> c_int; } static NAME: &'static str = "id"; @@ -347,19 +345,14 @@ fn id_print(possible_pw: Option, gid = unsafe { getgid() }; } - let mut ngroups; - let mut groups = Vec::with_capacity(NGROUPS as uint); + let groups = match possible_pw { + Some(pw) => get_group_list(pw.pw_name, pw.pw_gid), + None => get_groups(), + }; - if possible_pw.is_some() { - ngroups = NGROUPS; - let pw_name = possible_pw.unwrap().pw_name; - - unsafe { getgrouplist(pw_name, gid, groups.as_ptr(), &mut ngroups) }; - } else { - ngroups = unsafe { - getgroups(NGROUPS, groups.as_mut_ptr() as *mut u32) - }; - } + let groups = groups.unwrap_or_else(|errno| { + crash!(1, "failed to get group list (errno={:d})", errno); + }); if possible_pw.is_some() { print!( @@ -400,16 +393,14 @@ fn id_print(possible_pw: Option, } } - unsafe { groups.set_len(ngroups as uint) }; - - if ngroups > 0 { + if groups.len() > 0 { print!(" groups="); let mut first = true; for &gr in groups.iter() { if !first { print!(",") } print!("{:u}", gr); - let group = unsafe { getgrgid(gr as u32) }; + let group = unsafe { getgrgid(gr) }; if group.is_not_null() { let name = unsafe { from_c_str(read(group).gr_name) @@ -418,7 +409,7 @@ fn id_print(possible_pw: Option, } first = false } - - println!(""); } + + println!(""); } From e0d93471823aba7c4da11a25ad3da638e336b0bb Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Tue, 17 Jun 2014 14:46:02 +0200 Subject: [PATCH 5/7] id: remove unnecessary coercion --- id/id.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/id/id.rs b/id/id.rs index f7cad4328..77fd28d0a 100644 --- a/id/id.rs +++ b/id/id.rs @@ -383,7 +383,7 @@ fn id_print(possible_pw: Option, } let egid = unsafe { getegid() }; - if p_egid && (egid != gid as u32) { + if p_egid && (egid != gid) { print!(" egid={:u}", egid); unsafe { let grp = getgrgid(egid); From aa5293ecfa46d5738af5ea2e35dbeb514cb8beed Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Tue, 17 Jun 2014 14:47:40 +0200 Subject: [PATCH 6/7] id: remove dead code/unused imports --- id/id.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/id/id.rs b/id/id.rs index 77fd28d0a..6bc422d96 100644 --- a/id/id.rs +++ b/id/id.rs @@ -21,14 +21,12 @@ extern crate libc; use std::os; use std::ptr::read; use libc::{ - c_char, c_int, - gid_t, uid_t, getgid, getuid }; -use libc::funcs::posix88::unistd::{getegid, geteuid, getgroups, getlogin}; +use libc::funcs::posix88::unistd::{getegid, geteuid, getlogin}; use std::str::raw::from_c_str; use getopts::{getopts, optflag, usage}; use c_types::{ @@ -309,8 +307,6 @@ fn pline(possible_pw: Option) { pw_shell); } -static NGROUPS: i32 = 20; - #[cfg(target_os = "linux")] fn auditid() { } From 48892c5063e9d28d3f1ce62e2d079eaa6a7aa73e Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Tue, 17 Jun 2014 14:48:37 +0200 Subject: [PATCH 7/7] id: fix typo --- id/id.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/id/id.rs b/id/id.rs index 6bc422d96..c1610c15f 100644 --- a/id/id.rs +++ b/id/id.rs @@ -315,7 +315,7 @@ fn auditid() { let auditinfo: audit::c_auditinfo_addr_t = unsafe { audit::uninitialized() }; let address = &auditinfo as *audit::c_auditinfo_addr_t; if unsafe { audit::getaudit(address) } < 0 { - println!("Couldlnt retrieve information"); + println!("couldn't retrieve information"); return; }