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