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