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