src/basic: rename uid-alloc-range.[ch] to uid-classification.[ch]
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 9 Jan 2024 09:37:27 +0000 (10:37 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 9 Jan 2024 10:15:00 +0000 (11:15 +0100)
We had both uid-range.h and uid-alloc-range.h. The latter now contains helpers
like {uid,gid}_is_{system,dynamic,container}(), uid_for_system_journal(), so
the existing name is outdated. I think the uid-range.[ch] should stay separate
because it has a bunch of helpers for parsing and printing of uid ranges. So
let's rename as in $subject to better reflect the contents of the file and make
the two sets of files harder to confuse.

22 files changed:
src/basic/meson.build
src/basic/uid-alloc-range.c [deleted file]
src/basic/uid-alloc-range.h [deleted file]
src/basic/uid-classification.c [new file with mode: 0644]
src/basic/uid-classification.h [new file with mode: 0644]
src/core/dynamic-user.c
src/coredump/coredump.c
src/dissect/dissect.c
src/home/homectl.c
src/home/homed-home.c
src/journal/journald-server.c
src/libsystemd/sd-journal/sd-journal.c
src/login/logind-session.c
src/login/logind-user.c
src/shared/condition.c
src/shared/group-record.c
src/shared/user-record.c
src/sysusers/sysusers.c
src/test/meson.build
src/test/test-condition.c
src/test/test-uid-alloc-range.c [deleted file]
src/test/test-uid-classification.c [new file with mode: 0644]

index d7450d8b445ec37d0d05459d80be2747a58fb309..949ca4d81f971a41019890a8b48140a766cc5c0c 100644 (file)
@@ -96,7 +96,7 @@ basic_sources = files(
         'terminal-util.c',
         'time-util.c',
         'tmpfile-util.c',
-        'uid-alloc-range.c',
+        'uid-classification.c',
         'uid-range.c',
         'unit-def.c',
         'unit-file.c',
diff --git a/src/basic/uid-alloc-range.c b/src/basic/uid-alloc-range.c
deleted file mode 100644 (file)
index 669cb6d..0000000
+++ /dev/null
@@ -1,131 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include "chase.h"
-#include "fd-util.h"
-#include "fileio.h"
-#include "missing_threads.h"
-#include "string-util.h"
-#include "uid-alloc-range.h"
-#include "user-util.h"
-
-static const UGIDAllocationRange default_ugid_allocation_range = {
-        .system_alloc_uid_min = SYSTEM_ALLOC_UID_MIN,
-        .system_uid_max = SYSTEM_UID_MAX,
-        .system_alloc_gid_min = SYSTEM_ALLOC_GID_MIN,
-        .system_gid_max = SYSTEM_GID_MAX,
-};
-
-#if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
-static int parse_alloc_uid(const char *path, const char *name, const char *t, uid_t *ret_uid) {
-        uid_t uid;
-        int r;
-
-        r = parse_uid(t, &uid);
-        if (r < 0)
-                return log_debug_errno(r, "%s: failed to parse %s %s, ignoring: %m", path, name, t);
-        if (uid == 0)
-                uid = 1;
-
-        *ret_uid = uid;
-        return 0;
-}
-#endif
-
-int read_login_defs(UGIDAllocationRange *ret_defs, const char *path, const char *root) {
-#if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
-        _cleanup_fclose_ FILE *f = NULL;
-        UGIDAllocationRange defs;
-        int r;
-
-        if (!path)
-                path = "/etc/login.defs";
-
-        r = chase_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT, "re", NULL, &f);
-        if (r == -ENOENT)
-                goto defaults;
-        if (r < 0)
-                return log_debug_errno(r, "Failed to open %s: %m", path);
-
-        defs = default_ugid_allocation_range;
-
-        for (;;) {
-                _cleanup_free_ char *line = NULL;
-                char *t;
-
-                r = read_line(f, LINE_MAX, &line);
-                if (r < 0)
-                        return log_debug_errno(r, "Failed to read %s: %m", path);
-                if (r == 0)
-                        break;
-
-                if ((t = first_word(line, "SYS_UID_MIN")))
-                        (void) parse_alloc_uid(path, "SYS_UID_MIN", t, &defs.system_alloc_uid_min);
-                else if ((t = first_word(line, "SYS_UID_MAX")))
-                        (void) parse_alloc_uid(path, "SYS_UID_MAX", t, &defs.system_uid_max);
-                else if ((t = first_word(line, "SYS_GID_MIN")))
-                        (void) parse_alloc_uid(path, "SYS_GID_MIN", t, &defs.system_alloc_gid_min);
-                else if ((t = first_word(line, "SYS_GID_MAX")))
-                        (void) parse_alloc_uid(path, "SYS_GID_MAX", t, &defs.system_gid_max);
-        }
-
-        if (defs.system_alloc_uid_min > defs.system_uid_max) {
-                log_debug("%s: SYS_UID_MIN > SYS_UID_MAX, resetting.", path);
-                defs.system_alloc_uid_min = MIN(defs.system_uid_max - 1, (uid_t) SYSTEM_ALLOC_UID_MIN);
-                /* Look at sys_uid_max to make sure sys_uid_min..sys_uid_max remains a valid range. */
-        }
-        if (defs.system_alloc_gid_min > defs.system_gid_max) {
-                log_debug("%s: SYS_GID_MIN > SYS_GID_MAX, resetting.", path);
-                defs.system_alloc_gid_min = MIN(defs.system_gid_max - 1, (gid_t) SYSTEM_ALLOC_GID_MIN);
-                /* Look at sys_gid_max to make sure sys_gid_min..sys_gid_max remains a valid range. */
-        }
-
-        *ret_defs = defs;
-        return 1;
-defaults:
-#endif
-        *ret_defs = default_ugid_allocation_range;
-        return 0;
-}
-
-const UGIDAllocationRange *acquire_ugid_allocation_range(void) {
-#if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
-        static thread_local UGIDAllocationRange defs;
-        static thread_local int initialized = 0; /* == 0 → not initialized yet
-                                                  * < 0 → failure
-                                                  * > 0 → success */
-
-        /* This function will ignore failure to read the file, so it should only be called from places where
-         * we don't crucially depend on the answer. In other words, it's appropriate for journald, but
-         * probably not for sysusers. */
-
-        if (initialized == 0)
-                initialized = read_login_defs(&defs, NULL, NULL) < 0 ? -1 : 1;
-        if (initialized < 0)
-                return &default_ugid_allocation_range;
-
-        return &defs;
-
-#endif
-        return &default_ugid_allocation_range;
-}
-
-bool uid_is_system(uid_t uid) {
-        const UGIDAllocationRange *defs;
-        assert_se(defs = acquire_ugid_allocation_range());
-
-        return uid <= defs->system_uid_max;
-}
-
-bool gid_is_system(gid_t gid) {
-        const UGIDAllocationRange *defs;
-        assert_se(defs = acquire_ugid_allocation_range());
-
-        return gid <= defs->system_gid_max;
-}
-
-bool uid_for_system_journal(uid_t uid) {
-
-        /* Returns true if the specified UID shall get its data stored in the system journal. */
-
-        return uid_is_system(uid) || uid_is_dynamic(uid) || uid == UID_NOBODY || uid_is_container(uid);
-}
diff --git a/src/basic/uid-alloc-range.h b/src/basic/uid-alloc-range.h
deleted file mode 100644 (file)
index 5badde1..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-#pragma once
-
-#include <stdbool.h>
-#include <sys/types.h>
-
-bool uid_is_system(uid_t uid);
-bool gid_is_system(gid_t gid);
-
-static inline bool uid_is_dynamic(uid_t uid) {
-        return DYNAMIC_UID_MIN <= uid && uid <= DYNAMIC_UID_MAX;
-}
-
-static inline bool gid_is_dynamic(gid_t gid) {
-        return uid_is_dynamic((uid_t) gid);
-}
-
-static inline bool uid_is_container(uid_t uid) {
-        return CONTAINER_UID_BASE_MIN <= uid && uid <= CONTAINER_UID_BASE_MAX;
-}
-
-static inline bool gid_is_container(gid_t gid) {
-        return uid_is_container((uid_t) gid);
-}
-
-typedef struct UGIDAllocationRange {
-        uid_t system_alloc_uid_min;
-        uid_t system_uid_max;
-        gid_t system_alloc_gid_min;
-        gid_t system_gid_max;
-} UGIDAllocationRange;
-
-int read_login_defs(UGIDAllocationRange *ret_defs, const char *path, const char *root);
-const UGIDAllocationRange *acquire_ugid_allocation_range(void);
-
-bool uid_for_system_journal(uid_t uid);
diff --git a/src/basic/uid-classification.c b/src/basic/uid-classification.c
new file mode 100644 (file)
index 0000000..e2d2ceb
--- /dev/null
@@ -0,0 +1,131 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "chase.h"
+#include "fd-util.h"
+#include "fileio.h"
+#include "missing_threads.h"
+#include "string-util.h"
+#include "uid-classification.h"
+#include "user-util.h"
+
+static const UGIDAllocationRange default_ugid_allocation_range = {
+        .system_alloc_uid_min = SYSTEM_ALLOC_UID_MIN,
+        .system_uid_max = SYSTEM_UID_MAX,
+        .system_alloc_gid_min = SYSTEM_ALLOC_GID_MIN,
+        .system_gid_max = SYSTEM_GID_MAX,
+};
+
+#if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
+static int parse_alloc_uid(const char *path, const char *name, const char *t, uid_t *ret_uid) {
+        uid_t uid;
+        int r;
+
+        r = parse_uid(t, &uid);
+        if (r < 0)
+                return log_debug_errno(r, "%s: failed to parse %s %s, ignoring: %m", path, name, t);
+        if (uid == 0)
+                uid = 1;
+
+        *ret_uid = uid;
+        return 0;
+}
+#endif
+
+int read_login_defs(UGIDAllocationRange *ret_defs, const char *path, const char *root) {
+#if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
+        _cleanup_fclose_ FILE *f = NULL;
+        UGIDAllocationRange defs;
+        int r;
+
+        if (!path)
+                path = "/etc/login.defs";
+
+        r = chase_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT, "re", NULL, &f);
+        if (r == -ENOENT)
+                goto defaults;
+        if (r < 0)
+                return log_debug_errno(r, "Failed to open %s: %m", path);
+
+        defs = default_ugid_allocation_range;
+
+        for (;;) {
+                _cleanup_free_ char *line = NULL;
+                char *t;
+
+                r = read_line(f, LINE_MAX, &line);
+                if (r < 0)
+                        return log_debug_errno(r, "Failed to read %s: %m", path);
+                if (r == 0)
+                        break;
+
+                if ((t = first_word(line, "SYS_UID_MIN")))
+                        (void) parse_alloc_uid(path, "SYS_UID_MIN", t, &defs.system_alloc_uid_min);
+                else if ((t = first_word(line, "SYS_UID_MAX")))
+                        (void) parse_alloc_uid(path, "SYS_UID_MAX", t, &defs.system_uid_max);
+                else if ((t = first_word(line, "SYS_GID_MIN")))
+                        (void) parse_alloc_uid(path, "SYS_GID_MIN", t, &defs.system_alloc_gid_min);
+                else if ((t = first_word(line, "SYS_GID_MAX")))
+                        (void) parse_alloc_uid(path, "SYS_GID_MAX", t, &defs.system_gid_max);
+        }
+
+        if (defs.system_alloc_uid_min > defs.system_uid_max) {
+                log_debug("%s: SYS_UID_MIN > SYS_UID_MAX, resetting.", path);
+                defs.system_alloc_uid_min = MIN(defs.system_uid_max - 1, (uid_t) SYSTEM_ALLOC_UID_MIN);
+                /* Look at sys_uid_max to make sure sys_uid_min..sys_uid_max remains a valid range. */
+        }
+        if (defs.system_alloc_gid_min > defs.system_gid_max) {
+                log_debug("%s: SYS_GID_MIN > SYS_GID_MAX, resetting.", path);
+                defs.system_alloc_gid_min = MIN(defs.system_gid_max - 1, (gid_t) SYSTEM_ALLOC_GID_MIN);
+                /* Look at sys_gid_max to make sure sys_gid_min..sys_gid_max remains a valid range. */
+        }
+
+        *ret_defs = defs;
+        return 1;
+defaults:
+#endif
+        *ret_defs = default_ugid_allocation_range;
+        return 0;
+}
+
+const UGIDAllocationRange *acquire_ugid_allocation_range(void) {
+#if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
+        static thread_local UGIDAllocationRange defs;
+        static thread_local int initialized = 0; /* == 0 → not initialized yet
+                                                  * < 0 → failure
+                                                  * > 0 → success */
+
+        /* This function will ignore failure to read the file, so it should only be called from places where
+         * we don't crucially depend on the answer. In other words, it's appropriate for journald, but
+         * probably not for sysusers. */
+
+        if (initialized == 0)
+                initialized = read_login_defs(&defs, NULL, NULL) < 0 ? -1 : 1;
+        if (initialized < 0)
+                return &default_ugid_allocation_range;
+
+        return &defs;
+
+#endif
+        return &default_ugid_allocation_range;
+}
+
+bool uid_is_system(uid_t uid) {
+        const UGIDAllocationRange *defs;
+        assert_se(defs = acquire_ugid_allocation_range());
+
+        return uid <= defs->system_uid_max;
+}
+
+bool gid_is_system(gid_t gid) {
+        const UGIDAllocationRange *defs;
+        assert_se(defs = acquire_ugid_allocation_range());
+
+        return gid <= defs->system_gid_max;
+}
+
+bool uid_for_system_journal(uid_t uid) {
+
+        /* Returns true if the specified UID shall get its data stored in the system journal. */
+
+        return uid_is_system(uid) || uid_is_dynamic(uid) || uid == UID_NOBODY || uid_is_container(uid);
+}
diff --git a/src/basic/uid-classification.h b/src/basic/uid-classification.h
new file mode 100644 (file)
index 0000000..5badde1
--- /dev/null
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include <stdbool.h>
+#include <sys/types.h>
+
+bool uid_is_system(uid_t uid);
+bool gid_is_system(gid_t gid);
+
+static inline bool uid_is_dynamic(uid_t uid) {
+        return DYNAMIC_UID_MIN <= uid && uid <= DYNAMIC_UID_MAX;
+}
+
+static inline bool gid_is_dynamic(gid_t gid) {
+        return uid_is_dynamic((uid_t) gid);
+}
+
+static inline bool uid_is_container(uid_t uid) {
+        return CONTAINER_UID_BASE_MIN <= uid && uid <= CONTAINER_UID_BASE_MAX;
+}
+
+static inline bool gid_is_container(gid_t gid) {
+        return uid_is_container((uid_t) gid);
+}
+
+typedef struct UGIDAllocationRange {
+        uid_t system_alloc_uid_min;
+        uid_t system_uid_max;
+        gid_t system_alloc_gid_min;
+        gid_t system_gid_max;
+} UGIDAllocationRange;
+
+int read_login_defs(UGIDAllocationRange *ret_defs, const char *path, const char *root);
+const UGIDAllocationRange *acquire_ugid_allocation_range(void);
+
+bool uid_for_system_journal(uid_t uid);
index 12724c682c02f3d5573f537fb1393e627c2e133b..484b0e29abdce7218c8d4eead231635b74cd9bb8 100644 (file)
@@ -20,7 +20,7 @@
 #include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 #include "user-util.h"
 
 /* Takes a value generated randomly or by hashing and turns it into a UID in the right range */
index 2b6222b308e03de259e0d19f74582cd0445347b8..cd5e5343f083c8b50f8e3a286a83dd014617fed0 100644 (file)
@@ -51,7 +51,7 @@
 #include "strv.h"
 #include "sync-util.h"
 #include "tmpfile-util.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 #include "user-util.h"
 
 /* The maximum size up to which we process coredumps. We use 1G on 32-bit systems, and 32G on 64-bit systems */
index 35a5d7a236b442809b2077f385ce06d4b841f671..2b080257fed7363fff77e02c8a3f12a19a17ad69 100644 (file)
@@ -46,7 +46,7 @@
 #include "strv.h"
 #include "terminal-util.h"
 #include "tmpfile-util.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 #include "user-util.h"
 #include "vpick.h"
 
index 5a2eb8f31b590dc52dc71626e730776ba666cd92..222bf36e581b241937a8bfd1e902f4fb2a7827a3 100644 (file)
@@ -42,7 +42,7 @@
 #include "rlimit-util.h"
 #include "spawn-polkit-agent.h"
 #include "terminal-util.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 #include "user-record.h"
 #include "user-record-password-quality.h"
 #include "user-record-show.h"
index 787dc773ac9f9563016eb7f6e9243acd0a1a7b01..ea86dffb5289477e4926c9f1f83a5a2cecbb816a 100644 (file)
@@ -38,7 +38,7 @@
 #include "stat-util.h"
 #include "string-table.h"
 #include "strv.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 #include "user-record-password-quality.h"
 #include "user-record-sign.h"
 #include "user-record-util.h"
index 476da317fe764e8a976b17020bd3ccda7f0e490f..2469451fca44f750b3ade2a8d81b6aa6e574f459 100644 (file)
@@ -56,7 +56,7 @@
 #include "string-table.h"
 #include "string-util.h"
 #include "syslog-util.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 #include "user-util.h"
 #include "varlink-io.systemd.Journal.h"
 
index 4d7e53af08f32b74817b8605170470886ac07272..5a8c8a85790bc5afdc03c7ba45ea2a56d0c34ed0 100644 (file)
@@ -43,7 +43,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "syslog-util.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 
 #define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
 
index 8dfbb45c3ec182b4b6bbf828090e363bc4736a6c..c60811f72efb0a1b8d76082e93aaf283287f7341 100644 (file)
@@ -38,7 +38,7 @@
 #include "strv.h"
 #include "terminal-util.h"
 #include "tmpfile-util.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 #include "user-util.h"
 
 #define RELEASE_USEC (20*USEC_PER_SEC)
index 2675fa7e93d4818756842c7471bf53f4ee56e14e..4868eb48fb9f3dc33aa737b4a4f91facb09ee2df 100644 (file)
@@ -33,7 +33,7 @@
 #include "string-table.h"
 #include "strv.h"
 #include "tmpfile-util.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 #include "unit-name.h"
 #include "user-util.h"
 
index d3446e8a9da00dbd21bfc2ec06abf4df5936ed3e..7563f29c94fbc737e7a254056c39bfbb570e86e8 100644 (file)
@@ -59,7 +59,7 @@
 #include "string-util.h"
 #include "tomoyo-util.h"
 #include "tpm2-util.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 #include "user-util.h"
 #include "virt.h"
 
index 4fed5c671cd3d9271eca65fb294c754c3689bf87..7eebd2300e4f5c0e0e84a54e00bbf02fee6763f9 100644 (file)
@@ -2,7 +2,7 @@
 
 #include "group-record.h"
 #include "strv.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 #include "user-util.h"
 
 GroupRecord* group_record_new(void) {
index 4bd547a6c61681e03a4e40ae5ce4838de652476d..654c4d3588169d9f04a4a1b0f02ce4a1e59978e3 100644 (file)
@@ -16,7 +16,7 @@
 #include "rlimit-util.h"
 #include "string-table.h"
 #include "strv.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 #include "user-record.h"
 #include "user-util.h"
 
index e33b0200fd91a2e65a6709b2128881078ffc128d..e7dcb865c53f0d944fe7616a6a235d289dfafa6b 100644 (file)
@@ -35,7 +35,7 @@
 #include "strv.h"
 #include "sync-util.h"
 #include "tmpfile-util-label.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 #include "uid-range.h"
 #include "user-util.h"
 #include "utf8.h"
index 0c3cb5af8809c6f3766e0a37e6d749e67eddc012..49b021cb8abac5dd2cdc746c9254b07b02cb61ca 100644 (file)
@@ -171,7 +171,7 @@ simple_tests += files(
         'test-terminal-util.c',
         'test-tmpfile-util.c',
         'test-udev-util.c',
-        'test-uid-alloc-range.c',
+        'test-uid-classification.c',
         'test-uid-range.c',
         'test-umask-util.c',
         'test-unaligned.c',
index 66208463606c8a04b1c1ad2a1350e2bffe307abd..be83690ee506a4282a2396848dfa7dcb58d03f28 100644 (file)
@@ -41,7 +41,7 @@
 #include "tests.h"
 #include "tmpfile-util.h"
 #include "tomoyo-util.h"
-#include "uid-alloc-range.h"
+#include "uid-classification.h"
 #include "user-util.h"
 #include "virt.h"
 
diff --git a/src/test/test-uid-alloc-range.c b/src/test/test-uid-alloc-range.c
deleted file mode 100644 (file)
index cd06463..0000000
+++ /dev/null
@@ -1,93 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include <unistd.h>
-#include <sys/types.h>
-
-#include "fd-util.h"
-#include "fileio.h"
-#include "format-util.h"
-#include "fs-util.h"
-#include "tests.h"
-#include "tmpfile-util.h"
-#include "uid-alloc-range.h"
-
-static void test_read_login_defs_one(const char *path) {
-        log_info("/* %s(\"%s\") */", __func__, path ?: "<custom>");
-
-        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-user-record.XXXXXX";
-        _cleanup_fclose_ FILE *f = NULL;
-        if (!path) {
-                assert_se(fmkostemp_safe(name, "r+", &f) == 0);
-                fprintf(f,
-                        "SYS_UID_MIN "UID_FMT"\n"
-                        "SYS_UID_MAX "UID_FMT"\n"
-                        "SYS_GID_MIN "GID_FMT"\n"
-                        "SYS_GID_MAX "GID_FMT"\n",
-                        (uid_t) (SYSTEM_ALLOC_UID_MIN + 5),
-                        (uid_t) (SYSTEM_UID_MAX + 5),
-                        (gid_t) (SYSTEM_ALLOC_GID_MIN + 5),
-                        (gid_t) (SYSTEM_GID_MAX + 5));
-                assert_se(fflush_and_check(f) >= 0);
-        }
-
-        UGIDAllocationRange defs;
-        assert_se(read_login_defs(&defs, path ?: name, NULL) >= 0);
-
-        log_info("system_alloc_uid_min="UID_FMT, defs.system_alloc_uid_min);
-        log_info("system_uid_max="UID_FMT, defs.system_uid_max);
-        log_info("system_alloc_gid_min="GID_FMT, defs.system_alloc_gid_min);
-        log_info("system_gid_max="GID_FMT, defs.system_gid_max);
-
-        if (!path) {
-                uid_t offset = ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES ? 5 : 0;
-                assert_se(defs.system_alloc_uid_min == SYSTEM_ALLOC_UID_MIN + offset);
-                assert_se(defs.system_uid_max == SYSTEM_UID_MAX + offset);
-                assert_se(defs.system_alloc_gid_min == SYSTEM_ALLOC_GID_MIN + offset);
-                assert_se(defs.system_gid_max == SYSTEM_GID_MAX + offset);
-        } else if (streq(path, "/dev/null")) {
-                assert_se(defs.system_alloc_uid_min == SYSTEM_ALLOC_UID_MIN);
-                assert_se(defs.system_uid_max == SYSTEM_UID_MAX);
-                assert_se(defs.system_alloc_gid_min == SYSTEM_ALLOC_GID_MIN);
-                assert_se(defs.system_gid_max == SYSTEM_GID_MAX);
-        }
-}
-
-TEST(read_login_defs) {
-        test_read_login_defs_one("/dev/null");
-        test_read_login_defs_one("/etc/login.defs");
-        test_read_login_defs_one(NULL);
-}
-
-TEST(acquire_ugid_allocation_range) {
-        const UGIDAllocationRange *defs;
-        assert_se(defs = acquire_ugid_allocation_range());
-
-        log_info("system_alloc_uid_min="UID_FMT, defs->system_alloc_uid_min);
-        log_info("system_uid_max="UID_FMT, defs->system_uid_max);
-        log_info("system_alloc_gid_min="GID_FMT, defs->system_alloc_gid_min);
-        log_info("system_gid_max="GID_FMT, defs->system_gid_max);
-}
-
-TEST(uid_is_system) {
-        uid_t uid = 0;
-        log_info("uid_is_system("UID_FMT") = %s", uid, yes_no(uid_is_system(uid)));
-
-        uid = 999;
-        log_info("uid_is_system("UID_FMT") = %s", uid, yes_no(uid_is_system(uid)));
-
-        uid = getuid();
-        log_info("uid_is_system("UID_FMT") = %s", uid, yes_no(uid_is_system(uid)));
-}
-
-TEST(gid_is_system) {
-        gid_t gid = 0;
-        log_info("gid_is_system("GID_FMT") = %s", gid, yes_no(gid_is_system(gid)));
-
-        gid = 999;
-        log_info("gid_is_system("GID_FMT") = %s", gid, yes_no(gid_is_system(gid)));
-
-        gid = getgid();
-        log_info("gid_is_system("GID_FMT") = %s", gid, yes_no(gid_is_system(gid)));
-}
-
-DEFINE_TEST_MAIN(LOG_DEBUG);
diff --git a/src/test/test-uid-classification.c b/src/test/test-uid-classification.c
new file mode 100644 (file)
index 0000000..9c7500a
--- /dev/null
@@ -0,0 +1,93 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <unistd.h>
+#include <sys/types.h>
+
+#include "fd-util.h"
+#include "fileio.h"
+#include "format-util.h"
+#include "fs-util.h"
+#include "tests.h"
+#include "tmpfile-util.h"
+#include "uid-classification.h"
+
+static void test_read_login_defs_one(const char *path) {
+        log_info("/* %s(\"%s\") */", __func__, path ?: "<custom>");
+
+        _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-user-record.XXXXXX";
+        _cleanup_fclose_ FILE *f = NULL;
+        if (!path) {
+                assert_se(fmkostemp_safe(name, "r+", &f) == 0);
+                fprintf(f,
+                        "SYS_UID_MIN "UID_FMT"\n"
+                        "SYS_UID_MAX "UID_FMT"\n"
+                        "SYS_GID_MIN "GID_FMT"\n"
+                        "SYS_GID_MAX "GID_FMT"\n",
+                        (uid_t) (SYSTEM_ALLOC_UID_MIN + 5),
+                        (uid_t) (SYSTEM_UID_MAX + 5),
+                        (gid_t) (SYSTEM_ALLOC_GID_MIN + 5),
+                        (gid_t) (SYSTEM_GID_MAX + 5));
+                assert_se(fflush_and_check(f) >= 0);
+        }
+
+        UGIDAllocationRange defs;
+        assert_se(read_login_defs(&defs, path ?: name, NULL) >= 0);
+
+        log_info("system_alloc_uid_min="UID_FMT, defs.system_alloc_uid_min);
+        log_info("system_uid_max="UID_FMT, defs.system_uid_max);
+        log_info("system_alloc_gid_min="GID_FMT, defs.system_alloc_gid_min);
+        log_info("system_gid_max="GID_FMT, defs.system_gid_max);
+
+        if (!path) {
+                uid_t offset = ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES ? 5 : 0;
+                assert_se(defs.system_alloc_uid_min == SYSTEM_ALLOC_UID_MIN + offset);
+                assert_se(defs.system_uid_max == SYSTEM_UID_MAX + offset);
+                assert_se(defs.system_alloc_gid_min == SYSTEM_ALLOC_GID_MIN + offset);
+                assert_se(defs.system_gid_max == SYSTEM_GID_MAX + offset);
+        } else if (streq(path, "/dev/null")) {
+                assert_se(defs.system_alloc_uid_min == SYSTEM_ALLOC_UID_MIN);
+                assert_se(defs.system_uid_max == SYSTEM_UID_MAX);
+                assert_se(defs.system_alloc_gid_min == SYSTEM_ALLOC_GID_MIN);
+                assert_se(defs.system_gid_max == SYSTEM_GID_MAX);
+        }
+}
+
+TEST(read_login_defs) {
+        test_read_login_defs_one("/dev/null");
+        test_read_login_defs_one("/etc/login.defs");
+        test_read_login_defs_one(NULL);
+}
+
+TEST(acquire_ugid_allocation_range) {
+        const UGIDAllocationRange *defs;
+        assert_se(defs = acquire_ugid_allocation_range());
+
+        log_info("system_alloc_uid_min="UID_FMT, defs->system_alloc_uid_min);
+        log_info("system_uid_max="UID_FMT, defs->system_uid_max);
+        log_info("system_alloc_gid_min="GID_FMT, defs->system_alloc_gid_min);
+        log_info("system_gid_max="GID_FMT, defs->system_gid_max);
+}
+
+TEST(uid_is_system) {
+        uid_t uid = 0;
+        log_info("uid_is_system("UID_FMT") = %s", uid, yes_no(uid_is_system(uid)));
+
+        uid = 999;
+        log_info("uid_is_system("UID_FMT") = %s", uid, yes_no(uid_is_system(uid)));
+
+        uid = getuid();
+        log_info("uid_is_system("UID_FMT") = %s", uid, yes_no(uid_is_system(uid)));
+}
+
+TEST(gid_is_system) {
+        gid_t gid = 0;
+        log_info("gid_is_system("GID_FMT") = %s", gid, yes_no(gid_is_system(gid)));
+
+        gid = 999;
+        log_info("gid_is_system("GID_FMT") = %s", gid, yes_no(gid_is_system(gid)));
+
+        gid = getgid();
+        log_info("gid_is_system("GID_FMT") = %s", gid, yes_no(gid_is_system(gid)));
+}
+
+DEFINE_TEST_MAIN(LOG_DEBUG);