]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/firstboot/firstboot.c
user-util: add get{pw,gr}{uid,gid,name}_malloc() helpers
[thirdparty/systemd.git] / src / firstboot / firstboot.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
418b9be5 2
418b9be5 3#include <fcntl.h>
418b9be5 4#include <getopt.h>
3ff9fa59 5#include <linux/loop.h>
3f6fd1ba 6#include <unistd.h>
418b9be5 7
dccca82b
LP
8#include "sd-id128.h"
9
b5efdb8a 10#include "alloc-util.h"
3f6fd1ba 11#include "ask-password-api.h"
d6b4d1c7 12#include "build.h"
cea32691
ZJS
13#include "bus-error.h"
14#include "bus-locator.h"
a9399358 15#include "bus-unit-util.h"
cea32691 16#include "bus-util.h"
8eb668b9 17#include "bus-wait-for-jobs.h"
f461a28d 18#include "chase.h"
418b9be5 19#include "copy.h"
416f7b3a 20#include "creds-util.h"
3ff9fa59 21#include "dissect-image.h"
686d13b9 22#include "env-file.h"
7fc3f9c0 23#include "errno-util.h"
6bedfcbb 24#include "fd-util.h"
3f6fd1ba 25#include "fileio.h"
f4f15635 26#include "fs-util.h"
d8e32c47 27#include "glyph-util.h"
3f6fd1ba 28#include "hostname-util.h"
f05e1d0d 29#include "kbd-util.h"
42f3b2f9 30#include "libcrypt-util.h"
3f6fd1ba 31#include "locale-util.h"
b352e545 32#include "lock-util.h"
45f39418 33#include "main-func.h"
9ae4ef49 34#include "memory-util.h"
418b9be5 35#include "mkdir.h"
3ff9fa59 36#include "mount-util.h"
d58ad743 37#include "os-util.h"
614b022c 38#include "parse-argument.h"
6bedfcbb 39#include "parse-util.h"
d34b1823 40#include "password-quality-util.h"
418b9be5 41#include "path-util.h"
294bf0c3 42#include "pretty-print.h"
f582cbca 43#include "proc-cmdline.h"
3df3e884 44#include "random-util.h"
ce0458be 45#include "smack-util.h"
6bedfcbb 46#include "string-util.h"
3f6fd1ba 47#include "strv.h"
288a74cc 48#include "terminal-util.h"
3f6fd1ba 49#include "time-util.h"
b4909a3f 50#include "tmpfile-util-label.h"
3ff9fa59 51#include "tmpfile-util.h"
affb60b1 52#include "umask-util.h"
e929bee0 53#include "user-util.h"
418b9be5
LP
54
55static char *arg_root = NULL;
3ff9fa59 56static char *arg_image = NULL;
418b9be5
LP
57static char *arg_locale = NULL; /* $LANG */
58static char *arg_locale_messages = NULL; /* $LC_MESSAGES */
f8fd0930 59static char *arg_keymap = NULL;
418b9be5
LP
60static char *arg_timezone = NULL;
61static char *arg_hostname = NULL;
62static sd_id128_t arg_machine_id = {};
63static char *arg_root_password = NULL;
28900a1b 64static char *arg_root_shell = NULL;
a5925354 65static char *arg_kernel_cmdline = NULL;
418b9be5 66static bool arg_prompt_locale = false;
ed457f13 67static bool arg_prompt_keymap = false;
418b9be5
LP
68static bool arg_prompt_timezone = false;
69static bool arg_prompt_hostname = false;
70static bool arg_prompt_root_password = false;
28900a1b 71static bool arg_prompt_root_shell = false;
418b9be5 72static bool arg_copy_locale = false;
ed457f13 73static bool arg_copy_keymap = false;
418b9be5
LP
74static bool arg_copy_timezone = false;
75static bool arg_copy_root_password = false;
28900a1b 76static bool arg_copy_root_shell = false;
b4909a3f 77static bool arg_force = false;
4926ceaf 78static bool arg_delete_root_password = false;
676339a1 79static bool arg_root_password_is_hashed = false;
a1225020 80static bool arg_welcome = true;
05eb2c60 81static bool arg_reset = false;
84be0c71 82static ImagePolicy *arg_image_policy = NULL;
418b9be5 83
45f39418 84STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
3ff9fa59 85STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
45f39418
YW
86STATIC_DESTRUCTOR_REGISTER(arg_locale, freep);
87STATIC_DESTRUCTOR_REGISTER(arg_locale_messages, freep);
88STATIC_DESTRUCTOR_REGISTER(arg_keymap, freep);
89STATIC_DESTRUCTOR_REGISTER(arg_timezone, freep);
90STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep);
9ae4ef49 91STATIC_DESTRUCTOR_REGISTER(arg_root_password, erase_and_freep);
4f464e74
AAF
92STATIC_DESTRUCTOR_REGISTER(arg_root_shell, freep);
93STATIC_DESTRUCTOR_REGISTER(arg_kernel_cmdline, freep);
84be0c71 94STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
45f39418 95
418b9be5
LP
96static bool press_any_key(void) {
97 char k = 0;
98 bool need_nl = true;
99
100 printf("-- Press any key to proceed --");
101 fflush(stdout);
102
94956f8f 103 (void) read_one_char(stdin, &k, USEC_INFINITY, &need_nl);
418b9be5
LP
104
105 if (need_nl)
106 putchar('\n');
107
108 return k != 'q';
109}
110
a0657479 111static void print_welcome(int rfd) {
02b7005e 112 _cleanup_free_ char *pretty_name = NULL, *os_name = NULL, *ansi_color = NULL;
418b9be5 113 static bool done = false;
ae0d36c1 114 const char *pn, *ac;
418b9be5
LP
115 int r;
116
a0657479
DDM
117 assert(rfd >= 0);
118
a1225020
LP
119 if (!arg_welcome)
120 return;
121
418b9be5
LP
122 if (done)
123 return;
124
a0657479
DDM
125 r = parse_os_release_at(rfd,
126 "PRETTY_NAME", &pretty_name,
127 "NAME", &os_name,
128 "ANSI_COLOR", &ansi_color);
d58ad743
LP
129 if (r < 0)
130 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
131 "Failed to read os-release file, ignoring: %m");
418b9be5 132
02b7005e 133 pn = os_release_pretty_name(pretty_name, os_name);
ae0d36c1 134 ac = isempty(ansi_color) ? "0" : ansi_color;
4aeadec7 135
5fee4ac0
LP
136 (void) reset_terminal_fd(STDIN_FILENO, /* switch_to_text= */ false);
137
4aeadec7 138 if (colors_enabled())
ae0d36c1 139 printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ac, pn);
4aeadec7
LP
140 else
141 printf("\nWelcome to your new installation of %s!\n", pn);
142
143 printf("\nPlease configure your system!\n\n");
418b9be5
LP
144
145 press_any_key();
146
147 done = true;
148}
149
150static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) {
418b9be5 151 unsigned break_lines, break_modulo;
da6053d0 152 size_t n, per_column, i, j;
418b9be5
LP
153
154 assert(n_columns > 0);
155
156 n = strv_length(x);
be6b0c21 157 per_column = DIV_ROUND_UP(n, n_columns);
418b9be5
LP
158
159 break_lines = lines();
160 if (break_lines > 2)
161 break_lines--;
162
163 /* The first page gets two extra lines, since we want to show
164 * a title */
165 break_modulo = break_lines;
166 if (break_modulo > 3)
167 break_modulo -= 3;
168
169 for (i = 0; i < per_column; i++) {
170
b3a9d980 171 for (j = 0; j < n_columns; j++) {
418b9be5
LP
172 _cleanup_free_ char *e = NULL;
173
174 if (j * per_column + i >= n)
175 break;
176
177 e = ellipsize(x[j * per_column + i], width, percentage);
178 if (!e)
179 return log_oom();
180
f996072f 181 printf("%4zu) %-*s", j * per_column + i + 1, (int) width, e);
418b9be5
LP
182 }
183
184 putchar('\n');
185
186 /* on the first screen we reserve 2 extra lines for the title */
187 if (i % break_lines == break_modulo) {
188 if (!press_any_key())
189 return 0;
190 }
191 }
192
193 return 0;
194}
195
ecada8f2 196static int prompt_loop(const char *text, char **l, unsigned percentage, bool (*is_valid)(const char *name), char **ret) {
418b9be5
LP
197 int r;
198
199 assert(text);
200 assert(is_valid);
201 assert(ret);
202
203 for (;;) {
204 _cleanup_free_ char *p = NULL;
205 unsigned u;
206
ecada8f2
ZJS
207 r = ask_string(&p, "%s %s (empty to skip, \"list\" to list options): ",
208 special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), text);
23bbb0de
MS
209 if (r < 0)
210 return log_error_errno(r, "Failed to query user: %m");
418b9be5
LP
211
212 if (isempty(p)) {
213 log_warning("No data entered, skipping.");
214 return 0;
215 }
216
ecada8f2
ZJS
217 if (streq(p, "list")) {
218 r = show_menu(l, 3, 22, percentage);
219 if (r < 0)
220 return r;
221
222 putchar('\n');
223 continue;
224 };
225
418b9be5
LP
226 r = safe_atou(p, &u);
227 if (r >= 0) {
418b9be5
LP
228 if (u <= 0 || u > strv_length(l)) {
229 log_error("Specified entry number out of range.");
230 continue;
231 }
232
233 log_info("Selected '%s'.", l[u-1]);
b3f9c17a 234 return free_and_strdup_warn(ret, l[u-1]);
418b9be5
LP
235 }
236
237 if (!is_valid(p)) {
238 log_error("Entered data invalid.");
239 continue;
240 }
241
bfbf5f74 242 return free_and_replace(*ret, p);
418b9be5
LP
243 }
244}
245
b352e545 246static int should_configure(int dir_fd, const char *filename) {
a777a592
ZJS
247 _cleanup_fclose_ FILE *passwd = NULL, *shadow = NULL;
248 int r;
249
b352e545
DDM
250 assert(dir_fd >= 0);
251 assert(filename);
252
a777a592
ZJS
253 if (streq(filename, "passwd") && !arg_force)
254 /* We may need to do additional checks, so open the file. */
255 r = xfopenat(dir_fd, filename, "re", O_NOFOLLOW, &passwd);
256 else
257 r = RET_NERRNO(faccessat(dir_fd, filename, F_OK, AT_SYMLINK_NOFOLLOW));
258
259 if (r == -ENOENT)
260 return true; /* missing */
261 if (r < 0)
262 return log_error_errno(r, "Failed to access %s: %m", filename);
263 if (arg_force)
264 return true; /* exists, but if --force was given we should still configure the file. */
265
266 if (!passwd)
267 return false;
268
269 /* In case of /etc/passwd, do an additional check for the root password field.
270 * We first check that passwd redirects to shadow, and then we check shadow.
271 */
272 struct passwd *i;
273 while ((r = fgetpwent_sane(passwd, &i)) > 0) {
274 if (!streq(i->pw_name, "root"))
275 continue;
276
277 if (streq_ptr(i->pw_passwd, PASSWORD_SEE_SHADOW))
278 break;
279 log_debug("passwd: root account with non-shadow password found, treating root as configured");
280 return false;
281 }
282 if (r < 0)
283 return log_error_errno(r, "Failed to read %s: %m", filename);
284 if (r == 0) {
285 log_debug("No root account found in %s, assuming root is not configured.", filename);
286 return true;
287 }
df00c516 288
a777a592
ZJS
289 r = xfopenat(dir_fd, "shadow", "re", O_NOFOLLOW, &shadow);
290 if (r == -ENOENT) {
291 log_debug("No shadow file found, assuming root is not configured.");
df00c516
DDM
292 return true; /* missing */
293 }
a777a592
ZJS
294 if (r < 0)
295 return log_error_errno(r, "Failed to access shadow: %m");
b352e545 296
a777a592
ZJS
297 struct spwd *j;
298 while ((r = fgetspent_sane(shadow, &j)) > 0) {
299 if (!streq(j->sp_namp, "root"))
300 continue;
301
302 bool unprovisioned = streq_ptr(j->sp_pwdp, PASSWORD_UNPROVISIONED);
303 log_debug("Root account found, %s.",
304 unprovisioned ? "with unprovisioned password, treating root as not configured" :
305 "treating root as configured");
306 return unprovisioned;
307 }
308 if (r < 0)
309 return log_error_errno(r, "Failed to read shadow: %m");
310 assert(r == 0);
311 log_debug("No root account found in shadow, assuming root is not configured.");
312 return true;
b352e545
DDM
313}
314
a0657479
DDM
315static bool locale_is_installed_bool(const char *name) {
316 return locale_is_installed(name) > 0;
317}
a00a78b8 318
a0657479
DDM
319static bool locale_is_ok(int rfd, const char *name) {
320 assert(rfd >= 0);
a00a78b8 321
a0657479 322 return dir_fd_is_root(rfd) ? locale_is_installed_bool(name) : locale_is_valid(name);
a00a78b8
LP
323}
324
a0657479 325static int prompt_locale(int rfd) {
418b9be5 326 _cleanup_strv_free_ char **locales = NULL;
416f7b3a 327 bool acquired_from_creds = false;
418b9be5
LP
328 int r;
329
a0657479
DDM
330 assert(rfd >= 0);
331
418b9be5
LP
332 if (arg_locale || arg_locale_messages)
333 return 0;
334
416f7b3a
LP
335 r = read_credential("firstboot.locale", (void**) &arg_locale, NULL);
336 if (r < 0)
337 log_debug_errno(r, "Failed to read credential firstboot.locale, ignoring: %m");
338 else
339 acquired_from_creds = true;
340
341 r = read_credential("firstboot.locale-messages", (void**) &arg_locale_messages, NULL);
342 if (r < 0)
8914f7e8 343 log_debug_errno(r, "Failed to read credential firstboot.locale-messages, ignoring: %m");
416f7b3a
LP
344 else
345 acquired_from_creds = true;
346
347 if (acquired_from_creds) {
348 log_debug("Acquired locale from credentials.");
349 return 0;
350 }
351
eb650ffe
ZJS
352 if (!arg_prompt_locale) {
353 log_debug("Prompting for locale was not requested.");
418b9be5 354 return 0;
eb650ffe 355 }
418b9be5
LP
356
357 r = get_locales(&locales);
23bbb0de
MS
358 if (r < 0)
359 return log_error_errno(r, "Cannot query locales list: %m");
418b9be5 360
bdd7cd26
LP
361 if (strv_isempty(locales))
362 log_debug("No locales found, skipping locale selection.");
363 else if (strv_length(locales) == 1) {
418b9be5 364
bdd7cd26
LP
365 if (streq(locales[0], SYSTEMD_DEFAULT_LOCALE))
366 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
367 else {
368 log_debug("Only a single locale available (%s), selecting it as default.", locales[0]);
418b9be5 369
bdd7cd26
LP
370 arg_locale = strdup(locales[0]);
371 if (!arg_locale)
372 return log_oom();
418b9be5 373
bdd7cd26
LP
374 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
375 }
376 } else {
a0657479
DDM
377 bool (*is_valid)(const char *name) = dir_fd_is_root(rfd) ? locale_is_installed_bool
378 : locale_is_valid;
379
380 print_welcome(rfd);
418b9be5 381
ecada8f2 382 r = prompt_loop("Please enter system locale name or number",
a0657479 383 locales, 60, is_valid, &arg_locale);
bdd7cd26
LP
384 if (r < 0)
385 return r;
386
387 if (isempty(arg_locale))
388 return 0;
389
ecada8f2 390 r = prompt_loop("Please enter system message locale name or number",
a0657479 391 locales, 60, is_valid, &arg_locale_messages);
bdd7cd26
LP
392 if (r < 0)
393 return r;
394
395 /* Suppress the messages setting if it's the same as the main locale anyway */
396 if (streq_ptr(arg_locale, arg_locale_messages))
397 arg_locale_messages = mfree(arg_locale_messages);
398 }
418b9be5
LP
399
400 return 0;
401}
402
b352e545
DDM
403static int process_locale(int rfd) {
404 _cleanup_close_ int pfd = -EBADF;
405 _cleanup_free_ char *f = NULL;
418b9be5
LP
406 char* locales[3];
407 unsigned i = 0;
408 int r;
409
b352e545
DDM
410 assert(rfd >= 0);
411
412 pfd = chase_and_open_parent_at(rfd, "/etc/locale.conf",
b39710cc 413 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
b352e545
DDM
414 &f);
415 if (pfd < 0)
416 return log_error_errno(pfd, "Failed to chase /etc/locale.conf: %m");
418b9be5 417
b352e545
DDM
418 r = should_configure(pfd, f);
419 if (r == 0)
420 log_debug("Found /etc/locale.conf, assuming locale information has been configured.");
421 if (r <= 0)
422 return r;
418b9be5 423
fe75d5bc
DDM
424 r = dir_fd_is_root(rfd);
425 if (r < 0)
426 return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
427
428 if (arg_copy_locale && r == 0) {
b352e545 429 r = copy_file_atomic_at(AT_FDCWD, "/etc/locale.conf", pfd, f, 0644, COPY_REFLINK);
418b9be5 430 if (r != -ENOENT) {
23bbb0de 431 if (r < 0)
b352e545 432 return log_error_errno(r, "Failed to copy host's /etc/locale.conf: %m");
418b9be5 433
b352e545 434 log_info("Copied host's /etc/locale.conf.");
418b9be5
LP
435 return 0;
436 }
437 }
438
a0657479 439 r = prompt_locale(rfd);
418b9be5
LP
440 if (r < 0)
441 return r;
442
443 if (!isempty(arg_locale))
63c372cb 444 locales[i++] = strjoina("LANG=", arg_locale);
4c4a73ce 445 if (!isempty(arg_locale_messages) && !streq_ptr(arg_locale_messages, arg_locale))
63c372cb 446 locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages);
418b9be5
LP
447
448 if (i == 0)
449 return 0;
450
451 locales[i] = NULL;
452
f155cb6d 453 r = write_env_file(pfd, f, NULL, locales);
23bbb0de 454 if (r < 0)
b352e545 455 return log_error_errno(r, "Failed to write /etc/locale.conf: %m");
418b9be5 456
b352e545 457 log_info("/etc/locale.conf written.");
d0c50d8d 458 return 1;
418b9be5
LP
459}
460
a0657479 461static int prompt_keymap(int rfd) {
ed457f13
TB
462 _cleanup_strv_free_ char **kmaps = NULL;
463 int r;
464
a0657479
DDM
465 assert(rfd >= 0);
466
ed457f13
TB
467 if (arg_keymap)
468 return 0;
469
416f7b3a
LP
470 r = read_credential("firstboot.keymap", (void**) &arg_keymap, NULL);
471 if (r < 0)
472 log_debug_errno(r, "Failed to read credential firstboot.keymap, ignoring: %m");
473 else {
474 log_debug("Acquired keymap from credential.");
475 return 0;
476 }
477
eb650ffe
ZJS
478 if (!arg_prompt_keymap) {
479 log_debug("Prompting for keymap was not requested.");
ed457f13 480 return 0;
eb650ffe 481 }
ed457f13
TB
482
483 r = get_keymaps(&kmaps);
484 if (r == -ENOENT) /* no keymaps installed */
eb650ffe 485 return log_debug_errno(r, "No keymaps are installed.");
ed457f13
TB
486 if (r < 0)
487 return log_error_errno(r, "Failed to read keymaps: %m");
488
a0657479 489 print_welcome(rfd);
ed457f13 490
8700b4da 491 return prompt_loop("Please enter system keymap name or number",
ecada8f2 492 kmaps, 60, keymap_is_valid, &arg_keymap);
ed457f13
TB
493}
494
b352e545
DDM
495static int process_keymap(int rfd) {
496 _cleanup_close_ int pfd = -EBADF;
497 _cleanup_free_ char *f = NULL;
ed457f13
TB
498 char **keymap;
499 int r;
500
b352e545 501 assert(rfd >= 0);
ed457f13 502
b352e545 503 pfd = chase_and_open_parent_at(rfd, "/etc/vconsole.conf",
b39710cc 504 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
b352e545
DDM
505 &f);
506 if (pfd < 0)
507 return log_error_errno(pfd, "Failed to chase /etc/vconsole.conf: %m");
ed457f13 508
b352e545
DDM
509 r = should_configure(pfd, f);
510 if (r == 0)
511 log_debug("Found /etc/vconsole.conf, assuming console has been configured.");
512 if (r <= 0)
513 return r;
514
fe75d5bc
DDM
515 r = dir_fd_is_root(rfd);
516 if (r < 0)
517 return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
518
519 if (arg_copy_keymap && r == 0) {
b352e545 520 r = copy_file_atomic_at(AT_FDCWD, "/etc/vconsole.conf", pfd, f, 0644, COPY_REFLINK);
ed457f13
TB
521 if (r != -ENOENT) {
522 if (r < 0)
b352e545 523 return log_error_errno(r, "Failed to copy host's /etc/vconsole.conf: %m");
ed457f13 524
b352e545 525 log_info("Copied host's /etc/vconsole.conf.");
ed457f13
TB
526 return 0;
527 }
528 }
529
a0657479 530 r = prompt_keymap(rfd);
ed457f13
TB
531 if (r == -ENOENT)
532 return 0; /* don't fail if no keymaps are installed */
533 if (r < 0)
534 return r;
535
a7353b4d 536 if (isempty(arg_keymap))
ed457f13
TB
537 return 0;
538
a7353b4d
YW
539 keymap = STRV_MAKE(strjoina("KEYMAP=", arg_keymap));
540
0e7a7cd4 541 r = write_vconsole_conf(pfd, f, keymap);
ed457f13 542 if (r < 0)
b352e545 543 return log_error_errno(r, "Failed to write /etc/vconsole.conf: %m");
ed457f13 544
b352e545 545 log_info("/etc/vconsole.conf written.");
cea32691 546 return 1;
ed457f13
TB
547}
548
089fb865
MG
549static bool timezone_is_valid_log_error(const char *name) {
550 return timezone_is_valid(name, LOG_ERR);
551}
552
a0657479 553static int prompt_timezone(int rfd) {
418b9be5
LP
554 _cleanup_strv_free_ char **zones = NULL;
555 int r;
556
a0657479
DDM
557 assert(rfd >= 0);
558
418b9be5
LP
559 if (arg_timezone)
560 return 0;
561
416f7b3a
LP
562 r = read_credential("firstboot.timezone", (void**) &arg_timezone, NULL);
563 if (r < 0)
564 log_debug_errno(r, "Failed to read credential firstboot.timezone, ignoring: %m");
565 else {
566 log_debug("Acquired timezone from credential.");
567 return 0;
568 }
569
eb650ffe
ZJS
570 if (!arg_prompt_timezone) {
571 log_debug("Prompting for timezone was not requested.");
418b9be5 572 return 0;
eb650ffe 573 }
418b9be5
LP
574
575 r = get_timezones(&zones);
23bbb0de
MS
576 if (r < 0)
577 return log_error_errno(r, "Cannot query timezone list: %m");
418b9be5 578
a0657479 579 print_welcome(rfd);
418b9be5 580
ecada8f2
ZJS
581 r = prompt_loop("Please enter timezone name or number",
582 zones, 30, timezone_is_valid_log_error, &arg_timezone);
418b9be5
LP
583 if (r < 0)
584 return r;
585
586 return 0;
587}
588
b352e545
DDM
589static int process_timezone(int rfd) {
590 _cleanup_close_ int pfd = -EBADF;
591 _cleanup_free_ char *f = NULL;
592 const char *e;
418b9be5
LP
593 int r;
594
b352e545 595 assert(rfd >= 0);
418b9be5 596
b352e545
DDM
597 pfd = chase_and_open_parent_at(rfd, "/etc/localtime",
598 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
599 &f);
600 if (pfd < 0)
601 return log_error_errno(pfd, "Failed to chase /etc/localtime: %m");
418b9be5 602
b352e545
DDM
603 r = should_configure(pfd, f);
604 if (r == 0)
605 log_debug("Found /etc/localtime, assuming timezone has been configured.");
606 if (r <= 0)
607 return r;
608
fe75d5bc
DDM
609 r = dir_fd_is_root(rfd);
610 if (r < 0)
611 return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
612
613 if (arg_copy_timezone && r == 0) {
b352e545
DDM
614 _cleanup_free_ char *s = NULL;
615
616 r = readlink_malloc("/etc/localtime", &s);
418b9be5 617 if (r != -ENOENT) {
23bbb0de 618 if (r < 0)
b352e545 619 return log_error_errno(r, "Failed to read host's /etc/localtime: %m");
418b9be5 620
b352e545 621 r = symlinkat_atomic_full(s, pfd, f, /* make_relative= */ false);
e56dc320 622 if (r < 0)
b352e545 623 return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
418b9be5 624
b352e545 625 log_info("Copied host's /etc/localtime.");
418b9be5
LP
626 return 0;
627 }
628 }
629
a0657479 630 r = prompt_timezone(rfd);
418b9be5
LP
631 if (r < 0)
632 return r;
633
634 if (isempty(arg_timezone))
635 return 0;
636
63c372cb 637 e = strjoina("../usr/share/zoneinfo/", arg_timezone);
418b9be5 638
b352e545 639 r = symlinkat_atomic_full(e, pfd, f, /* make_relative= */ false);
e56dc320 640 if (r < 0)
b352e545 641 return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
418b9be5 642
b352e545 643 log_info("/etc/localtime written");
418b9be5
LP
644 return 0;
645}
646
a0657479 647static int prompt_hostname(int rfd) {
418b9be5
LP
648 int r;
649
a0657479
DDM
650 assert(rfd >= 0);
651
418b9be5
LP
652 if (arg_hostname)
653 return 0;
654
eb650ffe
ZJS
655 if (!arg_prompt_hostname) {
656 log_debug("Prompting for hostname was not requested.");
418b9be5 657 return 0;
eb650ffe 658 }
418b9be5 659
a0657479 660 print_welcome(rfd);
418b9be5
LP
661 putchar('\n');
662
663 for (;;) {
664 _cleanup_free_ char *h = NULL;
665
9a6f746f 666 r = ask_string(&h, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET));
23bbb0de
MS
667 if (r < 0)
668 return log_error_errno(r, "Failed to query hostname: %m");
418b9be5
LP
669
670 if (isempty(h)) {
671 log_warning("No hostname entered, skipping.");
672 break;
673 }
674
52ef5dd7 675 if (!hostname_is_valid(h, VALID_HOSTNAME_TRAILING_DOT)) {
418b9be5
LP
676 log_error("Specified hostname invalid.");
677 continue;
678 }
679
34ad6090 680 /* Get rid of the trailing dot that we allow, but don't want to see */
ae691c1d 681 arg_hostname = hostname_cleanup(h);
418b9be5
LP
682 h = NULL;
683 break;
684 }
685
686 return 0;
687}
688
b352e545
DDM
689static int process_hostname(int rfd) {
690 _cleanup_close_ int pfd = -EBADF;
691 _cleanup_free_ char *f = NULL;
418b9be5
LP
692 int r;
693
b352e545
DDM
694 assert(rfd >= 0);
695
696 pfd = chase_and_open_parent_at(rfd, "/etc/hostname",
697 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN,
698 &f);
699 if (pfd < 0)
700 return log_error_errno(pfd, "Failed to chase /etc/hostname: %m");
701
702 r = should_configure(pfd, f);
703 if (r == 0)
704 log_debug("Found /etc/hostname, assuming hostname has been configured.");
705 if (r <= 0)
706 return r;
418b9be5 707
a0657479 708 r = prompt_hostname(rfd);
418b9be5
LP
709 if (r < 0)
710 return r;
711
712 if (isempty(arg_hostname))
713 return 0;
714
b352e545
DDM
715 r = write_string_file_at(pfd, f, arg_hostname,
716 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC);
23bbb0de 717 if (r < 0)
b352e545 718 return log_error_errno(r, "Failed to write /etc/hostname: %m");
418b9be5 719
b352e545 720 log_info("/etc/hostname written.");
418b9be5
LP
721 return 0;
722}
723
b352e545
DDM
724static int process_machine_id(int rfd) {
725 _cleanup_close_ int pfd = -EBADF;
726 _cleanup_free_ char *f = NULL;
418b9be5
LP
727 int r;
728
b352e545
DDM
729 assert(rfd >= 0);
730
731 pfd = chase_and_open_parent_at(rfd, "/etc/machine-id",
b39710cc 732 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
b352e545
DDM
733 &f);
734 if (pfd < 0)
735 return log_error_errno(pfd, "Failed to chase /etc/machine-id: %m");
736
737 r = should_configure(pfd, f);
738 if (r == 0)
739 log_debug("Found /etc/machine-id, assuming machine-id has been configured.");
740 if (r <= 0)
741 return r;
418b9be5 742
eb650ffe
ZJS
743 if (sd_id128_is_null(arg_machine_id)) {
744 log_debug("Initialization of machine-id was not requested, skipping.");
418b9be5 745 return 0;
eb650ffe 746 }
418b9be5 747
b352e545
DDM
748 r = write_string_file_at(pfd, "machine-id", SD_ID128_TO_STRING(arg_machine_id),
749 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC);
23bbb0de 750 if (r < 0)
c7c1edd6 751 return log_error_errno(r, "Failed to write /etc/machine-id: %m");
418b9be5 752
b352e545 753 log_info("/etc/machine-id written.");
418b9be5
LP
754 return 0;
755}
756
a0657479 757static int prompt_root_password(int rfd) {
1fbc95d3 758 const char *msg1, *msg2;
418b9be5
LP
759 int r;
760
a0657479
DDM
761 assert(rfd >= 0);
762
418b9be5
LP
763 if (arg_root_password)
764 return 0;
765
ff86850b 766 if (get_credential_user_password("root", &arg_root_password, &arg_root_password_is_hashed) >= 0)
416f7b3a 767 return 0;
416f7b3a 768
eb650ffe
ZJS
769 if (!arg_prompt_root_password) {
770 log_debug("Prompting for root password was not requested.");
418b9be5 771 return 0;
eb650ffe 772 }
418b9be5 773
a0657479 774 print_welcome(rfd);
418b9be5
LP
775 putchar('\n');
776
3619634c
LP
777 msg1 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter a new root password (empty to skip):");
778 msg2 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter new root password again:");
418b9be5 779
7baf10a7
LP
780 suggest_passwords();
781
418b9be5 782 for (;;) {
c7b7d74e 783 _cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
7baf10a7 784 _cleanup_free_ char *error = NULL;
418b9be5 785
daa55720 786 r = ask_password_tty(-1, msg1, NULL, 0, 0, NULL, &a);
23bbb0de
MS
787 if (r < 0)
788 return log_error_errno(r, "Failed to query root password: %m");
39e96f84
ZJS
789 if (strv_length(a) != 1)
790 return log_error_errno(SYNTHETIC_ERRNO(EIO),
791 "Received multiple passwords, where we expected one.");
418b9be5 792
c7b7d74e 793 if (isempty(*a)) {
418b9be5
LP
794 log_warning("No password entered, skipping.");
795 break;
796 }
797
d34b1823 798 r = check_password_quality(*a, /* old */ NULL, "root", &error);
bb44fd07
ZJS
799 if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
800 log_warning("Password quality check is not supported, proceeding anyway.");
801 else if (r < 0)
802 return log_error_errno(r, "Failed to check password quality: %m");
803 else if (r == 0)
7baf10a7
LP
804 log_warning("Password is weak, accepting anyway: %s", error);
805
daa55720 806 r = ask_password_tty(-1, msg2, NULL, 0, 0, NULL, &b);
ab84f5b9 807 if (r < 0)
00843602 808 return log_error_errno(r, "Failed to query root password: %m");
39e96f84
ZJS
809 if (strv_length(b) != 1)
810 return log_error_errno(SYNTHETIC_ERRNO(EIO),
811 "Received multiple passwords, where we expected one.");
418b9be5 812
c7b7d74e 813 if (!streq(*a, *b)) {
418b9be5 814 log_error("Entered passwords did not match, please try again.");
418b9be5
LP
815 continue;
816 }
817
c7b7d74e 818 arg_root_password = TAKE_PTR(*a);
418b9be5
LP
819 break;
820 }
821
822 return 0;
823}
824
b352e545 825static int find_shell(int rfd, const char *path) {
31363bd5
DDM
826 int r;
827
828 assert(path);
829
830 if (!valid_shell(path))
831 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "%s is not a valid shell", path);
832
b352e545
DDM
833 r = chaseat(rfd, path, CHASE_AT_RESOLVE_IN_ROOT, NULL, NULL);
834 if (r < 0)
835 return log_error_errno(r, "Failed to resolve shell %s: %m", path);
31363bd5
DDM
836
837 return 0;
838}
839
b352e545 840static int prompt_root_shell(int rfd) {
28900a1b
DDM
841 int r;
842
a0657479
DDM
843 assert(rfd >= 0);
844
416f7b3a
LP
845 if (arg_root_shell)
846 return 0;
847
848 r = read_credential("passwd.shell.root", (void**) &arg_root_shell, NULL);
849 if (r < 0)
850 log_debug_errno(r, "Failed to read credential passwd.shell.root, ignoring: %m");
851 else {
852 log_debug("Acquired root shell from credential.");
853 return 0;
854 }
855
eb650ffe
ZJS
856 if (!arg_prompt_root_shell) {
857 log_debug("Prompting for root shell was not requested.");
28900a1b 858 return 0;
eb650ffe 859 }
28900a1b 860
a0657479 861 print_welcome(rfd);
28900a1b
DDM
862 putchar('\n');
863
864 for (;;) {
865 _cleanup_free_ char *s = NULL;
866
867 r = ask_string(&s, "%s Please enter root shell for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET));
868 if (r < 0)
869 return log_error_errno(r, "Failed to query root shell: %m");
870
871 if (isempty(s)) {
872 log_warning("No shell entered, skipping.");
873 break;
874 }
875
b352e545 876 r = find_shell(rfd, s);
31363bd5 877 if (r < 0)
28900a1b 878 continue;
28900a1b
DDM
879
880 arg_root_shell = TAKE_PTR(s);
881 break;
882 }
883
884 return 0;
885}
886
a0657479 887static int write_root_passwd(int rfd, int etc_fd, const char *password, const char *shell) {
4926ceaf
DDM
888 _cleanup_fclose_ FILE *original = NULL, *passwd = NULL;
889 _cleanup_(unlink_and_freep) char *passwd_tmp = NULL;
890 int r;
891
c4a53ebf
DDM
892 assert(password);
893
b352e545 894 r = fopen_temporary_at_label(etc_fd, "passwd", "passwd", &passwd, &passwd_tmp);
4926ceaf
DDM
895 if (r < 0)
896 return r;
897
b352e545
DDM
898 r = xfopenat(etc_fd, "passwd", "re", O_NOFOLLOW, &original);
899 if (r < 0 && r != -ENOENT)
900 return r;
901
4926ceaf
DDM
902 if (original) {
903 struct passwd *i;
904
bb72c434 905 r = copy_rights(fileno(original), fileno(passwd));
4926ceaf
DDM
906 if (r < 0)
907 return r;
908
909 while ((r = fgetpwent_sane(original, &i)) > 0) {
910
28900a1b 911 if (streq(i->pw_name, "root")) {
4926ceaf 912 i->pw_passwd = (char *) password;
28900a1b
DDM
913 if (shell)
914 i->pw_shell = (char *) shell;
915 }
4926ceaf
DDM
916
917 r = putpwent_sane(i, passwd);
918 if (r < 0)
919 return r;
920 }
921 if (r < 0)
922 return r;
923
924 } else {
925 struct passwd root = {
926 .pw_name = (char *) "root",
927 .pw_passwd = (char *) password,
928 .pw_uid = 0,
929 .pw_gid = 0,
930 .pw_gecos = (char *) "Super User",
931 .pw_dir = (char *) "/root",
a0657479 932 .pw_shell = (char *) (shell ?: default_root_shell_at(rfd)),
4926ceaf
DDM
933 };
934
935 if (errno != ENOENT)
936 return -errno;
937
b226422c 938 r = fchmod(fileno(passwd), 0644);
4926ceaf
DDM
939 if (r < 0)
940 return -errno;
941
942 r = putpwent_sane(&root, passwd);
943 if (r < 0)
944 return r;
945 }
946
947 r = fflush_sync_and_check(passwd);
948 if (r < 0)
949 return r;
950
b352e545 951 r = renameat_and_apply_smack_floor_label(etc_fd, passwd_tmp, etc_fd, "passwd");
4926ceaf
DDM
952 if (r < 0)
953 return r;
954
955 return 0;
956}
957
b352e545 958static int write_root_shadow(int etc_fd, const char *hashed_password) {
b4909a3f
DDM
959 _cleanup_fclose_ FILE *original = NULL, *shadow = NULL;
960 _cleanup_(unlink_and_freep) char *shadow_tmp = NULL;
100d5f6e
FB
961 int r;
962
c4a53ebf
DDM
963 assert(hashed_password);
964
b352e545 965 r = fopen_temporary_at_label(etc_fd, "shadow", "shadow", &shadow, &shadow_tmp);
b4909a3f
DDM
966 if (r < 0)
967 return r;
968
b352e545
DDM
969 r = xfopenat(etc_fd, "shadow", "re", O_NOFOLLOW, &original);
970 if (r < 0 && r != -ENOENT)
971 return r;
972
b4909a3f
DDM
973 if (original) {
974 struct spwd *i;
975
bb72c434 976 r = copy_rights(fileno(original), fileno(shadow));
b4909a3f
DDM
977 if (r < 0)
978 return r;
979
980 while ((r = fgetspent_sane(original, &i)) > 0) {
981
982 if (streq(i->sp_namp, "root")) {
983 i->sp_pwdp = (char *) hashed_password;
984 i->sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
985 }
986
987 r = putspent_sane(i, shadow);
988 if (r < 0)
989 return r;
990 }
991 if (r < 0)
992 return r;
993
994 } else {
995 struct spwd root = {
996 .sp_namp = (char*) "root",
997 .sp_pwdp = (char *) hashed_password,
998 .sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY),
999 .sp_min = -1,
1000 .sp_max = -1,
1001 .sp_warn = -1,
1002 .sp_inact = -1,
1003 .sp_expire = -1,
f5fbe71d 1004 .sp_flag = ULONG_MAX, /* this appears to be what everybody does ... */
b4909a3f
DDM
1005 };
1006
1007 if (errno != ENOENT)
1008 return -errno;
418b9be5 1009
b4909a3f
DDM
1010 r = fchmod(fileno(shadow), 0000);
1011 if (r < 0)
1012 return -errno;
418b9be5 1013
b4909a3f
DDM
1014 r = putspent_sane(&root, shadow);
1015 if (r < 0)
1016 return r;
1017 }
1018
1019 r = fflush_sync_and_check(shadow);
100d5f6e
FB
1020 if (r < 0)
1021 return r;
418b9be5 1022
b352e545 1023 r = renameat_and_apply_smack_floor_label(etc_fd, shadow_tmp, etc_fd, "shadow");
b4909a3f
DDM
1024 if (r < 0)
1025 return r;
1026
1027 return 0;
418b9be5
LP
1028}
1029
b352e545
DDM
1030static int process_root_account(int rfd) {
1031 _cleanup_close_ int pfd = -EBADF;
1032 _cleanup_(release_lock_file) LockFile lock = LOCK_FILE_INIT;
0e98d17e 1033 _cleanup_(erase_and_freep) char *_hashed_password = NULL;
c4a53ebf 1034 const char *password, *hashed_password;
b352e545
DDM
1035 int k = 0, r;
1036
1037 assert(rfd >= 0);
1038
1039 pfd = chase_and_open_parent_at(rfd, "/etc/passwd",
1040 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
1041 NULL);
1042 if (pfd < 0)
1043 return log_error_errno(pfd, "Failed to chase /etc/passwd: %m");
1044
1045 /* Ensure that passwd and shadow are in the same directory and are not symlinks. */
1046
1047 FOREACH_STRING(s, "passwd", "shadow") {
1048 r = verify_regular_at(pfd, s, /* follow = */ false);
1049 if (IN_SET(r, -EISDIR, -ELOOP, -EBADFD))
1050 return log_error_errno(r, "/etc/%s is not a regular file", s);
1051 if (r < 0 && r != -ENOENT)
1052 return log_error_errno(r, "Failed to check whether /etc/%s is a regular file: %m", s);
1053
1054 r = should_configure(pfd, s);
1055 if (r < 0)
1056 return r;
418b9be5 1057
b352e545
DDM
1058 k += r;
1059 }
c4a53ebf 1060
b352e545
DDM
1061 if (k == 0) {
1062 log_debug("Found /etc/passwd and /etc/shadow, assuming root account has been initialized.");
418b9be5 1063 return 0;
eb650ffe 1064 }
87a3a4a8 1065
67d5d9d5 1066 /* Don't create/modify passwd and shadow if not asked */
1067 if (!(arg_root_password || arg_prompt_root_password || arg_copy_root_password || arg_delete_root_password ||
eb650ffe
ZJS
1068 arg_root_shell || arg_prompt_root_shell || arg_copy_root_shell)) {
1069 log_debug("Initialization of root account was not requested, skipping.");
67d5d9d5 1070 return 0;
eb650ffe 1071 }
418b9be5 1072
fe585662 1073 r = make_lock_file_at(pfd, ETC_PASSWD_LOCK_FILENAME, LOCK_EX, &lock);
b352e545
DDM
1074 if (r < 0)
1075 return log_error_errno(r, "Failed to take a lock on /etc/passwd: %m");
4926ceaf 1076
fe75d5bc
DDM
1077 k = dir_fd_is_root(rfd);
1078 if (k < 0)
1079 return log_error_errno(k, "Failed to check if directory file descriptor is root: %m");
1080
1081 if (arg_copy_root_shell && k == 0) {
75673cd8 1082 _cleanup_free_ struct passwd *p = NULL;
28900a1b 1083
75673cd8
LP
1084 r = getpwnam_malloc("root", &p);
1085 if (r < 0)
1086 return log_error_errno(r, "Failed to find passwd entry for root: %m");
28900a1b
DDM
1087
1088 r = free_and_strdup(&arg_root_shell, p->pw_shell);
1089 if (r < 0)
1090 return log_oom();
1091 }
1092
b352e545 1093 r = prompt_root_shell(rfd);
28900a1b
DDM
1094 if (r < 0)
1095 return r;
1096
fe75d5bc 1097 if (arg_copy_root_password && k == 0) {
418b9be5
LP
1098 struct spwd *p;
1099
1100 errno = 0;
1101 p = getspnam("root");
c4a53ebf
DDM
1102 if (!p)
1103 return log_error_errno(errno_or_else(EIO), "Failed to find shadow entry for root: %m");
418b9be5 1104
c4a53ebf
DDM
1105 r = free_and_strdup(&arg_root_password, p->sp_pwdp);
1106 if (r < 0)
1107 return log_oom();
418b9be5 1108
c4a53ebf 1109 arg_root_password_is_hashed = true;
418b9be5
LP
1110 }
1111
a0657479 1112 r = prompt_root_password(rfd);
418b9be5
LP
1113 if (r < 0)
1114 return r;
1115
c4a53ebf 1116 if (arg_root_password && arg_root_password_is_hashed) {
53c25ac9 1117 password = PASSWORD_SEE_SHADOW;
676339a1 1118 hashed_password = arg_root_password;
c4a53ebf 1119 } else if (arg_root_password) {
0e98d17e
ZJS
1120 r = hash_password(arg_root_password, &_hashed_password);
1121 if (r < 0)
1122 return log_error_errno(r, "Failed to hash password: %m");
676339a1 1123
53c25ac9 1124 password = PASSWORD_SEE_SHADOW;
0e98d17e 1125 hashed_password = _hashed_password;
c4a53ebf 1126
c4a53ebf 1127 } else if (arg_delete_root_password)
53c25ac9 1128 password = hashed_password = PASSWORD_NONE;
c4a53ebf 1129 else
53c25ac9 1130 password = hashed_password = PASSWORD_LOCKED_AND_INVALID;
c4a53ebf 1131
a0657479 1132 r = write_root_passwd(rfd, pfd, password, arg_root_shell);
c4a53ebf 1133 if (r < 0)
b352e545 1134 return log_error_errno(r, "Failed to write /etc/passwd: %m");
c4a53ebf 1135
b352e545 1136 log_info("/etc/passwd written.");
418b9be5 1137
b352e545 1138 r = write_root_shadow(pfd, hashed_password);
23bbb0de 1139 if (r < 0)
b352e545 1140 return log_error_errno(r, "Failed to write /etc/shadow: %m");
418b9be5 1141
b352e545 1142 log_info("/etc/shadow written.");
418b9be5
LP
1143 return 0;
1144}
1145
b352e545
DDM
1146static int process_kernel_cmdline(int rfd) {
1147 _cleanup_close_ int pfd = -EBADF;
1148 _cleanup_free_ char *f = NULL;
a5925354
DDM
1149 int r;
1150
b352e545
DDM
1151 assert(rfd >= 0);
1152
1153 pfd = chase_and_open_parent_at(rfd, "/etc/kernel/cmdline",
b39710cc 1154 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
b352e545
DDM
1155 &f);
1156 if (pfd < 0)
1157 return log_error_errno(pfd, "Failed to chase /etc/kernel/cmdline: %m");
1158
1159 r = should_configure(pfd, f);
1160 if (r == 0)
1161 log_debug("Found /etc/kernel/cmdline, assuming kernel command line has been configured.");
1162 if (r <= 0)
1163 return r;
a5925354 1164
eb650ffe
ZJS
1165 if (!arg_kernel_cmdline) {
1166 log_debug("Creation of /etc/kernel/cmdline was not requested, skipping.");
a5925354 1167 return 0;
eb650ffe 1168 }
a5925354 1169
b352e545
DDM
1170 r = write_string_file_at(pfd, "cmdline", arg_kernel_cmdline,
1171 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC);
a5925354 1172 if (r < 0)
b352e545 1173 return log_error_errno(r, "Failed to write /etc/kernel/cmdline: %m");
a5925354 1174
b352e545 1175 log_info("/etc/kernel/cmdline written.");
a5925354
DDM
1176 return 0;
1177}
1178
05eb2c60
DDM
1179static int reset_one(int rfd, const char *path) {
1180 _cleanup_close_ int pfd = -EBADF;
1181 _cleanup_free_ char *f = NULL;
1182
1183 assert(rfd >= 0);
1184 assert(path);
1185
1186 pfd = chase_and_open_parent_at(rfd, path, CHASE_AT_RESOLVE_IN_ROOT|CHASE_WARN|CHASE_NOFOLLOW, &f);
1187 if (pfd == -ENOENT)
1188 return 0;
1189 if (pfd < 0)
1190 return log_error_errno(pfd, "Failed to resolve %s: %m", path);
1191
1192 if (unlinkat(pfd, f, 0) < 0)
1193 return errno == ENOENT ? 0 : log_error_errno(errno, "Failed to remove %s: %m", path);
1194
1195 log_info("Removed %s", path);
1196 return 0;
1197}
1198
1199static int process_reset(int rfd) {
1200 int r;
1201
1202 assert(rfd >= 0);
1203
1204 if (!arg_reset)
1205 return 0;
1206
1207 FOREACH_STRING(p,
1208 "/etc/locale.conf",
1209 "/etc/vconsole.conf",
1210 "/etc/hostname",
1211 "/etc/machine-id",
cd320749
NR
1212 "/etc/kernel/cmdline",
1213 "/etc/localtime") {
05eb2c60
DDM
1214 r = reset_one(rfd, p);
1215 if (r < 0)
1216 return r;
1217 }
1218
1219 return 0;
1220}
1221
37ec0fdd
LP
1222static int help(void) {
1223 _cleanup_free_ char *link = NULL;
1224 int r;
1225
1226 r = terminal_urlify_man("systemd-firstboot", "1", &link);
1227 if (r < 0)
1228 return log_oom();
1229
418b9be5
LP
1230 printf("%s [OPTIONS...]\n\n"
1231 "Configures basic settings of the system.\n\n"
e16793ee
ZJS
1232 " -h --help Show this help\n"
1233 " --version Show package version\n"
1234 " --root=PATH Operate on an alternate filesystem root\n"
84be0c71
LP
1235 " --image=PATH Operate on disk image as filesystem root\n"
1236 " --image-policy=POLICY Specify disk image dissection policy\n"
e16793ee
ZJS
1237 " --locale=LOCALE Set primary locale (LANG=)\n"
1238 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
1239 " --keymap=KEYMAP Set keymap\n"
1240 " --timezone=TIMEZONE Set timezone\n"
1241 " --hostname=NAME Set hostname\n"
fd6ee7ed 1242 " --setup-machine-id Set a random machine ID\n"
981644ed 1243 " --machine-id=ID Set specified machine ID\n"
e16793ee
ZJS
1244 " --root-password=PASSWORD Set root password from plaintext password\n"
1245 " --root-password-file=FILE Set root password from file\n"
1246 " --root-password-hashed=HASH Set root password from hashed password\n"
1247 " --root-shell=SHELL Set root shell\n"
981644ed
AAF
1248 " --kernel-command-line=CMDLINE\n"
1249 " Set kernel command line\n"
e16793ee
ZJS
1250 " --prompt-locale Prompt the user for locale settings\n"
1251 " --prompt-keymap Prompt the user for keymap settings\n"
1252 " --prompt-timezone Prompt the user for timezone\n"
1253 " --prompt-hostname Prompt the user for hostname\n"
1254 " --prompt-root-password Prompt the user for root password\n"
1255 " --prompt-root-shell Prompt the user for root shell\n"
1256 " --prompt Prompt for all of the above\n"
1257 " --copy-locale Copy locale from host\n"
1258 " --copy-keymap Copy keymap from host\n"
1259 " --copy-timezone Copy timezone from host\n"
1260 " --copy-root-password Copy root password from host\n"
1261 " --copy-root-shell Copy root shell from host\n"
1262 " --copy Copy locale, keymap, timezone, root password\n"
e16793ee
ZJS
1263 " --force Overwrite existing files\n"
1264 " --delete-root-password Delete root password\n"
1265 " --welcome=no Disable the welcome text\n"
05eb2c60 1266 " --reset Remove existing files\n"
bc556335
DDM
1267 "\nSee the %s for details.\n",
1268 program_invocation_short_name,
1269 link);
37ec0fdd
LP
1270
1271 return 0;
418b9be5
LP
1272}
1273
1274static int parse_argv(int argc, char *argv[]) {
1275
1276 enum {
1277 ARG_VERSION = 0x100,
1278 ARG_ROOT,
3ff9fa59 1279 ARG_IMAGE,
06e78680 1280 ARG_IMAGE_POLICY,
418b9be5
LP
1281 ARG_LOCALE,
1282 ARG_LOCALE_MESSAGES,
ed457f13 1283 ARG_KEYMAP,
418b9be5
LP
1284 ARG_TIMEZONE,
1285 ARG_HOSTNAME,
fd6ee7ed 1286 ARG_SETUP_MACHINE_ID,
418b9be5
LP
1287 ARG_MACHINE_ID,
1288 ARG_ROOT_PASSWORD,
1289 ARG_ROOT_PASSWORD_FILE,
676339a1 1290 ARG_ROOT_PASSWORD_HASHED,
28900a1b 1291 ARG_ROOT_SHELL,
a5925354 1292 ARG_KERNEL_COMMAND_LINE,
418b9be5
LP
1293 ARG_PROMPT,
1294 ARG_PROMPT_LOCALE,
ed457f13 1295 ARG_PROMPT_KEYMAP,
418b9be5
LP
1296 ARG_PROMPT_TIMEZONE,
1297 ARG_PROMPT_HOSTNAME,
1298 ARG_PROMPT_ROOT_PASSWORD,
28900a1b 1299 ARG_PROMPT_ROOT_SHELL,
418b9be5
LP
1300 ARG_COPY,
1301 ARG_COPY_LOCALE,
ed457f13 1302 ARG_COPY_KEYMAP,
418b9be5
LP
1303 ARG_COPY_TIMEZONE,
1304 ARG_COPY_ROOT_PASSWORD,
28900a1b 1305 ARG_COPY_ROOT_SHELL,
b4909a3f 1306 ARG_FORCE,
4926ceaf 1307 ARG_DELETE_ROOT_PASSWORD,
a1225020 1308 ARG_WELCOME,
05eb2c60 1309 ARG_RESET,
418b9be5
LP
1310 };
1311
1312 static const struct option options[] = {
676339a1
DDM
1313 { "help", no_argument, NULL, 'h' },
1314 { "version", no_argument, NULL, ARG_VERSION },
1315 { "root", required_argument, NULL, ARG_ROOT },
3ff9fa59 1316 { "image", required_argument, NULL, ARG_IMAGE },
06e78680 1317 { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY },
676339a1
DDM
1318 { "locale", required_argument, NULL, ARG_LOCALE },
1319 { "locale-messages", required_argument, NULL, ARG_LOCALE_MESSAGES },
1320 { "keymap", required_argument, NULL, ARG_KEYMAP },
1321 { "timezone", required_argument, NULL, ARG_TIMEZONE },
1322 { "hostname", required_argument, NULL, ARG_HOSTNAME },
fd6ee7ed 1323 { "setup-machine-id", no_argument, NULL, ARG_SETUP_MACHINE_ID },
676339a1
DDM
1324 { "machine-id", required_argument, NULL, ARG_MACHINE_ID },
1325 { "root-password", required_argument, NULL, ARG_ROOT_PASSWORD },
1326 { "root-password-file", required_argument, NULL, ARG_ROOT_PASSWORD_FILE },
1327 { "root-password-hashed", required_argument, NULL, ARG_ROOT_PASSWORD_HASHED },
28900a1b 1328 { "root-shell", required_argument, NULL, ARG_ROOT_SHELL },
a5925354 1329 { "kernel-command-line", required_argument, NULL, ARG_KERNEL_COMMAND_LINE },
676339a1
DDM
1330 { "prompt", no_argument, NULL, ARG_PROMPT },
1331 { "prompt-locale", no_argument, NULL, ARG_PROMPT_LOCALE },
1332 { "prompt-keymap", no_argument, NULL, ARG_PROMPT_KEYMAP },
1333 { "prompt-timezone", no_argument, NULL, ARG_PROMPT_TIMEZONE },
1334 { "prompt-hostname", no_argument, NULL, ARG_PROMPT_HOSTNAME },
1335 { "prompt-root-password", no_argument, NULL, ARG_PROMPT_ROOT_PASSWORD },
28900a1b 1336 { "prompt-root-shell", no_argument, NULL, ARG_PROMPT_ROOT_SHELL },
676339a1
DDM
1337 { "copy", no_argument, NULL, ARG_COPY },
1338 { "copy-locale", no_argument, NULL, ARG_COPY_LOCALE },
1339 { "copy-keymap", no_argument, NULL, ARG_COPY_KEYMAP },
1340 { "copy-timezone", no_argument, NULL, ARG_COPY_TIMEZONE },
1341 { "copy-root-password", no_argument, NULL, ARG_COPY_ROOT_PASSWORD },
28900a1b 1342 { "copy-root-shell", no_argument, NULL, ARG_COPY_ROOT_SHELL },
676339a1
DDM
1343 { "force", no_argument, NULL, ARG_FORCE },
1344 { "delete-root-password", no_argument, NULL, ARG_DELETE_ROOT_PASSWORD },
a1225020 1345 { "welcome", required_argument, NULL, ARG_WELCOME },
05eb2c60 1346 { "reset", no_argument, NULL, ARG_RESET },
418b9be5
LP
1347 {}
1348 };
1349
1350 int r, c;
1351
1352 assert(argc >= 0);
1353 assert(argv);
1354
601185b4 1355 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
418b9be5
LP
1356
1357 switch (c) {
1358
1359 case 'h':
37ec0fdd 1360 return help();
418b9be5
LP
1361
1362 case ARG_VERSION:
3f6fd1ba 1363 return version();
418b9be5
LP
1364
1365 case ARG_ROOT:
614b022c 1366 r = parse_path_argument(optarg, true, &arg_root);
0f474365 1367 if (r < 0)
0f03c2a4 1368 return r;
418b9be5
LP
1369 break;
1370
3ff9fa59 1371 case ARG_IMAGE:
614b022c 1372 r = parse_path_argument(optarg, false, &arg_image);
3ff9fa59
LP
1373 if (r < 0)
1374 return r;
1375 break;
1376
06e78680
YW
1377 case ARG_IMAGE_POLICY:
1378 r = parse_image_policy_argument(optarg, &arg_image_policy);
1379 if (r < 0)
1380 return r;
1381 break;
1382
418b9be5 1383 case ARG_LOCALE:
2fc09a9c
DM
1384 r = free_and_strdup(&arg_locale, optarg);
1385 if (r < 0)
418b9be5
LP
1386 return log_oom();
1387
1388 break;
1389
1390 case ARG_LOCALE_MESSAGES:
2fc09a9c
DM
1391 r = free_and_strdup(&arg_locale_messages, optarg);
1392 if (r < 0)
418b9be5
LP
1393 return log_oom();
1394
1395 break;
1396
ed457f13 1397 case ARG_KEYMAP:
baaa35ad
ZJS
1398 if (!keymap_is_valid(optarg))
1399 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1400 "Keymap %s is not valid.", optarg);
ed457f13
TB
1401
1402 r = free_and_strdup(&arg_keymap, optarg);
1403 if (r < 0)
1404 return log_oom();
1405
1406 break;
1407
418b9be5 1408 case ARG_TIMEZONE:
baaa35ad
ZJS
1409 if (!timezone_is_valid(optarg, LOG_ERR))
1410 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1411 "Timezone %s is not valid.", optarg);
418b9be5 1412
2fc09a9c
DM
1413 r = free_and_strdup(&arg_timezone, optarg);
1414 if (r < 0)
418b9be5
LP
1415 return log_oom();
1416
1417 break;
1418
1419 case ARG_ROOT_PASSWORD:
2fc09a9c
DM
1420 r = free_and_strdup(&arg_root_password, optarg);
1421 if (r < 0)
418b9be5 1422 return log_oom();
676339a1
DDM
1423
1424 arg_root_password_is_hashed = false;
418b9be5
LP
1425 break;
1426
1427 case ARG_ROOT_PASSWORD_FILE:
0da16248 1428 arg_root_password = mfree(arg_root_password);
418b9be5
LP
1429
1430 r = read_one_line_file(optarg, &arg_root_password);
23bbb0de
MS
1431 if (r < 0)
1432 return log_error_errno(r, "Failed to read %s: %m", optarg);
418b9be5 1433
676339a1
DDM
1434 arg_root_password_is_hashed = false;
1435 break;
1436
1437 case ARG_ROOT_PASSWORD_HASHED:
1438 r = free_and_strdup(&arg_root_password, optarg);
1439 if (r < 0)
1440 return log_oom();
1441
1442 arg_root_password_is_hashed = true;
418b9be5
LP
1443 break;
1444
28900a1b 1445 case ARG_ROOT_SHELL:
28900a1b
DDM
1446 r = free_and_strdup(&arg_root_shell, optarg);
1447 if (r < 0)
1448 return log_oom();
1449
1450 break;
1451
418b9be5 1452 case ARG_HOSTNAME:
52ef5dd7 1453 if (!hostname_is_valid(optarg, VALID_HOSTNAME_TRAILING_DOT))
baaa35ad
ZJS
1454 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1455 "Host name %s is not valid.", optarg);
418b9be5 1456
2fc09a9c
DM
1457 r = free_and_strdup(&arg_hostname, optarg);
1458 if (r < 0)
418b9be5
LP
1459 return log_oom();
1460
79485fc2 1461 hostname_cleanup(arg_hostname);
418b9be5
LP
1462 break;
1463
fd6ee7ed
ZJS
1464 case ARG_SETUP_MACHINE_ID:
1465 r = sd_id128_randomize(&arg_machine_id);
1466 if (r < 0)
1467 return log_error_errno(r, "Failed to generate randomized machine ID: %m");
1468
1469 break;
1470
418b9be5 1471 case ARG_MACHINE_ID:
7fb1d980
YW
1472 r = sd_id128_from_string(optarg, &arg_machine_id);
1473 if (r < 0)
1474 return log_error_errno(r, "Failed to parse machine id %s.", optarg);
418b9be5
LP
1475
1476 break;
1477
a5925354
DDM
1478 case ARG_KERNEL_COMMAND_LINE:
1479 r = free_and_strdup(&arg_kernel_cmdline, optarg);
1480 if (r < 0)
1481 return log_oom();
1482
1483 break;
1484
418b9be5 1485 case ARG_PROMPT:
28900a1b
DDM
1486 arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname =
1487 arg_prompt_root_password = arg_prompt_root_shell = true;
418b9be5
LP
1488 break;
1489
1490 case ARG_PROMPT_LOCALE:
1491 arg_prompt_locale = true;
1492 break;
1493
ed457f13
TB
1494 case ARG_PROMPT_KEYMAP:
1495 arg_prompt_keymap = true;
1496 break;
1497
418b9be5
LP
1498 case ARG_PROMPT_TIMEZONE:
1499 arg_prompt_timezone = true;
1500 break;
1501
1502 case ARG_PROMPT_HOSTNAME:
1503 arg_prompt_hostname = true;
1504 break;
1505
1506 case ARG_PROMPT_ROOT_PASSWORD:
1507 arg_prompt_root_password = true;
1508 break;
1509
28900a1b
DDM
1510 case ARG_PROMPT_ROOT_SHELL:
1511 arg_prompt_root_shell = true;
1512 break;
1513
418b9be5 1514 case ARG_COPY:
28900a1b
DDM
1515 arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password =
1516 arg_copy_root_shell = true;
e926f647 1517 break;
418b9be5
LP
1518
1519 case ARG_COPY_LOCALE:
1520 arg_copy_locale = true;
1521 break;
1522
ed457f13
TB
1523 case ARG_COPY_KEYMAP:
1524 arg_copy_keymap = true;
1525 break;
1526
418b9be5
LP
1527 case ARG_COPY_TIMEZONE:
1528 arg_copy_timezone = true;
1529 break;
1530
1531 case ARG_COPY_ROOT_PASSWORD:
1532 arg_copy_root_password = true;
1533 break;
1534
28900a1b
DDM
1535 case ARG_COPY_ROOT_SHELL:
1536 arg_copy_root_shell = true;
1537 break;
1538
b4909a3f
DDM
1539 case ARG_FORCE:
1540 arg_force = true;
1541 break;
1542
4926ceaf
DDM
1543 case ARG_DELETE_ROOT_PASSWORD:
1544 arg_delete_root_password = true;
1545 break;
1546
a1225020
LP
1547 case ARG_WELCOME:
1548 r = parse_boolean(optarg);
1549 if (r < 0)
1550 return log_error_errno(r, "Failed to parse --welcome= argument: %s", optarg);
1551
1552 arg_welcome = r;
1553 break;
1554
05eb2c60
DDM
1555 case ARG_RESET:
1556 arg_reset = true;
1557 break;
1558
418b9be5
LP
1559 case '?':
1560 return -EINVAL;
1561
1562 default:
04499a70 1563 assert_not_reached();
418b9be5 1564 }
418b9be5 1565
4926ceaf
DDM
1566 if (arg_delete_root_password && (arg_copy_root_password || arg_root_password || arg_prompt_root_password))
1567 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
fd6ee7ed 1568 "--delete-root-password cannot be combined with other root password options.");
4926ceaf 1569
3ff9fa59 1570 if (arg_image && arg_root)
fd6ee7ed
ZJS
1571 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1572 "--root= and --image= cannot be used together.");
1573
1574 if (!sd_id128_is_null(arg_machine_id) && !(arg_image || arg_root) && !arg_force)
1575 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1576 "--machine-id=/--setup-machine-id only works with --root= or --image=.");
3ff9fa59 1577
418b9be5
LP
1578 return 1;
1579}
1580
d0c50d8d 1581static int reload_system_manager(sd_bus **bus) {
d0c50d8d
ZJS
1582 int r;
1583
1584 assert(bus);
1585
1586 if (!*bus) {
1587 r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
1588 if (r < 0)
1589 return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL);
1590 }
1591
a9399358 1592 r = bus_service_manager_reload(*bus);
d0c50d8d 1593 if (r < 0)
a9399358
LP
1594 return r;
1595
d0c50d8d
ZJS
1596 log_info("Requested manager reload to apply locale configuration.");
1597 return 0;
1598}
1599
cea32691
ZJS
1600static int reload_vconsole(sd_bus **bus) {
1601 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
8eb668b9
ZJS
1602 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1603 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
1604 const char *object;
cea32691
ZJS
1605 int r;
1606
1607 assert(bus);
1608
1609 if (!*bus) {
1610 r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
1611 if (r < 0)
1612 return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL);
1613 }
1614
8eb668b9
ZJS
1615 r = bus_wait_for_jobs_new(*bus, &w);
1616 if (r < 0)
1617 return log_error_errno(r, "Could not watch jobs: %m");
1618
1619 r = bus_call_method(*bus, bus_systemd_mgr, "RestartUnit", &error, &reply,
1620 "ss", "systemd-vconsole-setup.service", "replace");
cea32691
ZJS
1621 if (r < 0)
1622 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
8eb668b9
ZJS
1623
1624 r = sd_bus_message_read(reply, "o", &object);
1625 if (r < 0)
1626 return bus_log_parse_error(r);
1627
e22ad53d 1628 r = bus_wait_for_jobs_one(w, object, BUS_WAIT_JOBS_LOG_ERROR, NULL);
8eb668b9
ZJS
1629 if (r < 0)
1630 return log_error_errno(r, "Failed to wait for systemd-vconsole-setup.service/restart: %m");
cea32691
ZJS
1631 return 0;
1632}
1633
45f39418 1634static int run(int argc, char *argv[]) {
cea32691 1635 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
3ff9fa59 1636 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
a4b3e942 1637 _cleanup_(umount_and_freep) char *mounted_dir = NULL;
b352e545 1638 _cleanup_close_ int rfd = -EBADF;
418b9be5
LP
1639 int r;
1640
1641 r = parse_argv(argc, argv);
1642 if (r <= 0)
45f39418 1643 return r;
418b9be5 1644
d2acb93d 1645 log_setup();
418b9be5
LP
1646
1647 umask(0022);
1648
cea32691 1649 bool offline = arg_root || arg_image;
3ff9fa59 1650
cea32691 1651 if (!offline) {
3ff9fa59
LP
1652 /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1653 * command line option, because we are called to provision the host with basic settings (as
1654 * opposed to some other file system tree/image) */
1655
cea32691 1656 bool enabled;
3787934b 1657 r = proc_cmdline_get_bool("systemd.firstboot", /* flags = */ 0, &enabled);
3ff9fa59
LP
1658 if (r < 0)
1659 return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
eb650ffe 1660 if (r > 0 && !enabled) {
0a9c4a10
LP
1661 log_debug("Found systemd.firstboot=no kernel command line argument, turning off all prompts.");
1662 arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname = arg_prompt_root_password = arg_prompt_root_shell = false;
eb650ffe 1663 }
3ff9fa59
LP
1664 }
1665
6aa05ebd
LP
1666 if (arg_image) {
1667 assert(!arg_root);
1668
1669 r = mount_image_privately_interactively(
1670 arg_image,
84be0c71 1671 arg_image_policy,
4b5de5dd
LP
1672 DISSECT_IMAGE_GENERIC_ROOT |
1673 DISSECT_IMAGE_REQUIRE_ROOT |
1674 DISSECT_IMAGE_VALIDATE_OS |
1675 DISSECT_IMAGE_RELAX_VAR_CHECK |
c65f854a
LP
1676 DISSECT_IMAGE_FSCK |
1677 DISSECT_IMAGE_GROWFS,
a4b3e942 1678 &mounted_dir,
b352e545 1679 &rfd,
e330f97a 1680 &loop_device);
6aa05ebd
LP
1681 if (r < 0)
1682 return r;
1683
a4b3e942 1684 arg_root = strdup(mounted_dir);
6aa05ebd
LP
1685 if (!arg_root)
1686 return log_oom();
b352e545
DDM
1687 } else {
1688 rfd = open(empty_to_root(arg_root), O_DIRECTORY|O_CLOEXEC);
1689 if (rfd < 0)
1690 return log_error_errno(errno, "Failed to open %s: %m", empty_to_root(arg_root));
1691 }
1692
1693 LOG_SET_PREFIX(arg_image ?: arg_root);
1694
a0657479
DDM
1695 /* We check these conditions here instead of in parse_argv() so that we can take the root directory
1696 * into account. */
1697
1698 if (arg_locale && !locale_is_ok(rfd, arg_locale))
1699 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale);
1700 if (arg_locale_messages && !locale_is_ok(rfd, arg_locale_messages))
1701 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale_messages);
1702
b352e545
DDM
1703 if (arg_root_shell) {
1704 r = find_shell(rfd, arg_root_shell);
1705 if (r < 0)
1706 return r;
6aa05ebd 1707 }
f582cbca 1708
05eb2c60
DDM
1709 r = process_reset(rfd);
1710 if (r < 0)
1711 return r;
1712
b352e545 1713 r = process_locale(rfd);
418b9be5 1714 if (r < 0)
45f39418 1715 return r;
d0c50d8d
ZJS
1716 if (r > 0 && !offline)
1717 (void) reload_system_manager(&bus);
418b9be5 1718
b352e545 1719 r = process_keymap(rfd);
ed457f13 1720 if (r < 0)
45f39418 1721 return r;
cea32691
ZJS
1722 if (r > 0 && !offline)
1723 (void) reload_vconsole(&bus);
ed457f13 1724
b352e545 1725 r = process_timezone(rfd);
418b9be5 1726 if (r < 0)
45f39418 1727 return r;
418b9be5 1728
b352e545 1729 r = process_hostname(rfd);
418b9be5 1730 if (r < 0)
45f39418 1731 return r;
418b9be5 1732
b352e545 1733 r = process_machine_id(rfd);
418b9be5 1734 if (r < 0)
45f39418 1735 return r;
418b9be5 1736
b352e545 1737 r = process_root_account(rfd);
418b9be5 1738 if (r < 0)
45f39418
YW
1739 return r;
1740
b352e545 1741 r = process_kernel_cmdline(rfd);
a5925354
DDM
1742 if (r < 0)
1743 return r;
1744
45f39418 1745 return 0;
418b9be5 1746}
45f39418
YW
1747
1748DEFINE_MAIN_FUNCTION(run);