1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2014 Lennart Poettering
11 /* libxcrypt is a replacement for glibc's libcrypt, and libcrypt might be
12 * removed from glibc at some point. As part of the removal, defines for
13 * crypt(3) are dropped from unistd.h, and we must include crypt.h instead.
15 * Newer versions of glibc (v2.0+) already ship crypt.h with a definition
16 * of crypt(3) as well, so we simply include it if it is present. MariaDB,
17 * MySQL, PostgreSQL, Perl and some other wide-spread packages do it the
18 * same way since ages without any problems.
25 #include "alloc-util.h"
26 #include "ask-password-api.h"
31 #include "hostname-util.h"
32 #include "locale-util.h"
35 #include "parse-util.h"
36 #include "path-util.h"
37 #include "proc-cmdline.h"
38 #include "random-util.h"
39 #include "string-util.h"
41 #include "terminal-util.h"
42 #include "time-util.h"
43 #include "umask-util.h"
44 #include "user-util.h"
46 static char *arg_root
= NULL
;
47 static char *arg_locale
= NULL
; /* $LANG */
48 static char *arg_keymap
= NULL
;
49 static char *arg_locale_messages
= NULL
; /* $LC_MESSAGES */
50 static char *arg_timezone
= NULL
;
51 static char *arg_hostname
= NULL
;
52 static sd_id128_t arg_machine_id
= {};
53 static char *arg_root_password
= NULL
;
54 static bool arg_prompt_locale
= false;
55 static bool arg_prompt_keymap
= false;
56 static bool arg_prompt_timezone
= false;
57 static bool arg_prompt_hostname
= false;
58 static bool arg_prompt_root_password
= false;
59 static bool arg_copy_locale
= false;
60 static bool arg_copy_keymap
= false;
61 static bool arg_copy_timezone
= false;
62 static bool arg_copy_root_password
= false;
64 static bool press_any_key(void) {
68 printf("-- Press any key to proceed --");
71 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
79 static void print_welcome(void) {
80 _cleanup_free_
char *pretty_name
= NULL
;
81 static bool done
= false;
87 r
= parse_os_release(arg_root
, "PRETTY_NAME", &pretty_name
, NULL
);
89 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
90 "Failed to read os-release file, ignoring: %m");
92 printf("\nWelcome to your new installation of %s!\nPlease configure a few basic system settings:\n\n",
93 isempty(pretty_name
) ? "Linux" : pretty_name
);
100 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
101 unsigned break_lines
, break_modulo
;
102 size_t n
, per_column
, i
, j
;
104 assert(n_columns
> 0);
107 per_column
= DIV_ROUND_UP(n
, n_columns
);
109 break_lines
= lines();
113 /* The first page gets two extra lines, since we want to show
115 break_modulo
= break_lines
;
116 if (break_modulo
> 3)
119 for (i
= 0; i
< per_column
; i
++) {
121 for (j
= 0; j
< n_columns
; j
++) {
122 _cleanup_free_
char *e
= NULL
;
124 if (j
* per_column
+ i
>= n
)
127 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
131 printf("%4zu) %-*s", j
* per_column
+ i
+ 1, width
, e
);
136 /* on the first screen we reserve 2 extra lines for the title */
137 if (i
% break_lines
== break_modulo
) {
138 if (!press_any_key())
146 static int prompt_loop(const char *text
, char **l
, bool (*is_valid
)(const char *name
), char **ret
) {
154 _cleanup_free_
char *p
= NULL
;
157 r
= ask_string(&p
, "%s %s (empty to skip): ", special_glyph(TRIANGULAR_BULLET
), text
);
159 return log_error_errno(r
, "Failed to query user: %m");
162 log_warning("No data entered, skipping.");
166 r
= safe_atou(p
, &u
);
170 if (u
<= 0 || u
> strv_length(l
)) {
171 log_error("Specified entry number out of range.");
175 log_info("Selected '%s'.", l
[u
-1]);
187 log_error("Entered data invalid.");
198 static int prompt_locale(void) {
199 _cleanup_strv_free_
char **locales
= NULL
;
202 if (arg_locale
|| arg_locale_messages
)
205 if (!arg_prompt_locale
)
208 r
= get_locales(&locales
);
210 return log_error_errno(r
, "Cannot query locales list: %m");
214 printf("\nAvailable Locales:\n\n");
215 r
= show_menu(locales
, 3, 22, 60);
221 r
= prompt_loop("Please enter system locale name or number", locales
, locale_is_valid
, &arg_locale
);
225 if (isempty(arg_locale
))
228 r
= prompt_loop("Please enter system message locale name or number", locales
, locale_is_valid
, &arg_locale_messages
);
235 static int process_locale(void) {
236 const char *etc_localeconf
;
241 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
242 if (laccess(etc_localeconf
, F_OK
) >= 0)
245 if (arg_copy_locale
&& arg_root
) {
247 mkdir_parents(etc_localeconf
, 0755);
248 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, COPY_REFLINK
);
251 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
253 log_info("%s copied.", etc_localeconf
);
262 if (!isempty(arg_locale
))
263 locales
[i
++] = strjoina("LANG=", arg_locale
);
264 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
265 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
272 mkdir_parents(etc_localeconf
, 0755);
273 r
= write_env_file(etc_localeconf
, locales
);
275 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
277 log_info("%s written.", etc_localeconf
);
281 static int prompt_keymap(void) {
282 _cleanup_strv_free_
char **kmaps
= NULL
;
288 if (!arg_prompt_keymap
)
291 r
= get_keymaps(&kmaps
);
292 if (r
== -ENOENT
) /* no keymaps installed */
295 return log_error_errno(r
, "Failed to read keymaps: %m");
299 printf("\nAvailable keymaps:\n\n");
300 r
= show_menu(kmaps
, 3, 22, 60);
306 return prompt_loop("Please enter system keymap name or number",
307 kmaps
, keymap_is_valid
, &arg_keymap
);
310 static int process_keymap(void) {
311 const char *etc_vconsoleconf
;
315 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
316 if (laccess(etc_vconsoleconf
, F_OK
) >= 0)
319 if (arg_copy_keymap
&& arg_root
) {
321 mkdir_parents(etc_vconsoleconf
, 0755);
322 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, COPY_REFLINK
);
325 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
327 log_info("%s copied.", etc_vconsoleconf
);
334 return 0; /* don't fail if no keymaps are installed */
338 if (isempty(arg_keymap
))
341 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
343 r
= mkdir_parents(etc_vconsoleconf
, 0755);
345 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
347 r
= write_env_file(etc_vconsoleconf
, keymap
);
349 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
351 log_info("%s written.", etc_vconsoleconf
);
355 static bool timezone_is_valid_log_error(const char *name
) {
356 return timezone_is_valid(name
, LOG_ERR
);
359 static int prompt_timezone(void) {
360 _cleanup_strv_free_
char **zones
= NULL
;
366 if (!arg_prompt_timezone
)
369 r
= get_timezones(&zones
);
371 return log_error_errno(r
, "Cannot query timezone list: %m");
375 printf("\nAvailable Time Zones:\n\n");
376 r
= show_menu(zones
, 3, 22, 30);
382 r
= prompt_loop("Please enter timezone name or number", zones
, timezone_is_valid_log_error
, &arg_timezone
);
389 static int process_timezone(void) {
390 const char *etc_localtime
, *e
;
393 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
394 if (laccess(etc_localtime
, F_OK
) >= 0)
397 if (arg_copy_timezone
&& arg_root
) {
398 _cleanup_free_
char *p
= NULL
;
400 r
= readlink_malloc("/etc/localtime", &p
);
403 return log_error_errno(r
, "Failed to read host timezone: %m");
405 mkdir_parents(etc_localtime
, 0755);
406 if (symlink(p
, etc_localtime
) < 0)
407 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
409 log_info("%s copied.", etc_localtime
);
414 r
= prompt_timezone();
418 if (isempty(arg_timezone
))
421 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
423 mkdir_parents(etc_localtime
, 0755);
424 if (symlink(e
, etc_localtime
) < 0)
425 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
427 log_info("%s written", etc_localtime
);
431 static int prompt_hostname(void) {
437 if (!arg_prompt_hostname
)
444 _cleanup_free_
char *h
= NULL
;
446 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(TRIANGULAR_BULLET
));
448 return log_error_errno(r
, "Failed to query hostname: %m");
451 log_warning("No hostname entered, skipping.");
455 if (!hostname_is_valid(h
, true)) {
456 log_error("Specified hostname invalid.");
460 /* Get rid of the trailing dot that we allow, but don't want to see */
461 arg_hostname
= hostname_cleanup(h
);
469 static int process_hostname(void) {
470 const char *etc_hostname
;
473 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
474 if (laccess(etc_hostname
, F_OK
) >= 0)
477 r
= prompt_hostname();
481 if (isempty(arg_hostname
))
484 mkdir_parents(etc_hostname
, 0755);
485 r
= write_string_file(etc_hostname
, arg_hostname
,
486 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
);
488 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
490 log_info("%s written.", etc_hostname
);
494 static int process_machine_id(void) {
495 const char *etc_machine_id
;
496 char id
[SD_ID128_STRING_MAX
];
499 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
500 if (laccess(etc_machine_id
, F_OK
) >= 0)
503 if (sd_id128_is_null(arg_machine_id
))
506 mkdir_parents(etc_machine_id
, 0755);
507 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
508 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
);
510 return log_error_errno(r
, "Failed to write machine id: %m");
512 log_info("%s written.", etc_machine_id
);
516 static int prompt_root_password(void) {
517 const char *msg1
, *msg2
, *etc_shadow
;
520 if (arg_root_password
)
523 if (!arg_prompt_root_password
)
526 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
527 if (laccess(etc_shadow
, F_OK
) >= 0)
533 msg1
= strjoina(special_glyph(TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip): ");
534 msg2
= strjoina(special_glyph(TRIANGULAR_BULLET
), " Please enter new root password again: ");
537 _cleanup_string_free_erase_
char *a
= NULL
, *b
= NULL
;
539 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
541 return log_error_errno(r
, "Failed to query root password: %m");
544 log_warning("No password entered, skipping.");
548 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
550 return log_error_errno(r
, "Failed to query root password: %m");
553 log_error("Entered passwords did not match, please try again.");
557 arg_root_password
= TAKE_PTR(a
);
564 static int write_root_shadow(const char *path
, const struct spwd
*p
) {
565 _cleanup_fclose_
FILE *f
= NULL
;
572 f
= fopen(path
, "wex");
576 r
= putspent_sane(p
, f
);
580 return fflush_sync_and_check(f
);
583 static int process_root_password(void) {
585 static const char table
[] =
586 "abcdefghijklmnopqrstuvwxyz"
587 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
592 .sp_namp
= (char*) "root",
598 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
601 _cleanup_close_
int lock
= -1;
607 const char *etc_shadow
;
610 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
611 if (laccess(etc_shadow
, F_OK
) >= 0)
614 mkdir_parents(etc_shadow
, 0755);
616 lock
= take_etc_passwd_lock(arg_root
);
618 return log_error_errno(lock
, "Failed to take a lock: %m");
620 if (arg_copy_root_password
&& arg_root
) {
624 p
= getspnam("root");
625 if (p
|| errno
!= ENOENT
) {
630 return log_error_errno(errno
, "Failed to find shadow entry for root: %m");
633 r
= write_root_shadow(etc_shadow
, p
);
635 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
637 log_info("%s copied.", etc_shadow
);
642 r
= prompt_root_password();
646 if (!arg_root_password
)
649 r
= acquire_random_bytes(raw
, 16, true);
651 return log_error_errno(r
, "Failed to get salt: %m");
653 /* We only bother with SHA512 hashed passwords, the rest is legacy, and we don't do legacy. */
654 assert_cc(sizeof(table
) == 64 + 1);
655 j
= stpcpy(salt
, "$6$");
656 for (i
= 0; i
< 16; i
++)
657 j
[i
] = table
[raw
[i
] & 63];
662 item
.sp_pwdp
= crypt(arg_root_password
, salt
);
667 return log_error_errno(errno
, "Failed to encrypt password: %m");
670 item
.sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
672 r
= write_root_shadow(etc_shadow
, &item
);
674 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
676 log_info("%s written.", etc_shadow
);
680 static void help(void) {
681 printf("%s [OPTIONS...]\n\n"
682 "Configures basic settings of the system.\n\n"
683 " -h --help Show this help\n"
684 " --version Show package version\n"
685 " --root=PATH Operate on an alternate filesystem root\n"
686 " --locale=LOCALE Set primary locale (LANG=)\n"
687 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
688 " --keymap=KEYMAP Set keymap\n"
689 " --timezone=TIMEZONE Set timezone\n"
690 " --hostname=NAME Set host name\n"
691 " --machine-ID=ID Set machine ID\n"
692 " --root-password=PASSWORD Set root password\n"
693 " --root-password-file=FILE Set root password from file\n"
694 " --prompt-locale Prompt the user for locale settings\n"
695 " --prompt-keymap Prompt the user for keymap settings\n"
696 " --prompt-timezone Prompt the user for timezone\n"
697 " --prompt-hostname Prompt the user for hostname\n"
698 " --prompt-root-password Prompt the user for root password\n"
699 " --prompt Prompt for all of the above\n"
700 " --copy-locale Copy locale from host\n"
701 " --copy-keymap Copy keymap from host\n"
702 " --copy-timezone Copy timezone from host\n"
703 " --copy-root-password Copy root password from host\n"
704 " --copy Copy locale, keymap, timezone, root password\n"
705 " --setup-machine-id Generate a new random machine ID\n"
706 , program_invocation_short_name
);
709 static int parse_argv(int argc
, char *argv
[]) {
721 ARG_ROOT_PASSWORD_FILE
,
727 ARG_PROMPT_ROOT_PASSWORD
,
732 ARG_COPY_ROOT_PASSWORD
,
733 ARG_SETUP_MACHINE_ID
,
736 static const struct option options
[] = {
737 { "help", no_argument
, NULL
, 'h' },
738 { "version", no_argument
, NULL
, ARG_VERSION
},
739 { "root", required_argument
, NULL
, ARG_ROOT
},
740 { "locale", required_argument
, NULL
, ARG_LOCALE
},
741 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
742 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
743 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
744 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
745 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
746 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
747 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
748 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
749 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
750 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
751 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
752 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
753 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
754 { "copy", no_argument
, NULL
, ARG_COPY
},
755 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
756 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
757 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
758 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
759 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
768 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
780 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
786 if (!locale_is_valid(optarg
)) {
787 log_error("Locale %s is not valid.", optarg
);
791 r
= free_and_strdup(&arg_locale
, optarg
);
797 case ARG_LOCALE_MESSAGES
:
798 if (!locale_is_valid(optarg
)) {
799 log_error("Locale %s is not valid.", optarg
);
803 r
= free_and_strdup(&arg_locale_messages
, optarg
);
810 if (!keymap_is_valid(optarg
)) {
811 log_error("Keymap %s is not valid.", optarg
);
815 r
= free_and_strdup(&arg_keymap
, optarg
);
822 if (!timezone_is_valid(optarg
, LOG_ERR
)) {
823 log_error("Timezone %s is not valid.", optarg
);
827 r
= free_and_strdup(&arg_timezone
, optarg
);
833 case ARG_ROOT_PASSWORD
:
834 r
= free_and_strdup(&arg_root_password
, optarg
);
839 case ARG_ROOT_PASSWORD_FILE
:
840 arg_root_password
= mfree(arg_root_password
);
842 r
= read_one_line_file(optarg
, &arg_root_password
);
844 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
849 if (!hostname_is_valid(optarg
, true)) {
850 log_error("Host name %s is not valid.", optarg
);
854 hostname_cleanup(optarg
);
855 r
= free_and_strdup(&arg_hostname
, optarg
);
862 if (sd_id128_from_string(optarg
, &arg_machine_id
) < 0) {
863 log_error("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 int main(int argc
, char *argv
[]) {
935 r
= parse_argv(argc
, argv
);
939 log_set_target(LOG_TARGET_AUTO
);
940 log_parse_environment();
945 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
947 log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring.");
950 if (r
> 0 && !enabled
) {
951 r
= 0; /* disabled */
955 r
= process_locale();
959 r
= process_keymap();
963 r
= process_timezone();
967 r
= process_hostname();
971 r
= process_machine_id();
975 r
= process_root_password();
982 free(arg_locale_messages
);
986 string_erase(arg_root_password
);
987 free(arg_root_password
);
989 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;