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