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"
28 #include "hostname-util.h"
29 #include "locale-util.h"
30 #include "main-func.h"
33 #include "parse-util.h"
34 #include "path-util.h"
35 #include "pretty-print.h"
36 #include "proc-cmdline.h"
37 #include "random-util.h"
38 #include "string-util.h"
40 #include "terminal-util.h"
41 #include "time-util.h"
42 #include "umask-util.h"
43 #include "user-util.h"
45 static char *arg_root
= NULL
;
46 static char *arg_locale
= NULL
; /* $LANG */
47 static char *arg_keymap
= NULL
;
48 static char *arg_locale_messages
= NULL
; /* $LC_MESSAGES */
49 static char *arg_timezone
= NULL
;
50 static char *arg_hostname
= NULL
;
51 static sd_id128_t arg_machine_id
= {};
52 static char *arg_root_password
= 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;
63 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
64 STATIC_DESTRUCTOR_REGISTER(arg_locale
, freep
);
65 STATIC_DESTRUCTOR_REGISTER(arg_locale_messages
, freep
);
66 STATIC_DESTRUCTOR_REGISTER(arg_keymap
, freep
);
67 STATIC_DESTRUCTOR_REGISTER(arg_timezone
, freep
);
68 STATIC_DESTRUCTOR_REGISTER(arg_hostname
, freep
);
69 STATIC_DESTRUCTOR_REGISTER(arg_root_password
, string_free_erasep
);
71 static bool press_any_key(void) {
75 printf("-- Press any key to proceed --");
78 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
86 static void print_welcome(void) {
87 _cleanup_free_
char *pretty_name
= NULL
;
88 static bool done
= false;
94 r
= parse_os_release(arg_root
, "PRETTY_NAME", &pretty_name
, NULL
);
96 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
97 "Failed to read os-release file, ignoring: %m");
99 printf("\nWelcome to your new installation of %s!\nPlease configure a few basic system settings:\n\n",
100 isempty(pretty_name
) ? "Linux" : pretty_name
);
107 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
108 unsigned break_lines
, break_modulo
;
109 size_t n
, per_column
, i
, j
;
111 assert(n_columns
> 0);
114 per_column
= DIV_ROUND_UP(n
, n_columns
);
116 break_lines
= lines();
120 /* The first page gets two extra lines, since we want to show
122 break_modulo
= break_lines
;
123 if (break_modulo
> 3)
126 for (i
= 0; i
< per_column
; i
++) {
128 for (j
= 0; j
< n_columns
; j
++) {
129 _cleanup_free_
char *e
= NULL
;
131 if (j
* per_column
+ i
>= n
)
134 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
138 printf("%4zu) %-*s", j
* per_column
+ i
+ 1, width
, e
);
143 /* on the first screen we reserve 2 extra lines for the title */
144 if (i
% break_lines
== break_modulo
) {
145 if (!press_any_key())
153 static int prompt_loop(const char *text
, char **l
, bool (*is_valid
)(const char *name
), char **ret
) {
161 _cleanup_free_
char *p
= NULL
;
164 r
= ask_string(&p
, "%s %s (empty to skip): ", special_glyph(TRIANGULAR_BULLET
), text
);
166 return log_error_errno(r
, "Failed to query user: %m");
169 log_warning("No data entered, skipping.");
173 r
= safe_atou(p
, &u
);
177 if (u
<= 0 || u
> strv_length(l
)) {
178 log_error("Specified entry number out of range.");
182 log_info("Selected '%s'.", l
[u
-1]);
194 log_error("Entered data invalid.");
205 static int prompt_locale(void) {
206 _cleanup_strv_free_
char **locales
= NULL
;
209 if (arg_locale
|| arg_locale_messages
)
212 if (!arg_prompt_locale
)
215 r
= get_locales(&locales
);
217 return log_error_errno(r
, "Cannot query locales list: %m");
221 printf("\nAvailable Locales:\n\n");
222 r
= show_menu(locales
, 3, 22, 60);
228 r
= prompt_loop("Please enter system locale name or number", locales
, locale_is_valid
, &arg_locale
);
232 if (isempty(arg_locale
))
235 r
= prompt_loop("Please enter system message locale name or number", locales
, locale_is_valid
, &arg_locale_messages
);
242 static int process_locale(void) {
243 const char *etc_localeconf
;
248 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
249 if (laccess(etc_localeconf
, F_OK
) >= 0)
252 if (arg_copy_locale
&& arg_root
) {
254 mkdir_parents(etc_localeconf
, 0755);
255 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, COPY_REFLINK
);
258 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
260 log_info("%s copied.", etc_localeconf
);
269 if (!isempty(arg_locale
))
270 locales
[i
++] = strjoina("LANG=", arg_locale
);
271 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
272 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
279 mkdir_parents(etc_localeconf
, 0755);
280 r
= write_env_file(etc_localeconf
, locales
);
282 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
284 log_info("%s written.", etc_localeconf
);
288 static int prompt_keymap(void) {
289 _cleanup_strv_free_
char **kmaps
= NULL
;
295 if (!arg_prompt_keymap
)
298 r
= get_keymaps(&kmaps
);
299 if (r
== -ENOENT
) /* no keymaps installed */
302 return log_error_errno(r
, "Failed to read keymaps: %m");
306 printf("\nAvailable keymaps:\n\n");
307 r
= show_menu(kmaps
, 3, 22, 60);
313 return prompt_loop("Please enter system keymap name or number",
314 kmaps
, keymap_is_valid
, &arg_keymap
);
317 static int process_keymap(void) {
318 const char *etc_vconsoleconf
;
322 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
323 if (laccess(etc_vconsoleconf
, F_OK
) >= 0)
326 if (arg_copy_keymap
&& arg_root
) {
328 mkdir_parents(etc_vconsoleconf
, 0755);
329 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, COPY_REFLINK
);
332 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
334 log_info("%s copied.", etc_vconsoleconf
);
341 return 0; /* don't fail if no keymaps are installed */
345 if (isempty(arg_keymap
))
348 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
350 r
= mkdir_parents(etc_vconsoleconf
, 0755);
352 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
354 r
= write_env_file(etc_vconsoleconf
, keymap
);
356 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
358 log_info("%s written.", etc_vconsoleconf
);
362 static bool timezone_is_valid_log_error(const char *name
) {
363 return timezone_is_valid(name
, LOG_ERR
);
366 static int prompt_timezone(void) {
367 _cleanup_strv_free_
char **zones
= NULL
;
373 if (!arg_prompt_timezone
)
376 r
= get_timezones(&zones
);
378 return log_error_errno(r
, "Cannot query timezone list: %m");
382 printf("\nAvailable Time Zones:\n\n");
383 r
= show_menu(zones
, 3, 22, 30);
389 r
= prompt_loop("Please enter timezone name or number", zones
, timezone_is_valid_log_error
, &arg_timezone
);
396 static int process_timezone(void) {
397 const char *etc_localtime
, *e
;
400 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
401 if (laccess(etc_localtime
, F_OK
) >= 0)
404 if (arg_copy_timezone
&& arg_root
) {
405 _cleanup_free_
char *p
= NULL
;
407 r
= readlink_malloc("/etc/localtime", &p
);
410 return log_error_errno(r
, "Failed to read host timezone: %m");
412 mkdir_parents(etc_localtime
, 0755);
413 if (symlink(p
, etc_localtime
) < 0)
414 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
416 log_info("%s copied.", etc_localtime
);
421 r
= prompt_timezone();
425 if (isempty(arg_timezone
))
428 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
430 mkdir_parents(etc_localtime
, 0755);
431 if (symlink(e
, etc_localtime
) < 0)
432 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
434 log_info("%s written", etc_localtime
);
438 static int prompt_hostname(void) {
444 if (!arg_prompt_hostname
)
451 _cleanup_free_
char *h
= NULL
;
453 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(TRIANGULAR_BULLET
));
455 return log_error_errno(r
, "Failed to query hostname: %m");
458 log_warning("No hostname entered, skipping.");
462 if (!hostname_is_valid(h
, true)) {
463 log_error("Specified hostname invalid.");
467 /* Get rid of the trailing dot that we allow, but don't want to see */
468 arg_hostname
= hostname_cleanup(h
);
476 static int process_hostname(void) {
477 const char *etc_hostname
;
480 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
481 if (laccess(etc_hostname
, F_OK
) >= 0)
484 r
= prompt_hostname();
488 if (isempty(arg_hostname
))
491 mkdir_parents(etc_hostname
, 0755);
492 r
= write_string_file(etc_hostname
, arg_hostname
,
493 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
);
495 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
497 log_info("%s written.", etc_hostname
);
501 static int process_machine_id(void) {
502 const char *etc_machine_id
;
503 char id
[SD_ID128_STRING_MAX
];
506 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
507 if (laccess(etc_machine_id
, F_OK
) >= 0)
510 if (sd_id128_is_null(arg_machine_id
))
513 mkdir_parents(etc_machine_id
, 0755);
514 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
515 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
);
517 return log_error_errno(r
, "Failed to write machine id: %m");
519 log_info("%s written.", etc_machine_id
);
523 static int prompt_root_password(void) {
524 const char *msg1
, *msg2
, *etc_shadow
;
527 if (arg_root_password
)
530 if (!arg_prompt_root_password
)
533 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
534 if (laccess(etc_shadow
, F_OK
) >= 0)
540 msg1
= strjoina(special_glyph(TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip): ");
541 msg2
= strjoina(special_glyph(TRIANGULAR_BULLET
), " Please enter new root password again: ");
544 _cleanup_strv_free_erase_
char **a
= NULL
, **b
= NULL
;
546 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
548 return log_error_errno(r
, "Failed to query root password: %m");
549 if (strv_length(a
) != 1) {
550 log_warning("Received multiple passwords, where we expected one.");
555 log_warning("No password entered, skipping.");
559 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
561 return log_error_errno(r
, "Failed to query root password: %m");
563 if (!streq(*a
, *b
)) {
564 log_error("Entered passwords did not match, please try again.");
568 arg_root_password
= TAKE_PTR(*a
);
575 static int write_root_shadow(const char *path
, const struct spwd
*p
) {
576 _cleanup_fclose_
FILE *f
= NULL
;
583 f
= fopen(path
, "wex");
587 r
= putspent_sane(p
, f
);
591 return fflush_sync_and_check(f
);
594 static int process_root_password(void) {
596 static const char table
[] =
597 "abcdefghijklmnopqrstuvwxyz"
598 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
603 .sp_namp
= (char*) "root",
609 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
612 _cleanup_close_
int lock
= -1;
618 const char *etc_shadow
;
621 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
622 if (laccess(etc_shadow
, F_OK
) >= 0)
625 mkdir_parents(etc_shadow
, 0755);
627 lock
= take_etc_passwd_lock(arg_root
);
629 return log_error_errno(lock
, "Failed to take a lock: %m");
631 if (arg_copy_root_password
&& arg_root
) {
635 p
= getspnam("root");
636 if (p
|| errno
!= ENOENT
) {
641 return log_error_errno(errno
, "Failed to find shadow entry for root: %m");
644 r
= write_root_shadow(etc_shadow
, p
);
646 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
648 log_info("%s copied.", etc_shadow
);
653 r
= prompt_root_password();
657 if (!arg_root_password
)
660 /* Insist on the best randomness by setting RANDOM_BLOCK, this is about keeping passwords secret after all. */
661 r
= genuine_random_bytes(raw
, 16, RANDOM_BLOCK
);
663 return log_error_errno(r
, "Failed to get salt: %m");
665 /* We only bother with SHA512 hashed passwords, the rest is legacy, and we don't do legacy. */
666 assert_cc(sizeof(table
) == 64 + 1);
667 j
= stpcpy(salt
, "$6$");
668 for (i
= 0; i
< 16; i
++)
669 j
[i
] = table
[raw
[i
] & 63];
674 item
.sp_pwdp
= crypt(arg_root_password
, salt
);
679 return log_error_errno(errno
, "Failed to encrypt password: %m");
682 item
.sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
684 r
= write_root_shadow(etc_shadow
, &item
);
686 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
688 log_info("%s written.", etc_shadow
);
692 static int help(void) {
693 _cleanup_free_
char *link
= NULL
;
696 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
700 printf("%s [OPTIONS...]\n\n"
701 "Configures basic settings of the system.\n\n"
702 " -h --help Show this help\n"
703 " --version Show package version\n"
704 " --root=PATH Operate on an alternate filesystem root\n"
705 " --locale=LOCALE Set primary locale (LANG=)\n"
706 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
707 " --keymap=KEYMAP Set keymap\n"
708 " --timezone=TIMEZONE Set timezone\n"
709 " --hostname=NAME Set host name\n"
710 " --machine-ID=ID Set machine ID\n"
711 " --root-password=PASSWORD Set root password\n"
712 " --root-password-file=FILE Set root password from file\n"
713 " --prompt-locale Prompt the user for locale settings\n"
714 " --prompt-keymap Prompt the user for keymap settings\n"
715 " --prompt-timezone Prompt the user for timezone\n"
716 " --prompt-hostname Prompt the user for hostname\n"
717 " --prompt-root-password Prompt the user for root password\n"
718 " --prompt Prompt for all of the above\n"
719 " --copy-locale Copy locale from host\n"
720 " --copy-keymap Copy keymap from host\n"
721 " --copy-timezone Copy timezone from host\n"
722 " --copy-root-password Copy root password from host\n"
723 " --copy Copy locale, keymap, timezone, root password\n"
724 " --setup-machine-id Generate a new random machine ID\n"
725 "\nSee the %s for details.\n"
726 , program_invocation_short_name
733 static int parse_argv(int argc
, char *argv
[]) {
745 ARG_ROOT_PASSWORD_FILE
,
751 ARG_PROMPT_ROOT_PASSWORD
,
756 ARG_COPY_ROOT_PASSWORD
,
757 ARG_SETUP_MACHINE_ID
,
760 static const struct option options
[] = {
761 { "help", no_argument
, NULL
, 'h' },
762 { "version", no_argument
, NULL
, ARG_VERSION
},
763 { "root", required_argument
, NULL
, ARG_ROOT
},
764 { "locale", required_argument
, NULL
, ARG_LOCALE
},
765 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
766 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
767 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
768 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
769 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
770 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
771 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
772 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
773 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
774 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
775 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
776 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
777 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
778 { "copy", no_argument
, NULL
, ARG_COPY
},
779 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
780 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
781 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
782 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
783 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
792 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
803 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
809 if (!locale_is_valid(optarg
)) {
810 log_error("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 log_error("Locale %s is not valid.", optarg
);
826 r
= free_and_strdup(&arg_locale_messages
, optarg
);
833 if (!keymap_is_valid(optarg
)) {
834 log_error("Keymap %s is not valid.", optarg
);
838 r
= free_and_strdup(&arg_keymap
, optarg
);
845 if (!timezone_is_valid(optarg
, LOG_ERR
)) {
846 log_error("Timezone %s is not valid.", optarg
);
850 r
= free_and_strdup(&arg_timezone
, optarg
);
856 case ARG_ROOT_PASSWORD
:
857 r
= free_and_strdup(&arg_root_password
, optarg
);
862 case ARG_ROOT_PASSWORD_FILE
:
863 arg_root_password
= mfree(arg_root_password
);
865 r
= read_one_line_file(optarg
, &arg_root_password
);
867 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
872 if (!hostname_is_valid(optarg
, true)) {
873 log_error("Host name %s is not valid.", optarg
);
877 hostname_cleanup(optarg
);
878 r
= free_and_strdup(&arg_hostname
, optarg
);
885 if (sd_id128_from_string(optarg
, &arg_machine_id
) < 0) {
886 log_error("Failed to parse machine id %s.", optarg
);
893 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
= arg_prompt_root_password
= true;
896 case ARG_PROMPT_LOCALE
:
897 arg_prompt_locale
= true;
900 case ARG_PROMPT_KEYMAP
:
901 arg_prompt_keymap
= true;
904 case ARG_PROMPT_TIMEZONE
:
905 arg_prompt_timezone
= true;
908 case ARG_PROMPT_HOSTNAME
:
909 arg_prompt_hostname
= true;
912 case ARG_PROMPT_ROOT_PASSWORD
:
913 arg_prompt_root_password
= true;
917 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
= true;
920 case ARG_COPY_LOCALE
:
921 arg_copy_locale
= true;
924 case ARG_COPY_KEYMAP
:
925 arg_copy_keymap
= true;
928 case ARG_COPY_TIMEZONE
:
929 arg_copy_timezone
= true;
932 case ARG_COPY_ROOT_PASSWORD
:
933 arg_copy_root_password
= true;
936 case ARG_SETUP_MACHINE_ID
:
938 r
= sd_id128_randomize(&arg_machine_id
);
940 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
948 assert_not_reached("Unhandled option");
954 static int run(int argc
, char *argv
[]) {
958 r
= parse_argv(argc
, argv
);
966 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
968 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
969 if (r
> 0 && !enabled
)
970 return 0; /* disabled */
972 r
= process_locale();
976 r
= process_keymap();
980 r
= process_timezone();
984 r
= process_hostname();
988 r
= process_machine_id();
992 r
= process_root_password();
999 DEFINE_MAIN_FUNCTION(run
);