-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
101 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"bincode", | ||
"chacha", | ||
"clientos", | ||
"cstring", | ||
"datetime", | ||
"disconnectable", | ||
"distro", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::env; | ||
|
||
use anyhow::bail; | ||
use uzers::User; | ||
|
||
pub fn get_user_by_name(username: &str) -> anyhow::Result<User> { | ||
uzers::get_user_by_name(username).ok_or_else(|| anyhow::anyhow!("User ({}) not found", username)) | ||
} | ||
|
||
pub fn get_non_root_user() -> anyhow::Result<User> { | ||
let current_user = whoami::username(); | ||
|
||
let user = if current_user == "root" { | ||
get_real_user()? | ||
} else { | ||
get_user_by_name(¤t_user)? | ||
}; | ||
|
||
if user.uid() == 0 { | ||
bail!("Non-root user not found") | ||
} | ||
|
||
Ok(user) | ||
} | ||
|
||
fn get_real_user() -> anyhow::Result<User> { | ||
// Read the UID from SUDO_UID or PKEXEC_UID environment variable if available. | ||
let uid = match env::var("SUDO_UID") { | ||
Ok(uid) => uid.parse::<u32>()?, | ||
_ => env::var("PKEXEC_UID")?.parse::<u32>()?, | ||
}; | ||
|
||
uzers::get_user_by_uid(uid).ok_or_else(|| anyhow::anyhow!("User not found")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters