chiark / gitweb /
e0de7a9a854f2ca25ec858adc26766f92d0643aa
[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
10 //#include <grp.h>
11 //#include <gshadow.h>
12 //#include <pwd.h>
13 //#include <shadow.h>
14 #include <stdbool.h>
15 #include <stdint.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18
19 bool uid_is_valid(uid_t uid);
20
21 static inline bool gid_is_valid(gid_t gid) {
22         return uid_is_valid((uid_t) gid);
23 }
24
25 int parse_uid(const char *s, uid_t* ret_uid);
26
27 static inline int parse_gid(const char *s, gid_t *ret_gid) {
28         return parse_uid(s, (uid_t*) ret_gid);
29 }
30
31 char* getlogname_malloc(void);
32 #if 0 /// UNNEEDED by elogind
33 char* getusername_malloc(void);
34 #endif // 0
35
36 int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
37 #if 0 /// UNNEEDED by elogind
38 int get_user_creds_clean(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
39 int get_group_creds(const char **groupname, gid_t *gid);
40 #endif // 0
41
42 char* uid_to_name(uid_t uid);
43 char* gid_to_name(gid_t gid);
44
45 #if 0 /// UNNEEDED by elogind
46 int in_gid(gid_t gid);
47 int in_group(const char *name);
48
49 int get_home_dir(char **ret);
50 int get_shell(char **_ret);
51 #endif // 0
52
53 int reset_uid_gid(void);
54
55 #if 0 /// UNNEEDED by elogind
56 int take_etc_passwd_lock(const char *root);
57 #endif // 0
58
59 #define UID_INVALID ((uid_t) -1)
60 #define GID_INVALID ((gid_t) -1)
61
62 #define UID_NOBODY ((uid_t) 65534U)
63 #define GID_NOBODY ((gid_t) 65534U)
64
65 #define ETC_PASSWD_LOCK_PATH "/etc/.pwd.lock"
66
67 #if 0 /// UNNEEDED by elogind
68 static inline bool uid_is_dynamic(uid_t uid) {
69         return DYNAMIC_UID_MIN <= uid && uid <= DYNAMIC_UID_MAX;
70 }
71
72 static inline bool gid_is_dynamic(gid_t gid) {
73         return uid_is_dynamic((uid_t) gid);
74 }
75 #endif // 0
76
77 static inline bool uid_is_system(uid_t uid) {
78         return uid <= SYSTEM_UID_MAX;
79 }
80
81 static inline bool gid_is_system(gid_t gid) {
82         return gid <= SYSTEM_GID_MAX;
83 }
84
85 /* The following macros add 1 when converting things, since UID 0 is a valid UID, while the pointer
86  * NULL is special */
87 #define PTR_TO_UID(p) ((uid_t) (((uintptr_t) (p))-1))
88 #define UID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
89
90 #define PTR_TO_GID(p) ((gid_t) (((uintptr_t) (p))-1))
91 #define GID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
92
93 static inline bool userns_supported(void) {
94         return access("/proc/self/uid_map", F_OK) >= 0;
95 }
96
97 bool valid_user_group_name(const char *u);
98 bool valid_user_group_name_or_id(const char *u);
99 bool valid_gecos(const char *d);
100 bool valid_home(const char *p);
101
102 static inline bool valid_shell(const char *p) {
103         /* We have the same requirements, so just piggy-back on the home check.
104          *
105          * Let's ignore /etc/shells because this is only applicable to real and
106          * not system users. It is also incompatible with the idea of empty /etc.
107          */
108         return valid_home(p);
109 }
110
111 int maybe_setgroups(size_t size, const gid_t *list);
112
113 bool synthesize_nobody(void);
114
115 int fgetpwent_sane(FILE *stream, struct passwd **pw);
116 int fgetspent_sane(FILE *stream, struct spwd **sp);
117 int fgetgrent_sane(FILE *stream, struct group **gr);
118 int putpwent_sane(const struct passwd *pw, FILE *stream);
119 int putspent_sane(const struct spwd *sp, FILE *stream);
120 int putgrent_sane(const struct group *gr, FILE *stream);
121 #ifdef ENABLE_GSHADOW
122 int fgetsgent_sane(FILE *stream, struct sgrp **sg);
123 int putsgent_sane(const struct sgrp *sg, FILE *stream);
124 #endif