]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/user-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
17 #include "alloc-util.h"
20 #include "format-util.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "string-util.h"
27 #include "user-util.h"
30 bool uid_is_valid(uid_t uid
) {
32 /* Also see POSIX IEEE Std 1003.1-2008, 2016 Edition, 3.436. */
34 /* Some libc APIs use UID_INVALID as special placeholder */
35 if (uid
== (uid_t
) UINT32_C(0xFFFFFFFF))
38 /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
39 if (uid
== (uid_t
) UINT32_C(0xFFFF))
45 int parse_uid(const char *s
, uid_t
*ret
) {
51 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
52 r
= safe_atou32(s
, &uid
);
56 if (!uid_is_valid(uid
))
57 return -ENXIO
; /* we return ENXIO instead of EINVAL
58 * here, to make it easy to distuingish
59 * invalid numeric uids from invalid
68 char* getlogname_malloc(void) {
72 if (isatty(STDIN_FILENO
) && fstat(STDIN_FILENO
, &st
) >= 0)
77 return uid_to_name(uid
);
80 char *getusername_malloc(void) {
87 return uid_to_name(getuid());
91 const char **username
,
92 uid_t
*uid
, gid_t
*gid
,
102 /* We enforce some special rules for uid=0 and uid=65534: in order to avoid NSS lookups for root we hardcode
103 * their user record data. */
105 if (STR_IN_SET(*username
, "root", "0")) {
122 if (synthesize_nobody() &&
123 STR_IN_SET(*username
, NOBODY_USER_NAME
, "65534")) {
124 *username
= NOBODY_USER_NAME
;
135 *shell
= "/sbin/nologin";
140 if (parse_uid(*username
, &u
) >= 0) {
144 /* If there are multiple users with the same id, make
145 * sure to leave $USER to the configured value instead
146 * of the first occurrence in the database. However if
147 * the uid was configured by a numeric uid, then let's
148 * pick the real username from /etc/passwd. */
150 *username
= p
->pw_name
;
153 p
= getpwnam(*username
);
157 return errno
> 0 ? -errno
: -ESRCH
;
160 if (!uid_is_valid(p
->pw_uid
))
167 if (!gid_is_valid(p
->pw_gid
))
177 *shell
= p
->pw_shell
;
182 static inline bool is_nologin_shell(const char *shell
) {
184 return PATH_IN_SET(shell
,
185 /* 'nologin' is the friendliest way to disable logins for a user account. It prints a nice
186 * message and exits. Different distributions place the binary at different places though,
187 * hence let's list them all. */
192 /* 'true' and 'false' work too for the same purpose, but are less friendly as they don't do
193 * any message printing. Different distributions place the binary at various places but at
194 * least not in the 'sbin' directory. */
201 int get_user_creds_clean(
202 const char **username
,
203 uid_t
*uid
, gid_t
*gid
,
205 const char **shell
) {
209 /* Like get_user_creds(), but resets home/shell to NULL if they don't contain anything relevant. */
211 r
= get_user_creds(username
, uid
, gid
, home
, shell
);
216 (isempty(*shell
) || is_nologin_shell(*shell
)))
219 if (home
&& empty_or_root(*home
))
225 int get_group_creds(const char **groupname
, gid_t
*gid
) {
231 /* We enforce some special rules for gid=0: in order to avoid
232 * NSS lookups for root we hardcode its data. */
234 if (STR_IN_SET(*groupname
, "root", "0")) {
243 if (synthesize_nobody() &&
244 STR_IN_SET(*groupname
, NOBODY_GROUP_NAME
, "65534")) {
245 *groupname
= NOBODY_GROUP_NAME
;
253 if (parse_gid(*groupname
, &id
) >= 0) {
258 *groupname
= g
->gr_name
;
261 g
= getgrnam(*groupname
);
265 return errno
> 0 ? -errno
: -ESRCH
;
268 if (!gid_is_valid(g
->gr_gid
))
277 char* uid_to_name(uid_t uid
) {
281 /* Shortcut things to avoid NSS lookups */
283 return strdup("root");
284 if (synthesize_nobody() &&
286 return strdup(NOBODY_USER_NAME
);
288 if (uid_is_valid(uid
)) {
291 bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
);
296 struct passwd pwbuf
, *pw
= NULL
;
297 _cleanup_free_
char *buf
= NULL
;
299 buf
= malloc(bufsize
);
303 r
= getpwuid_r(uid
, &pwbuf
, buf
, (size_t) bufsize
, &pw
);
305 return strdup(pw
->pw_name
);
313 if (asprintf(&ret
, UID_FMT
, uid
) < 0)
319 char* gid_to_name(gid_t gid
) {
324 return strdup("root");
325 if (synthesize_nobody() &&
327 return strdup(NOBODY_GROUP_NAME
);
329 if (gid_is_valid(gid
)) {
332 bufsize
= sysconf(_SC_GETGR_R_SIZE_MAX
);
337 struct group grbuf
, *gr
= NULL
;
338 _cleanup_free_
char *buf
= NULL
;
340 buf
= malloc(bufsize
);
344 r
= getgrgid_r(gid
, &grbuf
, buf
, (size_t) bufsize
, &gr
);
346 return strdup(gr
->gr_name
);
354 if (asprintf(&ret
, GID_FMT
, gid
) < 0)
360 int in_gid(gid_t gid
) {
368 if (getegid() == gid
)
371 if (!gid_is_valid(gid
))
374 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
375 assert(ngroups_max
> 0);
377 gids
= newa(gid_t
, ngroups_max
);
379 r
= getgroups(ngroups_max
, gids
);
383 for (i
= 0; i
< r
; i
++)
390 int in_group(const char *name
) {
394 r
= get_group_creds(&name
, &gid
);
401 int get_home_dir(char **_h
) {
409 /* Take the user specified one */
410 e
= secure_getenv("HOME");
411 if (e
&& path_is_absolute(e
)) {
420 /* Hardcode home directory for root and nobody to avoid NSS */
430 if (synthesize_nobody() &&
440 /* Check the database... */
444 return errno
> 0 ? -errno
: -ESRCH
;
446 if (!path_is_absolute(p
->pw_dir
))
449 h
= strdup(p
->pw_dir
);
457 int get_shell(char **_s
) {
465 /* Take the user specified one */
476 /* Hardcode shell for root and nobody to avoid NSS */
479 s
= strdup("/bin/sh");
486 if (synthesize_nobody() &&
488 s
= strdup("/sbin/nologin");
496 /* Check the database... */
500 return errno
> 0 ? -errno
: -ESRCH
;
502 if (!path_is_absolute(p
->pw_shell
))
505 s
= strdup(p
->pw_shell
);
513 int reset_uid_gid(void) {
516 r
= maybe_setgroups(0, NULL
);
520 if (setresgid(0, 0, 0) < 0)
523 if (setresuid(0, 0, 0) < 0)
529 int take_etc_passwd_lock(const char *root
) {
531 struct flock flock
= {
533 .l_whence
= SEEK_SET
,
541 /* This is roughly the same as lckpwdf(), but not as awful. We
542 * don't want to use alarm() and signals, hence we implement
543 * our own trivial version of this.
545 * Note that shadow-utils also takes per-database locks in
546 * addition to lckpwdf(). However, we don't given that they
547 * are redundant as they invoke lckpwdf() first and keep
548 * it during everything they do. The per-database locks are
549 * awfully racy, and thus we just won't do them. */
552 path
= prefix_roota(root
, ETC_PASSWD_LOCK_PATH
);
554 path
= ETC_PASSWD_LOCK_PATH
;
556 fd
= open(path
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0600);
558 return log_debug_errno(errno
, "Cannot open %s: %m", path
);
560 r
= fcntl(fd
, F_SETLKW
, &flock
);
563 return log_debug_errno(errno
, "Locking %s failed: %m", path
);
569 bool valid_user_group_name(const char *u
) {
573 /* Checks if the specified name is a valid user/group name. Also see POSIX IEEE Std 1003.1-2008, 2016 Edition,
574 * 3.437. We are a bit stricter here however. Specifically we deviate from POSIX rules:
576 * - We don't allow any dots (this would break chown syntax which permits dots as user/group name separator)
577 * - We require that names fit into the appropriate utmp field
578 * - We don't allow empty user names
580 * Note that other systems are even more restrictive, and don't permit underscores or uppercase characters.
586 if (!(u
[0] >= 'a' && u
[0] <= 'z') &&
587 !(u
[0] >= 'A' && u
[0] <= 'Z') &&
591 for (i
= u
+1; *i
; i
++) {
592 if (!(*i
>= 'a' && *i
<= 'z') &&
593 !(*i
>= 'A' && *i
<= 'Z') &&
594 !(*i
>= '0' && *i
<= '9') &&
595 !IN_SET(*i
, '_', '-'))
599 sz
= sysconf(_SC_LOGIN_NAME_MAX
);
602 if ((size_t) (i
-u
) > (size_t) sz
)
605 if ((size_t) (i
-u
) > UT_NAMESIZE
- 1)
611 bool valid_user_group_name_or_id(const char *u
) {
613 /* Similar as above, but is also fine with numeric UID/GID specifications, as long as they are in the right
614 * range, and not the invalid user ids. */
619 if (valid_user_group_name(u
))
622 return parse_uid(u
, NULL
) >= 0;
625 bool valid_gecos(const char *d
) {
630 if (!utf8_is_valid(d
))
633 if (string_has_cc(d
, NULL
))
636 /* Colons are used as field separators, and hence not OK */
643 bool valid_home(const char *p
) {
644 /* Note that this function is also called by valid_shell(), any
645 * changes must account for that. */
650 if (!utf8_is_valid(p
))
653 if (string_has_cc(p
, NULL
))
656 if (!path_is_absolute(p
))
659 if (!path_is_normalized(p
))
662 /* Colons are used as field separators, and hence not OK */
669 int maybe_setgroups(size_t size
, const gid_t
*list
) {
672 /* Check if setgroups is allowed before we try to drop all the auxiliary groups */
673 if (size
== 0) { /* Dropping all aux groups? */
674 _cleanup_free_
char *setgroups_content
= NULL
;
677 r
= read_one_line_file("/proc/self/setgroups", &setgroups_content
);
679 /* Old kernels don't have /proc/self/setgroups, so assume we can use setgroups */
680 can_setgroups
= true;
684 can_setgroups
= streq(setgroups_content
, "allow");
686 if (!can_setgroups
) {
687 log_debug("Skipping setgroups(), /proc/self/setgroups is set to 'deny'");
692 if (setgroups(size
, list
) < 0)
698 bool synthesize_nobody(void) {
703 /* Returns true when we shall synthesize the "nobody" user (which we do by default). This can be turned off by
704 * touching /etc/systemd/dont-synthesize-nobody in order to provide upgrade compatibility with legacy systems
705 * that used the "nobody" user name and group name for other UIDs/GIDs than 65534.
707 * Note that we do not employ any kind of synchronization on the following caching variable. If the variable is
708 * accessed in multi-threaded programs in the worst case it might happen that we initialize twice, but that
709 * shouldn't matter as each initialization should come to the same result. */
710 static int cache
= -1;
713 cache
= access("/etc/systemd/dont-synthesize-nobody", F_OK
) < 0;
719 int putpwent_sane(const struct passwd
*pw
, FILE *stream
) {
724 if (putpwent(pw
, stream
) != 0)
725 return errno
> 0 ? -errno
: -EIO
;
730 int putspent_sane(const struct spwd
*sp
, FILE *stream
) {
735 if (putspent(sp
, stream
) != 0)
736 return errno
> 0 ? -errno
: -EIO
;
741 int putgrent_sane(const struct group
*gr
, FILE *stream
) {
746 if (putgrent(gr
, stream
) != 0)
747 return errno
> 0 ? -errno
: -EIO
;
753 int putsgent_sane(const struct sgrp
*sg
, FILE *stream
) {
758 if (putsgent(sg
, stream
) != 0)
759 return errno
> 0 ? -errno
: -EIO
;
765 int fgetpwent_sane(FILE *stream
, struct passwd
**pw
) {
772 p
= fgetpwent(stream
);
773 if (!p
&& errno
!= ENOENT
)
774 return errno
> 0 ? -errno
: -EIO
;
780 int fgetspent_sane(FILE *stream
, struct spwd
**sp
) {
787 s
= fgetspent(stream
);
788 if (!s
&& errno
!= ENOENT
)
789 return errno
> 0 ? -errno
: -EIO
;
795 int fgetgrent_sane(FILE *stream
, struct group
**gr
) {
802 g
= fgetgrent(stream
);
803 if (!g
&& errno
!= ENOENT
)
804 return errno
> 0 ? -errno
: -EIO
;
811 int fgetsgent_sane(FILE *stream
, struct sgrp
**sg
) {
818 s
= fgetsgent(stream
);
819 if (!s
&& errno
!= ENOENT
)
820 return errno
> 0 ? -errno
: -EIO
;