]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
1b992147 | 2 | |
3f6fd1ba | 3 | #include <getopt.h> |
932ad62b | 4 | #include <utmp.h> |
1b992147 | 5 | |
b5efdb8a | 6 | #include "alloc-util.h" |
1b992147 LP |
7 | #include "conf-files.h" |
8 | #include "copy.h" | |
99e9f896 | 9 | #include "creds-util.h" |
a0f29c76 | 10 | #include "def.h" |
0c3ee127 | 11 | #include "dissect-image.h" |
a0f29c76 | 12 | #include "fd-util.h" |
ee228be1 | 13 | #include "fileio.h" |
f97b34a6 | 14 | #include "format-util.h" |
dcd5c891 | 15 | #include "fs-util.h" |
3f6fd1ba | 16 | #include "hashmap.h" |
99e9f896 | 17 | #include "libcrypt-util.h" |
71da1673 | 18 | #include "main-func.h" |
99e9f896 | 19 | #include "memory-util.h" |
0c3ee127 | 20 | #include "mount-util.h" |
d481b830 | 21 | #include "nscd-flush.h" |
dcd5c891 | 22 | #include "pager.h" |
614b022c | 23 | #include "parse-argument.h" |
3f6fd1ba | 24 | #include "path-util.h" |
294bf0c3 | 25 | #include "pretty-print.h" |
3f6fd1ba | 26 | #include "selinux-util.h" |
0c3ee127 | 27 | #include "set.h" |
07630cea | 28 | #include "smack-util.h" |
3f6fd1ba | 29 | #include "specifier.h" |
07630cea | 30 | #include "string-util.h" |
3f6fd1ba | 31 | #include "strv.h" |
bf819d3a | 32 | #include "sync-util.h" |
e4de7287 | 33 | #include "tmpfile-util-label.h" |
3f6fd1ba | 34 | #include "uid-range.h" |
aa25270c | 35 | #include "user-record.h" |
a0f29c76 | 36 | #include "user-util.h" |
3f6fd1ba LP |
37 | #include "utf8.h" |
38 | #include "util.h" | |
1b992147 LP |
39 | |
40 | typedef enum ItemType { | |
41 | ADD_USER = 'u', | |
42 | ADD_GROUP = 'g', | |
a12b0cc3 | 43 | ADD_MEMBER = 'm', |
8530dc44 | 44 | ADD_RANGE = 'r', |
1b992147 | 45 | } ItemType; |
dcd5c891 | 46 | |
1b992147 LP |
47 | typedef struct Item { |
48 | ItemType type; | |
49 | ||
50 | char *name; | |
649916d3 | 51 | char *group_name; |
1b992147 LP |
52 | char *uid_path; |
53 | char *gid_path; | |
54 | char *description; | |
7629889c | 55 | char *home; |
7b1aaf66 | 56 | char *shell; |
1b992147 LP |
57 | |
58 | gid_t gid; | |
59 | uid_t uid; | |
60 | ||
61 | bool gid_set:1; | |
fb959f14 ZJS |
62 | |
63 | /* When set the group with the specified gid must exist | |
64 | * and the check if a uid clashes with the gid is skipped. | |
65 | */ | |
b9ee05c2 | 66 | bool id_set_strict:1; |
fb959f14 | 67 | |
1b992147 LP |
68 | bool uid_set:1; |
69 | ||
c1b6b04f KS |
70 | bool todo_user:1; |
71 | bool todo_group:1; | |
1b992147 LP |
72 | } Item; |
73 | ||
74 | static char *arg_root = NULL; | |
0c3ee127 | 75 | static char *arg_image = NULL; |
ec0327d6 | 76 | static bool arg_cat_config = false; |
d16a1c1b | 77 | static const char *arg_replace = NULL; |
1b600bd5 | 78 | static bool arg_inline = false; |
0221d68a | 79 | static PagerFlags arg_pager_flags = 0; |
1b992147 | 80 | |
5bc9c980 MV |
81 | static OrderedHashmap *users = NULL, *groups = NULL; |
82 | static OrderedHashmap *todo_uids = NULL, *todo_gids = NULL; | |
83 | static OrderedHashmap *members = NULL; | |
1b992147 | 84 | |
71da1673 YW |
85 | static Hashmap *database_by_uid = NULL, *database_by_username = NULL; |
86 | static Hashmap *database_by_gid = NULL, *database_by_groupname = NULL; | |
87 | static Set *database_users = NULL, *database_groups = NULL; | |
1b992147 | 88 | |
fed1e721 | 89 | static uid_t search_uid = UID_INVALID; |
8530dc44 LP |
90 | static UidRange *uid_range = NULL; |
91 | static unsigned n_uid_range = 0; | |
1b992147 | 92 | |
4b6f9b20 ZJS |
93 | static UGIDAllocationRange login_defs = {}; |
94 | static bool login_defs_need_warning = false; | |
95 | ||
71da1673 YW |
96 | STATIC_DESTRUCTOR_REGISTER(groups, ordered_hashmap_freep); |
97 | STATIC_DESTRUCTOR_REGISTER(users, ordered_hashmap_freep); | |
98 | STATIC_DESTRUCTOR_REGISTER(members, ordered_hashmap_freep); | |
99 | STATIC_DESTRUCTOR_REGISTER(todo_uids, ordered_hashmap_freep); | |
100 | STATIC_DESTRUCTOR_REGISTER(todo_gids, ordered_hashmap_freep); | |
101 | STATIC_DESTRUCTOR_REGISTER(database_by_uid, hashmap_freep); | |
102 | STATIC_DESTRUCTOR_REGISTER(database_by_username, hashmap_freep); | |
103 | STATIC_DESTRUCTOR_REGISTER(database_users, set_free_freep); | |
104 | STATIC_DESTRUCTOR_REGISTER(database_by_gid, hashmap_freep); | |
105 | STATIC_DESTRUCTOR_REGISTER(database_by_groupname, hashmap_freep); | |
106 | STATIC_DESTRUCTOR_REGISTER(database_groups, set_free_freep); | |
107 | STATIC_DESTRUCTOR_REGISTER(uid_range, freep); | |
108 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); | |
0c3ee127 | 109 | STATIC_DESTRUCTOR_REGISTER(arg_image, freep); |
71da1673 | 110 | |
08c7c321 ZJS |
111 | static int errno_is_not_exists(int code) { |
112 | /* See getpwnam(3) and getgrnam(3): those codes and others can be returned if the user or group are | |
113 | * not found. */ | |
114 | return IN_SET(code, 0, ENOENT, ESRCH, EBADF, EPERM); | |
115 | } | |
116 | ||
4b6f9b20 ZJS |
117 | static void maybe_emit_login_defs_warning(void) { |
118 | if (!login_defs_need_warning) | |
119 | return; | |
120 | ||
121 | if (login_defs.system_alloc_uid_min != SYSTEM_ALLOC_UID_MIN || | |
122 | login_defs.system_uid_max != SYSTEM_UID_MAX) | |
123 | log_warning("login.defs specifies UID allocation range "UID_FMT"–"UID_FMT | |
124 | " that is different than the built-in defaults ("UID_FMT"–"UID_FMT")", | |
125 | login_defs.system_alloc_uid_min, login_defs.system_uid_max, | |
126 | SYSTEM_ALLOC_UID_MIN, SYSTEM_UID_MAX); | |
127 | if (login_defs.system_alloc_gid_min != SYSTEM_ALLOC_GID_MIN || | |
128 | login_defs.system_gid_max != SYSTEM_GID_MAX) | |
129 | log_warning("login.defs specifies GID allocation range "GID_FMT"–"GID_FMT | |
130 | " that is different than the built-in defaults ("GID_FMT"–"GID_FMT")", | |
131 | login_defs.system_alloc_gid_min, login_defs.system_gid_max, | |
132 | SYSTEM_ALLOC_GID_MIN, SYSTEM_GID_MAX); | |
133 | ||
134 | login_defs_need_warning = false; | |
135 | } | |
136 | ||
1b992147 LP |
137 | static int load_user_database(void) { |
138 | _cleanup_fclose_ FILE *f = NULL; | |
139 | const char *passwd_path; | |
140 | struct passwd *pw; | |
141 | int r; | |
142 | ||
1d13f648 | 143 | passwd_path = prefix_roota(arg_root, "/etc/passwd"); |
1b992147 LP |
144 | f = fopen(passwd_path, "re"); |
145 | if (!f) | |
146 | return errno == ENOENT ? 0 : -errno; | |
147 | ||
71da1673 | 148 | r = hashmap_ensure_allocated(&database_by_username, &string_hash_ops); |
1b992147 LP |
149 | if (r < 0) |
150 | return r; | |
151 | ||
71da1673 | 152 | r = hashmap_ensure_allocated(&database_by_uid, NULL); |
1b992147 LP |
153 | if (r < 0) |
154 | return r; | |
155 | ||
71da1673 | 156 | r = set_ensure_allocated(&database_users, NULL); |
1b992147 LP |
157 | if (r < 0) |
158 | return r; | |
159 | ||
100d5f6e | 160 | while ((r = fgetpwent_sane(f, &pw)) > 0) { |
43e948ee | 161 | char *n; |
340ac019 | 162 | int k, q; |
1b992147 LP |
163 | |
164 | n = strdup(pw->pw_name); | |
165 | if (!n) | |
166 | return -ENOMEM; | |
167 | ||
71da1673 YW |
168 | k = set_put(database_users, n); |
169 | if (k < 0) { | |
1b992147 LP |
170 | free(n); |
171 | return k; | |
172 | } | |
173 | ||
71da1673 YW |
174 | k = hashmap_put(database_by_username, n, UID_TO_PTR(pw->pw_uid)); |
175 | if (k < 0 && k != -EEXIST) | |
176 | return k; | |
1b992147 | 177 | |
71da1673 YW |
178 | q = hashmap_put(database_by_uid, UID_TO_PTR(pw->pw_uid), n); |
179 | if (q < 0 && q != -EEXIST) | |
180 | return q; | |
1b992147 | 181 | } |
100d5f6e | 182 | return r; |
1b992147 LP |
183 | } |
184 | ||
185 | static int load_group_database(void) { | |
186 | _cleanup_fclose_ FILE *f = NULL; | |
187 | const char *group_path; | |
188 | struct group *gr; | |
189 | int r; | |
190 | ||
1d13f648 | 191 | group_path = prefix_roota(arg_root, "/etc/group"); |
1b992147 LP |
192 | f = fopen(group_path, "re"); |
193 | if (!f) | |
194 | return errno == ENOENT ? 0 : -errno; | |
195 | ||
71da1673 | 196 | r = hashmap_ensure_allocated(&database_by_groupname, &string_hash_ops); |
1b992147 LP |
197 | if (r < 0) |
198 | return r; | |
199 | ||
71da1673 | 200 | r = hashmap_ensure_allocated(&database_by_gid, NULL); |
1b992147 LP |
201 | if (r < 0) |
202 | return r; | |
203 | ||
71da1673 | 204 | r = set_ensure_allocated(&database_groups, NULL); |
1b992147 LP |
205 | if (r < 0) |
206 | return r; | |
207 | ||
f030d36c | 208 | while ((r = fgetgrent_sane(f, &gr)) > 0) { |
1b992147 LP |
209 | char *n; |
210 | int k, q; | |
211 | ||
212 | n = strdup(gr->gr_name); | |
213 | if (!n) | |
214 | return -ENOMEM; | |
215 | ||
71da1673 YW |
216 | k = set_put(database_groups, n); |
217 | if (k < 0) { | |
1b992147 LP |
218 | free(n); |
219 | return k; | |
220 | } | |
221 | ||
71da1673 YW |
222 | k = hashmap_put(database_by_groupname, n, GID_TO_PTR(gr->gr_gid)); |
223 | if (k < 0 && k != -EEXIST) | |
224 | return k; | |
1b992147 | 225 | |
71da1673 YW |
226 | q = hashmap_put(database_by_gid, GID_TO_PTR(gr->gr_gid), n); |
227 | if (q < 0 && q != -EEXIST) | |
228 | return q; | |
1b992147 | 229 | } |
f030d36c | 230 | return r; |
1b992147 LP |
231 | } |
232 | ||
9f1c1940 | 233 | static int make_backup(const char *target, const char *x) { |
62c03398 | 234 | _cleanup_(unlink_and_freep) char *dst_tmp = NULL; |
9f1c1940 | 235 | _cleanup_fclose_ FILE *dst = NULL; |
62c03398 LP |
236 | _cleanup_close_ int src = -1; |
237 | const char *backup; | |
1b992147 LP |
238 | struct stat st; |
239 | int r; | |
240 | ||
62c03398 LP |
241 | assert(target); |
242 | assert(x); | |
243 | ||
1b992147 LP |
244 | src = open(x, O_RDONLY|O_CLOEXEC|O_NOCTTY); |
245 | if (src < 0) { | |
246 | if (errno == ENOENT) /* No backup necessary... */ | |
247 | return 0; | |
248 | ||
249 | return -errno; | |
250 | } | |
251 | ||
252 | if (fstat(src, &st) < 0) | |
253 | return -errno; | |
254 | ||
62c03398 LP |
255 | r = fopen_temporary_label( |
256 | target, /* The path for which to the lookup the label */ | |
257 | x, /* Where we want the file actually to end up */ | |
258 | &dst, | |
259 | &dst_tmp /* The temporary file we write to */); | |
9f1c1940 ZJS |
260 | if (r < 0) |
261 | return r; | |
1b992147 | 262 | |
f5fbe71d | 263 | r = copy_bytes(src, fileno(dst), UINT64_MAX, COPY_REFLINK); |
1b992147 | 264 | if (r < 0) |
62c03398 | 265 | return r; |
9f1c1940 | 266 | |
63c372cb | 267 | backup = strjoina(x, "-"); |
9f1c1940 | 268 | |
62c03398 LP |
269 | /* Copy over the access mask. Don't fail on chmod() or chown(). If it stays owned by us and/or |
270 | * unreadable by others, then it isn't too bad... */ | |
271 | r = fchmod_and_chown(fileno(dst), st.st_mode & 07777, st.st_uid, st.st_gid); | |
c039af23 YW |
272 | if (r < 0) |
273 | log_warning_errno(r, "Failed to change access mode or ownership of %s: %m", backup); | |
1b992147 | 274 | |
62c03398 | 275 | if (futimens(fileno(dst), (const struct timespec[2]) { st.st_atim, st.st_mtim }) < 0) |
56f64d95 | 276 | log_warning_errno(errno, "Failed to fix access and modification time of %s: %m", backup); |
1b992147 | 277 | |
62c03398 | 278 | r = fsync_full(fileno(dst)); |
0675e94a | 279 | if (r < 0) |
62c03398 | 280 | return r; |
0675e94a | 281 | |
62c03398 LP |
282 | if (rename(dst_tmp, backup) < 0) |
283 | return errno; | |
1b992147 | 284 | |
7802194a | 285 | dst_tmp = mfree(dst_tmp); /* disable the unlink_and_freep() hook now that the file has been renamed */ |
1b992147 | 286 | return 0; |
1b992147 LP |
287 | } |
288 | ||
a12b0cc3 LP |
289 | static int putgrent_with_members(const struct group *gr, FILE *group) { |
290 | char **a; | |
291 | ||
292 | assert(gr); | |
293 | assert(group); | |
294 | ||
5bc9c980 | 295 | a = ordered_hashmap_get(members, gr->gr_name); |
a12b0cc3 LP |
296 | if (a) { |
297 | _cleanup_strv_free_ char **l = NULL; | |
298 | bool added = false; | |
299 | char **i; | |
300 | ||
301 | l = strv_copy(gr->gr_mem); | |
302 | if (!l) | |
303 | return -ENOMEM; | |
304 | ||
305 | STRV_FOREACH(i, a) { | |
306 | if (strv_find(l, *i)) | |
307 | continue; | |
308 | ||
309 | if (strv_extend(&l, *i) < 0) | |
310 | return -ENOMEM; | |
311 | ||
312 | added = true; | |
313 | } | |
314 | ||
315 | if (added) { | |
316 | struct group t; | |
100d5f6e | 317 | int r; |
a12b0cc3 LP |
318 | |
319 | strv_uniq(l); | |
320 | strv_sort(l); | |
321 | ||
322 | t = *gr; | |
323 | t.gr_mem = l; | |
324 | ||
100d5f6e FB |
325 | r = putgrent_sane(&t, group); |
326 | return r < 0 ? r : 1; | |
a12b0cc3 LP |
327 | } |
328 | } | |
329 | ||
100d5f6e | 330 | return putgrent_sane(gr, group); |
a12b0cc3 LP |
331 | } |
332 | ||
349cc4a5 | 333 | #if ENABLE_GSHADOW |
9ab315cc LP |
334 | static int putsgent_with_members(const struct sgrp *sg, FILE *gshadow) { |
335 | char **a; | |
336 | ||
337 | assert(sg); | |
338 | assert(gshadow); | |
339 | ||
5bc9c980 | 340 | a = ordered_hashmap_get(members, sg->sg_namp); |
9ab315cc LP |
341 | if (a) { |
342 | _cleanup_strv_free_ char **l = NULL; | |
343 | bool added = false; | |
344 | char **i; | |
345 | ||
346 | l = strv_copy(sg->sg_mem); | |
347 | if (!l) | |
348 | return -ENOMEM; | |
349 | ||
350 | STRV_FOREACH(i, a) { | |
351 | if (strv_find(l, *i)) | |
352 | continue; | |
353 | ||
354 | if (strv_extend(&l, *i) < 0) | |
355 | return -ENOMEM; | |
356 | ||
357 | added = true; | |
358 | } | |
359 | ||
360 | if (added) { | |
361 | struct sgrp t; | |
100d5f6e | 362 | int r; |
9ab315cc LP |
363 | |
364 | strv_uniq(l); | |
365 | strv_sort(l); | |
366 | ||
367 | t = *sg; | |
368 | t.sg_mem = l; | |
369 | ||
100d5f6e FB |
370 | r = putsgent_sane(&t, gshadow); |
371 | return r < 0 ? r : 1; | |
9ab315cc LP |
372 | } |
373 | } | |
374 | ||
100d5f6e | 375 | return putsgent_sane(sg, gshadow); |
9ab315cc | 376 | } |
b14e1b43 | 377 | #endif |
9ab315cc | 378 | |
7b1aaf66 | 379 | static const char* default_shell(uid_t uid) { |
6db90462 | 380 | return uid == 0 ? "/bin/sh" : NOLOGIN; |
7b1aaf66 ZJS |
381 | } |
382 | ||
b20b0b66 FB |
383 | static int write_temporary_passwd(const char *passwd_path, FILE **tmpfile, char **tmpfile_path) { |
384 | _cleanup_fclose_ FILE *original = NULL, *passwd = NULL; | |
1dd98a71 | 385 | _cleanup_(unlink_and_freep) char *passwd_tmp = NULL; |
563dc6f8 | 386 | struct passwd *pw = NULL; |
1b992147 LP |
387 | Item *i; |
388 | int r; | |
389 | ||
5bc9c980 | 390 | if (ordered_hashmap_size(todo_uids) == 0) |
b20b0b66 | 391 | return 0; |
1b992147 | 392 | |
b20b0b66 FB |
393 | r = fopen_temporary_label("/etc/passwd", passwd_path, &passwd, &passwd_tmp); |
394 | if (r < 0) | |
335f6ab4 | 395 | return log_debug_errno(r, "Failed to open temporary copy of %s: %m", passwd_path); |
b20b0b66 FB |
396 | |
397 | original = fopen(passwd_path, "re"); | |
398 | if (original) { | |
b20b0b66 | 399 | |
82855aa6 ZJS |
400 | /* Allow fallback path for when /proc is not mounted. On any normal system /proc will be |
401 | * mounted, but e.g. when 'dnf --installroot' is used, it might not be. There is no security | |
402 | * relevance here, since the environment is ultimately trusted, and not requiring /proc makes | |
403 | * it easier to depend on sysusers in packaging scripts and suchlike. */ | |
404 | r = copy_rights_with_fallback(fileno(original), fileno(passwd), passwd_tmp); | |
1b992147 | 405 | if (r < 0) |
335f6ab4 ZJS |
406 | return log_debug_errno(r, "Failed to copy permissions from %s to %s: %m", |
407 | passwd_path, passwd_tmp); | |
1b992147 | 408 | |
100d5f6e | 409 | while ((r = fgetpwent_sane(original, &pw)) > 0) { |
5bc9c980 | 410 | i = ordered_hashmap_get(users, pw->pw_name); |
330d1def ZJS |
411 | if (i && i->todo_user) |
412 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
413 | "%s: User \"%s\" already exists.", | |
414 | passwd_path, pw->pw_name); | |
e3c72c21 | 415 | |
330d1def ZJS |
416 | if (ordered_hashmap_contains(todo_uids, UID_TO_PTR(pw->pw_uid))) |
417 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
418 | "%s: Detected collision for UID " UID_FMT ".", | |
419 | passwd_path, pw->pw_uid); | |
b20b0b66 | 420 | |
563dc6f8 FB |
421 | /* Make sure we keep the NIS entries (if any) at the end. */ |
422 | if (IN_SET(pw->pw_name[0], '+', '-')) | |
423 | break; | |
424 | ||
100d5f6e FB |
425 | r = putpwent_sane(pw, passwd); |
426 | if (r < 0) | |
335f6ab4 ZJS |
427 | return log_debug_errno(r, "Failed to add existing user \"%s\" to temporary passwd file: %m", |
428 | pw->pw_name); | |
b20b0b66 | 429 | } |
100d5f6e | 430 | if (r < 0) |
335f6ab4 | 431 | return log_debug_errno(r, "Failed to read %s: %m", passwd_path); |
1dd98a71 FB |
432 | |
433 | } else { | |
434 | if (errno != ENOENT) | |
335f6ab4 | 435 | return log_debug_errno(errno, "Failed to open %s: %m", passwd_path); |
1dd98a71 | 436 | if (fchmod(fileno(passwd), 0644) < 0) |
335f6ab4 | 437 | return log_debug_errno(errno, "Failed to fchmod %s: %m", passwd_tmp); |
b20b0b66 FB |
438 | } |
439 | ||
90e74a66 | 440 | ORDERED_HASHMAP_FOREACH(i, todo_uids) { |
99e9f896 LP |
441 | _cleanup_free_ char *creds_shell = NULL, *cn = NULL; |
442 | ||
b20b0b66 FB |
443 | struct passwd n = { |
444 | .pw_name = i->name, | |
445 | .pw_uid = i->uid, | |
446 | .pw_gid = i->gid, | |
d610e142 | 447 | .pw_gecos = (char*) strempty(i->description), |
1b992147 | 448 | |
b20b0b66 | 449 | /* "x" means the password is stored in the shadow file */ |
53c25ac9 | 450 | .pw_passwd = (char*) PASSWORD_SEE_SHADOW, |
a12b0cc3 | 451 | |
b20b0b66 | 452 | /* We default to the root directory as home */ |
636e72bc | 453 | .pw_dir = i->home ?: (char*) "/", |
1b992147 | 454 | |
b20b0b66 FB |
455 | /* Initialize the shell to nologin, with one exception: |
456 | * for root we patch in something special */ | |
7b1aaf66 | 457 | .pw_shell = i->shell ?: (char*) default_shell(i->uid), |
b20b0b66 | 458 | }; |
9ab315cc | 459 | |
99e9f896 LP |
460 | /* Try to pick up the shell for this account via the credentials logic */ |
461 | cn = strjoin("passwd.shell.", i->name); | |
462 | if (!cn) | |
463 | return -ENOMEM; | |
464 | ||
465 | r = read_credential(cn, (void**) &creds_shell, NULL); | |
466 | if (r < 0) | |
467 | log_debug_errno(r, "Couldn't read credential '%s', ignoring: %m", cn); | |
468 | else | |
469 | n.pw_shell = creds_shell; | |
470 | ||
100d5f6e FB |
471 | r = putpwent_sane(&n, passwd); |
472 | if (r < 0) | |
335f6ab4 ZJS |
473 | return log_debug_errno(r, "Failed to add new user \"%s\" to temporary passwd file: %m", |
474 | pw->pw_name); | |
b20b0b66 FB |
475 | } |
476 | ||
563dc6f8 FB |
477 | /* Append the remaining NIS entries if any */ |
478 | while (pw) { | |
100d5f6e FB |
479 | r = putpwent_sane(pw, passwd); |
480 | if (r < 0) | |
335f6ab4 ZJS |
481 | return log_debug_errno(r, "Failed to add existing user \"%s\" to temporary passwd file: %m", |
482 | pw->pw_name); | |
563dc6f8 | 483 | |
100d5f6e FB |
484 | r = fgetpwent_sane(original, &pw); |
485 | if (r < 0) | |
335f6ab4 | 486 | return log_debug_errno(r, "Failed to read %s: %m", passwd_path); |
100d5f6e FB |
487 | if (r == 0) |
488 | break; | |
563dc6f8 | 489 | } |
563dc6f8 | 490 | |
b20b0b66 FB |
491 | r = fflush_and_check(passwd); |
492 | if (r < 0) | |
335f6ab4 | 493 | return log_debug_errno(r, "Failed to flush %s: %m", passwd_tmp); |
b20b0b66 | 494 | |
1cc6c93a YW |
495 | *tmpfile = TAKE_PTR(passwd); |
496 | *tmpfile_path = TAKE_PTR(passwd_tmp); | |
497 | ||
b20b0b66 | 498 | return 0; |
b20b0b66 FB |
499 | } |
500 | ||
501 | static int write_temporary_shadow(const char *shadow_path, FILE **tmpfile, char **tmpfile_path) { | |
502 | _cleanup_fclose_ FILE *original = NULL, *shadow = NULL; | |
1dd98a71 | 503 | _cleanup_(unlink_and_freep) char *shadow_tmp = NULL; |
19ec7de2 | 504 | struct spwd *sp = NULL; |
b20b0b66 FB |
505 | long lstchg; |
506 | Item *i; | |
507 | int r; | |
508 | ||
5bc9c980 | 509 | if (ordered_hashmap_size(todo_uids) == 0) |
b20b0b66 FB |
510 | return 0; |
511 | ||
512 | r = fopen_temporary_label("/etc/shadow", shadow_path, &shadow, &shadow_tmp); | |
513 | if (r < 0) | |
335f6ab4 | 514 | return log_debug_errno(r, "Failed to open temporary copy of %s: %m", shadow_path); |
9ab315cc | 515 | |
b20b0b66 FB |
516 | lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY); |
517 | ||
518 | original = fopen(shadow_path, "re"); | |
519 | if (original) { | |
b20b0b66 | 520 | |
82855aa6 | 521 | r = copy_rights_with_fallback(fileno(original), fileno(shadow), shadow_tmp); |
9ab315cc | 522 | if (r < 0) |
335f6ab4 ZJS |
523 | return log_debug_errno(r, "Failed to copy permissions from %s to %s: %m", |
524 | shadow_path, shadow_tmp); | |
9ab315cc | 525 | |
100d5f6e | 526 | while ((r = fgetspent_sane(original, &sp)) > 0) { |
5bc9c980 | 527 | i = ordered_hashmap_get(users, sp->sp_namp); |
b20b0b66 FB |
528 | if (i && i->todo_user) { |
529 | /* we will update the existing entry */ | |
530 | sp->sp_lstchg = lstchg; | |
e3c72c21 | 531 | |
b20b0b66 FB |
532 | /* only the /etc/shadow stage is left, so we can |
533 | * safely remove the item from the todo set */ | |
534 | i->todo_user = false; | |
5bc9c980 | 535 | ordered_hashmap_remove(todo_uids, UID_TO_PTR(i->uid)); |
9ab315cc | 536 | } |
b20b0b66 | 537 | |
19ec7de2 FB |
538 | /* Make sure we keep the NIS entries (if any) at the end. */ |
539 | if (IN_SET(sp->sp_namp[0], '+', '-')) | |
540 | break; | |
541 | ||
100d5f6e FB |
542 | r = putspent_sane(sp, shadow); |
543 | if (r < 0) | |
335f6ab4 ZJS |
544 | return log_debug_errno(r, "Failed to add existing user \"%s\" to temporary shadow file: %m", |
545 | sp->sp_namp); | |
546 | ||
b20b0b66 | 547 | } |
100d5f6e | 548 | if (r < 0) |
335f6ab4 | 549 | return log_debug_errno(r, "Failed to read %s: %m", shadow_path); |
1dd98a71 FB |
550 | |
551 | } else { | |
552 | if (errno != ENOENT) | |
335f6ab4 | 553 | return log_debug_errno(errno, "Failed to open %s: %m", shadow_path); |
1dd98a71 | 554 | if (fchmod(fileno(shadow), 0000) < 0) |
335f6ab4 | 555 | return log_debug_errno(errno, "Failed to fchmod %s: %m", shadow_tmp); |
b20b0b66 | 556 | } |
9ab315cc | 557 | |
90e74a66 | 558 | ORDERED_HASHMAP_FOREACH(i, todo_uids) { |
99e9f896 LP |
559 | _cleanup_(erase_and_freep) char *creds_password = NULL; |
560 | _cleanup_free_ char *cn = NULL; | |
561 | ||
b20b0b66 FB |
562 | struct spwd n = { |
563 | .sp_namp = i->name, | |
53c25ac9 | 564 | .sp_pwdp = (char*) PASSWORD_LOCKED_AND_INVALID, |
b20b0b66 FB |
565 | .sp_lstchg = lstchg, |
566 | .sp_min = -1, | |
567 | .sp_max = -1, | |
568 | .sp_warn = -1, | |
569 | .sp_inact = -1, | |
12c82937 | 570 | .sp_expire = -1, |
f5fbe71d | 571 | .sp_flag = ULONG_MAX, /* this appears to be what everybody does ... */ |
b20b0b66 | 572 | }; |
9ab315cc | 573 | |
99e9f896 LP |
574 | /* Try to pick up the password for this account via the credentials logic */ |
575 | cn = strjoin("passwd.hashed-password.", i->name); | |
576 | if (!cn) | |
577 | return -ENOMEM; | |
578 | ||
579 | r = read_credential(cn, (void**) &creds_password, NULL); | |
580 | if (r == -ENOENT) { | |
581 | _cleanup_(erase_and_freep) char *plaintext_password = NULL; | |
582 | ||
583 | free(cn); | |
584 | cn = strjoin("passwd.plaintext-password.", i->name); | |
585 | if (!cn) | |
586 | return -ENOMEM; | |
587 | ||
588 | r = read_credential(cn, (void**) &plaintext_password, NULL); | |
589 | if (r < 0) | |
590 | log_debug_errno(r, "Couldn't read credential '%s', ignoring: %m", cn); | |
591 | else { | |
592 | r = hash_password(plaintext_password, &creds_password); | |
593 | if (r < 0) | |
594 | return log_debug_errno(r, "Failed to hash password: %m"); | |
595 | } | |
596 | } else if (r < 0) | |
597 | log_debug_errno(r, "Couldn't read credential '%s', ignoring: %m", cn); | |
598 | ||
599 | if (creds_password) | |
600 | n.sp_pwdp = creds_password; | |
601 | ||
100d5f6e FB |
602 | r = putspent_sane(&n, shadow); |
603 | if (r < 0) | |
335f6ab4 ZJS |
604 | return log_debug_errno(r, "Failed to add new user \"%s\" to temporary shadow file: %m", |
605 | sp->sp_namp); | |
1b992147 | 606 | } |
19ec7de2 FB |
607 | |
608 | /* Append the remaining NIS entries if any */ | |
609 | while (sp) { | |
100d5f6e FB |
610 | r = putspent_sane(sp, shadow); |
611 | if (r < 0) | |
335f6ab4 ZJS |
612 | return log_debug_errno(r, "Failed to add existing user \"%s\" to temporary shadow file: %m", |
613 | sp->sp_namp); | |
19ec7de2 | 614 | |
100d5f6e FB |
615 | r = fgetspent_sane(original, &sp); |
616 | if (r < 0) | |
335f6ab4 | 617 | return log_debug_errno(r, "Failed to read %s: %m", shadow_path); |
100d5f6e FB |
618 | if (r == 0) |
619 | break; | |
19ec7de2 FB |
620 | } |
621 | if (!IN_SET(errno, 0, ENOENT)) | |
622 | return -errno; | |
1b992147 | 623 | |
0675e94a | 624 | r = fflush_sync_and_check(shadow); |
b20b0b66 | 625 | if (r < 0) |
335f6ab4 | 626 | return log_debug_errno(r, "Failed to flush %s: %m", shadow_tmp); |
b20b0b66 | 627 | |
1cc6c93a YW |
628 | *tmpfile = TAKE_PTR(shadow); |
629 | *tmpfile_path = TAKE_PTR(shadow_tmp); | |
630 | ||
b20b0b66 | 631 | return 0; |
b20b0b66 | 632 | } |
1b992147 | 633 | |
b20b0b66 FB |
634 | static int write_temporary_group(const char *group_path, FILE **tmpfile, char **tmpfile_path) { |
635 | _cleanup_fclose_ FILE *original = NULL, *group = NULL; | |
1dd98a71 | 636 | _cleanup_(unlink_and_freep) char *group_tmp = NULL; |
b20b0b66 | 637 | bool group_changed = false; |
563dc6f8 | 638 | struct group *gr = NULL; |
b20b0b66 FB |
639 | Item *i; |
640 | int r; | |
1b992147 | 641 | |
5bc9c980 | 642 | if (ordered_hashmap_size(todo_gids) == 0 && ordered_hashmap_size(members) == 0) |
b20b0b66 | 643 | return 0; |
e3c72c21 | 644 | |
b20b0b66 FB |
645 | r = fopen_temporary_label("/etc/group", group_path, &group, &group_tmp); |
646 | if (r < 0) | |
335f6ab4 | 647 | return log_debug_errno(r, "Failed to open temporary copy of %s: %m", group_path); |
b20b0b66 FB |
648 | |
649 | original = fopen(group_path, "re"); | |
650 | if (original) { | |
b20b0b66 | 651 | |
82855aa6 | 652 | r = copy_rights_with_fallback(fileno(original), fileno(group), group_tmp); |
b20b0b66 | 653 | if (r < 0) |
335f6ab4 ZJS |
654 | return log_debug_errno(r, "Failed to copy permissions from %s to %s: %m", |
655 | group_path, group_tmp); | |
b20b0b66 | 656 | |
100d5f6e | 657 | while ((r = fgetgrent_sane(original, &gr)) > 0) { |
b20b0b66 FB |
658 | /* Safety checks against name and GID collisions. Normally, |
659 | * this should be unnecessary, but given that we look at the | |
660 | * entries anyway here, let's make an extra verification | |
661 | * step that we don't generate duplicate entries. */ | |
662 | ||
5bc9c980 | 663 | i = ordered_hashmap_get(groups, gr->gr_name); |
330d1def ZJS |
664 | if (i && i->todo_group) |
665 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
666 | "%s: Group \"%s\" already exists.", | |
667 | group_path, gr->gr_name); | |
b20b0b66 | 668 | |
330d1def ZJS |
669 | if (ordered_hashmap_contains(todo_gids, GID_TO_PTR(gr->gr_gid))) |
670 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
671 | "%s: Detected collision for GID " GID_FMT ".", | |
672 | group_path, gr->gr_gid); | |
1b992147 | 673 | |
563dc6f8 FB |
674 | /* Make sure we keep the NIS entries (if any) at the end. */ |
675 | if (IN_SET(gr->gr_name[0], '+', '-')) | |
676 | break; | |
677 | ||
b20b0b66 FB |
678 | r = putgrent_with_members(gr, group); |
679 | if (r < 0) | |
335f6ab4 ZJS |
680 | return log_debug_errno(r, "Failed to add existing group \"%s\" to temporary group file: %m", |
681 | gr->gr_name); | |
b20b0b66 FB |
682 | if (r > 0) |
683 | group_changed = true; | |
b20b0b66 | 684 | } |
100d5f6e | 685 | if (r < 0) |
335f6ab4 | 686 | return log_debug_errno(r, "Failed to read %s: %m", group_path); |
1dd98a71 FB |
687 | |
688 | } else { | |
689 | if (errno != ENOENT) | |
335f6ab4 | 690 | return log_debug_errno(errno, "Failed to open %s: %m", group_path); |
1dd98a71 | 691 | if (fchmod(fileno(group), 0644) < 0) |
335f6ab4 | 692 | return log_debug_errno(errno, "Failed to fchmod %s: %m", group_tmp); |
b20b0b66 | 693 | } |
7629889c | 694 | |
90e74a66 | 695 | ORDERED_HASHMAP_FOREACH(i, todo_gids) { |
b20b0b66 FB |
696 | struct group n = { |
697 | .gr_name = i->name, | |
698 | .gr_gid = i->gid, | |
53c25ac9 | 699 | .gr_passwd = (char*) PASSWORD_SEE_SHADOW, |
b20b0b66 | 700 | }; |
1b992147 | 701 | |
b20b0b66 FB |
702 | r = putgrent_with_members(&n, group); |
703 | if (r < 0) | |
335f6ab4 ZJS |
704 | return log_debug_errno(r, "Failed to add new group \"%s\" to temporary group file: %m", |
705 | gr->gr_name); | |
7629889c | 706 | |
b20b0b66 FB |
707 | group_changed = true; |
708 | } | |
1b992147 | 709 | |
563dc6f8 FB |
710 | /* Append the remaining NIS entries if any */ |
711 | while (gr) { | |
100d5f6e FB |
712 | r = putgrent_sane(gr, group); |
713 | if (r < 0) | |
335f6ab4 ZJS |
714 | return log_debug_errno(r, "Failed to add existing group \"%s\" to temporary group file: %m", |
715 | gr->gr_name); | |
563dc6f8 | 716 | |
100d5f6e FB |
717 | r = fgetgrent_sane(original, &gr); |
718 | if (r < 0) | |
335f6ab4 | 719 | return log_debug_errno(r, "Failed to read %s: %m", group_path); |
100d5f6e FB |
720 | if (r == 0) |
721 | break; | |
563dc6f8 | 722 | } |
563dc6f8 | 723 | |
0675e94a | 724 | r = fflush_sync_and_check(group); |
b20b0b66 | 725 | if (r < 0) |
335f6ab4 | 726 | return log_debug_errno(r, "Failed to flush %s: %m", group_tmp); |
1b992147 | 727 | |
b20b0b66 | 728 | if (group_changed) { |
1cc6c93a YW |
729 | *tmpfile = TAKE_PTR(group); |
730 | *tmpfile_path = TAKE_PTR(group_tmp); | |
b20b0b66 FB |
731 | } |
732 | return 0; | |
b20b0b66 FB |
733 | } |
734 | ||
735 | static int write_temporary_gshadow(const char * gshadow_path, FILE **tmpfile, char **tmpfile_path) { | |
349cc4a5 | 736 | #if ENABLE_GSHADOW |
b20b0b66 | 737 | _cleanup_fclose_ FILE *original = NULL, *gshadow = NULL; |
1dd98a71 | 738 | _cleanup_(unlink_and_freep) char *gshadow_tmp = NULL; |
b20b0b66 | 739 | bool group_changed = false; |
b20b0b66 FB |
740 | Item *i; |
741 | int r; | |
742 | ||
5bc9c980 | 743 | if (ordered_hashmap_size(todo_gids) == 0 && ordered_hashmap_size(members) == 0) |
b20b0b66 FB |
744 | return 0; |
745 | ||
746 | r = fopen_temporary_label("/etc/gshadow", gshadow_path, &gshadow, &gshadow_tmp); | |
747 | if (r < 0) | |
335f6ab4 | 748 | return log_debug_errno(r, "Failed to open temporary copy of %s: %m", gshadow_path); |
9ab315cc | 749 | |
b20b0b66 FB |
750 | original = fopen(gshadow_path, "re"); |
751 | if (original) { | |
752 | struct sgrp *sg; | |
753 | ||
82855aa6 | 754 | r = copy_rights_with_fallback(fileno(original), fileno(gshadow), gshadow_tmp); |
1b992147 | 755 | if (r < 0) |
335f6ab4 ZJS |
756 | return log_debug_errno(r, "Failed to copy permissions from %s to %s: %m", |
757 | gshadow_path, gshadow_tmp); | |
9ab315cc | 758 | |
100d5f6e | 759 | while ((r = fgetsgent_sane(original, &sg)) > 0) { |
c5abf225 | 760 | |
5bc9c980 | 761 | i = ordered_hashmap_get(groups, sg->sg_namp); |
330d1def ZJS |
762 | if (i && i->todo_group) |
763 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
764 | "%s: Group \"%s\" already exists.", | |
765 | gshadow_path, sg->sg_namp); | |
9ab315cc | 766 | |
b20b0b66 | 767 | r = putsgent_with_members(sg, gshadow); |
fff19499 | 768 | if (r < 0) |
335f6ab4 ZJS |
769 | return log_debug_errno(r, "Failed to add existing group \"%s\" to temporary gshadow file: %m", |
770 | sg->sg_namp); | |
b20b0b66 FB |
771 | if (r > 0) |
772 | group_changed = true; | |
b20b0b66 | 773 | } |
100d5f6e FB |
774 | if (r < 0) |
775 | return r; | |
1dd98a71 FB |
776 | |
777 | } else { | |
778 | if (errno != ENOENT) | |
335f6ab4 | 779 | return log_debug_errno(errno, "Failed to open %s: %m", gshadow_path); |
1dd98a71 | 780 | if (fchmod(fileno(gshadow), 0000) < 0) |
335f6ab4 | 781 | return log_debug_errno(errno, "Failed to fchmod %s: %m", gshadow_tmp); |
b20b0b66 | 782 | } |
9ab315cc | 783 | |
90e74a66 | 784 | ORDERED_HASHMAP_FOREACH(i, todo_gids) { |
b20b0b66 FB |
785 | struct sgrp n = { |
786 | .sg_namp = i->name, | |
53c25ac9 | 787 | .sg_passwd = (char*) PASSWORD_LOCKED_AND_INVALID, |
b20b0b66 | 788 | }; |
9ab315cc | 789 | |
b20b0b66 | 790 | r = putsgent_with_members(&n, gshadow); |
9ab315cc | 791 | if (r < 0) |
335f6ab4 ZJS |
792 | return log_debug_errno(r, "Failed to add new group \"%s\" to temporary gshadow file: %m", |
793 | n.sg_namp); | |
b20b0b66 FB |
794 | |
795 | group_changed = true; | |
9ab315cc LP |
796 | } |
797 | ||
0675e94a | 798 | r = fflush_sync_and_check(gshadow); |
b20b0b66 | 799 | if (r < 0) |
335f6ab4 | 800 | return log_debug_errno(r, "Failed to flush %s: %m", gshadow_tmp); |
b20b0b66 | 801 | |
9ab315cc | 802 | if (group_changed) { |
1cc6c93a YW |
803 | *tmpfile = TAKE_PTR(gshadow); |
804 | *tmpfile_path = TAKE_PTR(gshadow_tmp); | |
b20b0b66 | 805 | } |
b14e1b43 | 806 | #endif |
62d1c93a | 807 | return 0; |
b20b0b66 FB |
808 | } |
809 | ||
810 | static int write_files(void) { | |
b20b0b66 | 811 | _cleanup_fclose_ FILE *passwd = NULL, *group = NULL, *shadow = NULL, *gshadow = NULL; |
1dd98a71 | 812 | _cleanup_(unlink_and_freep) char *passwd_tmp = NULL, *group_tmp = NULL, *shadow_tmp = NULL, *gshadow_tmp = NULL; |
62d1c93a | 813 | const char *passwd_path, *shadow_path, *group_path, *gshadow_path; |
b20b0b66 FB |
814 | int r; |
815 | ||
816 | passwd_path = prefix_roota(arg_root, "/etc/passwd"); | |
817 | shadow_path = prefix_roota(arg_root, "/etc/shadow"); | |
818 | group_path = prefix_roota(arg_root, "/etc/group"); | |
819 | gshadow_path = prefix_roota(arg_root, "/etc/gshadow"); | |
820 | ||
821 | r = write_temporary_group(group_path, &group, &group_tmp); | |
822 | if (r < 0) | |
1dd98a71 | 823 | return r; |
b20b0b66 FB |
824 | |
825 | r = write_temporary_gshadow(gshadow_path, &gshadow, &gshadow_tmp); | |
826 | if (r < 0) | |
1dd98a71 | 827 | return r; |
b20b0b66 FB |
828 | |
829 | r = write_temporary_passwd(passwd_path, &passwd, &passwd_tmp); | |
830 | if (r < 0) | |
1dd98a71 | 831 | return r; |
b20b0b66 FB |
832 | |
833 | r = write_temporary_shadow(shadow_path, &shadow, &shadow_tmp); | |
834 | if (r < 0) | |
1dd98a71 | 835 | return r; |
b20b0b66 FB |
836 | |
837 | /* Make a backup of the old files */ | |
838 | if (group) { | |
839 | r = make_backup("/etc/group", group_path); | |
840 | if (r < 0) | |
335f6ab4 | 841 | return log_debug_errno(r, "Failed to make backup %s: %m", group_path); |
b20b0b66 FB |
842 | } |
843 | if (gshadow) { | |
844 | r = make_backup("/etc/gshadow", gshadow_path); | |
845 | if (r < 0) | |
335f6ab4 | 846 | return log_debug_errno(r, "Failed to make backup %s: %m", gshadow_path); |
1b992147 LP |
847 | } |
848 | ||
849 | if (passwd) { | |
9f1c1940 | 850 | r = make_backup("/etc/passwd", passwd_path); |
1b992147 | 851 | if (r < 0) |
335f6ab4 | 852 | return log_debug_errno(r, "Failed to make backup %s: %m", passwd_path); |
1b992147 | 853 | } |
9ab315cc LP |
854 | if (shadow) { |
855 | r = make_backup("/etc/shadow", shadow_path); | |
856 | if (r < 0) | |
335f6ab4 | 857 | return log_debug_errno(r, "Failed to make backup %s: %m", shadow_path); |
9ab315cc | 858 | } |
1b992147 LP |
859 | |
860 | /* And make the new files count */ | |
b20b0b66 | 861 | if (group) { |
2da3dc69 | 862 | r = rename_and_apply_smack_floor_label(group_tmp, group_path); |
b20b0b66 | 863 | if (r < 0) |
335f6ab4 ZJS |
864 | return log_debug_errno(r, "Failed to rename %s to %s: %m", |
865 | group_tmp, group_path); | |
b20b0b66 | 866 | group_tmp = mfree(group_tmp); |
d481b830 FB |
867 | |
868 | if (!arg_root && !arg_image) | |
869 | (void) nscd_flush_cache(STRV_MAKE("group")); | |
b20b0b66 FB |
870 | } |
871 | if (gshadow) { | |
2da3dc69 | 872 | r = rename_and_apply_smack_floor_label(gshadow_tmp, gshadow_path); |
b20b0b66 | 873 | if (r < 0) |
335f6ab4 ZJS |
874 | return log_debug_errno(r, "Failed to rename %s to %s: %m", |
875 | gshadow_tmp, gshadow_path); | |
1b992147 | 876 | |
b20b0b66 | 877 | gshadow_tmp = mfree(gshadow_tmp); |
1b992147 LP |
878 | } |
879 | ||
880 | if (passwd) { | |
2da3dc69 | 881 | r = rename_and_apply_smack_floor_label(passwd_tmp, passwd_path); |
c02e7b1e | 882 | if (r < 0) |
335f6ab4 ZJS |
883 | return log_debug_errno(r, "Failed to rename %s to %s: %m", |
884 | passwd_tmp, passwd_path); | |
1b992147 | 885 | |
97b11eed | 886 | passwd_tmp = mfree(passwd_tmp); |
d481b830 FB |
887 | |
888 | if (!arg_root && !arg_image) | |
889 | (void) nscd_flush_cache(STRV_MAKE("passwd")); | |
1b992147 | 890 | } |
9ab315cc | 891 | if (shadow) { |
2da3dc69 | 892 | r = rename_and_apply_smack_floor_label(shadow_tmp, shadow_path); |
c02e7b1e | 893 | if (r < 0) |
335f6ab4 ZJS |
894 | return log_debug_errno(r, "Failed to rename %s to %s: %m", |
895 | shadow_tmp, shadow_path); | |
9ab315cc | 896 | |
97b11eed | 897 | shadow_tmp = mfree(shadow_tmp); |
9ab315cc | 898 | } |
1b992147 | 899 | |
1dd98a71 | 900 | return 0; |
1b992147 LP |
901 | } |
902 | ||
b9ee05c2 | 903 | static int uid_is_ok(uid_t uid, const char *name, bool check_with_gid) { |
1b992147 LP |
904 | |
905 | /* Let's see if we already have assigned the UID a second time */ | |
5bc9c980 | 906 | if (ordered_hashmap_get(todo_uids, UID_TO_PTR(uid))) |
1b992147 LP |
907 | return 0; |
908 | ||
909 | /* Try to avoid using uids that are already used by a group | |
910 | * that doesn't have the same name as our new user. */ | |
b9ee05c2 | 911 | if (check_with_gid) { |
62d1c93a ZJS |
912 | Item *i; |
913 | ||
b9ee05c2 MV |
914 | i = ordered_hashmap_get(todo_gids, GID_TO_PTR(uid)); |
915 | if (i && !streq(i->name, name)) | |
916 | return 0; | |
917 | } | |
1b992147 LP |
918 | |
919 | /* Let's check the files directly */ | |
71da1673 | 920 | if (hashmap_contains(database_by_uid, UID_TO_PTR(uid))) |
1b992147 LP |
921 | return 0; |
922 | ||
b9ee05c2 | 923 | if (check_with_gid) { |
62d1c93a ZJS |
924 | const char *n; |
925 | ||
71da1673 | 926 | n = hashmap_get(database_by_gid, GID_TO_PTR(uid)); |
b9ee05c2 MV |
927 | if (n && !streq(n, name)) |
928 | return 0; | |
929 | } | |
1b992147 LP |
930 | |
931 | /* Let's also check via NSS, to avoid UID clashes over LDAP and such, just in case */ | |
932 | if (!arg_root) { | |
62d1c93a ZJS |
933 | struct passwd *p; |
934 | struct group *g; | |
935 | ||
1b992147 LP |
936 | errno = 0; |
937 | p = getpwuid(uid); | |
938 | if (p) | |
939 | return 0; | |
b0284aba | 940 | if (!IN_SET(errno, 0, ENOENT)) |
1b992147 LP |
941 | return -errno; |
942 | ||
b9ee05c2 MV |
943 | if (check_with_gid) { |
944 | errno = 0; | |
945 | g = getgrgid((gid_t) uid); | |
946 | if (g) { | |
947 | if (!streq(g->gr_name, name)) | |
948 | return 0; | |
949 | } else if (!IN_SET(errno, 0, ENOENT)) | |
950 | return -errno; | |
951 | } | |
1b992147 LP |
952 | } |
953 | ||
954 | return 1; | |
955 | } | |
956 | ||
957 | static int root_stat(const char *p, struct stat *st) { | |
958 | const char *fix; | |
959 | ||
1d13f648 | 960 | fix = prefix_roota(arg_root, p); |
1b992147 LP |
961 | if (stat(fix, st) < 0) |
962 | return -errno; | |
963 | ||
964 | return 0; | |
965 | } | |
966 | ||
967 | static int read_id_from_file(Item *i, uid_t *_uid, gid_t *_gid) { | |
968 | struct stat st; | |
969 | bool found_uid = false, found_gid = false; | |
56d21cde PDS |
970 | uid_t uid = 0; |
971 | gid_t gid = 0; | |
1b992147 LP |
972 | |
973 | assert(i); | |
974 | ||
975 | /* First, try to get the gid directly */ | |
976 | if (_gid && i->gid_path && root_stat(i->gid_path, &st) >= 0) { | |
977 | gid = st.st_gid; | |
978 | found_gid = true; | |
979 | } | |
980 | ||
981 | /* Then, try to get the uid directly */ | |
982 | if ((_uid || (_gid && !found_gid)) | |
983 | && i->uid_path | |
984 | && root_stat(i->uid_path, &st) >= 0) { | |
985 | ||
986 | uid = st.st_uid; | |
987 | found_uid = true; | |
988 | ||
989 | /* If we need the gid, but had no success yet, also derive it from the uid path */ | |
990 | if (_gid && !found_gid) { | |
991 | gid = st.st_gid; | |
992 | found_gid = true; | |
993 | } | |
994 | } | |
995 | ||
996 | /* If that didn't work yet, then let's reuse the gid as uid */ | |
997 | if (_uid && !found_uid && i->gid_path) { | |
998 | ||
999 | if (found_gid) { | |
1000 | uid = (uid_t) gid; | |
1001 | found_uid = true; | |
1002 | } else if (root_stat(i->gid_path, &st) >= 0) { | |
1003 | uid = (uid_t) st.st_gid; | |
1004 | found_uid = true; | |
1005 | } | |
1006 | } | |
1007 | ||
1008 | if (_uid) { | |
1009 | if (!found_uid) | |
1010 | return 0; | |
1011 | ||
1012 | *_uid = uid; | |
1013 | } | |
1014 | ||
1015 | if (_gid) { | |
1016 | if (!found_gid) | |
1017 | return 0; | |
1018 | ||
1019 | *_gid = gid; | |
1020 | } | |
1021 | ||
1022 | return 1; | |
1023 | } | |
1024 | ||
1025 | static int add_user(Item *i) { | |
1026 | void *z; | |
1027 | int r; | |
1028 | ||
1029 | assert(i); | |
1030 | ||
1031 | /* Check the database directly */ | |
71da1673 | 1032 | z = hashmap_get(database_by_username, i->name); |
1b992147 LP |
1033 | if (z) { |
1034 | log_debug("User %s already exists.", i->name); | |
c1b6b04f | 1035 | i->uid = PTR_TO_UID(z); |
1b992147 LP |
1036 | i->uid_set = true; |
1037 | return 0; | |
1038 | } | |
1039 | ||
1040 | if (!arg_root) { | |
1041 | struct passwd *p; | |
1b992147 LP |
1042 | |
1043 | /* Also check NSS */ | |
1044 | errno = 0; | |
1045 | p = getpwnam(i->name); | |
1046 | if (p) { | |
1047 | log_debug("User %s already exists.", i->name); | |
1048 | i->uid = p->pw_uid; | |
1049 | i->uid_set = true; | |
1050 | ||
2fc09a9c DM |
1051 | r = free_and_strdup(&i->description, p->pw_gecos); |
1052 | if (r < 0) | |
1053 | return log_oom(); | |
1054 | ||
1b992147 LP |
1055 | return 0; |
1056 | } | |
08c7c321 | 1057 | if (!errno_is_not_exists(errno)) |
4a62c710 | 1058 | return log_error_errno(errno, "Failed to check if user %s already exists: %m", i->name); |
1b992147 LP |
1059 | } |
1060 | ||
1061 | /* Try to use the suggested numeric uid */ | |
1062 | if (i->uid_set) { | |
b9ee05c2 | 1063 | r = uid_is_ok(i->uid, i->name, !i->id_set_strict); |
f647962d MS |
1064 | if (r < 0) |
1065 | return log_error_errno(r, "Failed to verify uid " UID_FMT ": %m", i->uid); | |
1b992147 LP |
1066 | if (r == 0) { |
1067 | log_debug("Suggested user ID " UID_FMT " for %s already used.", i->uid, i->name); | |
1068 | i->uid_set = false; | |
1069 | } | |
1070 | } | |
1071 | ||
1072 | /* If that didn't work, try to read it from the specified path */ | |
1073 | if (!i->uid_set) { | |
1074 | uid_t c; | |
1075 | ||
1076 | if (read_id_from_file(i, &c, NULL) > 0) { | |
1077 | ||
8530dc44 | 1078 | if (c <= 0 || !uid_range_contains(uid_range, n_uid_range, c)) |
1b992147 LP |
1079 | log_debug("User ID " UID_FMT " of file not suitable for %s.", c, i->name); |
1080 | else { | |
b9ee05c2 | 1081 | r = uid_is_ok(c, i->name, true); |
f647962d MS |
1082 | if (r < 0) |
1083 | return log_error_errno(r, "Failed to verify uid " UID_FMT ": %m", i->uid); | |
1084 | else if (r > 0) { | |
1b992147 LP |
1085 | i->uid = c; |
1086 | i->uid_set = true; | |
1087 | } else | |
1088 | log_debug("User ID " UID_FMT " of file for %s is already used.", c, i->name); | |
1089 | } | |
1090 | } | |
1091 | } | |
1092 | ||
b938cb90 | 1093 | /* Otherwise, try to reuse the group ID */ |
1b992147 | 1094 | if (!i->uid_set && i->gid_set) { |
b9ee05c2 | 1095 | r = uid_is_ok((uid_t) i->gid, i->name, true); |
f647962d MS |
1096 | if (r < 0) |
1097 | return log_error_errno(r, "Failed to verify uid " UID_FMT ": %m", i->uid); | |
1b992147 LP |
1098 | if (r > 0) { |
1099 | i->uid = (uid_t) i->gid; | |
1100 | i->uid_set = true; | |
1101 | } | |
1102 | } | |
1103 | ||
1104 | /* And if that didn't work either, let's try to find a free one */ | |
1105 | if (!i->uid_set) { | |
4b6f9b20 ZJS |
1106 | maybe_emit_login_defs_warning(); |
1107 | ||
8530dc44 LP |
1108 | for (;;) { |
1109 | r = uid_range_next_lower(uid_range, n_uid_range, &search_uid); | |
330d1def ZJS |
1110 | if (r < 0) |
1111 | return log_error_errno(r, "No free user ID available for %s.", i->name); | |
1b992147 | 1112 | |
b9ee05c2 | 1113 | r = uid_is_ok(search_uid, i->name, true); |
f647962d MS |
1114 | if (r < 0) |
1115 | return log_error_errno(r, "Failed to verify uid " UID_FMT ": %m", i->uid); | |
1116 | else if (r > 0) | |
1b992147 LP |
1117 | break; |
1118 | } | |
1119 | ||
1b992147 LP |
1120 | i->uid_set = true; |
1121 | i->uid = search_uid; | |
1b992147 LP |
1122 | } |
1123 | ||
1dfb9321 SS |
1124 | r = ordered_hashmap_ensure_put(&todo_uids, NULL, UID_TO_PTR(i->uid), i); |
1125 | if (r == -EEXIST) | |
1126 | return log_error_errno(r, "Requested user %s with uid " UID_FMT " and gid" GID_FMT " to be created is duplicated " | |
1127 | "or conflicts with another user.", i->name, i->uid, i->gid); | |
1128 | if (r == -ENOMEM) | |
1b992147 | 1129 | return log_oom(); |
1b992147 | 1130 | if (r < 0) |
1dfb9321 SS |
1131 | return log_error_errno(r, "Failed to store user %s with uid " UID_FMT " and gid " GID_FMT " to be created: %m", |
1132 | i->name, i->uid, i->gid); | |
1b992147 | 1133 | |
c1b6b04f | 1134 | i->todo_user = true; |
62d1c93a ZJS |
1135 | log_info("Creating user %s (%s) with uid " UID_FMT " and gid " GID_FMT ".", |
1136 | i->name, strna(i->description), i->uid, i->gid); | |
1b992147 LP |
1137 | |
1138 | return 0; | |
1139 | } | |
1140 | ||
1141 | static int gid_is_ok(gid_t gid) { | |
1142 | struct group *g; | |
1143 | struct passwd *p; | |
1144 | ||
5bc9c980 | 1145 | if (ordered_hashmap_get(todo_gids, GID_TO_PTR(gid))) |
1b992147 LP |
1146 | return 0; |
1147 | ||
1148 | /* Avoid reusing gids that are already used by a different user */ | |
5bc9c980 | 1149 | if (ordered_hashmap_get(todo_uids, UID_TO_PTR(gid))) |
1b992147 LP |
1150 | return 0; |
1151 | ||
71da1673 | 1152 | if (hashmap_contains(database_by_gid, GID_TO_PTR(gid))) |
1b992147 LP |
1153 | return 0; |
1154 | ||
71da1673 | 1155 | if (hashmap_contains(database_by_uid, UID_TO_PTR(gid))) |
1b992147 LP |
1156 | return 0; |
1157 | ||
1158 | if (!arg_root) { | |
1159 | errno = 0; | |
1160 | g = getgrgid(gid); | |
1161 | if (g) | |
1162 | return 0; | |
b0284aba | 1163 | if (!IN_SET(errno, 0, ENOENT)) |
1b992147 LP |
1164 | return -errno; |
1165 | ||
1166 | errno = 0; | |
1167 | p = getpwuid((uid_t) gid); | |
1168 | if (p) | |
1169 | return 0; | |
b0284aba | 1170 | if (!IN_SET(errno, 0, ENOENT)) |
1b992147 LP |
1171 | return -errno; |
1172 | } | |
1173 | ||
1174 | return 1; | |
1175 | } | |
1176 | ||
649916d3 | 1177 | static int get_gid_by_name(const char *name, gid_t *gid) { |
1b992147 | 1178 | void *z; |
1b992147 | 1179 | |
649916d3 | 1180 | assert(gid); |
1b992147 LP |
1181 | |
1182 | /* Check the database directly */ | |
649916d3 | 1183 | z = hashmap_get(database_by_groupname, name); |
1b992147 | 1184 | if (z) { |
649916d3 | 1185 | *gid = PTR_TO_GID(z); |
1b992147 LP |
1186 | return 0; |
1187 | } | |
1188 | ||
1189 | /* Also check NSS */ | |
1190 | if (!arg_root) { | |
1191 | struct group *g; | |
1192 | ||
1193 | errno = 0; | |
649916d3 | 1194 | g = getgrnam(name); |
1b992147 | 1195 | if (g) { |
649916d3 | 1196 | *gid = g->gr_gid; |
1b992147 LP |
1197 | return 0; |
1198 | } | |
08c7c321 | 1199 | if (!errno_is_not_exists(errno)) |
649916d3 DM |
1200 | return log_error_errno(errno, "Failed to check if group %s already exists: %m", name); |
1201 | } | |
1202 | ||
1203 | return -ENOENT; | |
1204 | } | |
1205 | ||
1206 | static int add_group(Item *i) { | |
1207 | int r; | |
1208 | ||
1209 | assert(i); | |
1210 | ||
1211 | r = get_gid_by_name(i->name, &i->gid); | |
1212 | if (r != -ENOENT) { | |
1213 | if (r < 0) | |
1214 | return r; | |
1215 | log_debug("Group %s already exists.", i->name); | |
1216 | i->gid_set = true; | |
1217 | return 0; | |
1b992147 LP |
1218 | } |
1219 | ||
1220 | /* Try to use the suggested numeric gid */ | |
1221 | if (i->gid_set) { | |
1222 | r = gid_is_ok(i->gid); | |
f647962d MS |
1223 | if (r < 0) |
1224 | return log_error_errno(r, "Failed to verify gid " GID_FMT ": %m", i->gid); | |
b9ee05c2 | 1225 | if (i->id_set_strict) { |
28e7fad7 MV |
1226 | /* If we require the gid to already exist we can return here: |
1227 | * r > 0: means the gid does not exist -> fail | |
1228 | * r == 0: means the gid exists -> nothing more to do. | |
1229 | */ | |
baaa35ad ZJS |
1230 | if (r > 0) |
1231 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1232 | "Failed to create %s: please create GID %d", | |
1233 | i->name, i->gid); | |
28e7fad7 MV |
1234 | if (r == 0) |
1235 | return 0; | |
1236 | } | |
1b992147 LP |
1237 | if (r == 0) { |
1238 | log_debug("Suggested group ID " GID_FMT " for %s already used.", i->gid, i->name); | |
1239 | i->gid_set = false; | |
1240 | } | |
1241 | } | |
1242 | ||
1243 | /* Try to reuse the numeric uid, if there's one */ | |
1244 | if (!i->gid_set && i->uid_set) { | |
1245 | r = gid_is_ok((gid_t) i->uid); | |
f647962d MS |
1246 | if (r < 0) |
1247 | return log_error_errno(r, "Failed to verify gid " GID_FMT ": %m", i->gid); | |
1b992147 LP |
1248 | if (r > 0) { |
1249 | i->gid = (gid_t) i->uid; | |
1250 | i->gid_set = true; | |
1251 | } | |
1252 | } | |
1253 | ||
1254 | /* If that didn't work, try to read it from the specified path */ | |
1255 | if (!i->gid_set) { | |
1256 | gid_t c; | |
1257 | ||
1258 | if (read_id_from_file(i, NULL, &c) > 0) { | |
1259 | ||
8530dc44 | 1260 | if (c <= 0 || !uid_range_contains(uid_range, n_uid_range, c)) |
1b992147 LP |
1261 | log_debug("Group ID " GID_FMT " of file not suitable for %s.", c, i->name); |
1262 | else { | |
1263 | r = gid_is_ok(c); | |
f647962d MS |
1264 | if (r < 0) |
1265 | return log_error_errno(r, "Failed to verify gid " GID_FMT ": %m", i->gid); | |
1266 | else if (r > 0) { | |
1b992147 LP |
1267 | i->gid = c; |
1268 | i->gid_set = true; | |
1269 | } else | |
1270 | log_debug("Group ID " GID_FMT " of file for %s already used.", c, i->name); | |
1271 | } | |
1272 | } | |
1273 | } | |
1274 | ||
1275 | /* And if that didn't work either, let's try to find a free one */ | |
1276 | if (!i->gid_set) { | |
4b6f9b20 ZJS |
1277 | maybe_emit_login_defs_warning(); |
1278 | ||
8530dc44 LP |
1279 | for (;;) { |
1280 | /* We look for new GIDs in the UID pool! */ | |
1281 | r = uid_range_next_lower(uid_range, n_uid_range, &search_uid); | |
330d1def ZJS |
1282 | if (r < 0) |
1283 | return log_error_errno(r, "No free group ID available for %s.", i->name); | |
8530dc44 LP |
1284 | |
1285 | r = gid_is_ok(search_uid); | |
f647962d MS |
1286 | if (r < 0) |
1287 | return log_error_errno(r, "Failed to verify gid " GID_FMT ": %m", i->gid); | |
1288 | else if (r > 0) | |
1b992147 LP |
1289 | break; |
1290 | } | |
1291 | ||
1b992147 | 1292 | i->gid_set = true; |
8530dc44 | 1293 | i->gid = search_uid; |
1b992147 LP |
1294 | } |
1295 | ||
1dfb9321 SS |
1296 | r = ordered_hashmap_ensure_put(&todo_gids, NULL, GID_TO_PTR(i->gid), i); |
1297 | if (r == -EEXIST) | |
1298 | return log_error_errno(r, "Requested group %s with gid "GID_FMT " to be created is duplicated or conflicts with another user.", i->name, i->gid); | |
1299 | if (r == -ENOMEM) | |
1b992147 | 1300 | return log_oom(); |
1b992147 | 1301 | if (r < 0) |
1dfb9321 | 1302 | return log_error_errno(r, "Failed to store group %s with gid " GID_FMT " to be created: %m", i->name, i->gid); |
1b992147 | 1303 | |
c1b6b04f | 1304 | i->todo_group = true; |
1b992147 LP |
1305 | log_info("Creating group %s with gid " GID_FMT ".", i->name, i->gid); |
1306 | ||
1307 | return 0; | |
1308 | } | |
1309 | ||
1310 | static int process_item(Item *i) { | |
1311 | int r; | |
1312 | ||
1313 | assert(i); | |
1314 | ||
1315 | switch (i->type) { | |
1316 | ||
b5327d0a YW |
1317 | case ADD_USER: { |
1318 | Item *j; | |
1319 | ||
649916d3 | 1320 | j = ordered_hashmap_get(groups, i->group_name ?: i->name); |
b5327d0a | 1321 | if (j && j->todo_group) { |
649916d3 | 1322 | /* When a group with the target name is already in queue, |
b5327d0a YW |
1323 | * use the information about the group and do not create |
1324 | * duplicated group entry. */ | |
1325 | i->gid_set = j->gid_set; | |
1326 | i->gid = j->gid; | |
1327 | i->id_set_strict = true; | |
649916d3 DM |
1328 | } else if (i->group_name) { |
1329 | /* When a group name was given instead of a GID and it's | |
1330 | * not in queue, then it must already exist. */ | |
1331 | r = get_gid_by_name(i->group_name, &i->gid); | |
1332 | if (r < 0) | |
1333 | return log_error_errno(r, "Group %s not found.", i->group_name); | |
1334 | i->gid_set = true; | |
1335 | i->id_set_strict = true; | |
b5327d0a YW |
1336 | } else { |
1337 | r = add_group(i); | |
1338 | if (r < 0) | |
1339 | return r; | |
1340 | } | |
1b992147 LP |
1341 | |
1342 | return add_user(i); | |
b5327d0a | 1343 | } |
1b992147 | 1344 | |
e2c2060f | 1345 | case ADD_GROUP: |
1b992147 | 1346 | return add_group(i); |
1b992147 | 1347 | |
a12b0cc3 | 1348 | default: |
04499a70 | 1349 | assert_not_reached(); |
a12b0cc3 | 1350 | } |
1b992147 LP |
1351 | } |
1352 | ||
71da1673 | 1353 | static Item* item_free(Item *i) { |
1b992147 | 1354 | if (!i) |
71da1673 | 1355 | return NULL; |
1b992147 LP |
1356 | |
1357 | free(i->name); | |
649916d3 | 1358 | free(i->group_name); |
1b992147 LP |
1359 | free(i->uid_path); |
1360 | free(i->gid_path); | |
1361 | free(i->description); | |
d9b8ea54 | 1362 | free(i->home); |
7b1aaf66 | 1363 | free(i->shell); |
71da1673 | 1364 | return mfree(i); |
1b992147 LP |
1365 | } |
1366 | ||
1367 | DEFINE_TRIVIAL_CLEANUP_FUNC(Item*, item_free); | |
71da1673 | 1368 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(item_hash_ops, char, string_hash_func, string_compare_func, Item, item_free); |
1b992147 | 1369 | |
a12b0cc3 LP |
1370 | static int add_implicit(void) { |
1371 | char *g, **l; | |
a12b0cc3 LP |
1372 | int r; |
1373 | ||
1374 | /* Implicitly create additional users and groups, if they were listed in "m" lines */ | |
90e74a66 | 1375 | ORDERED_HASHMAP_FOREACH_KEY(l, g, members) { |
a12b0cc3 LP |
1376 | char **m; |
1377 | ||
d4f0412d YW |
1378 | STRV_FOREACH(m, l) |
1379 | if (!ordered_hashmap_get(users, *m)) { | |
a12b0cc3 LP |
1380 | _cleanup_(item_freep) Item *j = NULL; |
1381 | ||
a12b0cc3 LP |
1382 | j = new0(Item, 1); |
1383 | if (!j) | |
1384 | return log_oom(); | |
1385 | ||
1386 | j->type = ADD_USER; | |
1387 | j->name = strdup(*m); | |
1388 | if (!j->name) | |
1389 | return log_oom(); | |
1390 | ||
f334deba SS |
1391 | r = ordered_hashmap_ensure_put(&users, &item_hash_ops, j->name, j); |
1392 | if (r == -ENOMEM) | |
a12b0cc3 | 1393 | return log_oom(); |
f334deba SS |
1394 | if (r < 0) |
1395 | return log_error_errno(r, "Failed to add implicit user '%s': %m", j->name); | |
a12b0cc3 LP |
1396 | |
1397 | log_debug("Adding implicit user '%s' due to m line", j->name); | |
f334deba | 1398 | TAKE_PTR(j); |
a12b0cc3 | 1399 | } |
d4f0412d YW |
1400 | |
1401 | if (!(ordered_hashmap_get(users, g) || | |
1402 | ordered_hashmap_get(groups, g))) { | |
1403 | _cleanup_(item_freep) Item *j = NULL; | |
1404 | ||
d4f0412d YW |
1405 | j = new0(Item, 1); |
1406 | if (!j) | |
1407 | return log_oom(); | |
1408 | ||
1409 | j->type = ADD_GROUP; | |
1410 | j->name = strdup(g); | |
1411 | if (!j->name) | |
1412 | return log_oom(); | |
1413 | ||
f334deba SS |
1414 | r = ordered_hashmap_ensure_put(&groups, &item_hash_ops, j->name, j); |
1415 | if (r == -ENOMEM) | |
d4f0412d | 1416 | return log_oom(); |
f334deba SS |
1417 | if (r < 0) |
1418 | return log_error_errno(r, "Failed to add implicit group '%s': %m", j->name); | |
d4f0412d YW |
1419 | |
1420 | log_debug("Adding implicit group '%s' due to m line", j->name); | |
f334deba | 1421 | TAKE_PTR(j); |
a12b0cc3 LP |
1422 | } |
1423 | } | |
1424 | ||
1425 | return 0; | |
1426 | } | |
1427 | ||
1b992147 LP |
1428 | static bool item_equal(Item *a, Item *b) { |
1429 | assert(a); | |
1430 | assert(b); | |
1431 | ||
1432 | if (a->type != b->type) | |
1433 | return false; | |
1434 | ||
1435 | if (!streq_ptr(a->name, b->name)) | |
1436 | return false; | |
1437 | ||
1438 | if (!streq_ptr(a->uid_path, b->uid_path)) | |
1439 | return false; | |
1440 | ||
1441 | if (!streq_ptr(a->gid_path, b->gid_path)) | |
1442 | return false; | |
1443 | ||
1444 | if (!streq_ptr(a->description, b->description)) | |
1445 | return false; | |
1446 | ||
1447 | if (a->uid_set != b->uid_set) | |
1448 | return false; | |
1449 | ||
1450 | if (a->uid_set && a->uid != b->uid) | |
1451 | return false; | |
1452 | ||
1453 | if (a->gid_set != b->gid_set) | |
1454 | return false; | |
1455 | ||
1456 | if (a->gid_set && a->gid != b->gid) | |
1457 | return false; | |
1458 | ||
7629889c LP |
1459 | if (!streq_ptr(a->home, b->home)) |
1460 | return false; | |
1461 | ||
7b1aaf66 ZJS |
1462 | if (!streq_ptr(a->shell, b->shell)) |
1463 | return false; | |
1464 | ||
1b992147 LP |
1465 | return true; |
1466 | } | |
1467 | ||
1b992147 LP |
1468 | static int parse_line(const char *fname, unsigned line, const char *buffer) { |
1469 | ||
7b1aaf66 ZJS |
1470 | _cleanup_free_ char *action = NULL, |
1471 | *name = NULL, *resolved_name = NULL, | |
1472 | *id = NULL, *resolved_id = NULL, | |
b8bed700 YW |
1473 | *description = NULL, *resolved_description = NULL, |
1474 | *home = NULL, *resolved_home = NULL, | |
71fb1588 | 1475 | *shell = NULL, *resolved_shell = NULL; |
1b992147 LP |
1476 | _cleanup_(item_freep) Item *i = NULL; |
1477 | Item *existing; | |
5bc9c980 | 1478 | OrderedHashmap *h; |
7629889c LP |
1479 | int r; |
1480 | const char *p; | |
1b992147 LP |
1481 | |
1482 | assert(fname); | |
1483 | assert(line >= 1); | |
1484 | assert(buffer); | |
1485 | ||
7629889c LP |
1486 | /* Parse columns */ |
1487 | p = buffer; | |
4ec85141 | 1488 | r = extract_many_words(&p, NULL, EXTRACT_UNQUOTE, |
7b1aaf66 | 1489 | &action, &name, &id, &description, &home, &shell, NULL); |
330d1def ZJS |
1490 | if (r < 0) |
1491 | return log_error_errno(r, "[%s:%u] Syntax error.", fname, line); | |
1492 | if (r < 2) | |
1493 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1494 | "[%s:%u] Missing action and name columns.", fname, line); | |
1495 | if (!isempty(p)) | |
1496 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1497 | "[%s:%u] Trailing garbage.", fname, line); | |
1b992147 | 1498 | |
7629889c | 1499 | /* Verify action */ |
330d1def ZJS |
1500 | if (strlen(action) != 1) |
1501 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1502 | "[%s:%u] Unknown modifier '%s'", fname, line, action); | |
1b992147 | 1503 | |
330d1def ZJS |
1504 | if (!IN_SET(action[0], ADD_USER, ADD_GROUP, ADD_MEMBER, ADD_RANGE)) |
1505 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), | |
1506 | "[%s:%u] Unknown command type '%c'.", fname, line, action[0]); | |
1b992147 | 1507 | |
7629889c | 1508 | /* Verify name */ |
e7b88b7b | 1509 | if (empty_or_dash(name)) |
97b11eed | 1510 | name = mfree(name); |
1b992147 | 1511 | |
8530dc44 | 1512 | if (name) { |
de61a04b | 1513 | r = specifier_printf(name, NAME_MAX, system_and_tmp_specifier_table, arg_root, NULL, &resolved_name); |
330d1def | 1514 | if (r < 0) |
0f7e4b28 | 1515 | return log_error_errno(r, "[%s:%u] Failed to replace specifiers in '%s': %m", fname, line, name); |
8530dc44 | 1516 | |
7a8867ab | 1517 | if (!valid_user_group_name(resolved_name, 0)) |
330d1def ZJS |
1518 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
1519 | "[%s:%u] '%s' is not a valid user or group name.", | |
1520 | fname, line, resolved_name); | |
1b992147 LP |
1521 | } |
1522 | ||
8530dc44 | 1523 | /* Verify id */ |
e7b88b7b | 1524 | if (empty_or_dash(id)) |
97b11eed | 1525 | id = mfree(id); |
7629889c | 1526 | |
8530dc44 | 1527 | if (id) { |
de61a04b | 1528 | r = specifier_printf(id, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_id); |
330d1def | 1529 | if (r < 0) |
0f7e4b28 | 1530 | return log_error_errno(r, "[%s:%u] Failed to replace specifiers in '%s': %m", |
330d1def | 1531 | fname, line, name); |
8530dc44 LP |
1532 | } |
1533 | ||
1534 | /* Verify description */ | |
e7b88b7b | 1535 | if (empty_or_dash(description)) |
97b11eed | 1536 | description = mfree(description); |
1b992147 | 1537 | |
8530dc44 | 1538 | if (description) { |
de61a04b | 1539 | r = specifier_printf(description, LONG_LINE_MAX, system_and_tmp_specifier_table, arg_root, NULL, &resolved_description); |
330d1def | 1540 | if (r < 0) |
0f7e4b28 | 1541 | return log_error_errno(r, "[%s:%u] Failed to replace specifiers in '%s': %m", |
330d1def | 1542 | fname, line, description); |
b8bed700 | 1543 | |
330d1def ZJS |
1544 | if (!valid_gecos(resolved_description)) |
1545 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1546 | "[%s:%u] '%s' is not a valid GECOS field.", | |
1547 | fname, line, resolved_description); | |
8530dc44 LP |
1548 | } |
1549 | ||
1550 | /* Verify home */ | |
e7b88b7b | 1551 | if (empty_or_dash(home)) |
97b11eed | 1552 | home = mfree(home); |
1b992147 | 1553 | |
8530dc44 | 1554 | if (home) { |
de61a04b | 1555 | r = specifier_printf(home, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_home); |
330d1def | 1556 | if (r < 0) |
0f7e4b28 | 1557 | return log_error_errno(r, "[%s:%u] Failed to replace specifiers in '%s': %m", |
330d1def | 1558 | fname, line, home); |
b8bed700 | 1559 | |
330d1def ZJS |
1560 | if (!valid_home(resolved_home)) |
1561 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1562 | "[%s:%u] '%s' is not a valid home directory field.", | |
1563 | fname, line, resolved_home); | |
8530dc44 LP |
1564 | } |
1565 | ||
7b1aaf66 | 1566 | /* Verify shell */ |
e7b88b7b | 1567 | if (empty_or_dash(shell)) |
7b1aaf66 ZJS |
1568 | shell = mfree(shell); |
1569 | ||
1570 | if (shell) { | |
de61a04b | 1571 | r = specifier_printf(shell, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_shell); |
330d1def | 1572 | if (r < 0) |
0f7e4b28 | 1573 | return log_error_errno(r, "[%s:%u] Failed to replace specifiers in '%s': %m", |
330d1def | 1574 | fname, line, shell); |
7b1aaf66 | 1575 | |
330d1def ZJS |
1576 | if (!valid_shell(resolved_shell)) |
1577 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1578 | "[%s:%u] '%s' is not a valid login shell field.", | |
1579 | fname, line, resolved_shell); | |
7b1aaf66 ZJS |
1580 | } |
1581 | ||
a12b0cc3 | 1582 | switch (action[0]) { |
1b992147 | 1583 | |
8530dc44 | 1584 | case ADD_RANGE: |
330d1def ZJS |
1585 | if (resolved_name) |
1586 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1587 | "[%s:%u] Lines of type 'r' don't take a name field.", | |
1588 | fname, line); | |
1589 | ||
1590 | if (!resolved_id) | |
1591 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
387f6955 | 1592 | "[%s:%u] Lines of type 'r' require an ID range in the third field.", |
330d1def ZJS |
1593 | fname, line); |
1594 | ||
1595 | if (description || home || shell) | |
1596 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1597 | "[%s:%u] Lines of type '%c' don't take a %s field.", | |
1598 | fname, line, action[0], | |
1599 | description ? "GECOS" : home ? "home directory" : "login shell"); | |
7629889c | 1600 | |
8530dc44 | 1601 | r = uid_range_add_str(&uid_range, &n_uid_range, resolved_id); |
330d1def ZJS |
1602 | if (r < 0) |
1603 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1604 | "[%s:%u] Invalid UID range %s.", fname, line, resolved_id); | |
1b992147 | 1605 | |
8530dc44 LP |
1606 | return 0; |
1607 | ||
1608 | case ADD_MEMBER: { | |
8530dc44 | 1609 | /* Try to extend an existing member or group item */ |
330d1def ZJS |
1610 | if (!name) |
1611 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1612 | "[%s:%u] Lines of type 'm' require a user name in the second field.", | |
1613 | fname, line); | |
1614 | ||
1615 | if (!resolved_id) | |
1616 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1617 | "[%s:%u] Lines of type 'm' require a group name in the third field.", | |
1618 | fname, line); | |
1619 | ||
7a8867ab | 1620 | if (!valid_user_group_name(resolved_id, 0)) |
330d1def ZJS |
1621 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
1622 | "[%s:%u] '%s' is not a valid user or group name.", | |
1623 | fname, line, resolved_id); | |
1624 | ||
1625 | if (description || home || shell) | |
1626 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1627 | "[%s:%u] Lines of type '%c' don't take a %s field.", | |
1628 | fname, line, action[0], | |
1629 | description ? "GECOS" : home ? "home directory" : "login shell"); | |
8530dc44 | 1630 | |
cde79109 | 1631 | r = string_strv_ordered_hashmap_put(&members, resolved_id, resolved_name); |
7629889c | 1632 | if (r < 0) |
cde79109 | 1633 | return log_error_errno(r, "Failed to store mapping for %s: %m", resolved_id); |
a12b0cc3 LP |
1634 | |
1635 | return 0; | |
1b992147 LP |
1636 | } |
1637 | ||
a12b0cc3 | 1638 | case ADD_USER: |
330d1def ZJS |
1639 | if (!name) |
1640 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1641 | "[%s:%u] Lines of type 'u' require a user name in the second field.", | |
1642 | fname, line); | |
8530dc44 | 1643 | |
71da1673 | 1644 | r = ordered_hashmap_ensure_allocated(&users, &item_hash_ops); |
a12b0cc3 LP |
1645 | if (r < 0) |
1646 | return log_oom(); | |
1647 | ||
1648 | i = new0(Item, 1); | |
1649 | if (!i) | |
1650 | return log_oom(); | |
1651 | ||
8530dc44 LP |
1652 | if (resolved_id) { |
1653 | if (path_is_absolute(resolved_id)) { | |
1cc6c93a | 1654 | i->uid_path = TAKE_PTR(resolved_id); |
4ff361cc | 1655 | path_simplify(i->uid_path); |
a12b0cc3 | 1656 | } else { |
4cb41413 MV |
1657 | _cleanup_free_ char *uid = NULL, *gid = NULL; |
1658 | if (split_pair(resolved_id, ":", &uid, &gid) == 0) { | |
1659 | r = parse_gid(gid, &i->gid); | |
649916d3 | 1660 | if (r < 0) { |
7a8867ab | 1661 | if (valid_user_group_name(gid, 0)) |
649916d3 DM |
1662 | i->group_name = TAKE_PTR(gid); |
1663 | else | |
1664 | return log_error_errno(r, "Failed to parse GID: '%s': %m", id); | |
1665 | } else { | |
1666 | i->gid_set = true; | |
1667 | i->id_set_strict = true; | |
1668 | } | |
4cb41413 | 1669 | free_and_replace(resolved_id, uid); |
a12b0cc3 | 1670 | } |
1825c909 MV |
1671 | if (!streq(resolved_id, "-")) { |
1672 | r = parse_uid(resolved_id, &i->uid); | |
1673 | if (r < 0) | |
1674 | return log_error_errno(r, "Failed to parse UID: '%s': %m", id); | |
1675 | i->uid_set = true; | |
1676 | } | |
a12b0cc3 LP |
1677 | } |
1678 | } | |
1679 | ||
b8bed700 YW |
1680 | i->description = TAKE_PTR(resolved_description); |
1681 | i->home = TAKE_PTR(resolved_home); | |
1cc6c93a | 1682 | i->shell = TAKE_PTR(resolved_shell); |
7b1aaf66 | 1683 | |
1b992147 | 1684 | h = users; |
a12b0cc3 LP |
1685 | break; |
1686 | ||
1687 | case ADD_GROUP: | |
330d1def ZJS |
1688 | if (!name) |
1689 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1690 | "[%s:%u] Lines of type 'g' require a user name in the second field.", | |
1691 | fname, line); | |
a12b0cc3 | 1692 | |
330d1def ZJS |
1693 | if (description || home || shell) |
1694 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1695 | "[%s:%u] Lines of type '%c' don't take a %s field.", | |
1696 | fname, line, action[0], | |
1697 | description ? "GECOS" : home ? "home directory" : "login shell"); | |
7629889c | 1698 | |
71da1673 | 1699 | r = ordered_hashmap_ensure_allocated(&groups, &item_hash_ops); |
7629889c LP |
1700 | if (r < 0) |
1701 | return log_oom(); | |
1702 | ||
a12b0cc3 LP |
1703 | i = new0(Item, 1); |
1704 | if (!i) | |
1705 | return log_oom(); | |
1706 | ||
8530dc44 LP |
1707 | if (resolved_id) { |
1708 | if (path_is_absolute(resolved_id)) { | |
1cc6c93a | 1709 | i->gid_path = TAKE_PTR(resolved_id); |
4ff361cc | 1710 | path_simplify(i->gid_path); |
a12b0cc3 | 1711 | } else { |
8530dc44 | 1712 | r = parse_gid(resolved_id, &i->gid); |
4cb41413 MV |
1713 | if (r < 0) |
1714 | return log_error_errno(r, "Failed to parse GID: '%s': %m", id); | |
a12b0cc3 LP |
1715 | |
1716 | i->gid_set = true; | |
1717 | } | |
1718 | } | |
1719 | ||
1b992147 | 1720 | h = groups; |
a12b0cc3 | 1721 | break; |
8530dc44 | 1722 | |
bce415ed RC |
1723 | default: |
1724 | return -EBADMSG; | |
1b992147 | 1725 | } |
a12b0cc3 LP |
1726 | |
1727 | i->type = action[0]; | |
1cc6c93a | 1728 | i->name = TAKE_PTR(resolved_name); |
1b992147 | 1729 | |
5bc9c980 | 1730 | existing = ordered_hashmap_get(h, i->name); |
1b992147 | 1731 | if (existing) { |
1b992147 LP |
1732 | /* Two identical items are fine */ |
1733 | if (!item_equal(existing, i)) | |
1734 | log_warning("Two or more conflicting lines for %s configured, ignoring.", i->name); | |
1735 | ||
1736 | return 0; | |
1737 | } | |
1738 | ||
5bc9c980 | 1739 | r = ordered_hashmap_put(h, i->name, i); |
a12b0cc3 LP |
1740 | if (r < 0) |
1741 | return log_oom(); | |
1b992147 LP |
1742 | |
1743 | i = NULL; | |
1744 | return 0; | |
1745 | } | |
1746 | ||
1747 | static int read_config_file(const char *fn, bool ignore_enoent) { | |
dfc87cbf | 1748 | _cleanup_fclose_ FILE *rf = NULL; |
2708160c | 1749 | _cleanup_free_ char *pp = NULL; |
dfc87cbf | 1750 | FILE *f = NULL; |
1b992147 | 1751 | unsigned v = 0; |
c4640902 | 1752 | int r = 0; |
1b992147 LP |
1753 | |
1754 | assert(fn); | |
1755 | ||
dfc87cbf LP |
1756 | if (streq(fn, "-")) |
1757 | f = stdin; | |
1758 | else { | |
2708160c | 1759 | r = search_and_fopen(fn, "re", arg_root, (const char**) CONF_PATHS_STRV("sysusers.d"), &rf, &pp); |
dfc87cbf LP |
1760 | if (r < 0) { |
1761 | if (ignore_enoent && r == -ENOENT) | |
1762 | return 0; | |
1b992147 | 1763 | |
8d3d7072 | 1764 | return log_error_errno(r, "Failed to open '%s', ignoring: %m", fn); |
dfc87cbf LP |
1765 | } |
1766 | ||
1767 | f = rf; | |
2708160c | 1768 | fn = pp; |
1b992147 LP |
1769 | } |
1770 | ||
050ca299 LP |
1771 | for (;;) { |
1772 | _cleanup_free_ char *line = NULL; | |
1b992147 LP |
1773 | char *l; |
1774 | int k; | |
1775 | ||
050ca299 LP |
1776 | k = read_line(f, LONG_LINE_MAX, &line); |
1777 | if (k < 0) | |
1778 | return log_error_errno(k, "Failed to read '%s': %m", fn); | |
1779 | if (k == 0) | |
1780 | break; | |
1781 | ||
1b992147 LP |
1782 | v++; |
1783 | ||
1784 | l = strstrip(line); | |
4c701096 | 1785 | if (IN_SET(*l, 0, '#')) |
1b992147 LP |
1786 | continue; |
1787 | ||
1788 | k = parse_line(fn, v, l); | |
1789 | if (k < 0 && r == 0) | |
1790 | r = k; | |
1791 | } | |
1792 | ||
1793 | if (ferror(f)) { | |
56f64d95 | 1794 | log_error_errno(errno, "Failed to read from file %s: %m", fn); |
1b992147 LP |
1795 | if (r == 0) |
1796 | r = -EIO; | |
1797 | } | |
1798 | ||
1799 | return r; | |
1800 | } | |
1801 | ||
ec0327d6 ZJS |
1802 | static int cat_config(void) { |
1803 | _cleanup_strv_free_ char **files = NULL; | |
ec0327d6 ZJS |
1804 | int r; |
1805 | ||
a826d4f7 | 1806 | r = conf_files_list_with_replacement(arg_root, CONF_PATHS_STRV("sysusers.d"), arg_replace, &files, NULL); |
ec0327d6 ZJS |
1807 | if (r < 0) |
1808 | return r; | |
1809 | ||
0221d68a | 1810 | (void) pager_open(arg_pager_flags); |
dcd5c891 | 1811 | |
ec0327d6 ZJS |
1812 | return cat_files(NULL, files, 0); |
1813 | } | |
1814 | ||
37ec0fdd LP |
1815 | static int help(void) { |
1816 | _cleanup_free_ char *link = NULL; | |
1817 | int r; | |
1818 | ||
1819 | r = terminal_urlify_man("systemd-sysusers.service", "8", &link); | |
1820 | if (r < 0) | |
1821 | return log_oom(); | |
1822 | ||
1b992147 LP |
1823 | printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n" |
1824 | "Creates system user accounts.\n\n" | |
1825 | " -h --help Show this help\n" | |
1826 | " --version Show package version\n" | |
ec0327d6 | 1827 | " --cat-config Show configuration files\n" |
601185b4 | 1828 | " --root=PATH Operate on an alternate filesystem root\n" |
0c3ee127 | 1829 | " --image=PATH Operate on disk image as filesystem root\n" |
d16a1c1b | 1830 | " --replace=PATH Treat arguments as replacement for PATH\n" |
1b600bd5 | 1831 | " --inline Treat arguments as configuration lines\n" |
dcd5c891 | 1832 | " --no-pager Do not pipe output into a pager\n" |
bc556335 DDM |
1833 | "\nSee the %s for details.\n", |
1834 | program_invocation_short_name, | |
1835 | link); | |
37ec0fdd LP |
1836 | |
1837 | return 0; | |
1b992147 LP |
1838 | } |
1839 | ||
1840 | static int parse_argv(int argc, char *argv[]) { | |
1841 | ||
1842 | enum { | |
1843 | ARG_VERSION = 0x100, | |
ec0327d6 | 1844 | ARG_CAT_CONFIG, |
1b992147 | 1845 | ARG_ROOT, |
0c3ee127 | 1846 | ARG_IMAGE, |
d16a1c1b | 1847 | ARG_REPLACE, |
1b600bd5 | 1848 | ARG_INLINE, |
dcd5c891 | 1849 | ARG_NO_PAGER, |
1b992147 LP |
1850 | }; |
1851 | ||
1852 | static const struct option options[] = { | |
ec0327d6 ZJS |
1853 | { "help", no_argument, NULL, 'h' }, |
1854 | { "version", no_argument, NULL, ARG_VERSION }, | |
1855 | { "cat-config", no_argument, NULL, ARG_CAT_CONFIG }, | |
1856 | { "root", required_argument, NULL, ARG_ROOT }, | |
0c3ee127 | 1857 | { "image", required_argument, NULL, ARG_IMAGE }, |
ec0327d6 ZJS |
1858 | { "replace", required_argument, NULL, ARG_REPLACE }, |
1859 | { "inline", no_argument, NULL, ARG_INLINE }, | |
dcd5c891 | 1860 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, |
1b992147 LP |
1861 | {} |
1862 | }; | |
1863 | ||
0f474365 | 1864 | int c, r; |
1b992147 LP |
1865 | |
1866 | assert(argc >= 0); | |
1867 | assert(argv); | |
1868 | ||
601185b4 | 1869 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
1b992147 LP |
1870 | |
1871 | switch (c) { | |
1872 | ||
1873 | case 'h': | |
37ec0fdd | 1874 | return help(); |
1b992147 LP |
1875 | |
1876 | case ARG_VERSION: | |
3f6fd1ba | 1877 | return version(); |
1b992147 | 1878 | |
ec0327d6 ZJS |
1879 | case ARG_CAT_CONFIG: |
1880 | arg_cat_config = true; | |
1881 | break; | |
1882 | ||
1b992147 | 1883 | case ARG_ROOT: |
614b022c | 1884 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root); |
0f474365 | 1885 | if (r < 0) |
0f03c2a4 | 1886 | return r; |
1b992147 LP |
1887 | break; |
1888 | ||
0c3ee127 | 1889 | case ARG_IMAGE: |
3537577c ZJS |
1890 | #ifdef STANDALONE |
1891 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
1892 | "This systemd-sysusers version is compiled without support for --image=."); | |
1893 | #else | |
614b022c | 1894 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image); |
0c3ee127 LP |
1895 | if (r < 0) |
1896 | return r; | |
1897 | break; | |
3537577c | 1898 | #endif |
0c3ee127 | 1899 | |
d16a1c1b ZJS |
1900 | case ARG_REPLACE: |
1901 | if (!path_is_absolute(optarg) || | |
baaa35ad ZJS |
1902 | !endswith(optarg, ".conf")) |
1903 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1904 | "The argument to --replace= must an absolute path to a config file"); | |
d16a1c1b ZJS |
1905 | |
1906 | arg_replace = optarg; | |
1907 | break; | |
1908 | ||
1b600bd5 ZJS |
1909 | case ARG_INLINE: |
1910 | arg_inline = true; | |
1911 | break; | |
1912 | ||
dcd5c891 | 1913 | case ARG_NO_PAGER: |
0221d68a | 1914 | arg_pager_flags |= PAGER_DISABLE; |
dcd5c891 LP |
1915 | break; |
1916 | ||
1b992147 LP |
1917 | case '?': |
1918 | return -EINVAL; | |
1919 | ||
1920 | default: | |
04499a70 | 1921 | assert_not_reached(); |
1b992147 | 1922 | } |
1b992147 | 1923 | |
baaa35ad ZJS |
1924 | if (arg_replace && arg_cat_config) |
1925 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1926 | "Option --replace= is not supported with --cat-config"); | |
ec0327d6 | 1927 | |
baaa35ad ZJS |
1928 | if (arg_replace && optind >= argc) |
1929 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1930 | "When --replace= is given, some configuration items must be specified"); | |
d16a1c1b | 1931 | |
0c3ee127 LP |
1932 | if (arg_image && arg_root) |
1933 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported."); | |
1934 | ||
1b992147 LP |
1935 | return 1; |
1936 | } | |
1937 | ||
d16a1c1b ZJS |
1938 | static int parse_arguments(char **args) { |
1939 | char **arg; | |
1940 | unsigned pos = 1; | |
1941 | int r; | |
1942 | ||
1943 | STRV_FOREACH(arg, args) { | |
1944 | if (arg_inline) | |
1945 | /* Use (argument):n, where n==1 for the first positional arg */ | |
1946 | r = parse_line("(argument)", pos, *arg); | |
1947 | else | |
1948 | r = read_config_file(*arg, false); | |
1949 | if (r < 0) | |
1950 | return r; | |
1951 | ||
1952 | pos++; | |
1953 | } | |
1954 | ||
1955 | return 0; | |
1956 | } | |
1957 | ||
ec0327d6 | 1958 | static int read_config_files(char **args) { |
d16a1c1b ZJS |
1959 | _cleanup_strv_free_ char **files = NULL; |
1960 | _cleanup_free_ char *p = NULL; | |
1961 | char **f; | |
1962 | int r; | |
1963 | ||
a826d4f7 | 1964 | r = conf_files_list_with_replacement(arg_root, CONF_PATHS_STRV("sysusers.d"), arg_replace, &files, &p); |
d16a1c1b | 1965 | if (r < 0) |
ec0327d6 | 1966 | return r; |
d16a1c1b ZJS |
1967 | |
1968 | STRV_FOREACH(f, files) | |
1969 | if (p && path_equal(*f, p)) { | |
1970 | log_debug("Parsing arguments at position \"%s\"…", *f); | |
1971 | ||
1972 | r = parse_arguments(args); | |
1973 | if (r < 0) | |
1974 | return r; | |
1975 | } else { | |
1976 | log_debug("Reading config file \"%s\"…", *f); | |
1977 | ||
1978 | /* Just warn, ignore result otherwise */ | |
1979 | (void) read_config_file(*f, true); | |
1980 | } | |
1981 | ||
1982 | return 0; | |
1983 | } | |
1984 | ||
71da1673 | 1985 | static int run(int argc, char *argv[]) { |
3537577c | 1986 | #ifndef STANDALONE |
0c3ee127 LP |
1987 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
1988 | _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL; | |
1989 | _cleanup_(umount_and_rmdir_and_freep) char *unlink_dir = NULL; | |
3537577c | 1990 | #endif |
1b992147 | 1991 | _cleanup_close_ int lock = -1; |
1b992147 | 1992 | Item *i; |
dd2fd972 | 1993 | int r; |
1b992147 LP |
1994 | |
1995 | r = parse_argv(argc, argv); | |
1996 | if (r <= 0) | |
71da1673 | 1997 | return r; |
1b992147 | 1998 | |
d2acb93d | 1999 | log_setup(); |
1b992147 | 2000 | |
71da1673 YW |
2001 | if (arg_cat_config) |
2002 | return cat_config(); | |
ec0327d6 | 2003 | |
1b992147 LP |
2004 | umask(0022); |
2005 | ||
c3dacc8b | 2006 | r = mac_selinux_init(); |
71da1673 | 2007 | if (r < 0) |
a9ba0e32 | 2008 | return r; |
1b992147 | 2009 | |
3537577c | 2010 | #ifndef STANDALONE |
0c3ee127 LP |
2011 | if (arg_image) { |
2012 | assert(!arg_root); | |
2013 | ||
2014 | r = mount_image_privately_interactively( | |
2015 | arg_image, | |
4b5de5dd LP |
2016 | DISSECT_IMAGE_GENERIC_ROOT | |
2017 | DISSECT_IMAGE_REQUIRE_ROOT | | |
2018 | DISSECT_IMAGE_VALIDATE_OS | | |
2019 | DISSECT_IMAGE_RELAX_VAR_CHECK | | |
c65f854a LP |
2020 | DISSECT_IMAGE_FSCK | |
2021 | DISSECT_IMAGE_GROWFS, | |
0c3ee127 LP |
2022 | &unlink_dir, |
2023 | &loop_device, | |
2024 | &decrypted_image); | |
2025 | if (r < 0) | |
2026 | return r; | |
2027 | ||
2028 | arg_root = strdup(unlink_dir); | |
2029 | if (!arg_root) | |
2030 | return log_oom(); | |
2031 | } | |
3537577c ZJS |
2032 | #else |
2033 | assert(!arg_image); | |
2034 | #endif | |
0c3ee127 | 2035 | |
d16a1c1b ZJS |
2036 | /* If command line arguments are specified along with --replace, read all |
2037 | * configuration files and insert the positional arguments at the specified | |
2038 | * place. Otherwise, if command line arguments are specified, execute just | |
2039 | * them, and finally, without --replace= or any positional arguments, just | |
2040 | * read configuration and execute it. | |
2041 | */ | |
2042 | if (arg_replace || optind >= argc) | |
ec0327d6 | 2043 | r = read_config_files(argv + optind); |
d16a1c1b ZJS |
2044 | else |
2045 | r = parse_arguments(argv + optind); | |
2046 | if (r < 0) | |
71da1673 | 2047 | return r; |
1b992147 | 2048 | |
fe102d6a LP |
2049 | /* Let's tell nss-systemd not to synthesize the "root" and "nobody" entries for it, so that our detection |
2050 | * whether the names or UID/GID area already used otherwise doesn't get confused. After all, even though | |
2051 | * nss-systemd synthesizes these users/groups, they should still appear in /etc/passwd and /etc/group, as the | |
2052 | * synthesizing logic is merely supposed to be fallback for cases where we run with a completely unpopulated | |
2053 | * /etc. */ | |
71da1673 YW |
2054 | if (setenv("SYSTEMD_NSS_BYPASS_SYNTHETIC", "1", 1) < 0) |
2055 | return log_error_errno(errno, "Failed to set SYSTEMD_NSS_BYPASS_SYNTHETIC environment variable: %m"); | |
fe102d6a | 2056 | |
8530dc44 | 2057 | if (!uid_range) { |
aa25270c | 2058 | /* Default to default range of SYSTEMD_UID_MIN..SYSTEM_UID_MAX. */ |
4b6f9b20 | 2059 | r = read_login_defs(&login_defs, NULL, arg_root); |
71da1673 | 2060 | if (r < 0) |
aa25270c ZJS |
2061 | return log_error_errno(r, "Failed to read %s%s: %m", |
2062 | strempty(arg_root), "/etc/login.defs"); | |
2063 | ||
4b6f9b20 ZJS |
2064 | login_defs_need_warning = true; |
2065 | ||
aa25270c ZJS |
2066 | /* We pick a range that very conservative: we look at compiled-in maximum and the value in |
2067 | * /etc/login.defs. That way the uids/gids which we allocate will be interpreted correctly, | |
2068 | * even if /etc/login.defs is removed later. (The bottom bound doesn't matter much, since | |
2069 | * it's only used during allocation, so we use the configured value directly). */ | |
4b6f9b20 ZJS |
2070 | uid_t begin = login_defs.system_alloc_uid_min, |
2071 | end = MIN3((uid_t) SYSTEM_UID_MAX, login_defs.system_uid_max, login_defs.system_gid_max); | |
aa25270c ZJS |
2072 | if (begin < end) { |
2073 | r = uid_range_add(&uid_range, &n_uid_range, begin, end - begin + 1); | |
2074 | if (r < 0) | |
2075 | return log_oom(); | |
2076 | } | |
8530dc44 LP |
2077 | } |
2078 | ||
a12b0cc3 LP |
2079 | r = add_implicit(); |
2080 | if (r < 0) | |
71da1673 | 2081 | return r; |
a12b0cc3 | 2082 | |
e929bee0 | 2083 | lock = take_etc_passwd_lock(arg_root); |
71da1673 YW |
2084 | if (lock < 0) |
2085 | return log_error_errno(lock, "Failed to take /etc/passwd lock: %m"); | |
1b992147 LP |
2086 | |
2087 | r = load_user_database(); | |
71da1673 YW |
2088 | if (r < 0) |
2089 | return log_error_errno(r, "Failed to load user database: %m"); | |
1b992147 LP |
2090 | |
2091 | r = load_group_database(); | |
71da1673 YW |
2092 | if (r < 0) |
2093 | return log_error_errno(r, "Failed to read group database: %m"); | |
1b992147 | 2094 | |
90e74a66 | 2095 | ORDERED_HASHMAP_FOREACH(i, groups) |
c1a32819 | 2096 | (void) process_item(i); |
1b992147 | 2097 | |
90e74a66 | 2098 | ORDERED_HASHMAP_FOREACH(i, users) |
c1a32819 | 2099 | (void) process_item(i); |
1b992147 LP |
2100 | |
2101 | r = write_files(); | |
2102 | if (r < 0) | |
71da1673 | 2103 | return log_error_errno(r, "Failed to write files: %m"); |
1b992147 | 2104 | |
71da1673 | 2105 | return 0; |
1b992147 | 2106 | } |
71da1673 YW |
2107 | |
2108 | DEFINE_MAIN_FUNCTION(run); |