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_locale_messages
= NULL
; /* $LC_MESSAGES */
48 static char *arg_timezone
= NULL
;
49 static char *arg_hostname
= NULL
;
50 static sd_id128_t arg_machine_id
= {};
51 static char *arg_root_password
= NULL
;
52 static bool arg_prompt_locale
= 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_timezone
= false;
58 static bool arg_copy_root_password
= false;
60 static bool press_any_key(void) {
64 printf("-- Press any key to proceed --");
67 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
75 static void print_welcome(void) {
76 _cleanup_free_
char *pretty_name
= NULL
;
77 const char *os_release
= NULL
;
78 static bool done
= false;
84 os_release
= prefix_roota(arg_root
, "/etc/os-release");
85 r
= parse_env_file(os_release
, NEWLINE
,
86 "PRETTY_NAME", &pretty_name
,
90 os_release
= prefix_roota(arg_root
, "/usr/lib/os-release");
91 r
= parse_env_file(os_release
, NEWLINE
,
92 "PRETTY_NAME", &pretty_name
,
96 if (r
< 0 && r
!= -ENOENT
)
97 log_warning_errno(r
, "Failed to read os-release file: %m");
99 printf("\nWelcome to your new installation of %s!\nPlease configure a few basic system settings:\n\n",
100 isempty(pretty_name
) ? "Linux" : pretty_name
);
107 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
108 unsigned n
, per_column
, i
, j
;
109 unsigned break_lines
, break_modulo
;
111 assert(n_columns
> 0);
114 per_column
= (n
+ n_columns
- 1) / n_columns
;
116 break_lines
= lines();
120 /* The first page gets two extra lines, since we want to show
122 break_modulo
= break_lines
;
123 if (break_modulo
> 3)
126 for (i
= 0; i
< per_column
; i
++) {
128 for (j
= 0; j
< n_columns
; j
++) {
129 _cleanup_free_
char *e
= NULL
;
131 if (j
* per_column
+ i
>= n
)
134 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
138 printf("%4u) %-*s", j
* per_column
+ i
+ 1, width
, e
);
143 /* on the first screen we reserve 2 extra lines for the title */
144 if (i
% break_lines
== break_modulo
) {
145 if (!press_any_key())
153 static int prompt_loop(const char *text
, char **l
, bool (*is_valid
)(const char *name
), char **ret
) {
161 _cleanup_free_
char *p
= NULL
;
164 r
= ask_string(&p
, "%s %s (empty to skip): ", special_glyph(TRIANGULAR_BULLET
), text
);
166 return log_error_errno(r
, "Failed to query user: %m");
169 log_warning("No data entered, skipping.");
173 r
= safe_atou(p
, &u
);
177 if (u
<= 0 || u
> strv_length(l
)) {
178 log_error("Specified entry number out of range.");
182 log_info("Selected '%s'.", l
[u
-1]);
194 log_error("Entered data invalid.");
205 static int prompt_locale(void) {
206 _cleanup_strv_free_
char **locales
= NULL
;
209 if (arg_locale
|| arg_locale_messages
)
212 if (!arg_prompt_locale
)
215 r
= get_locales(&locales
);
217 return log_error_errno(r
, "Cannot query locales list: %m");
221 printf("\nAvailable Locales:\n\n");
222 r
= show_menu(locales
, 3, 22, 60);
228 r
= prompt_loop("Please enter system locale name or number", locales
, locale_is_valid
, &arg_locale
);
232 if (isempty(arg_locale
))
235 r
= prompt_loop("Please enter system message locale name or number", locales
, locale_is_valid
, &arg_locale_messages
);
242 static int process_locale(void) {
243 const char *etc_localeconf
;
248 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
249 if (laccess(etc_localeconf
, F_OK
) >= 0)
252 if (arg_copy_locale
&& arg_root
) {
254 mkdir_parents(etc_localeconf
, 0755);
255 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, COPY_REFLINK
);
258 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
260 log_info("%s copied.", etc_localeconf
);
269 if (!isempty(arg_locale
))
270 locales
[i
++] = strjoina("LANG=", arg_locale
);
271 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
272 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
279 mkdir_parents(etc_localeconf
, 0755);
280 r
= write_env_file(etc_localeconf
, locales
);
282 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
284 log_info("%s written.", etc_localeconf
);
288 static int prompt_timezone(void) {
289 _cleanup_strv_free_
char **zones
= NULL
;
295 if (!arg_prompt_timezone
)
298 r
= get_timezones(&zones
);
300 return log_error_errno(r
, "Cannot query timezone list: %m");
304 printf("\nAvailable Time Zones:\n\n");
305 r
= show_menu(zones
, 3, 22, 30);
311 r
= prompt_loop("Please enter timezone name or number", zones
, timezone_is_valid
, &arg_timezone
);
318 static int process_timezone(void) {
319 const char *etc_localtime
, *e
;
322 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
323 if (laccess(etc_localtime
, F_OK
) >= 0)
326 if (arg_copy_timezone
&& arg_root
) {
327 _cleanup_free_
char *p
= NULL
;
329 r
= readlink_malloc("/etc/localtime", &p
);
332 return log_error_errno(r
, "Failed to read host timezone: %m");
334 mkdir_parents(etc_localtime
, 0755);
335 if (symlink(p
, etc_localtime
) < 0)
336 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
338 log_info("%s copied.", etc_localtime
);
343 r
= prompt_timezone();
347 if (isempty(arg_timezone
))
350 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
352 mkdir_parents(etc_localtime
, 0755);
353 if (symlink(e
, etc_localtime
) < 0)
354 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
356 log_info("%s written", etc_localtime
);
360 static int prompt_hostname(void) {
366 if (!arg_prompt_hostname
)
373 _cleanup_free_
char *h
= NULL
;
375 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(TRIANGULAR_BULLET
));
377 return log_error_errno(r
, "Failed to query hostname: %m");
380 log_warning("No hostname entered, skipping.");
384 if (!hostname_is_valid(h
, true)) {
385 log_error("Specified hostname invalid.");
389 /* Get rid of the trailing dot that we allow, but don't want to see */
390 arg_hostname
= hostname_cleanup(h
);
398 static int process_hostname(void) {
399 const char *etc_hostname
;
402 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
403 if (laccess(etc_hostname
, F_OK
) >= 0)
406 r
= prompt_hostname();
410 if (isempty(arg_hostname
))
413 mkdir_parents(etc_hostname
, 0755);
414 r
= write_string_file(etc_hostname
, arg_hostname
, WRITE_STRING_FILE_CREATE
);
416 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
418 log_info("%s written.", etc_hostname
);
422 static int process_machine_id(void) {
423 const char *etc_machine_id
;
424 char id
[SD_ID128_STRING_MAX
];
427 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
428 if (laccess(etc_machine_id
, F_OK
) >= 0)
431 if (sd_id128_is_null(arg_machine_id
))
434 mkdir_parents(etc_machine_id
, 0755);
435 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
), WRITE_STRING_FILE_CREATE
);
437 return log_error_errno(r
, "Failed to write machine id: %m");
439 log_info("%s written.", etc_machine_id
);
443 static int prompt_root_password(void) {
444 const char *msg1
, *msg2
, *etc_shadow
;
447 if (arg_root_password
)
450 if (!arg_prompt_root_password
)
453 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
454 if (laccess(etc_shadow
, F_OK
) >= 0)
460 msg1
= strjoina(special_glyph(TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip): ");
461 msg2
= strjoina(special_glyph(TRIANGULAR_BULLET
), " Please enter new root password again: ");
464 _cleanup_string_free_erase_
char *a
= NULL
, *b
= NULL
;
466 r
= ask_password_tty(msg1
, NULL
, 0, 0, NULL
, &a
);
468 return log_error_errno(r
, "Failed to query root password: %m");
471 log_warning("No password entered, skipping.");
475 r
= ask_password_tty(msg2
, NULL
, 0, 0, NULL
, &b
);
477 return log_error_errno(r
, "Failed to query root password: %m");
480 log_error("Entered passwords did not match, please try again.");
484 arg_root_password
= a
;
492 static int write_root_shadow(const char *path
, const struct spwd
*p
) {
493 _cleanup_fclose_
FILE *f
= NULL
;
498 f
= fopen(path
, "wex");
503 if (putspent(p
, f
) != 0)
504 return errno
> 0 ? -errno
: -EIO
;
506 return fflush_and_check(f
);
509 static int process_root_password(void) {
511 static const char table
[] =
512 "abcdefghijklmnopqrstuvwxyz"
513 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
518 .sp_namp
= (char*) "root",
524 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
527 _cleanup_close_
int lock
= -1;
533 const char *etc_shadow
;
536 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
537 if (laccess(etc_shadow
, F_OK
) >= 0)
540 mkdir_parents(etc_shadow
, 0755);
542 lock
= take_etc_passwd_lock(arg_root
);
544 return log_error_errno(lock
, "Failed to take a lock: %m");
546 if (arg_copy_root_password
&& arg_root
) {
550 p
= getspnam("root");
551 if (p
|| errno
!= ENOENT
) {
556 return log_error_errno(errno
, "Failed to find shadow entry for root: %m");
559 r
= write_root_shadow(etc_shadow
, p
);
561 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
563 log_info("%s copied.", etc_shadow
);
568 r
= prompt_root_password();
572 if (!arg_root_password
)
575 r
= dev_urandom(raw
, 16);
577 return log_error_errno(r
, "Failed to get salt: %m");
579 /* We only bother with SHA512 hashed passwords, the rest is legacy, and we don't do legacy. */
580 assert_cc(sizeof(table
) == 64 + 1);
581 j
= stpcpy(salt
, "$6$");
582 for (i
= 0; i
< 16; i
++)
583 j
[i
] = table
[raw
[i
] & 63];
588 item
.sp_pwdp
= crypt(arg_root_password
, salt
);
593 return log_error_errno(errno
, "Failed to encrypt password: %m");
596 item
.sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
598 r
= write_root_shadow(etc_shadow
, &item
);
600 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
602 log_info("%s written.", etc_shadow
);
606 static void help(void) {
607 printf("%s [OPTIONS...]\n\n"
608 "Configures basic settings of the system.\n\n"
609 " -h --help Show this help\n"
610 " --version Show package version\n"
611 " --root=PATH Operate on an alternate filesystem root\n"
612 " --locale=LOCALE Set primary locale (LANG=)\n"
613 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
614 " --timezone=TIMEZONE Set timezone\n"
615 " --hostname=NAME Set host name\n"
616 " --machine-ID=ID Set machine ID\n"
617 " --root-password=PASSWORD Set root password\n"
618 " --root-password-file=FILE Set root password from file\n"
619 " --prompt-locale Prompt the user for locale settings\n"
620 " --prompt-timezone Prompt the user for timezone\n"
621 " --prompt-hostname Prompt the user for hostname\n"
622 " --prompt-root-password Prompt the user for root password\n"
623 " --prompt Prompt for all of the above\n"
624 " --copy-locale Copy locale from host\n"
625 " --copy-timezone Copy timezone from host\n"
626 " --copy-root-password Copy root password from host\n"
627 " --copy Copy locale, timezone, root password\n"
628 " --setup-machine-id Generate a new random machine ID\n"
629 , program_invocation_short_name
);
632 static int parse_argv(int argc
, char *argv
[]) {
643 ARG_ROOT_PASSWORD_FILE
,
648 ARG_PROMPT_ROOT_PASSWORD
,
652 ARG_COPY_ROOT_PASSWORD
,
653 ARG_SETUP_MACHINE_ID
,
656 static const struct option options
[] = {
657 { "help", no_argument
, NULL
, 'h' },
658 { "version", no_argument
, NULL
, ARG_VERSION
},
659 { "root", required_argument
, NULL
, ARG_ROOT
},
660 { "locale", required_argument
, NULL
, ARG_LOCALE
},
661 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
662 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
663 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
664 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
665 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
666 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
667 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
668 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
669 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
670 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
671 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
672 { "copy", no_argument
, NULL
, ARG_COPY
},
673 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
674 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
675 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
676 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
685 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
697 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
703 if (!locale_is_valid(optarg
)) {
704 log_error("Locale %s is not valid.", optarg
);
708 r
= free_and_strdup(&arg_locale
, optarg
);
714 case ARG_LOCALE_MESSAGES
:
715 if (!locale_is_valid(optarg
)) {
716 log_error("Locale %s is not valid.", optarg
);
720 r
= free_and_strdup(&arg_locale_messages
, optarg
);
727 if (!timezone_is_valid(optarg
)) {
728 log_error("Timezone %s is not valid.", optarg
);
732 r
= free_and_strdup(&arg_timezone
, optarg
);
738 case ARG_ROOT_PASSWORD
:
739 r
= free_and_strdup(&arg_root_password
, optarg
);
744 case ARG_ROOT_PASSWORD_FILE
:
745 arg_root_password
= mfree(arg_root_password
);
747 r
= read_one_line_file(optarg
, &arg_root_password
);
749 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
754 if (!hostname_is_valid(optarg
, true)) {
755 log_error("Host name %s is not valid.", optarg
);
759 hostname_cleanup(optarg
);
760 r
= free_and_strdup(&arg_hostname
, optarg
);
767 if (sd_id128_from_string(optarg
, &arg_machine_id
) < 0) {
768 log_error("Failed to parse machine id %s.", optarg
);
775 arg_prompt_locale
= arg_prompt_timezone
= arg_prompt_hostname
= arg_prompt_root_password
= true;
778 case ARG_PROMPT_LOCALE
:
779 arg_prompt_locale
= true;
782 case ARG_PROMPT_TIMEZONE
:
783 arg_prompt_timezone
= true;
786 case ARG_PROMPT_HOSTNAME
:
787 arg_prompt_hostname
= true;
790 case ARG_PROMPT_ROOT_PASSWORD
:
791 arg_prompt_root_password
= true;
795 arg_copy_locale
= arg_copy_timezone
= arg_copy_root_password
= true;
798 case ARG_COPY_LOCALE
:
799 arg_copy_locale
= true;
802 case ARG_COPY_TIMEZONE
:
803 arg_copy_timezone
= true;
806 case ARG_COPY_ROOT_PASSWORD
:
807 arg_copy_root_password
= true;
810 case ARG_SETUP_MACHINE_ID
:
812 r
= sd_id128_randomize(&arg_machine_id
);
814 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
822 assert_not_reached("Unhandled option");
828 int main(int argc
, char *argv
[]) {
832 r
= parse_argv(argc
, argv
);
836 log_set_target(LOG_TARGET_AUTO
);
837 log_parse_environment();
842 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
844 log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring.");
847 if (r
> 0 && !enabled
) {
848 r
= 0; /* disabled */
852 r
= process_locale();
856 r
= process_timezone();
860 r
= process_hostname();
864 r
= process_machine_id();
868 r
= process_root_password();
875 free(arg_locale_messages
);
878 string_erase(arg_root_password
);
879 free(arg_root_password
);
881 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;