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