]>
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
) 0xFFFFFFFF)
37 /* A long time ago UIDs where 16bit, hence explicitly avoid the 16bit -1 too */
38 if (uid
== (uid_t
) 0xFFFF)
44 int parse_uid(const char *s
, uid_t
* ret_uid
) {
51 r
= safe_atolu(s
, &ul
);
57 if ((unsigned long) uid
!= ul
)
60 if (!uid_is_valid(uid
))
61 return -ENXIO
; /* we return ENXIO instead of EINVAL
62 * here, to make it easy to distuingish
63 * invalid numeric uids invalid
72 char *lookup_uid(uid_t uid
) {
75 _cleanup_free_
char *buf
= NULL
;
76 struct passwd pwbuf
, *pw
= NULL
;
78 /* Shortcut things to avoid NSS lookups */
80 return strdup("root");
82 bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
);
86 buf
= malloc(bufsize
);
90 if (getpwuid_r(uid
, &pwbuf
, buf
, bufsize
, &pw
) == 0 && pw
)
91 return strdup(pw
->pw_name
);
93 if (asprintf(&name
, UID_FMT
, uid
) < 0)
99 char* getlogname_malloc(void) {
103 if (isatty(STDIN_FILENO
) && fstat(STDIN_FILENO
, &st
) >= 0)
108 return lookup_uid(uid
);
111 char *getusername_malloc(void) {
118 return lookup_uid(getuid());
122 const char **username
,
123 uid_t
*uid
, gid_t
*gid
,
125 const char **shell
) {
133 /* We enforce some special rules for uid=0: in order to avoid
134 * NSS lookups for root we hardcode its data. */
136 if (streq(*username
, "root") || streq(*username
, "0")) {
154 if (parse_uid(*username
, &u
) >= 0) {
158 /* If there are multiple users with the same id, make
159 * sure to leave $USER to the configured value instead
160 * of the first occurrence in the database. However if
161 * the uid was configured by a numeric uid, then let's
162 * pick the real username from /etc/passwd. */
164 *username
= p
->pw_name
;
167 p
= getpwnam(*username
);
171 return errno
> 0 ? -errno
: -ESRCH
;
183 *shell
= p
->pw_shell
;
188 int get_group_creds(const char **groupname
, gid_t
*gid
) {
194 /* We enforce some special rules for gid=0: in order to avoid
195 * NSS lookups for root we hardcode its data. */
197 if (streq(*groupname
, "root") || streq(*groupname
, "0")) {
206 if (parse_gid(*groupname
, &id
) >= 0) {
211 *groupname
= g
->gr_name
;
214 g
= getgrnam(*groupname
);
218 return errno
> 0 ? -errno
: -ESRCH
;
226 char* uid_to_name(uid_t uid
) {
231 return strdup("root");
235 return strdup(p
->pw_name
);
237 if (asprintf(&r
, UID_FMT
, uid
) < 0)
243 char* gid_to_name(gid_t gid
) {
248 return strdup("root");
252 return strdup(p
->gr_name
);
254 if (asprintf(&r
, GID_FMT
, gid
) < 0)
260 int in_gid(gid_t gid
) {
262 int ngroups_max
, r
, i
;
267 if (getegid() == gid
)
270 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
271 assert(ngroups_max
> 0);
273 gids
= alloca(sizeof(gid_t
) * ngroups_max
);
275 r
= getgroups(ngroups_max
, gids
);
279 for (i
= 0; i
< r
; i
++)
286 int in_group(const char *name
) {
290 r
= get_group_creds(&name
, &gid
);
297 int get_home_dir(char **_h
) {
305 /* Take the user specified one */
306 e
= secure_getenv("HOME");
307 if (e
&& path_is_absolute(e
)) {
316 /* Hardcode home directory for root to avoid NSS */
327 /* Check the database... */
331 return errno
> 0 ? -errno
: -ESRCH
;
333 if (!path_is_absolute(p
->pw_dir
))
336 h
= strdup(p
->pw_dir
);
344 int get_shell(char **_s
) {
352 /* Take the user specified one */
363 /* Hardcode home directory for root to avoid NSS */
366 s
= strdup("/bin/sh");
374 /* Check the database... */
378 return errno
> 0 ? -errno
: -ESRCH
;
380 if (!path_is_absolute(p
->pw_shell
))
383 s
= strdup(p
->pw_shell
);
391 int reset_uid_gid(void) {
393 if (setgroups(0, NULL
) < 0)
396 if (setresgid(0, 0, 0) < 0)
399 if (setresuid(0, 0, 0) < 0)