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