]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared: split out UID allocation range stuff from user-record.h
authorLennart Poettering <lennart@poettering.net>
Fri, 12 Nov 2021 17:50:44 +0000 (18:50 +0100)
committerLennart Poettering <lennart@poettering.net>
Sat, 13 Nov 2021 07:10:11 +0000 (08:10 +0100)
user-record.[ch] are about the UserRecord JSON stuff, and the UID
allocation range stuff (i.e. login.defs handling) is a very different
thing, and complex enough on its own, let's give it its own c/h files.

No code changes, just some splitting out of code.

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

index 2672496724c72e2012ed7b12b4b7f383839e2e16..04d8e7c5e39d837634ba33f8f5552ad4c9679dd6 100644 (file)
@@ -19,7 +19,7 @@
 #include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
-#include "user-record.h"
+#include "uid-alloc-range.h"
 #include "user-util.h"
 
 /* Takes a value generated randomly or by hashing and turns it into a UID in the right range */
index a88f4955715d6d862edd5c9c335f948fbf84c849..62a622a6cab14c3d9f28e9e763190ba20516b796 100644 (file)
@@ -50,7 +50,7 @@
 #include "strv.h"
 #include "sync-util.h"
 #include "tmpfile-util.h"
-#include "user-record.h"
+#include "uid-alloc-range.h"
 #include "user-util.h"
 
 /* The maximum size up to which we process coredumps */
index 21c12816c4d588bc431909862acd222e2e0e0b0d..a4f3116544d2b7d019cf782c35250edc3914fad3 100644 (file)
@@ -36,6 +36,7 @@
 #include "rlimit-util.h"
 #include "spawn-polkit-agent.h"
 #include "terminal-util.h"
+#include "uid-alloc-range.h"
 #include "user-record-pwquality.h"
 #include "user-record-show.h"
 #include "user-record-util.h"
index 2b4e5dde8f7bd255cd5d8ea06269a11455b07a07..a31e27d0511e68a5415adc232a27b9f64dc00063 100644 (file)
@@ -34,6 +34,7 @@
 #include "stat-util.h"
 #include "string-table.h"
 #include "strv.h"
+#include "uid-alloc-range.h"
 #include "user-record-pwquality.h"
 #include "user-record-sign.h"
 #include "user-record-util.h"
index fc7bdb87372f5b05f602e0275538172408b85328..ed53d320059c6d5884e07278e5b7c1267c10a48a 100644 (file)
@@ -55,7 +55,7 @@
 #include "string-table.h"
 #include "string-util.h"
 #include "syslog-util.h"
-#include "user-record.h"
+#include "uid-alloc-range.h"
 #include "user-util.h"
 
 #define USER_JOURNALS_MAX 1024
index 1c8d84329f87081dc05fe649e41217901acd642d..5266f557752ce5842b3f68e5a24633a38080d4ee 100644 (file)
@@ -32,6 +32,7 @@
 #include "string-table.h"
 #include "strv.h"
 #include "tmpfile-util.h"
+#include "uid-alloc-range.h"
 #include "unit-name.h"
 #include "user-util.h"
 #include "util.h"
index 3e6ae79553507e74841f250d999f977740f9c636..6e769e9d59b0ce21ff2e5e78f796c548b1d3e929 100644 (file)
@@ -47,7 +47,7 @@
 #include "string-table.h"
 #include "string-util.h"
 #include "tomoyo-util.h"
-#include "user-record.h"
+#include "uid-alloc-range.h"
 #include "user-util.h"
 #include "util.h"
 #include "virt.h"
index a13c06fd8800e129774137c4d26ff963754dbc7d..2f12ac1c22010ad46bd2f98e582ff09a1048bfef 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "group-record.h"
 #include "strv.h"
+#include "uid-alloc-range.h"
 #include "user-util.h"
 
 GroupRecord* group_record_new(void) {
index 229e58bebab7afe11aaa9d464dd6f6909994ef6a..1fd1d711b01f3d5ca04825b0bf7191378af9cd9c 100644 (file)
@@ -292,6 +292,8 @@ shared_sources = files('''
         tpm2-util.h
         udev-util.c
         udev-util.h
+        uid-alloc-range.c
+        uid-alloc-range.h
         uid-range.c
         uid-range.h
         user-record-nss.c
diff --git a/src/shared/uid-alloc-range.c b/src/shared/uid-alloc-range.c
new file mode 100644 (file)
index 0000000..9615183
--- /dev/null
@@ -0,0 +1,124 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "chase-symlinks.h"
+#include "fd-util.h"
+#include "fileio.h"
+#include "string-util.h"
+#include "uid-alloc-range.h"
+#include "user-util.h"
+
+#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) {
+        UGIDAllocationRange defs = {
+                .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
+        _cleanup_fclose_ FILE *f = NULL;
+        int r;
+
+        if (!path)
+                path = "/etc/login.defs";
+
+        r = chase_symlinks_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT, "re", NULL, &f);
+        if (r == -ENOENT)
+                goto assign;
+        if (r < 0)
+                return log_debug_errno(r, "Failed to open %s: %m", path);
+
+        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);
+        }
+
+ assign:
+        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. */
+        }
+#endif
+
+        *ret_defs = defs;
+        return 0;
+}
+
+const UGIDAllocationRange *acquire_ugid_allocation_range(void) {
+#if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
+        static thread_local UGIDAllocationRange defs = {
+#else
+        static const UGIDAllocationRange defs = {
+#endif
+                .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
+        /* 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. */
+
+        static thread_local bool initialized = false;
+
+        if (!initialized) {
+                (void) read_login_defs(&defs, NULL, NULL);
+                initialized = true;
+        }
+#endif
+
+        return &defs;
+}
+
+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;
+}
diff --git a/src/shared/uid-alloc-range.h b/src/shared/uid-alloc-range.h
new file mode 100644 (file)
index 0000000..d3bf077
--- /dev/null
@@ -0,0 +1,34 @@
+/* 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);
index 2fbe6ad5bd26890f1c4f109d71254b68d1653c95..b68b6a98d2687584ccee1d612ede2aa08e06f2dc 100644 (file)
@@ -3,11 +3,8 @@
 #include <sys/mount.h>
 
 #include "cgroup-util.h"
-#include "chase-symlinks.h"
 #include "dns-domain.h"
 #include "env-util.h"
-#include "fd-util.h"
-#include "fileio.h"
 #include "fs-util.h"
 #include "hexdecoct.h"
 #include "hostname-util.h"
 #include "path-util.h"
 #include "pkcs11-util.h"
 #include "rlimit-util.h"
-#include "stat-util.h"
 #include "string-table.h"
 #include "strv.h"
+#include "uid-alloc-range.h"
 #include "user-record.h"
 #include "user-util.h"
 
 #define DEFAULT_RATELIMIT_BURST 30
 #define DEFAULT_RATELIMIT_INTERVAL_USEC (1*USEC_PER_MINUTE)
 
-#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) {
-        UGIDAllocationRange defs = {
-                .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
-        _cleanup_fclose_ FILE *f = NULL;
-        int r;
-
-        if (!path)
-                path = "/etc/login.defs";
-
-        r = chase_symlinks_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT, "re", NULL, &f);
-        if (r == -ENOENT)
-                goto assign;
-        if (r < 0)
-                return log_debug_errno(r, "Failed to open %s: %m", path);
-
-        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);
-        }
-
- assign:
-        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. */
-        }
-#endif
-
-        *ret_defs = defs;
-        return 0;
-}
-
-const UGIDAllocationRange *acquire_ugid_allocation_range(void) {
-#if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
-        static thread_local UGIDAllocationRange defs = {
-#else
-        static const UGIDAllocationRange defs = {
-#endif
-                .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
-        /* 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. */
-
-        static thread_local bool initialized = false;
-
-        if (!initialized) {
-                (void) read_login_defs(&defs, NULL, NULL);
-                initialized = true;
-        }
-#endif
-
-        return &defs;
-}
-
-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;
-}
-
 UserRecord* user_record_new(void) {
         UserRecord *h;
 
index bc160a0a5ec2267e8f902f7d4395206e4116c4b0..c72bef4a7250212bb90744a6235ceb504bcf8f7f 100644 (file)
 /* The default disk size to use when nothing else is specified, relative to free disk space */
 #define USER_DISK_SIZE_DEFAULT_PERCENT 85
 
-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);
-
 typedef enum UserDisposition {
         USER_INTRINSIC,   /* root and nobody */
         USER_SYSTEM,      /* statically allocated users for system services */
index 106a602305cdb3bc266682e75b05c60f7d57c5d8..0e60f2c00fe938a9169d5a7bcfaf54cf425740c2 100644 (file)
@@ -31,8 +31,8 @@
 #include "strv.h"
 #include "sync-util.h"
 #include "tmpfile-util-label.h"
+#include "uid-alloc-range.h"
 #include "uid-range.h"
-#include "user-record.h"
 #include "user-util.h"
 #include "utf8.h"
 #include "util.h"
index d7cd63da815f0fdea7e40be79a50d03304397077..f7fa4ef2b199613c929f04fc3dec12797f40a212 100644 (file)
@@ -32,7 +32,7 @@
 #include "strv.h"
 #include "tests.h"
 #include "tomoyo-util.h"
-#include "user-record.h"
+#include "uid-alloc-range.h"
 #include "user-util.h"
 #include "virt.h"
 
index c9182e382bda184f48fcf372d47bf212f1e938d3..8fa0b165d47f6189454a793357937cf557372cce 100644 (file)
@@ -7,9 +7,9 @@
 #include "fileio.h"
 #include "format-util.h"
 #include "fs-util.h"
-#include "tmpfile-util.h"
 #include "tests.h"
-#include "user-record.h"
+#include "tmpfile-util.h"
+#include "uid-alloc-range.h"
 
 static void test_read_login_defs(const char *path) {
         log_info("/* %s(\"%s\") */", __func__, path ?: "<custom>");