]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/user-util.c
b5e6ce8a8aa285636f821f8f956822e9fb42d544
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* getlogname_malloc(void) {
71 if (isatty(STDIN_FILENO
) && fstat(STDIN_FILENO
, &st
) >= 0)
76 return uid_to_name(uid
);
79 char *getusername_malloc(void) {
86 return uid_to_name(getuid());
90 const char **username
,
91 uid_t
*uid
, gid_t
*gid
,
101 /* We enforce some special rules for uid=0: in order to avoid
102 * NSS lookups for root we hardcode its data. */
104 if (streq(*username
, "root") || streq(*username
, "0")) {
122 if (parse_uid(*username
, &u
) >= 0) {
126 /* If there are multiple users with the same id, make
127 * sure to leave $USER to the configured value instead
128 * of the first occurrence in the database. However if
129 * the uid was configured by a numeric uid, then let's
130 * pick the real username from /etc/passwd. */
132 *username
= p
->pw_name
;
135 p
= getpwnam(*username
);
139 return errno
> 0 ? -errno
: -ESRCH
;
151 *shell
= p
->pw_shell
;
156 int get_group_creds(const char **groupname
, gid_t
*gid
) {
162 /* We enforce some special rules for gid=0: in order to avoid
163 * NSS lookups for root we hardcode its data. */
165 if (streq(*groupname
, "root") || streq(*groupname
, "0")) {
174 if (parse_gid(*groupname
, &id
) >= 0) {
179 *groupname
= g
->gr_name
;
182 g
= getgrnam(*groupname
);
186 return errno
> 0 ? -errno
: -ESRCH
;
194 char* uid_to_name(uid_t uid
) {
198 /* Shortcut things to avoid NSS lookups */
200 return strdup("root");
202 if (uid_is_valid(uid
)) {
205 bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
);
210 struct passwd pwbuf
, *pw
= NULL
;
211 _cleanup_free_
char *buf
= NULL
;
213 buf
= malloc(bufsize
);
217 r
= getpwuid_r(uid
, &pwbuf
, buf
, (size_t) bufsize
, &pw
);
219 return strdup(pw
->pw_name
);
227 if (asprintf(&ret
, UID_FMT
, uid
) < 0)
233 char* gid_to_name(gid_t gid
) {
238 return strdup("root");
240 if (gid_is_valid(gid
)) {
243 bufsize
= sysconf(_SC_GETGR_R_SIZE_MAX
);
248 struct group grbuf
, *gr
= NULL
;
249 _cleanup_free_
char *buf
= NULL
;
251 buf
= malloc(bufsize
);
255 r
= getgrgid_r(gid
, &grbuf
, buf
, (size_t) bufsize
, &gr
);
257 return strdup(gr
->gr_name
);
265 if (asprintf(&ret
, GID_FMT
, gid
) < 0)
271 int in_gid(gid_t gid
) {
273 int ngroups_max
, r
, i
;
278 if (getegid() == gid
)
281 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
282 assert(ngroups_max
> 0);
284 gids
= alloca(sizeof(gid_t
) * ngroups_max
);
286 r
= getgroups(ngroups_max
, gids
);
290 for (i
= 0; i
< r
; i
++)
297 int in_group(const char *name
) {
301 r
= get_group_creds(&name
, &gid
);
308 int get_home_dir(char **_h
) {
316 /* Take the user specified one */
317 e
= secure_getenv("HOME");
318 if (e
&& path_is_absolute(e
)) {
327 /* Hardcode home directory for root to avoid NSS */
338 /* Check the database... */
342 return errno
> 0 ? -errno
: -ESRCH
;
344 if (!path_is_absolute(p
->pw_dir
))
347 h
= strdup(p
->pw_dir
);
355 int get_shell(char **_s
) {
363 /* Take the user specified one */
374 /* Hardcode home directory for root to avoid NSS */
377 s
= strdup("/bin/sh");
385 /* Check the database... */
389 return errno
> 0 ? -errno
: -ESRCH
;
391 if (!path_is_absolute(p
->pw_shell
))
394 s
= strdup(p
->pw_shell
);
402 int reset_uid_gid(void) {
404 if (setgroups(0, NULL
) < 0)
407 if (setresgid(0, 0, 0) < 0)
410 if (setresuid(0, 0, 0) < 0)