1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "alloc-util.h"
27 #include "ask-password-api.h"
32 #include "hostname-util.h"
33 #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 const char *os_release
= NULL
;
82 static bool done
= false;
88 os_release
= prefix_roota(arg_root
, "/etc/os-release");
89 r
= parse_env_file(os_release
, NEWLINE
,
90 "PRETTY_NAME", &pretty_name
,
94 os_release
= prefix_roota(arg_root
, "/usr/lib/os-release");
95 r
= parse_env_file(os_release
, NEWLINE
,
96 "PRETTY_NAME", &pretty_name
,
100 if (r
< 0 && r
!= -ENOENT
)
101 log_warning_errno(r
, "Failed to read os-release file: %m");
103 printf("\nWelcome to your new installation of %s!\nPlease configure a few basic system settings:\n\n",
104 isempty(pretty_name
) ? "Linux" : pretty_name
);
111 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
112 unsigned n
, per_column
, i
, j
;
113 unsigned break_lines
, break_modulo
;
115 assert(n_columns
> 0);
118 per_column
= (n
+ n_columns
- 1) / n_columns
;
120 break_lines
= lines();
124 /* The first page gets two extra lines, since we want to show
126 break_modulo
= break_lines
;
127 if (break_modulo
> 3)
130 for (i
= 0; i
< per_column
; i
++) {
132 for (j
= 0; j
< n_columns
; j
++) {
133 _cleanup_free_
char *e
= NULL
;
135 if (j
* per_column
+ i
>= n
)
138 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
142 printf("%4u) %-*s", j
* per_column
+ i
+ 1, width
, e
);
147 /* on the first screen we reserve 2 extra lines for the title */
148 if (i
% break_lines
== break_modulo
) {
149 if (!press_any_key())
157 static int prompt_loop(const char *text
, char **l
, bool (*is_valid
)(const char *name
), char **ret
) {
165 _cleanup_free_
char *p
= NULL
;
168 r
= ask_string(&p
, "%s %s (empty to skip): ", special_glyph(TRIANGULAR_BULLET
), text
);
170 return log_error_errno(r
, "Failed to query user: %m");
173 log_warning("No data entered, skipping.");
177 r
= safe_atou(p
, &u
);
181 if (u
<= 0 || u
> strv_length(l
)) {
182 log_error("Specified entry number out of range.");
186 log_info("Selected '%s'.", l
[u
-1]);
198 log_error("Entered data invalid.");
209 static int prompt_locale(void) {
210 _cleanup_strv_free_
char **locales
= NULL
;
213 if (arg_locale
|| arg_locale_messages
)
216 if (!arg_prompt_locale
)
219 r
= get_locales(&locales
);
221 return log_error_errno(r
, "Cannot query locales list: %m");
225 printf("\nAvailable Locales:\n\n");
226 r
= show_menu(locales
, 3, 22, 60);
232 r
= prompt_loop("Please enter system locale name or number", locales
, locale_is_valid
, &arg_locale
);
236 if (isempty(arg_locale
))
239 r
= prompt_loop("Please enter system message locale name or number", locales
, locale_is_valid
, &arg_locale_messages
);
246 static int process_locale(void) {
247 const char *etc_localeconf
;
252 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
253 if (laccess(etc_localeconf
, F_OK
) >= 0)
256 if (arg_copy_locale
&& arg_root
) {
258 mkdir_parents(etc_localeconf
, 0755);
259 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, COPY_REFLINK
);
262 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
264 log_info("%s copied.", etc_localeconf
);
273 if (!isempty(arg_locale
))
274 locales
[i
++] = strjoina("LANG=", arg_locale
);
275 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
276 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
283 mkdir_parents(etc_localeconf
, 0755);
284 r
= write_env_file(etc_localeconf
, locales
);
286 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
288 log_info("%s written.", etc_localeconf
);
292 static int prompt_keymap(void) {
293 _cleanup_strv_free_
char **kmaps
= NULL
;
299 if (!arg_prompt_keymap
)
302 r
= get_keymaps(&kmaps
);
303 if (r
== -ENOENT
) /* no keymaps installed */
306 return log_error_errno(r
, "Failed to read keymaps: %m");
310 printf("\nAvailable keymaps:\n\n");
311 r
= show_menu(kmaps
, 3, 22, 60);
317 return prompt_loop("Please enter system keymap name or number",
318 kmaps
, keymap_is_valid
, &arg_keymap
);
321 static int process_keymap(void) {
322 const char *etc_vconsoleconf
;
326 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
327 if (laccess(etc_vconsoleconf
, F_OK
) >= 0)
330 if (arg_copy_keymap
&& arg_root
) {
332 mkdir_parents(etc_vconsoleconf
, 0755);
333 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, COPY_REFLINK
);
336 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
338 log_info("%s copied.", etc_vconsoleconf
);
345 return 0; /* don't fail if no keymaps are installed */
349 if (!isempty(arg_keymap
))
350 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
355 mkdir_parents(etc_vconsoleconf
, 0755);
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 int prompt_timezone(void) {
365 _cleanup_strv_free_
char **zones
= NULL
;
371 if (!arg_prompt_timezone
)
374 r
= get_timezones(&zones
);
376 return log_error_errno(r
, "Cannot query timezone list: %m");
380 printf("\nAvailable Time Zones:\n\n");
381 r
= show_menu(zones
, 3, 22, 30);
387 r
= prompt_loop("Please enter timezone name or number", zones
, timezone_is_valid
, &arg_timezone
);
394 static int process_timezone(void) {
395 const char *etc_localtime
, *e
;
398 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
399 if (laccess(etc_localtime
, F_OK
) >= 0)
402 if (arg_copy_timezone
&& arg_root
) {
403 _cleanup_free_
char *p
= NULL
;
405 r
= readlink_malloc("/etc/localtime", &p
);
408 return log_error_errno(r
, "Failed to read host timezone: %m");
410 mkdir_parents(etc_localtime
, 0755);
411 if (symlink(p
, etc_localtime
) < 0)
412 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
414 log_info("%s copied.", etc_localtime
);
419 r
= prompt_timezone();
423 if (isempty(arg_timezone
))
426 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
428 mkdir_parents(etc_localtime
, 0755);
429 if (symlink(e
, etc_localtime
) < 0)
430 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
432 log_info("%s written", etc_localtime
);
436 static int prompt_hostname(void) {
442 if (!arg_prompt_hostname
)
449 _cleanup_free_
char *h
= NULL
;
451 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(TRIANGULAR_BULLET
));
453 return log_error_errno(r
, "Failed to query hostname: %m");
456 log_warning("No hostname entered, skipping.");
460 if (!hostname_is_valid(h
, true)) {
461 log_error("Specified hostname invalid.");
465 /* Get rid of the trailing dot that we allow, but don't want to see */
466 arg_hostname
= hostname_cleanup(h
);
474 static int process_hostname(void) {
475 const char *etc_hostname
;
478 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
479 if (laccess(etc_hostname
, F_OK
) >= 0)
482 r
= prompt_hostname();
486 if (isempty(arg_hostname
))
489 mkdir_parents(etc_hostname
, 0755);
490 r
= write_string_file(etc_hostname
, arg_hostname
,
491 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
);
493 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
495 log_info("%s written.", etc_hostname
);
499 static int process_machine_id(void) {
500 const char *etc_machine_id
;
501 char id
[SD_ID128_STRING_MAX
];
504 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
505 if (laccess(etc_machine_id
, F_OK
) >= 0)
508 if (sd_id128_is_null(arg_machine_id
))
511 mkdir_parents(etc_machine_id
, 0755);
512 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
513 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
);
515 return log_error_errno(r
, "Failed to write machine id: %m");
517 log_info("%s written.", etc_machine_id
);
521 static int prompt_root_password(void) {
522 const char *msg1
, *msg2
, *etc_shadow
;
525 if (arg_root_password
)
528 if (!arg_prompt_root_password
)
531 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
532 if (laccess(etc_shadow
, F_OK
) >= 0)
538 msg1
= strjoina(special_glyph(TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip): ");
539 msg2
= strjoina(special_glyph(TRIANGULAR_BULLET
), " Please enter new root password again: ");
542 _cleanup_string_free_erase_
char *a
= NULL
, *b
= NULL
;
544 r
= ask_password_tty(msg1
, NULL
, 0, 0, NULL
, &a
);
546 return log_error_errno(r
, "Failed to query root password: %m");
549 log_warning("No password entered, skipping.");
553 r
= ask_password_tty(msg2
, NULL
, 0, 0, NULL
, &b
);
555 return log_error_errno(r
, "Failed to query root password: %m");
558 log_error("Entered passwords did not match, please try again.");
562 arg_root_password
= a
;
570 static int write_root_shadow(const char *path
, const struct spwd
*p
) {
571 _cleanup_fclose_
FILE *f
= NULL
;
576 f
= fopen(path
, "wex");
581 if (putspent(p
, f
) != 0)
582 return errno
> 0 ? -errno
: -EIO
;
584 return fflush_sync_and_check(f
);
587 static int process_root_password(void) {
589 static const char table
[] =
590 "abcdefghijklmnopqrstuvwxyz"
591 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
596 .sp_namp
= (char*) "root",
602 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
605 _cleanup_close_
int lock
= -1;
611 const char *etc_shadow
;
614 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
615 if (laccess(etc_shadow
, F_OK
) >= 0)
618 mkdir_parents(etc_shadow
, 0755);
620 lock
= take_etc_passwd_lock(arg_root
);
622 return log_error_errno(lock
, "Failed to take a lock: %m");
624 if (arg_copy_root_password
&& arg_root
) {
628 p
= getspnam("root");
629 if (p
|| errno
!= ENOENT
) {
634 return log_error_errno(errno
, "Failed to find shadow entry for root: %m");
637 r
= write_root_shadow(etc_shadow
, p
);
639 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
641 log_info("%s copied.", etc_shadow
);
646 r
= prompt_root_password();
650 if (!arg_root_password
)
653 r
= acquire_random_bytes(raw
, 16, true);
655 return log_error_errno(r
, "Failed to get salt: %m");
657 /* We only bother with SHA512 hashed passwords, the rest is legacy, and we don't do legacy. */
658 assert_cc(sizeof(table
) == 64 + 1);
659 j
= stpcpy(salt
, "$6$");
660 for (i
= 0; i
< 16; i
++)
661 j
[i
] = table
[raw
[i
] & 63];
666 item
.sp_pwdp
= crypt(arg_root_password
, salt
);
671 return log_error_errno(errno
, "Failed to encrypt password: %m");
674 item
.sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
676 r
= write_root_shadow(etc_shadow
, &item
);
678 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
680 log_info("%s written.", etc_shadow
);
684 static void help(void) {
685 printf("%s [OPTIONS...]\n\n"
686 "Configures basic settings of the system.\n\n"
687 " -h --help Show this help\n"
688 " --version Show package version\n"
689 " --root=PATH Operate on an alternate filesystem root\n"
690 " --locale=LOCALE Set primary locale (LANG=)\n"
691 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
692 " --keymap=KEYMAP Set keymap\n"
693 " --timezone=TIMEZONE Set timezone\n"
694 " --hostname=NAME Set host name\n"
695 " --machine-ID=ID Set machine ID\n"
696 " --root-password=PASSWORD Set root password\n"
697 " --root-password-file=FILE Set root password from file\n"
698 " --prompt-locale Prompt the user for locale settings\n"
699 " --prompt-keymap Prompt the user for keymap settings\n"
700 " --prompt-timezone Prompt the user for timezone\n"
701 " --prompt-hostname Prompt the user for hostname\n"
702 " --prompt-root-password Prompt the user for root password\n"
703 " --prompt Prompt for all of the above\n"
704 " --copy-locale Copy locale from host\n"
705 " --copy-keymap Copy keymap from host\n"
706 " --copy-timezone Copy timezone from host\n"
707 " --copy-root-password Copy root password from host\n"
708 " --copy Copy locale, keymap, timezone, root password\n"
709 " --setup-machine-id Generate a new random machine ID\n"
710 , 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)
784 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
790 if (!locale_is_valid(optarg
)) {
791 log_error("Locale %s is not valid.", optarg
);
795 r
= free_and_strdup(&arg_locale
, optarg
);
801 case ARG_LOCALE_MESSAGES
:
802 if (!locale_is_valid(optarg
)) {
803 log_error("Locale %s is not valid.", optarg
);
807 r
= free_and_strdup(&arg_locale_messages
, optarg
);
814 if (!keymap_is_valid(optarg
)) {
815 log_error("Keymap %s is not valid.", optarg
);
819 r
= free_and_strdup(&arg_keymap
, optarg
);
826 if (!timezone_is_valid(optarg
)) {
827 log_error("Timezone %s is not valid.", optarg
);
831 r
= free_and_strdup(&arg_timezone
, optarg
);
837 case ARG_ROOT_PASSWORD
:
838 r
= free_and_strdup(&arg_root_password
, optarg
);
843 case ARG_ROOT_PASSWORD_FILE
:
844 arg_root_password
= mfree(arg_root_password
);
846 r
= read_one_line_file(optarg
, &arg_root_password
);
848 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
853 if (!hostname_is_valid(optarg
, true)) {
854 log_error("Host name %s is not valid.", optarg
);
858 hostname_cleanup(optarg
);
859 r
= free_and_strdup(&arg_hostname
, optarg
);
866 if (sd_id128_from_string(optarg
, &arg_machine_id
) < 0) {
867 log_error("Failed to parse machine id %s.", optarg
);
874 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
= arg_prompt_root_password
= true;
877 case ARG_PROMPT_LOCALE
:
878 arg_prompt_locale
= true;
881 case ARG_PROMPT_KEYMAP
:
882 arg_prompt_keymap
= true;
885 case ARG_PROMPT_TIMEZONE
:
886 arg_prompt_timezone
= true;
889 case ARG_PROMPT_HOSTNAME
:
890 arg_prompt_hostname
= true;
893 case ARG_PROMPT_ROOT_PASSWORD
:
894 arg_prompt_root_password
= true;
898 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
= true;
901 case ARG_COPY_LOCALE
:
902 arg_copy_locale
= true;
905 case ARG_COPY_KEYMAP
:
906 arg_copy_keymap
= true;
909 case ARG_COPY_TIMEZONE
:
910 arg_copy_timezone
= true;
913 case ARG_COPY_ROOT_PASSWORD
:
914 arg_copy_root_password
= true;
917 case ARG_SETUP_MACHINE_ID
:
919 r
= sd_id128_randomize(&arg_machine_id
);
921 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
929 assert_not_reached("Unhandled option");
935 int main(int argc
, char *argv
[]) {
939 r
= parse_argv(argc
, argv
);
943 log_set_target(LOG_TARGET_AUTO
);
944 log_parse_environment();
949 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
951 log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring.");
954 if (r
> 0 && !enabled
) {
955 r
= 0; /* disabled */
959 r
= process_locale();
963 r
= process_keymap();
967 r
= process_timezone();
971 r
= process_hostname();
975 r
= process_machine_id();
979 r
= process_root_password();
986 free(arg_locale_messages
);
990 string_erase(arg_root_password
);
991 free(arg_root_password
);
993 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;