]>
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/>.
33 #include "alloc-util.h"
35 #include "formats-util.h"
37 #include "parse-util.h"
38 #include "path-util.h"
39 #include "string-util.h"
40 #include "user-util.h"
42 bool uid_is_valid(uid_t uid
) {
44 /* Some libc APIs use UID_INVALID as special placeholder */
45 if (uid
== (uid_t
) UINT32_C(0xFFFFFFFF))
48 /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
49 if (uid
== (uid_t
) UINT32_C(0xFFFF))
55 int parse_uid(const char *s
, uid_t
*ret
) {
61 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
62 r
= safe_atou32(s
, &uid
);
66 if (!uid_is_valid(uid
))
67 return -ENXIO
; /* we return ENXIO instead of EINVAL
68 * here, to make it easy to distuingish
69 * invalid numeric uids from invalid
78 char* getlogname_malloc(void) {
82 if (isatty(STDIN_FILENO
) && fstat(STDIN_FILENO
, &st
) >= 0)
87 return uid_to_name(uid
);
90 char *getusername_malloc(void) {
97 return uid_to_name(getuid());
101 const char **username
,
102 uid_t
*uid
, gid_t
*gid
,
104 const char **shell
) {
112 /* We enforce some special rules for uid=0: in order to avoid
113 * NSS lookups for root we hardcode its data. */
115 if (streq(*username
, "root") || streq(*username
, "0")) {
133 if (parse_uid(*username
, &u
) >= 0) {
137 /* If there are multiple users with the same id, make
138 * sure to leave $USER to the configured value instead
139 * of the first occurrence in the database. However if
140 * the uid was configured by a numeric uid, then let's
141 * pick the real username from /etc/passwd. */
143 *username
= p
->pw_name
;
146 p
= getpwnam(*username
);
150 return errno
> 0 ? -errno
: -ESRCH
;
153 if (!uid_is_valid(p
->pw_uid
))
160 if (!gid_is_valid(p
->pw_gid
))
170 *shell
= p
->pw_shell
;
175 int get_group_creds(const char **groupname
, gid_t
*gid
) {
181 /* We enforce some special rules for gid=0: in order to avoid
182 * NSS lookups for root we hardcode its data. */
184 if (streq(*groupname
, "root") || streq(*groupname
, "0")) {
193 if (parse_gid(*groupname
, &id
) >= 0) {
198 *groupname
= g
->gr_name
;
201 g
= getgrnam(*groupname
);
205 return errno
> 0 ? -errno
: -ESRCH
;
208 if (!gid_is_valid(g
->gr_gid
))
217 char* uid_to_name(uid_t uid
) {
221 /* Shortcut things to avoid NSS lookups */
223 return strdup("root");
225 if (uid_is_valid(uid
)) {
228 bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
);
233 struct passwd pwbuf
, *pw
= NULL
;
234 _cleanup_free_
char *buf
= NULL
;
236 buf
= malloc(bufsize
);
240 r
= getpwuid_r(uid
, &pwbuf
, buf
, (size_t) bufsize
, &pw
);
242 return strdup(pw
->pw_name
);
250 if (asprintf(&ret
, UID_FMT
, uid
) < 0)
256 char* gid_to_name(gid_t gid
) {
261 return strdup("root");
263 if (gid_is_valid(gid
)) {
266 bufsize
= sysconf(_SC_GETGR_R_SIZE_MAX
);
271 struct group grbuf
, *gr
= NULL
;
272 _cleanup_free_
char *buf
= NULL
;
274 buf
= malloc(bufsize
);
278 r
= getgrgid_r(gid
, &grbuf
, buf
, (size_t) bufsize
, &gr
);
280 return strdup(gr
->gr_name
);
288 if (asprintf(&ret
, GID_FMT
, gid
) < 0)
294 int in_gid(gid_t gid
) {
296 int ngroups_max
, r
, i
;
301 if (getegid() == gid
)
304 if (!gid_is_valid(gid
))
307 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
308 assert(ngroups_max
> 0);
310 gids
= alloca(sizeof(gid_t
) * ngroups_max
);
312 r
= getgroups(ngroups_max
, gids
);
316 for (i
= 0; i
< r
; i
++)
323 int in_group(const char *name
) {
327 r
= get_group_creds(&name
, &gid
);
334 int get_home_dir(char **_h
) {
342 /* Take the user specified one */
343 e
= secure_getenv("HOME");
344 if (e
&& path_is_absolute(e
)) {
353 /* Hardcode home directory for root to avoid NSS */
364 /* Check the database... */
368 return errno
> 0 ? -errno
: -ESRCH
;
370 if (!path_is_absolute(p
->pw_dir
))
373 h
= strdup(p
->pw_dir
);
381 int get_shell(char **_s
) {
389 /* Take the user specified one */
400 /* Hardcode home directory for root to avoid NSS */
403 s
= strdup("/bin/sh");
411 /* Check the database... */
415 return errno
> 0 ? -errno
: -ESRCH
;
417 if (!path_is_absolute(p
->pw_shell
))
420 s
= strdup(p
->pw_shell
);
428 int reset_uid_gid(void) {
430 if (setgroups(0, NULL
) < 0)
433 if (setresgid(0, 0, 0) < 0)
436 if (setresuid(0, 0, 0) < 0)
442 int take_etc_passwd_lock(const char *root
) {
444 struct flock flock
= {
446 .l_whence
= SEEK_SET
,
454 /* This is roughly the same as lckpwdf(), but not as awful. We
455 * don't want to use alarm() and signals, hence we implement
456 * our own trivial version of this.
458 * Note that shadow-utils also takes per-database locks in
459 * addition to lckpwdf(). However, we don't given that they
460 * are redundant as they they invoke lckpwdf() first and keep
461 * it during everything they do. The per-database locks are
462 * awfully racy, and thus we just won't do them. */
465 path
= prefix_roota(root
, "/etc/.pwd.lock");
467 path
= "/etc/.pwd.lock";
469 fd
= open(path
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0600);
473 r
= fcntl(fd
, F_SETLKW
, &flock
);