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