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