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");
224 printf("\nAvailable Locales:\n\n");
225 r
= show_menu(locales
, 3, 22, 60);
231 r
= prompt_loop("Please enter system locale name or number", locales
, locale_is_valid
, &arg_locale
);
235 if (isempty(arg_locale
))
238 r
= prompt_loop("Please enter system message locale name or number", locales
, locale_is_valid
, &arg_locale_messages
);
245 static int process_locale(void) {
246 const char *etc_localeconf
;
251 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
252 if (laccess(etc_localeconf
, F_OK
) >= 0)
255 if (arg_copy_locale
&& arg_root
) {
257 (void) mkdir_parents(etc_localeconf
, 0755);
258 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, 0, COPY_REFLINK
);
261 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
263 log_info("%s copied.", etc_localeconf
);
272 if (!isempty(arg_locale
))
273 locales
[i
++] = strjoina("LANG=", arg_locale
);
274 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
275 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
282 (void) mkdir_parents(etc_localeconf
, 0755);
283 r
= write_env_file(etc_localeconf
, locales
);
285 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
287 log_info("%s written.", etc_localeconf
);
291 static int prompt_keymap(void) {
292 _cleanup_strv_free_
char **kmaps
= NULL
;
298 if (!arg_prompt_keymap
)
301 r
= get_keymaps(&kmaps
);
302 if (r
== -ENOENT
) /* no keymaps installed */
305 return log_error_errno(r
, "Failed to read keymaps: %m");
309 printf("\nAvailable keymaps:\n\n");
310 r
= show_menu(kmaps
, 3, 22, 60);
316 return prompt_loop("Please enter system keymap name or number",
317 kmaps
, keymap_is_valid
, &arg_keymap
);
320 static int process_keymap(void) {
321 const char *etc_vconsoleconf
;
325 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
326 if (laccess(etc_vconsoleconf
, F_OK
) >= 0)
329 if (arg_copy_keymap
&& arg_root
) {
331 (void) mkdir_parents(etc_vconsoleconf
, 0755);
332 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, 0, COPY_REFLINK
);
335 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
337 log_info("%s copied.", etc_vconsoleconf
);
344 return 0; /* don't fail if no keymaps are installed */
348 if (isempty(arg_keymap
))
351 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
353 r
= mkdir_parents(etc_vconsoleconf
, 0755);
355 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
357 r
= write_env_file(etc_vconsoleconf
, keymap
);
359 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
361 log_info("%s written.", etc_vconsoleconf
);
365 static bool timezone_is_valid_log_error(const char *name
) {
366 return timezone_is_valid(name
, LOG_ERR
);
369 static int prompt_timezone(void) {
370 _cleanup_strv_free_
char **zones
= NULL
;
376 if (!arg_prompt_timezone
)
379 r
= get_timezones(&zones
);
381 return log_error_errno(r
, "Cannot query timezone list: %m");
385 printf("\nAvailable Time Zones:\n\n");
386 r
= show_menu(zones
, 3, 22, 30);
392 r
= prompt_loop("Please enter timezone name or number", zones
, timezone_is_valid_log_error
, &arg_timezone
);
399 static int process_timezone(void) {
400 const char *etc_localtime
, *e
;
403 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
404 if (laccess(etc_localtime
, F_OK
) >= 0)
407 if (arg_copy_timezone
&& arg_root
) {
408 _cleanup_free_
char *p
= NULL
;
410 r
= readlink_malloc("/etc/localtime", &p
);
413 return log_error_errno(r
, "Failed to read host timezone: %m");
415 (void) mkdir_parents(etc_localtime
, 0755);
416 if (symlink(p
, etc_localtime
) < 0)
417 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
419 log_info("%s copied.", etc_localtime
);
424 r
= prompt_timezone();
428 if (isempty(arg_timezone
))
431 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
433 (void) mkdir_parents(etc_localtime
, 0755);
434 if (symlink(e
, etc_localtime
) < 0)
435 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
437 log_info("%s written", etc_localtime
);
441 static int prompt_hostname(void) {
447 if (!arg_prompt_hostname
)
454 _cleanup_free_
char *h
= NULL
;
456 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
458 return log_error_errno(r
, "Failed to query hostname: %m");
461 log_warning("No hostname entered, skipping.");
465 if (!hostname_is_valid(h
, true)) {
466 log_error("Specified hostname invalid.");
470 /* Get rid of the trailing dot that we allow, but don't want to see */
471 arg_hostname
= hostname_cleanup(h
);
479 static int process_hostname(void) {
480 const char *etc_hostname
;
483 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
484 if (laccess(etc_hostname
, F_OK
) >= 0)
487 r
= prompt_hostname();
491 if (isempty(arg_hostname
))
494 r
= write_string_file(etc_hostname
, arg_hostname
,
495 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
);
497 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
499 log_info("%s written.", etc_hostname
);
503 static int process_machine_id(void) {
504 const char *etc_machine_id
;
505 char id
[SD_ID128_STRING_MAX
];
508 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
509 if (laccess(etc_machine_id
, F_OK
) >= 0)
512 if (sd_id128_is_null(arg_machine_id
))
515 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
516 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
);
518 return log_error_errno(r
, "Failed to write machine id: %m");
520 log_info("%s written.", etc_machine_id
);
524 static int prompt_root_password(void) {
525 const char *msg1
, *msg2
, *etc_shadow
;
528 if (arg_root_password
)
531 if (!arg_prompt_root_password
)
534 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
535 if (laccess(etc_shadow
, F_OK
) >= 0)
541 msg1
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip): ");
542 msg2
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter new root password again: ");
545 _cleanup_strv_free_erase_
char **a
= NULL
, **b
= NULL
;
547 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
549 return log_error_errno(r
, "Failed to query root password: %m");
550 if (strv_length(a
) != 1) {
551 log_warning("Received multiple passwords, where we expected one.");
556 log_warning("No password entered, skipping.");
560 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
562 return log_error_errno(r
, "Failed to query root password: %m");
564 if (!streq(*a
, *b
)) {
565 log_error("Entered passwords did not match, please try again.");
569 arg_root_password
= TAKE_PTR(*a
);
576 static int write_root_shadow(const char *path
, const struct spwd
*p
) {
577 _cleanup_fclose_
FILE *f
= NULL
;
584 f
= fopen(path
, "wex");
588 r
= putspent_sane(p
, f
);
592 return fflush_sync_and_check(f
);
595 static int process_root_password(void) {
598 .sp_namp
= (char*) "root",
604 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
606 _cleanup_free_
char *salt
= NULL
;
607 _cleanup_close_
int lock
= -1;
608 struct crypt_data cd
= {};
610 const char *etc_shadow
;
613 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
614 if (laccess(etc_shadow
, F_OK
) >= 0)
617 (void) mkdir_parents(etc_shadow
, 0755);
619 lock
= take_etc_passwd_lock(arg_root
);
621 return log_error_errno(lock
, "Failed to take a lock: %m");
623 if (arg_copy_root_password
&& arg_root
) {
627 p
= getspnam("root");
628 if (p
|| errno
!= ENOENT
) {
633 return log_error_errno(errno
, "Failed to find shadow entry for root: %m");
636 r
= write_root_shadow(etc_shadow
, p
);
638 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
640 log_info("%s copied.", etc_shadow
);
645 r
= prompt_root_password();
649 if (!arg_root_password
)
652 r
= make_salt(&salt
);
654 return log_error_errno(r
, "Failed to get salt: %m");
657 item
.sp_pwdp
= crypt_r(arg_root_password
, salt
, &cd
);
659 return log_error_errno(errno
== 0 ? SYNTHETIC_ERRNO(EINVAL
) : errno
,
660 "Failed to encrypt password: %m");
662 item
.sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
664 r
= write_root_shadow(etc_shadow
, &item
);
666 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
668 log_info("%s written.", etc_shadow
);
672 static int help(void) {
673 _cleanup_free_
char *link
= NULL
;
676 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
680 printf("%s [OPTIONS...]\n\n"
681 "Configures basic settings of the system.\n\n"
682 " -h --help Show this help\n"
683 " --version Show package version\n"
684 " --root=PATH Operate on an alternate filesystem root\n"
685 " --locale=LOCALE Set primary locale (LANG=)\n"
686 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
687 " --keymap=KEYMAP Set keymap\n"
688 " --timezone=TIMEZONE Set timezone\n"
689 " --hostname=NAME Set host name\n"
690 " --machine-ID=ID Set machine ID\n"
691 " --root-password=PASSWORD Set root password\n"
692 " --root-password-file=FILE Set root password from file\n"
693 " --prompt-locale Prompt the user for locale settings\n"
694 " --prompt-keymap Prompt the user for keymap settings\n"
695 " --prompt-timezone Prompt the user for timezone\n"
696 " --prompt-hostname Prompt the user for hostname\n"
697 " --prompt-root-password Prompt the user for root password\n"
698 " --prompt Prompt for all of the above\n"
699 " --copy-locale Copy locale from host\n"
700 " --copy-keymap Copy keymap from host\n"
701 " --copy-timezone Copy timezone from host\n"
702 " --copy-root-password Copy root password from host\n"
703 " --copy Copy locale, keymap, timezone, root password\n"
704 " --setup-machine-id Generate a new random machine ID\n"
705 "\nSee the %s for details.\n"
706 , program_invocation_short_name
713 static int parse_argv(int argc
, char *argv
[]) {
725 ARG_ROOT_PASSWORD_FILE
,
731 ARG_PROMPT_ROOT_PASSWORD
,
736 ARG_COPY_ROOT_PASSWORD
,
737 ARG_SETUP_MACHINE_ID
,
740 static const struct option options
[] = {
741 { "help", no_argument
, NULL
, 'h' },
742 { "version", no_argument
, NULL
, ARG_VERSION
},
743 { "root", required_argument
, NULL
, ARG_ROOT
},
744 { "locale", required_argument
, NULL
, ARG_LOCALE
},
745 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
746 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
747 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
748 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
749 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
750 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
751 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
752 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
753 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
754 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
755 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
756 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
757 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
758 { "copy", no_argument
, NULL
, ARG_COPY
},
759 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
760 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
761 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
762 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
763 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
772 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
783 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
789 if (!locale_is_valid(optarg
))
790 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
791 "Locale %s is not valid.", optarg
);
793 r
= free_and_strdup(&arg_locale
, optarg
);
799 case ARG_LOCALE_MESSAGES
:
800 if (!locale_is_valid(optarg
))
801 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
802 "Locale %s is not valid.", optarg
);
804 r
= free_and_strdup(&arg_locale_messages
, optarg
);
811 if (!keymap_is_valid(optarg
))
812 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
813 "Keymap %s is not valid.", optarg
);
815 r
= free_and_strdup(&arg_keymap
, optarg
);
822 if (!timezone_is_valid(optarg
, LOG_ERR
))
823 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
824 "Timezone %s is not valid.", optarg
);
826 r
= free_and_strdup(&arg_timezone
, optarg
);
832 case ARG_ROOT_PASSWORD
:
833 r
= free_and_strdup(&arg_root_password
, optarg
);
838 case ARG_ROOT_PASSWORD_FILE
:
839 arg_root_password
= mfree(arg_root_password
);
841 r
= read_one_line_file(optarg
, &arg_root_password
);
843 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
848 if (!hostname_is_valid(optarg
, true))
849 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
850 "Host name %s is not valid.", optarg
);
852 hostname_cleanup(optarg
);
853 r
= free_and_strdup(&arg_hostname
, optarg
);
860 if (sd_id128_from_string(optarg
, &arg_machine_id
) < 0)
861 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
862 "Failed to parse machine id %s.", optarg
);
867 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
= arg_prompt_root_password
= true;
870 case ARG_PROMPT_LOCALE
:
871 arg_prompt_locale
= true;
874 case ARG_PROMPT_KEYMAP
:
875 arg_prompt_keymap
= true;
878 case ARG_PROMPT_TIMEZONE
:
879 arg_prompt_timezone
= true;
882 case ARG_PROMPT_HOSTNAME
:
883 arg_prompt_hostname
= true;
886 case ARG_PROMPT_ROOT_PASSWORD
:
887 arg_prompt_root_password
= true;
891 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
= true;
894 case ARG_COPY_LOCALE
:
895 arg_copy_locale
= true;
898 case ARG_COPY_KEYMAP
:
899 arg_copy_keymap
= true;
902 case ARG_COPY_TIMEZONE
:
903 arg_copy_timezone
= true;
906 case ARG_COPY_ROOT_PASSWORD
:
907 arg_copy_root_password
= true;
910 case ARG_SETUP_MACHINE_ID
:
912 r
= sd_id128_randomize(&arg_machine_id
);
914 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
922 assert_not_reached("Unhandled option");
928 static int run(int argc
, char *argv
[]) {
932 r
= parse_argv(argc
, argv
);
940 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
942 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
943 if (r
> 0 && !enabled
)
944 return 0; /* disabled */
946 r
= process_locale();
950 r
= process_keymap();
954 r
= process_timezone();
958 r
= process_hostname();
962 r
= process_machine_id();
966 r
= process_root_password();
973 DEFINE_MAIN_FUNCTION(run
);