]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
b1d4f8e1 LP |
2 | #pragma once |
3 | ||
100d5f6e | 4 | #include <grp.h> |
66a5b5ce | 5 | #if ENABLE_GSHADOW |
98dcb8f4 | 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 | 14 | |
76ef5d04 LP |
15 | /* Users managed by systemd-homed. See https://systemd.io/UIDS-GIDS for details how this range fits into the rest of the world */ |
16 | #define HOME_UID_MIN 60001 | |
17 | #define HOME_UID_MAX 60513 | |
18 | ||
19 | /* Users mapped from host into a container */ | |
20 | #define MAP_UID_MIN 60514 | |
21 | #define MAP_UID_MAX 60577 | |
22 | ||
b1d4f8e1 LP |
23 | bool uid_is_valid(uid_t uid); |
24 | ||
25 | static inline bool gid_is_valid(gid_t gid) { | |
26 | return uid_is_valid((uid_t) gid); | |
27 | } | |
28 | ||
29 | int parse_uid(const char *s, uid_t* ret_uid); | |
03de302a | 30 | int parse_uid_range(const char *s, uid_t *ret_lower, uid_t *ret_upper); |
b1d4f8e1 LP |
31 | |
32 | static inline int parse_gid(const char *s, gid_t *ret_gid) { | |
33 | return parse_uid(s, (uid_t*) ret_gid); | |
34 | } | |
35 | ||
b1d4f8e1 LP |
36 | char* getlogname_malloc(void); |
37 | char* getusername_malloc(void); | |
38 | ||
fafff8f1 | 39 | typedef enum UserCredsFlags { |
43ad3ad7 ZJS |
40 | 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 */ |
41 | USER_CREDS_ALLOW_MISSING = 1 << 1, /* if a numeric UID string is resolved, be OK if there's no record for it */ | |
42 | USER_CREDS_CLEAN = 1 << 2, /* try to clean up shell and home fields with invalid data */ | |
fafff8f1 LP |
43 | } UserCredsFlags; |
44 | ||
45 | int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell, UserCredsFlags flags); | |
46 | int get_group_creds(const char **groupname, gid_t *gid, UserCredsFlags flags); | |
b1d4f8e1 LP |
47 | |
48 | char* uid_to_name(uid_t uid); | |
49 | char* gid_to_name(gid_t gid); | |
50 | ||
51 | int in_gid(gid_t gid); | |
52 | int in_group(const char *name); | |
53 | ||
0c5d6679 DG |
54 | int merge_gid_lists(const gid_t *list1, size_t size1, const gid_t *list2, size_t size2, gid_t **result); |
55 | int getgroups_alloc(gid_t** gids); | |
56 | ||
b1d4f8e1 LP |
57 | int get_home_dir(char **ret); |
58 | int get_shell(char **_ret); | |
59 | ||
60 | int reset_uid_gid(void); | |
e929bee0 LP |
61 | |
62 | int take_etc_passwd_lock(const char *root); | |
ee104e11 LP |
63 | |
64 | #define UID_INVALID ((uid_t) -1) | |
65 | #define GID_INVALID ((gid_t) -1) | |
66 | ||
3a664727 LP |
67 | #define UID_NOBODY ((uid_t) 65534U) |
68 | #define GID_NOBODY ((gid_t) 65534U) | |
69 | ||
d1e4b8fd ZJS |
70 | #define ETC_PASSWD_LOCK_PATH "/etc/.pwd.lock" |
71 | ||
61755fda ZJS |
72 | /* The following macros add 1 when converting things, since UID 0 is a valid UID, while the pointer |
73 | * NULL is special */ | |
ee104e11 LP |
74 | #define PTR_TO_UID(p) ((uid_t) (((uintptr_t) (p))-1)) |
75 | #define UID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1)) | |
76 | ||
77 | #define PTR_TO_GID(p) ((gid_t) (((uintptr_t) (p))-1)) | |
78 | #define GID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1)) | |
ccabee0d LP |
79 | |
80 | static inline bool userns_supported(void) { | |
81 | return access("/proc/self/uid_map", F_OK) >= 0; | |
82 | } | |
e4631b48 | 83 | |
7a8867ab LP |
84 | typedef enum ValidUserFlags { |
85 | VALID_USER_RELAX = 1 << 0, | |
86 | VALID_USER_WARN = 1 << 1, | |
87 | VALID_USER_ALLOW_NUMERIC = 1 << 2, | |
88 | } ValidUserFlags; | |
89 | ||
90 | bool valid_user_group_name(const char *u, ValidUserFlags flags); | |
e4631b48 | 91 | bool valid_gecos(const char *d); |
b10fd796 | 92 | char *mangle_gecos(const char *d); |
e4631b48 | 93 | bool valid_home(const char *p); |
36d85478 | 94 | |
7b1aaf66 ZJS |
95 | static inline bool valid_shell(const char *p) { |
96 | /* We have the same requirements, so just piggy-back on the home check. | |
97 | * | |
98 | * Let's ignore /etc/shells because this is only applicable to real and | |
99 | * not system users. It is also incompatible with the idea of empty /etc. | |
100 | */ | |
101 | return valid_home(p); | |
102 | } | |
103 | ||
36d85478 | 104 | int maybe_setgroups(size_t size, const gid_t *list); |
24eccc34 LP |
105 | |
106 | bool synthesize_nobody(void); | |
100d5f6e FB |
107 | |
108 | int fgetpwent_sane(FILE *stream, struct passwd **pw); | |
109 | int fgetspent_sane(FILE *stream, struct spwd **sp); | |
110 | int fgetgrent_sane(FILE *stream, struct group **gr); | |
111 | int putpwent_sane(const struct passwd *pw, FILE *stream); | |
112 | int putspent_sane(const struct spwd *sp, FILE *stream); | |
113 | int putgrent_sane(const struct group *gr, FILE *stream); | |
4f07ffa8 | 114 | #if ENABLE_GSHADOW |
100d5f6e FB |
115 | int fgetsgent_sane(FILE *stream, struct sgrp **sg); |
116 | int putsgent_sane(const struct sgrp *sg, FILE *stream); | |
117 | #endif | |
f2c5edbe | 118 | |
6093b2bb | 119 | bool is_nologin_shell(const char *shell); |
7bdbafc2 LP |
120 | |
121 | int is_this_me(const char *username); | |
53c25ac9 | 122 | |
2700fecd LP |
123 | const char *get_home_root(void); |
124 | ||
cd933f14 P |
125 | static inline bool hashed_password_is_locked_or_invalid(const char *password) { |
126 | return password && password[0] != '$'; | |
127 | } | |
128 | ||
53c25ac9 LP |
129 | /* A locked *and* invalid password for "struct spwd"'s .sp_pwdp and "struct passwd"'s .pw_passwd field */ |
130 | #define PASSWORD_LOCKED_AND_INVALID "!*" | |
131 | ||
132 | /* A password indicating "look in shadow file, please!" for "struct passwd"'s .pw_passwd */ | |
133 | #define PASSWORD_SEE_SHADOW "x" | |
134 | ||
135 | /* A password indicating "hey, no password required for login" */ | |
136 | #define PASSWORD_NONE "" |