1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include <linux/loop.h>
10 #include "alloc-util.h"
11 #include "ask-password-api.h"
13 #include "bus-error.h"
14 #include "bus-locator.h"
15 #include "bus-unit-util.h"
17 #include "bus-wait-for-jobs.h"
20 #include "creds-util.h"
21 #include "dissect-image.h"
23 #include "errno-util.h"
27 #include "glyph-util.h"
28 #include "hostname-util.h"
30 #include "libcrypt-util.h"
31 #include "locale-util.h"
32 #include "lock-util.h"
33 #include "main-func.h"
34 #include "memory-util.h"
36 #include "mount-util.h"
38 #include "parse-argument.h"
39 #include "parse-util.h"
40 #include "password-quality-util.h"
41 #include "path-util.h"
42 #include "pretty-print.h"
43 #include "proc-cmdline.h"
44 #include "random-util.h"
45 #include "smack-util.h"
46 #include "string-util.h"
48 #include "terminal-util.h"
49 #include "time-util.h"
50 #include "tmpfile-util-label.h"
51 #include "tmpfile-util.h"
52 #include "umask-util.h"
53 #include "user-util.h"
55 static char *arg_root
= NULL
;
56 static char *arg_image
= NULL
;
57 static char *arg_locale
= NULL
; /* $LANG */
58 static char *arg_locale_messages
= NULL
; /* $LC_MESSAGES */
59 static char *arg_keymap
= NULL
;
60 static char *arg_timezone
= NULL
;
61 static char *arg_hostname
= NULL
;
62 static sd_id128_t arg_machine_id
= {};
63 static char *arg_root_password
= NULL
;
64 static char *arg_root_shell
= NULL
;
65 static char *arg_kernel_cmdline
= NULL
;
66 static bool arg_prompt_locale
= false;
67 static bool arg_prompt_keymap
= false;
68 static bool arg_prompt_timezone
= false;
69 static bool arg_prompt_hostname
= false;
70 static bool arg_prompt_root_password
= false;
71 static bool arg_prompt_root_shell
= false;
72 static bool arg_copy_locale
= false;
73 static bool arg_copy_keymap
= false;
74 static bool arg_copy_timezone
= false;
75 static bool arg_copy_root_password
= false;
76 static bool arg_copy_root_shell
= false;
77 static bool arg_force
= false;
78 static bool arg_delete_root_password
= false;
79 static bool arg_root_password_is_hashed
= false;
80 static bool arg_welcome
= true;
81 static bool arg_reset
= false;
82 static ImagePolicy
*arg_image_policy
= NULL
;
84 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
85 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
86 STATIC_DESTRUCTOR_REGISTER(arg_locale
, freep
);
87 STATIC_DESTRUCTOR_REGISTER(arg_locale_messages
, freep
);
88 STATIC_DESTRUCTOR_REGISTER(arg_keymap
, freep
);
89 STATIC_DESTRUCTOR_REGISTER(arg_timezone
, freep
);
90 STATIC_DESTRUCTOR_REGISTER(arg_hostname
, freep
);
91 STATIC_DESTRUCTOR_REGISTER(arg_root_password
, erase_and_freep
);
92 STATIC_DESTRUCTOR_REGISTER(arg_root_shell
, freep
);
93 STATIC_DESTRUCTOR_REGISTER(arg_kernel_cmdline
, freep
);
94 STATIC_DESTRUCTOR_REGISTER(arg_image_policy
, image_policy_freep
);
96 static bool press_any_key(void) {
100 printf("-- Press any key to proceed --");
103 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
111 static void print_welcome(int rfd
) {
112 _cleanup_free_
char *pretty_name
= NULL
, *os_name
= NULL
, *ansi_color
= NULL
;
113 static bool done
= false;
125 r
= parse_os_release_at(rfd
,
126 "PRETTY_NAME", &pretty_name
,
128 "ANSI_COLOR", &ansi_color
);
130 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
131 "Failed to read os-release file, ignoring: %m");
133 pn
= os_release_pretty_name(pretty_name
, os_name
);
134 ac
= isempty(ansi_color
) ? "0" : ansi_color
;
136 (void) reset_terminal_fd(STDIN_FILENO
, /* switch_to_text= */ false);
138 if (colors_enabled())
139 printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ac
, pn
);
141 printf("\nWelcome to your new installation of %s!\n", pn
);
143 printf("\nPlease configure your system!\n\n");
150 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
151 unsigned break_lines
, break_modulo
;
152 size_t n
, per_column
, i
, j
;
154 assert(n_columns
> 0);
157 per_column
= DIV_ROUND_UP(n
, n_columns
);
159 break_lines
= lines();
163 /* The first page gets two extra lines, since we want to show
165 break_modulo
= break_lines
;
166 if (break_modulo
> 3)
169 for (i
= 0; i
< per_column
; i
++) {
171 for (j
= 0; j
< n_columns
; j
++) {
172 _cleanup_free_
char *e
= NULL
;
174 if (j
* per_column
+ i
>= n
)
177 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
181 printf("%4zu) %-*s", j
* per_column
+ i
+ 1, (int) width
, e
);
186 /* on the first screen we reserve 2 extra lines for the title */
187 if (i
% break_lines
== break_modulo
) {
188 if (!press_any_key())
196 static int prompt_loop(const char *text
, char **l
, unsigned percentage
, bool (*is_valid
)(const char *name
), char **ret
) {
204 _cleanup_free_
char *p
= NULL
;
207 r
= ask_string(&p
, "%s %s (empty to skip, \"list\" to list options): ",
208 special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), text
);
210 return log_error_errno(r
, "Failed to query user: %m");
213 log_warning("No data entered, skipping.");
217 if (streq(p
, "list")) {
218 r
= show_menu(l
, 3, 22, percentage
);
226 r
= safe_atou(p
, &u
);
228 if (u
<= 0 || u
> strv_length(l
)) {
229 log_error("Specified entry number out of range.");
233 log_info("Selected '%s'.", l
[u
-1]);
234 return free_and_strdup_warn(ret
, l
[u
-1]);
238 log_error("Entered data invalid.");
242 return free_and_replace(*ret
, p
);
246 static int should_configure(int dir_fd
, const char *filename
) {
247 _cleanup_fclose_
FILE *passwd
= NULL
, *shadow
= NULL
;
253 if (streq(filename
, "passwd") && !arg_force
)
254 /* We may need to do additional checks, so open the file. */
255 r
= xfopenat(dir_fd
, filename
, "re", O_NOFOLLOW
, &passwd
);
257 r
= RET_NERRNO(faccessat(dir_fd
, filename
, F_OK
, AT_SYMLINK_NOFOLLOW
));
260 return true; /* missing */
262 return log_error_errno(r
, "Failed to access %s: %m", filename
);
264 return true; /* exists, but if --force was given we should still configure the file. */
269 /* In case of /etc/passwd, do an additional check for the root password field.
270 * We first check that passwd redirects to shadow, and then we check shadow.
273 while ((r
= fgetpwent_sane(passwd
, &i
)) > 0) {
274 if (!streq(i
->pw_name
, "root"))
277 if (streq_ptr(i
->pw_passwd
, PASSWORD_SEE_SHADOW
))
279 log_debug("passwd: root account with non-shadow password found, treating root as configured");
283 return log_error_errno(r
, "Failed to read %s: %m", filename
);
285 log_debug("No root account found in %s, assuming root is not configured.", filename
);
289 r
= xfopenat(dir_fd
, "shadow", "re", O_NOFOLLOW
, &shadow
);
291 log_debug("No shadow file found, assuming root is not configured.");
292 return true; /* missing */
295 return log_error_errno(r
, "Failed to access shadow: %m");
298 while ((r
= fgetspent_sane(shadow
, &j
)) > 0) {
299 if (!streq(j
->sp_namp
, "root"))
302 bool unprovisioned
= streq_ptr(j
->sp_pwdp
, PASSWORD_UNPROVISIONED
);
303 log_debug("Root account found, %s.",
304 unprovisioned
? "with unprovisioned password, treating root as not configured" :
305 "treating root as configured");
306 return unprovisioned
;
309 return log_error_errno(r
, "Failed to read shadow: %m");
311 log_debug("No root account found in shadow, assuming root is not configured.");
315 static bool locale_is_installed_bool(const char *name
) {
316 return locale_is_installed(name
) > 0;
319 static bool locale_is_ok(int rfd
, const char *name
) {
322 return dir_fd_is_root(rfd
) ? locale_is_installed_bool(name
) : locale_is_valid(name
);
325 static int prompt_locale(int rfd
) {
326 _cleanup_strv_free_
char **locales
= NULL
;
327 bool acquired_from_creds
= false;
332 if (arg_locale
|| arg_locale_messages
)
335 r
= read_credential("firstboot.locale", (void**) &arg_locale
, NULL
);
337 log_debug_errno(r
, "Failed to read credential firstboot.locale, ignoring: %m");
339 acquired_from_creds
= true;
341 r
= read_credential("firstboot.locale-messages", (void**) &arg_locale_messages
, NULL
);
343 log_debug_errno(r
, "Failed to read credential firstboot.locale-messages, ignoring: %m");
345 acquired_from_creds
= true;
347 if (acquired_from_creds
) {
348 log_debug("Acquired locale from credentials.");
352 if (!arg_prompt_locale
) {
353 log_debug("Prompting for locale was not requested.");
357 r
= get_locales(&locales
);
359 return log_error_errno(r
, "Cannot query locales list: %m");
361 if (strv_isempty(locales
))
362 log_debug("No locales found, skipping locale selection.");
363 else if (strv_length(locales
) == 1) {
365 if (streq(locales
[0], SYSTEMD_DEFAULT_LOCALE
))
366 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
368 log_debug("Only a single locale available (%s), selecting it as default.", locales
[0]);
370 arg_locale
= strdup(locales
[0]);
374 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
377 bool (*is_valid
)(const char *name
) = dir_fd_is_root(rfd
) ? locale_is_installed_bool
382 r
= prompt_loop("Please enter system locale name or number",
383 locales
, 60, is_valid
, &arg_locale
);
387 if (isempty(arg_locale
))
390 r
= prompt_loop("Please enter system message locale name or number",
391 locales
, 60, is_valid
, &arg_locale_messages
);
395 /* Suppress the messages setting if it's the same as the main locale anyway */
396 if (streq_ptr(arg_locale
, arg_locale_messages
))
397 arg_locale_messages
= mfree(arg_locale_messages
);
403 static int process_locale(int rfd
) {
404 _cleanup_close_
int pfd
= -EBADF
;
405 _cleanup_free_
char *f
= NULL
;
412 pfd
= chase_and_open_parent_at(rfd
, "/etc/locale.conf",
413 CHASE_AT_RESOLVE_IN_ROOT
|CHASE_MKDIR_0755
|CHASE_WARN
|CHASE_NOFOLLOW
,
416 return log_error_errno(pfd
, "Failed to chase /etc/locale.conf: %m");
418 r
= should_configure(pfd
, f
);
420 log_debug("Found /etc/locale.conf, assuming locale information has been configured.");
424 r
= dir_fd_is_root(rfd
);
426 return log_error_errno(r
, "Failed to check if directory file descriptor is root: %m");
428 if (arg_copy_locale
&& r
== 0) {
429 r
= copy_file_atomic_at(AT_FDCWD
, "/etc/locale.conf", pfd
, f
, 0644, COPY_REFLINK
);
432 return log_error_errno(r
, "Failed to copy host's /etc/locale.conf: %m");
434 log_info("Copied host's /etc/locale.conf.");
439 r
= prompt_locale(rfd
);
443 if (!isempty(arg_locale
))
444 locales
[i
++] = strjoina("LANG=", arg_locale
);
445 if (!isempty(arg_locale_messages
) && !streq_ptr(arg_locale_messages
, arg_locale
))
446 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
453 r
= write_env_file(pfd
, f
, NULL
, locales
);
455 return log_error_errno(r
, "Failed to write /etc/locale.conf: %m");
457 log_info("/etc/locale.conf written.");
461 static int prompt_keymap(int rfd
) {
462 _cleanup_strv_free_
char **kmaps
= NULL
;
470 r
= read_credential("firstboot.keymap", (void**) &arg_keymap
, NULL
);
472 log_debug_errno(r
, "Failed to read credential firstboot.keymap, ignoring: %m");
474 log_debug("Acquired keymap from credential.");
478 if (!arg_prompt_keymap
) {
479 log_debug("Prompting for keymap was not requested.");
483 r
= get_keymaps(&kmaps
);
484 if (r
== -ENOENT
) /* no keymaps installed */
485 return log_debug_errno(r
, "No keymaps are installed.");
487 return log_error_errno(r
, "Failed to read keymaps: %m");
491 return prompt_loop("Please enter system keymap name or number",
492 kmaps
, 60, keymap_is_valid
, &arg_keymap
);
495 static int process_keymap(int rfd
) {
496 _cleanup_close_
int pfd
= -EBADF
;
497 _cleanup_free_
char *f
= NULL
;
503 pfd
= chase_and_open_parent_at(rfd
, "/etc/vconsole.conf",
504 CHASE_AT_RESOLVE_IN_ROOT
|CHASE_MKDIR_0755
|CHASE_WARN
|CHASE_NOFOLLOW
,
507 return log_error_errno(pfd
, "Failed to chase /etc/vconsole.conf: %m");
509 r
= should_configure(pfd
, f
);
511 log_debug("Found /etc/vconsole.conf, assuming console has been configured.");
515 r
= dir_fd_is_root(rfd
);
517 return log_error_errno(r
, "Failed to check if directory file descriptor is root: %m");
519 if (arg_copy_keymap
&& r
== 0) {
520 r
= copy_file_atomic_at(AT_FDCWD
, "/etc/vconsole.conf", pfd
, f
, 0644, COPY_REFLINK
);
523 return log_error_errno(r
, "Failed to copy host's /etc/vconsole.conf: %m");
525 log_info("Copied host's /etc/vconsole.conf.");
530 r
= prompt_keymap(rfd
);
532 return 0; /* don't fail if no keymaps are installed */
536 if (isempty(arg_keymap
))
539 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
541 r
= write_vconsole_conf(pfd
, f
, keymap
);
543 return log_error_errno(r
, "Failed to write /etc/vconsole.conf: %m");
545 log_info("/etc/vconsole.conf written.");
549 static bool timezone_is_valid_log_error(const char *name
) {
550 return timezone_is_valid(name
, LOG_ERR
);
553 static int prompt_timezone(int rfd
) {
554 _cleanup_strv_free_
char **zones
= NULL
;
562 r
= read_credential("firstboot.timezone", (void**) &arg_timezone
, NULL
);
564 log_debug_errno(r
, "Failed to read credential firstboot.timezone, ignoring: %m");
566 log_debug("Acquired timezone from credential.");
570 if (!arg_prompt_timezone
) {
571 log_debug("Prompting for timezone was not requested.");
575 r
= get_timezones(&zones
);
577 return log_error_errno(r
, "Cannot query timezone list: %m");
581 r
= prompt_loop("Please enter timezone name or number",
582 zones
, 30, timezone_is_valid_log_error
, &arg_timezone
);
589 static int process_timezone(int rfd
) {
590 _cleanup_close_
int pfd
= -EBADF
;
591 _cleanup_free_
char *f
= NULL
;
597 pfd
= chase_and_open_parent_at(rfd
, "/etc/localtime",
598 CHASE_AT_RESOLVE_IN_ROOT
|CHASE_MKDIR_0755
|CHASE_WARN
|CHASE_NOFOLLOW
,
601 return log_error_errno(pfd
, "Failed to chase /etc/localtime: %m");
603 r
= should_configure(pfd
, f
);
605 log_debug("Found /etc/localtime, assuming timezone has been configured.");
609 r
= dir_fd_is_root(rfd
);
611 return log_error_errno(r
, "Failed to check if directory file descriptor is root: %m");
613 if (arg_copy_timezone
&& r
== 0) {
614 _cleanup_free_
char *s
= NULL
;
616 r
= readlink_malloc("/etc/localtime", &s
);
619 return log_error_errno(r
, "Failed to read host's /etc/localtime: %m");
621 r
= symlinkat_atomic_full(s
, pfd
, f
, /* make_relative= */ false);
623 return log_error_errno(r
, "Failed to create /etc/localtime symlink: %m");
625 log_info("Copied host's /etc/localtime.");
630 r
= prompt_timezone(rfd
);
634 if (isempty(arg_timezone
))
637 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
639 r
= symlinkat_atomic_full(e
, pfd
, f
, /* make_relative= */ false);
641 return log_error_errno(r
, "Failed to create /etc/localtime symlink: %m");
643 log_info("/etc/localtime written");
647 static int prompt_hostname(int rfd
) {
655 if (!arg_prompt_hostname
) {
656 log_debug("Prompting for hostname was not requested.");
664 _cleanup_free_
char *h
= NULL
;
666 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
668 return log_error_errno(r
, "Failed to query hostname: %m");
671 log_warning("No hostname entered, skipping.");
675 if (!hostname_is_valid(h
, VALID_HOSTNAME_TRAILING_DOT
)) {
676 log_error("Specified hostname invalid.");
680 /* Get rid of the trailing dot that we allow, but don't want to see */
681 arg_hostname
= hostname_cleanup(h
);
689 static int process_hostname(int rfd
) {
690 _cleanup_close_
int pfd
= -EBADF
;
691 _cleanup_free_
char *f
= NULL
;
696 pfd
= chase_and_open_parent_at(rfd
, "/etc/hostname",
697 CHASE_AT_RESOLVE_IN_ROOT
|CHASE_MKDIR_0755
|CHASE_WARN
,
700 return log_error_errno(pfd
, "Failed to chase /etc/hostname: %m");
702 r
= should_configure(pfd
, f
);
704 log_debug("Found /etc/hostname, assuming hostname has been configured.");
708 r
= prompt_hostname(rfd
);
712 if (isempty(arg_hostname
))
715 r
= write_string_file_at(pfd
, f
, arg_hostname
,
716 WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_SYNC
|WRITE_STRING_FILE_ATOMIC
);
718 return log_error_errno(r
, "Failed to write /etc/hostname: %m");
720 log_info("/etc/hostname written.");
724 static int process_machine_id(int rfd
) {
725 _cleanup_close_
int pfd
= -EBADF
;
726 _cleanup_free_
char *f
= NULL
;
731 pfd
= chase_and_open_parent_at(rfd
, "/etc/machine-id",
732 CHASE_AT_RESOLVE_IN_ROOT
|CHASE_MKDIR_0755
|CHASE_WARN
|CHASE_NOFOLLOW
,
735 return log_error_errno(pfd
, "Failed to chase /etc/machine-id: %m");
737 r
= should_configure(pfd
, f
);
739 log_debug("Found /etc/machine-id, assuming machine-id has been configured.");
743 if (sd_id128_is_null(arg_machine_id
)) {
744 log_debug("Initialization of machine-id was not requested, skipping.");
748 r
= write_string_file_at(pfd
, "machine-id", SD_ID128_TO_STRING(arg_machine_id
),
749 WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_SYNC
|WRITE_STRING_FILE_ATOMIC
);
751 return log_error_errno(r
, "Failed to write /etc/machine-id: %m");
753 log_info("/etc/machine-id written.");
757 static int prompt_root_password(int rfd
) {
758 const char *msg1
, *msg2
;
763 if (arg_root_password
)
766 if (get_credential_user_password("root", &arg_root_password
, &arg_root_password_is_hashed
) >= 0)
769 if (!arg_prompt_root_password
) {
770 log_debug("Prompting for root password was not requested.");
777 msg1
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip):");
778 msg2
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter new root password again:");
783 _cleanup_strv_free_erase_
char **a
= NULL
, **b
= NULL
;
784 _cleanup_free_
char *error
= NULL
;
786 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
788 return log_error_errno(r
, "Failed to query root password: %m");
789 if (strv_length(a
) != 1)
790 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
791 "Received multiple passwords, where we expected one.");
794 log_warning("No password entered, skipping.");
798 r
= check_password_quality(*a
, /* old */ NULL
, "root", &error
);
799 if (ERRNO_IS_NEG_NOT_SUPPORTED(r
))
800 log_warning("Password quality check is not supported, proceeding anyway.");
802 return log_error_errno(r
, "Failed to check password quality: %m");
804 log_warning("Password is weak, accepting anyway: %s", error
);
806 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
808 return log_error_errno(r
, "Failed to query root password: %m");
809 if (strv_length(b
) != 1)
810 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
811 "Received multiple passwords, where we expected one.");
813 if (!streq(*a
, *b
)) {
814 log_error("Entered passwords did not match, please try again.");
818 arg_root_password
= TAKE_PTR(*a
);
825 static int find_shell(int rfd
, const char *path
) {
830 if (!valid_shell(path
))
831 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "%s is not a valid shell", path
);
833 r
= chaseat(rfd
, path
, CHASE_AT_RESOLVE_IN_ROOT
, NULL
, NULL
);
835 return log_error_errno(r
, "Failed to resolve shell %s: %m", path
);
840 static int prompt_root_shell(int rfd
) {
848 r
= read_credential("passwd.shell.root", (void**) &arg_root_shell
, NULL
);
850 log_debug_errno(r
, "Failed to read credential passwd.shell.root, ignoring: %m");
852 log_debug("Acquired root shell from credential.");
856 if (!arg_prompt_root_shell
) {
857 log_debug("Prompting for root shell was not requested.");
865 _cleanup_free_
char *s
= NULL
;
867 r
= ask_string(&s
, "%s Please enter root shell for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
869 return log_error_errno(r
, "Failed to query root shell: %m");
872 log_warning("No shell entered, skipping.");
876 r
= find_shell(rfd
, s
);
880 arg_root_shell
= TAKE_PTR(s
);
887 static int write_root_passwd(int rfd
, int etc_fd
, const char *password
, const char *shell
) {
888 _cleanup_fclose_
FILE *original
= NULL
, *passwd
= NULL
;
889 _cleanup_(unlink_and_freep
) char *passwd_tmp
= NULL
;
894 r
= fopen_temporary_at_label(etc_fd
, "passwd", "passwd", &passwd
, &passwd_tmp
);
898 r
= xfopenat(etc_fd
, "passwd", "re", O_NOFOLLOW
, &original
);
899 if (r
< 0 && r
!= -ENOENT
)
905 r
= copy_rights(fileno(original
), fileno(passwd
));
909 while ((r
= fgetpwent_sane(original
, &i
)) > 0) {
911 if (streq(i
->pw_name
, "root")) {
912 i
->pw_passwd
= (char *) password
;
914 i
->pw_shell
= (char *) shell
;
917 r
= putpwent_sane(i
, passwd
);
925 struct passwd root
= {
926 .pw_name
= (char *) "root",
927 .pw_passwd
= (char *) password
,
930 .pw_gecos
= (char *) "Super User",
931 .pw_dir
= (char *) "/root",
932 .pw_shell
= (char *) (shell
?: default_root_shell_at(rfd
)),
938 r
= fchmod(fileno(passwd
), 0644);
942 r
= putpwent_sane(&root
, passwd
);
947 r
= fflush_sync_and_check(passwd
);
951 r
= renameat_and_apply_smack_floor_label(etc_fd
, passwd_tmp
, etc_fd
, "passwd");
958 static int write_root_shadow(int etc_fd
, const char *hashed_password
) {
959 _cleanup_fclose_
FILE *original
= NULL
, *shadow
= NULL
;
960 _cleanup_(unlink_and_freep
) char *shadow_tmp
= NULL
;
963 assert(hashed_password
);
965 r
= fopen_temporary_at_label(etc_fd
, "shadow", "shadow", &shadow
, &shadow_tmp
);
969 r
= xfopenat(etc_fd
, "shadow", "re", O_NOFOLLOW
, &original
);
970 if (r
< 0 && r
!= -ENOENT
)
976 r
= copy_rights(fileno(original
), fileno(shadow
));
980 while ((r
= fgetspent_sane(original
, &i
)) > 0) {
982 if (streq(i
->sp_namp
, "root")) {
983 i
->sp_pwdp
= (char *) hashed_password
;
984 i
->sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
987 r
= putspent_sane(i
, shadow
);
996 .sp_namp
= (char*) "root",
997 .sp_pwdp
= (char *) hashed_password
,
998 .sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
),
1004 .sp_flag
= ULONG_MAX
, /* this appears to be what everybody does ... */
1007 if (errno
!= ENOENT
)
1010 r
= fchmod(fileno(shadow
), 0000);
1014 r
= putspent_sane(&root
, shadow
);
1019 r
= fflush_sync_and_check(shadow
);
1023 r
= renameat_and_apply_smack_floor_label(etc_fd
, shadow_tmp
, etc_fd
, "shadow");
1030 static int process_root_account(int rfd
) {
1031 _cleanup_close_
int pfd
= -EBADF
;
1032 _cleanup_(release_lock_file
) LockFile lock
= LOCK_FILE_INIT
;
1033 _cleanup_(erase_and_freep
) char *_hashed_password
= NULL
;
1034 const char *password
, *hashed_password
;
1039 pfd
= chase_and_open_parent_at(rfd
, "/etc/passwd",
1040 CHASE_AT_RESOLVE_IN_ROOT
|CHASE_MKDIR_0755
|CHASE_WARN
|CHASE_NOFOLLOW
,
1043 return log_error_errno(pfd
, "Failed to chase /etc/passwd: %m");
1045 /* Ensure that passwd and shadow are in the same directory and are not symlinks. */
1047 FOREACH_STRING(s
, "passwd", "shadow") {
1048 r
= verify_regular_at(pfd
, s
, /* follow = */ false);
1049 if (IN_SET(r
, -EISDIR
, -ELOOP
, -EBADFD
))
1050 return log_error_errno(r
, "/etc/%s is not a regular file", s
);
1051 if (r
< 0 && r
!= -ENOENT
)
1052 return log_error_errno(r
, "Failed to check whether /etc/%s is a regular file: %m", s
);
1054 r
= should_configure(pfd
, s
);
1062 log_debug("Found /etc/passwd and /etc/shadow, assuming root account has been initialized.");
1066 /* Don't create/modify passwd and shadow if not asked */
1067 if (!(arg_root_password
|| arg_prompt_root_password
|| arg_copy_root_password
|| arg_delete_root_password
||
1068 arg_root_shell
|| arg_prompt_root_shell
|| arg_copy_root_shell
)) {
1069 log_debug("Initialization of root account was not requested, skipping.");
1073 r
= make_lock_file_at(pfd
, ETC_PASSWD_LOCK_FILENAME
, LOCK_EX
, &lock
);
1075 return log_error_errno(r
, "Failed to take a lock on /etc/passwd: %m");
1077 k
= dir_fd_is_root(rfd
);
1079 return log_error_errno(k
, "Failed to check if directory file descriptor is root: %m");
1081 if (arg_copy_root_shell
&& k
== 0) {
1082 _cleanup_free_
struct passwd
*p
= NULL
;
1084 r
= getpwnam_malloc("root", &p
);
1086 return log_error_errno(r
, "Failed to find passwd entry for root: %m");
1088 r
= free_and_strdup(&arg_root_shell
, p
->pw_shell
);
1093 r
= prompt_root_shell(rfd
);
1097 if (arg_copy_root_password
&& k
== 0) {
1101 p
= getspnam("root");
1103 return log_error_errno(errno_or_else(EIO
), "Failed to find shadow entry for root: %m");
1105 r
= free_and_strdup(&arg_root_password
, p
->sp_pwdp
);
1109 arg_root_password_is_hashed
= true;
1112 r
= prompt_root_password(rfd
);
1116 if (arg_root_password
&& arg_root_password_is_hashed
) {
1117 password
= PASSWORD_SEE_SHADOW
;
1118 hashed_password
= arg_root_password
;
1119 } else if (arg_root_password
) {
1120 r
= hash_password(arg_root_password
, &_hashed_password
);
1122 return log_error_errno(r
, "Failed to hash password: %m");
1124 password
= PASSWORD_SEE_SHADOW
;
1125 hashed_password
= _hashed_password
;
1127 } else if (arg_delete_root_password
)
1128 password
= hashed_password
= PASSWORD_NONE
;
1130 password
= hashed_password
= PASSWORD_LOCKED_AND_INVALID
;
1132 r
= write_root_passwd(rfd
, pfd
, password
, arg_root_shell
);
1134 return log_error_errno(r
, "Failed to write /etc/passwd: %m");
1136 log_info("/etc/passwd written.");
1138 r
= write_root_shadow(pfd
, hashed_password
);
1140 return log_error_errno(r
, "Failed to write /etc/shadow: %m");
1142 log_info("/etc/shadow written.");
1146 static int process_kernel_cmdline(int rfd
) {
1147 _cleanup_close_
int pfd
= -EBADF
;
1148 _cleanup_free_
char *f
= NULL
;
1153 pfd
= chase_and_open_parent_at(rfd
, "/etc/kernel/cmdline",
1154 CHASE_AT_RESOLVE_IN_ROOT
|CHASE_MKDIR_0755
|CHASE_WARN
|CHASE_NOFOLLOW
,
1157 return log_error_errno(pfd
, "Failed to chase /etc/kernel/cmdline: %m");
1159 r
= should_configure(pfd
, f
);
1161 log_debug("Found /etc/kernel/cmdline, assuming kernel command line has been configured.");
1165 if (!arg_kernel_cmdline
) {
1166 log_debug("Creation of /etc/kernel/cmdline was not requested, skipping.");
1170 r
= write_string_file_at(pfd
, "cmdline", arg_kernel_cmdline
,
1171 WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_SYNC
|WRITE_STRING_FILE_ATOMIC
);
1173 return log_error_errno(r
, "Failed to write /etc/kernel/cmdline: %m");
1175 log_info("/etc/kernel/cmdline written.");
1179 static int reset_one(int rfd
, const char *path
) {
1180 _cleanup_close_
int pfd
= -EBADF
;
1181 _cleanup_free_
char *f
= NULL
;
1186 pfd
= chase_and_open_parent_at(rfd
, path
, CHASE_AT_RESOLVE_IN_ROOT
|CHASE_WARN
|CHASE_NOFOLLOW
, &f
);
1190 return log_error_errno(pfd
, "Failed to resolve %s: %m", path
);
1192 if (unlinkat(pfd
, f
, 0) < 0)
1193 return errno
== ENOENT
? 0 : log_error_errno(errno
, "Failed to remove %s: %m", path
);
1195 log_info("Removed %s", path
);
1199 static int process_reset(int rfd
) {
1209 "/etc/vconsole.conf",
1212 "/etc/kernel/cmdline",
1214 r
= reset_one(rfd
, p
);
1222 static int help(void) {
1223 _cleanup_free_
char *link
= NULL
;
1226 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
1230 printf("%s [OPTIONS...]\n\n"
1231 "Configures basic settings of the system.\n\n"
1232 " -h --help Show this help\n"
1233 " --version Show package version\n"
1234 " --root=PATH Operate on an alternate filesystem root\n"
1235 " --image=PATH Operate on disk image as filesystem root\n"
1236 " --image-policy=POLICY Specify disk image dissection policy\n"
1237 " --locale=LOCALE Set primary locale (LANG=)\n"
1238 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
1239 " --keymap=KEYMAP Set keymap\n"
1240 " --timezone=TIMEZONE Set timezone\n"
1241 " --hostname=NAME Set hostname\n"
1242 " --setup-machine-id Set a random machine ID\n"
1243 " --machine-id=ID Set specified machine ID\n"
1244 " --root-password=PASSWORD Set root password from plaintext password\n"
1245 " --root-password-file=FILE Set root password from file\n"
1246 " --root-password-hashed=HASH Set root password from hashed password\n"
1247 " --root-shell=SHELL Set root shell\n"
1248 " --kernel-command-line=CMDLINE\n"
1249 " Set kernel command line\n"
1250 " --prompt-locale Prompt the user for locale settings\n"
1251 " --prompt-keymap Prompt the user for keymap settings\n"
1252 " --prompt-timezone Prompt the user for timezone\n"
1253 " --prompt-hostname Prompt the user for hostname\n"
1254 " --prompt-root-password Prompt the user for root password\n"
1255 " --prompt-root-shell Prompt the user for root shell\n"
1256 " --prompt Prompt for all of the above\n"
1257 " --copy-locale Copy locale from host\n"
1258 " --copy-keymap Copy keymap from host\n"
1259 " --copy-timezone Copy timezone from host\n"
1260 " --copy-root-password Copy root password from host\n"
1261 " --copy-root-shell Copy root shell from host\n"
1262 " --copy Copy locale, keymap, timezone, root password\n"
1263 " --force Overwrite existing files\n"
1264 " --delete-root-password Delete root password\n"
1265 " --welcome=no Disable the welcome text\n"
1266 " --reset Remove existing files\n"
1267 "\nSee the %s for details.\n",
1268 program_invocation_short_name
,
1274 static int parse_argv(int argc
, char *argv
[]) {
1277 ARG_VERSION
= 0x100,
1282 ARG_LOCALE_MESSAGES
,
1286 ARG_SETUP_MACHINE_ID
,
1289 ARG_ROOT_PASSWORD_FILE
,
1290 ARG_ROOT_PASSWORD_HASHED
,
1292 ARG_KERNEL_COMMAND_LINE
,
1296 ARG_PROMPT_TIMEZONE
,
1297 ARG_PROMPT_HOSTNAME
,
1298 ARG_PROMPT_ROOT_PASSWORD
,
1299 ARG_PROMPT_ROOT_SHELL
,
1304 ARG_COPY_ROOT_PASSWORD
,
1305 ARG_COPY_ROOT_SHELL
,
1307 ARG_DELETE_ROOT_PASSWORD
,
1312 static const struct option options
[] = {
1313 { "help", no_argument
, NULL
, 'h' },
1314 { "version", no_argument
, NULL
, ARG_VERSION
},
1315 { "root", required_argument
, NULL
, ARG_ROOT
},
1316 { "image", required_argument
, NULL
, ARG_IMAGE
},
1317 { "image-policy", required_argument
, NULL
, ARG_IMAGE_POLICY
},
1318 { "locale", required_argument
, NULL
, ARG_LOCALE
},
1319 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
1320 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
1321 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
1322 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
1323 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
1324 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
1325 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
1326 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
1327 { "root-password-hashed", required_argument
, NULL
, ARG_ROOT_PASSWORD_HASHED
},
1328 { "root-shell", required_argument
, NULL
, ARG_ROOT_SHELL
},
1329 { "kernel-command-line", required_argument
, NULL
, ARG_KERNEL_COMMAND_LINE
},
1330 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
1331 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
1332 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
1333 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
1334 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
1335 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
1336 { "prompt-root-shell", no_argument
, NULL
, ARG_PROMPT_ROOT_SHELL
},
1337 { "copy", no_argument
, NULL
, ARG_COPY
},
1338 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
1339 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
1340 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
1341 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
1342 { "copy-root-shell", no_argument
, NULL
, ARG_COPY_ROOT_SHELL
},
1343 { "force", no_argument
, NULL
, ARG_FORCE
},
1344 { "delete-root-password", no_argument
, NULL
, ARG_DELETE_ROOT_PASSWORD
},
1345 { "welcome", required_argument
, NULL
, ARG_WELCOME
},
1346 { "reset", no_argument
, NULL
, ARG_RESET
},
1355 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
1366 r
= parse_path_argument(optarg
, true, &arg_root
);
1372 r
= parse_path_argument(optarg
, false, &arg_image
);
1377 case ARG_IMAGE_POLICY
:
1378 r
= parse_image_policy_argument(optarg
, &arg_image_policy
);
1384 r
= free_and_strdup(&arg_locale
, optarg
);
1390 case ARG_LOCALE_MESSAGES
:
1391 r
= free_and_strdup(&arg_locale_messages
, optarg
);
1398 if (!keymap_is_valid(optarg
))
1399 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1400 "Keymap %s is not valid.", optarg
);
1402 r
= free_and_strdup(&arg_keymap
, optarg
);
1409 if (!timezone_is_valid(optarg
, LOG_ERR
))
1410 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1411 "Timezone %s is not valid.", optarg
);
1413 r
= free_and_strdup(&arg_timezone
, optarg
);
1419 case ARG_ROOT_PASSWORD
:
1420 r
= free_and_strdup(&arg_root_password
, optarg
);
1424 arg_root_password_is_hashed
= false;
1427 case ARG_ROOT_PASSWORD_FILE
:
1428 arg_root_password
= mfree(arg_root_password
);
1430 r
= read_one_line_file(optarg
, &arg_root_password
);
1432 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
1434 arg_root_password_is_hashed
= false;
1437 case ARG_ROOT_PASSWORD_HASHED
:
1438 r
= free_and_strdup(&arg_root_password
, optarg
);
1442 arg_root_password_is_hashed
= true;
1445 case ARG_ROOT_SHELL
:
1446 r
= free_and_strdup(&arg_root_shell
, optarg
);
1453 if (!hostname_is_valid(optarg
, VALID_HOSTNAME_TRAILING_DOT
))
1454 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1455 "Host name %s is not valid.", optarg
);
1457 r
= free_and_strdup(&arg_hostname
, optarg
);
1461 hostname_cleanup(arg_hostname
);
1464 case ARG_SETUP_MACHINE_ID
:
1465 r
= sd_id128_randomize(&arg_machine_id
);
1467 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
1471 case ARG_MACHINE_ID
:
1472 r
= sd_id128_from_string(optarg
, &arg_machine_id
);
1474 return log_error_errno(r
, "Failed to parse machine id %s.", optarg
);
1478 case ARG_KERNEL_COMMAND_LINE
:
1479 r
= free_and_strdup(&arg_kernel_cmdline
, optarg
);
1486 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
=
1487 arg_prompt_root_password
= arg_prompt_root_shell
= true;
1490 case ARG_PROMPT_LOCALE
:
1491 arg_prompt_locale
= true;
1494 case ARG_PROMPT_KEYMAP
:
1495 arg_prompt_keymap
= true;
1498 case ARG_PROMPT_TIMEZONE
:
1499 arg_prompt_timezone
= true;
1502 case ARG_PROMPT_HOSTNAME
:
1503 arg_prompt_hostname
= true;
1506 case ARG_PROMPT_ROOT_PASSWORD
:
1507 arg_prompt_root_password
= true;
1510 case ARG_PROMPT_ROOT_SHELL
:
1511 arg_prompt_root_shell
= true;
1515 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
=
1516 arg_copy_root_shell
= true;
1519 case ARG_COPY_LOCALE
:
1520 arg_copy_locale
= true;
1523 case ARG_COPY_KEYMAP
:
1524 arg_copy_keymap
= true;
1527 case ARG_COPY_TIMEZONE
:
1528 arg_copy_timezone
= true;
1531 case ARG_COPY_ROOT_PASSWORD
:
1532 arg_copy_root_password
= true;
1535 case ARG_COPY_ROOT_SHELL
:
1536 arg_copy_root_shell
= true;
1543 case ARG_DELETE_ROOT_PASSWORD
:
1544 arg_delete_root_password
= true;
1548 r
= parse_boolean(optarg
);
1550 return log_error_errno(r
, "Failed to parse --welcome= argument: %s", optarg
);
1563 assert_not_reached();
1566 if (arg_delete_root_password
&& (arg_copy_root_password
|| arg_root_password
|| arg_prompt_root_password
))
1567 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1568 "--delete-root-password cannot be combined with other root password options.");
1570 if (arg_image
&& arg_root
)
1571 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1572 "--root= and --image= cannot be used together.");
1574 if (!sd_id128_is_null(arg_machine_id
) && !(arg_image
|| arg_root
) && !arg_force
)
1575 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1576 "--machine-id=/--setup-machine-id only works with --root= or --image=.");
1581 static int reload_system_manager(sd_bus
**bus
) {
1587 r
= bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL
, NULL
, RUNTIME_SCOPE_SYSTEM
, bus
);
1589 return bus_log_connect_error(r
, BUS_TRANSPORT_LOCAL
);
1592 r
= bus_service_manager_reload(*bus
);
1596 log_info("Requested manager reload to apply locale configuration.");
1600 static int reload_vconsole(sd_bus
**bus
) {
1601 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1602 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1603 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1610 r
= bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL
, NULL
, RUNTIME_SCOPE_SYSTEM
, bus
);
1612 return bus_log_connect_error(r
, BUS_TRANSPORT_LOCAL
);
1615 r
= bus_wait_for_jobs_new(*bus
, &w
);
1617 return log_error_errno(r
, "Could not watch jobs: %m");
1619 r
= bus_call_method(*bus
, bus_systemd_mgr
, "RestartUnit", &error
, &reply
,
1620 "ss", "systemd-vconsole-setup.service", "replace");
1622 return log_error_errno(r
, "Failed to issue method call: %s", bus_error_message(&error
, r
));
1624 r
= sd_bus_message_read(reply
, "o", &object
);
1626 return bus_log_parse_error(r
);
1628 r
= bus_wait_for_jobs_one(w
, object
, BUS_WAIT_JOBS_LOG_ERROR
, NULL
);
1630 return log_error_errno(r
, "Failed to wait for systemd-vconsole-setup.service/restart: %m");
1634 static int run(int argc
, char *argv
[]) {
1635 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1636 _cleanup_(loop_device_unrefp
) LoopDevice
*loop_device
= NULL
;
1637 _cleanup_(umount_and_freep
) char *mounted_dir
= NULL
;
1638 _cleanup_close_
int rfd
= -EBADF
;
1641 r
= parse_argv(argc
, argv
);
1649 bool offline
= arg_root
|| arg_image
;
1652 /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1653 * command line option, because we are called to provision the host with basic settings (as
1654 * opposed to some other file system tree/image) */
1657 r
= proc_cmdline_get_bool("systemd.firstboot", /* flags = */ 0, &enabled
);
1659 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
1660 if (r
> 0 && !enabled
) {
1661 log_debug("Found systemd.firstboot=no kernel command line argument, turning off all prompts.");
1662 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
= arg_prompt_root_password
= arg_prompt_root_shell
= false;
1669 r
= mount_image_privately_interactively(
1672 DISSECT_IMAGE_GENERIC_ROOT
|
1673 DISSECT_IMAGE_REQUIRE_ROOT
|
1674 DISSECT_IMAGE_VALIDATE_OS
|
1675 DISSECT_IMAGE_RELAX_VAR_CHECK
|
1676 DISSECT_IMAGE_FSCK
|
1677 DISSECT_IMAGE_GROWFS
,
1684 arg_root
= strdup(mounted_dir
);
1688 rfd
= open(empty_to_root(arg_root
), O_DIRECTORY
|O_CLOEXEC
);
1690 return log_error_errno(errno
, "Failed to open %s: %m", empty_to_root(arg_root
));
1693 LOG_SET_PREFIX(arg_image
?: arg_root
);
1695 /* We check these conditions here instead of in parse_argv() so that we can take the root directory
1698 if (arg_locale
&& !locale_is_ok(rfd
, arg_locale
))
1699 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale
);
1700 if (arg_locale_messages
&& !locale_is_ok(rfd
, arg_locale_messages
))
1701 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale_messages
);
1703 if (arg_root_shell
) {
1704 r
= find_shell(rfd
, arg_root_shell
);
1709 r
= process_reset(rfd
);
1713 r
= process_locale(rfd
);
1716 if (r
> 0 && !offline
)
1717 (void) reload_system_manager(&bus
);
1719 r
= process_keymap(rfd
);
1722 if (r
> 0 && !offline
)
1723 (void) reload_vconsole(&bus
);
1725 r
= process_timezone(rfd
);
1729 r
= process_hostname(rfd
);
1733 r
= process_machine_id(rfd
);
1737 r
= process_root_account(rfd
);
1741 r
= process_kernel_cmdline(rfd
);
1748 DEFINE_MAIN_FUNCTION(run
);