]>
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/>.
25 #include "user-util.h"
28 #include "string-util.h"
29 #include "path-util.h"
31 bool uid_is_valid(uid_t uid
) {
33 /* Some libc APIs use UID_INVALID as special placeholder */
34 if (uid
== (uid_t
) UINT32_C(0xFFFFFFFF))
37 /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
38 if (uid
== (uid_t
) UINT32_C(0xFFFF))
44 int parse_uid(const char *s
, uid_t
*ret
) {
50 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
51 r
= safe_atou32(s
, &uid
);
55 if (!uid_is_valid(uid
))
56 return -ENXIO
; /* we return ENXIO instead of EINVAL
57 * here, to make it easy to distuingish
58 * invalid numeric uids invalid
67 char *lookup_uid(uid_t uid
) {
70 _cleanup_free_
char *buf
= NULL
;
71 struct passwd pwbuf
, *pw
= NULL
;
73 /* Shortcut things to avoid NSS lookups */
75 return strdup("root");
77 bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
);
81 buf
= malloc(bufsize
);
85 if (getpwuid_r(uid
, &pwbuf
, buf
, bufsize
, &pw
) == 0 && pw
)
86 return strdup(pw
->pw_name
);
88 if (asprintf(&name
, UID_FMT
, uid
) < 0)
94 char* getlogname_malloc(void) {
98 if (isatty(STDIN_FILENO
) && fstat(STDIN_FILENO
, &st
) >= 0)
103 return lookup_uid(uid
);
106 char *getusername_malloc(void) {
113 return lookup_uid(getuid());
117 const char **username
,
118 uid_t
*uid
, gid_t
*gid
,
120 const char **shell
) {
128 /* We enforce some special rules for uid=0: in order to avoid
129 * NSS lookups for root we hardcode its data. */
131 if (streq(*username
, "root") || streq(*username
, "0")) {
149 if (parse_uid(*username
, &u
) >= 0) {
153 /* If there are multiple users with the same id, make
154 * sure to leave $USER to the configured value instead
155 * of the first occurrence in the database. However if
156 * the uid was configured by a numeric uid, then let's
157 * pick the real username from /etc/passwd. */
159 *username
= p
->pw_name
;
162 p
= getpwnam(*username
);
166 return errno
> 0 ? -errno
: -ESRCH
;
178 *shell
= p
->pw_shell
;
183 int get_group_creds(const char **groupname
, gid_t
*gid
) {
189 /* We enforce some special rules for gid=0: in order to avoid
190 * NSS lookups for root we hardcode its data. */
192 if (streq(*groupname
, "root") || streq(*groupname
, "0")) {
201 if (parse_gid(*groupname
, &id
) >= 0) {
206 *groupname
= g
->gr_name
;
209 g
= getgrnam(*groupname
);
213 return errno
> 0 ? -errno
: -ESRCH
;
221 char* uid_to_name(uid_t uid
) {
226 return strdup("root");
230 return strdup(p
->pw_name
);
232 if (asprintf(&r
, UID_FMT
, uid
) < 0)
238 char* gid_to_name(gid_t gid
) {
243 return strdup("root");
247 return strdup(p
->gr_name
);
249 if (asprintf(&r
, GID_FMT
, gid
) < 0)
255 int in_gid(gid_t gid
) {
257 int ngroups_max
, r
, i
;
262 if (getegid() == gid
)
265 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
266 assert(ngroups_max
> 0);
268 gids
= alloca(sizeof(gid_t
) * ngroups_max
);
270 r
= getgroups(ngroups_max
, gids
);
274 for (i
= 0; i
< r
; i
++)
281 int in_group(const char *name
) {
285 r
= get_group_creds(&name
, &gid
);
292 int get_home_dir(char **_h
) {
300 /* Take the user specified one */
301 e
= secure_getenv("HOME");
302 if (e
&& path_is_absolute(e
)) {
311 /* Hardcode home directory for root to avoid NSS */
322 /* Check the database... */
326 return errno
> 0 ? -errno
: -ESRCH
;
328 if (!path_is_absolute(p
->pw_dir
))
331 h
= strdup(p
->pw_dir
);
339 int get_shell(char **_s
) {
347 /* Take the user specified one */
358 /* Hardcode home directory for root to avoid NSS */
361 s
= strdup("/bin/sh");
369 /* Check the database... */
373 return errno
> 0 ? -errno
: -ESRCH
;
375 if (!path_is_absolute(p
->pw_shell
))
378 s
= strdup(p
->pw_shell
);
386 int reset_uid_gid(void) {
388 if (setgroups(0, NULL
) < 0)
391 if (setresgid(0, 0, 0) < 0)
394 if (setresuid(0, 0, 0) < 0)