]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/firstboot/firstboot.c
util: move display_is_local() to pam_systemd.c, its only user
[thirdparty/systemd.git] / src / firstboot / firstboot.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
418b9be5 2
418b9be5 3#include <fcntl.h>
418b9be5 4#include <getopt.h>
3f6fd1ba 5#include <unistd.h>
418b9be5 6
545cdb90 7#if HAVE_CRYPT_H
9f555bba
BE
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.
11 *
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.
16 */
17# include <crypt.h>
18#endif
19
dccca82b
LP
20#include "sd-id128.h"
21
b5efdb8a 22#include "alloc-util.h"
3f6fd1ba 23#include "ask-password-api.h"
418b9be5 24#include "copy.h"
686d13b9 25#include "env-file.h"
6bedfcbb 26#include "fd-util.h"
3f6fd1ba 27#include "fileio.h"
f4f15635 28#include "fs-util.h"
3f6fd1ba
LP
29#include "hostname-util.h"
30#include "locale-util.h"
45f39418 31#include "main-func.h"
418b9be5 32#include "mkdir.h"
d58ad743 33#include "os-util.h"
6bedfcbb 34#include "parse-util.h"
418b9be5 35#include "path-util.h"
294bf0c3 36#include "pretty-print.h"
f582cbca 37#include "proc-cmdline.h"
3df3e884 38#include "random-util.h"
6bedfcbb 39#include "string-util.h"
3f6fd1ba 40#include "strv.h"
288a74cc 41#include "terminal-util.h"
3f6fd1ba 42#include "time-util.h"
affb60b1 43#include "umask-util.h"
e929bee0 44#include "user-util.h"
418b9be5
LP
45
46static char *arg_root = NULL;
47static char *arg_locale = NULL; /* $LANG */
ed457f13 48static char *arg_keymap = NULL;
418b9be5
LP
49static char *arg_locale_messages = NULL; /* $LC_MESSAGES */
50static char *arg_timezone = NULL;
51static char *arg_hostname = NULL;
52static sd_id128_t arg_machine_id = {};
53static char *arg_root_password = NULL;
54static bool arg_prompt_locale = false;
ed457f13 55static bool arg_prompt_keymap = false;
418b9be5
LP
56static bool arg_prompt_timezone = false;
57static bool arg_prompt_hostname = false;
58static bool arg_prompt_root_password = false;
59static bool arg_copy_locale = false;
ed457f13 60static bool arg_copy_keymap = false;
418b9be5
LP
61static bool arg_copy_timezone = false;
62static bool arg_copy_root_password = false;
63
45f39418
YW
64STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
65STATIC_DESTRUCTOR_REGISTER(arg_locale, freep);
66STATIC_DESTRUCTOR_REGISTER(arg_locale_messages, freep);
67STATIC_DESTRUCTOR_REGISTER(arg_keymap, freep);
68STATIC_DESTRUCTOR_REGISTER(arg_timezone, freep);
69STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep);
70STATIC_DESTRUCTOR_REGISTER(arg_root_password, string_free_erasep);
71
418b9be5
LP
72static bool press_any_key(void) {
73 char k = 0;
74 bool need_nl = true;
75
76 printf("-- Press any key to proceed --");
77 fflush(stdout);
78
94956f8f 79 (void) read_one_char(stdin, &k, USEC_INFINITY, &need_nl);
418b9be5
LP
80
81 if (need_nl)
82 putchar('\n');
83
84 return k != 'q';
85}
86
87static void print_welcome(void) {
88 _cleanup_free_ char *pretty_name = NULL;
418b9be5
LP
89 static bool done = false;
90 int r;
91
92 if (done)
93 return;
94
d58ad743
LP
95 r = parse_os_release(arg_root, "PRETTY_NAME", &pretty_name, NULL);
96 if (r < 0)
97 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
98 "Failed to read os-release file, ignoring: %m");
418b9be5
LP
99
100 printf("\nWelcome to your new installation of %s!\nPlease configure a few basic system settings:\n\n",
101 isempty(pretty_name) ? "Linux" : pretty_name);
102
103 press_any_key();
104
105 done = true;
106}
107
108static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) {
418b9be5 109 unsigned break_lines, break_modulo;
da6053d0 110 size_t n, per_column, i, j;
418b9be5
LP
111
112 assert(n_columns > 0);
113
114 n = strv_length(x);
be6b0c21 115 per_column = DIV_ROUND_UP(n, n_columns);
418b9be5
LP
116
117 break_lines = lines();
118 if (break_lines > 2)
119 break_lines--;
120
121 /* The first page gets two extra lines, since we want to show
122 * a title */
123 break_modulo = break_lines;
124 if (break_modulo > 3)
125 break_modulo -= 3;
126
127 for (i = 0; i < per_column; i++) {
128
129 for (j = 0; j < n_columns; j ++) {
130 _cleanup_free_ char *e = NULL;
131
132 if (j * per_column + i >= n)
133 break;
134
135 e = ellipsize(x[j * per_column + i], width, percentage);
136 if (!e)
137 return log_oom();
138
da6053d0 139 printf("%4zu) %-*s", j * per_column + i + 1, width, e);
418b9be5
LP
140 }
141
142 putchar('\n');
143
144 /* on the first screen we reserve 2 extra lines for the title */
145 if (i % break_lines == break_modulo) {
146 if (!press_any_key())
147 return 0;
148 }
149 }
150
151 return 0;
152}
153
154static int prompt_loop(const char *text, char **l, bool (*is_valid)(const char *name), char **ret) {
155 int r;
156
157 assert(text);
158 assert(is_valid);
159 assert(ret);
160
161 for (;;) {
162 _cleanup_free_ char *p = NULL;
163 unsigned u;
164
9a6f746f 165 r = ask_string(&p, "%s %s (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), text);
23bbb0de
MS
166 if (r < 0)
167 return log_error_errno(r, "Failed to query user: %m");
418b9be5
LP
168
169 if (isempty(p)) {
170 log_warning("No data entered, skipping.");
171 return 0;
172 }
173
174 r = safe_atou(p, &u);
175 if (r >= 0) {
176 char *c;
177
178 if (u <= 0 || u > strv_length(l)) {
179 log_error("Specified entry number out of range.");
180 continue;
181 }
182
183 log_info("Selected '%s'.", l[u-1]);
184
185 c = strdup(l[u-1]);
186 if (!c)
187 return log_oom();
188
189 free(*ret);
190 *ret = c;
191 return 0;
192 }
193
194 if (!is_valid(p)) {
195 log_error("Entered data invalid.");
196 continue;
197 }
198
199 free(*ret);
200 *ret = p;
201 p = 0;
202 return 0;
203 }
204}
205
206static int prompt_locale(void) {
207 _cleanup_strv_free_ char **locales = NULL;
208 int r;
209
210 if (arg_locale || arg_locale_messages)
211 return 0;
212
213 if (!arg_prompt_locale)
214 return 0;
215
216 r = get_locales(&locales);
23bbb0de
MS
217 if (r < 0)
218 return log_error_errno(r, "Cannot query locales list: %m");
418b9be5
LP
219
220 print_welcome();
221
222 printf("\nAvailable Locales:\n\n");
223 r = show_menu(locales, 3, 22, 60);
224 if (r < 0)
225 return r;
226
227 putchar('\n');
228
229 r = prompt_loop("Please enter system locale name or number", locales, locale_is_valid, &arg_locale);
230 if (r < 0)
231 return r;
232
233 if (isempty(arg_locale))
234 return 0;
235
236 r = prompt_loop("Please enter system message locale name or number", locales, locale_is_valid, &arg_locale_messages);
237 if (r < 0)
238 return r;
239
240 return 0;
241}
242
243static int process_locale(void) {
244 const char *etc_localeconf;
245 char* locales[3];
246 unsigned i = 0;
247 int r;
248
1d13f648 249 etc_localeconf = prefix_roota(arg_root, "/etc/locale.conf");
b3cd687d 250 if (laccess(etc_localeconf, F_OK) >= 0)
418b9be5
LP
251 return 0;
252
253 if (arg_copy_locale && arg_root) {
254
255 mkdir_parents(etc_localeconf, 0755);
1c876927 256 r = copy_file("/etc/locale.conf", etc_localeconf, 0, 0644, 0, COPY_REFLINK);
418b9be5 257 if (r != -ENOENT) {
23bbb0de
MS
258 if (r < 0)
259 return log_error_errno(r, "Failed to copy %s: %m", etc_localeconf);
418b9be5
LP
260
261 log_info("%s copied.", etc_localeconf);
262 return 0;
263 }
264 }
265
266 r = prompt_locale();
267 if (r < 0)
268 return r;
269
270 if (!isempty(arg_locale))
63c372cb 271 locales[i++] = strjoina("LANG=", arg_locale);
418b9be5 272 if (!isempty(arg_locale_messages) && !streq(arg_locale_messages, arg_locale))
63c372cb 273 locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages);
418b9be5
LP
274
275 if (i == 0)
276 return 0;
277
278 locales[i] = NULL;
279
280 mkdir_parents(etc_localeconf, 0755);
281 r = write_env_file(etc_localeconf, locales);
23bbb0de
MS
282 if (r < 0)
283 return log_error_errno(r, "Failed to write %s: %m", etc_localeconf);
418b9be5
LP
284
285 log_info("%s written.", etc_localeconf);
286 return 0;
287}
288
ed457f13
TB
289static int prompt_keymap(void) {
290 _cleanup_strv_free_ char **kmaps = NULL;
291 int r;
292
293 if (arg_keymap)
294 return 0;
295
296 if (!arg_prompt_keymap)
297 return 0;
298
299 r = get_keymaps(&kmaps);
300 if (r == -ENOENT) /* no keymaps installed */
301 return r;
302 if (r < 0)
303 return log_error_errno(r, "Failed to read keymaps: %m");
304
305 print_welcome();
306
307 printf("\nAvailable keymaps:\n\n");
308 r = show_menu(kmaps, 3, 22, 60);
309 if (r < 0)
310 return r;
311
312 putchar('\n');
313
8700b4da
ZJS
314 return prompt_loop("Please enter system keymap name or number",
315 kmaps, keymap_is_valid, &arg_keymap);
ed457f13
TB
316}
317
318static int process_keymap(void) {
319 const char *etc_vconsoleconf;
320 char **keymap;
321 int r;
322
323 etc_vconsoleconf = prefix_roota(arg_root, "/etc/vconsole.conf");
324 if (laccess(etc_vconsoleconf, F_OK) >= 0)
325 return 0;
326
327 if (arg_copy_keymap && arg_root) {
328
329 mkdir_parents(etc_vconsoleconf, 0755);
330 r = copy_file("/etc/vconsole.conf", etc_vconsoleconf, 0, 0644, 0, COPY_REFLINK);
331 if (r != -ENOENT) {
332 if (r < 0)
333 return log_error_errno(r, "Failed to copy %s: %m", etc_vconsoleconf);
334
335 log_info("%s copied.", etc_vconsoleconf);
336 return 0;
337 }
338 }
339
340 r = prompt_keymap();
341 if (r == -ENOENT)
342 return 0; /* don't fail if no keymaps are installed */
343 if (r < 0)
344 return r;
345
a7353b4d 346 if (isempty(arg_keymap))
ed457f13
TB
347 return 0;
348
a7353b4d
YW
349 keymap = STRV_MAKE(strjoina("KEYMAP=", arg_keymap));
350
351 r = mkdir_parents(etc_vconsoleconf, 0755);
352 if (r < 0)
353 return log_error_errno(r, "Failed to create the parent directory of %s: %m", etc_vconsoleconf);
354
ed457f13
TB
355 r = write_env_file(etc_vconsoleconf, keymap);
356 if (r < 0)
357 return log_error_errno(r, "Failed to write %s: %m", etc_vconsoleconf);
358
359 log_info("%s written.", etc_vconsoleconf);
360 return 0;
361}
362
089fb865
MG
363static bool timezone_is_valid_log_error(const char *name) {
364 return timezone_is_valid(name, LOG_ERR);
365}
366
418b9be5
LP
367static int prompt_timezone(void) {
368 _cleanup_strv_free_ char **zones = NULL;
369 int r;
370
371 if (arg_timezone)
372 return 0;
373
374 if (!arg_prompt_timezone)
375 return 0;
376
377 r = get_timezones(&zones);
23bbb0de
MS
378 if (r < 0)
379 return log_error_errno(r, "Cannot query timezone list: %m");
418b9be5
LP
380
381 print_welcome();
382
383 printf("\nAvailable Time Zones:\n\n");
384 r = show_menu(zones, 3, 22, 30);
385 if (r < 0)
386 return r;
387
388 putchar('\n');
389
089fb865 390 r = prompt_loop("Please enter timezone name or number", zones, timezone_is_valid_log_error, &arg_timezone);
418b9be5
LP
391 if (r < 0)
392 return r;
393
394 return 0;
395}
396
397static int process_timezone(void) {
398 const char *etc_localtime, *e;
399 int r;
400
1d13f648 401 etc_localtime = prefix_roota(arg_root, "/etc/localtime");
b3cd687d 402 if (laccess(etc_localtime, F_OK) >= 0)
418b9be5
LP
403 return 0;
404
405 if (arg_copy_timezone && arg_root) {
406 _cleanup_free_ char *p = NULL;
407
408 r = readlink_malloc("/etc/localtime", &p);
409 if (r != -ENOENT) {
23bbb0de
MS
410 if (r < 0)
411 return log_error_errno(r, "Failed to read host timezone: %m");
418b9be5
LP
412
413 mkdir_parents(etc_localtime, 0755);
4a62c710
MS
414 if (symlink(p, etc_localtime) < 0)
415 return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime);
418b9be5
LP
416
417 log_info("%s copied.", etc_localtime);
418 return 0;
419 }
420 }
421
422 r = prompt_timezone();
423 if (r < 0)
424 return r;
425
426 if (isempty(arg_timezone))
427 return 0;
428
63c372cb 429 e = strjoina("../usr/share/zoneinfo/", arg_timezone);
418b9be5
LP
430
431 mkdir_parents(etc_localtime, 0755);
4a62c710
MS
432 if (symlink(e, etc_localtime) < 0)
433 return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime);
418b9be5
LP
434
435 log_info("%s written", etc_localtime);
436 return 0;
437}
438
439static int prompt_hostname(void) {
440 int r;
441
442 if (arg_hostname)
443 return 0;
444
445 if (!arg_prompt_hostname)
446 return 0;
447
448 print_welcome();
449 putchar('\n');
450
451 for (;;) {
452 _cleanup_free_ char *h = NULL;
453
9a6f746f 454 r = ask_string(&h, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET));
23bbb0de
MS
455 if (r < 0)
456 return log_error_errno(r, "Failed to query hostname: %m");
418b9be5
LP
457
458 if (isempty(h)) {
459 log_warning("No hostname entered, skipping.");
460 break;
461 }
462
34ad6090 463 if (!hostname_is_valid(h, true)) {
418b9be5
LP
464 log_error("Specified hostname invalid.");
465 continue;
466 }
467
34ad6090 468 /* Get rid of the trailing dot that we allow, but don't want to see */
ae691c1d 469 arg_hostname = hostname_cleanup(h);
418b9be5
LP
470 h = NULL;
471 break;
472 }
473
474 return 0;
475}
476
477static int process_hostname(void) {
478 const char *etc_hostname;
479 int r;
480
1d13f648 481 etc_hostname = prefix_roota(arg_root, "/etc/hostname");
b3cd687d 482 if (laccess(etc_hostname, F_OK) >= 0)
418b9be5
LP
483 return 0;
484
485 r = prompt_hostname();
486 if (r < 0)
487 return r;
488
489 if (isempty(arg_hostname))
490 return 0;
491
492 mkdir_parents(etc_hostname, 0755);
0675e94a
AJ
493 r = write_string_file(etc_hostname, arg_hostname,
494 WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC);
23bbb0de
MS
495 if (r < 0)
496 return log_error_errno(r, "Failed to write %s: %m", etc_hostname);
418b9be5
LP
497
498 log_info("%s written.", etc_hostname);
499 return 0;
500}
501
502static int process_machine_id(void) {
503 const char *etc_machine_id;
504 char id[SD_ID128_STRING_MAX];
505 int r;
506
1d13f648 507 etc_machine_id = prefix_roota(arg_root, "/etc/machine-id");
b3cd687d 508 if (laccess(etc_machine_id, F_OK) >= 0)
418b9be5
LP
509 return 0;
510
3bbaff3e 511 if (sd_id128_is_null(arg_machine_id))
418b9be5
LP
512 return 0;
513
514 mkdir_parents(etc_machine_id, 0755);
0675e94a
AJ
515 r = write_string_file(etc_machine_id, sd_id128_to_string(arg_machine_id, id),
516 WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC);
23bbb0de
MS
517 if (r < 0)
518 return log_error_errno(r, "Failed to write machine id: %m");
418b9be5
LP
519
520 log_info("%s written.", etc_machine_id);
521 return 0;
522}
523
524static int prompt_root_password(void) {
525 const char *msg1, *msg2, *etc_shadow;
526 int r;
527
528 if (arg_root_password)
529 return 0;
530
531 if (!arg_prompt_root_password)
532 return 0;
533
1d13f648 534 etc_shadow = prefix_roota(arg_root, "/etc/shadow");
b3cd687d 535 if (laccess(etc_shadow, F_OK) >= 0)
418b9be5
LP
536 return 0;
537
538 print_welcome();
539 putchar('\n');
540
9a6f746f
LP
541 msg1 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter a new root password (empty to skip): ");
542 msg2 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter new root password again: ");
418b9be5
LP
543
544 for (;;) {
c7b7d74e 545 _cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
418b9be5 546
daa55720 547 r = ask_password_tty(-1, msg1, NULL, 0, 0, NULL, &a);
23bbb0de
MS
548 if (r < 0)
549 return log_error_errno(r, "Failed to query root password: %m");
c7b7d74e
XF
550 if (strv_length(a) != 1) {
551 log_warning("Received multiple passwords, where we expected one.");
552 return -EINVAL;
553 }
418b9be5 554
c7b7d74e 555 if (isempty(*a)) {
418b9be5
LP
556 log_warning("No password entered, skipping.");
557 break;
558 }
559
daa55720 560 r = ask_password_tty(-1, msg2, NULL, 0, 0, NULL, &b);
ab84f5b9 561 if (r < 0)
00843602 562 return log_error_errno(r, "Failed to query root password: %m");
418b9be5 563
c7b7d74e 564 if (!streq(*a, *b)) {
418b9be5 565 log_error("Entered passwords did not match, please try again.");
418b9be5
LP
566 continue;
567 }
568
c7b7d74e 569 arg_root_password = TAKE_PTR(*a);
418b9be5
LP
570 break;
571 }
572
573 return 0;
574}
575
576static int write_root_shadow(const char *path, const struct spwd *p) {
577 _cleanup_fclose_ FILE *f = NULL;
100d5f6e
FB
578 int r;
579
418b9be5
LP
580 assert(path);
581 assert(p);
582
3250929b
LP
583 RUN_WITH_UMASK(0777)
584 f = fopen(path, "wex");
418b9be5
LP
585 if (!f)
586 return -errno;
587
100d5f6e
FB
588 r = putspent_sane(p, f);
589 if (r < 0)
590 return r;
418b9be5 591
0675e94a 592 return fflush_sync_and_check(f);
418b9be5
LP
593}
594
595static int process_root_password(void) {
596
597 static const char table[] =
598 "abcdefghijklmnopqrstuvwxyz"
599 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
600 "0123456789"
601 "./";
602
603 struct spwd item = {
604 .sp_namp = (char*) "root",
ad525df8
IS
605 .sp_min = -1,
606 .sp_max = -1,
607 .sp_warn = -1,
418b9be5
LP
608 .sp_inact = -1,
609 .sp_expire = -1,
610 .sp_flag = (unsigned long) -1, /* this appears to be what everybody does ... */
611 };
45035609
LP
612
613 _cleanup_close_ int lock = -1;
418b9be5
LP
614 char salt[3+16+1+1];
615 uint8_t raw[16];
616 unsigned i;
617 char *j;
618
619 const char *etc_shadow;
620 int r;
621
1d13f648 622 etc_shadow = prefix_roota(arg_root, "/etc/shadow");
b3cd687d 623 if (laccess(etc_shadow, F_OK) >= 0)
418b9be5
LP
624 return 0;
625
45035609
LP
626 mkdir_parents(etc_shadow, 0755);
627
e929bee0 628 lock = take_etc_passwd_lock(arg_root);
45035609 629 if (lock < 0)
db260eed 630 return log_error_errno(lock, "Failed to take a lock: %m");
45035609 631
418b9be5
LP
632 if (arg_copy_root_password && arg_root) {
633 struct spwd *p;
634
635 errno = 0;
636 p = getspnam("root");
637 if (p || errno != ENOENT) {
638 if (!p) {
639 if (!errno)
640 errno = EIO;
641
e1427b13 642 return log_error_errno(errno, "Failed to find shadow entry for root: %m");
418b9be5
LP
643 }
644
645 r = write_root_shadow(etc_shadow, p);
23bbb0de
MS
646 if (r < 0)
647 return log_error_errno(r, "Failed to write %s: %m", etc_shadow);
418b9be5
LP
648
649 log_info("%s copied.", etc_shadow);
650 return 0;
651 }
652 }
653
654 r = prompt_root_password();
655 if (r < 0)
656 return r;
657
658 if (!arg_root_password)
659 return 0;
660
68534345
LP
661 /* Insist on the best randomness by setting RANDOM_BLOCK, this is about keeping passwords secret after all. */
662 r = genuine_random_bytes(raw, 16, RANDOM_BLOCK);
23bbb0de
MS
663 if (r < 0)
664 return log_error_errno(r, "Failed to get salt: %m");
418b9be5
LP
665
666 /* We only bother with SHA512 hashed passwords, the rest is legacy, and we don't do legacy. */
667 assert_cc(sizeof(table) == 64 + 1);
668 j = stpcpy(salt, "$6$");
669 for (i = 0; i < 16; i++)
670 j[i] = table[raw[i] & 63];
671 j[i++] = '$';
672 j[i] = 0;
673
674 errno = 0;
675 item.sp_pwdp = crypt(arg_root_password, salt);
676 if (!item.sp_pwdp) {
677 if (!errno)
4546c341 678 errno = EINVAL;
418b9be5 679
e1427b13 680 return log_error_errno(errno, "Failed to encrypt password: %m");
418b9be5
LP
681 }
682
683 item.sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
684
685 r = write_root_shadow(etc_shadow, &item);
23bbb0de
MS
686 if (r < 0)
687 return log_error_errno(r, "Failed to write %s: %m", etc_shadow);
418b9be5
LP
688
689 log_info("%s written.", etc_shadow);
690 return 0;
691}
692
37ec0fdd
LP
693static int help(void) {
694 _cleanup_free_ char *link = NULL;
695 int r;
696
697 r = terminal_urlify_man("systemd-firstboot", "1", &link);
698 if (r < 0)
699 return log_oom();
700
418b9be5
LP
701 printf("%s [OPTIONS...]\n\n"
702 "Configures basic settings of the system.\n\n"
703 " -h --help Show this help\n"
704 " --version Show package version\n"
705 " --root=PATH Operate on an alternate filesystem root\n"
706 " --locale=LOCALE Set primary locale (LANG=)\n"
707 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
ed457f13 708 " --keymap=KEYMAP Set keymap\n"
418b9be5
LP
709 " --timezone=TIMEZONE Set timezone\n"
710 " --hostname=NAME Set host name\n"
711 " --machine-ID=ID Set machine ID\n"
712 " --root-password=PASSWORD Set root password\n"
713 " --root-password-file=FILE Set root password from file\n"
714 " --prompt-locale Prompt the user for locale settings\n"
ed457f13 715 " --prompt-keymap Prompt the user for keymap settings\n"
418b9be5
LP
716 " --prompt-timezone Prompt the user for timezone\n"
717 " --prompt-hostname Prompt the user for hostname\n"
718 " --prompt-root-password Prompt the user for root password\n"
b57b0625 719 " --prompt Prompt for all of the above\n"
418b9be5 720 " --copy-locale Copy locale from host\n"
ed457f13 721 " --copy-keymap Copy keymap from host\n"
418b9be5
LP
722 " --copy-timezone Copy timezone from host\n"
723 " --copy-root-password Copy root password from host\n"
ed457f13 724 " --copy Copy locale, keymap, timezone, root password\n"
601185b4 725 " --setup-machine-id Generate a new random machine ID\n"
37ec0fdd
LP
726 "\nSee the %s for details.\n"
727 , program_invocation_short_name
728 , link
729 );
730
731 return 0;
418b9be5
LP
732}
733
734static int parse_argv(int argc, char *argv[]) {
735
736 enum {
737 ARG_VERSION = 0x100,
738 ARG_ROOT,
739 ARG_LOCALE,
740 ARG_LOCALE_MESSAGES,
ed457f13 741 ARG_KEYMAP,
418b9be5
LP
742 ARG_TIMEZONE,
743 ARG_HOSTNAME,
744 ARG_MACHINE_ID,
745 ARG_ROOT_PASSWORD,
746 ARG_ROOT_PASSWORD_FILE,
747 ARG_PROMPT,
748 ARG_PROMPT_LOCALE,
ed457f13 749 ARG_PROMPT_KEYMAP,
418b9be5
LP
750 ARG_PROMPT_TIMEZONE,
751 ARG_PROMPT_HOSTNAME,
752 ARG_PROMPT_ROOT_PASSWORD,
753 ARG_COPY,
754 ARG_COPY_LOCALE,
ed457f13 755 ARG_COPY_KEYMAP,
418b9be5
LP
756 ARG_COPY_TIMEZONE,
757 ARG_COPY_ROOT_PASSWORD,
758 ARG_SETUP_MACHINE_ID,
759 };
760
761 static const struct option options[] = {
762 { "help", no_argument, NULL, 'h' },
763 { "version", no_argument, NULL, ARG_VERSION },
764 { "root", required_argument, NULL, ARG_ROOT },
765 { "locale", required_argument, NULL, ARG_LOCALE },
766 { "locale-messages", required_argument, NULL, ARG_LOCALE_MESSAGES },
ed457f13 767 { "keymap", required_argument, NULL, ARG_KEYMAP },
418b9be5
LP
768 { "timezone", required_argument, NULL, ARG_TIMEZONE },
769 { "hostname", required_argument, NULL, ARG_HOSTNAME },
770 { "machine-id", required_argument, NULL, ARG_MACHINE_ID },
771 { "root-password", required_argument, NULL, ARG_ROOT_PASSWORD },
772 { "root-password-file", required_argument, NULL, ARG_ROOT_PASSWORD_FILE },
773 { "prompt", no_argument, NULL, ARG_PROMPT },
774 { "prompt-locale", no_argument, NULL, ARG_PROMPT_LOCALE },
ed457f13 775 { "prompt-keymap", no_argument, NULL, ARG_PROMPT_KEYMAP },
418b9be5
LP
776 { "prompt-timezone", no_argument, NULL, ARG_PROMPT_TIMEZONE },
777 { "prompt-hostname", no_argument, NULL, ARG_PROMPT_HOSTNAME },
778 { "prompt-root-password", no_argument, NULL, ARG_PROMPT_ROOT_PASSWORD },
779 { "copy", no_argument, NULL, ARG_COPY },
780 { "copy-locale", no_argument, NULL, ARG_COPY_LOCALE },
ed457f13 781 { "copy-keymap", no_argument, NULL, ARG_COPY_KEYMAP },
418b9be5
LP
782 { "copy-timezone", no_argument, NULL, ARG_COPY_TIMEZONE },
783 { "copy-root-password", no_argument, NULL, ARG_COPY_ROOT_PASSWORD },
784 { "setup-machine-id", no_argument, NULL, ARG_SETUP_MACHINE_ID },
785 {}
786 };
787
788 int r, c;
789
790 assert(argc >= 0);
791 assert(argv);
792
601185b4 793 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
418b9be5
LP
794
795 switch (c) {
796
797 case 'h':
37ec0fdd 798 return help();
418b9be5
LP
799
800 case ARG_VERSION:
3f6fd1ba 801 return version();
418b9be5
LP
802
803 case ARG_ROOT:
0f03c2a4 804 r = parse_path_argument_and_warn(optarg, true, &arg_root);
0f474365 805 if (r < 0)
0f03c2a4 806 return r;
418b9be5
LP
807 break;
808
809 case ARG_LOCALE:
baaa35ad
ZJS
810 if (!locale_is_valid(optarg))
811 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
812 "Locale %s is not valid.", optarg);
418b9be5 813
2fc09a9c
DM
814 r = free_and_strdup(&arg_locale, optarg);
815 if (r < 0)
418b9be5
LP
816 return log_oom();
817
818 break;
819
820 case ARG_LOCALE_MESSAGES:
baaa35ad
ZJS
821 if (!locale_is_valid(optarg))
822 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
823 "Locale %s is not valid.", optarg);
418b9be5 824
2fc09a9c
DM
825 r = free_and_strdup(&arg_locale_messages, optarg);
826 if (r < 0)
418b9be5
LP
827 return log_oom();
828
829 break;
830
ed457f13 831 case ARG_KEYMAP:
baaa35ad
ZJS
832 if (!keymap_is_valid(optarg))
833 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
834 "Keymap %s is not valid.", optarg);
ed457f13
TB
835
836 r = free_and_strdup(&arg_keymap, optarg);
837 if (r < 0)
838 return log_oom();
839
840 break;
841
418b9be5 842 case ARG_TIMEZONE:
baaa35ad
ZJS
843 if (!timezone_is_valid(optarg, LOG_ERR))
844 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
845 "Timezone %s is not valid.", optarg);
418b9be5 846
2fc09a9c
DM
847 r = free_and_strdup(&arg_timezone, optarg);
848 if (r < 0)
418b9be5
LP
849 return log_oom();
850
851 break;
852
853 case ARG_ROOT_PASSWORD:
2fc09a9c
DM
854 r = free_and_strdup(&arg_root_password, optarg);
855 if (r < 0)
418b9be5 856 return log_oom();
418b9be5
LP
857 break;
858
859 case ARG_ROOT_PASSWORD_FILE:
0da16248 860 arg_root_password = mfree(arg_root_password);
418b9be5
LP
861
862 r = read_one_line_file(optarg, &arg_root_password);
23bbb0de
MS
863 if (r < 0)
864 return log_error_errno(r, "Failed to read %s: %m", optarg);
418b9be5
LP
865
866 break;
867
868 case ARG_HOSTNAME:
baaa35ad
ZJS
869 if (!hostname_is_valid(optarg, true))
870 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
871 "Host name %s is not valid.", optarg);
418b9be5 872
ae691c1d 873 hostname_cleanup(optarg);
2fc09a9c
DM
874 r = free_and_strdup(&arg_hostname, optarg);
875 if (r < 0)
418b9be5
LP
876 return log_oom();
877
878 break;
879
880 case ARG_MACHINE_ID:
baaa35ad
ZJS
881 if (sd_id128_from_string(optarg, &arg_machine_id) < 0)
882 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
883 "Failed to parse machine id %s.", optarg);
418b9be5
LP
884
885 break;
886
887 case ARG_PROMPT:
ed457f13 888 arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname = arg_prompt_root_password = true;
418b9be5
LP
889 break;
890
891 case ARG_PROMPT_LOCALE:
892 arg_prompt_locale = true;
893 break;
894
ed457f13
TB
895 case ARG_PROMPT_KEYMAP:
896 arg_prompt_keymap = true;
897 break;
898
418b9be5
LP
899 case ARG_PROMPT_TIMEZONE:
900 arg_prompt_timezone = true;
901 break;
902
903 case ARG_PROMPT_HOSTNAME:
904 arg_prompt_hostname = true;
905 break;
906
907 case ARG_PROMPT_ROOT_PASSWORD:
908 arg_prompt_root_password = true;
909 break;
910
911 case ARG_COPY:
ed457f13 912 arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password = true;
e926f647 913 break;
418b9be5
LP
914
915 case ARG_COPY_LOCALE:
916 arg_copy_locale = true;
917 break;
918
ed457f13
TB
919 case ARG_COPY_KEYMAP:
920 arg_copy_keymap = true;
921 break;
922
418b9be5
LP
923 case ARG_COPY_TIMEZONE:
924 arg_copy_timezone = true;
925 break;
926
927 case ARG_COPY_ROOT_PASSWORD:
928 arg_copy_root_password = true;
929 break;
930
931 case ARG_SETUP_MACHINE_ID:
932
933 r = sd_id128_randomize(&arg_machine_id);
23bbb0de
MS
934 if (r < 0)
935 return log_error_errno(r, "Failed to generate randomized machine ID: %m");
418b9be5
LP
936
937 break;
938
939 case '?':
940 return -EINVAL;
941
942 default:
943 assert_not_reached("Unhandled option");
944 }
418b9be5
LP
945
946 return 1;
947}
948
45f39418 949static int run(int argc, char *argv[]) {
1d84ad94 950 bool enabled;
418b9be5
LP
951 int r;
952
953 r = parse_argv(argc, argv);
954 if (r <= 0)
45f39418 955 return r;
418b9be5 956
6bf3c61c 957 log_setup_service();
418b9be5
LP
958
959 umask(0022);
960
1d84ad94 961 r = proc_cmdline_get_bool("systemd.firstboot", &enabled);
45f39418
YW
962 if (r < 0)
963 return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
964 if (r > 0 && !enabled)
965 return 0; /* disabled */
f582cbca 966
418b9be5
LP
967 r = process_locale();
968 if (r < 0)
45f39418 969 return r;
418b9be5 970
ed457f13
TB
971 r = process_keymap();
972 if (r < 0)
45f39418 973 return r;
ed457f13 974
418b9be5
LP
975 r = process_timezone();
976 if (r < 0)
45f39418 977 return r;
418b9be5
LP
978
979 r = process_hostname();
980 if (r < 0)
45f39418 981 return r;
418b9be5
LP
982
983 r = process_machine_id();
984 if (r < 0)
45f39418 985 return r;
418b9be5
LP
986
987 r = process_root_password();
988 if (r < 0)
45f39418
YW
989 return r;
990
991 return 0;
418b9be5 992}
45f39418
YW
993
994DEFINE_MAIN_FUNCTION(run);