| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <gshadow.h> |
| 4 |
#include <pwd.h> |
| 5 |
#include <stdlib.h> |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "dlfcn-util.h" |
| 9 |
#include "errno-list.h" |
| 10 |
#include "format-util.h" |
| 11 |
#include "log.h" |
| 12 |
#include "main-func.h" |
| 13 |
#include "nss-test-util.h" |
| 14 |
#include "nss-util.h" |
| 15 |
#include "parse-util.h" |
| 16 |
#include "path-util.h" |
| 17 |
#include "string-util.h" |
| 18 |
#include "strv.h" |
| 19 |
#include "tests.h" |
| 20 |
|
| 21 |
static size_t arg_bufsize = 1024; |
| 22 |
|
| 23 |
static void print_struct_passwd(const struct passwd *pwd) { |
| 24 |
log_info(" \"%s\" / "UID_FMT":"GID_FMT, |
| 25 |
pwd->pw_name, pwd->pw_uid, pwd->pw_gid); |
| 26 |
log_info(" passwd=\"%s\"", pwd->pw_passwd); |
| 27 |
log_info(" gecos=\"%s\"", pwd->pw_gecos); |
| 28 |
log_info(" dir=\"%s\"", pwd->pw_dir); |
| 29 |
log_info(" shell=\"%s\"", pwd->pw_shell); |
| 30 |
} |
| 31 |
|
| 32 |
static void print_struct_group(const struct group *gr) { |
| 33 |
_cleanup_free_ char *members = NULL; |
| 34 |
|
| 35 |
log_info(" \"%s\" / "GID_FMT, |
| 36 |
gr->gr_name, gr->gr_gid); |
| 37 |
log_info(" passwd=\"%s\"", gr->gr_passwd); |
| 38 |
|
| 39 |
assert_se(members = strv_join(ASSERT_PTR(gr->gr_mem), ", ")); |
| 40 |
// FIXME: use shell_maybe_quote(SHELL_ESCAPE_EMPTY) when it becomes available |
| 41 |
log_info(" members=%s", members); |
| 42 |
} |
| 43 |
|
| 44 |
static void print_struct_sgrp(const struct sgrp *sg) { |
| 45 |
_cleanup_free_ char *administrators = NULL, *members = NULL; |
| 46 |
|
| 47 |
log_info(" \"%s\"", sg->sg_namp); |
| 48 |
log_info(" passwd=\"%s\"", sg->sg_passwd); |
| 49 |
|
| 50 |
assert_se(administrators = strv_join(ASSERT_PTR(sg->sg_adm), ", ")); |
| 51 |
// FIXME: use shell_maybe_quote(SHELL_ESCAPE_EMPTY) when it becomes available |
| 52 |
log_info(" administrators=%s", administrators); |
| 53 |
|
| 54 |
assert_se(members = strv_join(ASSERT_PTR(sg->sg_mem), ", ")); |
| 55 |
// FIXME: use shell_maybe_quote(SHELL_ESCAPE_EMPTY) when it becomes available |
| 56 |
log_info(" members=%s", members); |
| 57 |
} |
| 58 |
|
| 59 |
static void test_getpwnam_r(void *handle, const char *module, const char *name) { |
| 60 |
const char *fname; |
| 61 |
_nss_getpwnam_r_t f; |
| 62 |
char buffer[arg_bufsize]; |
| 63 |
int errno1 = 999; /* nss-dns doesn't set those */ |
| 64 |
enum nss_status status; |
| 65 |
char pretty_status[DECIMAL_STR_MAX(enum nss_status)]; |
| 66 |
struct passwd pwd; |
| 67 |
|
| 68 |
fname = strjoina("_nss_", module, "_getpwnam_r"); |
| 69 |
f = dlsym(handle, fname); |
| 70 |
log_debug("dlsym(0x%p, %s) → 0x%p", handle, fname, f); |
| 71 |
if (!f) { |
| 72 |
log_info("%s not defined", fname); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
status = f(name, &pwd, buffer, sizeof buffer, &errno1); |
| 77 |
log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s", |
| 78 |
fname, name, |
| 79 |
nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n", |
| 80 |
errno1, errno1 > 0 ? ERRNO_NAME(errno1) : "---"); |
| 81 |
if (status == NSS_STATUS_SUCCESS) |
| 82 |
print_struct_passwd(&pwd); |
| 83 |
} |
| 84 |
|
| 85 |
static void test_getgrnam_r(void *handle, const char *module, const char *name) { |
| 86 |
const char *fname; |
| 87 |
_nss_getgrnam_r_t f; |
| 88 |
char buffer[arg_bufsize]; |
| 89 |
int errno1 = 999; /* nss-dns doesn't set those */ |
| 90 |
enum nss_status status; |
| 91 |
char pretty_status[DECIMAL_STR_MAX(enum nss_status)]; |
| 92 |
struct group gr; |
| 93 |
|
| 94 |
fname = strjoina("_nss_", module, "_getgrnam_r"); |
| 95 |
f = dlsym(handle, fname); |
| 96 |
log_debug("dlsym(0x%p, %s) → 0x%p", handle, fname, f); |
| 97 |
if (!f) { |
| 98 |
log_info("%s not defined", fname); |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
status = f(name, &gr, buffer, sizeof buffer, &errno1); |
| 103 |
log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s", |
| 104 |
fname, name, |
| 105 |
nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n", |
| 106 |
errno1, errno1 > 0 ? ERRNO_NAME(errno1) : "---"); |
| 107 |
if (status == NSS_STATUS_SUCCESS) |
| 108 |
print_struct_group(&gr); |
| 109 |
} |
| 110 |
|
| 111 |
static void test_getsgnam_r(void *handle, const char *module, const char *name) { |
| 112 |
const char *fname; |
| 113 |
_nss_getsgnam_r_t f; |
| 114 |
char buffer[arg_bufsize]; |
| 115 |
int errno1 = 999; /* nss-dns doesn't set those */ |
| 116 |
enum nss_status status; |
| 117 |
char pretty_status[DECIMAL_STR_MAX(enum nss_status)]; |
| 118 |
struct sgrp sg; |
| 119 |
|
| 120 |
fname = strjoina("_nss_", module, "_getsgnam_r"); |
| 121 |
f = dlsym(handle, fname); |
| 122 |
log_debug("dlsym(0x%p, %s) → 0x%p", handle, fname, f); |
| 123 |
if (!f) { |
| 124 |
log_info("%s not defined", fname); |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
status = f(name, &sg, buffer, sizeof buffer, &errno1); |
| 129 |
log_info("%s(\"%s\") → status=%s%-20serrno=%d/%s", |
| 130 |
fname, name, |
| 131 |
nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n", |
| 132 |
errno1, errno1 > 0 ? ERRNO_NAME(errno1) : "---"); |
| 133 |
if (status == NSS_STATUS_SUCCESS) |
| 134 |
print_struct_sgrp(&sg); |
| 135 |
} |
| 136 |
|
| 137 |
static void test_getpwuid_r(void *handle, const char *module, uid_t uid) { |
| 138 |
const char *fname; |
| 139 |
_nss_getpwuid_r_t f; |
| 140 |
char buffer[arg_bufsize]; |
| 141 |
int errno1 = 999; /* nss-dns doesn't set those */ |
| 142 |
enum nss_status status; |
| 143 |
char pretty_status[DECIMAL_STR_MAX(enum nss_status)]; |
| 144 |
struct passwd pwd; |
| 145 |
|
| 146 |
fname = strjoina("_nss_", module, "_getpwuid_r"); |
| 147 |
f = dlsym(handle, fname); |
| 148 |
log_debug("dlsym(0x%p, %s) → 0x%p", handle, fname, f); |
| 149 |
if (!f) { |
| 150 |
log_info("%s not defined", fname); |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
status = f(uid, &pwd, buffer, sizeof buffer, &errno1); |
| 155 |
log_info("%s("UID_FMT") → status=%s%-20serrno=%d/%s", |
| 156 |
fname, uid, |
| 157 |
nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n", |
| 158 |
errno1, errno1 > 0 ? ERRNO_NAME(errno1) : "---"); |
| 159 |
if (status == NSS_STATUS_SUCCESS) |
| 160 |
print_struct_passwd(&pwd); |
| 161 |
} |
| 162 |
|
| 163 |
static void test_getgrgid_r(void *handle, const char *module, gid_t gid) { |
| 164 |
const char *fname; |
| 165 |
_nss_getgrgid_r_t f; |
| 166 |
char buffer[arg_bufsize]; |
| 167 |
int errno1 = 999; /* nss-dns doesn't set those */ |
| 168 |
enum nss_status status; |
| 169 |
char pretty_status[DECIMAL_STR_MAX(enum nss_status)]; |
| 170 |
struct group gr; |
| 171 |
|
| 172 |
fname = strjoina("_nss_", module, "_getgrgid_r"); |
| 173 |
f = dlsym(handle, fname); |
| 174 |
log_debug("dlsym(0x%p, %s) → 0x%p", handle, fname, f); |
| 175 |
if (!f) { |
| 176 |
log_info("%s not defined", fname); |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
status = f(gid, &gr, buffer, sizeof buffer, &errno1); |
| 181 |
log_info("%s("GID_FMT") → status=%s%-20serrno=%d/%s", |
| 182 |
fname, gid, |
| 183 |
nss_status_to_string(status, pretty_status, sizeof pretty_status), "\n", |
| 184 |
errno1, errno1 > 0 ? ERRNO_NAME(errno1) : "---"); |
| 185 |
if (status == NSS_STATUS_SUCCESS) |
| 186 |
print_struct_group(&gr); |
| 187 |
} |
| 188 |
|
| 189 |
static void test_byname(void *handle, const char *module, const char *name) { |
| 190 |
test_getpwnam_r(handle, module, name); |
| 191 |
test_getgrnam_r(handle, module, name); |
| 192 |
test_getsgnam_r(handle, module, name); |
| 193 |
puts(""); |
| 194 |
} |
| 195 |
|
| 196 |
static void test_byuid(void *handle, const char *module, uid_t uid) { |
| 197 |
test_getpwuid_r(handle, module, uid); |
| 198 |
test_getgrgid_r(handle, module, uid); |
| 199 |
puts(""); |
| 200 |
} |
| 201 |
|
| 202 |
static int test_one_module(const char *dir, |
| 203 |
const char *module, |
| 204 |
char **names) { |
| 205 |
|
| 206 |
log_info("======== %s ========", module); |
| 207 |
|
| 208 |
_cleanup_(dlclosep) void *handle = nss_open_handle(dir, module, RTLD_NOW|RTLD_NODELETE); |
| 209 |
if (!handle) |
| 210 |
return -EINVAL; |
| 211 |
|
| 212 |
STRV_FOREACH(name, names) |
| 213 |
test_byname(handle, module, *name); |
| 214 |
|
| 215 |
STRV_FOREACH(name, names) { |
| 216 |
uid_t uid; |
| 217 |
|
| 218 |
assert_cc(sizeof(uid_t) == sizeof(uint32_t)); |
| 219 |
/* We use safe_atou32 because we don't want to refuse invalid uids. */ |
| 220 |
if (safe_atou32(*name, &uid) < 0) |
| 221 |
continue; |
| 222 |
|
| 223 |
test_byuid(handle, module, uid); |
| 224 |
} |
| 225 |
|
| 226 |
log_info(" "); |
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
static int parse_argv(int argc, char **argv, |
| 231 |
char ***the_modules, |
| 232 |
char ***the_names) { |
| 233 |
|
| 234 |
_cleanup_strv_free_ char **modules = NULL, **names = NULL; |
| 235 |
const char *p; |
| 236 |
int r; |
| 237 |
|
| 238 |
p = getenv("SYSTEMD_TEST_NSS_BUFSIZE"); |
| 239 |
if (p) { |
| 240 |
r = safe_atozu(p, &arg_bufsize); |
| 241 |
if (r < 0) |
| 242 |
return log_error_errno(r, "Failed to parse $SYSTEMD_TEST_NSS_BUFSIZE"); |
| 243 |
} |
| 244 |
|
| 245 |
if (argc > 1) |
| 246 |
modules = strv_new(argv[1]); |
| 247 |
else |
| 248 |
modules = strv_new( |
| 249 |
#if ENABLE_NSS_SYSTEMD |
| 250 |
"systemd", |
| 251 |
#endif |
| 252 |
#if ENABLE_NSS_MYMACHINES |
| 253 |
"mymachines", |
| 254 |
#endif |
| 255 |
NULL); |
| 256 |
assert_se(modules); |
| 257 |
|
| 258 |
if (argc > 2) |
| 259 |
names = strv_copy(strv_skip(argv, 2)); |
| 260 |
else |
| 261 |
names = strv_new("root", |
| 262 |
NOBODY_USER_NAME, |
| 263 |
"foo_no_such_user", |
| 264 |
"0", |
| 265 |
"65534"); |
| 266 |
assert_se(names); |
| 267 |
|
| 268 |
*the_modules = TAKE_PTR(modules); |
| 269 |
*the_names = TAKE_PTR(names); |
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
static int run(int argc, char **argv) { |
| 274 |
_cleanup_free_ char *dir = NULL; |
| 275 |
_cleanup_strv_free_ char **modules = NULL, **names = NULL; |
| 276 |
int r; |
| 277 |
|
| 278 |
test_setup_logging(LOG_INFO); |
| 279 |
|
| 280 |
r = parse_argv(argc, argv, &modules, &names); |
| 281 |
if (r < 0) |
| 282 |
return log_error_errno(r, "Failed to parse arguments: %m"); |
| 283 |
|
| 284 |
assert_se(path_extract_directory(argv[0], &dir) >= 0); |
| 285 |
|
| 286 |
STRV_FOREACH(module, modules) { |
| 287 |
r = test_one_module(dir, *module, names); |
| 288 |
if (r < 0) |
| 289 |
return r; |
| 290 |
} |
| 291 |
|
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
DEFINE_MAIN_FUNCTION(run); |