From b07f3752b6f72b57c05b000987b14302eefb195a Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Fri, 19 Feb 2016 17:37:53 +0300 Subject: [PATCH 1/2] unistd: add setuid, setgid syscalls --- src/unistd.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/unistd.rs b/src/unistd.rs index a4e666180c..1e3bdfb8fd 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -13,7 +13,7 @@ pub use self::linux::*; mod ffi { use libc::{c_char, c_int, size_t}; - pub use libc::{fork, close, read, write, pipe, ftruncate, unlink, setpgid, getegid, geteuid, getgid, getpid, getppid, getuid}; + pub use libc::{fork, close, read, write, pipe, ftruncate, unlink, setpgid, getegid, geteuid, getgid, getpid, getppid, getuid, setuid, setgid}; #[allow(improper_ctypes)] extern { @@ -369,6 +369,20 @@ pub fn getegid() -> gid_t { unsafe { ffi::getegid() } } +#[inline] +pub fn setuid(uid: uid_t) -> Result<()> { + let res = unsafe { ffi::setuid(uid) }; + + Errno::result(res).map(drop) +} + +#[inline] +pub fn setgid(gid: gid_t) -> Result<()> { + let res = unsafe { ffi::setgid(gid) }; + + Errno::result(res).map(drop) +} + #[cfg(any(target_os = "linux", target_os = "android"))] mod linux { use sys::syscall::{syscall, SYSPIVOTROOT}; From 6a2522cca37a921e39bdf451f46095ba3dfa4566 Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Fri, 19 Feb 2016 17:38:33 +0300 Subject: [PATCH 2/2] unistd: add chown syscall --- src/unistd.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/unistd.rs b/src/unistd.rs index 1e3bdfb8fd..6d793aec2d 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -13,7 +13,7 @@ pub use self::linux::*; mod ffi { use libc::{c_char, c_int, size_t}; - pub use libc::{fork, close, read, write, pipe, ftruncate, unlink, setpgid, getegid, geteuid, getgid, getpid, getppid, getuid, setuid, setgid}; + pub use libc::{fork, close, read, write, pipe, ftruncate, unlink, setpgid, getegid, geteuid, getgid, getpid, getppid, getuid, setuid, setgid, chown}; #[allow(improper_ctypes)] extern { @@ -28,7 +28,7 @@ mod ffi { // Execute PATH with arguments ARGV and environment from `environ'. // doc: http://man7.org/linux/man-pages/man3/execv.3.html - pub fn execv (path: *const c_char, argv: *const *const c_char) -> c_int; + pub fn execv(path: *const c_char, argv: *const *const c_char) -> c_int; // execute program // doc: http://man7.org/linux/man-pages/man2/execve.2.html @@ -157,6 +157,16 @@ pub fn chdir(path: &P) -> Result<()> { Errno::result(res).map(drop) } +#[inline] +pub fn chown(path: &P, owner: Option, group: Option) -> Result<()> { + let res = try!(path.with_nix_path(|cstr| { + // We use `0 - 1` to get `-1 : {u,g}id_t` which is specified as the no-op value for chown(3). + unsafe { ffi::chown(cstr.as_ptr(), owner.unwrap_or(0 - 1), group.unwrap_or(0 - 1)) } + })); + + Errno::result(res).map(drop) +} + fn to_exec_array(args: &[CString]) -> Vec<*const c_char> { use std::ptr; use libc::c_char;