]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/user-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
20 #include "alloc-util.h"
23 #include "format-util.h"
26 #include "parse-util.h"
27 #include "path-util.h"
28 #include "string-util.h"
30 #include "user-util.h"
33 bool uid_is_valid(uid_t uid
) {
35 /* Also see POSIX IEEE Std 1003.1-2008, 2016 Edition, 3.436. */
37 /* Some libc APIs use UID_INVALID as special placeholder */
38 if (uid
== (uid_t
) UINT32_C(0xFFFFFFFF))
41 /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
42 if (uid
== (uid_t
) UINT32_C(0xFFFF))
48 int parse_uid(const char *s
, uid_t
*ret
) {
54 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
55 r
= safe_atou32(s
, &uid
);
59 if (!uid_is_valid(uid
))
60 return -ENXIO
; /* we return ENXIO instead of EINVAL
61 * here, to make it easy to distuingish
62 * invalid numeric uids from invalid
71 char* getlogname_malloc(void) {
75 if (isatty(STDIN_FILENO
) && fstat(STDIN_FILENO
, &st
) >= 0)
80 return uid_to_name(uid
);
83 char *getusername_malloc(void) {
90 return uid_to_name(getuid());
94 const char **username
,
95 uid_t
*uid
, gid_t
*gid
,
105 /* We enforce some special rules for uid=0 and uid=65534: in order to avoid NSS lookups for root we hardcode
106 * their user record data. */
108 if (STR_IN_SET(*username
, "root", "0")) {
125 if (synthesize_nobody() &&
126 STR_IN_SET(*username
, NOBODY_USER_NAME
, "65534")) {
127 *username
= NOBODY_USER_NAME
;
138 *shell
= "/sbin/nologin";
143 if (parse_uid(*username
, &u
) >= 0) {
147 /* If there are multiple users with the same id, make
148 * sure to leave $USER to the configured value instead
149 * of the first occurrence in the database. However if
150 * the uid was configured by a numeric uid, then let's
151 * pick the real username from /etc/passwd. */
153 *username
= p
->pw_name
;
156 p
= getpwnam(*username
);
160 return errno
> 0 ? -errno
: -ESRCH
;
163 if (!uid_is_valid(p
->pw_uid
))
170 if (!gid_is_valid(p
->pw_gid
))
180 *shell
= p
->pw_shell
;
185 static inline bool is_nologin_shell(const char *shell
) {
187 return PATH_IN_SET(shell
,
188 /* 'nologin' is the friendliest way to disable logins for a user account. It prints a nice
189 * message and exits. Different distributions place the binary at different places though,
190 * hence let's list them all. */
195 /* 'true' and 'false' work too for the same purpose, but are less friendly as they don't do
196 * any message printing. Different distributions place the binary at various places but at
197 * least not in the 'sbin' directory. */
204 int get_user_creds_clean(
205 const char **username
,
206 uid_t
*uid
, gid_t
*gid
,
208 const char **shell
) {
212 /* Like get_user_creds(), but resets home/shell to NULL if they don't contain anything relevant. */
214 r
= get_user_creds(username
, uid
, gid
, home
, shell
);
219 (isempty(*shell
) || is_nologin_shell(*shell
)))
222 if (home
&& empty_or_root(*home
))
228 int get_group_creds(const char **groupname
, gid_t
*gid
) {
234 /* We enforce some special rules for gid=0: in order to avoid
235 * NSS lookups for root we hardcode its data. */
237 if (STR_IN_SET(*groupname
, "root", "0")) {
246 if (synthesize_nobody() &&
247 STR_IN_SET(*groupname
, NOBODY_GROUP_NAME
, "65534")) {
248 *groupname
= NOBODY_GROUP_NAME
;
256 if (parse_gid(*groupname
, &id
) >= 0) {
261 *groupname
= g
->gr_name
;
264 g
= getgrnam(*groupname
);
268 return errno
> 0 ? -errno
: -ESRCH
;
271 if (!gid_is_valid(g
->gr_gid
))
280 char* uid_to_name(uid_t uid
) {
284 /* Shortcut things to avoid NSS lookups */
286 return strdup("root");
287 if (synthesize_nobody() &&
289 return strdup(NOBODY_USER_NAME
);
291 if (uid_is_valid(uid
)) {
294 bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
);
299 struct passwd pwbuf
, *pw
= NULL
;
300 _cleanup_free_
char *buf
= NULL
;
302 buf
= malloc(bufsize
);
306 r
= getpwuid_r(uid
, &pwbuf
, buf
, (size_t) bufsize
, &pw
);
308 return strdup(pw
->pw_name
);
316 if (asprintf(&ret
, UID_FMT
, uid
) < 0)
322 char* gid_to_name(gid_t gid
) {
327 return strdup("root");
328 if (synthesize_nobody() &&
330 return strdup(NOBODY_GROUP_NAME
);
332 if (gid_is_valid(gid
)) {
335 bufsize
= sysconf(_SC_GETGR_R_SIZE_MAX
);
340 struct group grbuf
, *gr
= NULL
;
341 _cleanup_free_
char *buf
= NULL
;
343 buf
= malloc(bufsize
);
347 r
= getgrgid_r(gid
, &grbuf
, buf
, (size_t) bufsize
, &gr
);
349 return strdup(gr
->gr_name
);
357 if (asprintf(&ret
, GID_FMT
, gid
) < 0)
363 int in_gid(gid_t gid
) {
371 if (getegid() == gid
)
374 if (!gid_is_valid(gid
))
377 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
378 assert(ngroups_max
> 0);
380 gids
= newa(gid_t
, ngroups_max
);
382 r
= getgroups(ngroups_max
, gids
);
386 for (i
= 0; i
< r
; i
++)
393 int in_group(const char *name
) {
397 r
= get_group_creds(&name
, &gid
);
404 int get_home_dir(char **_h
) {
412 /* Take the user specified one */
413 e
= secure_getenv("HOME");
414 if (e
&& path_is_absolute(e
)) {
423 /* Hardcode home directory for root and nobody to avoid NSS */
433 if (synthesize_nobody() &&
443 /* Check the database... */
447 return errno
> 0 ? -errno
: -ESRCH
;
449 if (!path_is_absolute(p
->pw_dir
))
452 h
= strdup(p
->pw_dir
);
460 int get_shell(char **_s
) {
468 /* Take the user specified one */
479 /* Hardcode shell for root and nobody to avoid NSS */
482 s
= strdup("/bin/sh");
489 if (synthesize_nobody() &&
491 s
= strdup("/sbin/nologin");
499 /* Check the database... */
503 return errno
> 0 ? -errno
: -ESRCH
;
505 if (!path_is_absolute(p
->pw_shell
))
508 s
= strdup(p
->pw_shell
);
516 int reset_uid_gid(void) {
519 r
= maybe_setgroups(0, NULL
);
523 if (setresgid(0, 0, 0) < 0)
526 if (setresuid(0, 0, 0) < 0)
532 int take_etc_passwd_lock(const char *root
) {
534 struct flock flock
= {
536 .l_whence
= SEEK_SET
,
544 /* This is roughly the same as lckpwdf(), but not as awful. We
545 * don't want to use alarm() and signals, hence we implement
546 * our own trivial version of this.
548 * Note that shadow-utils also takes per-database locks in
549 * addition to lckpwdf(). However, we don't given that they
550 * are redundant as they invoke lckpwdf() first and keep
551 * it during everything they do. The per-database locks are
552 * awfully racy, and thus we just won't do them. */
555 path
= prefix_roota(root
, ETC_PASSWD_LOCK_PATH
);
557 path
= ETC_PASSWD_LOCK_PATH
;
559 fd
= open(path
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0600);
561 return log_debug_errno(errno
, "Cannot open %s: %m", path
);
563 r
= fcntl(fd
, F_SETLKW
, &flock
);
566 return log_debug_errno(errno
, "Locking %s failed: %m", path
);
572 bool valid_user_group_name(const char *u
) {
576 /* Checks if the specified name is a valid user/group name. Also see POSIX IEEE Std 1003.1-2008, 2016 Edition,
577 * 3.437. We are a bit stricter here however. Specifically we deviate from POSIX rules:
579 * - We don't allow any dots (this would break chown syntax which permits dots as user/group name separator)
580 * - We require that names fit into the appropriate utmp field
581 * - We don't allow empty user names
583 * Note that other systems are even more restrictive, and don't permit underscores or uppercase characters.
589 if (!(u
[0] >= 'a' && u
[0] <= 'z') &&
590 !(u
[0] >= 'A' && u
[0] <= 'Z') &&
594 for (i
= u
+1; *i
; i
++) {
595 if (!(*i
>= 'a' && *i
<= 'z') &&
596 !(*i
>= 'A' && *i
<= 'Z') &&
597 !(*i
>= '0' && *i
<= '9') &&
598 !IN_SET(*i
, '_', '-'))
602 sz
= sysconf(_SC_LOGIN_NAME_MAX
);
605 if ((size_t) (i
-u
) > (size_t) sz
)
608 if ((size_t) (i
-u
) > UT_NAMESIZE
- 1)
614 bool valid_user_group_name_or_id(const char *u
) {
616 /* Similar as above, but is also fine with numeric UID/GID specifications, as long as they are in the right
617 * range, and not the invalid user ids. */
622 if (valid_user_group_name(u
))
625 return parse_uid(u
, NULL
) >= 0;
628 bool valid_gecos(const char *d
) {
633 if (!utf8_is_valid(d
))
636 if (string_has_cc(d
, NULL
))
639 /* Colons are used as field separators, and hence not OK */
646 bool valid_home(const char *p
) {
647 /* Note that this function is also called by valid_shell(), any
648 * changes must account for that. */
653 if (!utf8_is_valid(p
))
656 if (string_has_cc(p
, NULL
))
659 if (!path_is_absolute(p
))
662 if (!path_is_normalized(p
))
665 /* Colons are used as field separators, and hence not OK */
672 int maybe_setgroups(size_t size
, const gid_t
*list
) {
675 /* Check if setgroups is allowed before we try to drop all the auxiliary groups */
676 if (size
== 0) { /* Dropping all aux groups? */
677 _cleanup_free_
char *setgroups_content
= NULL
;
680 r
= read_one_line_file("/proc/self/setgroups", &setgroups_content
);
682 /* Old kernels don't have /proc/self/setgroups, so assume we can use setgroups */
683 can_setgroups
= true;
687 can_setgroups
= streq(setgroups_content
, "allow");
689 if (!can_setgroups
) {
690 log_debug("Skipping setgroups(), /proc/self/setgroups is set to 'deny'");
695 if (setgroups(size
, list
) < 0)
701 bool synthesize_nobody(void) {
706 /* Returns true when we shall synthesize the "nobody" user (which we do by default). This can be turned off by
707 * touching /etc/systemd/dont-synthesize-nobody in order to provide upgrade compatibility with legacy systems
708 * that used the "nobody" user name and group name for other UIDs/GIDs than 65534.
710 * Note that we do not employ any kind of synchronization on the following caching variable. If the variable is
711 * accessed in multi-threaded programs in the worst case it might happen that we initialize twice, but that
712 * shouldn't matter as each initialization should come to the same result. */
713 static int cache
= -1;
716 cache
= access("/etc/systemd/dont-synthesize-nobody", F_OK
) < 0;
722 int putpwent_sane(const struct passwd
*pw
, FILE *stream
) {
727 if (putpwent(pw
, stream
) != 0)
728 return errno
> 0 ? -errno
: -EIO
;
733 int putspent_sane(const struct spwd
*sp
, FILE *stream
) {
738 if (putspent(sp
, stream
) != 0)
739 return errno
> 0 ? -errno
: -EIO
;
744 int putgrent_sane(const struct group
*gr
, FILE *stream
) {
749 if (putgrent(gr
, stream
) != 0)
750 return errno
> 0 ? -errno
: -EIO
;
756 int putsgent_sane(const struct sgrp
*sg
, FILE *stream
) {
761 if (putsgent(sg
, stream
) != 0)
762 return errno
> 0 ? -errno
: -EIO
;
768 int fgetpwent_sane(FILE *stream
, struct passwd
**pw
) {
775 p
= fgetpwent(stream
);
776 if (!p
&& errno
!= ENOENT
)
777 return errno
> 0 ? -errno
: -EIO
;
783 int fgetspent_sane(FILE *stream
, struct spwd
**sp
) {
790 s
= fgetspent(stream
);
791 if (!s
&& errno
!= ENOENT
)
792 return errno
> 0 ? -errno
: -EIO
;
798 int fgetgrent_sane(FILE *stream
, struct group
**gr
) {
805 g
= fgetgrent(stream
);
806 if (!g
&& errno
!= ENOENT
)
807 return errno
> 0 ? -errno
: -EIO
;
814 int fgetsgent_sane(FILE *stream
, struct sgrp
**sg
) {
821 s
= fgetsgent(stream
);
822 if (!s
&& errno
!= ENOENT
)
823 return errno
> 0 ? -errno
: -EIO
;