1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 20:47:46 +00:00

chroot: use uucore::entries

This commit is contained in:
Knight 2016-08-02 14:25:19 +08:00
parent 270290efe6
commit 2b96f8f0c1
2 changed files with 15 additions and 26 deletions

View file

@ -9,8 +9,11 @@ path = "chroot.rs"
[dependencies] [dependencies]
getopts = "*" getopts = "*"
libc = "*"
uucore = { path="../uucore" } [dependencies.uucore]
path = "../uucore"
default-features = false
features = ["entries"]
[[bin]] [[bin]]
name = "chroot" name = "chroot"

View file

@ -4,39 +4,25 @@
* This file is part of the uutils coreutils package. * This file is part of the uutils coreutils package.
* *
* (c) Vsevolod Velichko <torkvemada@sorokdva.net> * (c) Vsevolod Velichko <torkvemada@sorokdva.net>
* (c) Jian Zeng <anonymousknight96 AT gmail.com>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
extern crate getopts; extern crate getopts;
extern crate libc;
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
use uucore::libc::{self, setgid, setuid, chroot, setgroups};
use uucore::entries;
use getopts::Options; use getopts::Options;
use libc::{setgid, setuid};
use std::ffi::CString; use std::ffi::CString;
use std::io::{Error, Write}; use std::io::{Error, Write};
use std::iter::FromIterator; use std::iter::FromIterator;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
use uucore::c_types::{get_pw_from_args, get_group};
extern {
fn chroot(path: *const libc::c_char) -> libc::c_int;
}
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
extern {
fn setgroups(size: libc::c_int, list: *const libc::gid_t) -> libc::c_int;
}
#[cfg(target_os = "linux")]
extern {
fn setgroups(size: libc::size_t, list: *const libc::gid_t) -> libc::c_int;
}
static NAME: &'static str = "chroot"; static NAME: &'static str = "chroot";
static VERSION: &'static str = env!("CARGO_PKG_VERSION"); static VERSION: &'static str = env!("CARGO_PKG_VERSION");
@ -146,9 +132,9 @@ fn enter_chroot(root: &Path) {
fn set_main_group(group: &str) { fn set_main_group(group: &str) {
if !group.is_empty() { if !group.is_empty() {
let group_id = match get_group(group) { let group_id = match entries::grp2gid(group) {
None => crash!(1, "no such group: {}", group), Ok(g) => g,
Some(g) => g.gr_gid _ => crash!(1, "no such group: {}", group),
}; };
let err = unsafe { setgid(group_id) }; let err = unsafe { setgid(group_id) };
if err != 0 { if err != 0 {
@ -177,9 +163,9 @@ fn set_groups_from_str(groups: &str) {
if !groups.is_empty() { if !groups.is_empty() {
let groups_vec: Vec<libc::gid_t> = FromIterator::from_iter( let groups_vec: Vec<libc::gid_t> = FromIterator::from_iter(
groups.split(',').map( groups.split(',').map(
|x| match get_group(x) { |x| match entries::grp2gid(x) {
None => crash!(1, "no such group: {}", x), Ok(g) => g,
Some(g) => g.gr_gid _ => crash!(1, "no such group: {}", x),
}) })
); );
let err = set_groups(groups_vec); let err = set_groups(groups_vec);
@ -191,7 +177,7 @@ fn set_groups_from_str(groups: &str) {
fn set_user(user: &str) { fn set_user(user: &str) {
if !user.is_empty() { if !user.is_empty() {
let user_id = get_pw_from_args(&vec!(user.to_owned())).unwrap().pw_uid; let user_id = entries::usr2uid(user).unwrap();
let err = unsafe { setuid(user_id as libc::uid_t) }; let err = unsafe { setuid(user_id as libc::uid_t) };
if err != 0 { if err != 0 {
crash!(1, "cannot set user to {}: {}", user, Error::last_os_error()) crash!(1, "cannot set user to {}: {}", user, Error::last_os_error())