]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/user-util.h
man/systemd-sysext: list ephemeral/ephemeral-import in the list of options
[thirdparty/systemd.git] / src / basic / user-util.h
CommitLineData
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
0c15577a 11#include "forward.h"
579ce77e 12
76ef5d04 13/* Users managed by systemd-homed. See https://systemd.io/UIDS-GIDS for details how this range fits into the rest of the world */
63b98386
JJ
14#define HOME_UID_MIN ((uid_t) 60001)
15#define HOME_UID_MAX ((uid_t) 60513)
76ef5d04
LP
16
17/* Users mapped from host into a container */
63b98386
JJ
18#define MAP_UID_MIN ((uid_t) 60514)
19#define MAP_UID_MAX ((uid_t) 60577)
76ef5d04 20
180341e7 21bool uid_is_valid(uid_t uid) _const_;
b1d4f8e1
LP
22
23static inline bool gid_is_valid(gid_t gid) {
24 return uid_is_valid((uid_t) gid);
25}
26
27int parse_uid(const char *s, uid_t* ret_uid);
03de302a 28int parse_uid_range(const char *s, uid_t *ret_lower, uid_t *ret_upper);
b1d4f8e1
LP
29
30static inline int parse_gid(const char *s, gid_t *ret_gid) {
31 return parse_uid(s, (uid_t*) ret_gid);
32}
33
b1d4f8e1
LP
34char* getlogname_malloc(void);
35char* getusername_malloc(void);
36
579ce77e
MY
37const char* default_root_shell_at(int rfd);
38const char* default_root_shell(const char *root);
39
180341e7 40bool is_nologin_shell(const char *shell) _pure_;
0c15577a 41bool shell_is_placeholder(const char *shell) _pure_;
579ce77e 42
fafff8f1 43typedef enum UserCredsFlags {
eea9d3eb
MY
44 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 */
45 USER_CREDS_ALLOW_MISSING = 1 << 1, /* if a numeric UID string is resolved, be OK if there's no record for it */
46 USER_CREDS_CLEAN = 1 << 2, /* try to clean up shell and home fields with invalid data */
47 USER_CREDS_SUPPRESS_PLACEHOLDER = 1 << 3, /* suppress home and/or shell fields if value is placeholder (root/empty/nologin) */
fafff8f1
LP
48} UserCredsFlags;
49
83e9b584
LP
50int get_user_creds(const char **username, uid_t *ret_uid, gid_t *ret_gid, const char **ret_home, const char **ret_shell, UserCredsFlags flags);
51int get_group_creds(const char **groupname, gid_t *ret_gid, UserCredsFlags flags);
b1d4f8e1
LP
52
53char* uid_to_name(uid_t uid);
54char* gid_to_name(gid_t gid);
55
56int in_gid(gid_t gid);
57int in_group(const char *name);
58
0c5d6679 59int merge_gid_lists(const gid_t *list1, size_t size1, const gid_t *list2, size_t size2, gid_t **result);
f0e8db76 60int getgroups_alloc(gid_t **ret);
0c5d6679 61
b1d4f8e1 62int get_home_dir(char **ret);
8795d9ba 63int get_shell(char **ret);
b1d4f8e1 64
6498a0c2
LP
65int fully_set_uid_gid(uid_t uid, gid_t gid, const gid_t supplementary_gids[], size_t n_supplementary_gids);
66static inline int reset_uid_gid(void) {
67 return fully_set_uid_gid(0, 0, NULL, 0);
68}
e929bee0
LP
69
70int take_etc_passwd_lock(const char *root);
ee104e11
LP
71
72#define UID_INVALID ((uid_t) -1)
73#define GID_INVALID ((gid_t) -1)
74
3a664727
LP
75#define UID_NOBODY ((uid_t) 65534U)
76#define GID_NOBODY ((gid_t) 65534U)
77
1aa18710
QD
78/* If REMOUNT_IDMAPPING_HOST_ROOT is set for remount_idmap() we'll include a mapping here that maps the host
79 * root user accessing the idmapped mount to the this user ID on the backing fs. This is the last valid UID in
da890466 80 * the *signed* 32-bit range. You might wonder why precisely use this specific UID for this purpose? Well, we
50ae2966
LP
81 * definitely cannot use the first 0…65536 UIDs for that, since in most cases that's precisely the file range
82 * we intend to map to some high UID range, and since UID mappings have to be bijective we thus cannot use
da890466 83 * them at all. Furthermore the UID range beyond INT32_MAX (i.e. the range above the signed 32-bit range) is
50ae2966 84 * icky, since many APIs cannot use it (example: setfsuid() returns the old UID as signed integer). Following
da890466
ZJS
85 * our usual logic of assigning a 16-bit UID range to each container, so that the upper 16-bit of a 32-bit UID
86 * value indicate kind of a "container ID" and the lower 16-bit map directly to the intended user you can read
50ae2966
LP
87 * this specific UID as the "nobody" user of the container with ID 0x7FFF, which is kinda nice. */
88#define UID_MAPPED_ROOT ((uid_t) (INT32_MAX-1))
89#define GID_MAPPED_ROOT ((gid_t) (INT32_MAX-1))
90
fe585662
DDM
91#define ETC_PASSWD_LOCK_FILENAME ".pwd.lock"
92#define ETC_PASSWD_LOCK_PATH "/etc/" ETC_PASSWD_LOCK_FILENAME
d1e4b8fd 93
61755fda
ZJS
94/* The following macros add 1 when converting things, since UID 0 is a valid UID, while the pointer
95 * NULL is special */
ee104e11
LP
96#define PTR_TO_UID(p) ((uid_t) (((uintptr_t) (p))-1))
97#define UID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
98
99#define PTR_TO_GID(p) ((gid_t) (((uintptr_t) (p))-1))
100#define GID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
ccabee0d 101
7a8867ab
LP
102typedef enum ValidUserFlags {
103 VALID_USER_RELAX = 1 << 0,
104 VALID_USER_WARN = 1 << 1,
105 VALID_USER_ALLOW_NUMERIC = 1 << 2,
106} ValidUserFlags;
107
108bool valid_user_group_name(const char *u, ValidUserFlags flags);
e4631b48 109bool valid_gecos(const char *d);
78435d62 110char* mangle_gecos(const char *d);
e4631b48 111bool valid_home(const char *p);
4167e9e2 112bool valid_shell(const char *p);
7b1aaf66 113
36d85478 114int maybe_setgroups(size_t size, const gid_t *list);
24eccc34
LP
115
116bool synthesize_nobody(void);
100d5f6e
FB
117
118int fgetpwent_sane(FILE *stream, struct passwd **pw);
119int fgetspent_sane(FILE *stream, struct spwd **sp);
120int fgetgrent_sane(FILE *stream, struct group **gr);
121int putpwent_sane(const struct passwd *pw, FILE *stream);
122int putspent_sane(const struct spwd *sp, FILE *stream);
123int putgrent_sane(const struct group *gr, FILE *stream);
4f07ffa8 124#if ENABLE_GSHADOW
100d5f6e
FB
125int fgetsgent_sane(FILE *stream, struct sgrp **sg);
126int putsgent_sane(const struct sgrp *sg, FILE *stream);
127#endif
f2c5edbe 128
7bdbafc2 129int is_this_me(const char *username);
53c25ac9 130
78435d62 131const char* get_home_root(void);
2700fecd 132
cd933f14
P
133static inline bool hashed_password_is_locked_or_invalid(const char *password) {
134 return password && password[0] != '$';
135}
136
53c25ac9
LP
137/* A locked *and* invalid password for "struct spwd"'s .sp_pwdp and "struct passwd"'s .pw_passwd field */
138#define PASSWORD_LOCKED_AND_INVALID "!*"
139
140/* A password indicating "look in shadow file, please!" for "struct passwd"'s .pw_passwd */
141#define PASSWORD_SEE_SHADOW "x"
142
143/* A password indicating "hey, no password required for login" */
144#define PASSWORD_NONE ""
a777a592
ZJS
145
146/* Used by sysusers to indicate that the password should be filled in by firstboot.
147 * Also see https://github.com/systemd/systemd/pull/24680#pullrequestreview-1439464325.
148 */
149#define PASSWORD_UNPROVISIONED "!unprovisioned"
75673cd8
LP
150
151int getpwuid_malloc(uid_t uid, struct passwd **ret);
152int getpwnam_malloc(const char *name, struct passwd **ret);
153
154int getgrnam_malloc(const char *name, struct group **ret);
155int getgrgid_malloc(gid_t gid, struct group **ret);