From e412fc5e042b8f642bcba42f5c175124583e05ae Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 22 Nov 2024 09:34:56 +0100 Subject: [PATCH 1/3] userbdctl: show 'mapped' user range only inside of userns Outside of userns the concept makes no sense, there cannot be users mapped from further outside. --- src/userdb/userdbctl.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/userdb/userdbctl.c b/src/userdb/userdbctl.c index cbbd9b10b5..6028a87827 100644 --- a/src/userdb/userdbctl.c +++ b/src/userdb/userdbctl.c @@ -23,6 +23,7 @@ #include "user-util.h" #include "userdb.h" #include "verbs.h" +#include "virt.h" static enum { OUTPUT_CLASSIC, @@ -139,10 +140,16 @@ static int show_user(UserRecord *ur, Table *table) { return 0; } +static bool test_show_mapped(void) { + /* Show mapped user range only in environments where user mapping is a thing. */ + return running_in_userns() > 0; +} + static const struct { uid_t first, last; const char *name; UserDisposition disposition; + bool (*test)(void); } uid_range_table[] = { { .first = 1, @@ -175,6 +182,7 @@ static const struct { .last = MAP_UID_MAX, .name = "mapped", .disposition = USER_REGULAR, + .test = test_show_mapped, }, }; @@ -192,6 +200,9 @@ static int table_add_uid_boundaries(Table *table, const UIDRange *p) { if (!uid_range_covers(p, i->first, i->last - i->first + 1)) continue; + if (i->test && !i->test()) + continue; + name = strjoin(special_glyph(SPECIAL_GLYPH_ARROW_DOWN), " begin ", i->name, " users ", special_glyph(SPECIAL_GLYPH_ARROW_DOWN)); @@ -575,6 +586,9 @@ static int table_add_gid_boundaries(Table *table, const UIDRange *p) { if (!uid_range_covers(p, i->first, i->last - i->first + 1)) continue; + if (i->test && !i->test()) + continue; + name = strjoin(special_glyph(SPECIAL_GLYPH_ARROW_DOWN), " begin ", i->name, " groups ", special_glyph(SPECIAL_GLYPH_ARROW_DOWN)); From 7f8a4f12dfea6f644f92788bd9b03983898e9d32 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 22 Nov 2024 10:11:32 +0100 Subject: [PATCH 2/3] userdbctl: fix counting Fixes: #35294 --- src/userdb/userdbctl.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/userdb/userdbctl.c b/src/userdb/userdbctl.c index 6028a87827..19de87d01e 100644 --- a/src/userdb/userdbctl.c +++ b/src/userdb/userdbctl.c @@ -187,7 +187,7 @@ static const struct { }; static int table_add_uid_boundaries(Table *table, const UIDRange *p) { - int r; + int r, n_added = 0; assert(table); @@ -260,9 +260,11 @@ static int table_add_uid_boundaries(Table *table, const UIDRange *p) { TABLE_INT, 1); /* sort after any other entry with the same UID */ if (r < 0) return table_log_add_error(r); + + n_added += 2; } - return ELEMENTSOF(uid_range_table) * 2; + return n_added; } static int add_unavailable_uid(Table *table, uid_t start, uid_t end) { @@ -576,7 +578,7 @@ static int show_group(GroupRecord *gr, Table *table) { } static int table_add_gid_boundaries(Table *table, const UIDRange *p) { - int r; + int r, n_added = 0; assert(table); @@ -640,9 +642,11 @@ static int table_add_gid_boundaries(Table *table, const UIDRange *p) { TABLE_INT, 1); /* sort after any other entry with the same GID */ if (r < 0) return table_log_add_error(r); + + n_added += 2; } - return ELEMENTSOF(uid_range_table) * 2; + return n_added; } static int add_unavailable_gid(Table *table, uid_t start, uid_t end) { From 47c5ca237b3074e143ad77a5926d9f442ed17eb3 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 22 Nov 2024 11:26:34 +0100 Subject: [PATCH 3/3] userdbctl: respect selected disposition also when showing gid boundaries Follow-up for: ad5de3222f7 --- src/userdb/userdbctl.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/userdb/userdbctl.c b/src/userdb/userdbctl.c index 19de87d01e..bc69175b10 100644 --- a/src/userdb/userdbctl.c +++ b/src/userdb/userdbctl.c @@ -585,6 +585,9 @@ static int table_add_gid_boundaries(Table *table, const UIDRange *p) { FOREACH_ELEMENT(i, uid_range_table) { _cleanup_free_ char *name = NULL, *comment = NULL; + if (!FLAGS_SET(arg_disposition_mask, UINT64_C(1) << i->disposition)) + continue; + if (!uid_range_covers(p, i->first, i->last - i->first + 1)) continue;