| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "fd-util.h" |
| 6 |
#include "fileio.h" |
| 7 |
#include "format-util.h" |
| 8 |
#include "tests.h" |
| 9 |
#include "tmpfile-util.h" |
| 10 |
#include "uid-classification.h" |
| 11 |
|
| 12 |
static void test_read_login_defs_one(const char *path) { |
| 13 |
log_info("/* %s(\"%s\") */", __func__, path ?: "<custom>"); |
| 14 |
|
| 15 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-user-record.XXXXXX"; |
| 16 |
_cleanup_fclose_ FILE *f = NULL; |
| 17 |
if (!path) { |
| 18 |
assert_se(fmkostemp_safe(name, "r+", &f) == 0); |
| 19 |
fprintf(f, |
| 20 |
"SYS_UID_MIN "UID_FMT"\n" |
| 21 |
"SYS_UID_MAX "UID_FMT"\n" |
| 22 |
"SYS_GID_MIN "GID_FMT"\n" |
| 23 |
"SYS_GID_MAX "GID_FMT"\n", |
| 24 |
(uid_t) (SYSTEM_ALLOC_UID_MIN + 5), |
| 25 |
(uid_t) (SYSTEM_UID_MAX + 5), |
| 26 |
(gid_t) (SYSTEM_ALLOC_GID_MIN + 5), |
| 27 |
(gid_t) (SYSTEM_GID_MAX + 5)); |
| 28 |
assert_se(fflush_and_check(f) >= 0); |
| 29 |
} |
| 30 |
|
| 31 |
UGIDAllocationRange defs; |
| 32 |
assert_se(read_login_defs(&defs, path ?: name, NULL) >= 0); |
| 33 |
|
| 34 |
log_info("system_alloc_uid_min="UID_FMT, defs.system_alloc_uid_min); |
| 35 |
log_info("system_uid_max="UID_FMT, defs.system_uid_max); |
| 36 |
log_info("system_alloc_gid_min="GID_FMT, defs.system_alloc_gid_min); |
| 37 |
log_info("system_gid_max="GID_FMT, defs.system_gid_max); |
| 38 |
|
| 39 |
if (!path) { |
| 40 |
uid_t offset = ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES ? 5 : 0; |
| 41 |
assert_se(defs.system_alloc_uid_min == SYSTEM_ALLOC_UID_MIN + offset); |
| 42 |
assert_se(defs.system_uid_max == SYSTEM_UID_MAX + offset); |
| 43 |
assert_se(defs.system_alloc_gid_min == SYSTEM_ALLOC_GID_MIN + offset); |
| 44 |
assert_se(defs.system_gid_max == SYSTEM_GID_MAX + offset); |
| 45 |
} else if (streq(path, "/dev/null")) { |
| 46 |
assert_se(defs.system_alloc_uid_min == SYSTEM_ALLOC_UID_MIN); |
| 47 |
assert_se(defs.system_uid_max == SYSTEM_UID_MAX); |
| 48 |
assert_se(defs.system_alloc_gid_min == SYSTEM_ALLOC_GID_MIN); |
| 49 |
assert_se(defs.system_gid_max == SYSTEM_GID_MAX); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
TEST(read_login_defs) { |
| 54 |
test_read_login_defs_one("/dev/null"); |
| 55 |
test_read_login_defs_one("/etc/login.defs"); |
| 56 |
test_read_login_defs_one(NULL); |
| 57 |
} |
| 58 |
|
| 59 |
TEST(acquire_ugid_allocation_range) { |
| 60 |
const UGIDAllocationRange *defs; |
| 61 |
assert_se(defs = acquire_ugid_allocation_range()); |
| 62 |
|
| 63 |
log_info("system_alloc_uid_min="UID_FMT, defs->system_alloc_uid_min); |
| 64 |
log_info("system_uid_max="UID_FMT, defs->system_uid_max); |
| 65 |
log_info("system_alloc_gid_min="GID_FMT, defs->system_alloc_gid_min); |
| 66 |
log_info("system_gid_max="GID_FMT, defs->system_gid_max); |
| 67 |
} |
| 68 |
|
| 69 |
TEST(uid_is_system) { |
| 70 |
uid_t uid = 0; |
| 71 |
log_info("uid_is_system("UID_FMT") = %s", uid, yes_no(uid_is_system(uid))); |
| 72 |
|
| 73 |
uid = 999; |
| 74 |
log_info("uid_is_system("UID_FMT") = %s", uid, yes_no(uid_is_system(uid))); |
| 75 |
|
| 76 |
uid = getuid(); |
| 77 |
log_info("uid_is_system("UID_FMT") = %s", uid, yes_no(uid_is_system(uid))); |
| 78 |
} |
| 79 |
|
| 80 |
TEST(gid_is_system) { |
| 81 |
gid_t gid = 0; |
| 82 |
log_info("gid_is_system("GID_FMT") = %s", gid, yes_no(gid_is_system(gid))); |
| 83 |
|
| 84 |
gid = 999; |
| 85 |
log_info("gid_is_system("GID_FMT") = %s", gid, yes_no(gid_is_system(gid))); |
| 86 |
|
| 87 |
gid = getgid(); |
| 88 |
log_info("gid_is_system("GID_FMT") = %s", gid, yes_no(gid_is_system(gid))); |
| 89 |
} |
| 90 |
|
| 91 |
DEFINE_TEST_MAIN(LOG_DEBUG); |