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