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