2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "alloc-util.h"
26 #include "ask-password-api.h"
31 #include "hostname-util.h"
32 #include "locale-util.h"
34 #include "parse-util.h"
35 #include "path-util.h"
36 #include "proc-cmdline.h"
37 #include "random-util.h"
38 #include "string-util.h"
40 #include "terminal-util.h"
41 #include "time-util.h"
42 #include "umask-util.h"
43 #include "user-util.h"
45 static char *arg_root
= NULL
;
46 static char *arg_locale
= NULL
; /* $LANG */
47 static char *arg_keymap
= NULL
;
48 static char *arg_locale_messages
= NULL
; /* $LC_MESSAGES */
49 static char *arg_timezone
= NULL
;
50 static char *arg_hostname
= NULL
;
51 static sd_id128_t arg_machine_id
= {};
52 static char *arg_root_password
= NULL
;
53 static bool arg_prompt_locale
= false;
54 static bool arg_prompt_keymap
= false;
55 static bool arg_prompt_timezone
= false;
56 static bool arg_prompt_hostname
= false;
57 static bool arg_prompt_root_password
= false;
58 static bool arg_copy_locale
= false;
59 static bool arg_copy_keymap
= false;
60 static bool arg_copy_timezone
= false;
61 static bool arg_copy_root_password
= false;
63 static bool press_any_key(void) {
67 printf("-- Press any key to proceed --");
70 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
78 static void print_welcome(void) {
79 _cleanup_free_
char *pretty_name
= NULL
;
80 const char *os_release
= NULL
;
81 static bool done
= false;
87 os_release
= prefix_roota(arg_root
, "/etc/os-release");
88 r
= parse_env_file(os_release
, NEWLINE
,
89 "PRETTY_NAME", &pretty_name
,
93 os_release
= prefix_roota(arg_root
, "/usr/lib/os-release");
94 r
= parse_env_file(os_release
, NEWLINE
,
95 "PRETTY_NAME", &pretty_name
,
99 if (r
< 0 && r
!= -ENOENT
)
100 log_warning_errno(r
, "Failed to read os-release file: %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 n
, per_column
, i
, j
;
112 unsigned break_lines
, break_modulo
;
114 assert(n_columns
> 0);
117 per_column
= (n
+ n_columns
- 1) / 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("%4u) %-*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(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 mkdir_parents(etc_localeconf
, 0755);
258 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 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 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 r
= prompt_loop("Please enter system keymap name or number", kmaps
, keymap_is_valid
, &arg_keymap
);
320 if (isempty(arg_keymap
))
326 static int process_keymap(void) {
327 const char *etc_vconsoleconf
;
331 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
332 if (laccess(etc_vconsoleconf
, F_OK
) >= 0)
335 if (arg_copy_keymap
&& arg_root
) {
337 mkdir_parents(etc_vconsoleconf
, 0755);
338 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, COPY_REFLINK
);
341 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
343 log_info("%s copied.", etc_vconsoleconf
);
350 return 0; /* don't fail if no keymaps are installed */
354 if (!isempty(arg_keymap
))
355 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
360 mkdir_parents(etc_vconsoleconf
, 0755);
361 r
= write_env_file(etc_vconsoleconf
, keymap
);
363 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
365 log_info("%s written.", etc_vconsoleconf
);
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
, &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 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 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(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 mkdir_parents(etc_hostname
, 0755);
495 r
= write_string_file(etc_hostname
, arg_hostname
,
496 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
);
498 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
500 log_info("%s written.", etc_hostname
);
504 static int process_machine_id(void) {
505 const char *etc_machine_id
;
506 char id
[SD_ID128_STRING_MAX
];
509 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
510 if (laccess(etc_machine_id
, F_OK
) >= 0)
513 if (sd_id128_is_null(arg_machine_id
))
516 mkdir_parents(etc_machine_id
, 0755);
517 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
518 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
);
520 return log_error_errno(r
, "Failed to write machine id: %m");
522 log_info("%s written.", etc_machine_id
);
526 static int prompt_root_password(void) {
527 const char *msg1
, *msg2
, *etc_shadow
;
530 if (arg_root_password
)
533 if (!arg_prompt_root_password
)
536 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
537 if (laccess(etc_shadow
, F_OK
) >= 0)
543 msg1
= strjoina(special_glyph(TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip): ");
544 msg2
= strjoina(special_glyph(TRIANGULAR_BULLET
), " Please enter new root password again: ");
547 _cleanup_string_free_erase_
char *a
= NULL
, *b
= NULL
;
549 r
= ask_password_tty(msg1
, NULL
, 0, 0, NULL
, &a
);
551 return log_error_errno(r
, "Failed to query root password: %m");
554 log_warning("No password entered, skipping.");
558 r
= ask_password_tty(msg2
, NULL
, 0, 0, NULL
, &b
);
560 return log_error_errno(r
, "Failed to query root password: %m");
563 log_error("Entered passwords did not match, please try again.");
567 arg_root_password
= a
;
575 static int write_root_shadow(const char *path
, const struct spwd
*p
) {
576 _cleanup_fclose_
FILE *f
= NULL
;
581 f
= fopen(path
, "wex");
586 if (putspent(p
, f
) != 0)
587 return errno
> 0 ? -errno
: -EIO
;
589 return fflush_sync_and_check(f
);
592 static int process_root_password(void) {
594 static const char table
[] =
595 "abcdefghijklmnopqrstuvwxyz"
596 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
601 .sp_namp
= (char*) "root",
607 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
610 _cleanup_close_
int lock
= -1;
616 const char *etc_shadow
;
619 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
620 if (laccess(etc_shadow
, F_OK
) >= 0)
623 mkdir_parents(etc_shadow
, 0755);
625 lock
= take_etc_passwd_lock(arg_root
);
627 return log_error_errno(lock
, "Failed to take a lock: %m");
629 if (arg_copy_root_password
&& arg_root
) {
633 p
= getspnam("root");
634 if (p
|| errno
!= ENOENT
) {
639 return log_error_errno(errno
, "Failed to find shadow entry for root: %m");
642 r
= write_root_shadow(etc_shadow
, p
);
644 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
646 log_info("%s copied.", etc_shadow
);
651 r
= prompt_root_password();
655 if (!arg_root_password
)
658 r
= acquire_random_bytes(raw
, 16, true);
660 return log_error_errno(r
, "Failed to get salt: %m");
662 /* We only bother with SHA512 hashed passwords, the rest is legacy, and we don't do legacy. */
663 assert_cc(sizeof(table
) == 64 + 1);
664 j
= stpcpy(salt
, "$6$");
665 for (i
= 0; i
< 16; i
++)
666 j
[i
] = table
[raw
[i
] & 63];
671 item
.sp_pwdp
= crypt(arg_root_password
, salt
);
676 return log_error_errno(errno
, "Failed to encrypt password: %m");
679 item
.sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
681 r
= write_root_shadow(etc_shadow
, &item
);
683 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
685 log_info("%s written.", etc_shadow
);
689 static void help(void) {
690 printf("%s [OPTIONS...]\n\n"
691 "Configures basic settings of the system.\n\n"
692 " -h --help Show this help\n"
693 " --version Show package version\n"
694 " --root=PATH Operate on an alternate filesystem root\n"
695 " --locale=LOCALE Set primary locale (LANG=)\n"
696 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
697 " --keymap=KEYMAP Set keymap\n"
698 " --timezone=TIMEZONE Set timezone\n"
699 " --hostname=NAME Set host name\n"
700 " --machine-ID=ID Set machine ID\n"
701 " --root-password=PASSWORD Set root password\n"
702 " --root-password-file=FILE Set root password from file\n"
703 " --prompt-locale Prompt the user for locale settings\n"
704 " --prompt-keymap Prompt the user for keymap settings\n"
705 " --prompt-timezone Prompt the user for timezone\n"
706 " --prompt-hostname Prompt the user for hostname\n"
707 " --prompt-root-password Prompt the user for root password\n"
708 " --prompt Prompt for all of the above\n"
709 " --copy-locale Copy locale from host\n"
710 " --copy-keymap Copy keymap from host\n"
711 " --copy-timezone Copy timezone from host\n"
712 " --copy-root-password Copy root password from host\n"
713 " --copy Copy locale, keymap, timezone, root password\n"
714 " --setup-machine-id Generate a new random machine ID\n"
715 , program_invocation_short_name
);
718 static int parse_argv(int argc
, char *argv
[]) {
730 ARG_ROOT_PASSWORD_FILE
,
736 ARG_PROMPT_ROOT_PASSWORD
,
741 ARG_COPY_ROOT_PASSWORD
,
742 ARG_SETUP_MACHINE_ID
,
745 static const struct option options
[] = {
746 { "help", no_argument
, NULL
, 'h' },
747 { "version", no_argument
, NULL
, ARG_VERSION
},
748 { "root", required_argument
, NULL
, ARG_ROOT
},
749 { "locale", required_argument
, NULL
, ARG_LOCALE
},
750 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
751 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
752 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
753 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
754 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
755 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
756 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
757 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
758 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
759 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
760 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
761 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
762 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
763 { "copy", no_argument
, NULL
, ARG_COPY
},
764 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
765 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
766 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
767 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
768 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
777 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
789 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
795 if (!locale_is_valid(optarg
)) {
796 log_error("Locale %s is not valid.", optarg
);
800 r
= free_and_strdup(&arg_locale
, optarg
);
806 case ARG_LOCALE_MESSAGES
:
807 if (!locale_is_valid(optarg
)) {
808 log_error("Locale %s is not valid.", optarg
);
812 r
= free_and_strdup(&arg_locale_messages
, optarg
);
819 if (!keymap_is_valid(optarg
)) {
820 log_error("Keymap %s is not valid.", optarg
);
824 r
= free_and_strdup(&arg_keymap
, optarg
);
831 if (!timezone_is_valid(optarg
)) {
832 log_error("Timezone %s is not valid.", optarg
);
836 r
= free_and_strdup(&arg_timezone
, optarg
);
842 case ARG_ROOT_PASSWORD
:
843 r
= free_and_strdup(&arg_root_password
, optarg
);
848 case ARG_ROOT_PASSWORD_FILE
:
849 arg_root_password
= mfree(arg_root_password
);
851 r
= read_one_line_file(optarg
, &arg_root_password
);
853 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
858 if (!hostname_is_valid(optarg
, true)) {
859 log_error("Host name %s is not valid.", optarg
);
863 hostname_cleanup(optarg
);
864 r
= free_and_strdup(&arg_hostname
, optarg
);
871 if (sd_id128_from_string(optarg
, &arg_machine_id
) < 0) {
872 log_error("Failed to parse machine id %s.", optarg
);
879 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
= arg_prompt_root_password
= true;
882 case ARG_PROMPT_LOCALE
:
883 arg_prompt_locale
= true;
886 case ARG_PROMPT_KEYMAP
:
887 arg_prompt_keymap
= true;
890 case ARG_PROMPT_TIMEZONE
:
891 arg_prompt_timezone
= true;
894 case ARG_PROMPT_HOSTNAME
:
895 arg_prompt_hostname
= true;
898 case ARG_PROMPT_ROOT_PASSWORD
:
899 arg_prompt_root_password
= true;
903 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
= true;
906 case ARG_COPY_LOCALE
:
907 arg_copy_locale
= true;
910 case ARG_COPY_KEYMAP
:
911 arg_copy_keymap
= true;
914 case ARG_COPY_TIMEZONE
:
915 arg_copy_timezone
= true;
918 case ARG_COPY_ROOT_PASSWORD
:
919 arg_copy_root_password
= true;
922 case ARG_SETUP_MACHINE_ID
:
924 r
= sd_id128_randomize(&arg_machine_id
);
926 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
934 assert_not_reached("Unhandled option");
940 int main(int argc
, char *argv
[]) {
944 r
= parse_argv(argc
, argv
);
948 log_set_target(LOG_TARGET_AUTO
);
949 log_parse_environment();
954 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
956 log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring.");
959 if (r
> 0 && !enabled
) {
960 r
= 0; /* disabled */
964 r
= process_locale();
968 r
= process_keymap();
972 r
= process_timezone();
976 r
= process_hostname();
980 r
= process_machine_id();
984 r
= process_root_password();
991 free(arg_locale_messages
);
995 string_erase(arg_root_password
);
996 free(arg_root_password
);
998 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;