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"
35 #include "parse-util.h"
36 #include "path-util.h"
37 #include "pretty-print.h"
38 #include "proc-cmdline.h"
39 #include "random-util.h"
40 #include "string-util.h"
42 #include "terminal-util.h"
43 #include "time-util.h"
44 #include "umask-util.h"
45 #include "user-util.h"
47 static char *arg_root
= NULL
;
48 static char *arg_locale
= NULL
; /* $LANG */
49 static char *arg_keymap
= NULL
;
50 static char *arg_locale_messages
= NULL
; /* $LC_MESSAGES */
51 static char *arg_timezone
= NULL
;
52 static char *arg_hostname
= NULL
;
53 static sd_id128_t arg_machine_id
= {};
54 static char *arg_root_password
= NULL
;
55 static bool arg_prompt_locale
= false;
56 static bool arg_prompt_keymap
= false;
57 static bool arg_prompt_timezone
= false;
58 static bool arg_prompt_hostname
= false;
59 static bool arg_prompt_root_password
= false;
60 static bool arg_copy_locale
= false;
61 static bool arg_copy_keymap
= false;
62 static bool arg_copy_timezone
= false;
63 static bool arg_copy_root_password
= false;
65 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
66 STATIC_DESTRUCTOR_REGISTER(arg_locale
, freep
);
67 STATIC_DESTRUCTOR_REGISTER(arg_locale_messages
, freep
);
68 STATIC_DESTRUCTOR_REGISTER(arg_keymap
, freep
);
69 STATIC_DESTRUCTOR_REGISTER(arg_timezone
, freep
);
70 STATIC_DESTRUCTOR_REGISTER(arg_hostname
, freep
);
71 STATIC_DESTRUCTOR_REGISTER(arg_root_password
, string_free_erasep
);
73 static bool press_any_key(void) {
77 printf("-- Press any key to proceed --");
80 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
88 static void print_welcome(void) {
89 _cleanup_free_
char *pretty_name
= NULL
;
90 static bool done
= false;
96 r
= parse_os_release(arg_root
, "PRETTY_NAME", &pretty_name
, NULL
);
98 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
99 "Failed to read os-release file, ignoring: %m");
101 printf("\nWelcome to your new installation of %s!\nPlease configure a few basic system settings:\n\n",
102 isempty(pretty_name
) ? "Linux" : pretty_name
);
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
, bool (*is_valid
)(const char *name
), char **ret
) {
163 _cleanup_free_
char *p
= NULL
;
166 r
= ask_string(&p
, "%s %s (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), text
);
168 return log_error_errno(r
, "Failed to query user: %m");
171 log_warning("No data entered, skipping.");
175 r
= safe_atou(p
, &u
);
179 if (u
<= 0 || u
> strv_length(l
)) {
180 log_error("Specified entry number out of range.");
184 log_info("Selected '%s'.", l
[u
-1]);
196 log_error("Entered data invalid.");
207 static int prompt_locale(void) {
208 _cleanup_strv_free_
char **locales
= NULL
;
211 if (arg_locale
|| arg_locale_messages
)
214 if (!arg_prompt_locale
)
217 r
= get_locales(&locales
);
219 return log_error_errno(r
, "Cannot query locales list: %m");
223 printf("\nAvailable Locales:\n\n");
224 r
= show_menu(locales
, 3, 22, 60);
230 r
= prompt_loop("Please enter system locale name or number", locales
, locale_is_valid
, &arg_locale
);
234 if (isempty(arg_locale
))
237 r
= prompt_loop("Please enter system message locale name or number", locales
, locale_is_valid
, &arg_locale_messages
);
244 static int process_locale(void) {
245 const char *etc_localeconf
;
250 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
251 if (laccess(etc_localeconf
, F_OK
) >= 0)
254 if (arg_copy_locale
&& arg_root
) {
256 (void) mkdir_parents(etc_localeconf
, 0755);
257 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, 0, COPY_REFLINK
);
260 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
262 log_info("%s copied.", etc_localeconf
);
271 if (!isempty(arg_locale
))
272 locales
[i
++] = strjoina("LANG=", arg_locale
);
273 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
274 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
281 (void) mkdir_parents(etc_localeconf
, 0755);
282 r
= write_env_file(etc_localeconf
, locales
);
284 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
286 log_info("%s written.", etc_localeconf
);
290 static int prompt_keymap(void) {
291 _cleanup_strv_free_
char **kmaps
= NULL
;
297 if (!arg_prompt_keymap
)
300 r
= get_keymaps(&kmaps
);
301 if (r
== -ENOENT
) /* no keymaps installed */
304 return log_error_errno(r
, "Failed to read keymaps: %m");
308 printf("\nAvailable keymaps:\n\n");
309 r
= show_menu(kmaps
, 3, 22, 60);
315 return prompt_loop("Please enter system keymap name or number",
316 kmaps
, keymap_is_valid
, &arg_keymap
);
319 static int process_keymap(void) {
320 const char *etc_vconsoleconf
;
324 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
325 if (laccess(etc_vconsoleconf
, F_OK
) >= 0)
328 if (arg_copy_keymap
&& arg_root
) {
330 (void) mkdir_parents(etc_vconsoleconf
, 0755);
331 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, 0, COPY_REFLINK
);
334 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
336 log_info("%s copied.", etc_vconsoleconf
);
343 return 0; /* don't fail if no keymaps are installed */
347 if (isempty(arg_keymap
))
350 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
352 r
= mkdir_parents(etc_vconsoleconf
, 0755);
354 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
356 r
= write_env_file(etc_vconsoleconf
, keymap
);
358 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
360 log_info("%s written.", etc_vconsoleconf
);
364 static bool timezone_is_valid_log_error(const char *name
) {
365 return timezone_is_valid(name
, LOG_ERR
);
368 static int prompt_timezone(void) {
369 _cleanup_strv_free_
char **zones
= NULL
;
375 if (!arg_prompt_timezone
)
378 r
= get_timezones(&zones
);
380 return log_error_errno(r
, "Cannot query timezone list: %m");
384 printf("\nAvailable Time Zones:\n\n");
385 r
= show_menu(zones
, 3, 22, 30);
391 r
= prompt_loop("Please enter timezone name or number", zones
, timezone_is_valid_log_error
, &arg_timezone
);
398 static int process_timezone(void) {
399 const char *etc_localtime
, *e
;
402 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
403 if (laccess(etc_localtime
, F_OK
) >= 0)
406 if (arg_copy_timezone
&& arg_root
) {
407 _cleanup_free_
char *p
= NULL
;
409 r
= readlink_malloc("/etc/localtime", &p
);
412 return log_error_errno(r
, "Failed to read host timezone: %m");
414 (void) mkdir_parents(etc_localtime
, 0755);
415 if (symlink(p
, etc_localtime
) < 0)
416 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
418 log_info("%s copied.", etc_localtime
);
423 r
= prompt_timezone();
427 if (isempty(arg_timezone
))
430 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
432 (void) mkdir_parents(etc_localtime
, 0755);
433 if (symlink(e
, etc_localtime
) < 0)
434 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
436 log_info("%s written", etc_localtime
);
440 static int prompt_hostname(void) {
446 if (!arg_prompt_hostname
)
453 _cleanup_free_
char *h
= NULL
;
455 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
457 return log_error_errno(r
, "Failed to query hostname: %m");
460 log_warning("No hostname entered, skipping.");
464 if (!hostname_is_valid(h
, true)) {
465 log_error("Specified hostname invalid.");
469 /* Get rid of the trailing dot that we allow, but don't want to see */
470 arg_hostname
= hostname_cleanup(h
);
478 static int process_hostname(void) {
479 const char *etc_hostname
;
482 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
483 if (laccess(etc_hostname
, F_OK
) >= 0)
486 r
= prompt_hostname();
490 if (isempty(arg_hostname
))
493 r
= write_string_file(etc_hostname
, arg_hostname
,
494 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
);
496 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
498 log_info("%s written.", etc_hostname
);
502 static int process_machine_id(void) {
503 const char *etc_machine_id
;
504 char id
[SD_ID128_STRING_MAX
];
507 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
508 if (laccess(etc_machine_id
, F_OK
) >= 0)
511 if (sd_id128_is_null(arg_machine_id
))
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
| WRITE_STRING_FILE_MKDIR_0755
);
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(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip): ");
541 msg2
= strjoina(special_glyph(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) {
597 .sp_namp
= (char*) "root",
603 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
605 _cleanup_free_
char *salt
= NULL
;
606 _cleanup_close_
int lock
= -1;
607 struct crypt_data cd
= {};
609 const char *etc_shadow
;
612 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
613 if (laccess(etc_shadow
, F_OK
) >= 0)
616 (void) mkdir_parents(etc_shadow
, 0755);
618 lock
= take_etc_passwd_lock(arg_root
);
620 return log_error_errno(lock
, "Failed to take a lock: %m");
622 if (arg_copy_root_password
&& arg_root
) {
626 p
= getspnam("root");
627 if (p
|| errno
!= ENOENT
) {
632 return log_error_errno(errno
, "Failed to find shadow entry for root: %m");
635 r
= write_root_shadow(etc_shadow
, p
);
637 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
639 log_info("%s copied.", etc_shadow
);
644 r
= prompt_root_password();
648 if (!arg_root_password
)
651 r
= make_salt(&salt
);
653 return log_error_errno(r
, "Failed to get salt: %m");
656 item
.sp_pwdp
= crypt_r(arg_root_password
, salt
, &cd
);
658 return log_error_errno(errno
== 0 ? SYNTHETIC_ERRNO(EINVAL
) : errno
,
659 "Failed to encrypt password: %m");
661 item
.sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
663 r
= write_root_shadow(etc_shadow
, &item
);
665 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
667 log_info("%s written.", etc_shadow
);
671 static int help(void) {
672 _cleanup_free_
char *link
= NULL
;
675 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
679 printf("%s [OPTIONS...]\n\n"
680 "Configures basic settings of the system.\n\n"
681 " -h --help Show this help\n"
682 " --version Show package version\n"
683 " --root=PATH Operate on an alternate filesystem root\n"
684 " --locale=LOCALE Set primary locale (LANG=)\n"
685 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
686 " --keymap=KEYMAP Set keymap\n"
687 " --timezone=TIMEZONE Set timezone\n"
688 " --hostname=NAME Set host name\n"
689 " --machine-ID=ID Set machine ID\n"
690 " --root-password=PASSWORD Set root password\n"
691 " --root-password-file=FILE Set root password from file\n"
692 " --prompt-locale Prompt the user for locale settings\n"
693 " --prompt-keymap Prompt the user for keymap settings\n"
694 " --prompt-timezone Prompt the user for timezone\n"
695 " --prompt-hostname Prompt the user for hostname\n"
696 " --prompt-root-password Prompt the user for root password\n"
697 " --prompt Prompt for all of the above\n"
698 " --copy-locale Copy locale from host\n"
699 " --copy-keymap Copy keymap from host\n"
700 " --copy-timezone Copy timezone from host\n"
701 " --copy-root-password Copy root password from host\n"
702 " --copy Copy locale, keymap, timezone, root password\n"
703 " --setup-machine-id Generate a new random machine ID\n"
704 "\nSee the %s for details.\n"
705 , program_invocation_short_name
712 static int parse_argv(int argc
, char *argv
[]) {
724 ARG_ROOT_PASSWORD_FILE
,
730 ARG_PROMPT_ROOT_PASSWORD
,
735 ARG_COPY_ROOT_PASSWORD
,
736 ARG_SETUP_MACHINE_ID
,
739 static const struct option options
[] = {
740 { "help", no_argument
, NULL
, 'h' },
741 { "version", no_argument
, NULL
, ARG_VERSION
},
742 { "root", required_argument
, NULL
, ARG_ROOT
},
743 { "locale", required_argument
, NULL
, ARG_LOCALE
},
744 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
745 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
746 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
747 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
748 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
749 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
750 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
751 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
752 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
753 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
754 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
755 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
756 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
757 { "copy", no_argument
, NULL
, ARG_COPY
},
758 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
759 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
760 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
761 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
762 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
771 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
782 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
788 if (!locale_is_valid(optarg
))
789 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
790 "Locale %s is not valid.", optarg
);
792 r
= free_and_strdup(&arg_locale
, optarg
);
798 case ARG_LOCALE_MESSAGES
:
799 if (!locale_is_valid(optarg
))
800 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
801 "Locale %s is not valid.", optarg
);
803 r
= free_and_strdup(&arg_locale_messages
, optarg
);
810 if (!keymap_is_valid(optarg
))
811 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
812 "Keymap %s is not valid.", optarg
);
814 r
= free_and_strdup(&arg_keymap
, optarg
);
821 if (!timezone_is_valid(optarg
, LOG_ERR
))
822 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
823 "Timezone %s is not valid.", optarg
);
825 r
= free_and_strdup(&arg_timezone
, optarg
);
831 case ARG_ROOT_PASSWORD
:
832 r
= free_and_strdup(&arg_root_password
, optarg
);
837 case ARG_ROOT_PASSWORD_FILE
:
838 arg_root_password
= mfree(arg_root_password
);
840 r
= read_one_line_file(optarg
, &arg_root_password
);
842 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
847 if (!hostname_is_valid(optarg
, true))
848 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
849 "Host name %s is not valid.", optarg
);
851 hostname_cleanup(optarg
);
852 r
= free_and_strdup(&arg_hostname
, optarg
);
859 if (sd_id128_from_string(optarg
, &arg_machine_id
) < 0)
860 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
861 "Failed to parse machine id %s.", optarg
);
866 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
= arg_prompt_root_password
= true;
869 case ARG_PROMPT_LOCALE
:
870 arg_prompt_locale
= true;
873 case ARG_PROMPT_KEYMAP
:
874 arg_prompt_keymap
= true;
877 case ARG_PROMPT_TIMEZONE
:
878 arg_prompt_timezone
= true;
881 case ARG_PROMPT_HOSTNAME
:
882 arg_prompt_hostname
= true;
885 case ARG_PROMPT_ROOT_PASSWORD
:
886 arg_prompt_root_password
= true;
890 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
= true;
893 case ARG_COPY_LOCALE
:
894 arg_copy_locale
= true;
897 case ARG_COPY_KEYMAP
:
898 arg_copy_keymap
= true;
901 case ARG_COPY_TIMEZONE
:
902 arg_copy_timezone
= true;
905 case ARG_COPY_ROOT_PASSWORD
:
906 arg_copy_root_password
= true;
909 case ARG_SETUP_MACHINE_ID
:
911 r
= sd_id128_randomize(&arg_machine_id
);
913 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
921 assert_not_reached("Unhandled option");
927 static int run(int argc
, char *argv
[]) {
931 r
= parse_argv(argc
, argv
);
939 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
941 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
942 if (r
> 0 && !enabled
)
943 return 0; /* disabled */
945 r
= process_locale();
949 r
= process_keymap();
953 r
= process_timezone();
957 r
= process_hostname();
961 r
= process_machine_id();
965 r
= process_root_password();
972 DEFINE_MAIN_FUNCTION(run
);