Skip to content

Commit 25e9cf6

Browse files
committed
sysusers: Make loading /etc/passwd optional
On general principle; this fixes a test failure in the lint unit tests which don't create /etc/passwd. But also, someone might reasonably create an image this way. Signed-off-by: Colin Walters <walters@verbum.org>
1 parent f0e890a commit 25e9cf6

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

sysusers/src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ pub fn read_sysusers(rootfs: &Dir) -> Result<Vec<SysusersEntry>> {
201201
}
202202

203203
/// The result of analyzing /etc/{passwd,group} in a root vs systemd-sysusers.
204-
#[derive(Debug)]
204+
#[derive(Debug, Default)]
205205
pub struct SysusersAnalysis {
206206
/// Entries which are found in /etc/passwd but not present in systemd-sysusers.
207207
pub missing_users: BTreeSet<String>,
@@ -230,8 +230,14 @@ pub fn analyze(rootfs: &Dir) -> Result<SysusersAnalysis> {
230230
id: Option<u32>,
231231
}
232232

233-
let mut passwd = nameservice::passwd::load_etc_passwd(rootfs)
233+
let Some(passwd) = nameservice::passwd::load_etc_passwd(rootfs)
234234
.map_err(|e| Error::PasswdLoadFailure(e.to_string()))?
235+
else {
236+
// If there's no /etc/passwd then we're done
237+
return Ok(SysusersAnalysis::default());
238+
};
239+
240+
let mut passwd = passwd
235241
.into_iter()
236242
.map(|mut e| {
237243
// Make the name be the map key, leaving the old value a stub

sysusers/src/nameservice/passwd.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

44
use anyhow::{anyhow, Context, Result};
5-
use cap_std_ext::cap_std::fs::Dir;
5+
use cap_std_ext::{cap_std::fs::Dir, dirext::CapStdExtDirExt};
66
use std::io::{BufRead, BufReader, Write};
77

88
// Entry from passwd file.
@@ -78,9 +78,12 @@ pub(crate) fn parse_passwd_content(content: impl BufRead) -> Result<Vec<PasswdEn
7878
Ok(passwds)
7979
}
8080

81-
pub(crate) fn load_etc_passwd(rootfs: &Dir) -> Result<Vec<PasswdEntry>> {
82-
let r = rootfs.open("etc/passwd").map(BufReader::new)?;
83-
parse_passwd_content(r)
81+
pub(crate) fn load_etc_passwd(rootfs: &Dir) -> Result<Option<Vec<PasswdEntry>>> {
82+
if let Some(r) = rootfs.open_optional("etc/passwd")? {
83+
parse_passwd_content(BufReader::new(r)).map(Some)
84+
} else {
85+
Ok(None)
86+
}
8487
}
8588

8689
#[cfg(test)]

0 commit comments

Comments
 (0)