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