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