]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/user-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
22 #include "alloc-util.h"
25 #include "format-util.h"
28 #include "parse-util.h"
29 #include "path-util.h"
30 #include "string-util.h"
32 #include "user-util.h"
35 bool uid_is_valid(uid_t uid
) {
37 /* Also see POSIX IEEE Std 1003.1-2008, 2016 Edition, 3.436. */
39 /* Some libc APIs use UID_INVALID as special placeholder */
40 if (uid
== (uid_t
) UINT32_C(0xFFFFFFFF))
43 /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
44 if (uid
== (uid_t
) UINT32_C(0xFFFF))
50 int parse_uid(const char *s
, uid_t
*ret
) {
56 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
57 r
= safe_atou32(s
, &uid
);
61 if (!uid_is_valid(uid
))
62 return -ENXIO
; /* we return ENXIO instead of EINVAL
63 * here, to make it easy to distuingish
64 * invalid numeric uids from invalid
73 char* getlogname_malloc(void) {
77 if (isatty(STDIN_FILENO
) && fstat(STDIN_FILENO
, &st
) >= 0)
82 return uid_to_name(uid
);
85 char *getusername_malloc(void) {
92 return uid_to_name(getuid());
96 const char **username
,
97 uid_t
*uid
, gid_t
*gid
,
107 /* We enforce some special rules for uid=0 and uid=65534: in order to avoid NSS lookups for root we hardcode
108 * their user record data. */
110 if (STR_IN_SET(*username
, "root", "0")) {
127 if (synthesize_nobody() &&
128 STR_IN_SET(*username
, NOBODY_USER_NAME
, "65534")) {
129 *username
= NOBODY_USER_NAME
;
140 *shell
= "/sbin/nologin";
145 if (parse_uid(*username
, &u
) >= 0) {
149 /* If there are multiple users with the same id, make
150 * sure to leave $USER to the configured value instead
151 * of the first occurrence in the database. However if
152 * the uid was configured by a numeric uid, then let's
153 * pick the real username from /etc/passwd. */
155 *username
= p
->pw_name
;
158 p
= getpwnam(*username
);
162 return errno
> 0 ? -errno
: -ESRCH
;
165 if (!uid_is_valid(p
->pw_uid
))
172 if (!gid_is_valid(p
->pw_gid
))
182 *shell
= p
->pw_shell
;
187 static inline bool is_nologin_shell(const char *shell
) {
189 return PATH_IN_SET(shell
,
190 /* 'nologin' is the friendliest way to disable logins for a user account. It prints a nice
191 * message and exits. Different distributions place the binary at different places though,
192 * hence let's list them all. */
197 /* 'true' and 'false' work too for the same purpose, but are less friendly as they don't do
198 * any message printing. Different distributions place the binary at various places but at
199 * least not in the 'sbin' directory. */
206 int get_user_creds_clean(
207 const char **username
,
208 uid_t
*uid
, gid_t
*gid
,
210 const char **shell
) {
214 /* Like get_user_creds(), but resets home/shell to NULL if they don't contain anything relevant. */
216 r
= get_user_creds(username
, uid
, gid
, home
, shell
);
221 (isempty(*shell
) || is_nologin_shell(*shell
)))
225 (isempty(*home
) || path_equal(*home
, "/")))
231 int get_group_creds(const char **groupname
, gid_t
*gid
) {
237 /* We enforce some special rules for gid=0: in order to avoid
238 * NSS lookups for root we hardcode its data. */
240 if (STR_IN_SET(*groupname
, "root", "0")) {
249 if (synthesize_nobody() &&
250 STR_IN_SET(*groupname
, NOBODY_GROUP_NAME
, "65534")) {
251 *groupname
= NOBODY_GROUP_NAME
;
259 if (parse_gid(*groupname
, &id
) >= 0) {
264 *groupname
= g
->gr_name
;
267 g
= getgrnam(*groupname
);
271 return errno
> 0 ? -errno
: -ESRCH
;
274 if (!gid_is_valid(g
->gr_gid
))
283 char* uid_to_name(uid_t uid
) {
287 /* Shortcut things to avoid NSS lookups */
289 return strdup("root");
290 if (synthesize_nobody() &&
292 return strdup(NOBODY_USER_NAME
);
294 if (uid_is_valid(uid
)) {
297 bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
);
302 struct passwd pwbuf
, *pw
= NULL
;
303 _cleanup_free_
char *buf
= NULL
;
305 buf
= malloc(bufsize
);
309 r
= getpwuid_r(uid
, &pwbuf
, buf
, (size_t) bufsize
, &pw
);
311 return strdup(pw
->pw_name
);
319 if (asprintf(&ret
, UID_FMT
, uid
) < 0)
325 char* gid_to_name(gid_t gid
) {
330 return strdup("root");
331 if (synthesize_nobody() &&
333 return strdup(NOBODY_GROUP_NAME
);
335 if (gid_is_valid(gid
)) {
338 bufsize
= sysconf(_SC_GETGR_R_SIZE_MAX
);
343 struct group grbuf
, *gr
= NULL
;
344 _cleanup_free_
char *buf
= NULL
;
346 buf
= malloc(bufsize
);
350 r
= getgrgid_r(gid
, &grbuf
, buf
, (size_t) bufsize
, &gr
);
352 return strdup(gr
->gr_name
);
360 if (asprintf(&ret
, GID_FMT
, gid
) < 0)
366 int in_gid(gid_t gid
) {
374 if (getegid() == gid
)
377 if (!gid_is_valid(gid
))
380 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
381 assert(ngroups_max
> 0);
383 gids
= newa(gid_t
, ngroups_max
);
385 r
= getgroups(ngroups_max
, gids
);
389 for (i
= 0; i
< r
; i
++)
396 int in_group(const char *name
) {
400 r
= get_group_creds(&name
, &gid
);
407 int get_home_dir(char **_h
) {
415 /* Take the user specified one */
416 e
= secure_getenv("HOME");
417 if (e
&& path_is_absolute(e
)) {
426 /* Hardcode home directory for root and nobody to avoid NSS */
436 if (synthesize_nobody() &&
446 /* Check the database... */
450 return errno
> 0 ? -errno
: -ESRCH
;
452 if (!path_is_absolute(p
->pw_dir
))
455 h
= strdup(p
->pw_dir
);
463 int get_shell(char **_s
) {
471 /* Take the user specified one */
482 /* Hardcode shell for root and nobody to avoid NSS */
485 s
= strdup("/bin/sh");
492 if (synthesize_nobody() &&
494 s
= strdup("/sbin/nologin");
502 /* Check the database... */
506 return errno
> 0 ? -errno
: -ESRCH
;
508 if (!path_is_absolute(p
->pw_shell
))
511 s
= strdup(p
->pw_shell
);
519 int reset_uid_gid(void) {
522 r
= maybe_setgroups(0, NULL
);
526 if (setresgid(0, 0, 0) < 0)
529 if (setresuid(0, 0, 0) < 0)
535 int take_etc_passwd_lock(const char *root
) {
537 struct flock flock
= {
539 .l_whence
= SEEK_SET
,
547 /* This is roughly the same as lckpwdf(), but not as awful. We
548 * don't want to use alarm() and signals, hence we implement
549 * our own trivial version of this.
551 * Note that shadow-utils also takes per-database locks in
552 * addition to lckpwdf(). However, we don't given that they
553 * are redundant as they invoke lckpwdf() first and keep
554 * it during everything they do. The per-database locks are
555 * awfully racy, and thus we just won't do them. */
558 path
= prefix_roota(root
, ETC_PASSWD_LOCK_PATH
);
560 path
= ETC_PASSWD_LOCK_PATH
;
562 fd
= open(path
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0600);
564 return log_debug_errno(errno
, "Cannot open %s: %m", path
);
566 r
= fcntl(fd
, F_SETLKW
, &flock
);
569 return log_debug_errno(errno
, "Locking %s failed: %m", path
);
575 bool valid_user_group_name(const char *u
) {
579 /* Checks if the specified name is a valid user/group name. Also see POSIX IEEE Std 1003.1-2008, 2016 Edition,
580 * 3.437. We are a bit stricter here however. Specifically we deviate from POSIX rules:
582 * - We don't allow any dots (this would break chown syntax which permits dots as user/group name separator)
583 * - We require that names fit into the appropriate utmp field
584 * - We don't allow empty user names
586 * Note that other systems are even more restrictive, and don't permit underscores or uppercase characters.
592 if (!(u
[0] >= 'a' && u
[0] <= 'z') &&
593 !(u
[0] >= 'A' && u
[0] <= 'Z') &&
597 for (i
= u
+1; *i
; i
++) {
598 if (!(*i
>= 'a' && *i
<= 'z') &&
599 !(*i
>= 'A' && *i
<= 'Z') &&
600 !(*i
>= '0' && *i
<= '9') &&
601 !IN_SET(*i
, '_', '-'))
605 sz
= sysconf(_SC_LOGIN_NAME_MAX
);
608 if ((size_t) (i
-u
) > (size_t) sz
)
611 if ((size_t) (i
-u
) > UT_NAMESIZE
- 1)
617 bool valid_user_group_name_or_id(const char *u
) {
619 /* Similar as above, but is also fine with numeric UID/GID specifications, as long as they are in the right
620 * range, and not the invalid user ids. */
625 if (valid_user_group_name(u
))
628 return parse_uid(u
, NULL
) >= 0;
631 bool valid_gecos(const char *d
) {
636 if (!utf8_is_valid(d
))
639 if (string_has_cc(d
, NULL
))
642 /* Colons are used as field separators, and hence not OK */
649 bool valid_home(const char *p
) {
650 /* Note that this function is also called by valid_shell(), any
651 * changes must account for that. */
656 if (!utf8_is_valid(p
))
659 if (string_has_cc(p
, NULL
))
662 if (!path_is_absolute(p
))
665 if (!path_is_normalized(p
))
668 /* Colons are used as field separators, and hence not OK */
675 int maybe_setgroups(size_t size
, const gid_t
*list
) {
678 /* Check if setgroups is allowed before we try to drop all the auxiliary groups */
679 if (size
== 0) { /* Dropping all aux groups? */
680 _cleanup_free_
char *setgroups_content
= NULL
;
683 r
= read_one_line_file("/proc/self/setgroups", &setgroups_content
);
685 /* Old kernels don't have /proc/self/setgroups, so assume we can use setgroups */
686 can_setgroups
= true;
690 can_setgroups
= streq(setgroups_content
, "allow");
692 if (!can_setgroups
) {
693 log_debug("Skipping setgroups(), /proc/self/setgroups is set to 'deny'");
698 if (setgroups(size
, list
) < 0)
704 bool synthesize_nobody(void) {
709 /* Returns true when we shall synthesize the "nobody" user (which we do by default). This can be turned off by
710 * touching /etc/systemd/dont-synthesize-nobody in order to provide upgrade compatibility with legacy systems
711 * that used the "nobody" user name and group name for other UIDs/GIDs than 65534.
713 * Note that we do not employ any kind of synchronization on the following caching variable. If the variable is
714 * accessed in multi-threaded programs in the worst case it might happen that we initialize twice, but that
715 * shouldn't matter as each initialization should come to the same result. */
716 static int cache
= -1;
719 cache
= access("/etc/systemd/dont-synthesize-nobody", F_OK
) < 0;
725 int putpwent_sane(const struct passwd
*pw
, FILE *stream
) {
730 if (putpwent(pw
, stream
) != 0)
731 return errno
> 0 ? -errno
: -EIO
;
736 int putspent_sane(const struct spwd
*sp
, FILE *stream
) {
741 if (putspent(sp
, stream
) != 0)
742 return errno
> 0 ? -errno
: -EIO
;
747 int putgrent_sane(const struct group
*gr
, FILE *stream
) {
752 if (putgrent(gr
, stream
) != 0)
753 return errno
> 0 ? -errno
: -EIO
;
759 int putsgent_sane(const struct sgrp
*sg
, FILE *stream
) {
764 if (putsgent(sg
, stream
) != 0)
765 return errno
> 0 ? -errno
: -EIO
;
771 int fgetpwent_sane(FILE *stream
, struct passwd
**pw
) {
778 p
= fgetpwent(stream
);
782 return errno
> 0 ? -errno
: -EIO
;
789 int fgetspent_sane(FILE *stream
, struct spwd
**sp
) {
796 s
= fgetspent(stream
);
800 return errno
> 0 ? -errno
: -EIO
;
807 int fgetgrent_sane(FILE *stream
, struct group
**gr
) {
814 g
= fgetgrent(stream
);
818 return errno
> 0 ? -errno
: -EIO
;
826 int fgetsgent_sane(FILE *stream
, struct sgrp
**sg
) {
833 s
= fgetsgent(stream
);
837 return errno
> 0 ? -errno
: -EIO
;