HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/userdb.c
Git blob: e3487fbb628e9f94d4adf0cac82759ccad3094db
Lines 1–1000 of 2,224
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <fnmatch.h>
#include <gshadow.h>
#include <stdlib.h>
#include "sd-event.h"
#include "sd-varlink.h"
#include "alloc-util.h"
#include "bitfield.h"
#include "conf-files.h"
#include "dirent-util.h"
#include "dlfcn-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "format-util.h"
#include "json-util.h"
#include "log.h"
#include "parse-util.h"
#include "set.h"
#include "socket-util.h"
#include "string-util.h"
#include "strv.h"
#include "uid-classification.h"
#include "user-record-nss.h"
#include "user-util.h"
#include "userdb.h"
#include "userdb-dropin.h"
#include "xattr-util.h"
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(link_hash_ops, void, trivial_hash_func, trivial_compare_func, sd_varlink, sd_varlink_unref);
typedef enum LookupWhat {
LOOKUP_USER,
LOOKUP_GROUP,
LOOKUP_MEMBERSHIP,
_LOOKUP_WHAT_MAX,
} LookupWhat;
typedef struct SocketFilter {
/* Restricts the sockets to connect to */
uid_t uid;
gid_t gid;
const char *username;
const char *groupname;
} SocketFilter;
struct UserDBIterator {
LookupWhat what;
UserDBFlags flags;
Set *links;
const char *method; /* Note, this is a const static string! */
sd_json_variant *query;
bool more:1;
bool nss_covered:1;
bool nss_iterating:1;
bool dropin_covered:1;
bool synthesize_root:1;
bool synthesize_nobody:1;
bool nss_systemd_blocked:1;
char **dropins;
size_t current_dropin;
int error;
unsigned n_found;
sd_event *event;
UserRecord *found_user; /* when .what == LOOKUP_USER */
GroupRecord *found_group; /* when .what == LOOKUP_GROUP */
char *found_user_name, *found_group_name; /* when .what == LOOKUP_MEMBERSHIP */
char **members_of_group;
size_t index_members_of_group;
char *filter_user_name, *filter_group_name;
};
static int userdb_connect(UserDBIterator *iterator, const char *path, const char *method, bool more, sd_json_variant *query);
UserDBIterator* userdb_iterator_free(UserDBIterator *iterator) {
if (!iterator)
return NULL;
sd_json_variant_unref(iterator->query);
set_free(iterator->links);
strv_free(iterator->dropins);
switch (iterator->what) {
case LOOKUP_USER:
user_record_unref(iterator->found_user);
if (iterator->nss_iterating)
endpwent();
break;
case LOOKUP_GROUP:
group_record_unref(iterator->found_group);
if (iterator->nss_iterating)
endgrent();
break;
case LOOKUP_MEMBERSHIP:
free(iterator->found_user_name);
free(iterator->found_group_name);
strv_free(iterator->members_of_group);
free(iterator->filter_user_name);
free(iterator->filter_group_name);
if (iterator->nss_iterating)
endgrent();
break;
default:
assert_not_reached();
}
sd_event_unref(iterator->event);
if (iterator->nss_systemd_blocked)
assert_se(userdb_block_nss_systemd(false) >= 0);
return mfree(iterator);
}
static UserDBIterator* userdb_iterator_new(LookupWhat what, UserDBFlags flags) {
UserDBIterator *i;
assert(what >= 0);
assert(what < _LOOKUP_WHAT_MAX);
i = new(UserDBIterator, 1);
if (!i)
return NULL;
*i = (UserDBIterator) {
.what = what,
.flags = flags,
.synthesize_root = !FLAGS_SET(flags, USERDB_DONT_SYNTHESIZE_INTRINSIC),
.synthesize_nobody = !FLAGS_SET(flags, USERDB_DONT_SYNTHESIZE_INTRINSIC),
};
return i;
}
static int userdb_iterator_block_nss_systemd(UserDBIterator *iterator) {
int r;
assert(iterator);
if (iterator->nss_systemd_blocked)
return 0;
r = userdb_block_nss_systemd(true);
if (r < 0)
return r;
iterator->nss_systemd_blocked = true;
return 1;
}
struct user_group_data {
sd_json_variant *record;
bool incomplete;
};
static void user_group_data_done(struct user_group_data *d) {
sd_json_variant_unref(d->record);
}
struct membership_data {
char *user_name;
char *group_name;
};
static void membership_data_done(struct membership_data *d) {
free(d->user_name);
free(d->group_name);
}
static int userdb_maybe_restart_query(
UserDBIterator *iterator,
sd_varlink *link,
sd_json_variant *parameters,
const char *error_id) {
int r;
assert(iterator);
assert(link);
assert(error_id);
/* These fields were added in v258 and didn't exist in previous implementations. Hence, we consider
* their support optional: if any service refuses any of these fields, we'll restart the query
* without them, and apply the filtering they are supposed to do client side. */
static const char *const fields[] = {
"fuzzyNames",
"dispositionMask",
"uuid",
"uidMin",
"uidMax",
"gidMin",
"gidMax",
NULL
};
/* Figure out if the reported error indicates any of the suppressible fields are at fault, and that
* our query actually included them */
bool restart = false;
STRV_FOREACH(f, fields) {
if (!sd_varlink_error_is_invalid_parameter(error_id, parameters, *f))
continue;
if (!sd_json_variant_by_key(iterator->query, *f))
continue;
restart = true;
break;
}
if (!restart)
return 0;
/* Now patch the fields out */
_cleanup_(sd_json_variant_unrefp) sd_json_variant *patched_query =
sd_json_variant_ref(iterator->query);
r = sd_json_variant_filter(&patched_query, (char**const) fields);
if (r < 0)
return r;
/* NB: we stored the socket path in the varlink connection description when we set things up here! */
r = userdb_connect(
iterator,
ASSERT_PTR(sd_varlink_get_description(link)),
iterator->method,
iterator->more,
patched_query);
if (r < 0)
return r;
log_debug("Restarted query to service '%s' due to missing features.", sd_varlink_get_description(link));
return 1;
}
static int userdb_on_query_reply(
sd_varlink *link,
sd_json_variant *parameters,
const char *error_id,
sd_varlink_reply_flags_t flags,
void *userdata) {
UserDBIterator *iterator = ASSERT_PTR(userdata);
int r;
if (error_id) {
log_debug("Got lookup error: %s", error_id);
r = userdb_maybe_restart_query(iterator, link, parameters, error_id);
if (r < 0)
return r;
if (r > 0) {
r = 0;
goto finish;
}
/* Convert various forms of record not found into -ESRCH, since NSS typically doesn't care,
* about the details. Note that if a userName specification is refused as invalid parameter,
* we also turn this into -ESRCH following the logic that there cannot be a user record for a
* completely invalid user name. */
if (STR_IN_SET(error_id,
"io.systemd.UserDatabase.NoRecordFound",
"io.systemd.UserDatabase.ConflictingRecordFound") ||
sd_varlink_error_is_invalid_parameter(error_id, parameters, "userName") ||
sd_varlink_error_is_invalid_parameter(error_id, parameters, "groupName"))
r = -ESRCH;
else if (streq(error_id, "io.systemd.UserDatabase.NonMatchingRecordFound"))
r = -ENOEXEC;
else if (streq(error_id, "io.systemd.UserDatabase.ServiceNotAvailable"))
r = -EHOSTDOWN;
else if (streq(error_id, "io.systemd.UserDatabase.EnumerationNotSupported"))
r = -EOPNOTSUPP;
else if (streq(error_id, SD_VARLINK_ERROR_TIMEOUT))
r = -ETIMEDOUT;
else
r = -EIO;
goto finish;
}
switch (iterator->what) {
case LOOKUP_USER: {
_cleanup_(user_group_data_done) struct user_group_data user_data = {};
static const sd_json_dispatch_field dispatch_table[] = {
{ "record", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_variant, offsetof(struct user_group_data, record), 0 },
{ "incomplete", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct user_group_data, incomplete), 0 },
{}
};
_cleanup_(user_record_unrefp) UserRecord *hr = NULL;
assert_se(!iterator->found_user);
r = sd_json_dispatch(parameters, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &user_data);
if (r < 0)
goto finish;
if (!user_data.record) {
r = log_debug_errno(SYNTHETIC_ERRNO(EIO), "Reply is missing record key");
goto finish;
}
hr = user_record_new();
if (!hr) {
r = -ENOMEM;
goto finish;
}
r = user_record_load(hr, user_data.record, USER_RECORD_LOAD_REFUSE_SECRET|USER_RECORD_PERMISSIVE);
if (r < 0)
goto finish;
if (!hr->service) {
r = log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "User record does not carry service information, refusing.");
goto finish;
}
hr->incomplete = user_data.incomplete;
/* We match the root user by the name since the name is our primary key. We match the nobody
* use by UID though, since the name might differ on OSes */
if (streq_ptr(hr->user_name, "root"))
iterator->synthesize_root = false;
if (hr->uid == UID_NOBODY)
iterator->synthesize_nobody = false;
iterator->found_user = TAKE_PTR(hr);
iterator->n_found++;
/* More stuff coming? then let's just exit cleanly here */
if (FLAGS_SET(flags, SD_VARLINK_REPLY_CONTINUES))
return 0;
/* Otherwise, let's remove this link and exit cleanly then */
r = 0;
goto finish;
}
case LOOKUP_GROUP: {
_cleanup_(user_group_data_done) struct user_group_data group_data = {};
static const sd_json_dispatch_field dispatch_table[] = {
{ "record", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_variant, offsetof(struct user_group_data, record), 0 },
{ "incomplete", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct user_group_data, incomplete), 0 },
{}
};
_cleanup_(group_record_unrefp) GroupRecord *g = NULL;
assert_se(!iterator->found_group);
r = sd_json_dispatch(parameters, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &group_data);
if (r < 0)
goto finish;
if (!group_data.record) {
r = log_debug_errno(SYNTHETIC_ERRNO(EIO), "Reply is missing record key");
goto finish;
}
g = group_record_new();
if (!g) {
r = -ENOMEM;
goto finish;
}
r = group_record_load(g, group_data.record, USER_RECORD_LOAD_REFUSE_SECRET|USER_RECORD_PERMISSIVE);
if (r < 0)
goto finish;
if (!g->service) {
r = log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Group record does not carry service information, refusing.");
goto finish;
}
g->incomplete = group_data.incomplete;
if (streq_ptr(g->group_name, "root"))
iterator->synthesize_root = false;
if (g->gid == GID_NOBODY)
iterator->synthesize_nobody = false;
iterator->found_group = TAKE_PTR(g);
iterator->n_found++;
if (FLAGS_SET(flags, SD_VARLINK_REPLY_CONTINUES))
return 0;
r = 0;
goto finish;
}
case LOOKUP_MEMBERSHIP: {
_cleanup_(membership_data_done) struct membership_data membership_data = {};
static const sd_json_dispatch_field dispatch_table[] = {
{ "userName", SD_JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(struct membership_data, user_name), SD_JSON_RELAX },
{ "groupName", SD_JSON_VARIANT_STRING, json_dispatch_user_group_name, offsetof(struct membership_data, group_name), SD_JSON_RELAX },
{}
};
assert(!iterator->found_user_name);
assert(!iterator->found_group_name);
r = sd_json_dispatch(parameters, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &membership_data);
if (r < 0)
goto finish;
iterator->found_user_name = TAKE_PTR(membership_data.user_name);
iterator->found_group_name = TAKE_PTR(membership_data.group_name);
iterator->n_found++;
if (FLAGS_SET(flags, SD_VARLINK_REPLY_CONTINUES))
return 0;
r = 0;
goto finish;
}
default:
assert_not_reached();
}
finish:
/* If we got one ENOEXEC, let that win. Similarly, ESRCH wins except for ENOEXEC. This way when we do
* a wild dump we won't be tripped up by bad errors – as long as at least one connection ended
* somewhat cleanly. */
if (r == -ENOEXEC ||
(r == -ESRCH && iterator->error != ENOEXEC) ||
iterator->error == 0)
iterator->error = -r;
assert_se(set_remove(iterator->links, link) == link);
link = sd_varlink_unref(link);
return 0;
}
static int userdb_connect(
UserDBIterator *iterator,
const char *path,
const char *method,
bool more,
sd_json_variant *query) {
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL;
int r;
assert(iterator);
assert(path);
assert(method);
r = sd_varlink_connect_address(&vl, path);
if (r < 0)
return log_debug_errno(r, "Unable to connect to %s: %m", path);
sd_varlink_set_userdata(vl, iterator);
if (!iterator->event) {
r = sd_event_new(&iterator->event);
if (r < 0)
return log_debug_errno(r, "Unable to allocate event loop: %m");
}
r = sd_varlink_attach_event(vl, iterator->event, SD_EVENT_PRIORITY_NORMAL);
if (r < 0)
return log_debug_errno(r, "Failed to attach varlink connection to event loop: %m");
/* Note, this is load bearing: we store the socket path as description for the varlink
* connection. That's not just good for debugging, but we reuse this information in case we need to
* reissue the query with a reduced set of parameters. */
r = sd_varlink_set_description(vl, path);
if (r < 0)
return log_debug_errno(r, "Failed to set varlink connection description: %m");
r = sd_varlink_bind_reply(vl, userdb_on_query_reply);
if (r < 0)
return log_debug_errno(r, "Failed to bind reply callback: %m");
_cleanup_free_ char *service = NULL;
r = path_extract_filename(path, &service);
if (r < 0)
return log_debug_errno(r, "Failed to extract service name from socket path: %m");
assert(r != O_DIRECTORY);
_cleanup_(sd_json_variant_unrefp) sd_json_variant *patched_query = sd_json_variant_ref(query);
r = sd_json_variant_set_field_string(&patched_query, "service", service);
if (r < 0)
return log_debug_errno(r, "Unable to set service JSON field: %m");
if (more)
r = sd_varlink_observe(vl, method, patched_query);
else
r = sd_varlink_invoke(vl, method, patched_query);
if (r < 0)
return log_debug_errno(r, "Failed to invoke varlink method: %m");
r = set_ensure_consume(&iterator->links, &link_hash_ops, TAKE_PTR(vl));
if (r < 0)
return log_debug_errno(r, "Failed to add varlink connection to set: %m");
return r;
}
static int socket_filter_id_test(
id_t id,
const char *xattr,
int dir_fd,
const char *fname,
const char *full_path /* for logging only */) {
int r;
assert(xattr);
assert(dir_fd >= 0);
assert(fname);
assert(full_path);
/* Generic implementation for both UIDs and GIDs */
if (!uid_is_valid(id))
return true;
/* Looks for an xattr of the specified name on the referenced socket entrypoint inode. Parses this
* as a pair of start UID/end UID. Matches the specified UID against it. If it matches yay. If it
* doesn't nay. If no UID is specified or no xattr set, also yay. */
_cleanup_free_ char *t = NULL;
r = getxattr_at_malloc(dir_fd, fname, xattr, AT_SYMLINK_FOLLOW, &t, /* ret_size= */ NULL);
if (r == -ENODATA)
return true; /* no such xattr? match! */
if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) {
log_debug_errno(r, "Extended attributes on sockets not supported after all, ignoring: %m");
return true; /* if sockets don't support xattrs here, then this is a match */
}
if (r < 0)
return log_debug_errno(r, "Unexpected error while reading extended attribute '%s' of '%s', ignoring: %m", xattr, full_path);
_cleanup_strv_free_ char **l = strv_split(t, ",");
if (!l)
return log_oom_debug();
STRV_FOREACH(i, l) {
/* Single UID or UID range */
uid_t x, y;
r = parse_uid_range(*i, &x, &y);
if (r < 0)
return log_debug_errno(r, "Malformed extended attribute '%s' on '%s', ignoring.", xattr, full_path);
if (id >= x && id <= y)
return true;
}
return false;
}
static int socket_filter_name_test(
const char *name,
const char *xattr,
int dir_fd,
const char *fname,
const char *full_path /* for logging only */) {
int r;
assert(xattr);
assert(dir_fd >= 0);
assert(fname);
assert(full_path);
/* Looks for an xattr of the specified name on the referenced socket entrypoint inode. Parses this
* as a NUL terminated string list. Matches the specified name against it via fnmatch(). If it matches
* yay. If it doesn't nay. If no name is specified or no xattr set, also yay. */
if (!name)
return true;
_cleanup_strv_free_ char **l = NULL;
r = getxattr_at_strv(dir_fd, fname, xattr, AT_SYMLINK_FOLLOW, &l);
if (r == -ENODATA)
return true; /* no such xattr? match! */
if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) {
log_debug_errno(r, "Extended attributes on sockets not supported after all, ignoring: %m");
return true; /* if sockets don't support xattrs here, then this is a match */
}
if (r < 0)
return log_debug_errno(r, "Unexpected error while reading extended attribute '%s' of '%s', ignoring: %m", xattr, full_path);
return strv_fnmatch_full(l, name, FNM_NOESCAPE, /* ret_matched_pos= */ NULL);
}
static int socket_filter_test(
const SocketFilter *sfilter,
int dir_fd,
const char *fname,
const char *full_path /* for logging only */) {
assert(dir_fd >= 0);
assert(fname);
assert(full_path);
/* Before we contact a Varlink service, let's see if there's any chance it might answer our questions
* at all. For such pre-filtering a userdb backend can install xattrs on their entrypoint inode to
* indicate that it's never going to answer outside of a certain UID/GID range or username/groupname
* pattern */
/* No filter means all sockets match */
if (!sfilter)
return 1;
/* No xattr support? then all sockets match. This check mostly exists to take benefit of the internal caching
* of the test. Either way we'll check below again. */
if (socket_xattr_supported() == 0)
return 1;
if (socket_filter_id_test(sfilter->uid, "user.userdb.uid", dir_fd, fname, full_path) == 0)
return 0;
if (socket_filter_id_test(sfilter->gid, "user.userdb.gid", dir_fd, fname, full_path) == 0)
return 0;
if (socket_filter_name_test(sfilter->username, "user.userdb.username", dir_fd, fname, full_path) == 0)
return 0;
if (socket_filter_name_test(sfilter->groupname, "user.userdb.groupname", dir_fd, fname, full_path) == 0)
return 0;
return 1;
}
static int userdb_start_query(
UserDBIterator *iterator,
const char *method, /* must be a static string, we are not going to copy this here! */
bool more,
sd_json_variant *query,
const SocketFilter *sfilter,
UserDBFlags flags) {
_cleanup_strv_free_ char **except = NULL, **only = NULL;
_cleanup_closedir_ DIR *d = NULL;
const char *e;
int r, ret = 0;
assert(iterator);
assert(method);
if (FLAGS_SET(flags, USERDB_EXCLUDE_VARLINK))
return -ENOLINK;
assert(!iterator->query);
iterator->method = method; /* note: we don't make a copy here! */
iterator->query = sd_json_variant_ref(query);
iterator->more = more;
e = getenv("SYSTEMD_BYPASS_USERDB");
if (e) {
r = parse_boolean(e);
if (r > 0)
return -ENOLINK;
if (r < 0) {
except = strv_split(e, ":");
if (!except)
return -ENOMEM;
}
}
e = getenv("SYSTEMD_ONLY_USERDB");
if (e) {
only = strv_split(e, ":");
if (!only)
return -ENOMEM;
}
/* First, let's talk to the multiplexer, if we can */
if ((flags & (USERDB_AVOID_MULTIPLEXER|USERDB_EXCLUDE_DYNAMIC_USER|USERDB_EXCLUDE_NSS|USERDB_EXCLUDE_DROPIN|USERDB_DONT_SYNTHESIZE_INTRINSIC|USERDB_DONT_SYNTHESIZE_FOREIGN)) == 0 &&
!strv_contains(except, "io.systemd.Multiplexer") &&
(!only || strv_contains(only, "io.systemd.Multiplexer"))) {
r = userdb_connect(iterator, "/run/systemd/userdb/io.systemd.Multiplexer", method, more, query);
if (r >= 0) {
iterator->nss_covered = true; /* The multiplexer does NSS */
iterator->dropin_covered = true; /* It also handles drop-in stuff */
return 0;
}
}
d = opendir("/run/systemd/userdb/");
if (!d) {
if (errno == ENOENT)
return -ESRCH;
return -errno;
}
FOREACH_DIRENT(de, d, return -errno) {
_cleanup_free_ char *p = NULL;
bool is_nss, is_dropin;
if (streq(de->d_name, "io.systemd.Multiplexer")) /* We already tried this above, don't try this again */
continue;
if (FLAGS_SET(flags, USERDB_EXCLUDE_DYNAMIC_USER) &&
streq(de->d_name, "io.systemd.DynamicUser"))
continue;
/* Avoid NSS if this is requested. Note that we also skip NSS when we were asked to skip the
* multiplexer, since in that case it's safer to do NSS in the client side emulation below
* (and when we run as part of systemd-userdbd.service we don't want to talk to ourselves
* anyway). */
is_nss = streq(de->d_name, "io.systemd.NameServiceSwitch");
if ((flags & (USERDB_EXCLUDE_NSS|USERDB_AVOID_MULTIPLEXER)) && is_nss)
continue;
/* Similar for the drop-in service */
is_dropin = streq(de->d_name, "io.systemd.DropIn");
if ((flags & (USERDB_EXCLUDE_DROPIN|USERDB_AVOID_MULTIPLEXER)) && is_dropin)
continue;
if (strv_contains(except, de->d_name))
continue;
if (only && !strv_contains(only, de->d_name))
continue;
p = path_join("/run/systemd/userdb/", de->d_name);
if (!p)
return -ENOMEM;
r = socket_filter_test(sfilter, dirfd(d), de->d_name, p);
if (r < 0)
log_debug_errno(r, "Failed to filter sockets, ignoring: %m");
else if (r == 0)
continue;
r = userdb_connect(iterator, p, method, more, query);
if (is_nss && r >= 0) /* Turn off fallback NSS + dropin if we found the NSS/dropin service
* and could connect to it */
iterator->nss_covered = true;
if (is_dropin && r >= 0)
iterator->dropin_covered = true;
RET_GATHER(ret, r);
}
if (set_isempty(iterator->links))
return ret < 0 ? ret : -ESRCH; /* propagate the first error we saw if we couldn't connect to anything. */
/* We connected to some services, in this case, ignore the ones we failed on */
return 0;
}
static int userdb_process(
UserDBIterator *iterator,
UserRecord **ret_user_record,
GroupRecord **ret_group_record,
char **ret_user_name,
char **ret_group_name) {
int r;
assert(iterator);
for (;;) {
if (iterator->what == LOOKUP_USER && iterator->found_user) {
if (ret_user_record)
*ret_user_record = TAKE_PTR(iterator->found_user);
else
iterator->found_user = user_record_unref(iterator->found_user);
if (ret_group_record)
*ret_group_record = NULL;
if (ret_user_name)
*ret_user_name = NULL;
if (ret_group_name)
*ret_group_name = NULL;
return 0;
}
if (iterator->what == LOOKUP_GROUP && iterator->found_group) {
if (ret_group_record)
*ret_group_record = TAKE_PTR(iterator->found_group);
else
iterator->found_group = group_record_unref(iterator->found_group);
if (ret_user_record)
*ret_user_record = NULL;
if (ret_user_name)
*ret_user_name = NULL;
if (ret_group_name)
*ret_group_name = NULL;
return 0;
}
if (iterator->what == LOOKUP_MEMBERSHIP && iterator->found_user_name && iterator->found_group_name) {
if (ret_user_name)
*ret_user_name = TAKE_PTR(iterator->found_user_name);
else
iterator->found_user_name = mfree(iterator->found_user_name);
if (ret_group_name)
*ret_group_name = TAKE_PTR(iterator->found_group_name);
else
iterator->found_group_name = mfree(iterator->found_group_name);
if (ret_user_record)
*ret_user_record = NULL;
if (ret_group_record)
*ret_group_record = NULL;
return 0;
}
if (set_isempty(iterator->links)) {
if (iterator->error == 0)
return -ESRCH;
return -abs(iterator->error);
}
if (!iterator->event)
return -ESRCH;
r = sd_event_run(iterator->event, UINT64_MAX);
if (r < 0)
return r;
}
}
static int synthetic_root_user_build(UserRecord **ret) {
return user_record_buildo(
ret,
SD_JSON_BUILD_PAIR("userName", JSON_BUILD_CONST_STRING("root")),
SD_JSON_BUILD_PAIR_UNSIGNED("uid", 0),
SD_JSON_BUILD_PAIR_UNSIGNED("gid", 0),
SD_JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_CONST_STRING("/root")),
SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("intrinsic")));
}
static int synthetic_nobody_user_build(UserRecord **ret) {
return user_record_buildo(
ret,
SD_JSON_BUILD_PAIR("userName", JSON_BUILD_CONST_STRING(NOBODY_USER_NAME)),
SD_JSON_BUILD_PAIR_UNSIGNED("uid", UID_NOBODY),
SD_JSON_BUILD_PAIR_UNSIGNED("gid", GID_NOBODY),
SD_JSON_BUILD_PAIR("shell", JSON_BUILD_CONST_STRING(NOLOGIN)),
SD_JSON_BUILD_PAIR_BOOLEAN("locked", true),
SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("intrinsic")));
}
static int synthetic_foreign_user_build(uid_t foreign_uid, UserRecord **ret) {
assert(ret);
if (!uid_is_valid(foreign_uid))
return -ESRCH;
if (foreign_uid > 0xFFFF)
return -ESRCH;
_cleanup_free_ char *un = NULL;
if (asprintf(&un, "foreign-" UID_FMT, foreign_uid) < 0)
return -ENOMEM;
_cleanup_free_ char *rn = NULL;
if (asprintf(&rn, "Foreign System Image UID " UID_FMT, foreign_uid) < 0)
return -ENOMEM;
return user_record_buildo(
ret,
SD_JSON_BUILD_PAIR_STRING("userName", un),
SD_JSON_BUILD_PAIR_STRING("realName", rn),
SD_JSON_BUILD_PAIR_UNSIGNED("uid", FOREIGN_UID_BASE + foreign_uid),
SD_JSON_BUILD_PAIR_UNSIGNED("gid", FOREIGN_UID_BASE + foreign_uid),
SD_JSON_BUILD_PAIR("shell", JSON_BUILD_CONST_STRING(NOLOGIN)),
SD_JSON_BUILD_PAIR_BOOLEAN("locked", true),
SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("foreign")));
}
static int synthetic_numeric_user_build(uid_t uid, UserRecord **ret) {
assert(ret);
if (uid == 0) /* This should be handled by synthetic_root_user_build() */
return -ESRCH;
if (!uid_is_system(uid))
return -ESRCH;
_cleanup_free_ char *un = NULL;
if (asprintf(&un, "unknown-" UID_FMT, uid) < 0)
return -ENOMEM;
_cleanup_free_ char *rn = NULL;
if (asprintf(&rn, "Unknown System UID " UID_FMT, uid) < 0)
return -ENOMEM;
return user_record_buildo(
ret,
SD_JSON_BUILD_PAIR_STRING("userName", un),
SD_JSON_BUILD_PAIR_STRING("realName", rn),
SD_JSON_BUILD_PAIR_UNSIGNED("uid", uid),
SD_JSON_BUILD_PAIR_STRING("disposition", "system"));
}
static int user_name_foreign_extract_uid(const char *name, uid_t *ret_uid) {
int r;
assert(name);
assert(ret_uid);
/* Parses the inner UID from a user name of the foreign UID range, in the form "foreign-NNN". Returns
* > 0 if that worked, 0 if it didn't. */
const char *e = startswith(name, "foreign-");
if (!e)
goto nomatch;
uid_t uid;
r = parse_uid(e, &uid);
if (r < 0)
goto nomatch;
if (uid > 0xFFFF)
goto nomatch;
*ret_uid = uid;
return 1;
nomatch:
*ret_uid = UID_INVALID;
return 0;
}
static int query_append_common(sd_json_variant **query, const UserDBMatch *match) {
int r;
_cleanup_strv_free_ char **dispositions = NULL;
assert(query);
uint64_t mask = match->disposition_mask;
if (FLAGS_SET(mask, USER_DISPOSITION_MASK_ALL)) {
for (UserDisposition d = 0; d < _USER_DISPOSITION_MAX; d++) {
if (!BIT_SET(mask, d))
continue;
r = strv_extend(&dispositions, user_disposition_to_string(d));
if (r < 0)
return r;
}
}
return sd_json_variant_merge_objectbo(
query,
SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(match->fuzzy_names), "fuzzyNames", SD_JSON_BUILD_STRV(match->fuzzy_names)),
SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(match->uuid), "uuid", SD_JSON_BUILD_UUID(match->uuid)),
SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(dispositions), "dispositionMask", SD_JSON_BUILD_STRV(dispositions)));
}
static int query_append_uid_match(sd_json_variant **query, const UserDBMatch *match) {
int r;
assert(query);
if (!userdb_match_is_set(match))
return 0;
r = sd_json_variant_merge_objectbo(
query,
SD_JSON_BUILD_PAIR_CONDITION(match->uid_min > 0, "uidMin", SD_JSON_BUILD_UNSIGNED(match->uid_min)),
SD_JSON_BUILD_PAIR_CONDITION(match->uid_max < UID_INVALID-1, "uidMax", SD_JSON_BUILD_UNSIGNED(match->uid_max)));
if (r < 0)
return r;
return query_append_common(query, match);
}
static int userdb_by_name_fallbacks(
const char *name,
UserDBIterator *iterator,
UserDBFlags flags,
UserRecord **ret) {
int r;
assert(name);
assert(iterator);
assert(ret);