Derive the session USER and HOME from the policy's `run_as_user`. For name-based identities, looks up the home directory via `/etc/passwd` (or defaults to `/home/{user}`). For numeric UIDs, there is no passwd entry — falls back to `("{uid}", "/sandbox")` so the agent session still has a meaningful USER identifier.
(policy: &SandboxPolicy)
| 668 | /// `("{uid}", "/sandbox")` so the agent session still has a meaningful |
| 669 | /// USER identifier. |
| 670 | fn session_user_and_home(policy: &SandboxPolicy) -> (String, String) { |
| 671 | match policy.process.run_as_user.as_deref() { |
| 672 | Some(user) if !user.is_empty() => { |
| 673 | // Numeric UID — no passwd entry expected; use default HOME. |
| 674 | if user.parse::<u32>().is_ok() { |
| 675 | return (user.to_string(), "/sandbox".to_string()); |
| 676 | } |
| 677 | // Name-based identity — look up home from /etc/passwd. |
| 678 | let home = nix::unistd::User::from_name(user) |
| 679 | .ok() |
| 680 | .flatten() |
| 681 | .map_or_else( |
| 682 | || format!("/home/{user}"), |
| 683 | |u| u.dir.to_string_lossy().into_owned(), |
| 684 | ); |
| 685 | (user.to_string(), home) |
| 686 | } |
| 687 | _ => ("sandbox".to_string(), "/sandbox".to_string()), |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | #[allow(clippy::too_many_arguments)] |
| 692 | fn apply_child_env( |