From ea083786be7009c233996082ac78adf934ce1890 Mon Sep 17 00:00:00 2001 From: "Marc R. Schoolderman" Date: Thu, 19 Dec 2024 16:59:04 +0100 Subject: [PATCH] rotate the requested group id to the front if it is not there already --- src/system/mod.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/system/mod.rs b/src/system/mod.rs index 199d78575..c67b8b403 100644 --- a/src/system/mod.rs +++ b/src/system/mod.rs @@ -276,8 +276,15 @@ pub fn set_target_user( ) { use std::os::unix::process::CommandExt; - // add target group to list of additional groups if not present - if !target_user.groups.contains(&target_group.gid) { + if let Some(index) = target_user + .groups + .iter() + .position(|id| id == &target_group.gid) + { + // make sure the requested group id is the first in the list (necessary on FreeBSD) + target_user.groups.swap(0, index) + } else { + // add target group to list of additional groups if not present target_user.groups.insert(0, target_group.gid); }