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