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"
32 #include "parse-util.h"
33 #include "path-util.h"
34 #include "proc-cmdline.h"
35 #include "random-util.h"
36 #include "string-util.h"
38 #include "terminal-util.h"
39 #include "time-util.h"
40 #include "umask-util.h"
41 #include "user-util.h"
43 static char *arg_root
= NULL
;
44 static char *arg_locale
= NULL
; /* $LANG */
45 static char *arg_keymap
= NULL
;
46 static char *arg_locale_messages
= NULL
; /* $LC_MESSAGES */
47 static char *arg_timezone
= NULL
;
48 static char *arg_hostname
= NULL
;
49 static sd_id128_t arg_machine_id
= {};
50 static char *arg_root_password
= NULL
;
51 static bool arg_prompt_locale
= false;
52 static bool arg_prompt_keymap
= false;
53 static bool arg_prompt_timezone
= false;
54 static bool arg_prompt_hostname
= false;
55 static bool arg_prompt_root_password
= false;
56 static bool arg_copy_locale
= false;
57 static bool arg_copy_keymap
= false;
58 static bool arg_copy_timezone
= false;
59 static bool arg_copy_root_password
= false;
61 static bool press_any_key(void) {
65 printf("-- Press any key to proceed --");
68 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
76 static void print_welcome(void) {
77 _cleanup_free_
char *pretty_name
= NULL
;
78 static bool done
= false;
84 r
= parse_os_release(arg_root
, "PRETTY_NAME", &pretty_name
, NULL
);
86 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
87 "Failed to read os-release file, ignoring: %m");
89 printf("\nWelcome to your new installation of %s!\nPlease configure a few basic system settings:\n\n",
90 isempty(pretty_name
) ? "Linux" : pretty_name
);
97 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
98 unsigned break_lines
, break_modulo
;
99 size_t n
, per_column
, i
, j
;
101 assert(n_columns
> 0);
104 per_column
= DIV_ROUND_UP(n
, n_columns
);
106 break_lines
= lines();
110 /* The first page gets two extra lines, since we want to show
112 break_modulo
= break_lines
;
113 if (break_modulo
> 3)
116 for (i
= 0; i
< per_column
; i
++) {
118 for (j
= 0; j
< n_columns
; j
++) {
119 _cleanup_free_
char *e
= NULL
;
121 if (j
* per_column
+ i
>= n
)
124 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
128 printf("%4zu) %-*s", j
* per_column
+ i
+ 1, width
, e
);
133 /* on the first screen we reserve 2 extra lines for the title */
134 if (i
% break_lines
== break_modulo
) {
135 if (!press_any_key())
143 static int prompt_loop(const char *text
, char **l
, bool (*is_valid
)(const char *name
), char **ret
) {
151 _cleanup_free_
char *p
= NULL
;
154 r
= ask_string(&p
, "%s %s (empty to skip): ", special_glyph(TRIANGULAR_BULLET
), text
);
156 return log_error_errno(r
, "Failed to query user: %m");
159 log_warning("No data entered, skipping.");
163 r
= safe_atou(p
, &u
);
167 if (u
<= 0 || u
> strv_length(l
)) {
168 log_error("Specified entry number out of range.");
172 log_info("Selected '%s'.", l
[u
-1]);
184 log_error("Entered data invalid.");
195 static int prompt_locale(void) {
196 _cleanup_strv_free_
char **locales
= NULL
;
199 if (arg_locale
|| arg_locale_messages
)
202 if (!arg_prompt_locale
)
205 r
= get_locales(&locales
);
207 return log_error_errno(r
, "Cannot query locales list: %m");
211 printf("\nAvailable Locales:\n\n");
212 r
= show_menu(locales
, 3, 22, 60);
218 r
= prompt_loop("Please enter system locale name or number", locales
, locale_is_valid
, &arg_locale
);
222 if (isempty(arg_locale
))
225 r
= prompt_loop("Please enter system message locale name or number", locales
, locale_is_valid
, &arg_locale_messages
);
232 static int process_locale(void) {
233 const char *etc_localeconf
;
238 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
239 if (laccess(etc_localeconf
, F_OK
) >= 0)
242 if (arg_copy_locale
&& arg_root
) {
244 mkdir_parents(etc_localeconf
, 0755);
245 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, COPY_REFLINK
);
248 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
250 log_info("%s copied.", etc_localeconf
);
259 if (!isempty(arg_locale
))
260 locales
[i
++] = strjoina("LANG=", arg_locale
);
261 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
262 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
269 mkdir_parents(etc_localeconf
, 0755);
270 r
= write_env_file(etc_localeconf
, locales
);
272 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
274 log_info("%s written.", etc_localeconf
);
278 static int prompt_keymap(void) {
279 _cleanup_strv_free_
char **kmaps
= NULL
;
285 if (!arg_prompt_keymap
)
288 r
= get_keymaps(&kmaps
);
289 if (r
== -ENOENT
) /* no keymaps installed */
292 return log_error_errno(r
, "Failed to read keymaps: %m");
296 printf("\nAvailable keymaps:\n\n");
297 r
= show_menu(kmaps
, 3, 22, 60);
303 return prompt_loop("Please enter system keymap name or number",
304 kmaps
, keymap_is_valid
, &arg_keymap
);
307 static int process_keymap(void) {
308 const char *etc_vconsoleconf
;
312 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
313 if (laccess(etc_vconsoleconf
, F_OK
) >= 0)
316 if (arg_copy_keymap
&& arg_root
) {
318 mkdir_parents(etc_vconsoleconf
, 0755);
319 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, COPY_REFLINK
);
322 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
324 log_info("%s copied.", etc_vconsoleconf
);
331 return 0; /* don't fail if no keymaps are installed */
335 if (isempty(arg_keymap
))
338 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
340 r
= mkdir_parents(etc_vconsoleconf
, 0755);
342 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
344 r
= write_env_file(etc_vconsoleconf
, keymap
);
346 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
348 log_info("%s written.", etc_vconsoleconf
);
352 static bool timezone_is_valid_log_error(const char *name
) {
353 return timezone_is_valid(name
, LOG_ERR
);
356 static int prompt_timezone(void) {
357 _cleanup_strv_free_
char **zones
= NULL
;
363 if (!arg_prompt_timezone
)
366 r
= get_timezones(&zones
);
368 return log_error_errno(r
, "Cannot query timezone list: %m");
372 printf("\nAvailable Time Zones:\n\n");
373 r
= show_menu(zones
, 3, 22, 30);
379 r
= prompt_loop("Please enter timezone name or number", zones
, timezone_is_valid_log_error
, &arg_timezone
);
386 static int process_timezone(void) {
387 const char *etc_localtime
, *e
;
390 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
391 if (laccess(etc_localtime
, F_OK
) >= 0)
394 if (arg_copy_timezone
&& arg_root
) {
395 _cleanup_free_
char *p
= NULL
;
397 r
= readlink_malloc("/etc/localtime", &p
);
400 return log_error_errno(r
, "Failed to read host timezone: %m");
402 mkdir_parents(etc_localtime
, 0755);
403 if (symlink(p
, etc_localtime
) < 0)
404 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
406 log_info("%s copied.", etc_localtime
);
411 r
= prompt_timezone();
415 if (isempty(arg_timezone
))
418 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
420 mkdir_parents(etc_localtime
, 0755);
421 if (symlink(e
, etc_localtime
) < 0)
422 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
424 log_info("%s written", etc_localtime
);
428 static int prompt_hostname(void) {
434 if (!arg_prompt_hostname
)
441 _cleanup_free_
char *h
= NULL
;
443 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(TRIANGULAR_BULLET
));
445 return log_error_errno(r
, "Failed to query hostname: %m");
448 log_warning("No hostname entered, skipping.");
452 if (!hostname_is_valid(h
, true)) {
453 log_error("Specified hostname invalid.");
457 /* Get rid of the trailing dot that we allow, but don't want to see */
458 arg_hostname
= hostname_cleanup(h
);
466 static int process_hostname(void) {
467 const char *etc_hostname
;
470 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
471 if (laccess(etc_hostname
, F_OK
) >= 0)
474 r
= prompt_hostname();
478 if (isempty(arg_hostname
))
481 mkdir_parents(etc_hostname
, 0755);
482 r
= write_string_file(etc_hostname
, arg_hostname
,
483 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
);
485 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
487 log_info("%s written.", etc_hostname
);
491 static int process_machine_id(void) {
492 const char *etc_machine_id
;
493 char id
[SD_ID128_STRING_MAX
];
496 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
497 if (laccess(etc_machine_id
, F_OK
) >= 0)
500 if (sd_id128_is_null(arg_machine_id
))
503 mkdir_parents(etc_machine_id
, 0755);
504 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
505 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
);
507 return log_error_errno(r
, "Failed to write machine id: %m");
509 log_info("%s written.", etc_machine_id
);
513 static int prompt_root_password(void) {
514 const char *msg1
, *msg2
, *etc_shadow
;
517 if (arg_root_password
)
520 if (!arg_prompt_root_password
)
523 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
524 if (laccess(etc_shadow
, F_OK
) >= 0)
530 msg1
= strjoina(special_glyph(TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip): ");
531 msg2
= strjoina(special_glyph(TRIANGULAR_BULLET
), " Please enter new root password again: ");
534 _cleanup_string_free_erase_
char *a
= NULL
, *b
= NULL
;
536 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
538 return log_error_errno(r
, "Failed to query root password: %m");
541 log_warning("No password entered, skipping.");
545 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
547 return log_error_errno(r
, "Failed to query root password: %m");
550 log_error("Entered passwords did not match, please try again.");
554 arg_root_password
= TAKE_PTR(a
);
561 static int write_root_shadow(const char *path
, const struct spwd
*p
) {
562 _cleanup_fclose_
FILE *f
= NULL
;
569 f
= fopen(path
, "wex");
573 r
= putspent_sane(p
, f
);
577 return fflush_sync_and_check(f
);
580 static int process_root_password(void) {
582 static const char table
[] =
583 "abcdefghijklmnopqrstuvwxyz"
584 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
589 .sp_namp
= (char*) "root",
595 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
598 _cleanup_close_
int lock
= -1;
604 const char *etc_shadow
;
607 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
608 if (laccess(etc_shadow
, F_OK
) >= 0)
611 mkdir_parents(etc_shadow
, 0755);
613 lock
= take_etc_passwd_lock(arg_root
);
615 return log_error_errno(lock
, "Failed to take a lock: %m");
617 if (arg_copy_root_password
&& arg_root
) {
621 p
= getspnam("root");
622 if (p
|| errno
!= ENOENT
) {
627 return log_error_errno(errno
, "Failed to find shadow entry for root: %m");
630 r
= write_root_shadow(etc_shadow
, p
);
632 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
634 log_info("%s copied.", etc_shadow
);
639 r
= prompt_root_password();
643 if (!arg_root_password
)
646 r
= acquire_random_bytes(raw
, 16, true);
648 return log_error_errno(r
, "Failed to get salt: %m");
650 /* We only bother with SHA512 hashed passwords, the rest is legacy, and we don't do legacy. */
651 assert_cc(sizeof(table
) == 64 + 1);
652 j
= stpcpy(salt
, "$6$");
653 for (i
= 0; i
< 16; i
++)
654 j
[i
] = table
[raw
[i
] & 63];
659 item
.sp_pwdp
= crypt(arg_root_password
, salt
);
664 return log_error_errno(errno
, "Failed to encrypt password: %m");
667 item
.sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
669 r
= write_root_shadow(etc_shadow
, &item
);
671 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
673 log_info("%s written.", etc_shadow
);
677 static void help(void) {
678 printf("%s [OPTIONS...]\n\n"
679 "Configures basic settings of the system.\n\n"
680 " -h --help Show this help\n"
681 " --version Show package version\n"
682 " --root=PATH Operate on an alternate filesystem root\n"
683 " --locale=LOCALE Set primary locale (LANG=)\n"
684 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
685 " --keymap=KEYMAP Set keymap\n"
686 " --timezone=TIMEZONE Set timezone\n"
687 " --hostname=NAME Set host name\n"
688 " --machine-ID=ID Set machine ID\n"
689 " --root-password=PASSWORD Set root password\n"
690 " --root-password-file=FILE Set root password from file\n"
691 " --prompt-locale Prompt the user for locale settings\n"
692 " --prompt-keymap Prompt the user for keymap settings\n"
693 " --prompt-timezone Prompt the user for timezone\n"
694 " --prompt-hostname Prompt the user for hostname\n"
695 " --prompt-root-password Prompt the user for root password\n"
696 " --prompt Prompt for all of the above\n"
697 " --copy-locale Copy locale from host\n"
698 " --copy-keymap Copy keymap from host\n"
699 " --copy-timezone Copy timezone from host\n"
700 " --copy-root-password Copy root password from host\n"
701 " --copy Copy locale, keymap, timezone, root password\n"
702 " --setup-machine-id Generate a new random machine ID\n"
703 , program_invocation_short_name
);
706 static int parse_argv(int argc
, char *argv
[]) {
718 ARG_ROOT_PASSWORD_FILE
,
724 ARG_PROMPT_ROOT_PASSWORD
,
729 ARG_COPY_ROOT_PASSWORD
,
730 ARG_SETUP_MACHINE_ID
,
733 static const struct option options
[] = {
734 { "help", no_argument
, NULL
, 'h' },
735 { "version", no_argument
, NULL
, ARG_VERSION
},
736 { "root", required_argument
, NULL
, ARG_ROOT
},
737 { "locale", required_argument
, NULL
, ARG_LOCALE
},
738 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
739 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
740 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
741 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
742 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
743 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
744 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
745 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
746 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
747 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
748 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
749 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
750 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
751 { "copy", no_argument
, NULL
, ARG_COPY
},
752 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
753 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
754 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
755 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
756 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
765 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
777 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
783 if (!locale_is_valid(optarg
)) {
784 log_error("Locale %s is not valid.", optarg
);
788 r
= free_and_strdup(&arg_locale
, optarg
);
794 case ARG_LOCALE_MESSAGES
:
795 if (!locale_is_valid(optarg
)) {
796 log_error("Locale %s is not valid.", optarg
);
800 r
= free_and_strdup(&arg_locale_messages
, optarg
);
807 if (!keymap_is_valid(optarg
)) {
808 log_error("Keymap %s is not valid.", optarg
);
812 r
= free_and_strdup(&arg_keymap
, optarg
);
819 if (!timezone_is_valid(optarg
, LOG_ERR
)) {
820 log_error("Timezone %s is not valid.", optarg
);
824 r
= free_and_strdup(&arg_timezone
, optarg
);
830 case ARG_ROOT_PASSWORD
:
831 r
= free_and_strdup(&arg_root_password
, optarg
);
836 case ARG_ROOT_PASSWORD_FILE
:
837 arg_root_password
= mfree(arg_root_password
);
839 r
= read_one_line_file(optarg
, &arg_root_password
);
841 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
846 if (!hostname_is_valid(optarg
, true)) {
847 log_error("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 log_error("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 int main(int argc
, char *argv
[]) {
932 r
= parse_argv(argc
, argv
);
936 log_set_target(LOG_TARGET_AUTO
);
937 log_parse_environment();
942 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
944 log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring.");
947 if (r
> 0 && !enabled
) {
948 r
= 0; /* disabled */
952 r
= process_locale();
956 r
= process_keymap();
960 r
= process_timezone();
964 r
= process_hostname();
968 r
= process_machine_id();
972 r
= process_root_password();
979 free(arg_locale_messages
);
983 string_erase(arg_root_password
);
984 free(arg_root_password
);
986 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;