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"
36 #include "formats-util.h"
39 #include "parse-util.h"
40 #include "path-util.h"
41 #include "string-util.h"
43 #include "user-util.h"
46 bool uid_is_valid(uid_t uid) {
48 /* Some libc APIs use UID_INVALID as special placeholder */
49 if (uid == (uid_t) UINT32_C(0xFFFFFFFF))
52 /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
53 if (uid == (uid_t) UINT32_C(0xFFFF))
59 int parse_uid(const char *s, uid_t *ret) {
65 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
66 r = safe_atou32(s, &uid);
70 if (!uid_is_valid(uid))
71 return -ENXIO; /* we return ENXIO instead of EINVAL
72 * here, to make it easy to distuingish
73 * invalid numeric uids from invalid
82 char* getlogname_malloc(void) {
86 if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
91 return uid_to_name(uid);
94 #if 0 /// UNNEEDED by elogind
95 char *getusername_malloc(void) {
102 return uid_to_name(getuid());
107 const char **username,
108 uid_t *uid, gid_t *gid,
110 const char **shell) {
118 /* We enforce some special rules for uid=0: in order to avoid
119 * NSS lookups for root we hardcode its data. */
121 if (streq(*username, "root") || streq(*username, "0")) {
139 if (parse_uid(*username, &u) >= 0) {
143 /* If there are multiple users with the same id, make
144 * sure to leave $USER to the configured value instead
145 * of the first occurrence in the database. However if
146 * the uid was configured by a numeric uid, then let's
147 * pick the real username from /etc/passwd. */
149 *username = p->pw_name;
152 p = getpwnam(*username);
156 return errno > 0 ? -errno : -ESRCH;
159 if (!uid_is_valid(p->pw_uid))
166 if (!gid_is_valid(p->pw_gid))
176 *shell = p->pw_shell;
181 int get_user_creds_clean(
182 const char **username,
183 uid_t *uid, gid_t *gid,
185 const char **shell) {
189 /* Like get_user_creds(), but resets home/shell to NULL if they don't contain anything relevant. */
191 r = get_user_creds(username, uid, gid, home, shell);
196 (isempty(*shell) || PATH_IN_SET(*shell,
200 "/usr/sbin/nologin")))
204 (isempty(*home) || path_equal(*home, "/")))
210 int get_group_creds(const char **groupname, gid_t *gid) {
216 /* We enforce some special rules for gid=0: in order to avoid
217 * NSS lookups for root we hardcode its data. */
219 if (streq(*groupname, "root") || streq(*groupname, "0")) {
228 if (parse_gid(*groupname, &id) >= 0) {
233 *groupname = g->gr_name;
236 g = getgrnam(*groupname);
240 return errno > 0 ? -errno : -ESRCH;
243 if (!gid_is_valid(g->gr_gid))
252 char* uid_to_name(uid_t uid) {
256 /* Shortcut things to avoid NSS lookups */
258 return strdup("root");
260 if (uid_is_valid(uid)) {
263 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
268 struct passwd pwbuf, *pw = NULL;
269 _cleanup_free_ char *buf = NULL;
271 buf = malloc(bufsize);
275 r = getpwuid_r(uid, &pwbuf, buf, (size_t) bufsize, &pw);
277 return strdup(pw->pw_name);
285 if (asprintf(&ret, UID_FMT, uid) < 0)
291 char* gid_to_name(gid_t gid) {
296 return strdup("root");
298 if (gid_is_valid(gid)) {
301 bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
306 struct group grbuf, *gr = NULL;
307 _cleanup_free_ char *buf = NULL;
309 buf = malloc(bufsize);
313 r = getgrgid_r(gid, &grbuf, buf, (size_t) bufsize, &gr);
315 return strdup(gr->gr_name);
323 if (asprintf(&ret, GID_FMT, gid) < 0)
329 #if 0 /// UNNEEDED by elogind
330 int in_gid(gid_t gid) {
332 int ngroups_max, r, i;
337 if (getegid() == gid)
340 if (!gid_is_valid(gid))
343 ngroups_max = sysconf(_SC_NGROUPS_MAX);
344 assert(ngroups_max > 0);
346 gids = alloca(sizeof(gid_t) * ngroups_max);
348 r = getgroups(ngroups_max, gids);
352 for (i = 0; i < r; i++)
359 int in_group(const char *name) {
363 r = get_group_creds(&name, &gid);
370 int get_home_dir(char **_h) {
378 /* Take the user specified one */
379 e = secure_getenv("HOME");
380 if (e && path_is_absolute(e)) {
389 /* Hardcode home directory for root to avoid NSS */
400 /* Check the database... */
404 return errno > 0 ? -errno : -ESRCH;
406 if (!path_is_absolute(p->pw_dir))
409 h = strdup(p->pw_dir);
417 int get_shell(char **_s) {
425 /* Take the user specified one */
436 /* Hardcode home directory for root to avoid NSS */
439 s = strdup("/bin/sh");
447 /* Check the database... */
451 return errno > 0 ? -errno : -ESRCH;
453 if (!path_is_absolute(p->pw_shell))
456 s = strdup(p->pw_shell);
465 int reset_uid_gid(void) {
468 r = maybe_setgroups(0, NULL);
472 if (setresgid(0, 0, 0) < 0)
475 if (setresuid(0, 0, 0) < 0)
481 #if 0 /// UNNEEDED by elogind
482 int take_etc_passwd_lock(const char *root) {
484 struct flock flock = {
486 .l_whence = SEEK_SET,
494 /* This is roughly the same as lckpwdf(), but not as awful. We
495 * don't want to use alarm() and signals, hence we implement
496 * our own trivial version of this.
498 * Note that shadow-utils also takes per-database locks in
499 * addition to lckpwdf(). However, we don't given that they
500 * are redundant as they invoke lckpwdf() first and keep
501 * it during everything they do. The per-database locks are
502 * awfully racy, and thus we just won't do them. */
505 path = prefix_roota(root, "/etc/.pwd.lock");
507 path = "/etc/.pwd.lock";
509 fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0600);
513 r = fcntl(fd, F_SETLKW, &flock);
523 bool valid_user_group_name(const char *u) {
527 /* Checks if the specified name is a valid user/group name. */
532 if (!(u[0] >= 'a' && u[0] <= 'z') &&
533 !(u[0] >= 'A' && u[0] <= 'Z') &&
537 for (i = u+1; *i; i++) {
538 if (!(*i >= 'a' && *i <= 'z') &&
539 !(*i >= 'A' && *i <= 'Z') &&
540 !(*i >= '0' && *i <= '9') &&
546 sz = sysconf(_SC_LOGIN_NAME_MAX);
549 if ((size_t) (i-u) > (size_t) sz)
552 if ((size_t) (i-u) > UT_NAMESIZE - 1)
558 bool valid_user_group_name_or_id(const char *u) {
560 /* Similar as above, but is also fine with numeric UID/GID specifications, as long as they are in the right
561 * range, and not the invalid user ids. */
566 if (valid_user_group_name(u))
569 return parse_uid(u, NULL) >= 0;
572 bool valid_gecos(const char *d) {
577 if (!utf8_is_valid(d))
580 if (string_has_cc(d, NULL))
583 /* Colons are used as field separators, and hence not OK */
590 bool valid_home(const char *p) {
595 if (!utf8_is_valid(p))
598 if (string_has_cc(p, NULL))
601 if (!path_is_absolute(p))
604 if (!path_is_safe(p))
607 /* Colons are used as field separators, and hence not OK */
614 int maybe_setgroups(size_t size, const gid_t *list) {
617 /* Check if setgroups is allowed before we try to drop all the auxiliary groups */
618 if (size == 0) { /* Dropping all aux groups? */
619 _cleanup_free_ char *setgroups_content = NULL;
622 r = read_one_line_file("/proc/self/setgroups", &setgroups_content);
624 /* Old kernels don't have /proc/self/setgroups, so assume we can use setgroups */
625 can_setgroups = true;
629 can_setgroups = streq(setgroups_content, "allow");
631 if (!can_setgroups) {
632 log_debug("Skipping setgroups(), /proc/self/setgroups is set to 'deny'");
637 if (setgroups(size, list) < 0)