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