| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <gshadow.h> |
| 4 |
|
| 5 |
#include "sd-json.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "errno-util.h" |
| 9 |
#include "format-util.h" |
| 10 |
#include "group-record.h" |
| 11 |
#include "libcrypt-util.h" |
| 12 |
#include "log.h" |
| 13 |
#include "string-util.h" |
| 14 |
#include "strv.h" |
| 15 |
#include "time-util.h" |
| 16 |
#include "user-record.h" |
| 17 |
#include "user-record-nss.h" |
| 18 |
#include "user-util.h" |
| 19 |
#include "utf8.h" |
| 20 |
|
| 21 |
#define SET_IF(field, condition, value, fallback) \ |
| 22 |
field = (condition) ? (value) : (fallback) |
| 23 |
|
| 24 |
static const char* utf8_only(const char *s) { |
| 25 |
return s && utf8_is_valid(s) ? s : NULL; |
| 26 |
} |
| 27 |
|
| 28 |
static int strv_extend_strv_utf8_only(char ***dst, char **src, bool filter_duplicates) { |
| 29 |
_cleanup_free_ char **t = NULL; |
| 30 |
size_t l, j = 0; |
| 31 |
|
| 32 |
/* First, do a shallow copy of s, filtering for only valid utf-8 strings */ |
| 33 |
l = strv_length(src); |
| 34 |
t = new(char*, l + 1); |
| 35 |
if (!t) |
| 36 |
return -ENOMEM; |
| 37 |
|
| 38 |
for (size_t i = 0; i < l; i++) |
| 39 |
if (utf8_is_valid(src[i])) |
| 40 |
t[j++] = src[i]; |
| 41 |
if (j == 0) |
| 42 |
return 0; |
| 43 |
|
| 44 |
t[j] = NULL; |
| 45 |
return strv_extend_strv(dst, t, filter_duplicates); |
| 46 |
} |
| 47 |
|
| 48 |
int nss_passwd_to_user_record( |
| 49 |
const struct passwd *pwd, |
| 50 |
const struct spwd *spwd, |
| 51 |
UserRecord **ret) { |
| 52 |
|
| 53 |
_cleanup_(user_record_unrefp) UserRecord *hr = NULL; |
| 54 |
int r; |
| 55 |
|
| 56 |
assert(pwd); |
| 57 |
|
| 58 |
if (isempty(pwd->pw_name)) |
| 59 |
return -EINVAL; |
| 60 |
|
| 61 |
if (spwd && !streq_ptr(spwd->sp_namp, pwd->pw_name)) |
| 62 |
return -EINVAL; |
| 63 |
|
| 64 |
hr = user_record_new(); |
| 65 |
if (!hr) |
| 66 |
return -ENOMEM; |
| 67 |
|
| 68 |
r = free_and_strdup(&hr->user_name, pwd->pw_name); |
| 69 |
if (r < 0) |
| 70 |
return r; |
| 71 |
|
| 72 |
/* Some bad NSS modules synthesize GECOS fields with embedded ":" or "\n" characters, which are not |
| 73 |
* something we can output in /etc/passwd compatible format, since these are record separators |
| 74 |
* there. We normally refuse that, but we need to maintain compatibility with arbitrary NSS modules, |
| 75 |
* hence let's do what glibc does: mangle the data to fit the format. */ |
| 76 |
if (isempty(pwd->pw_gecos) || streq_ptr(pwd->pw_gecos, hr->user_name)) |
| 77 |
hr->real_name = mfree(hr->real_name); |
| 78 |
else if (valid_gecos(pwd->pw_gecos)) { |
| 79 |
r = free_and_strdup(&hr->real_name, pwd->pw_gecos); |
| 80 |
if (r < 0) |
| 81 |
return r; |
| 82 |
} else { |
| 83 |
_cleanup_free_ char *mangled = NULL; |
| 84 |
|
| 85 |
mangled = mangle_gecos(pwd->pw_gecos); |
| 86 |
if (!mangled) |
| 87 |
return -ENOMEM; |
| 88 |
|
| 89 |
free_and_replace(hr->real_name, mangled); |
| 90 |
} |
| 91 |
|
| 92 |
r = free_and_strdup(&hr->home_directory, utf8_only(empty_to_null(pwd->pw_dir))); |
| 93 |
if (r < 0) |
| 94 |
return r; |
| 95 |
|
| 96 |
r = free_and_strdup(&hr->shell, utf8_only(empty_to_null(pwd->pw_shell))); |
| 97 |
if (r < 0) |
| 98 |
return r; |
| 99 |
|
| 100 |
hr->uid = pwd->pw_uid; |
| 101 |
hr->gid = pwd->pw_gid; |
| 102 |
|
| 103 |
if (spwd && |
| 104 |
looks_like_hashed_password(utf8_only(spwd->sp_pwdp))) { /* Ignore locked, disabled, and mojibake passwords */ |
| 105 |
strv_free_erase(hr->hashed_password); |
| 106 |
hr->hashed_password = strv_new(spwd->sp_pwdp); |
| 107 |
if (!hr->hashed_password) |
| 108 |
return -ENOMEM; |
| 109 |
} else |
| 110 |
hr->hashed_password = strv_free_erase(hr->hashed_password); |
| 111 |
|
| 112 |
/* shadow-utils suggests using "chage -E 0" (or -E 1, depending on which man page you check) |
| 113 |
* for locking a whole account, hence check for that. Note that it also defines a way to lock |
| 114 |
* just a password instead of the whole account, but that's mostly pointless in times of |
| 115 |
* password-less authorization, hence let's not bother. */ |
| 116 |
|
| 117 |
SET_IF(hr->locked, |
| 118 |
spwd && spwd->sp_expire >= 0, |
| 119 |
spwd->sp_expire <= 1, -1); |
| 120 |
|
| 121 |
SET_IF(hr->not_after_usec, |
| 122 |
spwd && spwd->sp_expire > 1 && (uint64_t) spwd->sp_expire < (UINT64_MAX-1)/USEC_PER_DAY, |
| 123 |
spwd->sp_expire * USEC_PER_DAY, UINT64_MAX); |
| 124 |
|
| 125 |
SET_IF(hr->password_change_now, |
| 126 |
spwd && spwd->sp_lstchg >= 0, |
| 127 |
spwd->sp_lstchg == 0, -1); |
| 128 |
|
| 129 |
SET_IF(hr->last_password_change_usec, |
| 130 |
spwd && spwd->sp_lstchg > 0 && (uint64_t) spwd->sp_lstchg <= (UINT64_MAX-1)/USEC_PER_DAY, |
| 131 |
spwd->sp_lstchg * USEC_PER_DAY, UINT64_MAX); |
| 132 |
|
| 133 |
SET_IF(hr->password_change_min_usec, |
| 134 |
spwd && spwd->sp_min > 0 && (uint64_t) spwd->sp_min <= (UINT64_MAX-1)/USEC_PER_DAY, |
| 135 |
spwd->sp_min * USEC_PER_DAY, UINT64_MAX); |
| 136 |
|
| 137 |
SET_IF(hr->password_change_max_usec, |
| 138 |
spwd && spwd->sp_max > 0 && (uint64_t) spwd->sp_max <= (UINT64_MAX-1)/USEC_PER_DAY, |
| 139 |
spwd->sp_max * USEC_PER_DAY, UINT64_MAX); |
| 140 |
|
| 141 |
SET_IF(hr->password_change_warn_usec, |
| 142 |
spwd && spwd->sp_warn > 0 && (uint64_t) spwd->sp_warn <= (UINT64_MAX-1)/USEC_PER_DAY, |
| 143 |
spwd->sp_warn * USEC_PER_DAY, UINT64_MAX); |
| 144 |
|
| 145 |
SET_IF(hr->password_change_inactive_usec, |
| 146 |
spwd && spwd->sp_inact > 0 && (uint64_t) spwd->sp_inact <= (UINT64_MAX-1)/USEC_PER_DAY, |
| 147 |
spwd->sp_inact * USEC_PER_DAY, UINT64_MAX); |
| 148 |
|
| 149 |
hr->json = sd_json_variant_unref(hr->json); |
| 150 |
r = sd_json_buildo( |
| 151 |
&hr->json, |
| 152 |
SD_JSON_BUILD_PAIR_STRING("userName", hr->user_name), |
| 153 |
SD_JSON_BUILD_PAIR_UNSIGNED("uid", hr->uid), |
| 154 |
SD_JSON_BUILD_PAIR_UNSIGNED("gid", user_record_gid(hr)), |
| 155 |
SD_JSON_BUILD_PAIR_CONDITION(!!hr->real_name, "realName", SD_JSON_BUILD_STRING(hr->real_name)), |
| 156 |
SD_JSON_BUILD_PAIR_CONDITION(!!hr->home_directory, "homeDirectory", SD_JSON_BUILD_STRING(hr->home_directory)), |
| 157 |
SD_JSON_BUILD_PAIR_CONDITION(!!hr->shell, "shell", SD_JSON_BUILD_STRING(hr->shell)), |
| 158 |
SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(hr->hashed_password), "privileged", SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_STRV("hashedPassword", hr->hashed_password))), |
| 159 |
SD_JSON_BUILD_PAIR_CONDITION(hr->locked >= 0, "locked", SD_JSON_BUILD_BOOLEAN(hr->locked)), |
| 160 |
SD_JSON_BUILD_PAIR_CONDITION(hr->not_after_usec != UINT64_MAX, "notAfterUSec", SD_JSON_BUILD_UNSIGNED(hr->not_after_usec)), |
| 161 |
SD_JSON_BUILD_PAIR_CONDITION(hr->password_change_now >= 0, "passwordChangeNow", SD_JSON_BUILD_BOOLEAN(hr->password_change_now)), |
| 162 |
SD_JSON_BUILD_PAIR_CONDITION(hr->last_password_change_usec != UINT64_MAX, "lastPasswordChangeUSec", SD_JSON_BUILD_UNSIGNED(hr->last_password_change_usec)), |
| 163 |
SD_JSON_BUILD_PAIR_CONDITION(hr->password_change_min_usec != UINT64_MAX, "passwordChangeMinUSec", SD_JSON_BUILD_UNSIGNED(hr->password_change_min_usec)), |
| 164 |
SD_JSON_BUILD_PAIR_CONDITION(hr->password_change_max_usec != UINT64_MAX, "passwordChangeMaxUSec", SD_JSON_BUILD_UNSIGNED(hr->password_change_max_usec)), |
| 165 |
SD_JSON_BUILD_PAIR_CONDITION(hr->password_change_warn_usec != UINT64_MAX, "passwordChangeWarnUSec", SD_JSON_BUILD_UNSIGNED(hr->password_change_warn_usec)), |
| 166 |
SD_JSON_BUILD_PAIR_CONDITION(hr->password_change_inactive_usec != UINT64_MAX, "passwordChangeInactiveUSec", SD_JSON_BUILD_UNSIGNED(hr->password_change_inactive_usec))); |
| 167 |
if (r < 0) |
| 168 |
return r; |
| 169 |
|
| 170 |
hr->mask = USER_RECORD_REGULAR | |
| 171 |
(!strv_isempty(hr->hashed_password) ? USER_RECORD_PRIVILEGED : 0); |
| 172 |
|
| 173 |
if (ret) |
| 174 |
*ret = TAKE_PTR(hr); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
int nss_spwd_for_passwd(const struct passwd *pwd, struct spwd *ret_spwd, char **ret_buffer) { |
| 179 |
size_t buflen = 4096; |
| 180 |
int r; |
| 181 |
|
| 182 |
assert(pwd); |
| 183 |
assert(ret_spwd); |
| 184 |
assert(ret_buffer); |
| 185 |
|
| 186 |
for (;;) { |
| 187 |
_cleanup_free_ char *buf = NULL; |
| 188 |
struct spwd spwd = {}, *result = NULL; |
| 189 |
|
| 190 |
buf = malloc0(buflen); |
| 191 |
if (!buf) |
| 192 |
return -ENOMEM; |
| 193 |
|
| 194 |
r = getspnam_r(pwd->pw_name, &spwd, buf, buflen, &result); |
| 195 |
if (r == 0) { |
| 196 |
if (!result) |
| 197 |
return -ESRCH; |
| 198 |
|
| 199 |
*ret_spwd = *result; |
| 200 |
*ret_buffer = TAKE_PTR(buf); |
| 201 |
return 0; |
| 202 |
} |
| 203 |
if (r < 0) |
| 204 |
return -EIO; /* Weird, this should not return negative! */ |
| 205 |
if (r != ERANGE) |
| 206 |
return -r; |
| 207 |
|
| 208 |
if (buflen > SIZE_MAX / 2) |
| 209 |
return -ERANGE; |
| 210 |
|
| 211 |
buflen *= 2; |
| 212 |
buf = mfree(buf); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
int nss_user_record_by_name( |
| 217 |
const char *name, |
| 218 |
bool with_shadow, |
| 219 |
UserRecord **ret) { |
| 220 |
|
| 221 |
_cleanup_free_ char *sbuf = NULL; |
| 222 |
_cleanup_free_ struct passwd *result = NULL; |
| 223 |
bool incomplete = false; |
| 224 |
struct spwd spwd, *sresult = NULL; |
| 225 |
int r; |
| 226 |
|
| 227 |
assert(name); |
| 228 |
|
| 229 |
r = getpwnam_malloc(name, &result); |
| 230 |
if (r < 0) |
| 231 |
return r; |
| 232 |
|
| 233 |
if (with_shadow) { |
| 234 |
r = nss_spwd_for_passwd(result, &spwd, &sbuf); |
| 235 |
if (r < 0) { |
| 236 |
log_debug_errno(r, "Failed to do shadow lookup for user %s, ignoring: %m", name); |
| 237 |
incomplete = ERRNO_IS_PRIVILEGE(r); |
| 238 |
} else |
| 239 |
sresult = &spwd; |
| 240 |
} else |
| 241 |
incomplete = true; |
| 242 |
|
| 243 |
r = nss_passwd_to_user_record(result, sresult, ret); |
| 244 |
if (r < 0) |
| 245 |
return r; |
| 246 |
|
| 247 |
if (ret) |
| 248 |
(*ret)->incomplete = incomplete; |
| 249 |
return 0; |
| 250 |
} |
| 251 |
|
| 252 |
int nss_user_record_by_uid( |
| 253 |
uid_t uid, |
| 254 |
bool with_shadow, |
| 255 |
UserRecord **ret) { |
| 256 |
|
| 257 |
_cleanup_free_ char *sbuf = NULL; |
| 258 |
_cleanup_free_ struct passwd *result = NULL; |
| 259 |
bool incomplete = false; |
| 260 |
struct spwd spwd, *sresult = NULL; |
| 261 |
int r; |
| 262 |
|
| 263 |
r = getpwuid_malloc(uid, &result); |
| 264 |
if (r < 0) |
| 265 |
return r; |
| 266 |
|
| 267 |
if (with_shadow) { |
| 268 |
r = nss_spwd_for_passwd(result, &spwd, &sbuf); |
| 269 |
if (r < 0) { |
| 270 |
log_debug_errno(r, "Failed to do shadow lookup for UID " UID_FMT ", ignoring: %m", uid); |
| 271 |
incomplete = ERRNO_IS_PRIVILEGE(r); |
| 272 |
} else |
| 273 |
sresult = &spwd; |
| 274 |
} else |
| 275 |
incomplete = true; |
| 276 |
|
| 277 |
r = nss_passwd_to_user_record(result, sresult, ret); |
| 278 |
if (r < 0) |
| 279 |
return r; |
| 280 |
|
| 281 |
if (ret) |
| 282 |
(*ret)->incomplete = incomplete; |
| 283 |
return 0; |
| 284 |
} |
| 285 |
|
| 286 |
int nss_group_to_group_record( |
| 287 |
const struct group *grp, |
| 288 |
const struct sgrp *sgrp, |
| 289 |
GroupRecord **ret) { |
| 290 |
|
| 291 |
_cleanup_(group_record_unrefp) GroupRecord *g = NULL; |
| 292 |
int r; |
| 293 |
|
| 294 |
assert(grp); |
| 295 |
|
| 296 |
if (isempty(grp->gr_name)) |
| 297 |
return -EINVAL; |
| 298 |
|
| 299 |
if (sgrp && !streq_ptr(sgrp->sg_namp, grp->gr_name)) |
| 300 |
return -EINVAL; |
| 301 |
|
| 302 |
g = group_record_new(); |
| 303 |
if (!g) |
| 304 |
return -ENOMEM; |
| 305 |
|
| 306 |
g->group_name = strdup(grp->gr_name); |
| 307 |
if (!g->group_name) |
| 308 |
return -ENOMEM; |
| 309 |
|
| 310 |
r = strv_extend_strv_utf8_only(&g->members, grp->gr_mem, false); |
| 311 |
if (r < 0) |
| 312 |
return r; |
| 313 |
|
| 314 |
g->gid = grp->gr_gid; |
| 315 |
|
| 316 |
if (sgrp) { |
| 317 |
if (looks_like_hashed_password(utf8_only(sgrp->sg_passwd))) { |
| 318 |
g->hashed_password = strv_new(sgrp->sg_passwd); |
| 319 |
if (!g->hashed_password) |
| 320 |
return -ENOMEM; |
| 321 |
} |
| 322 |
|
| 323 |
r = strv_extend_strv_utf8_only(&g->members, sgrp->sg_mem, true); |
| 324 |
if (r < 0) |
| 325 |
return r; |
| 326 |
|
| 327 |
r = strv_extend_strv_utf8_only(&g->administrators, sgrp->sg_adm, false); |
| 328 |
if (r < 0) |
| 329 |
return r; |
| 330 |
} |
| 331 |
|
| 332 |
r = sd_json_buildo( |
| 333 |
&g->json, |
| 334 |
SD_JSON_BUILD_PAIR_STRING("groupName", g->group_name), |
| 335 |
SD_JSON_BUILD_PAIR_UNSIGNED("gid", g->gid), |
| 336 |
SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(g->members), "members", SD_JSON_BUILD_STRV(g->members)), |
| 337 |
SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(g->hashed_password), "privileged", SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR_STRV("hashedPassword", g->hashed_password))), |
| 338 |
SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(g->administrators), "administrators", SD_JSON_BUILD_STRV(g->administrators))); |
| 339 |
if (r < 0) |
| 340 |
return r; |
| 341 |
|
| 342 |
g->mask = USER_RECORD_REGULAR | |
| 343 |
(!strv_isempty(g->hashed_password) ? USER_RECORD_PRIVILEGED : 0); |
| 344 |
|
| 345 |
if (ret) |
| 346 |
*ret = TAKE_PTR(g); |
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
int nss_sgrp_for_group(const struct group *grp, struct sgrp *ret_sgrp, char **ret_buffer) { |
| 351 |
size_t buflen = 4096; |
| 352 |
int r; |
| 353 |
|
| 354 |
assert(grp); |
| 355 |
assert(ret_sgrp); |
| 356 |
assert(ret_buffer); |
| 357 |
|
| 358 |
for (;;) { |
| 359 |
_cleanup_free_ char *buf = NULL; |
| 360 |
struct sgrp sgrp = {}, *result = NULL; |
| 361 |
|
| 362 |
buf = malloc0(buflen); |
| 363 |
if (!buf) |
| 364 |
return -ENOMEM; |
| 365 |
|
| 366 |
r = getsgnam_r(grp->gr_name, &sgrp, buf, buflen, &result); |
| 367 |
if (r == 0) { |
| 368 |
if (!result) |
| 369 |
return -ESRCH; |
| 370 |
|
| 371 |
*ret_sgrp = *result; |
| 372 |
*ret_buffer = TAKE_PTR(buf); |
| 373 |
return 0; |
| 374 |
} |
| 375 |
if (r < 0) |
| 376 |
return -EIO; /* Weird, this should not return negative! */ |
| 377 |
if (r != ERANGE) |
| 378 |
return -r; |
| 379 |
|
| 380 |
if (buflen > SIZE_MAX / 2) |
| 381 |
return -ERANGE; |
| 382 |
|
| 383 |
buflen *= 2; |
| 384 |
buf = mfree(buf); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
int nss_group_record_by_name( |
| 389 |
const char *name, |
| 390 |
bool with_shadow, |
| 391 |
GroupRecord **ret) { |
| 392 |
|
| 393 |
_cleanup_free_ char *sbuf = NULL; |
| 394 |
_cleanup_free_ struct group *result = NULL; |
| 395 |
bool incomplete = false; |
| 396 |
struct sgrp sgrp, *sresult = NULL; |
| 397 |
int r; |
| 398 |
|
| 399 |
assert(name); |
| 400 |
|
| 401 |
r = getgrnam_malloc(name, &result); |
| 402 |
if (r < 0) |
| 403 |
return r; |
| 404 |
|
| 405 |
if (with_shadow) { |
| 406 |
r = nss_sgrp_for_group(result, &sgrp, &sbuf); |
| 407 |
if (r < 0) { |
| 408 |
log_debug_errno(r, "Failed to do shadow lookup for group %s, ignoring: %m", result->gr_name); |
| 409 |
incomplete = ERRNO_IS_PRIVILEGE(r); |
| 410 |
} else |
| 411 |
sresult = &sgrp; |
| 412 |
} else |
| 413 |
incomplete = true; |
| 414 |
|
| 415 |
r = nss_group_to_group_record(result, sresult, ret); |
| 416 |
if (r < 0) |
| 417 |
return r; |
| 418 |
|
| 419 |
if (ret) |
| 420 |
(*ret)->incomplete = incomplete; |
| 421 |
return 0; |
| 422 |
} |
| 423 |
|
| 424 |
int nss_group_record_by_gid( |
| 425 |
gid_t gid, |
| 426 |
bool with_shadow, |
| 427 |
GroupRecord **ret) { |
| 428 |
|
| 429 |
_cleanup_free_ char *sbuf = NULL; |
| 430 |
_cleanup_free_ struct group *result = NULL; |
| 431 |
bool incomplete = false; |
| 432 |
struct sgrp sgrp, *sresult = NULL; |
| 433 |
int r; |
| 434 |
|
| 435 |
r = getgrgid_malloc(gid, &result); |
| 436 |
if (r < 0) |
| 437 |
return r; |
| 438 |
|
| 439 |
if (with_shadow) { |
| 440 |
r = nss_sgrp_for_group(result, &sgrp, &sbuf); |
| 441 |
if (r < 0) { |
| 442 |
log_debug_errno(r, "Failed to do shadow lookup for group %s, ignoring: %m", result->gr_name); |
| 443 |
incomplete = ERRNO_IS_PRIVILEGE(r); |
| 444 |
} else |
| 445 |
sresult = &sgrp; |
| 446 |
} else |
| 447 |
incomplete = true; |
| 448 |
|
| 449 |
r = nss_group_to_group_record(result, sresult, ret); |
| 450 |
if (r < 0) |
| 451 |
return r; |
| 452 |
|
| 453 |
if (ret) |
| 454 |
(*ret)->incomplete = incomplete; |
| 455 |
return 0; |
| 456 |
} |