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