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