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