diff --git a/Cargo.lock b/Cargo.lock index 1b0923d..940f247 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -160,7 +160,7 @@ dependencies = [ "http 1.0.0", "http-body 1.0.0", "http-body-util", - "hyper 1.1.0", + "hyper 1.2.0", "hyper-util", "itoa", "matchit", @@ -260,12 +260,9 @@ checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" [[package]] name = "cc" -version = "1.0.83" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" -dependencies = [ - "libc", -] +checksum = "7f9fa1897e4325be0d68d48df6aa1a71ac2ed4d27723887e7754192705350730" [[package]] name = "cfg-if" @@ -608,9 +605,9 @@ dependencies = [ [[package]] name = "hyper" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb5aa53871fc917b1a9ed87b683a5d86db645e23acb32c2e0785a353e522fb75" +checksum = "186548d73ac615b32a73aafe38fb4f56c0d340e110e5a200bcadbaf2e199263a" dependencies = [ "bytes", "futures-channel", @@ -622,6 +619,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite", + "smallvec", "tokio", ] @@ -647,7 +645,7 @@ dependencies = [ "futures-util", "http 1.0.0", "http-body 1.0.0", - "hyper 1.1.0", + "hyper 1.2.0", "pin-project-lite", "socket2", "tokio", @@ -1130,6 +1128,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "smallvec" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" + [[package]] name = "socket2" version = "0.5.5" diff --git a/crates/rsjudge-runner/examples/demo.rs b/crates/rsjudge-runner/examples/demo.rs index 63015dd..76e9b05 100644 --- a/crates/rsjudge-runner/examples/demo.rs +++ b/crates/rsjudge-runner/examples/demo.rs @@ -1,10 +1,17 @@ use std::process::Command; +use anyhow::anyhow; use rsjudge_runner::{ user::{builder, runner}, RunAs, }; +use uzers::{get_current_uid, get_user_by_uid}; fn main() -> anyhow::Result<()> { + let self_output = Command::new("id") + .run_as(&get_user_by_uid(get_current_uid()).ok_or(anyhow!("invalid user"))?) + .output()?; + println!("{}", String::from_utf8_lossy(&self_output.stdout)); + let builder_output = Command::new("id").run_as(builder()?).output()?; println!("{}", String::from_utf8_lossy(&builder_output.stdout)); diff --git a/crates/rsjudge-runner/src/lib.rs b/crates/rsjudge-runner/src/lib.rs index 35981c6..ac12c86 100644 --- a/crates/rsjudge-runner/src/lib.rs +++ b/crates/rsjudge-runner/src/lib.rs @@ -12,11 +12,17 @@ impl RunAs for Command { fn run_as(&mut self, user: &User) -> &mut Command { let uid = user.uid(); let gid = user.primary_group_id(); + let groups: Vec<_> = user + .groups() + .unwrap_or_default() + .into_iter() + .map(|g| g.gid().into()) + .collect(); self.uid(uid).gid(gid); unsafe { self.pre_exec(move || { - setgroups(&[gid.into()])?; + setgroups(&groups)?; Ok(()) }) }; diff --git a/crates/rsjudge-runner/src/user.rs b/crates/rsjudge-runner/src/user.rs index 3e7eb96..b512ff8 100644 --- a/crates/rsjudge-runner/src/user.rs +++ b/crates/rsjudge-runner/src/user.rs @@ -27,28 +27,3 @@ pub fn runner() -> anyhow::Result<&'static User> { .as_ref() .ok_or_else(|| anyhow!("No such user: rsjudge-runner")) } - -#[cfg(all(test, unix))] -mod tests { - use std::{os::unix::process::CommandExt, process::Command}; - - use super::*; - - #[test] - #[ignore = "Requires additional users."] - fn test_uid() -> anyhow::Result<()> { - let builder = builder()?; - let builder_output = Command::new("id") - .uid(builder.uid()) - .gid(builder.primary_group_id()) - .output()?; - println!("{}", String::from_utf8_lossy(&builder_output.stdout)); - let runner = runner()?; - let runner_output = Command::new("id") - .uid(runner.uid()) - .gid(runner.primary_group_id()) - .output()?; - println!("{}", String::from_utf8_lossy(&runner_output.stdout)); - Ok(()) - } -}