chiark / gitweb /
nss-systemd,user-util: add a way how synthesizing "nobody" can be turned off
[elogind.git] / src / basic / user-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2010 Lennart Poettering
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 bool uid_is_valid(uid_t uid);
29
30 static inline bool gid_is_valid(gid_t gid) {
31         return uid_is_valid((uid_t) gid);
32 }
33
34 int parse_uid(const char *s, uid_t* ret_uid);
35
36 static inline int parse_gid(const char *s, gid_t *ret_gid) {
37         return parse_uid(s, (uid_t*) ret_gid);
38 }
39
40 char* getlogname_malloc(void);
41 #if 0 /// UNNEEDED by elogind
42 char* getusername_malloc(void);
43 #endif // 0
44
45 int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
46 #if 0 /// UNNEEDED by elogind
47 int get_user_creds_clean(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
48 int get_group_creds(const char **groupname, gid_t *gid);
49 #endif // 0
50
51 char* uid_to_name(uid_t uid);
52 char* gid_to_name(gid_t gid);
53
54 #if 0 /// UNNEEDED by elogind
55 int in_gid(gid_t gid);
56 int in_group(const char *name);
57
58 int get_home_dir(char **ret);
59 int get_shell(char **_ret);
60 #endif // 0
61
62 int reset_uid_gid(void);
63
64 #if 0 /// UNNEEDED by elogind
65 int take_etc_passwd_lock(const char *root);
66 #endif // 0
67
68 #define UID_INVALID ((uid_t) -1)
69 #define GID_INVALID ((gid_t) -1)
70
71 #define UID_NOBODY ((uid_t) 65534U)
72 #define GID_NOBODY ((gid_t) 65534U)
73
74 #if 0 /// UNNEEDED by elogind
75 static inline bool uid_is_dynamic(uid_t uid) {
76         return DYNAMIC_UID_MIN <= uid && uid <= DYNAMIC_UID_MAX;
77 }
78
79 static inline bool gid_is_dynamic(gid_t gid) {
80         return uid_is_dynamic((uid_t) gid);
81 }
82 #endif // 0
83
84 static inline bool uid_is_system(uid_t uid) {
85         return uid <= SYSTEM_UID_MAX;
86 }
87
88 static inline bool gid_is_system(gid_t gid) {
89         return gid <= SYSTEM_GID_MAX;
90 }
91
92 /* The following macros add 1 when converting things, since UID 0 is a valid UID, while the pointer
93  * NULL is special */
94 #define PTR_TO_UID(p) ((uid_t) (((uintptr_t) (p))-1))
95 #define UID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
96
97 #define PTR_TO_GID(p) ((gid_t) (((uintptr_t) (p))-1))
98 #define GID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
99
100 static inline bool userns_supported(void) {
101         return access("/proc/self/uid_map", F_OK) >= 0;
102 }
103
104 bool valid_user_group_name(const char *u);
105 bool valid_user_group_name_or_id(const char *u);
106 bool valid_gecos(const char *d);
107 bool valid_home(const char *p);
108
109 int maybe_setgroups(size_t size, const gid_t *list);
110
111 bool synthesize_nobody(void);