]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/user-util.h
Merge pull request #12390 from poettering/string-file-mkdir
[thirdparty/systemd.git] / src / basic / user-util.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
b1d4f8e1
LP
2#pragma once
3
100d5f6e 4#include <grp.h>
66a5b5ce 5#if ENABLE_GSHADOW
100d5f6e 6#include <gshadow.h>
66a5b5ce 7#endif
100d5f6e
FB
8#include <pwd.h>
9#include <shadow.h>
b1d4f8e1 10#include <stdbool.h>
61755fda 11#include <stdint.h>
71d35b6b 12#include <sys/types.h>
ccabee0d 13#include <unistd.h>
b1d4f8e1
LP
14
15bool uid_is_valid(uid_t uid);
16
17static inline bool gid_is_valid(gid_t gid) {
18 return uid_is_valid((uid_t) gid);
19}
20
21int parse_uid(const char *s, uid_t* ret_uid);
22
23static inline int parse_gid(const char *s, gid_t *ret_gid) {
24 return parse_uid(s, (uid_t*) ret_gid);
25}
26
b1d4f8e1
LP
27char* getlogname_malloc(void);
28char* getusername_malloc(void);
29
fafff8f1 30typedef enum UserCredsFlags {
43ad3ad7
ZJS
31 USER_CREDS_PREFER_NSS = 1 << 0, /* if set, only synthesize user records if database lacks them. Normally we bypass the userdb entirely for the records we can synthesize */
32 USER_CREDS_ALLOW_MISSING = 1 << 1, /* if a numeric UID string is resolved, be OK if there's no record for it */
33 USER_CREDS_CLEAN = 1 << 2, /* try to clean up shell and home fields with invalid data */
fafff8f1
LP
34} UserCredsFlags;
35
36int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell, UserCredsFlags flags);
37int get_group_creds(const char **groupname, gid_t *gid, UserCredsFlags flags);
b1d4f8e1
LP
38
39char* uid_to_name(uid_t uid);
40char* gid_to_name(gid_t gid);
41
42int in_gid(gid_t gid);
43int in_group(const char *name);
44
45int get_home_dir(char **ret);
46int get_shell(char **_ret);
47
48int reset_uid_gid(void);
e929bee0
LP
49
50int take_etc_passwd_lock(const char *root);
ee104e11
LP
51
52#define UID_INVALID ((uid_t) -1)
53#define GID_INVALID ((gid_t) -1)
54
3a664727
LP
55#define UID_NOBODY ((uid_t) 65534U)
56#define GID_NOBODY ((gid_t) 65534U)
57
d1e4b8fd
ZJS
58#define ETC_PASSWD_LOCK_PATH "/etc/.pwd.lock"
59
61755fda
ZJS
60static inline bool uid_is_dynamic(uid_t uid) {
61 return DYNAMIC_UID_MIN <= uid && uid <= DYNAMIC_UID_MAX;
62}
63
83438277
LP
64static inline bool gid_is_dynamic(gid_t gid) {
65 return uid_is_dynamic((uid_t) gid);
66}
67
ece877d4
LP
68static inline bool uid_is_system(uid_t uid) {
69 return uid <= SYSTEM_UID_MAX;
70}
71
72static inline bool gid_is_system(gid_t gid) {
73 return gid <= SYSTEM_GID_MAX;
74}
75
61755fda
ZJS
76/* The following macros add 1 when converting things, since UID 0 is a valid UID, while the pointer
77 * NULL is special */
ee104e11
LP
78#define PTR_TO_UID(p) ((uid_t) (((uintptr_t) (p))-1))
79#define UID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
80
81#define PTR_TO_GID(p) ((gid_t) (((uintptr_t) (p))-1))
82#define GID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
ccabee0d
LP
83
84static inline bool userns_supported(void) {
85 return access("/proc/self/uid_map", F_OK) >= 0;
86}
e4631b48
LP
87
88bool valid_user_group_name(const char *u);
89bool valid_user_group_name_or_id(const char *u);
90bool valid_gecos(const char *d);
91bool valid_home(const char *p);
36d85478 92
7b1aaf66
ZJS
93static inline bool valid_shell(const char *p) {
94 /* We have the same requirements, so just piggy-back on the home check.
95 *
96 * Let's ignore /etc/shells because this is only applicable to real and
97 * not system users. It is also incompatible with the idea of empty /etc.
98 */
99 return valid_home(p);
100}
101
36d85478 102int maybe_setgroups(size_t size, const gid_t *list);
24eccc34
LP
103
104bool synthesize_nobody(void);
100d5f6e
FB
105
106int fgetpwent_sane(FILE *stream, struct passwd **pw);
107int fgetspent_sane(FILE *stream, struct spwd **sp);
108int fgetgrent_sane(FILE *stream, struct group **gr);
109int putpwent_sane(const struct passwd *pw, FILE *stream);
110int putspent_sane(const struct spwd *sp, FILE *stream);
111int putgrent_sane(const struct group *gr, FILE *stream);
4f07ffa8 112#if ENABLE_GSHADOW
100d5f6e
FB
113int fgetsgent_sane(FILE *stream, struct sgrp **sg);
114int putsgent_sane(const struct sgrp *sg, FILE *stream);
115#endif
f2c5edbe
LP
116
117int make_salt(char **ret);