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