| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <grp.h> |
| 5 |
#if ENABLE_GSHADOW |
| 6 |
# include <gshadow.h> |
| 7 |
#endif |
| 8 |
#include <pwd.h> |
| 9 |
#include <shadow.h> |
| 10 |
|
| 11 |
#include "basic-forward.h" |
| 12 |
#include "errno-util.h" |
| 13 |
|
| 14 |
/* Users managed by systemd-homed. See https://systemd.io/UIDS-GIDS for details |
| 15 |
* how this range fits into the rest of the world. */ |
| 16 |
#define HOME_UID_MIN ((uid_t) 60001) |
| 17 |
#define HOME_UID_MAX ((uid_t) 60513) |
| 18 |
|
| 19 |
/* Users mapped from host into a container */ |
| 20 |
#define MAP_UID_MIN ((uid_t) 60514) |
| 21 |
#define MAP_UID_MAX ((uid_t) 60577) |
| 22 |
|
| 23 |
/* A helper to print an error message when user or group resolution fails. */ |
| 24 |
const char* strerror_user(int errnum, char *buf, size_t buflen); |
| 25 |
#define STRERROR_USER(errnum) strerror_user(errnum, (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN) |
| 26 |
const char* strerror_group(int errnum, char *buf, size_t buflen); |
| 27 |
#define STRERROR_GROUP(errnum) strerror_group(errnum, (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN) |
| 28 |
|
| 29 |
static inline bool ERRNO_IS_NEG_BAD_ACCOUNT(intmax_t r) { |
| 30 |
return IN_SET(r, |
| 31 |
-ESRCH, |
| 32 |
-ENOEXEC); |
| 33 |
} |
| 34 |
_DEFINE_ABS_WRAPPER(BAD_ACCOUNT); |
| 35 |
|
| 36 |
bool uid_is_valid(uid_t uid) _const_; |
| 37 |
|
| 38 |
static inline bool gid_is_valid(gid_t gid) { |
| 39 |
return uid_is_valid((uid_t) gid); |
| 40 |
} |
| 41 |
|
| 42 |
int parse_uid(const char *s, uid_t* ret_uid); |
| 43 |
int parse_uid_range(const char *s, uid_t *ret_lower, uid_t *ret_upper); |
| 44 |
|
| 45 |
static inline int parse_gid(const char *s, gid_t *ret_gid) { |
| 46 |
return parse_uid(s, (uid_t*) ret_gid); |
| 47 |
} |
| 48 |
|
| 49 |
char* getlogname_malloc(void); |
| 50 |
char* getusername_malloc(void); |
| 51 |
|
| 52 |
const char* default_root_shell_at(int rfd); |
| 53 |
const char* default_root_shell(const char *root); |
| 54 |
|
| 55 |
bool is_nologin_shell(const char *shell) _pure_; |
| 56 |
bool shell_is_placeholder(const char *shell) _pure_; |
| 57 |
|
| 58 |
typedef enum UserCredsFlags { |
| 59 |
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 */ |
| 60 |
USER_CREDS_ALLOW_MISSING = 1 << 1, /* if a numeric UID string is resolved, be OK if there's no record for it */ |
| 61 |
USER_CREDS_CLEAN = 1 << 2, /* try to clean up shell and home fields with invalid data */ |
| 62 |
USER_CREDS_SUPPRESS_PLACEHOLDER = 1 << 3, /* suppress home and/or shell fields if value is placeholder (root/empty/nologin) */ |
| 63 |
} UserCredsFlags; |
| 64 |
|
| 65 |
int get_user_creds( |
| 66 |
const char *username, |
| 67 |
UserCredsFlags flags, |
| 68 |
char **ret_username, |
| 69 |
uid_t *ret_uid, gid_t *ret_gid, |
| 70 |
char **ret_home, |
| 71 |
char **ret_shell); |
| 72 |
int get_group_creds(const char *groupname, UserCredsFlags flags, char **ret_name, gid_t *ret_gid); |
| 73 |
|
| 74 |
char* uid_to_name(uid_t uid); |
| 75 |
char* gid_to_name(gid_t gid); |
| 76 |
|
| 77 |
int in_gid(gid_t gid); |
| 78 |
int in_group(const char *name); |
| 79 |
|
| 80 |
int merge_gid_lists(const gid_t *list1, size_t size1, const gid_t *list2, size_t size2, gid_t **ret); |
| 81 |
int getgroups_alloc(gid_t **ret); |
| 82 |
|
| 83 |
int get_home_dir(char **ret); |
| 84 |
int get_shell(char **ret); |
| 85 |
|
| 86 |
int fully_set_uid_gid(uid_t uid, gid_t gid, const gid_t supplementary_gids[], size_t n_supplementary_gids); |
| 87 |
static inline int reset_uid_gid(void) { |
| 88 |
return fully_set_uid_gid(0, 0, NULL, 0); |
| 89 |
} |
| 90 |
|
| 91 |
int take_etc_passwd_lock(const char *root); |
| 92 |
|
| 93 |
#define UID_INVALID ((uid_t) -1) |
| 94 |
#define GID_INVALID ((gid_t) -1) |
| 95 |
|
| 96 |
#define UID_NOBODY ((uid_t) 65534U) |
| 97 |
#define GID_NOBODY ((gid_t) 65534U) |
| 98 |
|
| 99 |
/* Conventional size of a user-namespace UID/GID delegation block (64K). |
| 100 |
* Untyped so it can be used in both UID and GID contexts without casts. */ |
| 101 |
#define USERNS_RANGE_SIZE 0x10000U |
| 102 |
|
| 103 |
/* If REMOUNT_IDMAPPING_HOST_ROOT is set for remount_idmap() we'll include a mapping here that maps the host |
| 104 |
* root user accessing the idmapped mount to the this user ID on the backing fs. This is the last valid UID in |
| 105 |
* the *signed* 32-bit range. You might wonder why precisely use this specific UID for this purpose? Well, we |
| 106 |
* definitely cannot use the first 0…65536 UIDs for that, since in most cases that's precisely the file range |
| 107 |
* we intend to map to some high UID range, and since UID mappings have to be bijective we thus cannot use |
| 108 |
* them at all. Furthermore the UID range beyond INT32_MAX (i.e. the range above the signed 32-bit range) is |
| 109 |
* icky, since many APIs cannot use it (example: setfsuid() returns the old UID as signed integer). Following |
| 110 |
* our usual logic of assigning a 16-bit UID range to each container, so that the upper 16-bit of a 32-bit UID |
| 111 |
* value indicate kind of a "container ID" and the lower 16-bit map directly to the intended user you can read |
| 112 |
* this specific UID as the "nobody" user of the container with ID 0x7FFF, which is kinda nice. */ |
| 113 |
#define UID_MAPPED_ROOT ((uid_t) (INT32_MAX-1)) |
| 114 |
#define GID_MAPPED_ROOT ((gid_t) (INT32_MAX-1)) |
| 115 |
|
| 116 |
#define ETC_PASSWD_LOCK_FILENAME ".pwd.lock" |
| 117 |
#define ETC_PASSWD_LOCK_PATH "/etc/" ETC_PASSWD_LOCK_FILENAME |
| 118 |
|
| 119 |
/* The following macros add 1 when converting things, since UID 0 is a valid UID, while the pointer |
| 120 |
* NULL is special */ |
| 121 |
#define PTR_TO_UID(p) ((uid_t) (((uintptr_t) (p))-1)) |
| 122 |
#define UID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1)) |
| 123 |
|
| 124 |
#define PTR_TO_GID(p) ((gid_t) (((uintptr_t) (p))-1)) |
| 125 |
#define GID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1)) |
| 126 |
|
| 127 |
typedef enum ValidUserFlags { |
| 128 |
VALID_USER_RELAX = 1 << 0, |
| 129 |
VALID_USER_WARN = 1 << 1, |
| 130 |
VALID_USER_ALLOW_NUMERIC = 1 << 2, |
| 131 |
} ValidUserFlags; |
| 132 |
|
| 133 |
bool valid_user_group_name(const char *u, ValidUserFlags flags); |
| 134 |
bool valid_gecos(const char *d); |
| 135 |
char* mangle_gecos(const char *d); |
| 136 |
bool valid_home(const char *p); |
| 137 |
bool valid_shell(const char *p); |
| 138 |
|
| 139 |
int maybe_setgroups(size_t size, const gid_t *list); |
| 140 |
|
| 141 |
bool synthesize_nobody(void); |
| 142 |
|
| 143 |
int fgetpwent_sane(FILE *stream, struct passwd **pw); |
| 144 |
int fgetspent_sane(FILE *stream, struct spwd **sp); |
| 145 |
int fgetgrent_sane(FILE *stream, struct group **gr); |
| 146 |
int putpwent_sane(const struct passwd *pw, FILE *stream); |
| 147 |
int putspent_sane(const struct spwd *sp, FILE *stream); |
| 148 |
int putgrent_sane(const struct group *gr, FILE *stream); |
| 149 |
#if ENABLE_GSHADOW |
| 150 |
int fgetsgent_sane(FILE *stream, struct sgrp **sg); |
| 151 |
int putsgent_sane(const struct sgrp *sg, FILE *stream); |
| 152 |
#endif |
| 153 |
|
| 154 |
int is_this_me(const char *username); |
| 155 |
|
| 156 |
const char* get_home_root(void); |
| 157 |
|
| 158 |
static inline bool hashed_password_is_locked_or_invalid(const char *password) { |
| 159 |
return password && password[0] != '$'; |
| 160 |
} |
| 161 |
|
| 162 |
/* A locked *and* invalid password for "struct spwd"'s .sp_pwdp and "struct passwd"'s .pw_passwd field */ |
| 163 |
#define PASSWORD_LOCKED_AND_INVALID "!*" |
| 164 |
|
| 165 |
/* A password indicating "look in shadow file, please!" for "struct passwd"'s .pw_passwd */ |
| 166 |
#define PASSWORD_SEE_SHADOW "x" |
| 167 |
|
| 168 |
/* A password indicating "hey, no password required for login" */ |
| 169 |
#define PASSWORD_NONE "" |
| 170 |
|
| 171 |
/* Used by sysusers to indicate that the password should be filled in by firstboot. |
| 172 |
* Also see https://github.com/systemd/systemd/pull/24680#pullrequestreview-1439464325. |
| 173 |
*/ |
| 174 |
#define PASSWORD_UNPROVISIONED "!unprovisioned" |
| 175 |
|
| 176 |
int getpwuid_malloc(uid_t uid, struct passwd **ret); |
| 177 |
int getpwnam_malloc(const char *name, struct passwd **ret); |
| 178 |
|
| 179 |
int getgrnam_malloc(const char *name, struct group **ret); |
| 180 |
int getgrgid_malloc(gid_t gid, struct group **ret); |