1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 /* libxcrypt is a replacement for glibc's libcrypt, and libcrypt might be
9 * removed from glibc at some point. As part of the removal, defines for
10 * crypt(3) are dropped from unistd.h, and we must include crypt.h instead.
12 * Newer versions of glibc (v2.0+) already ship crypt.h with a definition
13 * of crypt(3) as well, so we simply include it if it is present. MariaDB,
14 * MySQL, PostgreSQL, Perl and some other wide-spread packages do it the
15 * same way since ages without any problems.
22 #include "alloc-util.h"
23 #include "ask-password-api.h"
29 #include "hostname-util.h"
31 #include "locale-util.h"
32 #include "main-func.h"
33 #include "memory-util.h"
36 #include "parse-util.h"
37 #include "path-util.h"
38 #include "pretty-print.h"
39 #include "proc-cmdline.h"
40 #include "random-util.h"
41 #include "string-util.h"
43 #include "terminal-util.h"
44 #include "time-util.h"
45 #include "umask-util.h"
46 #include "user-util.h"
48 static char *arg_root
= NULL
;
49 static char *arg_locale
= NULL
; /* $LANG */
50 static char *arg_keymap
= NULL
;
51 static char *arg_locale_messages
= NULL
; /* $LC_MESSAGES */
52 static char *arg_timezone
= NULL
;
53 static char *arg_hostname
= NULL
;
54 static sd_id128_t arg_machine_id
= {};
55 static char *arg_root_password
= NULL
;
56 static bool arg_prompt_locale
= false;
57 static bool arg_prompt_keymap
= false;
58 static bool arg_prompt_timezone
= false;
59 static bool arg_prompt_hostname
= false;
60 static bool arg_prompt_root_password
= false;
61 static bool arg_copy_locale
= false;
62 static bool arg_copy_keymap
= false;
63 static bool arg_copy_timezone
= false;
64 static bool arg_copy_root_password
= false;
66 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
67 STATIC_DESTRUCTOR_REGISTER(arg_locale
, freep
);
68 STATIC_DESTRUCTOR_REGISTER(arg_locale_messages
, freep
);
69 STATIC_DESTRUCTOR_REGISTER(arg_keymap
, freep
);
70 STATIC_DESTRUCTOR_REGISTER(arg_timezone
, freep
);
71 STATIC_DESTRUCTOR_REGISTER(arg_hostname
, freep
);
72 STATIC_DESTRUCTOR_REGISTER(arg_root_password
, erase_and_freep
);
74 static bool press_any_key(void) {
78 printf("-- Press any key to proceed --");
81 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
89 static void print_welcome(void) {
90 _cleanup_free_
char *pretty_name
= NULL
;
91 static bool done
= false;
97 r
= parse_os_release(arg_root
, "PRETTY_NAME", &pretty_name
, NULL
);
99 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
100 "Failed to read os-release file, ignoring: %m");
102 printf("\nWelcome to your new installation of %s!\nPlease configure a few basic system settings:\n\n",
103 isempty(pretty_name
) ? "Linux" : pretty_name
);
110 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
111 unsigned break_lines
, break_modulo
;
112 size_t n
, per_column
, i
, j
;
114 assert(n_columns
> 0);
117 per_column
= DIV_ROUND_UP(n
, n_columns
);
119 break_lines
= lines();
123 /* The first page gets two extra lines, since we want to show
125 break_modulo
= break_lines
;
126 if (break_modulo
> 3)
129 for (i
= 0; i
< per_column
; i
++) {
131 for (j
= 0; j
< n_columns
; j
++) {
132 _cleanup_free_
char *e
= NULL
;
134 if (j
* per_column
+ i
>= n
)
137 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
141 printf("%4zu) %-*s", j
* per_column
+ i
+ 1, width
, e
);
146 /* on the first screen we reserve 2 extra lines for the title */
147 if (i
% break_lines
== break_modulo
) {
148 if (!press_any_key())
156 static int prompt_loop(const char *text
, char **l
, bool (*is_valid
)(const char *name
), char **ret
) {
164 _cleanup_free_
char *p
= NULL
;
167 r
= ask_string(&p
, "%s %s (empty to skip): ", 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 r
= safe_atou(p
, &u
);
180 if (u
<= 0 || u
> strv_length(l
)) {
181 log_error("Specified entry number out of range.");
185 log_info("Selected '%s'.", l
[u
-1]);
197 log_error("Entered data invalid.");
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 printf("\nAvailable Locales:\n\n");
241 r
= show_menu(locales
, 3, 22, 60);
247 r
= prompt_loop("Please enter system locale name or number", locales
, locale_is_valid
, &arg_locale
);
251 if (isempty(arg_locale
))
254 r
= prompt_loop("Please enter system message locale name or number", locales
, locale_is_valid
, &arg_locale_messages
);
258 /* Suppress the messages setting if it's the same as the main locale anyway */
259 if (streq_ptr(arg_locale
, arg_locale_messages
))
260 arg_locale_messages
= mfree(arg_locale_messages
);
266 static int process_locale(void) {
267 const char *etc_localeconf
;
272 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
273 if (laccess(etc_localeconf
, F_OK
) >= 0)
276 if (arg_copy_locale
&& arg_root
) {
278 (void) mkdir_parents(etc_localeconf
, 0755);
279 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, 0, COPY_REFLINK
);
282 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
284 log_info("%s copied.", etc_localeconf
);
293 if (!isempty(arg_locale
))
294 locales
[i
++] = strjoina("LANG=", arg_locale
);
295 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
296 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
303 (void) mkdir_parents(etc_localeconf
, 0755);
304 r
= write_env_file(etc_localeconf
, locales
);
306 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
308 log_info("%s written.", etc_localeconf
);
312 static int prompt_keymap(void) {
313 _cleanup_strv_free_
char **kmaps
= NULL
;
319 if (!arg_prompt_keymap
)
322 r
= get_keymaps(&kmaps
);
323 if (r
== -ENOENT
) /* no keymaps installed */
326 return log_error_errno(r
, "Failed to read keymaps: %m");
330 printf("\nAvailable keymaps:\n\n");
331 r
= show_menu(kmaps
, 3, 22, 60);
337 return prompt_loop("Please enter system keymap name or number",
338 kmaps
, keymap_is_valid
, &arg_keymap
);
341 static int process_keymap(void) {
342 const char *etc_vconsoleconf
;
346 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
347 if (laccess(etc_vconsoleconf
, F_OK
) >= 0)
350 if (arg_copy_keymap
&& arg_root
) {
352 (void) mkdir_parents(etc_vconsoleconf
, 0755);
353 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, 0, COPY_REFLINK
);
356 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
358 log_info("%s copied.", etc_vconsoleconf
);
365 return 0; /* don't fail if no keymaps are installed */
369 if (isempty(arg_keymap
))
372 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
374 r
= mkdir_parents(etc_vconsoleconf
, 0755);
376 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
378 r
= write_env_file(etc_vconsoleconf
, keymap
);
380 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
382 log_info("%s written.", etc_vconsoleconf
);
386 static bool timezone_is_valid_log_error(const char *name
) {
387 return timezone_is_valid(name
, LOG_ERR
);
390 static int prompt_timezone(void) {
391 _cleanup_strv_free_
char **zones
= NULL
;
397 if (!arg_prompt_timezone
)
400 r
= get_timezones(&zones
);
402 return log_error_errno(r
, "Cannot query timezone list: %m");
406 printf("\nAvailable Time Zones:\n\n");
407 r
= show_menu(zones
, 3, 22, 30);
413 r
= prompt_loop("Please enter timezone name or number", zones
, timezone_is_valid_log_error
, &arg_timezone
);
420 static int process_timezone(void) {
421 const char *etc_localtime
, *e
;
424 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
425 if (laccess(etc_localtime
, F_OK
) >= 0)
428 if (arg_copy_timezone
&& arg_root
) {
429 _cleanup_free_
char *p
= NULL
;
431 r
= readlink_malloc("/etc/localtime", &p
);
434 return log_error_errno(r
, "Failed to read host timezone: %m");
436 (void) mkdir_parents(etc_localtime
, 0755);
437 if (symlink(p
, etc_localtime
) < 0)
438 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
440 log_info("%s copied.", etc_localtime
);
445 r
= prompt_timezone();
449 if (isempty(arg_timezone
))
452 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
454 (void) mkdir_parents(etc_localtime
, 0755);
455 if (symlink(e
, etc_localtime
) < 0)
456 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
458 log_info("%s written", etc_localtime
);
462 static int prompt_hostname(void) {
468 if (!arg_prompt_hostname
)
475 _cleanup_free_
char *h
= NULL
;
477 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
479 return log_error_errno(r
, "Failed to query hostname: %m");
482 log_warning("No hostname entered, skipping.");
486 if (!hostname_is_valid(h
, true)) {
487 log_error("Specified hostname invalid.");
491 /* Get rid of the trailing dot that we allow, but don't want to see */
492 arg_hostname
= hostname_cleanup(h
);
500 static int process_hostname(void) {
501 const char *etc_hostname
;
504 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
505 if (laccess(etc_hostname
, F_OK
) >= 0)
508 r
= prompt_hostname();
512 if (isempty(arg_hostname
))
515 r
= write_string_file(etc_hostname
, arg_hostname
,
516 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
);
518 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
520 log_info("%s written.", etc_hostname
);
524 static int process_machine_id(void) {
525 const char *etc_machine_id
;
526 char id
[SD_ID128_STRING_MAX
];
529 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
530 if (laccess(etc_machine_id
, F_OK
) >= 0)
533 if (sd_id128_is_null(arg_machine_id
))
536 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
537 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
);
539 return log_error_errno(r
, "Failed to write machine id: %m");
541 log_info("%s written.", etc_machine_id
);
545 static int prompt_root_password(void) {
546 const char *msg1
, *msg2
, *etc_shadow
;
549 if (arg_root_password
)
552 if (!arg_prompt_root_password
)
555 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
556 if (laccess(etc_shadow
, F_OK
) >= 0)
562 msg1
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip): ");
563 msg2
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter new root password again: ");
566 _cleanup_strv_free_erase_
char **a
= NULL
, **b
= NULL
;
568 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
570 return log_error_errno(r
, "Failed to query root password: %m");
571 if (strv_length(a
) != 1) {
572 log_warning("Received multiple passwords, where we expected one.");
577 log_warning("No password entered, skipping.");
581 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
583 return log_error_errno(r
, "Failed to query root password: %m");
585 if (!streq(*a
, *b
)) {
586 log_error("Entered passwords did not match, please try again.");
590 arg_root_password
= TAKE_PTR(*a
);
597 static int write_root_shadow(const char *path
, const struct spwd
*p
) {
598 _cleanup_fclose_
FILE *f
= NULL
;
605 f
= fopen(path
, "wex");
609 r
= putspent_sane(p
, f
);
613 return fflush_sync_and_check(f
);
616 static int process_root_password(void) {
619 .sp_namp
= (char*) "root",
625 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
627 _cleanup_free_
char *salt
= NULL
;
628 _cleanup_close_
int lock
= -1;
629 struct crypt_data cd
= {};
631 const char *etc_shadow
;
634 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
635 if (laccess(etc_shadow
, F_OK
) >= 0)
638 (void) mkdir_parents(etc_shadow
, 0755);
640 lock
= take_etc_passwd_lock(arg_root
);
642 return log_error_errno(lock
, "Failed to take a lock: %m");
644 if (arg_copy_root_password
&& arg_root
) {
648 p
= getspnam("root");
649 if (p
|| errno
!= ENOENT
) {
654 return log_error_errno(errno
, "Failed to find shadow entry for root: %m");
657 r
= write_root_shadow(etc_shadow
, p
);
659 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
661 log_info("%s copied.", etc_shadow
);
666 r
= prompt_root_password();
670 if (!arg_root_password
)
673 r
= make_salt(&salt
);
675 return log_error_errno(r
, "Failed to get salt: %m");
678 item
.sp_pwdp
= crypt_r(arg_root_password
, salt
, &cd
);
680 return log_error_errno(errno
== 0 ? SYNTHETIC_ERRNO(EINVAL
) : errno
,
681 "Failed to encrypt password: %m");
683 item
.sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
685 r
= write_root_shadow(etc_shadow
, &item
);
687 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
689 log_info("%s written.", etc_shadow
);
693 static int help(void) {
694 _cleanup_free_
char *link
= NULL
;
697 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
701 printf("%s [OPTIONS...]\n\n"
702 "Configures basic settings of the system.\n\n"
703 " -h --help Show this help\n"
704 " --version Show package version\n"
705 " --root=PATH Operate on an alternate filesystem root\n"
706 " --locale=LOCALE Set primary locale (LANG=)\n"
707 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
708 " --keymap=KEYMAP Set keymap\n"
709 " --timezone=TIMEZONE Set timezone\n"
710 " --hostname=NAME Set host name\n"
711 " --machine-ID=ID Set machine ID\n"
712 " --root-password=PASSWORD Set root password\n"
713 " --root-password-file=FILE Set root password from file\n"
714 " --prompt-locale Prompt the user for locale settings\n"
715 " --prompt-keymap Prompt the user for keymap settings\n"
716 " --prompt-timezone Prompt the user for timezone\n"
717 " --prompt-hostname Prompt the user for hostname\n"
718 " --prompt-root-password Prompt the user for root password\n"
719 " --prompt Prompt for all of the above\n"
720 " --copy-locale Copy locale from host\n"
721 " --copy-keymap Copy keymap from host\n"
722 " --copy-timezone Copy timezone from host\n"
723 " --copy-root-password Copy root password from host\n"
724 " --copy Copy locale, keymap, timezone, root password\n"
725 " --setup-machine-id Generate a new random machine ID\n"
726 "\nSee the %s for details.\n"
727 , program_invocation_short_name
734 static int parse_argv(int argc
, char *argv
[]) {
746 ARG_ROOT_PASSWORD_FILE
,
752 ARG_PROMPT_ROOT_PASSWORD
,
757 ARG_COPY_ROOT_PASSWORD
,
758 ARG_SETUP_MACHINE_ID
,
761 static const struct option options
[] = {
762 { "help", no_argument
, NULL
, 'h' },
763 { "version", no_argument
, NULL
, ARG_VERSION
},
764 { "root", required_argument
, NULL
, ARG_ROOT
},
765 { "locale", required_argument
, NULL
, ARG_LOCALE
},
766 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
767 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
768 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
769 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
770 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
771 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
772 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
773 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
774 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
775 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
776 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
777 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
778 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
779 { "copy", no_argument
, NULL
, ARG_COPY
},
780 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
781 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
782 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
783 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
784 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
793 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
804 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
810 if (!locale_is_valid(optarg
))
811 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
812 "Locale %s is not valid.", optarg
);
814 r
= free_and_strdup(&arg_locale
, optarg
);
820 case ARG_LOCALE_MESSAGES
:
821 if (!locale_is_valid(optarg
))
822 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
823 "Locale %s is not valid.", optarg
);
825 r
= free_and_strdup(&arg_locale_messages
, optarg
);
832 if (!keymap_is_valid(optarg
))
833 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
834 "Keymap %s is not valid.", optarg
);
836 r
= free_and_strdup(&arg_keymap
, optarg
);
843 if (!timezone_is_valid(optarg
, LOG_ERR
))
844 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
845 "Timezone %s is not valid.", optarg
);
847 r
= free_and_strdup(&arg_timezone
, optarg
);
853 case ARG_ROOT_PASSWORD
:
854 r
= free_and_strdup(&arg_root_password
, optarg
);
859 case ARG_ROOT_PASSWORD_FILE
:
860 arg_root_password
= mfree(arg_root_password
);
862 r
= read_one_line_file(optarg
, &arg_root_password
);
864 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
869 if (!hostname_is_valid(optarg
, true))
870 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
871 "Host name %s is not valid.", optarg
);
873 hostname_cleanup(optarg
);
874 r
= free_and_strdup(&arg_hostname
, optarg
);
881 if (sd_id128_from_string(optarg
, &arg_machine_id
) < 0)
882 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
883 "Failed to parse machine id %s.", optarg
);
888 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
= arg_prompt_root_password
= true;
891 case ARG_PROMPT_LOCALE
:
892 arg_prompt_locale
= true;
895 case ARG_PROMPT_KEYMAP
:
896 arg_prompt_keymap
= true;
899 case ARG_PROMPT_TIMEZONE
:
900 arg_prompt_timezone
= true;
903 case ARG_PROMPT_HOSTNAME
:
904 arg_prompt_hostname
= true;
907 case ARG_PROMPT_ROOT_PASSWORD
:
908 arg_prompt_root_password
= true;
912 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
= true;
915 case ARG_COPY_LOCALE
:
916 arg_copy_locale
= true;
919 case ARG_COPY_KEYMAP
:
920 arg_copy_keymap
= true;
923 case ARG_COPY_TIMEZONE
:
924 arg_copy_timezone
= true;
927 case ARG_COPY_ROOT_PASSWORD
:
928 arg_copy_root_password
= true;
931 case ARG_SETUP_MACHINE_ID
:
933 r
= sd_id128_randomize(&arg_machine_id
);
935 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
943 assert_not_reached("Unhandled option");
949 static int run(int argc
, char *argv
[]) {
953 r
= parse_argv(argc
, argv
);
961 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
963 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
964 if (r
> 0 && !enabled
)
965 return 0; /* disabled */
967 r
= process_locale();
971 r
= process_keymap();
975 r
= process_timezone();
979 r
= process_hostname();
983 r
= process_machine_id();
987 r
= process_root_password();
994 DEFINE_MAIN_FUNCTION(run
);