]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/user-util.c
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/>.
35 #include "alloc-util.h"
37 #include "formats-util.h"
39 #include "parse-util.h"
40 #include "path-util.h"
41 #include "string-util.h"
42 #include "user-util.h"
45 bool uid_is_valid(uid_t uid
) {
47 /* Some libc APIs use UID_INVALID as special placeholder */
48 if (uid
== (uid_t
) UINT32_C(0xFFFFFFFF))
51 /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
52 if (uid
== (uid_t
) UINT32_C(0xFFFF))
58 int parse_uid(const char *s
, uid_t
*ret
) {
64 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
65 r
= safe_atou32(s
, &uid
);
69 if (!uid_is_valid(uid
))
70 return -ENXIO
; /* we return ENXIO instead of EINVAL
71 * here, to make it easy to distuingish
72 * invalid numeric uids from invalid
81 char* getlogname_malloc(void) {
85 if (isatty(STDIN_FILENO
) && fstat(STDIN_FILENO
, &st
) >= 0)
90 return uid_to_name(uid
);
93 char *getusername_malloc(void) {
100 return uid_to_name(getuid());
104 const char **username
,
105 uid_t
*uid
, gid_t
*gid
,
107 const char **shell
) {
115 /* We enforce some special rules for uid=0: in order to avoid
116 * NSS lookups for root we hardcode its data. */
118 if (streq(*username
, "root") || streq(*username
, "0")) {
136 if (parse_uid(*username
, &u
) >= 0) {
140 /* If there are multiple users with the same id, make
141 * sure to leave $USER to the configured value instead
142 * of the first occurrence in the database. However if
143 * the uid was configured by a numeric uid, then let's
144 * pick the real username from /etc/passwd. */
146 *username
= p
->pw_name
;
149 p
= getpwnam(*username
);
153 return errno
> 0 ? -errno
: -ESRCH
;
156 if (!uid_is_valid(p
->pw_uid
))
163 if (!gid_is_valid(p
->pw_gid
))
173 *shell
= p
->pw_shell
;
178 int get_group_creds(const char **groupname
, gid_t
*gid
) {
184 /* We enforce some special rules for gid=0: in order to avoid
185 * NSS lookups for root we hardcode its data. */
187 if (streq(*groupname
, "root") || streq(*groupname
, "0")) {
196 if (parse_gid(*groupname
, &id
) >= 0) {
201 *groupname
= g
->gr_name
;
204 g
= getgrnam(*groupname
);
208 return errno
> 0 ? -errno
: -ESRCH
;
211 if (!gid_is_valid(g
->gr_gid
))
220 char* uid_to_name(uid_t uid
) {
224 /* Shortcut things to avoid NSS lookups */
226 return strdup("root");
228 if (uid_is_valid(uid
)) {
231 bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
);
236 struct passwd pwbuf
, *pw
= NULL
;
237 _cleanup_free_
char *buf
= NULL
;
239 buf
= malloc(bufsize
);
243 r
= getpwuid_r(uid
, &pwbuf
, buf
, (size_t) bufsize
, &pw
);
245 return strdup(pw
->pw_name
);
253 if (asprintf(&ret
, UID_FMT
, uid
) < 0)
259 char* gid_to_name(gid_t gid
) {
264 return strdup("root");
266 if (gid_is_valid(gid
)) {
269 bufsize
= sysconf(_SC_GETGR_R_SIZE_MAX
);
274 struct group grbuf
, *gr
= NULL
;
275 _cleanup_free_
char *buf
= NULL
;
277 buf
= malloc(bufsize
);
281 r
= getgrgid_r(gid
, &grbuf
, buf
, (size_t) bufsize
, &gr
);
283 return strdup(gr
->gr_name
);
291 if (asprintf(&ret
, GID_FMT
, gid
) < 0)
297 int in_gid(gid_t gid
) {
299 int ngroups_max
, r
, i
;
304 if (getegid() == gid
)
307 if (!gid_is_valid(gid
))
310 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
311 assert(ngroups_max
> 0);
313 gids
= alloca(sizeof(gid_t
) * ngroups_max
);
315 r
= getgroups(ngroups_max
, gids
);
319 for (i
= 0; i
< r
; i
++)
326 int in_group(const char *name
) {
330 r
= get_group_creds(&name
, &gid
);
337 int get_home_dir(char **_h
) {
345 /* Take the user specified one */
346 e
= secure_getenv("HOME");
347 if (e
&& path_is_absolute(e
)) {
356 /* Hardcode home directory for root to avoid NSS */
367 /* Check the database... */
371 return errno
> 0 ? -errno
: -ESRCH
;
373 if (!path_is_absolute(p
->pw_dir
))
376 h
= strdup(p
->pw_dir
);
384 int get_shell(char **_s
) {
392 /* Take the user specified one */
403 /* Hardcode home directory for root to avoid NSS */
406 s
= strdup("/bin/sh");
414 /* Check the database... */
418 return errno
> 0 ? -errno
: -ESRCH
;
420 if (!path_is_absolute(p
->pw_shell
))
423 s
= strdup(p
->pw_shell
);
431 int reset_uid_gid(void) {
433 if (setgroups(0, NULL
) < 0)
436 if (setresgid(0, 0, 0) < 0)
439 if (setresuid(0, 0, 0) < 0)
445 int take_etc_passwd_lock(const char *root
) {
447 struct flock flock
= {
449 .l_whence
= SEEK_SET
,
457 /* This is roughly the same as lckpwdf(), but not as awful. We
458 * don't want to use alarm() and signals, hence we implement
459 * our own trivial version of this.
461 * Note that shadow-utils also takes per-database locks in
462 * addition to lckpwdf(). However, we don't given that they
463 * are redundant as they invoke lckpwdf() first and keep
464 * it during everything they do. The per-database locks are
465 * awfully racy, and thus we just won't do them. */
468 path
= prefix_roota(root
, "/etc/.pwd.lock");
470 path
= "/etc/.pwd.lock";
472 fd
= open(path
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0600);
476 r
= fcntl(fd
, F_SETLKW
, &flock
);
485 bool valid_user_group_name(const char *u
) {
489 /* Checks if the specified name is a valid user/group name. */
494 if (!(u
[0] >= 'a' && u
[0] <= 'z') &&
495 !(u
[0] >= 'A' && u
[0] <= 'Z') &&
499 for (i
= u
+1; *i
; i
++) {
500 if (!(*i
>= 'a' && *i
<= 'z') &&
501 !(*i
>= 'A' && *i
<= 'Z') &&
502 !(*i
>= '0' && *i
<= '9') &&
508 sz
= sysconf(_SC_LOGIN_NAME_MAX
);
511 if ((size_t) (i
-u
) > (size_t) sz
)
514 if ((size_t) (i
-u
) > UT_NAMESIZE
- 1)
520 bool valid_user_group_name_or_id(const char *u
) {
522 /* Similar as above, but is also fine with numeric UID/GID specifications, as long as they are in the right
523 * range, and not the invalid user ids. */
528 if (valid_user_group_name(u
))
531 return parse_uid(u
, NULL
) >= 0;
534 bool valid_gecos(const char *d
) {
539 if (!utf8_is_valid(d
))
542 if (string_has_cc(d
, NULL
))
545 /* Colons are used as field separators, and hence not OK */
552 bool valid_home(const char *p
) {
557 if (!utf8_is_valid(p
))
560 if (string_has_cc(p
, NULL
))
563 if (!path_is_absolute(p
))
566 if (!path_is_safe(p
))
569 /* Colons are used as field separators, and hence not OK */