Repository navigation
pwcat: add password database dump utility #63
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
Open
Franklin-Qi
wants to merge
1
commit into
uutils:main
Choose a base branch
from
Franklin-Qi:feature-issue#61-add-pwcat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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,101 @@ | ||
| // This file is part of the uutils awk package. | ||
| // | ||
| // For the full copyright and license information, please view the LICENSE | ||
| // files that was distributed with this source code. | ||
|
|
||
| //! Dump the password database in `/etc/passwd` format for gawk library routines. | ||
| //! | ||
| //! Behavior matches the `pwcat` helper described in the GNU Awk User's Guide: | ||
| //! <https://www.gnu.org/software/gawk/manual/html_node/Passwd-Functions.html> | ||
| //! | ||
| //! Uses `getpwent()`/`endpwent()` so NSS sources beyond local files (LDAP, SSSD, | ||
| //! systemd-userdb, etc.) are included, as required by routines such as | ||
| //! `passwd.awk`. | ||
| //! | ||
| //! Platforms without a password database get an empty dump and a successful exit. | ||
|
|
||
| #[cfg(unix)] | ||
| use std::io::{self, Write}; | ||
| use std::process::ExitCode; | ||
|
|
||
| #[cfg(unix)] | ||
| fn main() -> ExitCode { | ||
| match run() { | ||
| Ok(()) => ExitCode::SUCCESS, | ||
| Err(err) if err.kind() == io::ErrorKind::BrokenPipe => ExitCode::SUCCESS, | ||
| Err(err) => { | ||
| let _ = writeln!(io::stderr(), "pwcat: {err}"); | ||
| ExitCode::FAILURE | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(unix))] | ||
| fn main() -> ExitCode { | ||
| ExitCode::SUCCESS | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| fn run() -> io::Result<()> { | ||
| struct EndPwent; | ||
|
|
||
| impl Drop for EndPwent { | ||
| fn drop(&mut self) { | ||
| // SAFETY: pairs with `setpwent` below; `pwcat` is single-threaded. | ||
| unsafe { | ||
| libc::endpwent(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let mut out = io::stdout().lock(); | ||
|
|
||
| // SAFETY: `getpwent`/`setpwent`/`endpwent` share process-global state and are | ||
| // not thread-safe. `pwcat` is a single-threaded helper, so exclusive use is OK. | ||
| unsafe { | ||
| libc::setpwent(); | ||
| } | ||
| let _end = EndPwent; | ||
|
|
||
| loop { | ||
| // SAFETY: see note above; pointer is only used while non-null and before | ||
| // the next `getpwent`/`endpwent` call. | ||
| let passwd = unsafe { libc::getpwent() }; | ||
| if passwd.is_null() { | ||
| break; | ||
| } | ||
| // SAFETY: `getpwent` returned a non-null pointer to a valid `passwd`. | ||
| let passwd = unsafe { &*passwd }; | ||
|
|
||
| write_field(&mut out, passwd.pw_name)?; | ||
| out.write_all(b":")?; | ||
| write_field_or_star(&mut out, passwd.pw_passwd)?; | ||
| write!(out, ":{}:{}:", passwd.pw_uid, passwd.pw_gid)?; | ||
| write_field(&mut out, passwd.pw_gecos)?; | ||
| out.write_all(b":")?; | ||
| write_field(&mut out, passwd.pw_dir)?; | ||
| out.write_all(b":")?; | ||
| write_field(&mut out, passwd.pw_shell)?; | ||
| out.write_all(b"\n")?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| fn write_field(out: &mut impl Write, ptr: *const libc::c_char) -> io::Result<()> { | ||
| if ptr.is_null() { | ||
| return Ok(()); | ||
| } | ||
| // SAFETY: caller guarantees a NUL-terminated C string when non-null. | ||
| let bytes = unsafe { std::ffi::CStr::from_ptr(ptr) }.to_bytes(); | ||
| out.write_all(bytes) | ||
| } | ||
|
|
||
| #[cfg(unix)] | ||
| fn write_field_or_star(out: &mut impl Write, ptr: *const libc::c_char) -> io::Result<()> { | ||
| if ptr.is_null() { | ||
| return out.write_all(b"*"); | ||
| } | ||
| write_field(out, ptr) | ||
| } | ||
This file contains hidden or 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,95 @@ | ||
| // This file is part of the uutils awk package. | ||
| // | ||
| // For the full copyright and license information, please view the LICENSE | ||
| // files that was distributed with this source code. | ||
|
|
||
| use std::process::{Command, Stdio}; | ||
|
|
||
| use crate::{TESTS_BINARY, ucmd}; | ||
|
|
||
| #[cfg_attr( | ||
| not(target_os = "linux"), | ||
| ignore = "pwcat tests require Linux NSS via getent" | ||
| )] | ||
| #[test] | ||
| fn pwcat_outputs_passwd_database_format() { | ||
| let result = ucmd().succeeds(); | ||
| let stdout = result.stdout_str(); | ||
|
|
||
| assert!( | ||
| !stdout.is_empty(), | ||
| "pwcat produced no output; password database may be unavailable in this environment" | ||
| ); | ||
|
|
||
| for line in stdout.lines().filter(|line| !line.is_empty()) { | ||
| let fields: Vec<&str> = line.split(':').collect(); | ||
| assert_eq!( | ||
| fields.len(), | ||
| 7, | ||
| "expected 7 colon-separated fields in line: {line}" | ||
| ); | ||
| assert!( | ||
| fields[2].chars().all(|ch| ch.is_ascii_digit()), | ||
| "expected numeric uid in line: {line}" | ||
| ); | ||
| assert!( | ||
| fields[3].chars().all(|ch| ch.is_ascii_digit()), | ||
| "expected numeric gid in line: {line}" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Regression test for gawk compatibility: pwcat must match the password database | ||
| // format consumed by gawk library routines (see passwd.awk). | ||
| #[cfg_attr( | ||
| not(target_os = "linux"), | ||
| ignore = "pwcat tests require Linux NSS via getent" | ||
| )] | ||
| #[test] | ||
| fn pwcat_matches_getent_passwd() { | ||
| let getent = Command::new("getent") | ||
| .arg("passwd") | ||
| .output() | ||
| .expect("failed to spawn getent; install it or skip this host explicitly"); | ||
| assert!( | ||
| getent.status.success(), | ||
| "getent passwd is required for this test (exit {:?}): {}", | ||
| getent.status.code(), | ||
| String::from_utf8_lossy(&getent.stderr) | ||
| ); | ||
|
|
||
| let pwcat = ucmd().succeeds(); | ||
| assert_eq!( | ||
| getent.stdout.as_slice(), | ||
| pwcat.stdout(), | ||
| "pwcat output should match getent passwd" | ||
| ); | ||
| } | ||
|
|
||
| #[cfg_attr( | ||
| not(target_os = "linux"), | ||
| ignore = "pwcat tests require Linux NSS via getent" | ||
| )] | ||
| #[test] | ||
| fn pwcat_ignores_broken_pipe() { | ||
| let mut child = Command::new(TESTS_BINARY) | ||
| .stdout(Stdio::piped()) | ||
| .stderr(Stdio::piped()) | ||
| .spawn() | ||
| .expect("failed to spawn pwcat"); | ||
|
|
||
| // Close the read end immediately so the next write hits EPIPE/BrokenPipe. | ||
| drop(child.stdout.take()); | ||
|
|
||
| let status = child.wait().expect("failed to wait for pwcat"); | ||
| assert!( | ||
| status.success(), | ||
| "pwcat should treat broken pipe as success, got {status}" | ||
| ); | ||
| } | ||
|
|
||
| #[cfg(not(unix))] | ||
| #[test] | ||
| fn pwcat_without_passwd_database_prints_nothing_and_succeeds() { | ||
| ucmd().succeeds().no_output(); | ||
| } |
This file contains hidden or 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,27 @@ | ||
| // This file is part of the uutils awk package. | ||
| // | ||
| // For the full copyright and license information, please view the LICENSE | ||
| // files that was distributed with this source code. | ||
|
|
||
| use std::env; | ||
|
|
||
| use uutests::util::TestScenario; | ||
|
|
||
| pub const TESTS_BINARY: &str = env!("CARGO_BIN_EXE_pwcat"); | ||
|
|
||
| #[ctor::ctor(unsafe)] | ||
| fn init() { | ||
| unsafe { | ||
| env::set_var("UUTESTS_BINARY_PATH", TESTS_BINARY); | ||
| env::remove_var("UUTESTS_UTIL_NAME"); | ||
| env::set_var("UUTESTS_UTIL_NAME", ""); | ||
| env::set_var("UUTILS_MULTICALL", "0"); | ||
| } | ||
| } | ||
|
|
||
| fn ucmd() -> uutests::util::UCommand { | ||
| TestScenario::new("pwcat").cmd(TESTS_BINARY) | ||
| } | ||
|
|
||
| #[path = "by-util/test_pwcat.rs"] | ||
| mod test_pwcat; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.