chiark / gitweb /
Prep v239: Add missing updates that evaded migration.
[elogind.git] / src / basic / user-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 //#include <grp.h>
5 //#include <gshadow.h>
6 //#include <pwd.h>
7 //#include <shadow.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12
13 bool uid_is_valid(uid_t uid);
14
15 static inline bool gid_is_valid(gid_t gid) {
16         return uid_is_valid((uid_t) gid);
17 }
18
19 int parse_uid(const char *s, uid_t* ret_uid);
20
21 static inline int parse_gid(const char *s, gid_t *ret_gid) {
22         return parse_uid(s, (uid_t*) ret_gid);
23 }
24
25 char* getlogname_malloc(void);
26 #if 0 /// UNNEEDED by elogind
27 char* getusername_malloc(void);
28 #endif // 0
29
30 int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
31 #if 0 /// UNNEEDED by elogind
32 int get_user_creds_clean(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
33 int get_group_creds(const char **groupname, gid_t *gid);
34 #endif // 0
35
36 char* uid_to_name(uid_t uid);
37 char* gid_to_name(gid_t gid);
38
39 #if 0 /// UNNEEDED by elogind
40 int in_gid(gid_t gid);
41 int in_group(const char *name);
42
43 int get_home_dir(char **ret);
44 int get_shell(char **_ret);
45 #endif // 0
46
47 int reset_uid_gid(void);
48
49 #if 0 /// UNNEEDED by elogind
50 int take_etc_passwd_lock(const char *root);
51 #endif // 0
52
53 #define UID_INVALID ((uid_t) -1)
54 #define GID_INVALID ((gid_t) -1)
55
56 #define UID_NOBODY ((uid_t) 65534U)
57 #define GID_NOBODY ((gid_t) 65534U)
58
59 #define ETC_PASSWD_LOCK_PATH "/etc/.pwd.lock"
60
61 #if 0 /// UNNEEDED by elogind
62 static inline bool uid_is_dynamic(uid_t uid) {
63         return DYNAMIC_UID_MIN <= uid && uid <= DYNAMIC_UID_MAX;
64 }
65
66 static inline bool gid_is_dynamic(gid_t gid) {
67         return uid_is_dynamic((uid_t) gid);
68 }
69 #endif // 0
70
71 static inline bool uid_is_system(uid_t uid) {
72         return uid <= SYSTEM_UID_MAX;
73 }
74
75 static inline bool gid_is_system(gid_t gid) {
76         return gid <= SYSTEM_GID_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 static inline bool valid_shell(const char *p) {
97         /* We have the same requirements, so just piggy-back on the home check.
98          *
99          * Let's ignore /etc/shells because this is only applicable to real and
100          * not system users. It is also incompatible with the idea of empty /etc.
101          */
102         return valid_home(p);
103 }
104
105 int maybe_setgroups(size_t size, const gid_t *list);
106
107 bool synthesize_nobody(void);
108
109 #if 0 /// UNNEEDED by elogind
110 int fgetpwent_sane(FILE *stream, struct passwd **pw);
111 int fgetspent_sane(FILE *stream, struct spwd **sp);
112 int fgetgrent_sane(FILE *stream, struct group **gr);
113 int putpwent_sane(const struct passwd *pw, FILE *stream);
114 int putspent_sane(const struct spwd *sp, FILE *stream);
115 int putgrent_sane(const struct group *gr, FILE *stream);
116 #ifdef ENABLE_GSHADOW
117 int fgetsgent_sane(FILE *stream, struct sgrp **sg);
118 int putsgent_sane(const struct sgrp *sg, FILE *stream);
119 #endif
120 #endif // 0