]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/user-util.h
Merge pull request #2471 from michaelolbrich/transient-mounts
[thirdparty/systemd.git] / src / basic / user-util.h
1 #pragma once
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <stdbool.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 bool uid_is_valid(uid_t uid);
27
28 static inline bool gid_is_valid(gid_t gid) {
29 return uid_is_valid((uid_t) gid);
30 }
31
32 int parse_uid(const char *s, uid_t* ret_uid);
33
34 static inline int parse_gid(const char *s, gid_t *ret_gid) {
35 return parse_uid(s, (uid_t*) ret_gid);
36 }
37
38 char* getlogname_malloc(void);
39 char* getusername_malloc(void);
40
41 int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
42 int get_group_creds(const char **groupname, gid_t *gid);
43
44 char* uid_to_name(uid_t uid);
45 char* gid_to_name(gid_t gid);
46
47 int in_gid(gid_t gid);
48 int in_group(const char *name);
49
50 int get_home_dir(char **ret);
51 int get_shell(char **_ret);
52
53 int reset_uid_gid(void);
54
55 int take_etc_passwd_lock(const char *root);
56
57 #define UID_INVALID ((uid_t) -1)
58 #define GID_INVALID ((gid_t) -1)
59
60 /* The following macros add 1 when converting things, since UID 0 is a
61 * valid UID, while the pointer NULL is special */
62 #define PTR_TO_UID(p) ((uid_t) (((uintptr_t) (p))-1))
63 #define UID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
64
65 #define PTR_TO_GID(p) ((gid_t) (((uintptr_t) (p))-1))
66 #define GID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
67
68 static inline bool userns_supported(void) {
69 return access("/proc/self/uid_map", F_OK) >= 0;
70 }
71
72 bool valid_user_group_name(const char *u);
73 bool valid_user_group_name_or_id(const char *u);
74 bool valid_gecos(const char *d);
75 bool valid_home(const char *p);