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