]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/user-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 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"
44 bool uid_is_valid(uid_t uid
) {
46 /* Some libc APIs use UID_INVALID as special placeholder */
47 if (uid
== (uid_t
) UINT32_C(0xFFFFFFFF))
50 /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
51 if (uid
== (uid_t
) UINT32_C(0xFFFF))
57 int parse_uid(const char *s
, uid_t
*ret
) {
63 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
64 r
= safe_atou32(s
, &uid
);
68 if (!uid_is_valid(uid
))
69 return -ENXIO
; /* we return ENXIO instead of EINVAL
70 * here, to make it easy to distuingish
71 * invalid numeric uids invalid
80 char* getlogname_malloc(void) {
84 if (isatty(STDIN_FILENO
) && fstat(STDIN_FILENO
, &st
) >= 0)
89 return uid_to_name(uid
);
92 char *getusername_malloc(void) {
99 return uid_to_name(getuid());
103 const char **username
,
104 uid_t
*uid
, gid_t
*gid
,
106 const char **shell
) {
114 /* We enforce some special rules for uid=0: in order to avoid
115 * NSS lookups for root we hardcode its data. */
117 if (streq(*username
, "root") || streq(*username
, "0")) {
135 if (parse_uid(*username
, &u
) >= 0) {
139 /* If there are multiple users with the same id, make
140 * sure to leave $USER to the configured value instead
141 * of the first occurrence in the database. However if
142 * the uid was configured by a numeric uid, then let's
143 * pick the real username from /etc/passwd. */
145 *username
= p
->pw_name
;
148 p
= getpwnam(*username
);
152 return errno
> 0 ? -errno
: -ESRCH
;
155 if (!uid_is_valid(p
->pw_uid
))
162 if (!gid_is_valid(p
->pw_gid
))
172 *shell
= p
->pw_shell
;
177 int get_group_creds(const char **groupname
, gid_t
*gid
) {
183 /* We enforce some special rules for gid=0: in order to avoid
184 * NSS lookups for root we hardcode its data. */
186 if (streq(*groupname
, "root") || streq(*groupname
, "0")) {
195 if (parse_gid(*groupname
, &id
) >= 0) {
200 *groupname
= g
->gr_name
;
203 g
= getgrnam(*groupname
);
207 return errno
> 0 ? -errno
: -ESRCH
;
210 if (!gid_is_valid(g
->gr_gid
))
219 char* uid_to_name(uid_t uid
) {
223 /* Shortcut things to avoid NSS lookups */
225 return strdup("root");
227 if (uid_is_valid(uid
)) {
230 bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
);
235 struct passwd pwbuf
, *pw
= NULL
;
236 _cleanup_free_
char *buf
= NULL
;
238 buf
= malloc(bufsize
);
242 r
= getpwuid_r(uid
, &pwbuf
, buf
, (size_t) bufsize
, &pw
);
244 return strdup(pw
->pw_name
);
252 if (asprintf(&ret
, UID_FMT
, uid
) < 0)
258 char* gid_to_name(gid_t gid
) {
263 return strdup("root");
265 if (gid_is_valid(gid
)) {
268 bufsize
= sysconf(_SC_GETGR_R_SIZE_MAX
);
273 struct group grbuf
, *gr
= NULL
;
274 _cleanup_free_
char *buf
= NULL
;
276 buf
= malloc(bufsize
);
280 r
= getgrgid_r(gid
, &grbuf
, buf
, (size_t) bufsize
, &gr
);
282 return strdup(gr
->gr_name
);
290 if (asprintf(&ret
, GID_FMT
, gid
) < 0)
296 int in_gid(gid_t gid
) {
298 int ngroups_max
, r
, i
;
303 if (getegid() == gid
)
306 if (!gid_is_valid(gid
))
309 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
310 assert(ngroups_max
> 0);
312 gids
= alloca(sizeof(gid_t
) * ngroups_max
);
314 r
= getgroups(ngroups_max
, gids
);
318 for (i
= 0; i
< r
; i
++)
325 int in_group(const char *name
) {
329 r
= get_group_creds(&name
, &gid
);
336 int get_home_dir(char **_h
) {
344 /* Take the user specified one */
345 e
= secure_getenv("HOME");
346 if (e
&& path_is_absolute(e
)) {
355 /* Hardcode home directory for root to avoid NSS */
366 /* Check the database... */
370 return errno
> 0 ? -errno
: -ESRCH
;
372 if (!path_is_absolute(p
->pw_dir
))
375 h
= strdup(p
->pw_dir
);
383 int get_shell(char **_s
) {
391 /* Take the user specified one */
402 /* Hardcode home directory for root to avoid NSS */
405 s
= strdup("/bin/sh");
413 /* Check the database... */
417 return errno
> 0 ? -errno
: -ESRCH
;
419 if (!path_is_absolute(p
->pw_shell
))
422 s
= strdup(p
->pw_shell
);
430 int reset_uid_gid(void) {
432 if (setgroups(0, NULL
) < 0)
435 if (setresgid(0, 0, 0) < 0)
438 if (setresuid(0, 0, 0) < 0)
444 int take_etc_passwd_lock(const char *root
) {
446 struct flock flock
= {
448 .l_whence
= SEEK_SET
,
456 /* This is roughly the same as lckpwdf(), but not as awful. We
457 * don't want to use alarm() and signals, hence we implement
458 * our own trivial version of this.
460 * Note that shadow-utils also takes per-database locks in
461 * addition to lckpwdf(). However, we don't given that they
462 * are redundant as they they invoke lckpwdf() first and keep
463 * it during everything they do. The per-database locks are
464 * awfully racy, and thus we just won't do them. */
467 path
= prefix_roota(root
, "/etc/.pwd.lock");
469 path
= "/etc/.pwd.lock";
471 fd
= open(path
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0600);
475 r
= fcntl(fd
, F_SETLKW
, &flock
);