Skip to content

device: use runtime instead of compile-time environment var for macos drop privileges #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions boringtun/src/device/drop_privileges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ use nix::unistd::User;
pub fn get_saved_ids() -> Result<(uid_t, gid_t), Error> {
// Get the user name of the sudoer
#[cfg(target_os = "macos")]
{
let uname: &'static str = env!("USER");
let user = User::from_name(uname).unwrap().expect("a user");
Ok((uid_t::from(user.uid), gid_t::from(user.gid)))
match std::env::var("USER") {
Ok(uname) => match User::from_name(&uname) {
Ok(Some(user)) => Ok((uid_t::from(user.uid), gid_t::from(user.gid))),
Err(e) => Err(Error::DropPrivileges(format!(
"Failed parse user; err: {:?}",
e
))),
Ok(None) => Err(Error::DropPrivileges("Failed to find user".to_owned())),
},
Err(e) => Err(Error::DropPrivileges(format!(
"Could not get environment variable for user; err: {:?}",
e
))),
}
#[cfg(not(target_os = "macos"))]
{
Expand Down