Debian Codegraph @

src/basic/user-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/file.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <utmpx.h>
10
11 #include "sd-messages.h"
12
13 #include "alloc-util.h"
14 #include "chase.h"
15 #include "errno-util.h"
16 #include "extract-word.h"
17 #include "fd-util.h"
18 #include "fileio.h"
19 #include "format-util.h"
20 #include "lock-util.h"
21 #include "log.h"
22 #include "mkdir.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "string-util.h"
26 #include "strv.h"
27 #include "terminal-util.h"
28 #include "user-util.h"
29 #include "utf8.h"
30
31 #define DEFINE_STRERROR_ACCOUNT(type) \
32 const char* strerror_##type( \
33 int errnum, \
34 char *buf, \
35 size_t buflen) { \
36 \
37 errnum = ABS(errnum); \
38 switch (errnum) { \
39 case ESRCH: \
40 return "Unknown " STRINGIFY(type); \
41 case ENOEXEC: \
42 return "Not a system " STRINGIFY(type); \
43 default: \
44 return strerror_r(errnum, buf, buflen); \
45 } \
46 }
47
48 DEFINE_STRERROR_ACCOUNT(user);
49 DEFINE_STRERROR_ACCOUNT(group);
50
51 bool uid_is_valid(uid_t uid) {
52
53 /* Also see POSIX IEEE Std 1003.1-2008, 2016 Edition, 3.436. */
54
55 /* Some libc APIs use UID_INVALID as special placeholder */
56 if (uid == (uid_t) UINT32_C(0xFFFFFFFF))
57 return false;
58
59 /* A long time ago UIDs where 16 bit, hence explicitly avoid the 16-bit -1 too */
60 if (uid == (uid_t) UINT32_C(0xFFFF))
61 return false;
62
63 return true;
64 }
65
66 int parse_uid(const char *s, uid_t *ret) {
67 uint32_t uid = 0;
68 int r;
69
70 assert(s);
71
72 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
73
74 /* We are very strict when parsing UIDs, and prohibit +/- as prefix, leading zero as prefix, and
75 * whitespace. We do this, since this call is often used in a context where we parse things as UID
76 * first, and if that doesn't work we fall back to NSS. Thus we really want to make sure that UIDs
77 * are parsed as UIDs only if they really really look like UIDs. */
78 r = safe_atou32_full(s, 10
79 | SAFE_ATO_REFUSE_PLUS_MINUS
80 | SAFE_ATO_REFUSE_LEADING_ZERO
81 | SAFE_ATO_REFUSE_LEADING_WHITESPACE, &uid);
82 if (r < 0)
83 return r;
84
85 if (!uid_is_valid(uid))
86 return -ENXIO; /* we return ENXIO instead of EINVAL
87 * here, to make it easy to distinguish
88 * invalid numeric uids from invalid
89 * strings. */
90
91 if (ret)
92 *ret = uid;
93
94 return 0;
95 }
96
97 int parse_uid_range(const char *s, uid_t *ret_lower, uid_t *ret_upper) {
98 _cleanup_free_ char *word = NULL;
99 uid_t l, u;
100 int r;
101
102 assert(s);
103 assert(ret_lower);
104 assert(ret_upper);
105
106 r = extract_first_word(&s, &word, "-", EXTRACT_DONT_COALESCE_SEPARATORS);
107 if (r < 0)
108 return r;
109 if (r == 0)
110 return -EINVAL;
111
112 r = parse_uid(word, &l);
113 if (r < 0)
114 return r;
115
116 /* Check for the upper bound and extract it if needed */
117 if (!s)
118 /* Single number with no dash. */
119 u = l;
120 else if (!*s)
121 /* Trailing dash is an error. */
122 return -EINVAL;
123 else {
124 r = parse_uid(s, &u);
125 if (r < 0)
126 return r;
127
128 if (l > u)
129 return -EINVAL;
130 }
131
132 *ret_lower = l;
133 *ret_upper = u;
134 return 0;
135 }
136
137 char* getlogname_malloc(void) {
138 uid_t uid;
139 struct stat st;
140
141 if (isatty_safe(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
142 uid = st.st_uid;
143 else
144 uid = getuid();
145
146 return uid_to_name(uid);
147 }
148
149 char* getusername_malloc(void) {
150 const char *e;
151
152 e = secure_getenv("USER");
153 if (e)
154 return strdup(e);
155
156 return uid_to_name(getuid());
157 }
158
159 bool is_nologin_shell(const char *shell) {
160 return PATH_IN_SET(shell,
161 /* 'nologin' is the friendliest way to disable logins for a user account. It prints a nice
162 * message and exits. Different distributions place the binary at different places though,
163 * hence let's list them all. */
164 "/bin/nologin",
165 "/sbin/nologin",
166 "/usr/bin/nologin",
167 "/usr/sbin/nologin",
168 /* 'true' and 'false' work too for the same purpose, but are less friendly as they don't do
169 * any message printing. Different distributions place the binary at various places but at
170 * least not in the 'sbin' directory. */
171 "/bin/false",
172 "/usr/bin/false",
173 "/bin/true",
174 "/usr/bin/true");
175 }
176
177 bool shell_is_placeholder(const char *shell) {
178 return isempty(shell) || is_nologin_shell(shell);
179 }
180
181 const char* default_root_shell_at(int rfd) {
182 /* We want to use the preferred shell, i.e. DEFAULT_USER_SHELL, which usually
183 * will be /bin/bash. Fall back to /bin/sh if DEFAULT_USER_SHELL is not found,
184 * or any access errors. */
185
186 assert(rfd >= 0 || rfd == AT_FDCWD);
187
188 int r = chaseat(rfd, rfd, DEFAULT_USER_SHELL, /* flags= */ 0, NULL, NULL);
189 if (r < 0 && r != -ENOENT)
190 log_debug_errno(r, "Failed to look up shell '%s': %m", DEFAULT_USER_SHELL);
191 if (r > 0)
192 return DEFAULT_USER_SHELL;
193
194 return "/bin/sh";
195 }
196
197 const char* default_root_shell(const char *root) {
198 _cleanup_close_ int rfd = -EBADF;
199
200 rfd = open(empty_to_root(root), O_CLOEXEC | O_DIRECTORY | O_PATH);
201 if (rfd < 0)
202 return "/bin/sh";
203
204 return default_root_shell_at(rfd);
205 }
206
207 static int return_user_creds(
208 const char *username,
209 uid_t uid, gid_t gid,
210 const char *home,
211 const char *shell,
212 char **ret_username,
213 uid_t *ret_uid, gid_t *ret_gid,
214 char **ret_home,
215 char **ret_shell) {
216 /* Helper function to help with the strdups and atomic setting of return params. */
217
218 _cleanup_free_ char *s1 = NULL, *s2 = NULL, *s3 = NULL;
219 int r;
220
221 if (ret_username) {
222 r = strdup_to(&s1, username);
223 if (r < 0)
224 return r;
225 }
226
227 if (ret_home) {
228 r = strdup_to(&s2, home);
229 if (r < 0)
230 return r;
231 }
232
233 if (ret_shell) {
234 r = strdup_to(&s3, shell);
235 if (r < 0)
236 return r;
237 }
238
239 if (ret_username)
240 *ret_username = TAKE_PTR(s1);
241 if (ret_uid)
242 *ret_uid = uid;
243 if (ret_gid)
244 *ret_gid = gid;
245 if (ret_home)
246 *ret_home = TAKE_PTR(s2);
247 if (ret_shell)
248 *ret_shell = TAKE_PTR(s3);
249 return 0;
250 }
251
252 static int synthesize_user_creds(
253 const char *username,
254 UserCredsFlags flags,
255 char **ret_username,
256 uid_t *ret_uid, gid_t *ret_gid,
257 char **ret_home,
258 char **ret_shell) {
259 assert(username);
260
261 /* We enforce some special rules for uid=0 and uid=65534: in order to avoid nss lookups for root we
262 * hardcode their user record data. */
263 if (STR_IN_SET(username, "root", "0"))
264 return return_user_creds("root", 0, 0,
265 "/root",
266 ret_shell ? default_root_shell(NULL) : NULL,
267 ret_username,
268 ret_uid, ret_gid,
269 ret_home,
270 ret_shell);
271
272 if (STR_IN_SET(username, NOBODY_USER_NAME, "65534") &&
273 synthesize_nobody())
274 return return_user_creds(NOBODY_USER_NAME, UID_NOBODY, GID_NOBODY,
275 FLAGS_SET(flags, USER_CREDS_SUPPRESS_PLACEHOLDER) ? NULL : "/",
276 FLAGS_SET(flags, USER_CREDS_SUPPRESS_PLACEHOLDER) ? NULL : NOLOGIN,
277 ret_username,
278 ret_uid, ret_gid,
279 ret_home,
280 ret_shell);
281
282 return -ENOMEDIUM;
283 }
284
285 int get_user_creds(
286 const char *username,
287 UserCredsFlags flags,
288 char **ret_username,
289 uid_t *ret_uid, gid_t *ret_gid,
290 char **ret_home,
291 char **ret_shell) {
292
293 uid_t u = UID_INVALID;
294 _cleanup_free_ struct passwd *pw = NULL;
295 int r;
296
297 assert(username);
298 assert((ret_home || ret_shell) || !(flags & (USER_CREDS_SUPPRESS_PLACEHOLDER|USER_CREDS_CLEAN)));
299
300 if (!FLAGS_SET(flags, USER_CREDS_PREFER_NSS) ||
301 (!ret_home && !ret_shell)) {
302
303 /* So here's the deal: normally, we'll try to synthesize all records we can synthesize, and override
304 * the user database with that. However, if the user specifies USER_CREDS_PREFER_NSS then the
305 * user database will override the synthetic records instead — except if the user is only interested in
306 * the UID and/or GID (but not the home directory, or the shell), in which case we'll always override
307 * the user database (i.e. the USER_CREDS_PREFER_NSS flag has no effect in this case). Why?
308 * Simply because there are valid usecase where the user might change the home directory or the shell
309 * of the relevant users, but changing the UID/GID mappings for them is something we explicitly don't
310 * support. */
311
312 r = synthesize_user_creds(username, flags, ret_username, ret_uid, ret_gid, ret_home, ret_shell);
313 if (r >= 0)
314 return 0;
315 if (r != -ENOMEDIUM) /* not a username we can synthesize */
316 return r;
317 }
318
319 if (parse_uid(username, &u) >= 0) {
320 r = getpwuid_malloc(u, &pw);
321
322 /* If there are multiple users with the same id, make sure to leave $USER to the configured value
323 * instead of the first occurrence in the database. However if the uid was configured by a numeric uid,
324 * then let's pick the real username from /etc/passwd. */
325 if (r >= 0)
326 username = pw->pw_name;
327
328 else if (FLAGS_SET(flags, USER_CREDS_ALLOW_MISSING) && !ret_gid && !ret_home && !ret_shell) {
329 /* If the specified user is a numeric UID and it isn't in the user database, and the caller
330 * passed USER_CREDS_ALLOW_MISSING and was only interested in the UID, then just return that
331 * and don't complain. */
332 if (ret_username)
333 *ret_username = NULL;
334 if (ret_uid)
335 *ret_uid = u;
336 return 0;
337 }
338 } else
339 r = getpwnam_malloc(username, &pw);
340
341 if (r < 0) {
342 /* If the user requested that we only synthesize as fallback, do so now */
343 if (FLAGS_SET(flags, USER_CREDS_PREFER_NSS) &&
344 synthesize_user_creds(username, flags, ret_username, ret_uid, ret_gid, ret_home, ret_shell) >= 0)
345 return 0;
346
347 return r;
348 }
349
350 if (ret_uid && !uid_is_valid(pw->pw_uid))
351 return -EBADMSG;
352
353 if (ret_gid && !gid_is_valid(pw->pw_gid))
354 return -EBADMSG;
355
356 /* Note: we don't insist on normalized paths, since there are setups that have /./ in the path */
357 const char *h =
358 (FLAGS_SET(flags, USER_CREDS_SUPPRESS_PLACEHOLDER) && empty_or_root(pw->pw_dir)) ||
359 (FLAGS_SET(flags, USER_CREDS_CLEAN) && (!path_is_valid(pw->pw_dir) || !path_is_absolute(pw->pw_dir)))
360 ? NULL : pw->pw_dir;
361
362 const char *s =
363 (FLAGS_SET(flags, USER_CREDS_SUPPRESS_PLACEHOLDER) && shell_is_placeholder(pw->pw_shell)) ||
364 (FLAGS_SET(flags, USER_CREDS_CLEAN) && (!path_is_valid(pw->pw_shell) || !path_is_absolute(pw->pw_shell)))
365 ? NULL : pw->pw_shell;
366
367 return return_user_creds(username, pw->pw_uid, pw->pw_gid, h, s,
368 ret_username, ret_uid, ret_gid, ret_home, ret_shell);
369 }
370
371 static int synthesize_group_creds(const char *groupname, char **ret_name, gid_t *ret_gid) {
372 assert(groupname);
373
374 gid_t id;
375 const char *n;
376 int r;
377
378 if (STR_IN_SET(groupname, "root", "0")) {
379 id = 0;
380 n = "root";
381 } else if (STR_IN_SET(groupname, NOBODY_GROUP_NAME, "65534") &&
382 synthesize_nobody()) {
383 id = GID_NOBODY;
384 n = NOBODY_GROUP_NAME;
385 } else
386 return -ENOMEDIUM;
387
388 r = strdup_to_full(ret_name, n);
389 if (r < 0)
390 return r;
391 if (ret_gid)
392 *ret_gid = id;
393 return 0;
394 }
395
396 int get_group_creds(const char *groupname, UserCredsFlags flags, char **ret_name, gid_t *ret_gid) {
397 _cleanup_free_ struct group *gr = NULL;
398 gid_t id;
399 int r;
400
401 assert(groupname);
402
403 if (!FLAGS_SET(flags, USER_CREDS_PREFER_NSS)) {
404 r = synthesize_group_creds(groupname, ret_name, ret_gid);
405 if (r >= 0)
406 return 0;
407 if (r != -ENOMEDIUM) /* not a groupname we can synthesize */
408 return r;
409 }
410
411 if (parse_gid(groupname, &id) >= 0) {
412 r = getgrgid_malloc(id, &gr);
413 if (r >= 0)
414 groupname = gr->gr_name;
415 else if (FLAGS_SET(flags, USER_CREDS_ALLOW_MISSING)) {
416 if (ret_gid)
417 *ret_gid = id;
418 if (ret_name)
419 *ret_name = NULL;
420 return 0;
421 }
422 } else
423 r = getgrnam_malloc(groupname, &gr);
424
425 if (r < 0) {
426 if (FLAGS_SET(flags, USER_CREDS_PREFER_NSS) &&
427 synthesize_group_creds(groupname, ret_name, ret_gid) >= 0)
428 return 0;
429 return r;
430 }
431
432 if (ret_gid && !gid_is_valid(gr->gr_gid))
433 return -EBADMSG;
434
435 r = strdup_to_full(ret_name, groupname);
436 if (r < 0)
437 return r;
438 if (ret_gid)
439 *ret_gid = gr->gr_gid;
440 return 0;
441 }
442
443 char* uid_to_name(uid_t uid) {
444 char *ret;
445 int r;
446
447 /* Shortcut things to avoid NSS lookups */
448 if (uid == 0)
449 return strdup("root");
450 if (uid == UID_NOBODY && synthesize_nobody())
451 return strdup(NOBODY_USER_NAME);
452
453 if (uid_is_valid(uid)) {
454 _cleanup_free_ struct passwd *pw = NULL;
455
456 r = getpwuid_malloc(uid, &pw);
457 if (r >= 0)
458 return strdup(pw->pw_name);
459 }
460
461 if (asprintf(&ret, UID_FMT, uid) < 0)
462 return NULL;
463
464 return ret;
465 }
466
467 char* gid_to_name(gid_t gid) {
468 char *ret;
469 int r;
470
471 if (gid == 0)
472 return strdup("root");
473 if (gid == GID_NOBODY && synthesize_nobody())
474 return strdup(NOBODY_GROUP_NAME);
475
476 if (gid_is_valid(gid)) {
477 _cleanup_free_ struct group *gr = NULL;
478
479 r = getgrgid_malloc(gid, &gr);
480 if (r >= 0)
481 return strdup(gr->gr_name);
482 }
483
484 if (asprintf(&ret, GID_FMT, gid) < 0)
485 return NULL;
486
487 return ret;
488 }
489
490 static bool gid_list_has(const gid_t *list, size_t size, gid_t val) {
491 assert(list || size == 0);
492
493 FOREACH_ARRAY(i, list, size)
494 if (*i == val)
495 return true;
496
497 return false;
498 }
499
500 int in_gid(gid_t gid) {
501 _cleanup_free_ gid_t *gids = NULL;
502 int ngroups;
503
504 if (getgid() == gid)
505 return 1;
506
507 if (getegid() == gid)
508 return 1;
509
510 if (!gid_is_valid(gid))
511 return -EINVAL;
512
513 ngroups = getgroups_alloc(&gids);
514 if (ngroups < 0)
515 return ngroups;
516
517 return gid_list_has(gids, ngroups, gid);
518 }
519
520 int in_group(const char *name) {
521 int r;
522 gid_t gid;
523
524 r = get_group_creds(name, /* flags= */ 0, /* ret_name= */ NULL, &gid);
525 if (r < 0)
526 return r;
527
528 return in_gid(gid);
529 }
530
531 int merge_gid_lists(const gid_t *list1, size_t size1, const gid_t *list2, size_t size2, gid_t **ret) {
532 size_t nresult = 0;
533 assert(ret);
534
535 if (size2 > INT_MAX - size1)
536 return -ENOBUFS;
537
538 gid_t *buf = new(gid_t, size1 + size2);
539 if (!buf)
540 return -ENOMEM;
541
542 /* Duplicates need to be skipped on merging, otherwise they'll be passed on and stored in the kernel. */
543 for (size_t i = 0; i < size1; i++)
544 if (!gid_list_has(buf, nresult, list1[i]))
545 buf[nresult++] = list1[i];
546 for (size_t i = 0; i < size2; i++)
547 if (!gid_list_has(buf, nresult, list2[i]))
548 buf[nresult++] = list2[i];
549 *ret = buf;
550 return (int)nresult;
551 }
552
553 int getgroups_alloc(gid_t **ret) {
554 int ngroups = 8;
555
556 assert(ret);
557
558 for (unsigned attempt = 0;;) {
559 _cleanup_free_ gid_t *p = NULL;
560
561 p = new(gid_t, ngroups);
562 if (!p)
563 return -ENOMEM;
564
565 ngroups = getgroups(ngroups, p);
566 if (ngroups > 0) {
567 *ret = TAKE_PTR(p);
568 return ngroups;
569 }
570 if (ngroups == 0)
571 break;
572 if (errno != EINVAL)
573 return -errno;
574
575 /* Give up eventually */
576 if (attempt++ > 10)
577 return -EINVAL;
578
579 /* Get actual size needed, and size the array explicitly. Note that this is potentially racy
580 * to use (in multi-threaded programs), hence let's call this in a loop. */
581 ngroups = getgroups(0, NULL);
582 if (ngroups < 0)
583 return -errno;
584 if (ngroups == 0)
585 break;
586 }
587
588 *ret = NULL;
589 return 0;
590 }
591
592 int get_home_dir(char **ret) {
593 _cleanup_free_ struct passwd *p = NULL;
594 const char *e;
595 uid_t u;
596 int r;
597
598 assert(ret);
599
600 /* Take the user specified one */
601 e = secure_getenv("HOME");
602 if (e && path_is_valid(e) && path_is_absolute(e))
603 goto found;
604
605 /* Hardcode home directory for root and nobody to avoid NSS */
606 u = getuid();
607 if (u == 0) {
608 e = "/root";
609 goto found;
610 }
611 if (u == UID_NOBODY && synthesize_nobody()) {
612 e = "/";
613 goto found;
614 }
615
616 /* Check the database... */
617 r = getpwuid_malloc(u, &p);
618 if (r < 0)
619 return r;
620
621 e = p->pw_dir;
622 if (!path_is_valid(e) || !path_is_absolute(e))
623 return -EINVAL;
624
625 found:
626 return path_simplify_alloc(e, ret);
627 }
628
629 int get_shell(char **ret) {
630 _cleanup_free_ struct passwd *p = NULL;
631 const char *e;
632 uid_t u;
633 int r;
634
635 assert(ret);
636
637 /* Take the user specified one */
638 e = secure_getenv("SHELL");
639 if (e && path_is_valid(e) && path_is_absolute(e))
640 goto found;
641
642 /* Hardcode shell for root and nobody to avoid NSS */
643 u = getuid();
644 if (u == 0) {
645 e = default_root_shell(NULL);
646 goto found;
647 }
648 if (u == UID_NOBODY && synthesize_nobody()) {
649 e = NOLOGIN;
650 goto found;
651 }
652
653 /* Check the database... */
654 r = getpwuid_malloc(u, &p);
655 if (r < 0)
656 return r;
657
658 e = p->pw_shell;
659 if (!path_is_valid(e) || !path_is_absolute(e))
660 return -EINVAL;
661
662 found:
663 return path_simplify_alloc(e, ret);
664 }
665
666 int fully_set_uid_gid(uid_t uid, gid_t gid, const gid_t supplementary_gids[], size_t n_supplementary_gids) {
667 int r;
668
669 assert(supplementary_gids || n_supplementary_gids == 0);
670
671 /* Sets all UIDs and all GIDs to the specified ones. Drops all auxiliary GIDs */
672
673 r = maybe_setgroups(n_supplementary_gids, supplementary_gids);
674 if (r < 0)
675 return r;
676
677 if (gid_is_valid(gid))
678 if (setresgid(gid, gid, gid) < 0)
679 return -errno;
680
681 if (uid_is_valid(uid))
682 if (setresuid(uid, uid, uid) < 0)
683 return -errno;
684
685 return 0;
686 }
687
688 int take_etc_passwd_lock(const char *root) {
689 int r;
690
691 /* This is roughly the same as lckpwdf(), but not as awful. We don't want to use alarm() and signals,
692 * hence we implement our own trivial version of this.
693 *
694 * Note that shadow-utils also takes per-database locks in addition to lckpwdf(). However, we don't,
695 * given that they are redundant: they invoke lckpwdf() first and keep it during everything they do.
696 * The per-database locks are awfully racy, and thus we just won't do them. */
697
698 _cleanup_free_ char *path = path_join(root, ETC_PASSWD_LOCK_PATH);
699 if (!path)
700 return log_oom_debug();
701
702 (void) mkdir_parents(path, 0755);
703
704 _cleanup_close_ int fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0600);
705 if (fd < 0)
706 return log_debug_errno(errno, "Cannot open %s: %m", path);
707
708 r = unposix_lock(fd, LOCK_EX);
709 if (r < 0)
710 return log_debug_errno(r, "Locking %s failed: %m", path);
711
712 return TAKE_FD(fd);
713 }
714
715 bool valid_user_group_name(const char *u, ValidUserFlags flags) {
716 const char *i;
717
718 /* Checks if the specified name is a valid user/group name. There are two flavours of this call:
719 * strict mode is the default which is POSIX plus some extra rules; and relaxed mode where we accept
720 * pretty much everything except the really worst offending names.
721 *
722 * Whenever we synthesize users ourselves we should use the strict mode. But when we process users
723 * created by other stuff, let's be more liberal. */
724
725 if (isempty(u)) /* An empty user name is never valid */
726 return false;
727
728 if (parse_uid(u, NULL) >= 0) /* Something that parses as numeric UID string is valid exactly when the
729 * flag for it is set */
730 return FLAGS_SET(flags, VALID_USER_ALLOW_NUMERIC);
731
732 if (FLAGS_SET(flags, VALID_USER_RELAX)) {
733
734 /* In relaxed mode we just check very superficially. Apparently SSSD and other stuff is
735 * extremely liberal (way too liberal if you ask me, even inserting "@" in user names, which
736 * is bound to cause problems for example when used with an MTA), hence only filter the most
737 * obvious cases, or where things would result in an invalid entry if such a user name would
738 * show up in /etc/passwd (or equivalent getent output).
739 *
740 * Note that we stepped far out of POSIX territory here. It's not our fault though, but
741 * SSSD's, Samba's and everybody else who ignored POSIX on this. (I mean, I am happy to step
742 * outside of POSIX' bounds any day, but I must say in this case I probably wouldn't
743 * have...) */
744
745 if (startswith(u, " ") || endswith(u, " ")) /* At least expect whitespace padding is removed
746 * at front and back (accept in the middle, since
747 * that's apparently a thing on Windows). Note
748 * that this also blocks usernames consisting of
749 * whitespace only. */
750 return false;
751
752 if (!utf8_is_valid(u)) /* We want to synthesize JSON from this, hence insist on UTF-8 */
753 return false;
754
755 if (string_has_cc(u, NULL)) /* CC characters are just dangerous (and \n in particular is the
756 * record separator in /etc/passwd), so we can't allow that. */
757 return false;
758
759 if (strpbrk(u, ":/")) /* Colons are the field separator in /etc/passwd, we can't allow
760 * that. Slashes are special to file systems paths and user names
761 * typically show up in the file system as home directories, hence
762 * don't allow slashes. */
763 return false;
764
765 if (in_charset(u, DIGITS)) /* Don't allow fully numeric strings, they might be confused with
766 * UIDs (note that this test is more broad than the parse_uid()
767 * test above, as it will cover more than the 32-bit range, and it
768 * will detect 65535 (which is in invalid UID, even though in the
769 * unsigned 32 bit range) */
770 return false;
771
772 if (u[0] == '-' && in_charset(u + 1, DIGITS)) /* Don't allow negative fully numeric strings
773 * either. After all some people write 65535 as
774 * -1 (even though that's not even true on
775 * 32-bit uid_t anyway) */
776 return false;
777
778 if (dot_or_dot_dot(u)) /* User names typically become home directory names, and these two are
779 * special in that context, don't allow that. */
780 return false;
781
782 /* Compare with strict result and warn if result doesn't match */
783 if (FLAGS_SET(flags, VALID_USER_WARN) && !valid_user_group_name(u, 0))
784 log_struct(LOG_NOTICE,
785 LOG_MESSAGE("Accepting user/group name '%s', which does not match strict user/group name rules.", u),
786 LOG_ITEM("USER_GROUP_NAME=%s", u),
787 LOG_MESSAGE_ID(SD_MESSAGE_UNSAFE_USER_NAME_STR));
788
789 /* Note that we make no restrictions on the length in relaxed mode! */
790 } else {
791 long sz;
792 size_t l;
793
794 /* Also see POSIX IEEE Std 1003.1-2008, 2016 Edition, 3.437. We are a bit stricter here
795 * however. Specifically we deviate from POSIX rules:
796 *
797 * - We don't allow empty user names (see above)
798 * - We require that names fit into the appropriate utmp field
799 * - We don't allow any dots (this conflicts with chown syntax which permits dots as user/group name separator)
800 * - We don't allow dashes or digit as the first character
801 *
802 * Note that other systems are even more restrictive, and don't permit underscores or uppercase characters.
803 */
804
805 if (!ascii_isalpha(u[0]) &&
806 u[0] != '_')
807 return false;
808
809 for (i = u+1; *i; i++)
810 if (!ascii_isalpha(*i) &&
811 !ascii_isdigit(*i) &&
812 !IN_SET(*i, '_', '-'))
813 return false;
814
815 l = i - u;
816
817 sz = sysconf(_SC_LOGIN_NAME_MAX);
818 assert_se(sz > 0);
819
820 if (l > (size_t) sz) /* glibc: 256 */
821 return false;
822 if (l > NAME_MAX) /* must fit in a filename: 255 */
823 return false;
824 if (l > sizeof_field(struct utmpx, ut_user) - 1) /* must fit in utmp: 31 */
825 return false;
826 }
827
828 return true;
829 }
830
831 bool valid_gecos(const char *d) {
832
833 if (!d)
834 return false;
835
836 if (!utf8_is_valid(d))
837 return false;
838
839 if (string_has_cc(d, NULL))
840 return false;
841
842 /* Colons are used as field separators, and hence not OK */
843 if (strchr(d, ':'))
844 return false;
845
846 return true;
847 }
848
849 char* mangle_gecos(const char *d) {
850 char *mangled;
851
852 /* Makes sure the provided string becomes valid as a GEGOS field, by dropping bad chars. glibc's
853 * putpwent() only changes \n and : to spaces. We do more: replace all CC too, and remove invalid
854 * UTF-8 */
855
856 mangled = strdup(d);
857 if (!mangled)
858 return NULL;
859
860 for (char *i = mangled; *i; i++) {
861 int len;
862
863 if ((uint8_t) *i < (uint8_t) ' ' || *i == ':') {
864 *i = ' ';
865 continue;
866 }
867
868 len = utf8_encoded_valid_unichar(i, SIZE_MAX);
869 if (len < 0) {
870 *i = ' ';
871 continue;
872 }
873
874 i += len - 1;
875 }
876
877 return mangled;
878 }
879
880 bool valid_home(const char *p) {
881 /* Note that this function is also called by valid_shell(), any
882 * changes must account for that. */
883
884 if (isempty(p))
885 return false;
886
887 if (!utf8_is_valid(p))
888 return false;
889
890 if (string_has_cc(p, NULL))
891 return false;
892
893 if (!path_is_absolute(p))
894 return false;
895
896 if (!path_is_normalized(p))
897 return false;
898
899 /* Colons are used as field separators, and hence not OK */
900 if (strchr(p, ':'))
901 return false;
902
903 return true;
904 }
905
906 bool valid_shell(const char *p) {
907 /* We have the same requirements, so just piggy-back on the home check.
908 *
909 * Let's ignore /etc/shells because this is only applicable to real and not system users. It is also
910 * incompatible with the idea of empty /etc/. */
911 if (!valid_home(p))
912 return false;
913
914 return !endswith(p, "/"); /* one additional restriction: shells may not be dirs */
915 }
916
917 int maybe_setgroups(size_t size, const gid_t *list) {
918 int r;
919
920 /* Check if setgroups is allowed before we try to drop all the auxiliary groups */
921 if (size == 0) { /* Dropping all aux groups? */
922
923 /* The kernel refuses setgroups() if there are no GID mappings in the current
924 * user namespace, so check that beforehand and don't try to setgroups() if
925 * there are no GID mappings. */
926 _cleanup_fclose_ FILE *f = fopen("/proc/self/gid_map", "re");
927 if (!f && errno != ENOENT)
928 return -errno;
929 if (f) {
930 r = safe_fgetc(f, /* ret= */ NULL);
931 if (r < 0)
932 return r;
933 if (r == 0) {
934 log_debug("Skipping setgroups(), /proc/self/gid_map is empty");
935 return 0;
936 }
937 }
938
939 _cleanup_free_ char *setgroups_content = NULL;
940 r = read_one_line_file("/proc/self/setgroups", &setgroups_content);
941 if (r < 0 && r != -ENOENT)
942 return r;
943 if (r > 0 && streq(setgroups_content, "deny")) {
944 log_debug("Skipping setgroups(), /proc/self/setgroups is set to 'deny'");
945 return 0;
946 }
947 }
948
949 return RET_NERRNO(setgroups(size, list));
950 }
951
952 bool synthesize_nobody(void) {
953 /* Returns true when we shall synthesize the "nobody" user (which we do by default). This can be turned off by
954 * touching /etc/systemd/dont-synthesize-nobody in order to provide upgrade compatibility with legacy systems
955 * that used the "nobody" user name and group name for other UIDs/GIDs than 65534.
956 *
957 * Note that we do not employ any kind of synchronization on the following caching variable. If the variable is
958 * accessed in multi-threaded programs in the worst case it might happen that we initialize twice, but that
959 * shouldn't matter as each initialization should come to the same result. */
960 static int cache = -1;
961
962 if (cache < 0)
963 cache = access("/etc/systemd/dont-synthesize-nobody", F_OK) < 0;
964
965 return cache;
966 }
967
968 int putpwent_sane(const struct passwd *pw, FILE *stream) {
969 assert(pw);
970 assert(stream);
971
972 errno = 0;
973 if (putpwent(pw, stream) != 0)
974 return errno_or_else(EIO);
975
976 return 0;
977 }
978
979 int putspent_sane(const struct spwd *sp, FILE *stream) {
980 assert(sp);
981 assert(stream);
982
983 errno = 0;
984 if (putspent(sp, stream) != 0)
985 return errno_or_else(EIO);
986
987 return 0;
988 }
989
990 int putgrent_sane(const struct group *gr, FILE *stream) {
991 assert(gr);
992 assert(stream);
993
994 errno = 0;
995 if (putgrent(gr, stream) != 0)
996 return errno_or_else(EIO);
997
998 return 0;
999 }
1000
1001 #if ENABLE_GSHADOW
1002 int putsgent_sane(const struct sgrp *sg, FILE *stream) {
1003 assert(sg);
1004 assert(stream);
1005
1006 errno = 0;
1007 if (putsgent(sg, stream) != 0)
1008 return errno_or_else(EIO);
1009
1010 return 0;
1011 }
1012 #endif
1013
1014 int fgetpwent_sane(FILE *stream, struct passwd **pw) {
1015 assert(stream);
1016 assert(pw);
1017
1018 errno = 0;
1019 struct passwd *p = fgetpwent(stream);
1020 if (!p && !IN_SET(errno, 0, ENOENT))
1021 return -errno;
1022
1023 *pw = p;
1024 return !!p;
1025 }
1026
1027 int fgetspent_sane(FILE *stream, struct spwd **sp) {
1028 assert(stream);
1029 assert(sp);
1030
1031 errno = 0;
1032 struct spwd *s = fgetspent(stream);
1033 if (!s && !IN_SET(errno, 0, ENOENT))
1034 return -errno;
1035
1036 *sp = s;
1037 return !!s;
1038 }
1039
1040 int fgetgrent_sane(FILE *stream, struct group **gr) {
1041 assert(stream);
1042 assert(gr);
1043
1044 errno = 0;
1045 struct group *g = fgetgrent(stream);
1046 if (!g && !IN_SET(errno, 0, ENOENT))
1047 return -errno;
1048
1049 *gr = g;
1050 return !!g;
1051 }
1052
1053 #if ENABLE_GSHADOW
1054 int fgetsgent_sane(FILE *stream, struct sgrp **sg) {
1055 assert(stream);
1056 assert(sg);
1057
1058 errno = 0;
1059 struct sgrp *s = fgetsgent(stream);
1060 if (!s && !IN_SET(errno, 0, ENOENT))
1061 return -errno;
1062
1063 *sg = s;
1064 return !!s;
1065 }
1066 #endif
1067
1068 int is_this_me(const char *username) {
1069 uid_t uid;
1070 int r;
1071
1072 /* Checks if the specified username is our current one. Passed string might be a UID or a user name. */
1073
1074 r = get_user_creds(username, /* flags= */ USER_CREDS_ALLOW_MISSING, NULL, &uid, NULL, NULL, NULL);
1075 if (r < 0)
1076 return r;
1077
1078 return uid == getuid();
1079 }
1080
1081 const char* get_home_root(void) {
1082 /* For debug purposes allow overriding where we look for home dirs */
1083 const char *e = secure_getenv("SYSTEMD_HOME_ROOT");
1084 if (e && path_is_absolute(e) && path_is_normalized(e))
1085 return e;
1086
1087 return "/home";
1088 }
1089
1090 static size_t getpw_buffer_size(void) {
1091 long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
1092 return bufsize <= 0 ? 4096U : (size_t) bufsize;
1093 }
1094
1095 static bool errno_is_user_doesnt_exist(int error) {
1096 /* See getpwnam(3) and getgrnam(3): those codes and others can be returned if the user or group are
1097 * not found. */
1098 return IN_SET(abs(error), ENOENT, ESRCH, EBADF, EPERM);
1099 }
1100
1101 int getpwnam_malloc(const char *name, struct passwd **ret) {
1102 size_t bufsize = getpw_buffer_size();
1103 int r;
1104
1105 /* A wrapper around getpwnam_r() that allocates the necessary buffer on the heap. The caller must
1106 * free() the returned structures! */
1107
1108 if (isempty(name))
1109 return -EINVAL;
1110
1111 for (;;) {
1112 _cleanup_free_ void *buf = NULL;
1113
1114 /* Silence static analyzers */
1115 assert(bufsize <= SIZE_MAX - ALIGN(sizeof(struct passwd)));
1116 buf = malloc0(ALIGN(sizeof(struct passwd)) + bufsize);
1117 if (!buf)
1118 return -ENOMEM;
1119
1120 struct passwd *pw = NULL;
1121 r = getpwnam_r(name, buf, (char*) buf + ALIGN(sizeof(struct passwd)), bufsize, &pw);
1122 if (r == 0) {
1123 if (pw) {
1124 if (ret)
1125 *ret = TAKE_PTR(buf);
1126 return 0;
1127 }
1128
1129 return -ESRCH;
1130 }
1131
1132 assert(r > 0);
1133
1134 /* getpwnam() may fail with ENOENT if /etc/passwd is missing. For us that is equivalent to
1135 * the name not being defined. */
1136 if (errno_is_user_doesnt_exist(r))
1137 return -ESRCH;
1138 if (r != ERANGE)
1139 return -r;
1140
1141 if (bufsize > SIZE_MAX/2 - ALIGN(sizeof(struct passwd)))
1142 return -ENOMEM;
1143 bufsize *= 2;
1144 }
1145 }
1146
1147 int getpwuid_malloc(uid_t uid, struct passwd **ret) {
1148 size_t bufsize = getpw_buffer_size();
1149 int r;
1150
1151 if (!uid_is_valid(uid))
1152 return -EINVAL;
1153
1154 for (;;) {
1155 _cleanup_free_ void *buf = NULL;
1156
1157 /* Silence static analyzers */
1158 assert(bufsize <= SIZE_MAX - ALIGN(sizeof(struct passwd)));
1159 buf = malloc0(ALIGN(sizeof(struct passwd)) + bufsize);
1160 if (!buf)
1161 return -ENOMEM;
1162
1163 struct passwd *pw = NULL;
1164 r = getpwuid_r(uid, buf, (char*) buf + ALIGN(sizeof(struct passwd)), bufsize, &pw);
1165 if (r == 0) {
1166 if (pw) {
1167 if (ret)
1168 *ret = TAKE_PTR(buf);
1169 return 0;
1170 }
1171
1172 return -ESRCH;
1173 }
1174
1175 assert(r > 0);
1176
1177 if (errno_is_user_doesnt_exist(r))
1178 return -ESRCH;
1179 if (r != ERANGE)
1180 return -r;
1181
1182 if (bufsize > SIZE_MAX/2 - ALIGN(sizeof(struct passwd)))
1183 return -ENOMEM;
1184 bufsize *= 2;
1185 }
1186 }
1187
1188 static size_t getgr_buffer_size(void) {
1189 long bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
1190 return bufsize <= 0 ? 4096U : (size_t) bufsize;
1191 }
1192
1193 int getgrnam_malloc(const char *name, struct group **ret) {
1194 size_t bufsize = getgr_buffer_size();
1195 int r;
1196
1197 if (isempty(name))
1198 return -EINVAL;
1199
1200 for (;;) {
1201 _cleanup_free_ void *buf = NULL;
1202
1203 /* Silence static analyzers */
1204 assert(bufsize <= SIZE_MAX - ALIGN(sizeof(struct group)));
1205 buf = malloc0(ALIGN(sizeof(struct group)) + bufsize);
1206 if (!buf)
1207 return -ENOMEM;
1208
1209 struct group *gr = NULL;
1210 r = getgrnam_r(name, buf, (char*) buf + ALIGN(sizeof(struct group)), bufsize, &gr);
1211 if (r == 0) {
1212 if (gr) {
1213 if (ret)
1214 *ret = TAKE_PTR(buf);
1215 return 0;
1216 }
1217
1218 return -ESRCH;
1219 }
1220
1221 assert(r > 0);
1222
1223 if (errno_is_user_doesnt_exist(r))
1224 return -ESRCH;
1225 if (r != ERANGE)
1226 return -r;
1227
1228 if (bufsize > SIZE_MAX/2 - ALIGN(sizeof(struct group)))
1229 return -ENOMEM;
1230 bufsize *= 2;
1231 }
1232 }
1233
1234 int getgrgid_malloc(gid_t gid, struct group **ret) {
1235 size_t bufsize = getgr_buffer_size();
1236 int r;
1237
1238 if (!gid_is_valid(gid))
1239 return -EINVAL;
1240
1241 for (;;) {
1242 _cleanup_free_ void *buf = NULL;
1243
1244 /* Silence static analyzers */
1245 assert(bufsize <= SIZE_MAX - ALIGN(sizeof(struct group)));
1246 buf = malloc0(ALIGN(sizeof(struct group)) + bufsize);
1247 if (!buf)
1248 return -ENOMEM;
1249
1250 struct group *gr = NULL;
1251 r = getgrgid_r(gid, buf, (char*) buf + ALIGN(sizeof(struct group)), bufsize, &gr);
1252 if (r == 0) {
1253 if (gr) {
1254 if (ret)
1255 *ret = TAKE_PTR(buf);
1256 return 0;
1257 }
1258
1259 return -ESRCH;
1260 }
1261
1262 assert(r > 0);
1263
1264 if (errno_is_user_doesnt_exist(r))
1265 return -ESRCH;
1266 if (r != ERANGE)
1267 return -r;
1268
1269 if (bufsize > SIZE_MAX/2 - ALIGN(sizeof(struct group)))
1270 return -ENOMEM;
1271 bufsize *= 2;
1272 }
1273 }