-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #539 from 7flying/rm-shadow-passwd
feat: replace shadow and passwd crates with our built-in functions
- Loading branch information
Showing
8 changed files
with
77 additions
and
50 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod common; | ||
use fdo_util::passwd_shadow; | ||
use std::env; | ||
use std::{fs, io::Write, process::Command, time::Duration}; | ||
|
||
|
@@ -401,17 +402,20 @@ ssh-ed25519 sshkey_default [email protected] | |
if ci { | ||
L.l("Running create initial user validation"); | ||
pretty_assertions::assert_eq!( | ||
passwd::Passwd::from_name(new_user).is_some(), | ||
passwd_shadow::is_user_in_passwd(new_user)?, | ||
true, | ||
"User: {} is not created during onboarding", | ||
&new_user | ||
); | ||
if let Some(test_user) = shadow::Shadow::from_name(new_user) { | ||
pretty_assertions::assert_eq!( | ||
test_user.password.is_empty(), | ||
false, | ||
"Password not created during onboarding" | ||
); | ||
match passwd_shadow::get_user_passwd(new_user) { | ||
Ok(pass) => { | ||
pretty_assertions::assert_eq!( | ||
pass.is_empty(), | ||
false, | ||
"Password not created during onboarding" | ||
); | ||
} | ||
Err(e) => bail!(e), | ||
} | ||
} else { | ||
L.l("Skipped create initial user validation | ||
|
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod common; | ||
use fdo_util::passwd_shadow; | ||
use std::env; | ||
#[allow(unused_imports)] | ||
use std::{fs, io::Write, process::Command, time::Duration}; | ||
|
@@ -280,17 +281,21 @@ ssh-ed25519 sshkey_default [email protected] | |
if ci { | ||
L.l("Running create initial user validation"); | ||
pretty_assertions::assert_eq!( | ||
passwd::Passwd::from_name(new_user).is_some(), | ||
passwd_shadow::is_user_in_passwd(new_user)?, | ||
true, | ||
"User: {} is not created during onboarding", | ||
&new_user | ||
); | ||
if let Some(test_user) = shadow::Shadow::from_name(new_user) { | ||
pretty_assertions::assert_eq!( | ||
test_user.password.is_empty(), | ||
false, | ||
"Password not created during onboarding" | ||
); | ||
|
||
match passwd_shadow::get_user_passwd(new_user) { | ||
Ok(pass) => { | ||
pretty_assertions::assert_eq!( | ||
pass.is_empty(), | ||
false, | ||
"Password not created during onboarding" | ||
); | ||
} | ||
Err(e) => bail!(e), | ||
} | ||
} else { | ||
L.l("Skipped create initial user validation | ||
|
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,42 @@ | ||
use anyhow::{bail, Result}; | ||
use std::fs; | ||
|
||
/// Checks whether an entry for the given user name 'username' exists in | ||
/// /etc/passwd | ||
pub fn is_user_in_passwd(username: &str) -> Result<bool> { | ||
for line in fs::read_to_string("/etc/passwd")?.lines() { | ||
let contents: Vec<&str> = line.split(':').collect(); | ||
if !contents.is_empty() && contents[0] == username { | ||
return Ok(true); | ||
} | ||
} | ||
Ok(false) | ||
} | ||
|
||
/// Returns the uid, gid and home of the given user or Error if user does not | ||
/// exist in /etc/passwd. | ||
pub fn get_user_uid_gid_home(username: &str) -> Result<(u32, u32, String)> { | ||
for line in fs::read_to_string("/etc/passwd")?.lines() { | ||
let contents: Vec<&str> = line.split(':').collect(); | ||
if !contents.is_empty() && contents[0] == username { | ||
return Ok(( | ||
contents[2].parse::<u32>()?, | ||
contents[3].parse::<u32>()?, | ||
contents[5].to_string(), | ||
)); | ||
} | ||
} | ||
bail!("User {username} not found") | ||
} | ||
|
||
/// Returns the password of a given user. Errors if the user does not exist | ||
/// in /etc/shadow. | ||
pub fn get_user_passwd(username: &str) -> Result<String> { | ||
for line in fs::read_to_string("/etc/shadow")?.lines() { | ||
let contents: Vec<&str> = line.split(':').collect(); | ||
if !contents.is_empty() && contents[0] == username { | ||
return Ok(contents[1].to_string()); | ||
} | ||
} | ||
bail!("User {username} not found") | ||
} |