2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
34 #include "alloc-util.h"
37 #include "format-util.h"
40 #include "parse-util.h"
41 #include "path-util.h"
42 #include "string-util.h"
44 #include "user-util.h"
47 bool uid_is_valid(uid_t uid) {
49 /* Some libc APIs use UID_INVALID as special placeholder */
50 if (uid == (uid_t) UINT32_C(0xFFFFFFFF))
53 /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
54 if (uid == (uid_t) UINT32_C(0xFFFF))
60 int parse_uid(const char *s, uid_t *ret) {
66 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
67 r = safe_atou32(s, &uid);
71 if (!uid_is_valid(uid))
72 return -ENXIO; /* we return ENXIO instead of EINVAL
73 * here, to make it easy to distuingish
74 * invalid numeric uids from invalid
83 char* getlogname_malloc(void) {
87 if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
92 return uid_to_name(uid);
95 #if 0 /// UNNEEDED by elogind
96 char *getusername_malloc(void) {
103 return uid_to_name(getuid());
108 const char **username,
109 uid_t *uid, gid_t *gid,
111 const char **shell) {
119 /* We enforce some special rules for uid=0: in order to avoid
120 * NSS lookups for root we hardcode its data. */
122 if (streq(*username, "root") || streq(*username, "0")) {
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 #if 0 /// UNNEEDED by elogind
183 int get_user_creds_clean(
184 const char **username,
185 uid_t *uid, gid_t *gid,
187 const char **shell) {
191 /* Like get_user_creds(), but resets home/shell to NULL if they don't contain anything relevant. */
193 r = get_user_creds(username, uid, gid, home, shell);
198 (isempty(*shell) || PATH_IN_SET(*shell,
202 "/usr/sbin/nologin")))
206 (isempty(*home) || path_equal(*home, "/")))
212 int get_group_creds(const char **groupname, gid_t *gid) {
218 /* We enforce some special rules for gid=0: in order to avoid
219 * NSS lookups for root we hardcode its data. */
221 if (streq(*groupname, "root") || streq(*groupname, "0")) {
230 if (parse_gid(*groupname, &id) >= 0) {
235 *groupname = g->gr_name;
238 g = getgrnam(*groupname);
242 return errno > 0 ? -errno : -ESRCH;
245 if (!gid_is_valid(g->gr_gid))
255 char* uid_to_name(uid_t uid) {
259 /* Shortcut things to avoid NSS lookups */
261 return strdup("root");
263 if (uid_is_valid(uid)) {
266 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
271 struct passwd pwbuf, *pw = NULL;
272 _cleanup_free_ char *buf = NULL;
274 buf = malloc(bufsize);
278 r = getpwuid_r(uid, &pwbuf, buf, (size_t) bufsize, &pw);
280 return strdup(pw->pw_name);
288 if (asprintf(&ret, UID_FMT, uid) < 0)
294 char* gid_to_name(gid_t gid) {
299 return strdup("root");
301 if (gid_is_valid(gid)) {
304 bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
309 struct group grbuf, *gr = NULL;
310 _cleanup_free_ char *buf = NULL;
312 buf = malloc(bufsize);
316 r = getgrgid_r(gid, &grbuf, buf, (size_t) bufsize, &gr);
318 return strdup(gr->gr_name);
326 if (asprintf(&ret, GID_FMT, gid) < 0)
332 #if 0 /// UNNEEDED by elogind
333 int in_gid(gid_t gid) {
335 int ngroups_max, r, i;
340 if (getegid() == gid)
343 if (!gid_is_valid(gid))
346 ngroups_max = sysconf(_SC_NGROUPS_MAX);
347 assert(ngroups_max > 0);
349 gids = alloca(sizeof(gid_t) * ngroups_max);
351 r = getgroups(ngroups_max, gids);
355 for (i = 0; i < r; i++)
362 int in_group(const char *name) {
366 r = get_group_creds(&name, &gid);
373 int get_home_dir(char **_h) {
381 /* Take the user specified one */
382 e = secure_getenv("HOME");
383 if (e && path_is_absolute(e)) {
392 /* Hardcode home directory for root to avoid NSS */
403 /* Check the database... */
407 return errno > 0 ? -errno : -ESRCH;
409 if (!path_is_absolute(p->pw_dir))
412 h = strdup(p->pw_dir);
420 int get_shell(char **_s) {
428 /* Take the user specified one */
439 /* Hardcode home directory for root to avoid NSS */
442 s = strdup("/bin/sh");
450 /* Check the database... */
454 return errno > 0 ? -errno : -ESRCH;
456 if (!path_is_absolute(p->pw_shell))
459 s = strdup(p->pw_shell);
468 int reset_uid_gid(void) {
471 r = maybe_setgroups(0, NULL);
475 if (setresgid(0, 0, 0) < 0)
478 if (setresuid(0, 0, 0) < 0)
484 #if 0 /// UNNEEDED by elogind
485 int take_etc_passwd_lock(const char *root) {
487 struct flock flock = {
489 .l_whence = SEEK_SET,
497 /* This is roughly the same as lckpwdf(), but not as awful. We
498 * don't want to use alarm() and signals, hence we implement
499 * our own trivial version of this.
501 * Note that shadow-utils also takes per-database locks in
502 * addition to lckpwdf(). However, we don't given that they
503 * are redundant as they invoke lckpwdf() first and keep
504 * it during everything they do. The per-database locks are
505 * awfully racy, and thus we just won't do them. */
508 path = prefix_roota(root, "/etc/.pwd.lock");
510 path = "/etc/.pwd.lock";
512 fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0600);
516 r = fcntl(fd, F_SETLKW, &flock);
526 bool valid_user_group_name(const char *u) {
530 /* Checks if the specified name is a valid user/group name. */
535 if (!(u[0] >= 'a' && u[0] <= 'z') &&
536 !(u[0] >= 'A' && u[0] <= 'Z') &&
540 for (i = u+1; *i; i++) {
541 if (!(*i >= 'a' && *i <= 'z') &&
542 !(*i >= 'A' && *i <= 'Z') &&
543 !(*i >= '0' && *i <= '9') &&
549 sz = sysconf(_SC_LOGIN_NAME_MAX);
552 if ((size_t) (i-u) > (size_t) sz)
555 if ((size_t) (i-u) > UT_NAMESIZE - 1)
561 bool valid_user_group_name_or_id(const char *u) {
563 /* Similar as above, but is also fine with numeric UID/GID specifications, as long as they are in the right
564 * range, and not the invalid user ids. */
569 if (valid_user_group_name(u))
572 return parse_uid(u, NULL) >= 0;
575 bool valid_gecos(const char *d) {
580 if (!utf8_is_valid(d))
583 if (string_has_cc(d, NULL))
586 /* Colons are used as field separators, and hence not OK */
593 bool valid_home(const char *p) {
598 if (!utf8_is_valid(p))
601 if (string_has_cc(p, NULL))
604 if (!path_is_absolute(p))
607 if (!path_is_safe(p))
610 /* Colons are used as field separators, and hence not OK */
617 int maybe_setgroups(size_t size, const gid_t *list) {
620 /* Check if setgroups is allowed before we try to drop all the auxiliary groups */
621 if (size == 0) { /* Dropping all aux groups? */
622 _cleanup_free_ char *setgroups_content = NULL;
625 r = read_one_line_file("/proc/self/setgroups", &setgroups_content);
627 /* Old kernels don't have /proc/self/setgroups, so assume we can use setgroups */
628 can_setgroups = true;
632 can_setgroups = streq(setgroups_content, "allow");
634 if (!can_setgroups) {
635 log_debug("Skipping setgroups(), /proc/self/setgroups is set to 'deny'");
640 if (setgroups(size, list) < 0)