]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/firstboot/firstboot.c
Merge pull request #20303 from andir/sysconfig-example
[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 int r;
570
571 etc_machine_id = prefix_roota(arg_root, "/etc/machine-id");
572 if (laccess(etc_machine_id, F_OK) >= 0 && !arg_force)
573 return 0;
574
575 if (sd_id128_is_null(arg_machine_id))
576 return 0;
577
578 r = write_string_file(etc_machine_id, SD_ID128_TO_STRING(arg_machine_id),
579 WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755 |
580 (arg_force ? WRITE_STRING_FILE_ATOMIC : 0));
581 if (r < 0)
582 return log_error_errno(r, "Failed to write machine id: %m");
583
584 log_info("%s written.", etc_machine_id);
585 return 0;
586 }
587
588 static int prompt_root_password(void) {
589 const char *msg1, *msg2;
590 int r;
591
592 if (arg_root_password)
593 return 0;
594
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
611 if (!arg_prompt_root_password)
612 return 0;
613
614 print_welcome();
615 putchar('\n');
616
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:");
619
620 suggest_passwords();
621
622 for (;;) {
623 _cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
624 _cleanup_free_ char *error = NULL;
625
626 r = ask_password_tty(-1, msg1, NULL, 0, 0, NULL, &a);
627 if (r < 0)
628 return log_error_errno(r, "Failed to query root password: %m");
629 if (strv_length(a) != 1)
630 return log_error_errno(SYNTHETIC_ERRNO(EIO),
631 "Received multiple passwords, where we expected one.");
632
633 if (isempty(*a)) {
634 log_warning("No password entered, skipping.");
635 break;
636 }
637
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
644 r = ask_password_tty(-1, msg2, NULL, 0, 0, NULL, &b);
645 if (r < 0)
646 return log_error_errno(r, "Failed to query root password: %m");
647 if (strv_length(b) != 1)
648 return log_error_errno(SYNTHETIC_ERRNO(EIO),
649 "Received multiple passwords, where we expected one.");
650
651 if (!streq(*a, *b)) {
652 log_error("Entered passwords did not match, please try again.");
653 continue;
654 }
655
656 arg_root_password = TAKE_PTR(*a);
657 break;
658 }
659
660 return 0;
661 }
662
663 static 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
681 static int prompt_root_shell(void) {
682 int r;
683
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)
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
713 r = find_shell(s, arg_root);
714 if (r < 0)
715 continue;
716
717 arg_root_shell = TAKE_PTR(s);
718 break;
719 }
720
721 return 0;
722 }
723
724 static int write_root_passwd(const char *passwd_path, const char *password, const char *shell) {
725 _cleanup_fclose_ FILE *original = NULL, *passwd = NULL;
726 _cleanup_(unlink_and_freep) char *passwd_tmp = NULL;
727 int r;
728
729 assert(password);
730
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
739 r = copy_rights(fileno(original), fileno(passwd));
740 if (r < 0)
741 return r;
742
743 while ((r = fgetpwent_sane(original, &i)) > 0) {
744
745 if (streq(i->pw_name, "root")) {
746 i->pw_passwd = (char *) password;
747 if (shell)
748 i->pw_shell = (char *) shell;
749 }
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",
766 .pw_shell = (char *) (shell ?: "/bin/sh"),
767 };
768
769 if (errno != ENOENT)
770 return -errno;
771
772 r = fchmod(fileno(passwd), 0644);
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
792 static 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;
795 int r;
796
797 assert(hashed_password);
798
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
807 r = copy_rights(fileno(original), fileno(shadow));
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,
835 .sp_flag = ULONG_MAX, /* this appears to be what everybody does ... */
836 };
837
838 if (errno != ENOENT)
839 return -errno;
840
841 r = fchmod(fileno(shadow), 0000);
842 if (r < 0)
843 return -errno;
844
845 r = putspent_sane(&root, shadow);
846 if (r < 0)
847 return r;
848 }
849
850 r = fflush_sync_and_check(shadow);
851 if (r < 0)
852 return r;
853
854 r = rename_and_apply_smack_floor_label(shadow_tmp, shadow_path);
855 if (r < 0)
856 return r;
857
858 return 0;
859 }
860
861 static int process_root_args(void) {
862 _cleanup_close_ int lock = -1;
863 _cleanup_(erase_and_freep) char *_hashed_password = NULL;
864 const char *password, *hashed_password;
865 const char *etc_passwd, *etc_shadow;
866 int r;
867
868 etc_passwd = prefix_roota(arg_root, "/etc/passwd");
869 etc_shadow = prefix_roota(arg_root, "/etc/shadow");
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)
875 return 0;
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;
880
881 (void) mkdir_parents(etc_passwd, 0755);
882
883 lock = take_etc_passwd_lock(arg_root);
884 if (lock < 0)
885 return log_error_errno(lock, "Failed to take a lock on %s: %m", etc_passwd);
886
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
904 if (arg_copy_root_password && arg_root) {
905 struct spwd *p;
906
907 errno = 0;
908 p = getspnam("root");
909 if (!p)
910 return log_error_errno(errno_or_else(EIO), "Failed to find shadow entry for root: %m");
911
912 r = free_and_strdup(&arg_root_password, p->sp_pwdp);
913 if (r < 0)
914 return log_oom();
915
916 arg_root_password_is_hashed = true;
917 }
918
919 r = prompt_root_password();
920 if (r < 0)
921 return r;
922
923 if (arg_root_password && arg_root_password_is_hashed) {
924 password = PASSWORD_SEE_SHADOW;
925 hashed_password = arg_root_password;
926 } else if (arg_root_password) {
927 r = hash_password(arg_root_password, &_hashed_password);
928 if (r < 0)
929 return log_error_errno(r, "Failed to hash password: %m");
930
931 password = PASSWORD_SEE_SHADOW;
932 hashed_password = _hashed_password;
933
934 } else if (arg_delete_root_password)
935 password = hashed_password = PASSWORD_NONE;
936 else
937 password = hashed_password = PASSWORD_LOCKED_AND_INVALID;
938
939 r = write_root_passwd(etc_passwd, password, arg_root_shell);
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);
944
945 r = write_root_shadow(etc_shadow, hashed_password);
946 if (r < 0)
947 return log_error_errno(r, "Failed to write %s: %m", etc_shadow);
948
949 log_info("%s written.", etc_shadow);
950 return 0;
951 }
952
953 static 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
974 static 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
982 printf("%s [OPTIONS...]\n\n"
983 "Configures basic settings of the system.\n\n"
984 " -h --help Show this help\n"
985 " --version Show package version\n"
986 " --root=PATH Operate on an alternate filesystem root\n"
987 " --image=PATH Operate on an alternate filesystem image\n"
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"
997 " --root-shell=SHELL Set root shell\n"
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"
1003 " --prompt-root-shell Prompt the user for root shell\n"
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"
1009 " --copy-root-shell Copy root shell from host\n"
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"
1014 " --welcome=no Disable the welcome text\n"
1015 "\nSee the %s for details.\n",
1016 program_invocation_short_name,
1017 link);
1018
1019 return 0;
1020 }
1021
1022 static int parse_argv(int argc, char *argv[]) {
1023
1024 enum {
1025 ARG_VERSION = 0x100,
1026 ARG_ROOT,
1027 ARG_IMAGE,
1028 ARG_LOCALE,
1029 ARG_LOCALE_MESSAGES,
1030 ARG_KEYMAP,
1031 ARG_TIMEZONE,
1032 ARG_HOSTNAME,
1033 ARG_MACHINE_ID,
1034 ARG_ROOT_PASSWORD,
1035 ARG_ROOT_PASSWORD_FILE,
1036 ARG_ROOT_PASSWORD_HASHED,
1037 ARG_ROOT_SHELL,
1038 ARG_KERNEL_COMMAND_LINE,
1039 ARG_PROMPT,
1040 ARG_PROMPT_LOCALE,
1041 ARG_PROMPT_KEYMAP,
1042 ARG_PROMPT_TIMEZONE,
1043 ARG_PROMPT_HOSTNAME,
1044 ARG_PROMPT_ROOT_PASSWORD,
1045 ARG_PROMPT_ROOT_SHELL,
1046 ARG_COPY,
1047 ARG_COPY_LOCALE,
1048 ARG_COPY_KEYMAP,
1049 ARG_COPY_TIMEZONE,
1050 ARG_COPY_ROOT_PASSWORD,
1051 ARG_COPY_ROOT_SHELL,
1052 ARG_SETUP_MACHINE_ID,
1053 ARG_FORCE,
1054 ARG_DELETE_ROOT_PASSWORD,
1055 ARG_WELCOME,
1056 };
1057
1058 static const struct option options[] = {
1059 { "help", no_argument, NULL, 'h' },
1060 { "version", no_argument, NULL, ARG_VERSION },
1061 { "root", required_argument, NULL, ARG_ROOT },
1062 { "image", required_argument, NULL, ARG_IMAGE },
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 },
1072 { "root-shell", required_argument, NULL, ARG_ROOT_SHELL },
1073 { "kernel-command-line", required_argument, NULL, ARG_KERNEL_COMMAND_LINE },
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 },
1080 { "prompt-root-shell", no_argument, NULL, ARG_PROMPT_ROOT_SHELL },
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 },
1086 { "copy-root-shell", no_argument, NULL, ARG_COPY_ROOT_SHELL },
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 },
1090 { "welcome", required_argument, NULL, ARG_WELCOME },
1091 {}
1092 };
1093
1094 int r, c;
1095
1096 assert(argc >= 0);
1097 assert(argv);
1098
1099 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
1100
1101 switch (c) {
1102
1103 case 'h':
1104 return help();
1105
1106 case ARG_VERSION:
1107 return version();
1108
1109 case ARG_ROOT:
1110 r = parse_path_argument(optarg, true, &arg_root);
1111 if (r < 0)
1112 return r;
1113 break;
1114
1115 case ARG_IMAGE:
1116 r = parse_path_argument(optarg, false, &arg_image);
1117 if (r < 0)
1118 return r;
1119 break;
1120
1121 case ARG_LOCALE:
1122 r = free_and_strdup(&arg_locale, optarg);
1123 if (r < 0)
1124 return log_oom();
1125
1126 break;
1127
1128 case ARG_LOCALE_MESSAGES:
1129 r = free_and_strdup(&arg_locale_messages, optarg);
1130 if (r < 0)
1131 return log_oom();
1132
1133 break;
1134
1135 case ARG_KEYMAP:
1136 if (!keymap_is_valid(optarg))
1137 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1138 "Keymap %s is not valid.", optarg);
1139
1140 r = free_and_strdup(&arg_keymap, optarg);
1141 if (r < 0)
1142 return log_oom();
1143
1144 break;
1145
1146 case ARG_TIMEZONE:
1147 if (!timezone_is_valid(optarg, LOG_ERR))
1148 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1149 "Timezone %s is not valid.", optarg);
1150
1151 r = free_and_strdup(&arg_timezone, optarg);
1152 if (r < 0)
1153 return log_oom();
1154
1155 break;
1156
1157 case ARG_ROOT_PASSWORD:
1158 r = free_and_strdup(&arg_root_password, optarg);
1159 if (r < 0)
1160 return log_oom();
1161
1162 arg_root_password_is_hashed = false;
1163 break;
1164
1165 case ARG_ROOT_PASSWORD_FILE:
1166 arg_root_password = mfree(arg_root_password);
1167
1168 r = read_one_line_file(optarg, &arg_root_password);
1169 if (r < 0)
1170 return log_error_errno(r, "Failed to read %s: %m", optarg);
1171
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;
1181 break;
1182
1183 case ARG_ROOT_SHELL:
1184 r = find_shell(optarg, arg_root);
1185 if (r < 0)
1186 return r;
1187
1188 r = free_and_strdup(&arg_root_shell, optarg);
1189 if (r < 0)
1190 return log_oom();
1191
1192 break;
1193
1194 case ARG_HOSTNAME:
1195 if (!hostname_is_valid(optarg, VALID_HOSTNAME_TRAILING_DOT))
1196 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1197 "Host name %s is not valid.", optarg);
1198
1199 r = free_and_strdup(&arg_hostname, optarg);
1200 if (r < 0)
1201 return log_oom();
1202
1203 hostname_cleanup(arg_hostname);
1204 break;
1205
1206 case ARG_MACHINE_ID:
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);
1210
1211 break;
1212
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
1220 case ARG_PROMPT:
1221 arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname =
1222 arg_prompt_root_password = arg_prompt_root_shell = true;
1223 break;
1224
1225 case ARG_PROMPT_LOCALE:
1226 arg_prompt_locale = true;
1227 break;
1228
1229 case ARG_PROMPT_KEYMAP:
1230 arg_prompt_keymap = true;
1231 break;
1232
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
1245 case ARG_PROMPT_ROOT_SHELL:
1246 arg_prompt_root_shell = true;
1247 break;
1248
1249 case ARG_COPY:
1250 arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password =
1251 arg_copy_root_shell = true;
1252 break;
1253
1254 case ARG_COPY_LOCALE:
1255 arg_copy_locale = true;
1256 break;
1257
1258 case ARG_COPY_KEYMAP:
1259 arg_copy_keymap = true;
1260 break;
1261
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
1270 case ARG_COPY_ROOT_SHELL:
1271 arg_copy_root_shell = true;
1272 break;
1273
1274 case ARG_SETUP_MACHINE_ID:
1275 r = sd_id128_randomize(&arg_machine_id);
1276 if (r < 0)
1277 return log_error_errno(r, "Failed to generate randomized machine ID: %m");
1278
1279 break;
1280
1281 case ARG_FORCE:
1282 arg_force = true;
1283 break;
1284
1285 case ARG_DELETE_ROOT_PASSWORD:
1286 arg_delete_root_password = true;
1287 break;
1288
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
1297 case '?':
1298 return -EINVAL;
1299
1300 default:
1301 assert_not_reached();
1302 }
1303
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
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
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
1319 return 1;
1320 }
1321
1322 static int run(int argc, char *argv[]) {
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;
1326 int r;
1327
1328 r = parse_argv(argc, argv);
1329 if (r <= 0)
1330 return r;
1331
1332 log_setup();
1333
1334 umask(0022);
1335
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
1350 if (arg_image) {
1351 assert(!arg_root);
1352
1353 r = mount_image_privately_interactively(
1354 arg_image,
1355 DISSECT_IMAGE_GENERIC_ROOT |
1356 DISSECT_IMAGE_REQUIRE_ROOT |
1357 DISSECT_IMAGE_VALIDATE_OS |
1358 DISSECT_IMAGE_RELAX_VAR_CHECK |
1359 DISSECT_IMAGE_FSCK |
1360 DISSECT_IMAGE_GROWFS,
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);