]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/firstboot/firstboot.c
grypt-util: drop two emacs modelines
[thirdparty/systemd.git] / src / firstboot / firstboot.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2014 Lennart Poettering
4 ***/
5
6 #include <fcntl.h>
7 #include <getopt.h>
8 #include <unistd.h>
9
10 #ifdef HAVE_CRYPT_H
11 /* libxcrypt is a replacement for glibc's libcrypt, and libcrypt might be
12 * removed from glibc at some point. As part of the removal, defines for
13 * crypt(3) are dropped from unistd.h, and we must include crypt.h instead.
14 *
15 * Newer versions of glibc (v2.0+) already ship crypt.h with a definition
16 * of crypt(3) as well, so we simply include it if it is present. MariaDB,
17 * MySQL, PostgreSQL, Perl and some other wide-spread packages do it the
18 * same way since ages without any problems.
19 */
20 # include <crypt.h>
21 #endif
22
23 #include "sd-id128.h"
24
25 #include "alloc-util.h"
26 #include "ask-password-api.h"
27 #include "copy.h"
28 #include "fd-util.h"
29 #include "fileio.h"
30 #include "fs-util.h"
31 #include "hostname-util.h"
32 #include "locale-util.h"
33 #include "mkdir.h"
34 #include "os-util.h"
35 #include "parse-util.h"
36 #include "path-util.h"
37 #include "proc-cmdline.h"
38 #include "random-util.h"
39 #include "string-util.h"
40 #include "strv.h"
41 #include "terminal-util.h"
42 #include "time-util.h"
43 #include "umask-util.h"
44 #include "user-util.h"
45
46 static char *arg_root = NULL;
47 static char *arg_locale = NULL; /* $LANG */
48 static char *arg_keymap = NULL;
49 static char *arg_locale_messages = NULL; /* $LC_MESSAGES */
50 static char *arg_timezone = NULL;
51 static char *arg_hostname = NULL;
52 static sd_id128_t arg_machine_id = {};
53 static char *arg_root_password = NULL;
54 static bool arg_prompt_locale = false;
55 static bool arg_prompt_keymap = false;
56 static bool arg_prompt_timezone = false;
57 static bool arg_prompt_hostname = false;
58 static bool arg_prompt_root_password = false;
59 static bool arg_copy_locale = false;
60 static bool arg_copy_keymap = false;
61 static bool arg_copy_timezone = false;
62 static bool arg_copy_root_password = false;
63
64 static bool press_any_key(void) {
65 char k = 0;
66 bool need_nl = true;
67
68 printf("-- Press any key to proceed --");
69 fflush(stdout);
70
71 (void) read_one_char(stdin, &k, USEC_INFINITY, &need_nl);
72
73 if (need_nl)
74 putchar('\n');
75
76 return k != 'q';
77 }
78
79 static void print_welcome(void) {
80 _cleanup_free_ char *pretty_name = NULL;
81 static bool done = false;
82 int r;
83
84 if (done)
85 return;
86
87 r = parse_os_release(arg_root, "PRETTY_NAME", &pretty_name, NULL);
88 if (r < 0)
89 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
90 "Failed to read os-release file, ignoring: %m");
91
92 printf("\nWelcome to your new installation of %s!\nPlease configure a few basic system settings:\n\n",
93 isempty(pretty_name) ? "Linux" : pretty_name);
94
95 press_any_key();
96
97 done = true;
98 }
99
100 static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) {
101 unsigned break_lines, break_modulo;
102 size_t n, per_column, i, j;
103
104 assert(n_columns > 0);
105
106 n = strv_length(x);
107 per_column = DIV_ROUND_UP(n, n_columns);
108
109 break_lines = lines();
110 if (break_lines > 2)
111 break_lines--;
112
113 /* The first page gets two extra lines, since we want to show
114 * a title */
115 break_modulo = break_lines;
116 if (break_modulo > 3)
117 break_modulo -= 3;
118
119 for (i = 0; i < per_column; i++) {
120
121 for (j = 0; j < n_columns; j ++) {
122 _cleanup_free_ char *e = NULL;
123
124 if (j * per_column + i >= n)
125 break;
126
127 e = ellipsize(x[j * per_column + i], width, percentage);
128 if (!e)
129 return log_oom();
130
131 printf("%4zu) %-*s", j * per_column + i + 1, width, e);
132 }
133
134 putchar('\n');
135
136 /* on the first screen we reserve 2 extra lines for the title */
137 if (i % break_lines == break_modulo) {
138 if (!press_any_key())
139 return 0;
140 }
141 }
142
143 return 0;
144 }
145
146 static int prompt_loop(const char *text, char **l, bool (*is_valid)(const char *name), char **ret) {
147 int r;
148
149 assert(text);
150 assert(is_valid);
151 assert(ret);
152
153 for (;;) {
154 _cleanup_free_ char *p = NULL;
155 unsigned u;
156
157 r = ask_string(&p, "%s %s (empty to skip): ", special_glyph(TRIANGULAR_BULLET), text);
158 if (r < 0)
159 return log_error_errno(r, "Failed to query user: %m");
160
161 if (isempty(p)) {
162 log_warning("No data entered, skipping.");
163 return 0;
164 }
165
166 r = safe_atou(p, &u);
167 if (r >= 0) {
168 char *c;
169
170 if (u <= 0 || u > strv_length(l)) {
171 log_error("Specified entry number out of range.");
172 continue;
173 }
174
175 log_info("Selected '%s'.", l[u-1]);
176
177 c = strdup(l[u-1]);
178 if (!c)
179 return log_oom();
180
181 free(*ret);
182 *ret = c;
183 return 0;
184 }
185
186 if (!is_valid(p)) {
187 log_error("Entered data invalid.");
188 continue;
189 }
190
191 free(*ret);
192 *ret = p;
193 p = 0;
194 return 0;
195 }
196 }
197
198 static int prompt_locale(void) {
199 _cleanup_strv_free_ char **locales = NULL;
200 int r;
201
202 if (arg_locale || arg_locale_messages)
203 return 0;
204
205 if (!arg_prompt_locale)
206 return 0;
207
208 r = get_locales(&locales);
209 if (r < 0)
210 return log_error_errno(r, "Cannot query locales list: %m");
211
212 print_welcome();
213
214 printf("\nAvailable Locales:\n\n");
215 r = show_menu(locales, 3, 22, 60);
216 if (r < 0)
217 return r;
218
219 putchar('\n');
220
221 r = prompt_loop("Please enter system locale name or number", locales, locale_is_valid, &arg_locale);
222 if (r < 0)
223 return r;
224
225 if (isempty(arg_locale))
226 return 0;
227
228 r = prompt_loop("Please enter system message locale name or number", locales, locale_is_valid, &arg_locale_messages);
229 if (r < 0)
230 return r;
231
232 return 0;
233 }
234
235 static int process_locale(void) {
236 const char *etc_localeconf;
237 char* locales[3];
238 unsigned i = 0;
239 int r;
240
241 etc_localeconf = prefix_roota(arg_root, "/etc/locale.conf");
242 if (laccess(etc_localeconf, F_OK) >= 0)
243 return 0;
244
245 if (arg_copy_locale && arg_root) {
246
247 mkdir_parents(etc_localeconf, 0755);
248 r = copy_file("/etc/locale.conf", etc_localeconf, 0, 0644, 0, COPY_REFLINK);
249 if (r != -ENOENT) {
250 if (r < 0)
251 return log_error_errno(r, "Failed to copy %s: %m", etc_localeconf);
252
253 log_info("%s copied.", etc_localeconf);
254 return 0;
255 }
256 }
257
258 r = prompt_locale();
259 if (r < 0)
260 return r;
261
262 if (!isempty(arg_locale))
263 locales[i++] = strjoina("LANG=", arg_locale);
264 if (!isempty(arg_locale_messages) && !streq(arg_locale_messages, arg_locale))
265 locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages);
266
267 if (i == 0)
268 return 0;
269
270 locales[i] = NULL;
271
272 mkdir_parents(etc_localeconf, 0755);
273 r = write_env_file(etc_localeconf, locales);
274 if (r < 0)
275 return log_error_errno(r, "Failed to write %s: %m", etc_localeconf);
276
277 log_info("%s written.", etc_localeconf);
278 return 0;
279 }
280
281 static int prompt_keymap(void) {
282 _cleanup_strv_free_ char **kmaps = NULL;
283 int r;
284
285 if (arg_keymap)
286 return 0;
287
288 if (!arg_prompt_keymap)
289 return 0;
290
291 r = get_keymaps(&kmaps);
292 if (r == -ENOENT) /* no keymaps installed */
293 return r;
294 if (r < 0)
295 return log_error_errno(r, "Failed to read keymaps: %m");
296
297 print_welcome();
298
299 printf("\nAvailable keymaps:\n\n");
300 r = show_menu(kmaps, 3, 22, 60);
301 if (r < 0)
302 return r;
303
304 putchar('\n');
305
306 return prompt_loop("Please enter system keymap name or number",
307 kmaps, keymap_is_valid, &arg_keymap);
308 }
309
310 static int process_keymap(void) {
311 const char *etc_vconsoleconf;
312 char **keymap;
313 int r;
314
315 etc_vconsoleconf = prefix_roota(arg_root, "/etc/vconsole.conf");
316 if (laccess(etc_vconsoleconf, F_OK) >= 0)
317 return 0;
318
319 if (arg_copy_keymap && arg_root) {
320
321 mkdir_parents(etc_vconsoleconf, 0755);
322 r = copy_file("/etc/vconsole.conf", etc_vconsoleconf, 0, 0644, 0, COPY_REFLINK);
323 if (r != -ENOENT) {
324 if (r < 0)
325 return log_error_errno(r, "Failed to copy %s: %m", etc_vconsoleconf);
326
327 log_info("%s copied.", etc_vconsoleconf);
328 return 0;
329 }
330 }
331
332 r = prompt_keymap();
333 if (r == -ENOENT)
334 return 0; /* don't fail if no keymaps are installed */
335 if (r < 0)
336 return r;
337
338 if (isempty(arg_keymap))
339 return 0;
340
341 keymap = STRV_MAKE(strjoina("KEYMAP=", arg_keymap));
342
343 r = mkdir_parents(etc_vconsoleconf, 0755);
344 if (r < 0)
345 return log_error_errno(r, "Failed to create the parent directory of %s: %m", etc_vconsoleconf);
346
347 r = write_env_file(etc_vconsoleconf, keymap);
348 if (r < 0)
349 return log_error_errno(r, "Failed to write %s: %m", etc_vconsoleconf);
350
351 log_info("%s written.", etc_vconsoleconf);
352 return 0;
353 }
354
355 static bool timezone_is_valid_log_error(const char *name) {
356 return timezone_is_valid(name, LOG_ERR);
357 }
358
359 static int prompt_timezone(void) {
360 _cleanup_strv_free_ char **zones = NULL;
361 int r;
362
363 if (arg_timezone)
364 return 0;
365
366 if (!arg_prompt_timezone)
367 return 0;
368
369 r = get_timezones(&zones);
370 if (r < 0)
371 return log_error_errno(r, "Cannot query timezone list: %m");
372
373 print_welcome();
374
375 printf("\nAvailable Time Zones:\n\n");
376 r = show_menu(zones, 3, 22, 30);
377 if (r < 0)
378 return r;
379
380 putchar('\n');
381
382 r = prompt_loop("Please enter timezone name or number", zones, timezone_is_valid_log_error, &arg_timezone);
383 if (r < 0)
384 return r;
385
386 return 0;
387 }
388
389 static int process_timezone(void) {
390 const char *etc_localtime, *e;
391 int r;
392
393 etc_localtime = prefix_roota(arg_root, "/etc/localtime");
394 if (laccess(etc_localtime, F_OK) >= 0)
395 return 0;
396
397 if (arg_copy_timezone && arg_root) {
398 _cleanup_free_ char *p = NULL;
399
400 r = readlink_malloc("/etc/localtime", &p);
401 if (r != -ENOENT) {
402 if (r < 0)
403 return log_error_errno(r, "Failed to read host timezone: %m");
404
405 mkdir_parents(etc_localtime, 0755);
406 if (symlink(p, etc_localtime) < 0)
407 return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime);
408
409 log_info("%s copied.", etc_localtime);
410 return 0;
411 }
412 }
413
414 r = prompt_timezone();
415 if (r < 0)
416 return r;
417
418 if (isempty(arg_timezone))
419 return 0;
420
421 e = strjoina("../usr/share/zoneinfo/", arg_timezone);
422
423 mkdir_parents(etc_localtime, 0755);
424 if (symlink(e, etc_localtime) < 0)
425 return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime);
426
427 log_info("%s written", etc_localtime);
428 return 0;
429 }
430
431 static int prompt_hostname(void) {
432 int r;
433
434 if (arg_hostname)
435 return 0;
436
437 if (!arg_prompt_hostname)
438 return 0;
439
440 print_welcome();
441 putchar('\n');
442
443 for (;;) {
444 _cleanup_free_ char *h = NULL;
445
446 r = ask_string(&h, "%s Please enter hostname for new system (empty to skip): ", special_glyph(TRIANGULAR_BULLET));
447 if (r < 0)
448 return log_error_errno(r, "Failed to query hostname: %m");
449
450 if (isempty(h)) {
451 log_warning("No hostname entered, skipping.");
452 break;
453 }
454
455 if (!hostname_is_valid(h, true)) {
456 log_error("Specified hostname invalid.");
457 continue;
458 }
459
460 /* Get rid of the trailing dot that we allow, but don't want to see */
461 arg_hostname = hostname_cleanup(h);
462 h = NULL;
463 break;
464 }
465
466 return 0;
467 }
468
469 static int process_hostname(void) {
470 const char *etc_hostname;
471 int r;
472
473 etc_hostname = prefix_roota(arg_root, "/etc/hostname");
474 if (laccess(etc_hostname, F_OK) >= 0)
475 return 0;
476
477 r = prompt_hostname();
478 if (r < 0)
479 return r;
480
481 if (isempty(arg_hostname))
482 return 0;
483
484 mkdir_parents(etc_hostname, 0755);
485 r = write_string_file(etc_hostname, arg_hostname,
486 WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC);
487 if (r < 0)
488 return log_error_errno(r, "Failed to write %s: %m", etc_hostname);
489
490 log_info("%s written.", etc_hostname);
491 return 0;
492 }
493
494 static int process_machine_id(void) {
495 const char *etc_machine_id;
496 char id[SD_ID128_STRING_MAX];
497 int r;
498
499 etc_machine_id = prefix_roota(arg_root, "/etc/machine-id");
500 if (laccess(etc_machine_id, F_OK) >= 0)
501 return 0;
502
503 if (sd_id128_is_null(arg_machine_id))
504 return 0;
505
506 mkdir_parents(etc_machine_id, 0755);
507 r = write_string_file(etc_machine_id, sd_id128_to_string(arg_machine_id, id),
508 WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC);
509 if (r < 0)
510 return log_error_errno(r, "Failed to write machine id: %m");
511
512 log_info("%s written.", etc_machine_id);
513 return 0;
514 }
515
516 static int prompt_root_password(void) {
517 const char *msg1, *msg2, *etc_shadow;
518 int r;
519
520 if (arg_root_password)
521 return 0;
522
523 if (!arg_prompt_root_password)
524 return 0;
525
526 etc_shadow = prefix_roota(arg_root, "/etc/shadow");
527 if (laccess(etc_shadow, F_OK) >= 0)
528 return 0;
529
530 print_welcome();
531 putchar('\n');
532
533 msg1 = strjoina(special_glyph(TRIANGULAR_BULLET), " Please enter a new root password (empty to skip): ");
534 msg2 = strjoina(special_glyph(TRIANGULAR_BULLET), " Please enter new root password again: ");
535
536 for (;;) {
537 _cleanup_string_free_erase_ char *a = NULL, *b = NULL;
538
539 r = ask_password_tty(-1, msg1, NULL, 0, 0, NULL, &a);
540 if (r < 0)
541 return log_error_errno(r, "Failed to query root password: %m");
542
543 if (isempty(a)) {
544 log_warning("No password entered, skipping.");
545 break;
546 }
547
548 r = ask_password_tty(-1, msg2, NULL, 0, 0, NULL, &b);
549 if (r < 0)
550 return log_error_errno(r, "Failed to query root password: %m");
551
552 if (!streq(a, b)) {
553 log_error("Entered passwords did not match, please try again.");
554 continue;
555 }
556
557 arg_root_password = TAKE_PTR(a);
558 break;
559 }
560
561 return 0;
562 }
563
564 static int write_root_shadow(const char *path, const struct spwd *p) {
565 _cleanup_fclose_ FILE *f = NULL;
566 int r;
567
568 assert(path);
569 assert(p);
570
571 RUN_WITH_UMASK(0777)
572 f = fopen(path, "wex");
573 if (!f)
574 return -errno;
575
576 r = putspent_sane(p, f);
577 if (r < 0)
578 return r;
579
580 return fflush_sync_and_check(f);
581 }
582
583 static int process_root_password(void) {
584
585 static const char table[] =
586 "abcdefghijklmnopqrstuvwxyz"
587 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
588 "0123456789"
589 "./";
590
591 struct spwd item = {
592 .sp_namp = (char*) "root",
593 .sp_min = -1,
594 .sp_max = -1,
595 .sp_warn = -1,
596 .sp_inact = -1,
597 .sp_expire = -1,
598 .sp_flag = (unsigned long) -1, /* this appears to be what everybody does ... */
599 };
600
601 _cleanup_close_ int lock = -1;
602 char salt[3+16+1+1];
603 uint8_t raw[16];
604 unsigned i;
605 char *j;
606
607 const char *etc_shadow;
608 int r;
609
610 etc_shadow = prefix_roota(arg_root, "/etc/shadow");
611 if (laccess(etc_shadow, F_OK) >= 0)
612 return 0;
613
614 mkdir_parents(etc_shadow, 0755);
615
616 lock = take_etc_passwd_lock(arg_root);
617 if (lock < 0)
618 return log_error_errno(lock, "Failed to take a lock: %m");
619
620 if (arg_copy_root_password && arg_root) {
621 struct spwd *p;
622
623 errno = 0;
624 p = getspnam("root");
625 if (p || errno != ENOENT) {
626 if (!p) {
627 if (!errno)
628 errno = EIO;
629
630 return log_error_errno(errno, "Failed to find shadow entry for root: %m");
631 }
632
633 r = write_root_shadow(etc_shadow, p);
634 if (r < 0)
635 return log_error_errno(r, "Failed to write %s: %m", etc_shadow);
636
637 log_info("%s copied.", etc_shadow);
638 return 0;
639 }
640 }
641
642 r = prompt_root_password();
643 if (r < 0)
644 return r;
645
646 if (!arg_root_password)
647 return 0;
648
649 r = acquire_random_bytes(raw, 16, true);
650 if (r < 0)
651 return log_error_errno(r, "Failed to get salt: %m");
652
653 /* We only bother with SHA512 hashed passwords, the rest is legacy, and we don't do legacy. */
654 assert_cc(sizeof(table) == 64 + 1);
655 j = stpcpy(salt, "$6$");
656 for (i = 0; i < 16; i++)
657 j[i] = table[raw[i] & 63];
658 j[i++] = '$';
659 j[i] = 0;
660
661 errno = 0;
662 item.sp_pwdp = crypt(arg_root_password, salt);
663 if (!item.sp_pwdp) {
664 if (!errno)
665 errno = EINVAL;
666
667 return log_error_errno(errno, "Failed to encrypt password: %m");
668 }
669
670 item.sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
671
672 r = write_root_shadow(etc_shadow, &item);
673 if (r < 0)
674 return log_error_errno(r, "Failed to write %s: %m", etc_shadow);
675
676 log_info("%s written.", etc_shadow);
677 return 0;
678 }
679
680 static void help(void) {
681 printf("%s [OPTIONS...]\n\n"
682 "Configures basic settings of the system.\n\n"
683 " -h --help Show this help\n"
684 " --version Show package version\n"
685 " --root=PATH Operate on an alternate filesystem root\n"
686 " --locale=LOCALE Set primary locale (LANG=)\n"
687 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
688 " --keymap=KEYMAP Set keymap\n"
689 " --timezone=TIMEZONE Set timezone\n"
690 " --hostname=NAME Set host name\n"
691 " --machine-ID=ID Set machine ID\n"
692 " --root-password=PASSWORD Set root password\n"
693 " --root-password-file=FILE Set root password from file\n"
694 " --prompt-locale Prompt the user for locale settings\n"
695 " --prompt-keymap Prompt the user for keymap settings\n"
696 " --prompt-timezone Prompt the user for timezone\n"
697 " --prompt-hostname Prompt the user for hostname\n"
698 " --prompt-root-password Prompt the user for root password\n"
699 " --prompt Prompt for all of the above\n"
700 " --copy-locale Copy locale from host\n"
701 " --copy-keymap Copy keymap from host\n"
702 " --copy-timezone Copy timezone from host\n"
703 " --copy-root-password Copy root password from host\n"
704 " --copy Copy locale, keymap, timezone, root password\n"
705 " --setup-machine-id Generate a new random machine ID\n"
706 , program_invocation_short_name);
707 }
708
709 static int parse_argv(int argc, char *argv[]) {
710
711 enum {
712 ARG_VERSION = 0x100,
713 ARG_ROOT,
714 ARG_LOCALE,
715 ARG_LOCALE_MESSAGES,
716 ARG_KEYMAP,
717 ARG_TIMEZONE,
718 ARG_HOSTNAME,
719 ARG_MACHINE_ID,
720 ARG_ROOT_PASSWORD,
721 ARG_ROOT_PASSWORD_FILE,
722 ARG_PROMPT,
723 ARG_PROMPT_LOCALE,
724 ARG_PROMPT_KEYMAP,
725 ARG_PROMPT_TIMEZONE,
726 ARG_PROMPT_HOSTNAME,
727 ARG_PROMPT_ROOT_PASSWORD,
728 ARG_COPY,
729 ARG_COPY_LOCALE,
730 ARG_COPY_KEYMAP,
731 ARG_COPY_TIMEZONE,
732 ARG_COPY_ROOT_PASSWORD,
733 ARG_SETUP_MACHINE_ID,
734 };
735
736 static const struct option options[] = {
737 { "help", no_argument, NULL, 'h' },
738 { "version", no_argument, NULL, ARG_VERSION },
739 { "root", required_argument, NULL, ARG_ROOT },
740 { "locale", required_argument, NULL, ARG_LOCALE },
741 { "locale-messages", required_argument, NULL, ARG_LOCALE_MESSAGES },
742 { "keymap", required_argument, NULL, ARG_KEYMAP },
743 { "timezone", required_argument, NULL, ARG_TIMEZONE },
744 { "hostname", required_argument, NULL, ARG_HOSTNAME },
745 { "machine-id", required_argument, NULL, ARG_MACHINE_ID },
746 { "root-password", required_argument, NULL, ARG_ROOT_PASSWORD },
747 { "root-password-file", required_argument, NULL, ARG_ROOT_PASSWORD_FILE },
748 { "prompt", no_argument, NULL, ARG_PROMPT },
749 { "prompt-locale", no_argument, NULL, ARG_PROMPT_LOCALE },
750 { "prompt-keymap", no_argument, NULL, ARG_PROMPT_KEYMAP },
751 { "prompt-timezone", no_argument, NULL, ARG_PROMPT_TIMEZONE },
752 { "prompt-hostname", no_argument, NULL, ARG_PROMPT_HOSTNAME },
753 { "prompt-root-password", no_argument, NULL, ARG_PROMPT_ROOT_PASSWORD },
754 { "copy", no_argument, NULL, ARG_COPY },
755 { "copy-locale", no_argument, NULL, ARG_COPY_LOCALE },
756 { "copy-keymap", no_argument, NULL, ARG_COPY_KEYMAP },
757 { "copy-timezone", no_argument, NULL, ARG_COPY_TIMEZONE },
758 { "copy-root-password", no_argument, NULL, ARG_COPY_ROOT_PASSWORD },
759 { "setup-machine-id", no_argument, NULL, ARG_SETUP_MACHINE_ID },
760 {}
761 };
762
763 int r, c;
764
765 assert(argc >= 0);
766 assert(argv);
767
768 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
769
770 switch (c) {
771
772 case 'h':
773 help();
774 return 0;
775
776 case ARG_VERSION:
777 return version();
778
779 case ARG_ROOT:
780 r = parse_path_argument_and_warn(optarg, true, &arg_root);
781 if (r < 0)
782 return r;
783 break;
784
785 case ARG_LOCALE:
786 if (!locale_is_valid(optarg)) {
787 log_error("Locale %s is not valid.", optarg);
788 return -EINVAL;
789 }
790
791 r = free_and_strdup(&arg_locale, optarg);
792 if (r < 0)
793 return log_oom();
794
795 break;
796
797 case ARG_LOCALE_MESSAGES:
798 if (!locale_is_valid(optarg)) {
799 log_error("Locale %s is not valid.", optarg);
800 return -EINVAL;
801 }
802
803 r = free_and_strdup(&arg_locale_messages, optarg);
804 if (r < 0)
805 return log_oom();
806
807 break;
808
809 case ARG_KEYMAP:
810 if (!keymap_is_valid(optarg)) {
811 log_error("Keymap %s is not valid.", optarg);
812 return -EINVAL;
813 }
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 log_error("Timezone %s is not valid.", optarg);
824 return -EINVAL;
825 }
826
827 r = free_and_strdup(&arg_timezone, optarg);
828 if (r < 0)
829 return log_oom();
830
831 break;
832
833 case ARG_ROOT_PASSWORD:
834 r = free_and_strdup(&arg_root_password, optarg);
835 if (r < 0)
836 return log_oom();
837 break;
838
839 case ARG_ROOT_PASSWORD_FILE:
840 arg_root_password = mfree(arg_root_password);
841
842 r = read_one_line_file(optarg, &arg_root_password);
843 if (r < 0)
844 return log_error_errno(r, "Failed to read %s: %m", optarg);
845
846 break;
847
848 case ARG_HOSTNAME:
849 if (!hostname_is_valid(optarg, true)) {
850 log_error("Host name %s is not valid.", optarg);
851 return -EINVAL;
852 }
853
854 hostname_cleanup(optarg);
855 r = free_and_strdup(&arg_hostname, optarg);
856 if (r < 0)
857 return log_oom();
858
859 break;
860
861 case ARG_MACHINE_ID:
862 if (sd_id128_from_string(optarg, &arg_machine_id) < 0) {
863 log_error("Failed to parse machine id %s.", optarg);
864 return -EINVAL;
865 }
866
867 break;
868
869 case ARG_PROMPT:
870 arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname = arg_prompt_root_password = true;
871 break;
872
873 case ARG_PROMPT_LOCALE:
874 arg_prompt_locale = true;
875 break;
876
877 case ARG_PROMPT_KEYMAP:
878 arg_prompt_keymap = true;
879 break;
880
881 case ARG_PROMPT_TIMEZONE:
882 arg_prompt_timezone = true;
883 break;
884
885 case ARG_PROMPT_HOSTNAME:
886 arg_prompt_hostname = true;
887 break;
888
889 case ARG_PROMPT_ROOT_PASSWORD:
890 arg_prompt_root_password = true;
891 break;
892
893 case ARG_COPY:
894 arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password = true;
895 break;
896
897 case ARG_COPY_LOCALE:
898 arg_copy_locale = true;
899 break;
900
901 case ARG_COPY_KEYMAP:
902 arg_copy_keymap = true;
903 break;
904
905 case ARG_COPY_TIMEZONE:
906 arg_copy_timezone = true;
907 break;
908
909 case ARG_COPY_ROOT_PASSWORD:
910 arg_copy_root_password = true;
911 break;
912
913 case ARG_SETUP_MACHINE_ID:
914
915 r = sd_id128_randomize(&arg_machine_id);
916 if (r < 0)
917 return log_error_errno(r, "Failed to generate randomized machine ID: %m");
918
919 break;
920
921 case '?':
922 return -EINVAL;
923
924 default:
925 assert_not_reached("Unhandled option");
926 }
927
928 return 1;
929 }
930
931 int main(int argc, char *argv[]) {
932 bool enabled;
933 int r;
934
935 r = parse_argv(argc, argv);
936 if (r <= 0)
937 goto finish;
938
939 log_set_target(LOG_TARGET_AUTO);
940 log_parse_environment();
941 log_open();
942
943 umask(0022);
944
945 r = proc_cmdline_get_bool("systemd.firstboot", &enabled);
946 if (r < 0) {
947 log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring.");
948 goto finish;
949 }
950 if (r > 0 && !enabled) {
951 r = 0; /* disabled */
952 goto finish;
953 }
954
955 r = process_locale();
956 if (r < 0)
957 goto finish;
958
959 r = process_keymap();
960 if (r < 0)
961 goto finish;
962
963 r = process_timezone();
964 if (r < 0)
965 goto finish;
966
967 r = process_hostname();
968 if (r < 0)
969 goto finish;
970
971 r = process_machine_id();
972 if (r < 0)
973 goto finish;
974
975 r = process_root_password();
976 if (r < 0)
977 goto finish;
978
979 finish:
980 free(arg_root);
981 free(arg_locale);
982 free(arg_locale_messages);
983 free(arg_keymap);
984 free(arg_timezone);
985 free(arg_hostname);
986 string_erase(arg_root_password);
987 free(arg_root_password);
988
989 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
990 }