chiark / gitweb /
basic: use for() loop instead of while()
[elogind.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 <stdint.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 bool uid_is_valid(uid_t uid);
28
29 static inline bool gid_is_valid(gid_t gid) {
30         return uid_is_valid((uid_t) gid);
31 }
32
33 int parse_uid(const char *s, uid_t* ret_uid);
34
35 static inline int parse_gid(const char *s, gid_t *ret_gid) {
36         return parse_uid(s, (uid_t*) ret_gid);
37 }
38
39 char* getlogname_malloc(void);
40 #if 0 /// UNNEEDED by elogind
41 char* getusername_malloc(void);
42 #endif // 0
43
44 int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
45 int get_user_creds_clean(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
46 int get_group_creds(const char **groupname, gid_t *gid);
47
48 char* uid_to_name(uid_t uid);
49 char* gid_to_name(gid_t gid);
50
51 #if 0 /// UNNEEDED by elogind
52 int in_gid(gid_t gid);
53 int in_group(const char *name);
54
55 int get_home_dir(char **ret);
56 int get_shell(char **_ret);
57 #endif // 0
58
59 int reset_uid_gid(void);
60
61 #if 0 /// UNNEEDED by elogind
62 int take_etc_passwd_lock(const char *root);
63 #endif // 0
64
65 #define UID_INVALID ((uid_t) -1)
66 #define GID_INVALID ((gid_t) -1)
67
68 /* Let's pick a UIDs within the 16bit range, so that we are compatible with containers using 16bit
69  * user namespacing. At least on Fedora normal users are allocated until UID 60000, hence do not
70  * allocate from below this. Also stay away from the upper end of the range as that is often used
71  * for overflow/nobody users. */
72 #define DYNAMIC_UID_MIN ((uid_t) UINT32_C(0x0000EF00))
73 #define DYNAMIC_UID_MAX ((uid_t) UINT32_C(0x0000FFEF))
74
75 static inline bool uid_is_dynamic(uid_t uid) {
76         return DYNAMIC_UID_MIN <= uid && uid <= DYNAMIC_UID_MAX;
77 }
78
79 /* The following macros add 1 when converting things, since UID 0 is a valid UID, while the pointer
80  * NULL is special */
81 #define PTR_TO_UID(p) ((uid_t) (((uintptr_t) (p))-1))
82 #define UID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
83
84 #define PTR_TO_GID(p) ((gid_t) (((uintptr_t) (p))-1))
85 #define GID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
86
87 static inline bool userns_supported(void) {
88         return access("/proc/self/uid_map", F_OK) >= 0;
89 }
90
91 bool valid_user_group_name(const char *u);
92 bool valid_user_group_name_or_id(const char *u);
93 bool valid_gecos(const char *d);
94 bool valid_home(const char *p);
95
96 int maybe_setgroups(size_t size, const gid_t *list);