]>
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/>.
34 #include "alloc-util.h"
36 #include "formats-util.h"
38 #include "parse-util.h"
39 #include "path-util.h"
40 #include "string-util.h"
41 #include "user-util.h"
43 bool uid_is_valid(uid_t uid
) {
45 /* Some libc APIs use UID_INVALID as special placeholder */
46 if (uid
== (uid_t
) UINT32_C(0xFFFFFFFF))
49 /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
50 if (uid
== (uid_t
) UINT32_C(0xFFFF))
56 int parse_uid(const char *s
, uid_t
*ret
) {
62 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
63 r
= safe_atou32(s
, &uid
);
67 if (!uid_is_valid(uid
))
68 return -ENXIO
; /* we return ENXIO instead of EINVAL
69 * here, to make it easy to distuingish
70 * invalid numeric uids from invalid
79 char* getlogname_malloc(void) {
83 if (isatty(STDIN_FILENO
) && fstat(STDIN_FILENO
, &st
) >= 0)
88 return uid_to_name(uid
);
91 char *getusername_malloc(void) {
98 return uid_to_name(getuid());
102 const char **username
,
103 uid_t
*uid
, gid_t
*gid
,
105 const char **shell
) {
113 /* We enforce some special rules for uid=0: in order to avoid
114 * NSS lookups for root we hardcode its data. */
116 if (streq(*username
, "root") || streq(*username
, "0")) {
134 if (parse_uid(*username
, &u
) >= 0) {
138 /* If there are multiple users with the same id, make
139 * sure to leave $USER to the configured value instead
140 * of the first occurrence in the database. However if
141 * the uid was configured by a numeric uid, then let's
142 * pick the real username from /etc/passwd. */
144 *username
= p
->pw_name
;
147 p
= getpwnam(*username
);
151 return errno
> 0 ? -errno
: -ESRCH
;
154 if (!uid_is_valid(p
->pw_uid
))
161 if (!gid_is_valid(p
->pw_gid
))
171 *shell
= p
->pw_shell
;
176 int get_group_creds(const char **groupname
, gid_t
*gid
) {
182 /* We enforce some special rules for gid=0: in order to avoid
183 * NSS lookups for root we hardcode its data. */
185 if (streq(*groupname
, "root") || streq(*groupname
, "0")) {
194 if (parse_gid(*groupname
, &id
) >= 0) {
199 *groupname
= g
->gr_name
;
202 g
= getgrnam(*groupname
);
206 return errno
> 0 ? -errno
: -ESRCH
;
209 if (!gid_is_valid(g
->gr_gid
))
218 char* uid_to_name(uid_t uid
) {
222 /* Shortcut things to avoid NSS lookups */
224 return strdup("root");
226 if (uid_is_valid(uid
)) {
229 bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
);
234 struct passwd pwbuf
, *pw
= NULL
;
235 _cleanup_free_
char *buf
= NULL
;
237 buf
= malloc(bufsize
);
241 r
= getpwuid_r(uid
, &pwbuf
, buf
, (size_t) bufsize
, &pw
);
243 return strdup(pw
->pw_name
);
251 if (asprintf(&ret
, UID_FMT
, uid
) < 0)
257 char* gid_to_name(gid_t gid
) {
262 return strdup("root");
264 if (gid_is_valid(gid
)) {
267 bufsize
= sysconf(_SC_GETGR_R_SIZE_MAX
);
272 struct group grbuf
, *gr
= NULL
;
273 _cleanup_free_
char *buf
= NULL
;
275 buf
= malloc(bufsize
);
279 r
= getgrgid_r(gid
, &grbuf
, buf
, (size_t) bufsize
, &gr
);
281 return strdup(gr
->gr_name
);
289 if (asprintf(&ret
, GID_FMT
, gid
) < 0)
295 int in_gid(gid_t gid
) {
297 int ngroups_max
, r
, i
;
302 if (getegid() == gid
)
305 if (!gid_is_valid(gid
))
308 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
309 assert(ngroups_max
> 0);
311 gids
= alloca(sizeof(gid_t
) * ngroups_max
);
313 r
= getgroups(ngroups_max
, gids
);
317 for (i
= 0; i
< r
; i
++)
324 int in_group(const char *name
) {
328 r
= get_group_creds(&name
, &gid
);
335 int get_home_dir(char **_h
) {
343 /* Take the user specified one */
344 e
= secure_getenv("HOME");
345 if (e
&& path_is_absolute(e
)) {
354 /* Hardcode home directory for root to avoid NSS */
365 /* Check the database... */
369 return errno
> 0 ? -errno
: -ESRCH
;
371 if (!path_is_absolute(p
->pw_dir
))
374 h
= strdup(p
->pw_dir
);
382 int get_shell(char **_s
) {
390 /* Take the user specified one */
401 /* Hardcode home directory for root to avoid NSS */
404 s
= strdup("/bin/sh");
412 /* Check the database... */
416 return errno
> 0 ? -errno
: -ESRCH
;
418 if (!path_is_absolute(p
->pw_shell
))
421 s
= strdup(p
->pw_shell
);
429 int reset_uid_gid(void) {
431 if (setgroups(0, NULL
) < 0)
434 if (setresgid(0, 0, 0) < 0)
437 if (setresuid(0, 0, 0) < 0)
443 int take_etc_passwd_lock(const char *root
) {
445 struct flock flock
= {
447 .l_whence
= SEEK_SET
,
455 /* This is roughly the same as lckpwdf(), but not as awful. We
456 * don't want to use alarm() and signals, hence we implement
457 * our own trivial version of this.
459 * Note that shadow-utils also takes per-database locks in
460 * addition to lckpwdf(). However, we don't given that they
461 * are redundant as they they invoke lckpwdf() first and keep
462 * it during everything they do. The per-database locks are
463 * awfully racy, and thus we just won't do them. */
466 path
= prefix_roota(root
, "/etc/.pwd.lock");
468 path
= "/etc/.pwd.lock";
470 fd
= open(path
, O_WRONLY
|O_CREAT
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
, 0600);
474 r
= fcntl(fd
, F_SETLKW
, &flock
);