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