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