| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-varlink.h" |
| 4 |
|
| 5 |
#include "dynamic-user.h" |
| 6 |
#include "hashmap.h" |
| 7 |
#include "json-util.h" |
| 8 |
#include "manager.h" |
| 9 |
#include "string-util.h" |
| 10 |
#include "uid-classification.h" |
| 11 |
#include "user-util.h" |
| 12 |
#include "varlink-dynamic-user.h" |
| 13 |
|
| 14 |
typedef struct LookupParameters { |
| 15 |
const char *user_name; |
| 16 |
const char *group_name; |
| 17 |
union { |
| 18 |
uid_t uid; |
| 19 |
gid_t gid; |
| 20 |
}; |
| 21 |
const char *service; |
| 22 |
} LookupParameters; |
| 23 |
|
| 24 |
static int build_user_json(const char *user_name, uid_t uid, sd_json_variant **ret) { |
| 25 |
assert(user_name); |
| 26 |
assert(uid_is_valid(uid)); |
| 27 |
assert(ret); |
| 28 |
|
| 29 |
return sd_json_buildo(ret, SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_OBJECT( |
| 30 |
SD_JSON_BUILD_PAIR_STRING("userName", user_name), |
| 31 |
SD_JSON_BUILD_PAIR_UNSIGNED("uid", uid), |
| 32 |
SD_JSON_BUILD_PAIR_UNSIGNED("gid", uid), |
| 33 |
SD_JSON_BUILD_PAIR("realName", JSON_BUILD_CONST_STRING("Dynamic User")), |
| 34 |
SD_JSON_BUILD_PAIR("homeDirectory", JSON_BUILD_CONST_STRING("/")), |
| 35 |
SD_JSON_BUILD_PAIR("shell", JSON_BUILD_CONST_STRING(NOLOGIN)), |
| 36 |
SD_JSON_BUILD_PAIR_BOOLEAN("locked", true), |
| 37 |
SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.DynamicUser")), |
| 38 |
SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("dynamic"))))); |
| 39 |
} |
| 40 |
|
| 41 |
static bool user_match_lookup_parameters(LookupParameters *p, const char *name, uid_t uid) { |
| 42 |
assert(p); |
| 43 |
|
| 44 |
if (p->user_name && !streq(name, p->user_name)) |
| 45 |
return false; |
| 46 |
|
| 47 |
if (uid_is_valid(p->uid) && uid != p->uid) |
| 48 |
return false; |
| 49 |
|
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
int vl_method_get_user_record(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 54 |
|
| 55 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 56 |
{ "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, uid), 0 }, |
| 57 |
{ "userName", SD_JSON_VARIANT_STRING, json_dispatch_const_user_group_name, offsetof(LookupParameters, user_name), SD_JSON_RELAX }, |
| 58 |
{ "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, |
| 59 |
{} |
| 60 |
}; |
| 61 |
|
| 62 |
LookupParameters p = { |
| 63 |
.uid = UID_INVALID, |
| 64 |
}; |
| 65 |
_cleanup_free_ char *found_name = NULL; |
| 66 |
uid_t found_uid = UID_INVALID, uid; |
| 67 |
Manager *m = ASSERT_PTR(userdata); |
| 68 |
const char *un; |
| 69 |
int r; |
| 70 |
|
| 71 |
assert(parameters); |
| 72 |
|
| 73 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 74 |
if (r != 0) |
| 75 |
return r; |
| 76 |
|
| 77 |
if (!streq_ptr(p.service, "io.systemd.DynamicUser")) |
| 78 |
return sd_varlink_error(link, "io.systemd.UserDatabase.BadService", NULL); |
| 79 |
|
| 80 |
r = sd_varlink_set_sentinel(link, "io.systemd.UserDatabase.NoRecordFound"); |
| 81 |
if (r < 0) |
| 82 |
return r; |
| 83 |
|
| 84 |
if (uid_is_valid(p.uid)) |
| 85 |
r = dynamic_user_lookup_uid(m, p.uid, &found_name); |
| 86 |
else if (p.user_name) |
| 87 |
r = dynamic_user_lookup_name(m, p.user_name, &found_uid); |
| 88 |
else { |
| 89 |
DynamicUser *d; |
| 90 |
|
| 91 |
HASHMAP_FOREACH(d, m->dynamic_users) { |
| 92 |
r = dynamic_user_current(d, &uid); |
| 93 |
if (r == -EAGAIN) /* not realized yet? */ |
| 94 |
continue; |
| 95 |
if (r < 0) |
| 96 |
return r; |
| 97 |
|
| 98 |
if (!uid_is_dynamic(uid)) |
| 99 |
continue; |
| 100 |
|
| 101 |
if (!user_match_lookup_parameters(&p, d->name, uid)) |
| 102 |
continue; |
| 103 |
|
| 104 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 105 |
r = build_user_json(d->name, uid, &v); |
| 106 |
if (r < 0) |
| 107 |
return r; |
| 108 |
|
| 109 |
r = sd_varlink_reply(link, v); |
| 110 |
if (r < 0) |
| 111 |
return r; |
| 112 |
} |
| 113 |
|
| 114 |
return 0; |
| 115 |
} |
| 116 |
if (r == -ESRCH) |
| 117 |
return 0; |
| 118 |
if (r < 0) |
| 119 |
return r; |
| 120 |
|
| 121 |
uid = uid_is_valid(found_uid) ? found_uid : p.uid; |
| 122 |
un = found_name ?: p.user_name; |
| 123 |
|
| 124 |
if (!user_match_lookup_parameters(&p, un, uid)) |
| 125 |
return sd_varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL); |
| 126 |
|
| 127 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 128 |
r = build_user_json(un, uid, &v); |
| 129 |
if (r < 0) |
| 130 |
return r; |
| 131 |
|
| 132 |
return sd_varlink_reply(link, v); |
| 133 |
} |
| 134 |
|
| 135 |
static int build_group_json(const char *group_name, gid_t gid, sd_json_variant **ret) { |
| 136 |
assert(group_name); |
| 137 |
assert(gid_is_valid(gid)); |
| 138 |
assert(ret); |
| 139 |
|
| 140 |
return sd_json_buildo(ret, SD_JSON_BUILD_PAIR("record", SD_JSON_BUILD_OBJECT( |
| 141 |
SD_JSON_BUILD_PAIR_STRING("groupName", group_name), |
| 142 |
SD_JSON_BUILD_PAIR("description", JSON_BUILD_CONST_STRING("Dynamic Group")), |
| 143 |
SD_JSON_BUILD_PAIR_UNSIGNED("gid", gid), |
| 144 |
SD_JSON_BUILD_PAIR("service", JSON_BUILD_CONST_STRING("io.systemd.DynamicUser")), |
| 145 |
SD_JSON_BUILD_PAIR("disposition", JSON_BUILD_CONST_STRING("dynamic"))))); |
| 146 |
} |
| 147 |
|
| 148 |
static bool group_match_lookup_parameters(LookupParameters *p, const char *name, gid_t gid) { |
| 149 |
assert(p); |
| 150 |
|
| 151 |
if (p->group_name && !streq(name, p->group_name)) |
| 152 |
return false; |
| 153 |
|
| 154 |
if (gid_is_valid(p->gid) && gid != p->gid) |
| 155 |
return false; |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
int vl_method_get_group_record(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 161 |
|
| 162 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 163 |
{ "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(LookupParameters, gid), 0 }, |
| 164 |
{ "groupName", SD_JSON_VARIANT_STRING, json_dispatch_const_user_group_name, offsetof(LookupParameters, group_name), SD_JSON_RELAX }, |
| 165 |
{ "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, |
| 166 |
{} |
| 167 |
}; |
| 168 |
|
| 169 |
LookupParameters p = { |
| 170 |
.gid = GID_INVALID, |
| 171 |
}; |
| 172 |
_cleanup_free_ char *found_name = NULL; |
| 173 |
uid_t found_gid = GID_INVALID, gid; |
| 174 |
Manager *m = ASSERT_PTR(userdata); |
| 175 |
const char *gn; |
| 176 |
int r; |
| 177 |
|
| 178 |
assert(parameters); |
| 179 |
|
| 180 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 181 |
if (r != 0) |
| 182 |
return r; |
| 183 |
|
| 184 |
r = sd_varlink_set_sentinel(link, "io.systemd.UserDatabase.NoRecordFound"); |
| 185 |
if (r < 0) |
| 186 |
return r; |
| 187 |
|
| 188 |
if (!streq_ptr(p.service, "io.systemd.DynamicUser")) |
| 189 |
return sd_varlink_error(link, "io.systemd.UserDatabase.BadService", NULL); |
| 190 |
|
| 191 |
if (gid_is_valid(p.gid)) |
| 192 |
r = dynamic_user_lookup_uid(m, (uid_t) p.gid, &found_name); |
| 193 |
else if (p.group_name) |
| 194 |
r = dynamic_user_lookup_name(m, p.group_name, &found_gid); |
| 195 |
else { |
| 196 |
DynamicUser *d; |
| 197 |
|
| 198 |
HASHMAP_FOREACH(d, m->dynamic_users) { |
| 199 |
uid_t uid; |
| 200 |
|
| 201 |
r = dynamic_user_current(d, &uid); |
| 202 |
if (r == -EAGAIN) |
| 203 |
continue; |
| 204 |
if (r < 0) |
| 205 |
return r; |
| 206 |
|
| 207 |
if (!gid_is_dynamic((gid_t) uid)) |
| 208 |
continue; |
| 209 |
|
| 210 |
if (!group_match_lookup_parameters(&p, d->name, (gid_t) uid)) |
| 211 |
continue; |
| 212 |
|
| 213 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 214 |
r = build_group_json(d->name, (gid_t) uid, &v); |
| 215 |
if (r < 0) |
| 216 |
return r; |
| 217 |
|
| 218 |
r = sd_varlink_reply(link, v); |
| 219 |
if (r < 0) |
| 220 |
return r; |
| 221 |
} |
| 222 |
|
| 223 |
return 0; |
| 224 |
} |
| 225 |
if (r == -ESRCH) |
| 226 |
return 0; |
| 227 |
if (r < 0) |
| 228 |
return r; |
| 229 |
|
| 230 |
gid = gid_is_valid(found_gid) ? found_gid : p.gid; |
| 231 |
gn = found_name ?: p.group_name; |
| 232 |
|
| 233 |
if (!group_match_lookup_parameters(&p, gn, gid)) |
| 234 |
return sd_varlink_error(link, "io.systemd.UserDatabase.ConflictingRecordFound", NULL); |
| 235 |
|
| 236 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 237 |
r = build_group_json(gn, gid, &v); |
| 238 |
if (r < 0) |
| 239 |
return r; |
| 240 |
|
| 241 |
return sd_varlink_reply(link, v); |
| 242 |
} |
| 243 |
|
| 244 |
int vl_method_get_memberships(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 245 |
|
| 246 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 247 |
{ "userName", SD_JSON_VARIANT_STRING, json_dispatch_const_user_group_name, offsetof(LookupParameters, user_name), SD_JSON_RELAX }, |
| 248 |
{ "groupName", SD_JSON_VARIANT_STRING, json_dispatch_const_user_group_name, offsetof(LookupParameters, group_name), SD_JSON_RELAX }, |
| 249 |
{ "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(LookupParameters, service), 0 }, |
| 250 |
{} |
| 251 |
}; |
| 252 |
|
| 253 |
LookupParameters p = {}; |
| 254 |
int r; |
| 255 |
|
| 256 |
assert(parameters); |
| 257 |
|
| 258 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 259 |
if (r != 0) |
| 260 |
return r; |
| 261 |
|
| 262 |
if (!streq_ptr(p.service, "io.systemd.DynamicUser")) |
| 263 |
return sd_varlink_error(link, "io.systemd.UserDatabase.BadService", NULL); |
| 264 |
|
| 265 |
/* We don't support auxiliary groups with dynamic users. */ |
| 266 |
return sd_varlink_error(link, "io.systemd.UserDatabase.NoRecordFound", NULL); |
| 267 |
} |