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