]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/firstboot/firstboot.c
Merge pull request #13010 from poettering/fsck-usr-wants
[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 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
245 static int process_locale(void) {
246 const char *etc_localeconf;
247 char* locales[3];
248 unsigned i = 0;
249 int r;
250
251 etc_localeconf = prefix_roota(arg_root, "/etc/locale.conf");
252 if (laccess(etc_localeconf, F_OK) >= 0)
253 return 0;
254
255 if (arg_copy_locale && arg_root) {
256
257 (void) mkdir_parents(etc_localeconf, 0755);
258 r = copy_file("/etc/locale.conf", etc_localeconf, 0, 0644, 0, 0, COPY_REFLINK);
259 if (r != -ENOENT) {
260 if (r < 0)
261 return log_error_errno(r, "Failed to copy %s: %m", etc_localeconf);
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))
273 locales[i++] = strjoina("LANG=", arg_locale);
274 if (!isempty(arg_locale_messages) && !streq(arg_locale_messages, arg_locale))
275 locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages);
276
277 if (i == 0)
278 return 0;
279
280 locales[i] = NULL;
281
282 (void) mkdir_parents(etc_localeconf, 0755);
283 r = write_env_file(etc_localeconf, locales);
284 if (r < 0)
285 return log_error_errno(r, "Failed to write %s: %m", etc_localeconf);
286
287 log_info("%s written.", etc_localeconf);
288 return 0;
289 }
290
291 static 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 return prompt_loop("Please enter system keymap name or number",
317 kmaps, keymap_is_valid, &arg_keymap);
318 }
319
320 static int process_keymap(void) {
321 const char *etc_vconsoleconf;
322 char **keymap;
323 int r;
324
325 etc_vconsoleconf = prefix_roota(arg_root, "/etc/vconsole.conf");
326 if (laccess(etc_vconsoleconf, F_OK) >= 0)
327 return 0;
328
329 if (arg_copy_keymap && arg_root) {
330
331 (void) mkdir_parents(etc_vconsoleconf, 0755);
332 r = copy_file("/etc/vconsole.conf", etc_vconsoleconf, 0, 0644, 0, 0, COPY_REFLINK);
333 if (r != -ENOENT) {
334 if (r < 0)
335 return log_error_errno(r, "Failed to copy %s: %m", etc_vconsoleconf);
336
337 log_info("%s copied.", etc_vconsoleconf);
338 return 0;
339 }
340 }
341
342 r = prompt_keymap();
343 if (r == -ENOENT)
344 return 0; /* don't fail if no keymaps are installed */
345 if (r < 0)
346 return r;
347
348 if (isempty(arg_keymap))
349 return 0;
350
351 keymap = STRV_MAKE(strjoina("KEYMAP=", arg_keymap));
352
353 r = mkdir_parents(etc_vconsoleconf, 0755);
354 if (r < 0)
355 return log_error_errno(r, "Failed to create the parent directory of %s: %m", etc_vconsoleconf);
356
357 r = write_env_file(etc_vconsoleconf, keymap);
358 if (r < 0)
359 return log_error_errno(r, "Failed to write %s: %m", etc_vconsoleconf);
360
361 log_info("%s written.", etc_vconsoleconf);
362 return 0;
363 }
364
365 static bool timezone_is_valid_log_error(const char *name) {
366 return timezone_is_valid(name, LOG_ERR);
367 }
368
369 static 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);
380 if (r < 0)
381 return log_error_errno(r, "Cannot query timezone list: %m");
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_log_error, &arg_timezone);
393 if (r < 0)
394 return r;
395
396 return 0;
397 }
398
399 static int process_timezone(void) {
400 const char *etc_localtime, *e;
401 int r;
402
403 etc_localtime = prefix_roota(arg_root, "/etc/localtime");
404 if (laccess(etc_localtime, F_OK) >= 0)
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) {
412 if (r < 0)
413 return log_error_errno(r, "Failed to read host timezone: %m");
414
415 (void) mkdir_parents(etc_localtime, 0755);
416 if (symlink(p, etc_localtime) < 0)
417 return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime);
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
431 e = strjoina("../usr/share/zoneinfo/", arg_timezone);
432
433 (void) mkdir_parents(etc_localtime, 0755);
434 if (symlink(e, etc_localtime) < 0)
435 return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime);
436
437 log_info("%s written", etc_localtime);
438 return 0;
439 }
440
441 static 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
456 r = ask_string(&h, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET));
457 if (r < 0)
458 return log_error_errno(r, "Failed to query hostname: %m");
459
460 if (isempty(h)) {
461 log_warning("No hostname entered, skipping.");
462 break;
463 }
464
465 if (!hostname_is_valid(h, true)) {
466 log_error("Specified hostname invalid.");
467 continue;
468 }
469
470 /* Get rid of the trailing dot that we allow, but don't want to see */
471 arg_hostname = hostname_cleanup(h);
472 h = NULL;
473 break;
474 }
475
476 return 0;
477 }
478
479 static int process_hostname(void) {
480 const char *etc_hostname;
481 int r;
482
483 etc_hostname = prefix_roota(arg_root, "/etc/hostname");
484 if (laccess(etc_hostname, F_OK) >= 0)
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 r = write_string_file(etc_hostname, arg_hostname,
495 WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755);
496 if (r < 0)
497 return log_error_errno(r, "Failed to write %s: %m", etc_hostname);
498
499 log_info("%s written.", etc_hostname);
500 return 0;
501 }
502
503 static int process_machine_id(void) {
504 const char *etc_machine_id;
505 char id[SD_ID128_STRING_MAX];
506 int r;
507
508 etc_machine_id = prefix_roota(arg_root, "/etc/machine-id");
509 if (laccess(etc_machine_id, F_OK) >= 0)
510 return 0;
511
512 if (sd_id128_is_null(arg_machine_id))
513 return 0;
514
515 r = write_string_file(etc_machine_id, sd_id128_to_string(arg_machine_id, id),
516 WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755);
517 if (r < 0)
518 return log_error_errno(r, "Failed to write machine id: %m");
519
520 log_info("%s written.", etc_machine_id);
521 return 0;
522 }
523
524 static int prompt_root_password(void) {
525 const char *msg1, *msg2, *etc_shadow;
526 int r;
527
528 if (arg_root_password)
529 return 0;
530
531 if (!arg_prompt_root_password)
532 return 0;
533
534 etc_shadow = prefix_roota(arg_root, "/etc/shadow");
535 if (laccess(etc_shadow, F_OK) >= 0)
536 return 0;
537
538 print_welcome();
539 putchar('\n');
540
541 msg1 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter a new root password (empty to skip): ");
542 msg2 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter new root password again: ");
543
544 for (;;) {
545 _cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
546
547 r = ask_password_tty(-1, msg1, NULL, 0, 0, NULL, &a);
548 if (r < 0)
549 return log_error_errno(r, "Failed to query root password: %m");
550 if (strv_length(a) != 1) {
551 log_warning("Received multiple passwords, where we expected one.");
552 return -EINVAL;
553 }
554
555 if (isempty(*a)) {
556 log_warning("No password entered, skipping.");
557 break;
558 }
559
560 r = ask_password_tty(-1, msg2, NULL, 0, 0, NULL, &b);
561 if (r < 0)
562 return log_error_errno(r, "Failed to query root password: %m");
563
564 if (!streq(*a, *b)) {
565 log_error("Entered passwords did not match, please try again.");
566 continue;
567 }
568
569 arg_root_password = TAKE_PTR(*a);
570 break;
571 }
572
573 return 0;
574 }
575
576 static int write_root_shadow(const char *path, const struct spwd *p) {
577 _cleanup_fclose_ FILE *f = NULL;
578 int r;
579
580 assert(path);
581 assert(p);
582
583 RUN_WITH_UMASK(0777)
584 f = fopen(path, "wex");
585 if (!f)
586 return -errno;
587
588 r = putspent_sane(p, f);
589 if (r < 0)
590 return r;
591
592 return fflush_sync_and_check(f);
593 }
594
595 static int process_root_password(void) {
596
597 struct spwd item = {
598 .sp_namp = (char*) "root",
599 .sp_min = -1,
600 .sp_max = -1,
601 .sp_warn = -1,
602 .sp_inact = -1,
603 .sp_expire = -1,
604 .sp_flag = (unsigned long) -1, /* this appears to be what everybody does ... */
605 };
606 _cleanup_free_ char *salt = NULL;
607 _cleanup_close_ int lock = -1;
608 struct crypt_data cd = {};
609
610 const char *etc_shadow;
611 int r;
612
613 etc_shadow = prefix_roota(arg_root, "/etc/shadow");
614 if (laccess(etc_shadow, F_OK) >= 0)
615 return 0;
616
617 (void) mkdir_parents(etc_shadow, 0755);
618
619 lock = take_etc_passwd_lock(arg_root);
620 if (lock < 0)
621 return log_error_errno(lock, "Failed to take a lock: %m");
622
623 if (arg_copy_root_password && arg_root) {
624 struct spwd *p;
625
626 errno = 0;
627 p = getspnam("root");
628 if (p || errno != ENOENT) {
629 if (!p) {
630 if (!errno)
631 errno = EIO;
632
633 return log_error_errno(errno, "Failed to find shadow entry for root: %m");
634 }
635
636 r = write_root_shadow(etc_shadow, p);
637 if (r < 0)
638 return log_error_errno(r, "Failed to write %s: %m", etc_shadow);
639
640 log_info("%s copied.", etc_shadow);
641 return 0;
642 }
643 }
644
645 r = prompt_root_password();
646 if (r < 0)
647 return r;
648
649 if (!arg_root_password)
650 return 0;
651
652 r = make_salt(&salt);
653 if (r < 0)
654 return log_error_errno(r, "Failed to get salt: %m");
655
656 errno = 0;
657 item.sp_pwdp = crypt_r(arg_root_password, salt, &cd);
658 if (!item.sp_pwdp)
659 return log_error_errno(errno == 0 ? SYNTHETIC_ERRNO(EINVAL) : errno,
660 "Failed to encrypt password: %m");
661
662 item.sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
663
664 r = write_root_shadow(etc_shadow, &item);
665 if (r < 0)
666 return log_error_errno(r, "Failed to write %s: %m", etc_shadow);
667
668 log_info("%s written.", etc_shadow);
669 return 0;
670 }
671
672 static int help(void) {
673 _cleanup_free_ char *link = NULL;
674 int r;
675
676 r = terminal_urlify_man("systemd-firstboot", "1", &link);
677 if (r < 0)
678 return log_oom();
679
680 printf("%s [OPTIONS...]\n\n"
681 "Configures basic settings of the system.\n\n"
682 " -h --help Show this help\n"
683 " --version Show package version\n"
684 " --root=PATH Operate on an alternate filesystem root\n"
685 " --locale=LOCALE Set primary locale (LANG=)\n"
686 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
687 " --keymap=KEYMAP Set keymap\n"
688 " --timezone=TIMEZONE Set timezone\n"
689 " --hostname=NAME Set host name\n"
690 " --machine-ID=ID Set machine ID\n"
691 " --root-password=PASSWORD Set root password\n"
692 " --root-password-file=FILE Set root password from file\n"
693 " --prompt-locale Prompt the user for locale settings\n"
694 " --prompt-keymap Prompt the user for keymap settings\n"
695 " --prompt-timezone Prompt the user for timezone\n"
696 " --prompt-hostname Prompt the user for hostname\n"
697 " --prompt-root-password Prompt the user for root password\n"
698 " --prompt Prompt for all of the above\n"
699 " --copy-locale Copy locale from host\n"
700 " --copy-keymap Copy keymap from host\n"
701 " --copy-timezone Copy timezone from host\n"
702 " --copy-root-password Copy root password from host\n"
703 " --copy Copy locale, keymap, timezone, root password\n"
704 " --setup-machine-id Generate a new random machine ID\n"
705 "\nSee the %s for details.\n"
706 , program_invocation_short_name
707 , link
708 );
709
710 return 0;
711 }
712
713 static int parse_argv(int argc, char *argv[]) {
714
715 enum {
716 ARG_VERSION = 0x100,
717 ARG_ROOT,
718 ARG_LOCALE,
719 ARG_LOCALE_MESSAGES,
720 ARG_KEYMAP,
721 ARG_TIMEZONE,
722 ARG_HOSTNAME,
723 ARG_MACHINE_ID,
724 ARG_ROOT_PASSWORD,
725 ARG_ROOT_PASSWORD_FILE,
726 ARG_PROMPT,
727 ARG_PROMPT_LOCALE,
728 ARG_PROMPT_KEYMAP,
729 ARG_PROMPT_TIMEZONE,
730 ARG_PROMPT_HOSTNAME,
731 ARG_PROMPT_ROOT_PASSWORD,
732 ARG_COPY,
733 ARG_COPY_LOCALE,
734 ARG_COPY_KEYMAP,
735 ARG_COPY_TIMEZONE,
736 ARG_COPY_ROOT_PASSWORD,
737 ARG_SETUP_MACHINE_ID,
738 };
739
740 static const struct option options[] = {
741 { "help", no_argument, NULL, 'h' },
742 { "version", no_argument, NULL, ARG_VERSION },
743 { "root", required_argument, NULL, ARG_ROOT },
744 { "locale", required_argument, NULL, ARG_LOCALE },
745 { "locale-messages", required_argument, NULL, ARG_LOCALE_MESSAGES },
746 { "keymap", required_argument, NULL, ARG_KEYMAP },
747 { "timezone", required_argument, NULL, ARG_TIMEZONE },
748 { "hostname", required_argument, NULL, ARG_HOSTNAME },
749 { "machine-id", required_argument, NULL, ARG_MACHINE_ID },
750 { "root-password", required_argument, NULL, ARG_ROOT_PASSWORD },
751 { "root-password-file", required_argument, NULL, ARG_ROOT_PASSWORD_FILE },
752 { "prompt", no_argument, NULL, ARG_PROMPT },
753 { "prompt-locale", no_argument, NULL, ARG_PROMPT_LOCALE },
754 { "prompt-keymap", no_argument, NULL, ARG_PROMPT_KEYMAP },
755 { "prompt-timezone", no_argument, NULL, ARG_PROMPT_TIMEZONE },
756 { "prompt-hostname", no_argument, NULL, ARG_PROMPT_HOSTNAME },
757 { "prompt-root-password", no_argument, NULL, ARG_PROMPT_ROOT_PASSWORD },
758 { "copy", no_argument, NULL, ARG_COPY },
759 { "copy-locale", no_argument, NULL, ARG_COPY_LOCALE },
760 { "copy-keymap", no_argument, NULL, ARG_COPY_KEYMAP },
761 { "copy-timezone", no_argument, NULL, ARG_COPY_TIMEZONE },
762 { "copy-root-password", no_argument, NULL, ARG_COPY_ROOT_PASSWORD },
763 { "setup-machine-id", no_argument, NULL, ARG_SETUP_MACHINE_ID },
764 {}
765 };
766
767 int r, c;
768
769 assert(argc >= 0);
770 assert(argv);
771
772 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
773
774 switch (c) {
775
776 case 'h':
777 return help();
778
779 case ARG_VERSION:
780 return version();
781
782 case ARG_ROOT:
783 r = parse_path_argument_and_warn(optarg, true, &arg_root);
784 if (r < 0)
785 return r;
786 break;
787
788 case ARG_LOCALE:
789 if (!locale_is_valid(optarg))
790 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
791 "Locale %s is not valid.", optarg);
792
793 r = free_and_strdup(&arg_locale, optarg);
794 if (r < 0)
795 return log_oom();
796
797 break;
798
799 case ARG_LOCALE_MESSAGES:
800 if (!locale_is_valid(optarg))
801 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
802 "Locale %s is not valid.", optarg);
803
804 r = free_and_strdup(&arg_locale_messages, optarg);
805 if (r < 0)
806 return log_oom();
807
808 break;
809
810 case ARG_KEYMAP:
811 if (!keymap_is_valid(optarg))
812 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
813 "Keymap %s is not valid.", optarg);
814
815 r = free_and_strdup(&arg_keymap, optarg);
816 if (r < 0)
817 return log_oom();
818
819 break;
820
821 case ARG_TIMEZONE:
822 if (!timezone_is_valid(optarg, LOG_ERR))
823 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
824 "Timezone %s is not valid.", optarg);
825
826 r = free_and_strdup(&arg_timezone, optarg);
827 if (r < 0)
828 return log_oom();
829
830 break;
831
832 case ARG_ROOT_PASSWORD:
833 r = free_and_strdup(&arg_root_password, optarg);
834 if (r < 0)
835 return log_oom();
836 break;
837
838 case ARG_ROOT_PASSWORD_FILE:
839 arg_root_password = mfree(arg_root_password);
840
841 r = read_one_line_file(optarg, &arg_root_password);
842 if (r < 0)
843 return log_error_errno(r, "Failed to read %s: %m", optarg);
844
845 break;
846
847 case ARG_HOSTNAME:
848 if (!hostname_is_valid(optarg, true))
849 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
850 "Host name %s is not valid.", optarg);
851
852 hostname_cleanup(optarg);
853 r = free_and_strdup(&arg_hostname, optarg);
854 if (r < 0)
855 return log_oom();
856
857 break;
858
859 case ARG_MACHINE_ID:
860 if (sd_id128_from_string(optarg, &arg_machine_id) < 0)
861 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
862 "Failed to parse machine id %s.", optarg);
863
864 break;
865
866 case ARG_PROMPT:
867 arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname = arg_prompt_root_password = true;
868 break;
869
870 case ARG_PROMPT_LOCALE:
871 arg_prompt_locale = true;
872 break;
873
874 case ARG_PROMPT_KEYMAP:
875 arg_prompt_keymap = true;
876 break;
877
878 case ARG_PROMPT_TIMEZONE:
879 arg_prompt_timezone = true;
880 break;
881
882 case ARG_PROMPT_HOSTNAME:
883 arg_prompt_hostname = true;
884 break;
885
886 case ARG_PROMPT_ROOT_PASSWORD:
887 arg_prompt_root_password = true;
888 break;
889
890 case ARG_COPY:
891 arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password = true;
892 break;
893
894 case ARG_COPY_LOCALE:
895 arg_copy_locale = true;
896 break;
897
898 case ARG_COPY_KEYMAP:
899 arg_copy_keymap = true;
900 break;
901
902 case ARG_COPY_TIMEZONE:
903 arg_copy_timezone = true;
904 break;
905
906 case ARG_COPY_ROOT_PASSWORD:
907 arg_copy_root_password = true;
908 break;
909
910 case ARG_SETUP_MACHINE_ID:
911
912 r = sd_id128_randomize(&arg_machine_id);
913 if (r < 0)
914 return log_error_errno(r, "Failed to generate randomized machine ID: %m");
915
916 break;
917
918 case '?':
919 return -EINVAL;
920
921 default:
922 assert_not_reached("Unhandled option");
923 }
924
925 return 1;
926 }
927
928 static int run(int argc, char *argv[]) {
929 bool enabled;
930 int r;
931
932 r = parse_argv(argc, argv);
933 if (r <= 0)
934 return r;
935
936 log_setup_service();
937
938 umask(0022);
939
940 r = proc_cmdline_get_bool("systemd.firstboot", &enabled);
941 if (r < 0)
942 return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
943 if (r > 0 && !enabled)
944 return 0; /* disabled */
945
946 r = process_locale();
947 if (r < 0)
948 return r;
949
950 r = process_keymap();
951 if (r < 0)
952 return r;
953
954 r = process_timezone();
955 if (r < 0)
956 return r;
957
958 r = process_hostname();
959 if (r < 0)
960 return r;
961
962 r = process_machine_id();
963 if (r < 0)
964 return r;
965
966 r = process_root_password();
967 if (r < 0)
968 return r;
969
970 return 0;
971 }
972
973 DEFINE_MAIN_FUNCTION(run);