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