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