2014-07-06 08:13:36 +00:00
|
|
|
#![crate_name = "chroot"]
|
2014-06-16 15:37:03 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
2014-06-16 22:08:55 +00:00
|
|
|
* (c) Vsevolod Velichko <torkvemada@sorokdva.net>
|
2014-06-16 15:37:03 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern crate getopts;
|
|
|
|
extern crate libc;
|
|
|
|
|
|
|
|
use getopts::{optflag, optopt, getopts, usage};
|
|
|
|
use c_types::{get_pw_from_args, get_group};
|
|
|
|
use libc::funcs::posix88::unistd::{execvp, setuid, setgid};
|
2015-01-08 20:59:07 +00:00
|
|
|
use std::ffi::c_str_to_bytes;
|
2014-09-17 01:50:38 +00:00
|
|
|
use std::io::fs::PathExtensions;
|
2015-01-08 20:59:07 +00:00
|
|
|
use std::iter::FromIterator;
|
2014-06-16 15:37:03 +00:00
|
|
|
|
2015-01-08 12:54:22 +00:00
|
|
|
#[path = "../common/util.rs"] #[macro_use] mod util;
|
2014-06-16 15:37:03 +00:00
|
|
|
#[path = "../common/c_types.rs"] mod c_types;
|
|
|
|
|
|
|
|
extern {
|
2014-06-30 11:13:38 +00:00
|
|
|
fn chroot(path: *const libc::c_char) -> libc::c_int;
|
2014-06-17 00:18:12 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 17:55:06 +00:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
|
2014-06-17 00:18:12 +00:00
|
|
|
extern {
|
2014-06-30 11:13:38 +00:00
|
|
|
fn setgroups(size: libc::c_int, list: *const libc::gid_t) -> libc::c_int;
|
2014-06-16 15:37:03 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 00:18:12 +00:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
extern {
|
2014-06-30 11:13:38 +00:00
|
|
|
fn setgroups(size: libc::size_t, list: *const libc::gid_t) -> libc::c_int;
|
2014-06-17 00:18:12 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 22:03:23 +00:00
|
|
|
static NAME: &'static str = "chroot";
|
|
|
|
static VERSION: &'static str = "1.0.0";
|
2014-06-16 15:37:03 +00:00
|
|
|
|
|
|
|
pub fn uumain(args: Vec<String>) -> int {
|
2014-07-20 01:13:55 +00:00
|
|
|
let program = &args[0];
|
2014-06-16 15:37:03 +00:00
|
|
|
|
|
|
|
let options = [
|
|
|
|
optopt("u", "user", "User (ID or name) to switch before running the program", "USER"),
|
|
|
|
optopt("g", "group", "Group (ID or name) to switch to", "GROUP"),
|
|
|
|
optopt("G", "groups", "Comma-separated list of groups to switch to", "GROUP1,GROUP2…"),
|
|
|
|
optopt("", "userspec", "Colon-separated user and group to switch to. \
|
|
|
|
Same as -u USER -g GROUP. \
|
|
|
|
Userspec has higher preference than -u and/or -g", "USER:GROUP"),
|
|
|
|
optflag("h", "help", "Show help"),
|
|
|
|
optflag("V", "version", "Show program's version")
|
|
|
|
];
|
|
|
|
|
2014-11-19 20:55:25 +00:00
|
|
|
let opts = match getopts(args.tail(), &options) {
|
2014-06-16 22:28:40 +00:00
|
|
|
Ok(m) => m,
|
2014-06-16 15:37:03 +00:00
|
|
|
Err(f) => {
|
2014-06-16 22:28:40 +00:00
|
|
|
show_error!("{}", f);
|
2014-11-19 20:55:25 +00:00
|
|
|
help_menu(program.as_slice(), &options);
|
2014-06-16 15:37:03 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if opts.opt_present("V") { version(); return 0 }
|
2014-11-19 20:55:25 +00:00
|
|
|
if opts.opt_present("h") { help_menu(program.as_slice(), &options); return 0 }
|
2014-06-16 15:37:03 +00:00
|
|
|
|
|
|
|
if opts.free.len() == 0 {
|
|
|
|
println!("Missing operand: NEWROOT");
|
2014-11-21 09:09:43 +00:00
|
|
|
println!("Try `{} --help` for more information.", program.as_slice());
|
2014-06-16 15:37:03 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-09-02 07:38:29 +00:00
|
|
|
let default_shell: &'static str = "/bin/sh";
|
|
|
|
let default_option: &'static str = "-i";
|
|
|
|
let user_shell = std::os::getenv("SHELL");
|
2014-06-16 15:37:03 +00:00
|
|
|
|
2014-07-20 01:13:55 +00:00
|
|
|
let newroot = Path::new(opts.free[0].as_slice());
|
2014-06-16 15:37:03 +00:00
|
|
|
if !newroot.is_dir() {
|
|
|
|
crash!(1, "cannot change root directory to `{}`: no such directory", newroot.display());
|
|
|
|
}
|
|
|
|
|
2014-06-16 22:03:23 +00:00
|
|
|
let command: Vec<&str> = match opts.free.len() {
|
2014-06-16 15:37:03 +00:00
|
|
|
1 => {
|
2014-09-02 07:38:29 +00:00
|
|
|
let shell: &str = match user_shell {
|
|
|
|
None => default_shell,
|
2014-06-16 22:55:39 +00:00
|
|
|
Some(ref s) => s.as_slice()
|
2014-06-16 15:37:03 +00:00
|
|
|
};
|
2014-09-02 07:38:29 +00:00
|
|
|
vec!(shell, default_option)
|
2014-06-16 15:37:03 +00:00
|
|
|
}
|
2014-06-16 22:28:40 +00:00
|
|
|
_ => opts.free.slice(1, opts.free.len()).iter().map(|x| x.as_slice()).collect()
|
2014-06-16 15:37:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
set_context(&newroot, &opts);
|
|
|
|
|
|
|
|
unsafe {
|
2014-11-28 22:49:22 +00:00
|
|
|
let executable = command[0].as_slice().to_c_str().into_inner();
|
|
|
|
let mut command_parts: Vec<*const i8> = command.iter().map(|x| x.to_c_str().into_inner()).collect();
|
2014-09-02 07:38:29 +00:00
|
|
|
command_parts.push(std::ptr::null());
|
|
|
|
execvp(executable as *const libc::c_char, command_parts.as_ptr() as *mut *const libc::c_char) as int
|
2014-06-16 15:37:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_context(root: &Path, options: &getopts::Matches) {
|
2014-09-02 07:38:29 +00:00
|
|
|
let userspec_str = options.opt_str("userspec");
|
|
|
|
let user_str = options.opt_str("user").unwrap_or_default();
|
|
|
|
let group_str = options.opt_str("group").unwrap_or_default();
|
|
|
|
let groups_str = options.opt_str("groups").unwrap_or_default();
|
|
|
|
let userspec = match userspec_str {
|
2014-06-16 15:37:03 +00:00
|
|
|
Some(ref u) => {
|
2014-06-16 22:03:23 +00:00
|
|
|
let s: Vec<&str> = u.as_slice().split(':').collect();
|
2014-06-16 15:37:03 +00:00
|
|
|
if s.len() != 2 {
|
2014-11-21 09:09:43 +00:00
|
|
|
crash!(1, "invalid userspec: `{}`", u.as_slice())
|
2014-06-16 15:37:03 +00:00
|
|
|
};
|
|
|
|
s
|
|
|
|
}
|
2014-06-16 22:28:40 +00:00
|
|
|
None => Vec::new()
|
2014-06-16 15:37:03 +00:00
|
|
|
};
|
2014-09-02 07:38:29 +00:00
|
|
|
let user = if userspec.is_empty() { user_str.as_slice() } else { userspec[0].as_slice() };
|
|
|
|
let group = if userspec.is_empty() { group_str.as_slice() } else { userspec[1].as_slice() };
|
2014-06-16 15:37:03 +00:00
|
|
|
|
|
|
|
enter_chroot(root);
|
|
|
|
|
2014-09-02 07:38:29 +00:00
|
|
|
set_groups_from_str(groups_str.as_slice());
|
2014-06-16 15:37:03 +00:00
|
|
|
set_main_group(group);
|
|
|
|
set_user(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enter_chroot(root: &Path) {
|
2014-09-02 07:38:29 +00:00
|
|
|
let root_str = root.display();
|
2014-11-19 20:56:52 +00:00
|
|
|
std::os::change_dir(root).unwrap();
|
2014-06-16 15:37:03 +00:00
|
|
|
let err = unsafe {
|
2014-11-28 22:49:22 +00:00
|
|
|
chroot(".".to_c_str().into_inner() as *const libc::c_char)
|
2014-06-16 15:37:03 +00:00
|
|
|
};
|
|
|
|
if err != 0 {
|
2014-11-21 09:09:43 +00:00
|
|
|
crash!(1, "cannot chroot to {}: {}", root_str, strerror(err).as_slice())
|
2014-06-16 15:37:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_main_group(group: &str) {
|
|
|
|
if !group.is_empty() {
|
|
|
|
let group_id = match get_group(group) {
|
2014-06-16 22:28:40 +00:00
|
|
|
None => crash!(1, "no such group: {}", group),
|
2014-06-16 15:37:03 +00:00
|
|
|
Some(g) => g.gr_gid
|
|
|
|
};
|
|
|
|
let err = unsafe { setgid(group_id) };
|
|
|
|
if err != 0 {
|
2014-11-21 09:09:43 +00:00
|
|
|
crash!(1, "cannot set gid to {}: {}", group_id, strerror(err).as_slice())
|
2014-06-16 15:37:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 17:55:06 +00:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
|
2014-06-17 00:18:12 +00:00
|
|
|
fn set_groups(groups: Vec<libc::gid_t>) -> libc::c_int {
|
|
|
|
unsafe {
|
|
|
|
setgroups(groups.len() as libc::c_int,
|
|
|
|
groups.as_slice().as_ptr())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
fn set_groups(groups: Vec<libc::gid_t>) -> libc::c_int {
|
|
|
|
unsafe {
|
|
|
|
setgroups(groups.len() as libc::size_t,
|
|
|
|
groups.as_slice().as_ptr())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_groups_from_str(groups: &str) {
|
2014-06-16 15:37:03 +00:00
|
|
|
if !groups.is_empty() {
|
2014-09-02 07:38:29 +00:00
|
|
|
let groups_vec: Vec<libc::gid_t> = FromIterator::from_iter(
|
2014-06-16 15:37:03 +00:00
|
|
|
groups.split(',').map(
|
|
|
|
|x| match get_group(x) {
|
2014-06-16 22:28:40 +00:00
|
|
|
None => crash!(1, "no such group: {}", x),
|
|
|
|
Some(g) => g.gr_gid
|
2014-06-16 15:37:03 +00:00
|
|
|
})
|
|
|
|
);
|
2014-09-02 07:38:29 +00:00
|
|
|
let err = set_groups(groups_vec);
|
2014-06-16 15:37:03 +00:00
|
|
|
if err != 0 {
|
2014-11-21 09:09:43 +00:00
|
|
|
crash!(1, "cannot set groups: {}", strerror(err).as_slice())
|
2014-06-16 15:37:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_user(user: &str) {
|
|
|
|
if !user.is_empty() {
|
|
|
|
let user_id = get_pw_from_args(&vec!(String::from_str(user))).unwrap().pw_uid;
|
|
|
|
let err = unsafe { setuid(user_id as libc::uid_t) };
|
|
|
|
if err != 0 {
|
2014-11-21 09:09:43 +00:00
|
|
|
crash!(1, "cannot set user to {}: {}", user, strerror(err).as_slice())
|
2014-06-16 15:37:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn strerror(errno: i32) -> String {
|
|
|
|
unsafe {
|
2015-01-08 20:59:07 +00:00
|
|
|
let err = libc::funcs::c95::string::strerror(errno) as *const libc::c_char;
|
|
|
|
let bytes= c_str_to_bytes(&err);
|
|
|
|
String::from_utf8_lossy(bytes).to_string()
|
2014-06-16 15:37:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn version() {
|
|
|
|
println!("{} v{}", NAME, VERSION)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn help_menu(program: &str, options: &[getopts::OptGroup]) {
|
|
|
|
version();
|
|
|
|
println!("Usage:");
|
2014-11-21 09:09:43 +00:00
|
|
|
println!(" {} [OPTION]… NEWROOT [COMMAND [ARG]…]", program);
|
2014-06-16 15:37:03 +00:00
|
|
|
println!("");
|
2014-11-21 09:09:43 +00:00
|
|
|
print!("{}", usage(
|
2014-06-16 15:37:03 +00:00
|
|
|
"Run COMMAND with root directory set to NEWROOT.\n\
|
|
|
|
If COMMAND is not specified, it defaults to '${SHELL} -i'. \
|
|
|
|
If ${SHELL} is not set, /bin/sh is used.", options))
|
|
|
|
}
|