]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/firstboot/firstboot.c
Merge pull request #16981 from keszybz/use-crypt_ra
[thirdparty/systemd.git] / src / firstboot / firstboot.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
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"
418b9be5 12#include "copy.h"
3ff9fa59 13#include "dissect-image.h"
686d13b9 14#include "env-file.h"
6bedfcbb 15#include "fd-util.h"
3f6fd1ba 16#include "fileio.h"
f4f15635 17#include "fs-util.h"
3f6fd1ba 18#include "hostname-util.h"
f05e1d0d 19#include "kbd-util.h"
42f3b2f9 20#include "libcrypt-util.h"
3f6fd1ba 21#include "locale-util.h"
45f39418 22#include "main-func.h"
9ae4ef49 23#include "memory-util.h"
418b9be5 24#include "mkdir.h"
3ff9fa59 25#include "mount-util.h"
d58ad743 26#include "os-util.h"
6bedfcbb 27#include "parse-util.h"
418b9be5 28#include "path-util.h"
294bf0c3 29#include "pretty-print.h"
f582cbca 30#include "proc-cmdline.h"
7baf10a7 31#include "pwquality-util.h"
3df3e884 32#include "random-util.h"
6bedfcbb 33#include "string-util.h"
3f6fd1ba 34#include "strv.h"
288a74cc 35#include "terminal-util.h"
3f6fd1ba 36#include "time-util.h"
b4909a3f 37#include "tmpfile-util-label.h"
3ff9fa59 38#include "tmpfile-util.h"
affb60b1 39#include "umask-util.h"
e929bee0 40#include "user-util.h"
418b9be5
LP
41
42static char *arg_root = NULL;
3ff9fa59 43static char *arg_image = NULL;
418b9be5 44static char *arg_locale = NULL; /* $LANG */
ed457f13 45static char *arg_keymap = NULL;
418b9be5
LP
46static char *arg_locale_messages = NULL; /* $LC_MESSAGES */
47static char *arg_timezone = NULL;
48static char *arg_hostname = NULL;
49static sd_id128_t arg_machine_id = {};
50static char *arg_root_password = NULL;
28900a1b 51static char *arg_root_shell = NULL;
a5925354 52static char *arg_kernel_cmdline = NULL;
418b9be5 53static bool arg_prompt_locale = false;
ed457f13 54static bool arg_prompt_keymap = false;
418b9be5
LP
55static bool arg_prompt_timezone = false;
56static bool arg_prompt_hostname = false;
57static bool arg_prompt_root_password = false;
28900a1b 58static bool arg_prompt_root_shell = false;
418b9be5 59static bool arg_copy_locale = false;
ed457f13 60static bool arg_copy_keymap = false;
418b9be5
LP
61static bool arg_copy_timezone = false;
62static bool arg_copy_root_password = false;
28900a1b 63static bool arg_copy_root_shell = false;
b4909a3f 64static bool arg_force = false;
4926ceaf 65static bool arg_delete_root_password = false;
676339a1 66static bool arg_root_password_is_hashed = false;
a1225020 67static bool arg_welcome = true;
418b9be5 68
45f39418 69STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
3ff9fa59 70STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
45f39418
YW
71STATIC_DESTRUCTOR_REGISTER(arg_locale, freep);
72STATIC_DESTRUCTOR_REGISTER(arg_locale_messages, freep);
73STATIC_DESTRUCTOR_REGISTER(arg_keymap, freep);
74STATIC_DESTRUCTOR_REGISTER(arg_timezone, freep);
75STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep);
9ae4ef49 76STATIC_DESTRUCTOR_REGISTER(arg_root_password, erase_and_freep);
45f39418 77
418b9be5
LP
78static bool press_any_key(void) {
79 char k = 0;
80 bool need_nl = true;
81
82 printf("-- Press any key to proceed --");
83 fflush(stdout);
84
94956f8f 85 (void) read_one_char(stdin, &k, USEC_INFINITY, &need_nl);
418b9be5
LP
86
87 if (need_nl)
88 putchar('\n');
89
90 return k != 'q';
91}
92
93static void print_welcome(void) {
4aeadec7 94 _cleanup_free_ char *pretty_name = NULL, *ansi_color = NULL;
418b9be5 95 static bool done = false;
ae0d36c1 96 const char *pn, *ac;
418b9be5
LP
97 int r;
98
a1225020
LP
99 if (!arg_welcome)
100 return;
101
418b9be5
LP
102 if (done)
103 return;
104
4aeadec7
LP
105 r = parse_os_release(
106 arg_root,
107 "PRETTY_NAME", &pretty_name,
108 "ANSI_COLOR", &ansi_color,
109 NULL);
d58ad743
LP
110 if (r < 0)
111 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
112 "Failed to read os-release file, ignoring: %m");
418b9be5 113
4aeadec7 114 pn = isempty(pretty_name) ? "Linux" : pretty_name;
ae0d36c1 115 ac = isempty(ansi_color) ? "0" : ansi_color;
4aeadec7
LP
116
117 if (colors_enabled())
ae0d36c1 118 printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ac, pn);
4aeadec7
LP
119 else
120 printf("\nWelcome to your new installation of %s!\n", pn);
121
122 printf("\nPlease configure your system!\n\n");
418b9be5
LP
123
124 press_any_key();
125
126 done = true;
127}
128
129static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) {
418b9be5 130 unsigned break_lines, break_modulo;
da6053d0 131 size_t n, per_column, i, j;
418b9be5
LP
132
133 assert(n_columns > 0);
134
135 n = strv_length(x);
be6b0c21 136 per_column = DIV_ROUND_UP(n, n_columns);
418b9be5
LP
137
138 break_lines = lines();
139 if (break_lines > 2)
140 break_lines--;
141
142 /* The first page gets two extra lines, since we want to show
143 * a title */
144 break_modulo = break_lines;
145 if (break_modulo > 3)
146 break_modulo -= 3;
147
148 for (i = 0; i < per_column; i++) {
149
150 for (j = 0; j < n_columns; j ++) {
151 _cleanup_free_ char *e = NULL;
152
153 if (j * per_column + i >= n)
154 break;
155
156 e = ellipsize(x[j * per_column + i], width, percentage);
157 if (!e)
158 return log_oom();
159
da6053d0 160 printf("%4zu) %-*s", j * per_column + i + 1, width, e);
418b9be5
LP
161 }
162
163 putchar('\n');
164
165 /* on the first screen we reserve 2 extra lines for the title */
166 if (i % break_lines == break_modulo) {
167 if (!press_any_key())
168 return 0;
169 }
170 }
171
172 return 0;
173}
174
ecada8f2 175static int prompt_loop(const char *text, char **l, unsigned percentage, bool (*is_valid)(const char *name), char **ret) {
418b9be5
LP
176 int r;
177
178 assert(text);
179 assert(is_valid);
180 assert(ret);
181
182 for (;;) {
183 _cleanup_free_ char *p = NULL;
184 unsigned u;
185
ecada8f2
ZJS
186 r = ask_string(&p, "%s %s (empty to skip, \"list\" to list options): ",
187 special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), text);
23bbb0de
MS
188 if (r < 0)
189 return log_error_errno(r, "Failed to query user: %m");
418b9be5
LP
190
191 if (isempty(p)) {
192 log_warning("No data entered, skipping.");
193 return 0;
194 }
195
ecada8f2
ZJS
196 if (streq(p, "list")) {
197 r = show_menu(l, 3, 22, percentage);
198 if (r < 0)
199 return r;
200
201 putchar('\n');
202 continue;
203 };
204
418b9be5
LP
205 r = safe_atou(p, &u);
206 if (r >= 0) {
418b9be5
LP
207 if (u <= 0 || u > strv_length(l)) {
208 log_error("Specified entry number out of range.");
209 continue;
210 }
211
212 log_info("Selected '%s'.", l[u-1]);
bfbf5f74 213 if (free_and_strdup(ret, l[u-1]) < 0)
418b9be5
LP
214 return log_oom();
215
418b9be5
LP
216 return 0;
217 }
218
219 if (!is_valid(p)) {
220 log_error("Entered data invalid.");
221 continue;
222 }
223
bfbf5f74 224 return free_and_replace(*ret, p);
418b9be5
LP
225 }
226}
227
a00a78b8
LP
228static bool locale_is_ok(const char *name) {
229
230 if (arg_root)
231 return locale_is_valid(name);
232
233 return locale_is_installed(name) > 0;
234}
235
418b9be5
LP
236static int prompt_locale(void) {
237 _cleanup_strv_free_ char **locales = NULL;
238 int r;
239
240 if (arg_locale || arg_locale_messages)
241 return 0;
242
243 if (!arg_prompt_locale)
244 return 0;
245
246 r = get_locales(&locales);
23bbb0de
MS
247 if (r < 0)
248 return log_error_errno(r, "Cannot query locales list: %m");
418b9be5 249
bdd7cd26
LP
250 if (strv_isempty(locales))
251 log_debug("No locales found, skipping locale selection.");
252 else if (strv_length(locales) == 1) {
418b9be5 253
bdd7cd26
LP
254 if (streq(locales[0], SYSTEMD_DEFAULT_LOCALE))
255 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
256 else {
257 log_debug("Only a single locale available (%s), selecting it as default.", locales[0]);
418b9be5 258
bdd7cd26
LP
259 arg_locale = strdup(locales[0]);
260 if (!arg_locale)
261 return log_oom();
418b9be5 262
bdd7cd26
LP
263 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
264 }
265 } else {
266 print_welcome();
418b9be5 267
ecada8f2 268 r = prompt_loop("Please enter system locale name or number",
a00a78b8 269 locales, 60, locale_is_ok, &arg_locale);
bdd7cd26
LP
270 if (r < 0)
271 return r;
272
273 if (isempty(arg_locale))
274 return 0;
275
ecada8f2 276 r = prompt_loop("Please enter system message locale name or number",
a00a78b8 277 locales, 60, locale_is_ok, &arg_locale_messages);
bdd7cd26
LP
278 if (r < 0)
279 return r;
280
281 /* Suppress the messages setting if it's the same as the main locale anyway */
282 if (streq_ptr(arg_locale, arg_locale_messages))
283 arg_locale_messages = mfree(arg_locale_messages);
284 }
418b9be5
LP
285
286 return 0;
287}
288
289static int process_locale(void) {
290 const char *etc_localeconf;
291 char* locales[3];
292 unsigned i = 0;
293 int r;
294
1d13f648 295 etc_localeconf = prefix_roota(arg_root, "/etc/locale.conf");
b4909a3f 296 if (laccess(etc_localeconf, F_OK) >= 0 && !arg_force)
418b9be5
LP
297 return 0;
298
299 if (arg_copy_locale && arg_root) {
300
22e596d6 301 (void) mkdir_parents(etc_localeconf, 0755);
8a016c74 302 r = copy_file("/etc/locale.conf", etc_localeconf, 0, 0644, 0, 0, COPY_REFLINK);
418b9be5 303 if (r != -ENOENT) {
23bbb0de
MS
304 if (r < 0)
305 return log_error_errno(r, "Failed to copy %s: %m", etc_localeconf);
418b9be5
LP
306
307 log_info("%s copied.", etc_localeconf);
308 return 0;
309 }
310 }
311
312 r = prompt_locale();
313 if (r < 0)
314 return r;
315
316 if (!isempty(arg_locale))
63c372cb 317 locales[i++] = strjoina("LANG=", arg_locale);
418b9be5 318 if (!isempty(arg_locale_messages) && !streq(arg_locale_messages, arg_locale))
63c372cb 319 locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages);
418b9be5
LP
320
321 if (i == 0)
322 return 0;
323
324 locales[i] = NULL;
325
22e596d6 326 (void) mkdir_parents(etc_localeconf, 0755);
418b9be5 327 r = write_env_file(etc_localeconf, locales);
23bbb0de
MS
328 if (r < 0)
329 return log_error_errno(r, "Failed to write %s: %m", etc_localeconf);
418b9be5
LP
330
331 log_info("%s written.", etc_localeconf);
332 return 0;
333}
334
ed457f13
TB
335static int prompt_keymap(void) {
336 _cleanup_strv_free_ char **kmaps = NULL;
337 int r;
338
339 if (arg_keymap)
340 return 0;
341
342 if (!arg_prompt_keymap)
343 return 0;
344
345 r = get_keymaps(&kmaps);
346 if (r == -ENOENT) /* no keymaps installed */
347 return r;
348 if (r < 0)
349 return log_error_errno(r, "Failed to read keymaps: %m");
350
351 print_welcome();
352
8700b4da 353 return prompt_loop("Please enter system keymap name or number",
ecada8f2 354 kmaps, 60, keymap_is_valid, &arg_keymap);
ed457f13
TB
355}
356
357static int process_keymap(void) {
358 const char *etc_vconsoleconf;
359 char **keymap;
360 int r;
361
362 etc_vconsoleconf = prefix_roota(arg_root, "/etc/vconsole.conf");
b4909a3f 363 if (laccess(etc_vconsoleconf, F_OK) >= 0 && !arg_force)
ed457f13
TB
364 return 0;
365
366 if (arg_copy_keymap && arg_root) {
367
22e596d6 368 (void) mkdir_parents(etc_vconsoleconf, 0755);
8a016c74 369 r = copy_file("/etc/vconsole.conf", etc_vconsoleconf, 0, 0644, 0, 0, COPY_REFLINK);
ed457f13
TB
370 if (r != -ENOENT) {
371 if (r < 0)
372 return log_error_errno(r, "Failed to copy %s: %m", etc_vconsoleconf);
373
374 log_info("%s copied.", etc_vconsoleconf);
375 return 0;
376 }
377 }
378
379 r = prompt_keymap();
380 if (r == -ENOENT)
381 return 0; /* don't fail if no keymaps are installed */
382 if (r < 0)
383 return r;
384
a7353b4d 385 if (isempty(arg_keymap))
ed457f13
TB
386 return 0;
387
a7353b4d
YW
388 keymap = STRV_MAKE(strjoina("KEYMAP=", arg_keymap));
389
390 r = mkdir_parents(etc_vconsoleconf, 0755);
391 if (r < 0)
392 return log_error_errno(r, "Failed to create the parent directory of %s: %m", etc_vconsoleconf);
393
ed457f13
TB
394 r = write_env_file(etc_vconsoleconf, keymap);
395 if (r < 0)
396 return log_error_errno(r, "Failed to write %s: %m", etc_vconsoleconf);
397
398 log_info("%s written.", etc_vconsoleconf);
399 return 0;
400}
401
089fb865
MG
402static bool timezone_is_valid_log_error(const char *name) {
403 return timezone_is_valid(name, LOG_ERR);
404}
405
418b9be5
LP
406static int prompt_timezone(void) {
407 _cleanup_strv_free_ char **zones = NULL;
408 int r;
409
410 if (arg_timezone)
411 return 0;
412
413 if (!arg_prompt_timezone)
414 return 0;
415
416 r = get_timezones(&zones);
23bbb0de
MS
417 if (r < 0)
418 return log_error_errno(r, "Cannot query timezone list: %m");
418b9be5
LP
419
420 print_welcome();
421
ecada8f2
ZJS
422 r = prompt_loop("Please enter timezone name or number",
423 zones, 30, timezone_is_valid_log_error, &arg_timezone);
418b9be5
LP
424 if (r < 0)
425 return r;
426
427 return 0;
428}
429
430static int process_timezone(void) {
431 const char *etc_localtime, *e;
432 int r;
433
1d13f648 434 etc_localtime = prefix_roota(arg_root, "/etc/localtime");
b4909a3f 435 if (laccess(etc_localtime, F_OK) >= 0 && !arg_force)
418b9be5
LP
436 return 0;
437
438 if (arg_copy_timezone && arg_root) {
439 _cleanup_free_ char *p = NULL;
440
441 r = readlink_malloc("/etc/localtime", &p);
442 if (r != -ENOENT) {
23bbb0de
MS
443 if (r < 0)
444 return log_error_errno(r, "Failed to read host timezone: %m");
418b9be5 445
22e596d6 446 (void) mkdir_parents(etc_localtime, 0755);
4a62c710
MS
447 if (symlink(p, etc_localtime) < 0)
448 return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime);
418b9be5
LP
449
450 log_info("%s copied.", etc_localtime);
451 return 0;
452 }
453 }
454
455 r = prompt_timezone();
456 if (r < 0)
457 return r;
458
459 if (isempty(arg_timezone))
460 return 0;
461
63c372cb 462 e = strjoina("../usr/share/zoneinfo/", arg_timezone);
418b9be5 463
22e596d6 464 (void) mkdir_parents(etc_localtime, 0755);
4a62c710
MS
465 if (symlink(e, etc_localtime) < 0)
466 return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime);
418b9be5
LP
467
468 log_info("%s written", etc_localtime);
469 return 0;
470}
471
472static int prompt_hostname(void) {
473 int r;
474
475 if (arg_hostname)
476 return 0;
477
478 if (!arg_prompt_hostname)
479 return 0;
480
481 print_welcome();
482 putchar('\n');
483
484 for (;;) {
485 _cleanup_free_ char *h = NULL;
486
9a6f746f 487 r = ask_string(&h, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET));
23bbb0de
MS
488 if (r < 0)
489 return log_error_errno(r, "Failed to query hostname: %m");
418b9be5
LP
490
491 if (isempty(h)) {
492 log_warning("No hostname entered, skipping.");
493 break;
494 }
495
34ad6090 496 if (!hostname_is_valid(h, true)) {
418b9be5
LP
497 log_error("Specified hostname invalid.");
498 continue;
499 }
500
34ad6090 501 /* Get rid of the trailing dot that we allow, but don't want to see */
ae691c1d 502 arg_hostname = hostname_cleanup(h);
418b9be5
LP
503 h = NULL;
504 break;
505 }
506
507 return 0;
508}
509
510static int process_hostname(void) {
511 const char *etc_hostname;
512 int r;
513
1d13f648 514 etc_hostname = prefix_roota(arg_root, "/etc/hostname");
b4909a3f 515 if (laccess(etc_hostname, F_OK) >= 0 && !arg_force)
418b9be5
LP
516 return 0;
517
518 r = prompt_hostname();
519 if (r < 0)
520 return r;
521
522 if (isempty(arg_hostname))
523 return 0;
524
0675e94a 525 r = write_string_file(etc_hostname, arg_hostname,
b4909a3f
DDM
526 WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755 |
527 (arg_force ? WRITE_STRING_FILE_ATOMIC : 0));
23bbb0de
MS
528 if (r < 0)
529 return log_error_errno(r, "Failed to write %s: %m", etc_hostname);
418b9be5
LP
530
531 log_info("%s written.", etc_hostname);
532 return 0;
533}
534
535static int process_machine_id(void) {
536 const char *etc_machine_id;
537 char id[SD_ID128_STRING_MAX];
538 int r;
539
1d13f648 540 etc_machine_id = prefix_roota(arg_root, "/etc/machine-id");
b4909a3f 541 if (laccess(etc_machine_id, F_OK) >= 0 && !arg_force)
418b9be5
LP
542 return 0;
543
3bbaff3e 544 if (sd_id128_is_null(arg_machine_id))
418b9be5
LP
545 return 0;
546
0675e94a 547 r = write_string_file(etc_machine_id, sd_id128_to_string(arg_machine_id, id),
b4909a3f
DDM
548 WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755 |
549 (arg_force ? WRITE_STRING_FILE_ATOMIC : 0));
23bbb0de
MS
550 if (r < 0)
551 return log_error_errno(r, "Failed to write machine id: %m");
418b9be5
LP
552
553 log_info("%s written.", etc_machine_id);
554 return 0;
555}
556
557static int prompt_root_password(void) {
1fbc95d3 558 const char *msg1, *msg2;
418b9be5
LP
559 int r;
560
561 if (arg_root_password)
562 return 0;
563
564 if (!arg_prompt_root_password)
565 return 0;
566
418b9be5
LP
567 print_welcome();
568 putchar('\n');
569
3619634c
LP
570 msg1 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter a new root password (empty to skip):");
571 msg2 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter new root password again:");
418b9be5 572
7baf10a7
LP
573 suggest_passwords();
574
418b9be5 575 for (;;) {
c7b7d74e 576 _cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
7baf10a7 577 _cleanup_free_ char *error = NULL;
418b9be5 578
daa55720 579 r = ask_password_tty(-1, msg1, NULL, 0, 0, NULL, &a);
23bbb0de
MS
580 if (r < 0)
581 return log_error_errno(r, "Failed to query root password: %m");
39e96f84
ZJS
582 if (strv_length(a) != 1)
583 return log_error_errno(SYNTHETIC_ERRNO(EIO),
584 "Received multiple passwords, where we expected one.");
418b9be5 585
c7b7d74e 586 if (isempty(*a)) {
418b9be5
LP
587 log_warning("No password entered, skipping.");
588 break;
589 }
590
7baf10a7
LP
591 r = quality_check_password(*a, "root", &error);
592 if (r < 0)
593 return log_error_errno(r, "Failed to check quality of password: %m");
594 if (r == 0)
595 log_warning("Password is weak, accepting anyway: %s", error);
596
daa55720 597 r = ask_password_tty(-1, msg2, NULL, 0, 0, NULL, &b);
ab84f5b9 598 if (r < 0)
00843602 599 return log_error_errno(r, "Failed to query root password: %m");
39e96f84
ZJS
600 if (strv_length(b) != 1)
601 return log_error_errno(SYNTHETIC_ERRNO(EIO),
602 "Received multiple passwords, where we expected one.");
418b9be5 603
c7b7d74e 604 if (!streq(*a, *b)) {
418b9be5 605 log_error("Entered passwords did not match, please try again.");
418b9be5
LP
606 continue;
607 }
608
c7b7d74e 609 arg_root_password = TAKE_PTR(*a);
418b9be5
LP
610 break;
611 }
612
613 return 0;
614}
615
31363bd5
DDM
616static int find_shell(const char *path, const char *root) {
617 int r;
618
619 assert(path);
620
621 if (!valid_shell(path))
622 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "%s is not a valid shell", path);
623
624 r = chase_symlinks(path, root, CHASE_PREFIX_ROOT, NULL, NULL);
625 if (r < 0) {
626 const char *p;
627 p = prefix_roota(root, path);
628 return log_error_errno(r, "Failed to resolve shell %s: %m", p);
629 }
630
631 return 0;
632}
633
28900a1b
DDM
634static int prompt_root_shell(void) {
635 int r;
636
637 if (arg_root_shell || !arg_prompt_root_shell)
638 return 0;
639
640 print_welcome();
641 putchar('\n');
642
643 for (;;) {
644 _cleanup_free_ char *s = NULL;
645
646 r = ask_string(&s, "%s Please enter root shell for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET));
647 if (r < 0)
648 return log_error_errno(r, "Failed to query root shell: %m");
649
650 if (isempty(s)) {
651 log_warning("No shell entered, skipping.");
652 break;
653 }
654
31363bd5
DDM
655 r = find_shell(s, arg_root);
656 if (r < 0)
28900a1b 657 continue;
28900a1b
DDM
658
659 arg_root_shell = TAKE_PTR(s);
660 break;
661 }
662
663 return 0;
664}
665
666static int write_root_passwd(const char *passwd_path, const char *password, const char *shell) {
4926ceaf
DDM
667 _cleanup_fclose_ FILE *original = NULL, *passwd = NULL;
668 _cleanup_(unlink_and_freep) char *passwd_tmp = NULL;
669 int r;
670
c4a53ebf
DDM
671 assert(password);
672
4926ceaf
DDM
673 r = fopen_temporary_label("/etc/passwd", passwd_path, &passwd, &passwd_tmp);
674 if (r < 0)
675 return r;
676
677 original = fopen(passwd_path, "re");
678 if (original) {
679 struct passwd *i;
680
681 r = sync_rights(fileno(original), fileno(passwd));
682 if (r < 0)
683 return r;
684
685 while ((r = fgetpwent_sane(original, &i)) > 0) {
686
28900a1b 687 if (streq(i->pw_name, "root")) {
4926ceaf 688 i->pw_passwd = (char *) password;
28900a1b
DDM
689 if (shell)
690 i->pw_shell = (char *) shell;
691 }
4926ceaf
DDM
692
693 r = putpwent_sane(i, passwd);
694 if (r < 0)
695 return r;
696 }
697 if (r < 0)
698 return r;
699
700 } else {
701 struct passwd root = {
702 .pw_name = (char *) "root",
703 .pw_passwd = (char *) password,
704 .pw_uid = 0,
705 .pw_gid = 0,
706 .pw_gecos = (char *) "Super User",
707 .pw_dir = (char *) "/root",
28900a1b 708 .pw_shell = (char *) (shell ?: "/bin/sh"),
4926ceaf
DDM
709 };
710
711 if (errno != ENOENT)
712 return -errno;
713
b226422c 714 r = fchmod(fileno(passwd), 0644);
4926ceaf
DDM
715 if (r < 0)
716 return -errno;
717
718 r = putpwent_sane(&root, passwd);
719 if (r < 0)
720 return r;
721 }
722
723 r = fflush_sync_and_check(passwd);
724 if (r < 0)
725 return r;
726
727 r = rename_and_apply_smack_floor_label(passwd_tmp, passwd_path);
728 if (r < 0)
729 return r;
730
731 return 0;
732}
733
b4909a3f
DDM
734static int write_root_shadow(const char *shadow_path, const char *hashed_password) {
735 _cleanup_fclose_ FILE *original = NULL, *shadow = NULL;
736 _cleanup_(unlink_and_freep) char *shadow_tmp = NULL;
100d5f6e
FB
737 int r;
738
c4a53ebf
DDM
739 assert(hashed_password);
740
b4909a3f
DDM
741 r = fopen_temporary_label("/etc/shadow", shadow_path, &shadow, &shadow_tmp);
742 if (r < 0)
743 return r;
744
745 original = fopen(shadow_path, "re");
746 if (original) {
747 struct spwd *i;
748
749 r = sync_rights(fileno(original), fileno(shadow));
750 if (r < 0)
751 return r;
752
753 while ((r = fgetspent_sane(original, &i)) > 0) {
754
755 if (streq(i->sp_namp, "root")) {
756 i->sp_pwdp = (char *) hashed_password;
757 i->sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
758 }
759
760 r = putspent_sane(i, shadow);
761 if (r < 0)
762 return r;
763 }
764 if (r < 0)
765 return r;
766
767 } else {
768 struct spwd root = {
769 .sp_namp = (char*) "root",
770 .sp_pwdp = (char *) hashed_password,
771 .sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY),
772 .sp_min = -1,
773 .sp_max = -1,
774 .sp_warn = -1,
775 .sp_inact = -1,
776 .sp_expire = -1,
777 .sp_flag = (unsigned long) -1, /* this appears to be what everybody does ... */
778 };
779
780 if (errno != ENOENT)
781 return -errno;
418b9be5 782
b4909a3f
DDM
783 r = fchmod(fileno(shadow), 0000);
784 if (r < 0)
785 return -errno;
418b9be5 786
b4909a3f
DDM
787 r = putspent_sane(&root, shadow);
788 if (r < 0)
789 return r;
790 }
791
792 r = fflush_sync_and_check(shadow);
100d5f6e
FB
793 if (r < 0)
794 return r;
418b9be5 795
b4909a3f
DDM
796 r = rename_and_apply_smack_floor_label(shadow_tmp, shadow_path);
797 if (r < 0)
798 return r;
799
800 return 0;
418b9be5
LP
801}
802
28900a1b 803static int process_root_args(void) {
45035609 804 _cleanup_close_ int lock = -1;
0e98d17e 805 _cleanup_(erase_and_freep) char *_hashed_password = NULL;
c4a53ebf
DDM
806 const char *password, *hashed_password;
807 const char *etc_passwd, *etc_shadow;
418b9be5
LP
808 int r;
809
c4a53ebf 810 etc_passwd = prefix_roota(arg_root, "/etc/passwd");
1d13f648 811 etc_shadow = prefix_roota(arg_root, "/etc/shadow");
c4a53ebf
DDM
812
813 /* We only mess with passwd and shadow if both do not exist or --force is specified. These files are
814 * tightly coupled and hence we make sure we have permission from the user to create/modify both
815 * files. */
816 if ((laccess(etc_passwd, F_OK) >= 0 || laccess(etc_shadow, F_OK) >= 0) && !arg_force)
418b9be5 817 return 0;
67d5d9d5 818 /* Don't create/modify passwd and shadow if not asked */
819 if (!(arg_root_password || arg_prompt_root_password || arg_copy_root_password || arg_delete_root_password ||
820 arg_root_shell || arg_prompt_root_shell || arg_copy_root_shell))
821 return 0;
418b9be5 822
c4a53ebf 823 (void) mkdir_parents(etc_passwd, 0755);
45035609 824
e929bee0 825 lock = take_etc_passwd_lock(arg_root);
45035609 826 if (lock < 0)
c4a53ebf 827 return log_error_errno(lock, "Failed to take a lock on %s: %m", etc_passwd);
4926ceaf 828
28900a1b
DDM
829 if (arg_copy_root_shell && arg_root) {
830 struct passwd *p;
831
832 errno = 0;
833 p = getpwnam("root");
834 if (!p)
835 return log_error_errno(errno_or_else(EIO), "Failed to find passwd entry for root: %m");
836
837 r = free_and_strdup(&arg_root_shell, p->pw_shell);
838 if (r < 0)
839 return log_oom();
840 }
841
842 r = prompt_root_shell();
843 if (r < 0)
844 return r;
845
418b9be5
LP
846 if (arg_copy_root_password && arg_root) {
847 struct spwd *p;
848
849 errno = 0;
850 p = getspnam("root");
c4a53ebf
DDM
851 if (!p)
852 return log_error_errno(errno_or_else(EIO), "Failed to find shadow entry for root: %m");
418b9be5 853
c4a53ebf
DDM
854 r = free_and_strdup(&arg_root_password, p->sp_pwdp);
855 if (r < 0)
856 return log_oom();
418b9be5 857
c4a53ebf 858 arg_root_password_is_hashed = true;
418b9be5
LP
859 }
860
861 r = prompt_root_password();
862 if (r < 0)
863 return r;
864
c4a53ebf
DDM
865 if (arg_root_password && arg_root_password_is_hashed) {
866 password = "x";
676339a1 867 hashed_password = arg_root_password;
c4a53ebf 868 } else if (arg_root_password) {
0e98d17e
ZJS
869 r = hash_password(arg_root_password, &_hashed_password);
870 if (r < 0)
871 return log_error_errno(r, "Failed to hash password: %m");
676339a1 872
c4a53ebf 873 password = "x";
0e98d17e 874 hashed_password = _hashed_password;
c4a53ebf 875
c4a53ebf
DDM
876 } else if (arg_delete_root_password)
877 password = hashed_password = "";
878 else
879 password = hashed_password = "!";
880
28900a1b 881 r = write_root_passwd(etc_passwd, password, arg_root_shell);
c4a53ebf
DDM
882 if (r < 0)
883 return log_error_errno(r, "Failed to write %s: %m", etc_passwd);
884
885 log_info("%s written", etc_passwd);
418b9be5 886
b4909a3f 887 r = write_root_shadow(etc_shadow, hashed_password);
23bbb0de
MS
888 if (r < 0)
889 return log_error_errno(r, "Failed to write %s: %m", etc_shadow);
418b9be5
LP
890
891 log_info("%s written.", etc_shadow);
892 return 0;
893}
894
a5925354
DDM
895static int process_kernel_cmdline(void) {
896 const char *etc_kernel_cmdline;
897 int r;
898
899 etc_kernel_cmdline = prefix_roota(arg_root, "/etc/kernel/cmdline");
900 if (laccess(etc_kernel_cmdline, F_OK) >= 0 && !arg_force)
901 return 0;
902
903 if (!arg_kernel_cmdline)
904 return 0;
905
906 r = write_string_file(etc_kernel_cmdline, arg_kernel_cmdline,
907 WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755 |
908 (arg_force ? WRITE_STRING_FILE_ATOMIC : 0));
909 if (r < 0)
910 return log_error_errno(r, "Failed to write %s: %m", etc_kernel_cmdline);
911
912 log_info("%s written.", etc_kernel_cmdline);
913 return 0;
914}
915
37ec0fdd
LP
916static int help(void) {
917 _cleanup_free_ char *link = NULL;
918 int r;
919
920 r = terminal_urlify_man("systemd-firstboot", "1", &link);
921 if (r < 0)
922 return log_oom();
923
418b9be5
LP
924 printf("%s [OPTIONS...]\n\n"
925 "Configures basic settings of the system.\n\n"
676339a1
DDM
926 " -h --help Show this help\n"
927 " --version Show package version\n"
928 " --root=PATH Operate on an alternate filesystem root\n"
3ff9fa59 929 " --image=PATH Operate on an alternate filesystem image\n"
676339a1
DDM
930 " --locale=LOCALE Set primary locale (LANG=)\n"
931 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
932 " --keymap=KEYMAP Set keymap\n"
933 " --timezone=TIMEZONE Set timezone\n"
934 " --hostname=NAME Set hostname\n"
935 " --machine-ID=ID Set machine ID\n"
936 " --root-password=PASSWORD Set root password from plaintext password\n"
937 " --root-password-file=FILE Set root password from file\n"
938 " --root-password-hashed=HASHED_PASSWORD Set root password from hashed password\n"
f649325b 939 " --root-shell=SHELL Set root shell\n"
676339a1
DDM
940 " --prompt-locale Prompt the user for locale settings\n"
941 " --prompt-keymap Prompt the user for keymap settings\n"
942 " --prompt-timezone Prompt the user for timezone\n"
943 " --prompt-hostname Prompt the user for hostname\n"
944 " --prompt-root-password Prompt the user for root password\n"
f649325b 945 " --prompt-root-shell Prompt the user for root shell\n"
676339a1
DDM
946 " --prompt Prompt for all of the above\n"
947 " --copy-locale Copy locale from host\n"
948 " --copy-keymap Copy keymap from host\n"
949 " --copy-timezone Copy timezone from host\n"
950 " --copy-root-password Copy root password from host\n"
f649325b 951 " --copy-root-shell Copy root shell from host\n"
676339a1
DDM
952 " --copy Copy locale, keymap, timezone, root password\n"
953 " --setup-machine-id Generate a new random machine ID\n"
954 " --force Overwrite existing files\n"
955 " --delete-root-password Delete root password\n"
a1225020 956 " --welcome=no Disable the welcome text\n"
37ec0fdd
LP
957 "\nSee the %s for details.\n"
958 , program_invocation_short_name
959 , link
960 );
961
962 return 0;
418b9be5
LP
963}
964
965static int parse_argv(int argc, char *argv[]) {
966
967 enum {
968 ARG_VERSION = 0x100,
969 ARG_ROOT,
3ff9fa59 970 ARG_IMAGE,
418b9be5
LP
971 ARG_LOCALE,
972 ARG_LOCALE_MESSAGES,
ed457f13 973 ARG_KEYMAP,
418b9be5
LP
974 ARG_TIMEZONE,
975 ARG_HOSTNAME,
976 ARG_MACHINE_ID,
977 ARG_ROOT_PASSWORD,
978 ARG_ROOT_PASSWORD_FILE,
676339a1 979 ARG_ROOT_PASSWORD_HASHED,
28900a1b 980 ARG_ROOT_SHELL,
a5925354 981 ARG_KERNEL_COMMAND_LINE,
418b9be5
LP
982 ARG_PROMPT,
983 ARG_PROMPT_LOCALE,
ed457f13 984 ARG_PROMPT_KEYMAP,
418b9be5
LP
985 ARG_PROMPT_TIMEZONE,
986 ARG_PROMPT_HOSTNAME,
987 ARG_PROMPT_ROOT_PASSWORD,
28900a1b 988 ARG_PROMPT_ROOT_SHELL,
418b9be5
LP
989 ARG_COPY,
990 ARG_COPY_LOCALE,
ed457f13 991 ARG_COPY_KEYMAP,
418b9be5
LP
992 ARG_COPY_TIMEZONE,
993 ARG_COPY_ROOT_PASSWORD,
28900a1b 994 ARG_COPY_ROOT_SHELL,
418b9be5 995 ARG_SETUP_MACHINE_ID,
b4909a3f 996 ARG_FORCE,
4926ceaf 997 ARG_DELETE_ROOT_PASSWORD,
a1225020 998 ARG_WELCOME,
418b9be5
LP
999 };
1000
1001 static const struct option options[] = {
676339a1
DDM
1002 { "help", no_argument, NULL, 'h' },
1003 { "version", no_argument, NULL, ARG_VERSION },
1004 { "root", required_argument, NULL, ARG_ROOT },
3ff9fa59 1005 { "image", required_argument, NULL, ARG_IMAGE },
676339a1
DDM
1006 { "locale", required_argument, NULL, ARG_LOCALE },
1007 { "locale-messages", required_argument, NULL, ARG_LOCALE_MESSAGES },
1008 { "keymap", required_argument, NULL, ARG_KEYMAP },
1009 { "timezone", required_argument, NULL, ARG_TIMEZONE },
1010 { "hostname", required_argument, NULL, ARG_HOSTNAME },
1011 { "machine-id", required_argument, NULL, ARG_MACHINE_ID },
1012 { "root-password", required_argument, NULL, ARG_ROOT_PASSWORD },
1013 { "root-password-file", required_argument, NULL, ARG_ROOT_PASSWORD_FILE },
1014 { "root-password-hashed", required_argument, NULL, ARG_ROOT_PASSWORD_HASHED },
28900a1b 1015 { "root-shell", required_argument, NULL, ARG_ROOT_SHELL },
a5925354 1016 { "kernel-command-line", required_argument, NULL, ARG_KERNEL_COMMAND_LINE },
676339a1
DDM
1017 { "prompt", no_argument, NULL, ARG_PROMPT },
1018 { "prompt-locale", no_argument, NULL, ARG_PROMPT_LOCALE },
1019 { "prompt-keymap", no_argument, NULL, ARG_PROMPT_KEYMAP },
1020 { "prompt-timezone", no_argument, NULL, ARG_PROMPT_TIMEZONE },
1021 { "prompt-hostname", no_argument, NULL, ARG_PROMPT_HOSTNAME },
1022 { "prompt-root-password", no_argument, NULL, ARG_PROMPT_ROOT_PASSWORD },
28900a1b 1023 { "prompt-root-shell", no_argument, NULL, ARG_PROMPT_ROOT_SHELL },
676339a1
DDM
1024 { "copy", no_argument, NULL, ARG_COPY },
1025 { "copy-locale", no_argument, NULL, ARG_COPY_LOCALE },
1026 { "copy-keymap", no_argument, NULL, ARG_COPY_KEYMAP },
1027 { "copy-timezone", no_argument, NULL, ARG_COPY_TIMEZONE },
1028 { "copy-root-password", no_argument, NULL, ARG_COPY_ROOT_PASSWORD },
28900a1b 1029 { "copy-root-shell", no_argument, NULL, ARG_COPY_ROOT_SHELL },
676339a1
DDM
1030 { "setup-machine-id", no_argument, NULL, ARG_SETUP_MACHINE_ID },
1031 { "force", no_argument, NULL, ARG_FORCE },
1032 { "delete-root-password", no_argument, NULL, ARG_DELETE_ROOT_PASSWORD },
a1225020 1033 { "welcome", required_argument, NULL, ARG_WELCOME },
418b9be5
LP
1034 {}
1035 };
1036
1037 int r, c;
1038
1039 assert(argc >= 0);
1040 assert(argv);
1041
601185b4 1042 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
418b9be5
LP
1043
1044 switch (c) {
1045
1046 case 'h':
37ec0fdd 1047 return help();
418b9be5
LP
1048
1049 case ARG_VERSION:
3f6fd1ba 1050 return version();
418b9be5
LP
1051
1052 case ARG_ROOT:
0f03c2a4 1053 r = parse_path_argument_and_warn(optarg, true, &arg_root);
0f474365 1054 if (r < 0)
0f03c2a4 1055 return r;
418b9be5
LP
1056 break;
1057
3ff9fa59
LP
1058 case ARG_IMAGE:
1059 r = parse_path_argument_and_warn(optarg, false, &arg_image);
1060 if (r < 0)
1061 return r;
1062 break;
1063
418b9be5 1064 case ARG_LOCALE:
2fc09a9c
DM
1065 r = free_and_strdup(&arg_locale, optarg);
1066 if (r < 0)
418b9be5
LP
1067 return log_oom();
1068
1069 break;
1070
1071 case ARG_LOCALE_MESSAGES:
2fc09a9c
DM
1072 r = free_and_strdup(&arg_locale_messages, optarg);
1073 if (r < 0)
418b9be5
LP
1074 return log_oom();
1075
1076 break;
1077
ed457f13 1078 case ARG_KEYMAP:
baaa35ad
ZJS
1079 if (!keymap_is_valid(optarg))
1080 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1081 "Keymap %s is not valid.", optarg);
ed457f13
TB
1082
1083 r = free_and_strdup(&arg_keymap, optarg);
1084 if (r < 0)
1085 return log_oom();
1086
1087 break;
1088
418b9be5 1089 case ARG_TIMEZONE:
baaa35ad
ZJS
1090 if (!timezone_is_valid(optarg, LOG_ERR))
1091 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1092 "Timezone %s is not valid.", optarg);
418b9be5 1093
2fc09a9c
DM
1094 r = free_and_strdup(&arg_timezone, optarg);
1095 if (r < 0)
418b9be5
LP
1096 return log_oom();
1097
1098 break;
1099
1100 case ARG_ROOT_PASSWORD:
2fc09a9c
DM
1101 r = free_and_strdup(&arg_root_password, optarg);
1102 if (r < 0)
418b9be5 1103 return log_oom();
676339a1
DDM
1104
1105 arg_root_password_is_hashed = false;
418b9be5
LP
1106 break;
1107
1108 case ARG_ROOT_PASSWORD_FILE:
0da16248 1109 arg_root_password = mfree(arg_root_password);
418b9be5
LP
1110
1111 r = read_one_line_file(optarg, &arg_root_password);
23bbb0de
MS
1112 if (r < 0)
1113 return log_error_errno(r, "Failed to read %s: %m", optarg);
418b9be5 1114
676339a1
DDM
1115 arg_root_password_is_hashed = false;
1116 break;
1117
1118 case ARG_ROOT_PASSWORD_HASHED:
1119 r = free_and_strdup(&arg_root_password, optarg);
1120 if (r < 0)
1121 return log_oom();
1122
1123 arg_root_password_is_hashed = true;
418b9be5
LP
1124 break;
1125
28900a1b 1126 case ARG_ROOT_SHELL:
31363bd5
DDM
1127 r = find_shell(optarg, arg_root);
1128 if (r < 0)
1129 return r;
28900a1b
DDM
1130
1131 r = free_and_strdup(&arg_root_shell, optarg);
1132 if (r < 0)
1133 return log_oom();
1134
1135 break;
1136
418b9be5 1137 case ARG_HOSTNAME:
baaa35ad
ZJS
1138 if (!hostname_is_valid(optarg, true))
1139 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1140 "Host name %s is not valid.", optarg);
418b9be5 1141
ae691c1d 1142 hostname_cleanup(optarg);
2fc09a9c
DM
1143 r = free_and_strdup(&arg_hostname, optarg);
1144 if (r < 0)
418b9be5
LP
1145 return log_oom();
1146
1147 break;
1148
1149 case ARG_MACHINE_ID:
baaa35ad
ZJS
1150 if (sd_id128_from_string(optarg, &arg_machine_id) < 0)
1151 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1152 "Failed to parse machine id %s.", optarg);
418b9be5
LP
1153
1154 break;
1155
a5925354
DDM
1156 case ARG_KERNEL_COMMAND_LINE:
1157 r = free_and_strdup(&arg_kernel_cmdline, optarg);
1158 if (r < 0)
1159 return log_oom();
1160
1161 break;
1162
418b9be5 1163 case ARG_PROMPT:
28900a1b
DDM
1164 arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname =
1165 arg_prompt_root_password = arg_prompt_root_shell = true;
418b9be5
LP
1166 break;
1167
1168 case ARG_PROMPT_LOCALE:
1169 arg_prompt_locale = true;
1170 break;
1171
ed457f13
TB
1172 case ARG_PROMPT_KEYMAP:
1173 arg_prompt_keymap = true;
1174 break;
1175
418b9be5
LP
1176 case ARG_PROMPT_TIMEZONE:
1177 arg_prompt_timezone = true;
1178 break;
1179
1180 case ARG_PROMPT_HOSTNAME:
1181 arg_prompt_hostname = true;
1182 break;
1183
1184 case ARG_PROMPT_ROOT_PASSWORD:
1185 arg_prompt_root_password = true;
1186 break;
1187
28900a1b
DDM
1188 case ARG_PROMPT_ROOT_SHELL:
1189 arg_prompt_root_shell = true;
1190 break;
1191
418b9be5 1192 case ARG_COPY:
28900a1b
DDM
1193 arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password =
1194 arg_copy_root_shell = true;
e926f647 1195 break;
418b9be5
LP
1196
1197 case ARG_COPY_LOCALE:
1198 arg_copy_locale = true;
1199 break;
1200
ed457f13
TB
1201 case ARG_COPY_KEYMAP:
1202 arg_copy_keymap = true;
1203 break;
1204
418b9be5
LP
1205 case ARG_COPY_TIMEZONE:
1206 arg_copy_timezone = true;
1207 break;
1208
1209 case ARG_COPY_ROOT_PASSWORD:
1210 arg_copy_root_password = true;
1211 break;
1212
28900a1b
DDM
1213 case ARG_COPY_ROOT_SHELL:
1214 arg_copy_root_shell = true;
1215 break;
1216
418b9be5 1217 case ARG_SETUP_MACHINE_ID:
418b9be5 1218 r = sd_id128_randomize(&arg_machine_id);
23bbb0de
MS
1219 if (r < 0)
1220 return log_error_errno(r, "Failed to generate randomized machine ID: %m");
418b9be5
LP
1221
1222 break;
1223
b4909a3f
DDM
1224 case ARG_FORCE:
1225 arg_force = true;
1226 break;
1227
4926ceaf
DDM
1228 case ARG_DELETE_ROOT_PASSWORD:
1229 arg_delete_root_password = true;
1230 break;
1231
a1225020
LP
1232 case ARG_WELCOME:
1233 r = parse_boolean(optarg);
1234 if (r < 0)
1235 return log_error_errno(r, "Failed to parse --welcome= argument: %s", optarg);
1236
1237 arg_welcome = r;
1238 break;
1239
418b9be5
LP
1240 case '?':
1241 return -EINVAL;
1242
1243 default:
1244 assert_not_reached("Unhandled option");
1245 }
418b9be5 1246
a00a78b8
LP
1247 /* We check if the specified locale strings are valid down here, so that we can take --root= into
1248 * account when looking for the locale files. */
1249
1250 if (arg_locale && !locale_is_ok(arg_locale))
1251 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale);
1252 if (arg_locale_messages && !locale_is_ok(arg_locale_messages))
1253 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale_messages);
1254
4926ceaf
DDM
1255 if (arg_delete_root_password && (arg_copy_root_password || arg_root_password || arg_prompt_root_password))
1256 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1257 "--delete-root-password cannot be combined with other root password options");
1258
3ff9fa59
LP
1259 if (arg_image && arg_root)
1260 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
1261
418b9be5
LP
1262 return 1;
1263}
1264
45f39418 1265static int run(int argc, char *argv[]) {
3ff9fa59
LP
1266 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
1267 _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL;
1268 _cleanup_(umount_and_rmdir_and_freep) char *unlink_dir = NULL;
418b9be5
LP
1269 int r;
1270
1271 r = parse_argv(argc, argv);
1272 if (r <= 0)
45f39418 1273 return r;
418b9be5 1274
6bf3c61c 1275 log_setup_service();
418b9be5
LP
1276
1277 umask(0022);
1278
3ff9fa59
LP
1279 if (!arg_root && !arg_image) {
1280 bool enabled;
1281
1282 /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1283 * command line option, because we are called to provision the host with basic settings (as
1284 * opposed to some other file system tree/image) */
1285
1286 r = proc_cmdline_get_bool("systemd.firstboot", &enabled);
1287 if (r < 0)
1288 return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
1289 if (r > 0 && !enabled)
1290 return 0; /* disabled */
1291 }
1292
6aa05ebd
LP
1293 if (arg_image) {
1294 assert(!arg_root);
1295
1296 r = mount_image_privately_interactively(
1297 arg_image,
1298 DISSECT_IMAGE_REQUIRE_ROOT|DISSECT_IMAGE_VALIDATE_OS|DISSECT_IMAGE_RELAX_VAR_CHECK|DISSECT_IMAGE_FSCK,
1299 &unlink_dir,
1300 &loop_device,
1301 &decrypted_image);
1302 if (r < 0)
1303 return r;
1304
1305 arg_root = strdup(unlink_dir);
1306 if (!arg_root)
1307 return log_oom();
1308 }
f582cbca 1309
418b9be5
LP
1310 r = process_locale();
1311 if (r < 0)
45f39418 1312 return r;
418b9be5 1313
ed457f13
TB
1314 r = process_keymap();
1315 if (r < 0)
45f39418 1316 return r;
ed457f13 1317
418b9be5
LP
1318 r = process_timezone();
1319 if (r < 0)
45f39418 1320 return r;
418b9be5
LP
1321
1322 r = process_hostname();
1323 if (r < 0)
45f39418 1324 return r;
418b9be5
LP
1325
1326 r = process_machine_id();
1327 if (r < 0)
45f39418 1328 return r;
418b9be5 1329
28900a1b 1330 r = process_root_args();
418b9be5 1331 if (r < 0)
45f39418
YW
1332 return r;
1333
a5925354
DDM
1334 r = process_kernel_cmdline();
1335 if (r < 0)
1336 return r;
1337
45f39418 1338 return 0;
418b9be5 1339}
45f39418
YW
1340
1341DEFINE_MAIN_FUNCTION(run);