1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "ask-password-api.h"
16 #include "hostname-util.h"
18 #include "libcrypt-util.h"
19 #include "locale-util.h"
20 #include "main-func.h"
21 #include "memory-util.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "pretty-print.h"
27 #include "proc-cmdline.h"
28 #include "random-util.h"
29 #include "string-util.h"
31 #include "terminal-util.h"
32 #include "time-util.h"
33 #include "umask-util.h"
34 #include "user-util.h"
36 static char *arg_root
= NULL
;
37 static char *arg_locale
= NULL
; /* $LANG */
38 static char *arg_keymap
= NULL
;
39 static char *arg_locale_messages
= NULL
; /* $LC_MESSAGES */
40 static char *arg_timezone
= NULL
;
41 static char *arg_hostname
= NULL
;
42 static sd_id128_t arg_machine_id
= {};
43 static char *arg_root_password
= NULL
;
44 static bool arg_prompt_locale
= false;
45 static bool arg_prompt_keymap
= false;
46 static bool arg_prompt_timezone
= false;
47 static bool arg_prompt_hostname
= false;
48 static bool arg_prompt_root_password
= false;
49 static bool arg_copy_locale
= false;
50 static bool arg_copy_keymap
= false;
51 static bool arg_copy_timezone
= false;
52 static bool arg_copy_root_password
= false;
54 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
55 STATIC_DESTRUCTOR_REGISTER(arg_locale
, freep
);
56 STATIC_DESTRUCTOR_REGISTER(arg_locale_messages
, freep
);
57 STATIC_DESTRUCTOR_REGISTER(arg_keymap
, freep
);
58 STATIC_DESTRUCTOR_REGISTER(arg_timezone
, freep
);
59 STATIC_DESTRUCTOR_REGISTER(arg_hostname
, freep
);
60 STATIC_DESTRUCTOR_REGISTER(arg_root_password
, erase_and_freep
);
62 static bool press_any_key(void) {
66 printf("-- Press any key to proceed --");
69 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
77 static void print_welcome(void) {
78 _cleanup_free_
char *pretty_name
= NULL
, *ansi_color
= NULL
;
79 static bool done
= false;
88 "PRETTY_NAME", &pretty_name
,
89 "ANSI_COLOR", &ansi_color
,
92 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
93 "Failed to read os-release file, ignoring: %m");
95 pn
= isempty(pretty_name
) ? "Linux" : pretty_name
;
98 printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ansi_color
, pn
);
100 printf("\nWelcome to your new installation of %s!\n", pn
);
102 printf("\nPlease configure your system!\n\n");
109 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
110 unsigned break_lines
, break_modulo
;
111 size_t n
, per_column
, i
, j
;
113 assert(n_columns
> 0);
116 per_column
= DIV_ROUND_UP(n
, n_columns
);
118 break_lines
= lines();
122 /* The first page gets two extra lines, since we want to show
124 break_modulo
= break_lines
;
125 if (break_modulo
> 3)
128 for (i
= 0; i
< per_column
; i
++) {
130 for (j
= 0; j
< n_columns
; j
++) {
131 _cleanup_free_
char *e
= NULL
;
133 if (j
* per_column
+ i
>= n
)
136 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
140 printf("%4zu) %-*s", j
* per_column
+ i
+ 1, width
, e
);
145 /* on the first screen we reserve 2 extra lines for the title */
146 if (i
% break_lines
== break_modulo
) {
147 if (!press_any_key())
155 static int prompt_loop(const char *text
, char **l
, unsigned percentage
, bool (*is_valid
)(const char *name
), char **ret
) {
163 _cleanup_free_
char *p
= NULL
;
166 r
= ask_string(&p
, "%s %s (empty to skip, \"list\" to list options): ",
167 special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), text
);
169 return log_error_errno(r
, "Failed to query user: %m");
172 log_warning("No data entered, skipping.");
176 if (streq(p
, "list")) {
177 r
= show_menu(l
, 3, 22, percentage
);
185 r
= safe_atou(p
, &u
);
187 if (u
<= 0 || u
> strv_length(l
)) {
188 log_error("Specified entry number out of range.");
192 log_info("Selected '%s'.", l
[u
-1]);
193 if (free_and_strdup(ret
, l
[u
-1]) < 0)
200 log_error("Entered data invalid.");
204 return free_and_replace(*ret
, p
);
208 static int prompt_locale(void) {
209 _cleanup_strv_free_
char **locales
= NULL
;
212 if (arg_locale
|| arg_locale_messages
)
215 if (!arg_prompt_locale
)
218 r
= get_locales(&locales
);
220 return log_error_errno(r
, "Cannot query locales list: %m");
222 if (strv_isempty(locales
))
223 log_debug("No locales found, skipping locale selection.");
224 else if (strv_length(locales
) == 1) {
226 if (streq(locales
[0], SYSTEMD_DEFAULT_LOCALE
))
227 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
229 log_debug("Only a single locale available (%s), selecting it as default.", locales
[0]);
231 arg_locale
= strdup(locales
[0]);
235 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
240 r
= prompt_loop("Please enter system locale name or number",
241 locales
, 60, locale_is_valid
, &arg_locale
);
245 if (isempty(arg_locale
))
248 r
= prompt_loop("Please enter system message locale name or number",
249 locales
, 60, locale_is_valid
, &arg_locale_messages
);
253 /* Suppress the messages setting if it's the same as the main locale anyway */
254 if (streq_ptr(arg_locale
, arg_locale_messages
))
255 arg_locale_messages
= mfree(arg_locale_messages
);
261 static int process_locale(void) {
262 const char *etc_localeconf
;
267 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
268 if (laccess(etc_localeconf
, F_OK
) >= 0)
271 if (arg_copy_locale
&& arg_root
) {
273 (void) mkdir_parents(etc_localeconf
, 0755);
274 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, 0, COPY_REFLINK
);
277 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
279 log_info("%s copied.", etc_localeconf
);
288 if (!isempty(arg_locale
))
289 locales
[i
++] = strjoina("LANG=", arg_locale
);
290 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
291 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
298 (void) mkdir_parents(etc_localeconf
, 0755);
299 r
= write_env_file(etc_localeconf
, locales
);
301 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
303 log_info("%s written.", etc_localeconf
);
307 static int prompt_keymap(void) {
308 _cleanup_strv_free_
char **kmaps
= NULL
;
314 if (!arg_prompt_keymap
)
317 r
= get_keymaps(&kmaps
);
318 if (r
== -ENOENT
) /* no keymaps installed */
321 return log_error_errno(r
, "Failed to read keymaps: %m");
325 return prompt_loop("Please enter system keymap name or number",
326 kmaps
, 60, keymap_is_valid
, &arg_keymap
);
329 static int process_keymap(void) {
330 const char *etc_vconsoleconf
;
334 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
335 if (laccess(etc_vconsoleconf
, F_OK
) >= 0)
338 if (arg_copy_keymap
&& arg_root
) {
340 (void) mkdir_parents(etc_vconsoleconf
, 0755);
341 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, 0, COPY_REFLINK
);
344 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
346 log_info("%s copied.", etc_vconsoleconf
);
353 return 0; /* don't fail if no keymaps are installed */
357 if (isempty(arg_keymap
))
360 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
362 r
= mkdir_parents(etc_vconsoleconf
, 0755);
364 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
366 r
= write_env_file(etc_vconsoleconf
, keymap
);
368 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
370 log_info("%s written.", etc_vconsoleconf
);
374 static bool timezone_is_valid_log_error(const char *name
) {
375 return timezone_is_valid(name
, LOG_ERR
);
378 static int prompt_timezone(void) {
379 _cleanup_strv_free_
char **zones
= NULL
;
385 if (!arg_prompt_timezone
)
388 r
= get_timezones(&zones
);
390 return log_error_errno(r
, "Cannot query timezone list: %m");
394 r
= prompt_loop("Please enter timezone name or number",
395 zones
, 30, timezone_is_valid_log_error
, &arg_timezone
);
402 static int process_timezone(void) {
403 const char *etc_localtime
, *e
;
406 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
407 if (laccess(etc_localtime
, F_OK
) >= 0)
410 if (arg_copy_timezone
&& arg_root
) {
411 _cleanup_free_
char *p
= NULL
;
413 r
= readlink_malloc("/etc/localtime", &p
);
416 return log_error_errno(r
, "Failed to read host timezone: %m");
418 (void) mkdir_parents(etc_localtime
, 0755);
419 if (symlink(p
, etc_localtime
) < 0)
420 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
422 log_info("%s copied.", etc_localtime
);
427 r
= prompt_timezone();
431 if (isempty(arg_timezone
))
434 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
436 (void) mkdir_parents(etc_localtime
, 0755);
437 if (symlink(e
, etc_localtime
) < 0)
438 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
440 log_info("%s written", etc_localtime
);
444 static int prompt_hostname(void) {
450 if (!arg_prompt_hostname
)
457 _cleanup_free_
char *h
= NULL
;
459 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
461 return log_error_errno(r
, "Failed to query hostname: %m");
464 log_warning("No hostname entered, skipping.");
468 if (!hostname_is_valid(h
, true)) {
469 log_error("Specified hostname invalid.");
473 /* Get rid of the trailing dot that we allow, but don't want to see */
474 arg_hostname
= hostname_cleanup(h
);
482 static int process_hostname(void) {
483 const char *etc_hostname
;
486 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
487 if (laccess(etc_hostname
, F_OK
) >= 0)
490 r
= prompt_hostname();
494 if (isempty(arg_hostname
))
497 r
= write_string_file(etc_hostname
, arg_hostname
,
498 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
);
500 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
502 log_info("%s written.", etc_hostname
);
506 static int process_machine_id(void) {
507 const char *etc_machine_id
;
508 char id
[SD_ID128_STRING_MAX
];
511 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
512 if (laccess(etc_machine_id
, F_OK
) >= 0)
515 if (sd_id128_is_null(arg_machine_id
))
518 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
519 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
);
521 return log_error_errno(r
, "Failed to write machine id: %m");
523 log_info("%s written.", etc_machine_id
);
527 static int prompt_root_password(void) {
528 const char *msg1
, *msg2
, *etc_shadow
;
531 if (arg_root_password
)
534 if (!arg_prompt_root_password
)
537 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
538 if (laccess(etc_shadow
, F_OK
) >= 0)
544 msg1
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip):");
545 msg2
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter new root password again:");
548 _cleanup_strv_free_erase_
char **a
= NULL
, **b
= NULL
;
550 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
552 return log_error_errno(r
, "Failed to query root password: %m");
553 if (strv_length(a
) != 1) {
554 log_warning("Received multiple passwords, where we expected one.");
559 log_warning("No password entered, skipping.");
563 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
565 return log_error_errno(r
, "Failed to query root password: %m");
567 if (!streq(*a
, *b
)) {
568 log_error("Entered passwords did not match, please try again.");
572 arg_root_password
= TAKE_PTR(*a
);
579 static int write_root_shadow(const char *path
, const struct spwd
*p
) {
580 _cleanup_fclose_
FILE *f
= NULL
;
587 f
= fopen(path
, "wex");
591 r
= putspent_sane(p
, f
);
595 return fflush_sync_and_check(f
);
598 static int process_root_password(void) {
601 .sp_namp
= (char*) "root",
607 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
609 _cleanup_free_
char *salt
= NULL
;
610 _cleanup_close_
int lock
= -1;
611 struct crypt_data cd
= {};
613 const char *etc_shadow
;
616 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
617 if (laccess(etc_shadow
, F_OK
) >= 0)
620 (void) mkdir_parents(etc_shadow
, 0755);
622 lock
= take_etc_passwd_lock(arg_root
);
624 return log_error_errno(lock
, "Failed to take a lock: %m");
626 if (arg_copy_root_password
&& arg_root
) {
630 p
= getspnam("root");
631 if (p
|| errno
!= ENOENT
) {
636 return log_error_errno(errno
, "Failed to find shadow entry for root: %m");
639 r
= write_root_shadow(etc_shadow
, p
);
641 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
643 log_info("%s copied.", etc_shadow
);
648 r
= prompt_root_password();
652 if (!arg_root_password
)
655 r
= make_salt(&salt
);
657 return log_error_errno(r
, "Failed to get salt: %m");
660 item
.sp_pwdp
= crypt_r(arg_root_password
, salt
, &cd
);
662 return log_error_errno(errno
== 0 ? SYNTHETIC_ERRNO(EINVAL
) : errno
,
663 "Failed to encrypt password: %m");
665 item
.sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
667 r
= write_root_shadow(etc_shadow
, &item
);
669 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
671 log_info("%s written.", etc_shadow
);
675 static int help(void) {
676 _cleanup_free_
char *link
= NULL
;
679 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
683 printf("%s [OPTIONS...]\n\n"
684 "Configures basic settings of the system.\n\n"
685 " -h --help Show this help\n"
686 " --version Show package version\n"
687 " --root=PATH Operate on an alternate filesystem root\n"
688 " --locale=LOCALE Set primary locale (LANG=)\n"
689 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
690 " --keymap=KEYMAP Set keymap\n"
691 " --timezone=TIMEZONE Set timezone\n"
692 " --hostname=NAME Set host name\n"
693 " --machine-ID=ID Set machine ID\n"
694 " --root-password=PASSWORD Set root password\n"
695 " --root-password-file=FILE Set root password from file\n"
696 " --prompt-locale Prompt the user for locale settings\n"
697 " --prompt-keymap Prompt the user for keymap settings\n"
698 " --prompt-timezone Prompt the user for timezone\n"
699 " --prompt-hostname Prompt the user for hostname\n"
700 " --prompt-root-password Prompt the user for root password\n"
701 " --prompt Prompt for all of the above\n"
702 " --copy-locale Copy locale from host\n"
703 " --copy-keymap Copy keymap from host\n"
704 " --copy-timezone Copy timezone from host\n"
705 " --copy-root-password Copy root password from host\n"
706 " --copy Copy locale, keymap, timezone, root password\n"
707 " --setup-machine-id Generate a new random machine ID\n"
708 "\nSee the %s for details.\n"
709 , program_invocation_short_name
716 static int parse_argv(int argc
, char *argv
[]) {
728 ARG_ROOT_PASSWORD_FILE
,
734 ARG_PROMPT_ROOT_PASSWORD
,
739 ARG_COPY_ROOT_PASSWORD
,
740 ARG_SETUP_MACHINE_ID
,
743 static const struct option options
[] = {
744 { "help", no_argument
, NULL
, 'h' },
745 { "version", no_argument
, NULL
, ARG_VERSION
},
746 { "root", required_argument
, NULL
, ARG_ROOT
},
747 { "locale", required_argument
, NULL
, ARG_LOCALE
},
748 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
749 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
750 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
751 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
752 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
753 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
754 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
755 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
756 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
757 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
758 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
759 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
760 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
761 { "copy", no_argument
, NULL
, ARG_COPY
},
762 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
763 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
764 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
765 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
766 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
775 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
786 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
792 if (!locale_is_valid(optarg
))
793 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
794 "Locale %s is not valid.", optarg
);
796 r
= free_and_strdup(&arg_locale
, optarg
);
802 case ARG_LOCALE_MESSAGES
:
803 if (!locale_is_valid(optarg
))
804 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
805 "Locale %s is not valid.", optarg
);
807 r
= free_and_strdup(&arg_locale_messages
, optarg
);
814 if (!keymap_is_valid(optarg
))
815 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
816 "Keymap %s is not valid.", optarg
);
818 r
= free_and_strdup(&arg_keymap
, optarg
);
825 if (!timezone_is_valid(optarg
, LOG_ERR
))
826 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
827 "Timezone %s is not valid.", optarg
);
829 r
= free_and_strdup(&arg_timezone
, optarg
);
835 case ARG_ROOT_PASSWORD
:
836 r
= free_and_strdup(&arg_root_password
, optarg
);
841 case ARG_ROOT_PASSWORD_FILE
:
842 arg_root_password
= mfree(arg_root_password
);
844 r
= read_one_line_file(optarg
, &arg_root_password
);
846 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
851 if (!hostname_is_valid(optarg
, true))
852 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
853 "Host name %s is not valid.", optarg
);
855 hostname_cleanup(optarg
);
856 r
= free_and_strdup(&arg_hostname
, optarg
);
863 if (sd_id128_from_string(optarg
, &arg_machine_id
) < 0)
864 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
865 "Failed to parse machine id %s.", optarg
);
870 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
= arg_prompt_root_password
= true;
873 case ARG_PROMPT_LOCALE
:
874 arg_prompt_locale
= true;
877 case ARG_PROMPT_KEYMAP
:
878 arg_prompt_keymap
= true;
881 case ARG_PROMPT_TIMEZONE
:
882 arg_prompt_timezone
= true;
885 case ARG_PROMPT_HOSTNAME
:
886 arg_prompt_hostname
= true;
889 case ARG_PROMPT_ROOT_PASSWORD
:
890 arg_prompt_root_password
= true;
894 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
= true;
897 case ARG_COPY_LOCALE
:
898 arg_copy_locale
= true;
901 case ARG_COPY_KEYMAP
:
902 arg_copy_keymap
= true;
905 case ARG_COPY_TIMEZONE
:
906 arg_copy_timezone
= true;
909 case ARG_COPY_ROOT_PASSWORD
:
910 arg_copy_root_password
= true;
913 case ARG_SETUP_MACHINE_ID
:
915 r
= sd_id128_randomize(&arg_machine_id
);
917 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
925 assert_not_reached("Unhandled option");
931 static int run(int argc
, char *argv
[]) {
935 r
= parse_argv(argc
, argv
);
943 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
945 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
946 if (r
> 0 && !enabled
)
947 return 0; /* disabled */
949 r
= process_locale();
953 r
= process_keymap();
957 r
= process_timezone();
961 r
= process_hostname();
965 r
= process_machine_id();
969 r
= process_root_password();
976 DEFINE_MAIN_FUNCTION(run
);