Skip to content

Commit 6cbfed7

Browse files
committed
sysusers: Delete altfiles for passwd: and group: entries
1 parent a9c572a commit 6cbfed7

File tree

5 files changed

+60
-15
lines changed

5 files changed

+60
-15
lines changed

rpmostree-cxxrs.cxx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,8 @@ extern "C"
21652165
::rpmostreecxx::Treefile &treefile) noexcept;
21662166

21672167
::rust::repr::PtrLen
2168-
rpmostreecxx$cxxbridge1$composepost_nsswitch_altfiles (::std::int32_t rootfs_dfd) noexcept;
2168+
rpmostreecxx$cxxbridge1$composepost_nsswitch_altfiles (::std::int32_t rootfs_dfd,
2169+
bool sysusers) noexcept;
21692170

21702171
::rust::repr::PtrLen rpmostreecxx$cxxbridge1$compose_postprocess (
21712172
::std::int32_t rootfs_dfd, ::rpmostreecxx::Treefile &treefile, ::rust::Str next_version,
@@ -3945,9 +3946,10 @@ compose_prepare_rootfs (::std::int32_t src_rootfs_dfd, ::std::int32_t dest_rootf
39453946
}
39463947

39473948
void
3948-
composepost_nsswitch_altfiles (::std::int32_t rootfs_dfd)
3949+
composepost_nsswitch_altfiles (::std::int32_t rootfs_dfd, bool sysusers)
39493950
{
3950-
::rust::repr::PtrLen error$ = rpmostreecxx$cxxbridge1$composepost_nsswitch_altfiles (rootfs_dfd);
3951+
::rust::repr::PtrLen error$
3952+
= rpmostreecxx$cxxbridge1$composepost_nsswitch_altfiles (rootfs_dfd, sysusers);
39513953
if (error$.ptr)
39523954
{
39533955
throw ::rust::impl< ::rust::Error>::error (error$);

rpmostree-cxxrs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,7 @@ void configure_build_repo_from_target (::rpmostreecxx::OstreeRepo const &build_r
18371837
void compose_prepare_rootfs (::std::int32_t src_rootfs_dfd, ::std::int32_t dest_rootfs_dfd,
18381838
::rpmostreecxx::Treefile &treefile);
18391839

1840-
void composepost_nsswitch_altfiles (::std::int32_t rootfs_dfd);
1840+
void composepost_nsswitch_altfiles (::std::int32_t rootfs_dfd, bool sysusers);
18411841

18421842
void compose_postprocess (::std::int32_t rootfs_dfd, ::rpmostreecxx::Treefile &treefile,
18431843
::rust::Str next_version, bool unified_core);

rust/src/composepost.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,36 @@ fn strip_any_prefix<'a, 'b>(s: &'a str, prefixes: &[&'b str]) -> Option<(&'b str
640640
.find_map(|&p| s.strip_prefix(p).map(|r| (p, r)))
641641
}
642642

643+
#[context("Delete altfiles for passwd and group entries")]
644+
fn del_altfiles(buf: &str) -> Result<String> {
645+
let mut r = String::with_capacity(buf.len());
646+
for line in buf.lines() {
647+
let parts = if let Some(p) = strip_any_prefix(line, &["passwd:", "group:"]) {
648+
p
649+
} else {
650+
r.push_str(line);
651+
r.push('\n');
652+
continue;
653+
};
654+
let (prefix, rest) = parts;
655+
r.push_str(prefix);
656+
657+
for elt in rest.split_whitespace() {
658+
if elt == "altfiles" {
659+
// skip altfiles
660+
continue;
661+
} else {
662+
r.push(' ');
663+
r.push_str(elt);
664+
}
665+
}
666+
r.push('\n');
667+
}
668+
Ok(r)
669+
}
670+
643671
/// Inject `altfiles` after `files` for `passwd:` and `group:` entries.
672+
#[allow(dead_code)]
644673
fn add_altfiles(buf: &str) -> Result<String> {
645674
let mut r = String::with_capacity(buf.len());
646675
for line in buf.lines() {
@@ -677,20 +706,33 @@ fn add_altfiles(buf: &str) -> Result<String> {
677706
Ok(r)
678707
}
679708

680-
/// Add `altfiles` entries to `nsswitch.conf`.
709+
/// Add or delete `altfiles` entries to `nsswitch.conf`.
681710
///
682-
/// rpm-ostree currently depends on `altfiles`
683-
#[context("Adding altfiles to /etc/nsswitch.conf")]
684-
pub fn composepost_nsswitch_altfiles(rootfs_dfd: i32) -> CxxResult<()> {
711+
/// rpm-ostree currently depends on `altfiles`, should remove it when
712+
/// transfer to systemd-sysusers.
713+
#[context("Adding / deleting altfiles to /etc/nsswitch.conf")]
714+
pub fn composepost_nsswitch_altfiles(rootfs_dfd: i32, sysusers: bool) -> CxxResult<()> {
685715
let rootfs_dfd = unsafe { &crate::ffiutil::ffi_dirfd(rootfs_dfd)? };
686716
let path = "usr/etc/nsswitch.conf";
687717
if let Some(meta) = rootfs_dfd.symlink_metadata_optional(path)? {
688718
// If it's a symlink, then something else e.g. authselect must own it.
719+
// Do nothing if disable systemd-sysusers.
689720
if meta.is_symlink() {
690-
return Ok(());
721+
if !sysusers {
722+
return Ok(());
723+
}
691724
}
692-
let nsswitch = rootfs_dfd.read_to_string(path)?;
693-
let nsswitch = add_altfiles(&nsswitch)?;
725+
// Delete the symlink, create and update the config.
726+
let target = "usr/etc/authselect/nsswitch.conf";
727+
let nsswitch = rootfs_dfd
728+
.read_to_string(target)
729+
.with_context(|| format!("Reading target {}", target))?;
730+
rootfs_dfd
731+
.remove_file(path)
732+
.with_context(|| format!("Removing {}", path))?;
733+
rootfs_dfd.create(path)?;
734+
735+
let nsswitch = del_altfiles(&nsswitch)?;
694736
rootfs_dfd.atomic_write(path, nsswitch.as_bytes())?;
695737
}
696738

rust/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ pub mod ffi {
282282
dest_rootfs_dfd: i32,
283283
treefile: &mut Treefile,
284284
) -> Result<()>;
285-
fn composepost_nsswitch_altfiles(rootfs_dfd: i32) -> Result<()>;
285+
fn composepost_nsswitch_altfiles(rootfs_dfd: i32, sysusers: bool) -> Result<()>;
286286
fn compose_postprocess(
287287
rootfs_dfd: i32,
288288
treefile: &mut Treefile,

src/libpriv/rpmostree-postprocess.cxx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,12 @@ postprocess_final (int rootfs_dfd, rpmostreecxx::Treefile &treefile, gboolean un
417417

418418
g_print ("Migrating /usr/etc/group to /usr/lib/\n");
419419
ROSCXX_TRY (migrate_group_except_root (rootfs_dfd, preserve_groups_set), error);
420-
421-
/* NSS configuration to look at the new files */
422-
ROSCXX_TRY (composepost_nsswitch_altfiles (rootfs_dfd), error);
423420
}
424421

422+
/* NSS configuration to look at the new files. */
423+
/* Should remove altfiles if we transfer to systemd-sysusers. */
424+
ROSCXX_TRY (composepost_nsswitch_altfiles (rootfs_dfd, sysusers), error);
425+
425426
if (selinux)
426427
{
427428
if (!postprocess_selinux_policy_store_location (rootfs_dfd, cancellable, error))

0 commit comments

Comments
 (0)