Debian Codegraph @

src/firstboot/firstboot.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 #include "sd-bus.h"
7 #include "sd-id128.h"
8 #include "sd-varlink.h"
9
10 #include "alloc-util.h"
11 #include "ansi-color.h"
12 #include "ask-password-api.h"
13 #include "build.h"
14 #include "bus-error.h"
15 #include "bus-locator.h"
16 #include "bus-unit-util.h"
17 #include "bus-util.h"
18 #include "bus-wait-for-jobs.h"
19 #include "chase.h"
20 #include "copy.h"
21 #include "creds-util.h"
22 #include "dissect-image.h"
23 #include "env-file.h"
24 #include "errno-util.h"
25 #include "fd-util.h"
26 #include "fileio.h"
27 #include "format-table.h"
28 #include "fs-util.h"
29 #include "glyph-util.h"
30 #include "help-util.h"
31 #include "hostname-util.h"
32 #include "image-policy.h"
33 #include "kbd-util.h"
34 #include "label-util.h"
35 #include "libcrypt-util.h"
36 #include "locale-setup.h"
37 #include "locale-util.h"
38 #include "lock-util.h"
39 #include "loop-util.h"
40 #include "main-func.h"
41 #include "memory-util.h"
42 #include "mount-util.h"
43 #include "options.h"
44 #include "os-util.h"
45 #include "parse-argument.h"
46 #include "password-quality-util.h"
47 #include "path-util.h"
48 #include "plymouth-util.h"
49 #include "proc-cmdline.h"
50 #include "prompt-util.h"
51 #include "runtime-scope.h"
52 #include "smack-util.h"
53 #include "stat-util.h"
54 #include "string-util.h"
55 #include "strv.h"
56 #include "terminal-util.h"
57 #include "time-util.h"
58 #include "tmpfile-util.h"
59 #include "user-util.h"
60 #include "vconsole-util.h"
61
62 static char *arg_root = NULL;
63 static char *arg_image = NULL;
64 static char *arg_locale = NULL; /* $LANG */
65 static char *arg_locale_messages = NULL; /* $LC_MESSAGES */
66 static char *arg_keymap = NULL;
67 static char *arg_timezone = NULL;
68 static char *arg_hostname = NULL;
69 static sd_id128_t arg_machine_id = {};
70 static char **arg_machine_tags = NULL;
71 static char *arg_root_password = NULL;
72 static char *arg_root_shell = NULL;
73 static char *arg_kernel_cmdline = NULL;
74 static bool arg_prompt_locale = false;
75 static bool arg_prompt_keymap = false;
76 static bool arg_prompt_keymap_auto = false;
77 static bool arg_prompt_timezone = false;
78 static bool arg_prompt_hostname = false;
79 static bool arg_prompt_root_password = false;
80 static bool arg_prompt_root_shell = false;
81 static bool arg_copy_locale = false;
82 static bool arg_copy_keymap = false;
83 static bool arg_copy_timezone = false;
84 static bool arg_copy_root_password = false;
85 static bool arg_copy_root_shell = false;
86 static bool arg_force = false;
87 static bool arg_delete_root_password = false;
88 static bool arg_root_password_is_hashed = false;
89 static bool arg_welcome = true;
90 static bool arg_reset = false;
91 static ImagePolicy *arg_image_policy = NULL;
92 static bool arg_chrome = true;
93 static bool arg_mute_console = false;
94
95 STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
96 STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
97 STATIC_DESTRUCTOR_REGISTER(arg_locale, freep);
98 STATIC_DESTRUCTOR_REGISTER(arg_locale_messages, freep);
99 STATIC_DESTRUCTOR_REGISTER(arg_keymap, freep);
100 STATIC_DESTRUCTOR_REGISTER(arg_timezone, freep);
101 STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep);
102 STATIC_DESTRUCTOR_REGISTER(arg_machine_tags, strv_freep);
103 STATIC_DESTRUCTOR_REGISTER(arg_root_password, erase_and_freep);
104 STATIC_DESTRUCTOR_REGISTER(arg_root_shell, freep);
105 STATIC_DESTRUCTOR_REGISTER(arg_kernel_cmdline, freep);
106 STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
107
108 static bool welcome_done = false;
109
110 static void print_welcome(int rfd, sd_varlink **mute_console_link) {
111 _cleanup_free_ char *pretty_name = NULL, *os_name = NULL, *ansi_color = NULL, *fancy_name = NULL;
112 const char *pn, *ac;
113 int r;
114
115 assert(rfd >= 0);
116 assert(mute_console_link);
117
118 /* Needs to be called before mute_console or it will garble the screen */
119 if (arg_welcome)
120 (void) plymouth_hide_splash();
121
122 if (!*mute_console_link && arg_mute_console)
123 (void) mute_console(mute_console_link);
124
125 if (!arg_welcome)
126 return;
127
128 if (welcome_done) {
129 putchar('\n'); /* Add some breathing room between multiple prompts */
130 return;
131 }
132
133 (void) terminal_reset_defensive_locked(STDOUT_FILENO, /* flags= */ 0);
134
135 if (arg_chrome)
136 chrome_show("Initial Setup", /* bottom= */ NULL);
137
138 r = parse_os_release_at(rfd,
139 "PRETTY_NAME", &pretty_name,
140 "FANCY_NAME", &fancy_name,
141 "NAME", &os_name,
142 "ANSI_COLOR", &ansi_color);
143 if (r < 0)
144 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
145 "Failed to read os-release file, ignoring: %m");
146
147 pn = os_release_pretty_name(pretty_name, os_name);
148 ac = isempty(ansi_color) ? "0" : ansi_color;
149
150 if (use_fancy_name(unescape_fancy_name(&fancy_name)))
151 printf(ANSI_HIGHLIGHT "Welcome to " ANSI_NORMAL "%s" ANSI_HIGHLIGHT "!" ANSI_NORMAL "\n", fancy_name);
152 else if (colors_enabled())
153 printf(ANSI_HIGHLIGHT "Welcome to " ANSI_NORMAL "\x1B[%sm%s" ANSI_HIGHLIGHT "!" ANSI_NORMAL "\n", ac, pn);
154 else
155 printf("Welcome to %s!\n", pn);
156
157 putchar('\n');
158 if (emoji_enabled()) {
159 fputs(glyph(GLYPH_SPARKLES), stdout);
160 putchar(' ');
161 }
162 printf("Please configure the system!\n\n");
163
164 welcome_done = true;
165 }
166
167 static int should_configure(int dir_fd, const char *filename) {
168 _cleanup_fclose_ FILE *passwd = NULL, *shadow = NULL;
169 int r;
170
171 assert(dir_fd >= 0);
172 assert(filename);
173
174 if (streq(filename, "passwd") && !arg_force)
175 /* We may need to do additional checks, so open the file. */
176 r = xfopenat(dir_fd, filename, "re", O_NOFOLLOW, &passwd);
177 else
178 r = RET_NERRNO(faccessat(dir_fd, filename, F_OK, AT_SYMLINK_NOFOLLOW));
179
180 if (r == -ENOENT)
181 return true; /* missing */
182 if (r < 0)
183 return log_error_errno(r, "Failed to access %s: %m", filename);
184 if (arg_force)
185 return true; /* exists, but if --force was given we should still configure the file. */
186
187 if (!passwd)
188 return false;
189
190 /* In case of /etc/passwd, do an additional check for the root password field.
191 * We first check that passwd redirects to shadow, and then we check shadow.
192 */
193 struct passwd *i;
194 while ((r = fgetpwent_sane(passwd, &i)) > 0) {
195 if (!streq(i->pw_name, "root"))
196 continue;
197
198 if (streq_ptr(i->pw_passwd, PASSWORD_SEE_SHADOW))
199 break;
200 log_debug("passwd: root account with non-shadow password found, treating root as configured");
201 return false;
202 }
203 if (r < 0)
204 return log_error_errno(r, "Failed to read %s: %m", filename);
205 if (r == 0) {
206 log_debug("No root account found in %s, assuming root is not configured.", filename);
207 return true;
208 }
209
210 r = xfopenat(dir_fd, "shadow", "re", O_NOFOLLOW, &shadow);
211 if (r == -ENOENT) {
212 log_debug("No shadow file found, assuming root is not configured.");
213 return true; /* missing */
214 }
215 if (r < 0)
216 return log_error_errno(r, "Failed to access shadow: %m");
217
218 struct spwd *j;
219 while ((r = fgetspent_sane(shadow, &j)) > 0) {
220 if (!streq(j->sp_namp, "root"))
221 continue;
222
223 bool unprovisioned = streq_ptr(j->sp_pwdp, PASSWORD_UNPROVISIONED);
224 log_debug("Root account found, %s.",
225 unprovisioned ? "with unprovisioned password, treating root as not configured" :
226 "treating root as configured");
227 return unprovisioned;
228 }
229 if (r < 0)
230 return log_error_errno(r, "Failed to read shadow: %m");
231 assert(r == 0);
232 log_debug("No root account found in shadow, assuming root is not configured.");
233 return true;
234 }
235
236 static int locale_is_ok(const char *name, void *userdata) {
237 int rfd = ASSERT_FD(PTR_TO_FD(userdata)), r;
238
239 r = dir_fd_is_root(rfd);
240 if (r < 0)
241 log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m");
242
243 return r != 0 ? locale_is_installed(name) > 0 : locale_is_valid(name);
244 }
245
246 static int prompt_locale(int rfd, sd_varlink **mute_console_link) {
247 _cleanup_strv_free_ char **locales = NULL;
248 bool acquired_from_creds = false;
249 int r;
250
251 assert(rfd >= 0);
252
253 if (arg_locale || arg_locale_messages)
254 return 0;
255
256 r = read_credential("firstboot.locale", (void**) &arg_locale, NULL);
257 if (r < 0)
258 log_debug_errno(r, "Failed to read credential firstboot.locale, ignoring: %m");
259 else
260 acquired_from_creds = true;
261
262 r = read_credential("firstboot.locale-messages", (void**) &arg_locale_messages, NULL);
263 if (r < 0)
264 log_debug_errno(r, "Failed to read credential firstboot.locale-messages, ignoring: %m");
265 else
266 acquired_from_creds = true;
267
268 if (acquired_from_creds) {
269 log_debug("Acquired locale from credentials.");
270 return 0;
271 }
272
273 if (!arg_prompt_locale) {
274 log_debug("Prompting for locale was not requested.");
275 return 0;
276 }
277
278 r = get_locales(&locales);
279 if (r < 0)
280 return log_error_errno(r, "Cannot query locales list: %m");
281
282 if (strv_isempty(locales))
283 log_debug("No locales found, skipping locale selection.");
284 else if (strv_length(locales) == 1) {
285
286 if (streq(locales[0], SYSTEMD_DEFAULT_LOCALE))
287 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
288 else {
289 log_debug("Only a single locale available (%s), selecting it as default.", locales[0]);
290
291 arg_locale = strdup(locales[0]);
292 if (!arg_locale)
293 return log_oom();
294
295 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
296 }
297 } else {
298 print_welcome(rfd, mute_console_link);
299
300 _cleanup_free_ char *prefill = NULL;
301 (void) locale_lang_from_efi(&prefill, LOCALE_REQUIRE_INSTALLED|LOCALE_SUPPRESS_EN_US);
302
303 r = prompt_loop("Please enter the new system locale name or number",
304 GLYPH_WORLD,
305 prefill,
306 locales,
307 /* accepted= */ NULL,
308 /* ellipsize_percentage= */ 60,
309 /* n_columns= */ 3,
310 /* column_width= */ 20,
311 locale_is_ok,
312 /* refresh= */ NULL,
313 FD_TO_PTR(rfd),
314 PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
315 &arg_locale);
316 if (r < 0)
317 return r;
318 if (isempty(arg_locale))
319 return 0;
320
321 r = prompt_loop("Please enter the new system message locale name or number",
322 GLYPH_WORLD,
323 /* prefill= */ NULL,
324 locales,
325 /* accepted= */ NULL,
326 /* ellipsize_percentage= */ 60,
327 /* n_columns= */ 3,
328 /* column_width= */ 20,
329 locale_is_ok,
330 /* refresh= */ NULL,
331 FD_TO_PTR(rfd),
332 PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
333 &arg_locale_messages);
334 if (r < 0)
335 return r;
336
337 /* Suppress the messages setting if it's the same as the main locale anyway */
338 if (streq_ptr(arg_locale, arg_locale_messages))
339 arg_locale_messages = mfree(arg_locale_messages);
340 }
341
342 return 0;
343 }
344
345 static int process_locale(int rfd, sd_varlink **mute_console_link) {
346 _cleanup_close_ int pfd = -EBADF;
347 _cleanup_free_ char *f = NULL;
348 char* locales[3];
349 unsigned i = 0;
350 int r;
351
352 assert(rfd >= 0);
353
354 pfd = chase_and_open_parent_at(rfd, rfd, etc_locale_conf(),
355 CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
356 &f);
357 if (pfd < 0)
358 return log_error_errno(pfd, "Failed to chase /etc/locale.conf: %m");
359
360 r = should_configure(pfd, f);
361 if (r == 0)
362 log_debug("Found /etc/locale.conf, assuming locale information has been configured.");
363 if (r <= 0)
364 return r;
365
366 r = dir_fd_is_root(rfd);
367 if (r < 0)
368 return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
369
370 if (arg_copy_locale && r == 0) {
371 r = copy_file_atomic_at(AT_FDCWD, etc_locale_conf(), pfd, f, 0644, COPY_REFLINK);
372 if (r != -ENOENT) {
373 if (r < 0)
374 return log_error_errno(r, "Failed to copy host's /etc/locale.conf: %m");
375
376 log_info("Copied host's /etc/locale.conf.");
377 return 0;
378 }
379 }
380
381 r = prompt_locale(rfd, mute_console_link);
382 if (r < 0)
383 return r;
384
385 if (!isempty(arg_locale))
386 locales[i++] = strjoina("LANG=", arg_locale);
387 if (!isempty(arg_locale_messages) && !streq_ptr(arg_locale_messages, arg_locale))
388 locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages);
389
390 if (i == 0)
391 return 0;
392
393 locales[i] = NULL;
394
395 r = write_env_file(
396 pfd,
397 f,
398 /* headers= */ NULL,
399 locales,
400 WRITE_ENV_FILE_LABEL);
401 if (r < 0)
402 return log_error_errno(r, "Failed to write /etc/locale.conf: %m");
403
404 log_info("/etc/locale.conf written.");
405 return 1;
406 }
407
408 static int keymap_is_ok(const char* name, void *userdata) {
409 int rfd = ASSERT_FD(PTR_TO_FD(userdata)), r;
410
411 r = dir_fd_is_root(rfd);
412 if (r < 0)
413 log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m");
414
415 return r != 0 ? keymap_exists(name) > 0 : keymap_is_valid(name);
416 }
417
418 static int prompt_keymap(int rfd, sd_varlink **mute_console_link) {
419 _cleanup_strv_free_ char **kmaps = NULL;
420 int r;
421
422 assert(rfd >= 0);
423
424 if (arg_keymap)
425 return 0;
426
427 _cleanup_free_ char *km = NULL;
428 r = read_credential("firstboot.keymap", (void**) &km, NULL);
429 if (r < 0)
430 log_debug_errno(r, "Failed to read credential firstboot.keymap, ignoring: %m");
431 else if (!keymap_is_valid(km))
432 log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "Keymap '%s' supplied via credential is not valid, ignoring.", km);
433 else {
434 log_debug("Acquired keymap from credential.");
435 arg_keymap = TAKE_PTR(km);
436 return 0;
437 }
438
439 bool b;
440 if (arg_prompt_keymap_auto) {
441 _cleanup_free_ char *ttyname = NULL;
442
443 r = getttyname_harder(STDOUT_FILENO, &ttyname);
444 if (r < 0) {
445 log_debug_errno(r, "Cannot determine TTY we are connected, ignoring: %m");
446 b = false; /* if we can't resolve this, it's probably not a VT */
447 } else {
448 b = tty_is_vc_resolve(ttyname);
449 log_debug("Detected connection to local console: %s", yes_no(b));
450 }
451 } else
452 b = arg_prompt_keymap;
453 if (!b) {
454 log_debug("Prompting for keymap was not requested.");
455 return 0;
456 }
457
458 r = get_keymaps(&kmaps);
459 if (r == -ENOENT) /* no keymaps installed */
460 return log_debug_errno(r, "No keymaps are installed.");
461 if (r < 0)
462 return log_error_errno(r, "Failed to read keymaps: %m");
463
464 print_welcome(rfd, mute_console_link);
465
466 _cleanup_free_ char *prefill = NULL;
467 (void) vconsole_keymap_from_efi(&prefill);
468
469 return prompt_loop(
470 "Please enter the new keymap name or number",
471 GLYPH_KEYBOARD,
472 prefill,
473 kmaps,
474 /* accepted= */ NULL,
475 /* ellipsize_percentage= */ 60,
476 /* n_columns= */ 3,
477 /* column_width= */ 20,
478 keymap_is_ok,
479 /* refresh= */ NULL,
480 FD_TO_PTR(rfd),
481 PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
482 &arg_keymap);
483 }
484
485 static int process_keymap(int rfd, sd_varlink **mute_console_link) {
486 _cleanup_close_ int pfd = -EBADF;
487 _cleanup_free_ char *f = NULL;
488 _cleanup_strv_free_ char **keymap = NULL;
489 int r;
490
491 assert(rfd >= 0);
492
493 pfd = chase_and_open_parent_at(rfd, rfd, etc_vconsole_conf(),
494 CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
495 &f);
496 if (pfd < 0)
497 return log_error_errno(pfd, "Failed to chase /etc/vconsole.conf: %m");
498
499 r = should_configure(pfd, f);
500 if (r == 0)
501 log_debug("Found /etc/vconsole.conf, assuming console has been configured.");
502 if (r <= 0)
503 return r;
504
505 r = dir_fd_is_root(rfd);
506 if (r < 0)
507 return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
508
509 if (arg_copy_keymap && r == 0) {
510 r = copy_file_atomic_at(AT_FDCWD, etc_vconsole_conf(), pfd, f, 0644, COPY_REFLINK);
511 if (r != -ENOENT) {
512 if (r < 0)
513 return log_error_errno(r, "Failed to copy host's /etc/vconsole.conf: %m");
514
515 log_info("Copied host's /etc/vconsole.conf.");
516 return 0;
517 }
518 }
519
520 r = prompt_keymap(rfd, mute_console_link);
521 if (r == -ENOENT)
522 return 0; /* don't fail if no keymaps are installed */
523 if (r < 0)
524 return r;
525
526 if (isempty(arg_keymap))
527 return 0;
528
529 VCContext vc = {
530 .keymap = arg_keymap,
531 };
532 _cleanup_(x11_context_clear) X11Context xc = {};
533
534 r = vconsole_convert_to_x11(&vc, /* verify= */ NULL, &xc);
535 if (r < 0)
536 return log_error_errno(r, "Failed to convert keymap data: %m");
537
538 r = vconsole_serialize(&vc, &xc, &keymap);
539 if (r < 0)
540 return log_error_errno(r, "Failed to serialize keymap data: %m");
541
542 r = write_vconsole_conf(pfd, f, keymap);
543 if (r < 0)
544 return log_error_errno(r, "Failed to write /etc/vconsole.conf: %m");
545
546 log_info("/etc/vconsole.conf written.");
547 return 1;
548 }
549
550 static int timezone_is_ok(const char *name, void *userdata) {
551 return timezone_is_valid(name, LOG_DEBUG);
552 }
553
554 static int prompt_timezone(int rfd, sd_varlink **mute_console_link) {
555 _cleanup_strv_free_ char **zones = NULL;
556 int r;
557
558 assert(rfd >= 0);
559
560 if (arg_timezone)
561 return 0;
562
563 _cleanup_free_ char *tz = NULL;
564 r = read_credential("firstboot.timezone", (void**) &tz, NULL);
565 if (r < 0)
566 log_debug_errno(r, "Failed to read credential firstboot.timezone, ignoring: %m");
567 else if (!timezone_is_valid(tz, LOG_DEBUG))
568 log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "Timezone '%s' supplied via credential is not valid, ignoring.", tz);
569 else {
570 log_debug("Acquired timezone from credential.");
571 arg_timezone = TAKE_PTR(tz);
572 return 0;
573 }
574
575 if (!arg_prompt_timezone) {
576 log_debug("Prompting for timezone was not requested.");
577 return 0;
578 }
579
580 r = get_timezones(&zones);
581 if (r < 0)
582 return log_error_errno(r, "Cannot query timezone list: %m");
583
584 print_welcome(rfd, mute_console_link);
585
586 return prompt_loop(
587 "Please enter the new timezone name or number",
588 GLYPH_CLOCK,
589 /* prefill= */ NULL,
590 zones,
591 /* accepted= */ NULL,
592 /* ellipsize_percentage= */ 30,
593 /* n_columns= */ 3,
594 /* column_width= */ 20,
595 timezone_is_ok,
596 /* refresh= */ NULL,
597 FD_TO_PTR(rfd),
598 PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
599 &arg_timezone);
600 }
601
602 static int process_timezone(int rfd, sd_varlink **mute_console_link) {
603 _cleanup_close_ int pfd = -EBADF;
604 _cleanup_free_ char *f = NULL, *relpath = NULL;
605 const char *e;
606 int r;
607
608 assert(rfd >= 0);
609
610 pfd = chase_and_open_parent_at(rfd, rfd, etc_localtime(),
611 CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
612 &f);
613 if (pfd < 0)
614 return log_error_errno(pfd, "Failed to chase /etc/localtime: %m");
615
616 r = should_configure(pfd, f);
617 if (r == 0)
618 log_debug("Found /etc/localtime, assuming timezone has been configured.");
619 if (r <= 0)
620 return r;
621
622 r = dir_fd_is_root(rfd);
623 if (r < 0)
624 return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
625
626 if (arg_copy_timezone && r == 0) {
627 _cleanup_free_ char *s = NULL;
628
629 r = readlink_malloc(etc_localtime(), &s);
630 if (r != -ENOENT) {
631 if (r < 0)
632 return log_error_errno(r, "Failed to read host's /etc/localtime: %m");
633
634 r = symlinkat_atomic_full(s, pfd, f, SYMLINK_LABEL);
635 if (r < 0)
636 return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
637
638 log_info("Copied host's /etc/localtime.");
639 return 0;
640 }
641 }
642
643 r = prompt_timezone(rfd, mute_console_link);
644 if (r < 0)
645 return r;
646
647 if (isempty(arg_timezone))
648 return 0;
649
650 e = strjoina("/usr/share/zoneinfo/", arg_timezone);
651 r = path_make_relative_parent(etc_localtime(), e, &relpath);
652 if (r < 0)
653 return r;
654
655 r = symlinkat_atomic_full(relpath, pfd, f, SYMLINK_LABEL);
656 if (r < 0)
657 return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
658
659 log_info("/etc/localtime written");
660 return 0;
661 }
662
663 static int hostname_is_ok(const char *name, void *userdata) {
664 return hostname_is_valid(name, VALID_HOSTNAME_TRAILING_DOT);
665 }
666
667 static int prompt_hostname(int rfd, sd_varlink **mute_console_link) {
668 int r;
669
670 assert(rfd >= 0);
671
672 if (arg_hostname)
673 return 0;
674
675 _cleanup_free_ char *hn = NULL;
676 r = read_credential("firstboot.hostname", (void**) &hn, NULL);
677 if (r < 0)
678 log_debug_errno(r, "Failed to read credential firstboot.hostname, ignoring: %m");
679 else if (!hostname_is_valid(hn, VALID_HOSTNAME_TRAILING_DOT|VALID_HOSTNAME_QUESTION_MARK))
680 log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "Hostname '%s' supplied via credential is not valid, ignoring.", hn);
681 else {
682 log_debug("Acquired hostname from credentials.");
683 arg_hostname = TAKE_PTR(hn);
684 hostname_cleanup(arg_hostname);
685 return 0;
686 }
687
688 if (!arg_prompt_hostname) {
689 log_debug("Prompting for hostname was not requested.");
690 return 0;
691 }
692
693 print_welcome(rfd, mute_console_link);
694
695 r = prompt_loop("Please enter the new hostname",
696 GLYPH_LABEL,
697 /* prefill= */ NULL,
698 /* menu= */ NULL,
699 /* accepted= */ NULL,
700 /* ellipsize_percentage= */ 100,
701 /* n_columns= */ 3,
702 /* column_width= */ 20,
703 hostname_is_ok,
704 /* refresh= */ NULL,
705 FD_TO_PTR(rfd),
706 PROMPT_MAY_SKIP,
707 &arg_hostname);
708 if (r < 0)
709 return r;
710
711 if (arg_hostname)
712 hostname_cleanup(arg_hostname);
713
714 return 0;
715 }
716
717 static int process_hostname(int rfd, sd_varlink **mute_console_link) {
718 _cleanup_close_ int pfd = -EBADF;
719 _cleanup_free_ char *f = NULL;
720 int r;
721
722 assert(rfd >= 0);
723
724 pfd = chase_and_open_parent_at(rfd, rfd, etc_hostname(), CHASE_MKDIR_0755|CHASE_WARN, &f);
725 if (pfd < 0)
726 return log_error_errno(pfd, "Failed to chase /etc/hostname: %m");
727
728 r = should_configure(pfd, f);
729 if (r == 0)
730 log_debug("Found /etc/hostname, assuming hostname has been configured.");
731 if (r <= 0)
732 return r;
733
734 r = prompt_hostname(rfd, mute_console_link);
735 if (r < 0)
736 return r;
737
738 if (isempty(arg_hostname))
739 return 0;
740
741 r = write_string_file_at(pfd, f, arg_hostname,
742 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
743 if (r < 0)
744 return log_error_errno(r, "Failed to write /etc/hostname: %m");
745
746 log_info("/etc/hostname written.");
747 return 0;
748 }
749
750 static int process_machine_id(int rfd) {
751 _cleanup_close_ int pfd = -EBADF;
752 _cleanup_free_ char *f = NULL;
753 int r;
754
755 assert(rfd >= 0);
756
757 pfd = chase_and_open_parent_at(rfd, rfd, "/etc/machine-id",
758 CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
759 &f);
760 if (pfd < 0)
761 return log_error_errno(pfd, "Failed to chase /etc/machine-id: %m");
762
763 r = should_configure(pfd, f);
764 if (r == 0)
765 log_debug("Found /etc/machine-id, assuming machine-id has been configured.");
766 if (r <= 0)
767 return r;
768
769 if (sd_id128_is_null(arg_machine_id)) {
770 log_debug("Initialization of machine-id was not requested, skipping.");
771 return 0;
772 }
773
774 r = write_string_file_at(pfd, "machine-id", SD_ID128_TO_STRING(arg_machine_id),
775 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
776 if (r < 0)
777 return log_error_errno(r, "Failed to write /etc/machine-id: %m");
778
779 log_info("/etc/machine-id written.");
780 return 0;
781 }
782
783 static int process_machine_tags(int rfd) {
784 int r;
785
786 assert(rfd >= 0);
787
788 _cleanup_free_ char *f = NULL;
789 _cleanup_close_ int pfd = chase_and_open_parent_at(
790 /* root_fd= */ rfd,
791 /* dir_fd= */ rfd,
792 "/etc/machine-info",
793 CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
794 &f);
795 if (pfd < 0)
796 return log_error_errno(pfd, "Failed to chase /etc/machine-info parent: %m");
797
798 r = should_configure(pfd, f);
799 if (r == 0)
800 log_debug("Found /etc/machine-info, assuming machine tags have been configured.");
801 if (r <= 0)
802 return r;
803
804 if (!arg_machine_tags) {
805 _cleanup_free_ char *tags = NULL;
806 r = read_credential("firstboot.machine-tags", (void**) &tags, /* ret_size= */ NULL);
807 if (r < 0)
808 log_debug_errno(r, "Failed to read credential firstboot.machine-tags, ignoring: %m");
809 else {
810 _cleanup_strv_free_ char **l = NULL;
811 r = machine_tags_from_string(tags, /* graceful= */ false, &l);
812 if (r < 0)
813 log_warning_errno(r, "Failed to parse machine tags '%s', ignoring credential: %m", tags);
814 else {
815 strv_free_and_replace(arg_machine_tags, l);
816 log_debug("Acquired machine tags list from credentials.");
817 }
818 }
819 }
820
821 /* NB: We do not prompt for machine tags, at least not for now */
822
823 if (!arg_machine_tags) {
824 log_debug("Initialization of machine tags was not requested, skipping.");
825 return 0;
826 }
827
828 _cleanup_free_ char *j = strv_join(arg_machine_tags, ":");
829 if (!j)
830 return log_oom();
831
832 _cleanup_free_ char *c = strjoin("TAGS=\"", j, "\"\n");
833 if (!c)
834 return log_oom();
835
836 r = write_string_file_at(
837 pfd,
838 "machine-info",
839 c,
840 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
841 if (r < 0)
842 return log_error_errno(r, "Failed to write /etc/machine-info: %m");
843
844 log_info("/etc/machine-info written.");
845 return 0;
846 }
847
848 static int prompt_root_password(int rfd, sd_varlink **mute_console_link) {
849 const char *msg1, *msg2;
850 int r;
851
852 assert(rfd >= 0);
853
854 if (arg_root_password)
855 return 0;
856
857 if (get_credential_user_password("root", &arg_root_password, &arg_root_password_is_hashed) >= 0)
858 return 0;
859
860 if (!arg_prompt_root_password) {
861 log_debug("Prompting for root password was not requested.");
862 return 0;
863 }
864
865 print_welcome(rfd, mute_console_link);
866
867 msg1 = "Please enter the new root password (empty to skip):";
868 msg2 = "Please enter the new root password again:";
869
870 suggest_passwords();
871
872 for (;;) {
873 _cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
874 _cleanup_free_ char *error = NULL;
875
876 AskPasswordRequest req = {
877 .tty_fd = -EBADF,
878 .message = msg1,
879 .until = USEC_INFINITY,
880 .hup_fd = -EBADF,
881 };
882
883 r = ask_password_tty(&req, /* flags= */ 0, &a);
884 if (r < 0)
885 return log_error_errno(r, "Failed to query root password: %m");
886 if (strv_length(a) != 1)
887 return log_error_errno(SYNTHETIC_ERRNO(EIO),
888 "Received multiple passwords, where we expected one.");
889
890 if (isempty(*a)) {
891 log_info("No password entered, skipping.");
892 break;
893 }
894
895 r = check_password_quality(*a, /* old= */ NULL, "root", &error);
896 if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
897 log_warning("Password quality check is not supported, proceeding anyway.");
898 else if (r < 0)
899 return log_error_errno(r, "Failed to check password quality: %m");
900 else if (r == 0)
901 log_warning("Password is weak, accepting anyway: %s", error);
902
903 req.message = msg2;
904
905 r = ask_password_tty(&req, /* flags= */ 0, &b);
906 if (r < 0)
907 return log_error_errno(r, "Failed to query root password: %m");
908 if (strv_length(b) != 1)
909 return log_error_errno(SYNTHETIC_ERRNO(EIO),
910 "Received multiple passwords, where we expected one.");
911
912 if (!streq(*a, *b)) {
913 log_error("Entered passwords did not match, please try again.");
914 continue;
915 }
916
917 arg_root_password = TAKE_PTR(*a);
918 break;
919 }
920
921 return 0;
922 }
923
924 static int find_shell(int rfd, const char *path) {
925 int r;
926
927 assert(path);
928
929 if (!valid_shell(path))
930 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "%s is not a valid shell", path);
931
932 r = chaseat(rfd, rfd, path, /* flags= */ 0, /* ret_path= */ NULL, /* ret_fd= */ NULL);
933 if (r < 0)
934 return log_error_errno(r, "Failed to resolve shell %s: %m", path);
935
936 return 0;
937 }
938
939 static int shell_is_ok(const char *path, void *userdata) {
940 int rfd = ASSERT_FD(PTR_TO_FD(userdata));
941
942 return find_shell(rfd, path) >= 0;
943 }
944
945 static int prompt_root_shell(int rfd, sd_varlink **mute_console_link) {
946 int r;
947
948 assert(rfd >= 0);
949
950 if (arg_root_shell)
951 return 0;
952
953 r = read_credential("passwd.shell.root", (void**) &arg_root_shell, NULL);
954 if (r < 0)
955 log_debug_errno(r, "Failed to read credential passwd.shell.root, ignoring: %m");
956 else {
957 log_debug("Acquired root shell from credential.");
958 return 0;
959 }
960
961 if (!arg_prompt_root_shell) {
962 log_debug("Prompting for root shell was not requested.");
963 return 0;
964 }
965
966 print_welcome(rfd, mute_console_link);
967
968 return prompt_loop(
969 "Please enter the new root shell",
970 GLYPH_SHELL,
971 /* prefill= */ NULL,
972 /* menu= */ NULL,
973 /* accepted= */ NULL,
974 /* ellipsize_percentage= */ 0,
975 /* n_columns= */ 3,
976 /* column_width= */ 20,
977 shell_is_ok,
978 /* refresh= */ NULL,
979 FD_TO_PTR(rfd),
980 PROMPT_MAY_SKIP,
981 &arg_root_shell);
982 }
983
984 static int write_root_passwd(int rfd, int etc_fd, const char *password, const char *shell) {
985 _cleanup_fclose_ FILE *original = NULL, *passwd = NULL;
986 _cleanup_(unlink_and_freep) char *passwd_tmp = NULL;
987 int r;
988 bool found = false;
989
990 r = fopen_temporary_at_label(etc_fd, "passwd", "passwd", &passwd, &passwd_tmp);
991 if (r < 0)
992 return r;
993
994 r = xfopenat(etc_fd, "passwd", "re", O_NOFOLLOW, &original);
995 if (r < 0 && r != -ENOENT)
996 return r;
997
998 if (original) {
999 struct passwd *i;
1000
1001 r = copy_rights(fileno(original), fileno(passwd));
1002 if (r < 0)
1003 return r;
1004
1005 while ((r = fgetpwent_sane(original, &i)) > 0) {
1006
1007 if (streq(i->pw_name, "root")) {
1008 if (password)
1009 i->pw_passwd = (char *) password;
1010 if (shell)
1011 i->pw_shell = (char *) shell;
1012 found = true;
1013 }
1014
1015 r = putpwent_sane(i, passwd);
1016 if (r < 0)
1017 return r;
1018 }
1019 if (r < 0)
1020 return r;
1021
1022 } else {
1023 r = fchmod(fileno(passwd), 0644);
1024 if (r < 0)
1025 return -errno;
1026 }
1027
1028 if (!found) {
1029 struct passwd root = {
1030 .pw_name = (char *) "root",
1031 .pw_passwd = (char *) (password ?: PASSWORD_SEE_SHADOW),
1032 .pw_uid = 0,
1033 .pw_gid = 0,
1034 .pw_gecos = (char *) "Super User",
1035 .pw_dir = (char *) "/root",
1036 .pw_shell = (char *) (shell ?: default_root_shell_at(rfd)),
1037 };
1038
1039 r = putpwent_sane(&root, passwd);
1040 if (r < 0)
1041 return r;
1042 }
1043
1044 r = fflush_sync_and_check(passwd);
1045 if (r < 0)
1046 return r;
1047
1048 r = renameat_and_apply_smack_floor_label(etc_fd, passwd_tmp, etc_fd, "passwd");
1049 if (r < 0)
1050 return r;
1051
1052 return 0;
1053 }
1054
1055 static int write_root_shadow(int etc_fd, const char *hashed_password) {
1056 _cleanup_fclose_ FILE *original = NULL, *shadow = NULL;
1057 _cleanup_(unlink_and_freep) char *shadow_tmp = NULL;
1058 int r;
1059 bool found = false;
1060
1061 r = fopen_temporary_at_label(etc_fd, "shadow", "shadow", &shadow, &shadow_tmp);
1062 if (r < 0)
1063 return r;
1064
1065 r = xfopenat(etc_fd, "shadow", "re", O_NOFOLLOW, &original);
1066 if (r < 0 && r != -ENOENT)
1067 return r;
1068
1069 if (original) {
1070 struct spwd *i;
1071
1072 r = copy_rights(fileno(original), fileno(shadow));
1073 if (r < 0)
1074 return r;
1075
1076 while ((r = fgetspent_sane(original, &i)) > 0) {
1077
1078 if (streq(i->sp_namp, "root")) {
1079 if (hashed_password) {
1080 i->sp_pwdp = (char *) hashed_password;
1081 i->sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
1082 }
1083 found = true;
1084 }
1085
1086 r = putspent_sane(i, shadow);
1087 if (r < 0)
1088 return r;
1089 }
1090 if (r < 0)
1091 return r;
1092
1093 } else {
1094 r = fchmod(fileno(shadow), 0000);
1095 if (r < 0)
1096 return -errno;
1097 }
1098
1099 if (!found) {
1100 struct spwd root = {
1101 .sp_namp = (char*) "root",
1102 .sp_pwdp = (char *) (hashed_password ?: PASSWORD_LOCKED_AND_INVALID),
1103 .sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY),
1104 .sp_min = -1,
1105 .sp_max = -1,
1106 .sp_warn = -1,
1107 .sp_inact = -1,
1108 .sp_expire = -1,
1109 .sp_flag = ULONG_MAX, /* this appears to be what everybody does ... */
1110 };
1111
1112 r = putspent_sane(&root, shadow);
1113 if (r < 0)
1114 return r;
1115 }
1116
1117 r = fflush_sync_and_check(shadow);
1118 if (r < 0)
1119 return r;
1120
1121 r = renameat_and_apply_smack_floor_label(etc_fd, shadow_tmp, etc_fd, "shadow");
1122 if (r < 0)
1123 return r;
1124
1125 return 0;
1126 }
1127
1128 static int process_root_account(int rfd, sd_varlink **mute_console_link) {
1129 _cleanup_close_ int pfd = -EBADF;
1130 _cleanup_(release_lock_file) LockFile lock = LOCK_FILE_INIT;
1131 _cleanup_(erase_and_freep) char *_hashed_password = NULL;
1132 const char *password, *hashed_password;
1133 int k = 0, r;
1134
1135 assert(rfd >= 0);
1136
1137 pfd = chase_and_open_parent_at(rfd, rfd, "/etc/passwd",
1138 CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
1139 NULL);
1140 if (pfd < 0)
1141 return log_error_errno(pfd, "Failed to chase /etc/passwd: %m");
1142
1143 /* Ensure that passwd and shadow are in the same directory and are not symlinks. */
1144
1145 FOREACH_STRING(s, "passwd", "shadow") {
1146 r = verify_regular_at(pfd, s, /* follow= */ false);
1147 if (r < 0 && r != -ENOENT)
1148 return log_error_errno(r, "Verification of /etc/%s being regular file failed: %m", s);
1149
1150 r = should_configure(pfd, s);
1151 if (r < 0)
1152 return r;
1153
1154 k += r;
1155 }
1156
1157 if (k == 0) {
1158 log_debug("Found /etc/passwd and /etc/shadow, assuming root account has been initialized.");
1159 return 0;
1160 }
1161
1162 r = make_lock_file_at(pfd, ETC_PASSWD_LOCK_FILENAME, LOCK_EX, &lock);
1163 if (r < 0)
1164 return log_error_errno(r, "Failed to take a lock on /etc/passwd: %m");
1165
1166 k = dir_fd_is_root(rfd);
1167 if (k < 0)
1168 return log_error_errno(k, "Failed to check if directory file descriptor is root: %m");
1169
1170 if (arg_copy_root_shell && k == 0) {
1171 _cleanup_free_ struct passwd *p = NULL;
1172
1173 r = getpwnam_malloc("root", &p);
1174 if (r < 0)
1175 return log_error_errno(r, "Failed to find passwd entry for root: %m");
1176
1177 r = free_and_strdup(&arg_root_shell, p->pw_shell);
1178 if (r < 0)
1179 return log_oom();
1180 }
1181
1182 r = prompt_root_shell(rfd, mute_console_link);
1183 if (r < 0)
1184 return r;
1185
1186 if (arg_copy_root_password && k == 0) {
1187 struct spwd *p;
1188
1189 errno = 0;
1190 p = getspnam("root");
1191 if (!p)
1192 return log_error_errno(errno_or_else(EIO), "Failed to find shadow entry for root: %m");
1193
1194 r = free_and_strdup(&arg_root_password, p->sp_pwdp);
1195 if (r < 0)
1196 return log_oom();
1197
1198 arg_root_password_is_hashed = true;
1199 }
1200
1201 r = prompt_root_password(rfd, mute_console_link);
1202 if (r < 0)
1203 return r;
1204
1205 if (arg_root_password && arg_root_password_is_hashed) {
1206 password = PASSWORD_SEE_SHADOW;
1207 hashed_password = arg_root_password;
1208 } else if (arg_root_password) {
1209 r = hash_password(arg_root_password, &_hashed_password);
1210 if (r < 0)
1211 return log_error_errno(r, "Failed to hash password: %m");
1212
1213 password = PASSWORD_SEE_SHADOW;
1214 hashed_password = _hashed_password;
1215
1216 } else if (arg_delete_root_password) {
1217 password = PASSWORD_SEE_SHADOW;
1218 hashed_password = PASSWORD_NONE;
1219 } else if (!arg_root_password && arg_prompt_root_password) {
1220 /* If the user was prompted, but no password was supplied, lock the account. */
1221 password = PASSWORD_SEE_SHADOW;
1222 hashed_password = PASSWORD_LOCKED_AND_INVALID;
1223 } else
1224 /* Leave the password as is. */
1225 password = hashed_password = NULL;
1226
1227 /* Don't create/modify passwd and shadow if there's nothing to do. */
1228 if (!(password || hashed_password || arg_root_shell)) {
1229 log_debug("Initialization of root account was not requested, skipping.");
1230 return 0;
1231 }
1232
1233 r = write_root_passwd(rfd, pfd, password, arg_root_shell);
1234 if (r < 0)
1235 return log_error_errno(r, "Failed to write /etc/passwd: %m");
1236
1237 log_info("/etc/passwd written.");
1238
1239 r = write_root_shadow(pfd, hashed_password);
1240 if (r < 0)
1241 return log_error_errno(r, "Failed to write /etc/shadow: %m");
1242
1243 log_info("/etc/shadow written.");
1244 return 0;
1245 }
1246
1247 static int process_kernel_cmdline(int rfd) {
1248 _cleanup_close_ int pfd = -EBADF;
1249 _cleanup_free_ char *f = NULL;
1250 int r;
1251
1252 assert(rfd >= 0);
1253
1254 pfd = chase_and_open_parent_at(rfd, rfd, "/etc/kernel/cmdline",
1255 CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
1256 &f);
1257 if (pfd < 0)
1258 return log_error_errno(pfd, "Failed to chase /etc/kernel/cmdline: %m");
1259
1260 r = should_configure(pfd, f);
1261 if (r == 0)
1262 log_debug("Found /etc/kernel/cmdline, assuming kernel command line has been configured.");
1263 if (r <= 0)
1264 return r;
1265
1266 if (!arg_kernel_cmdline) {
1267 log_debug("Creation of /etc/kernel/cmdline was not requested, skipping.");
1268 return 0;
1269 }
1270
1271 r = write_string_file_at(pfd, "cmdline", arg_kernel_cmdline,
1272 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
1273 if (r < 0)
1274 return log_error_errno(r, "Failed to write /etc/kernel/cmdline: %m");
1275
1276 log_info("/etc/kernel/cmdline written.");
1277 return 0;
1278 }
1279
1280 static int reset_one(int rfd, const char *path) {
1281 _cleanup_close_ int pfd = -EBADF;
1282 _cleanup_free_ char *f = NULL;
1283
1284 assert(rfd >= 0);
1285 assert(path);
1286
1287 pfd = chase_and_open_parent_at(rfd, rfd, path, CHASE_WARN|CHASE_NOFOLLOW, &f);
1288 if (pfd == -ENOENT)
1289 return 0;
1290 if (pfd < 0)
1291 return log_error_errno(pfd, "Failed to resolve %s: %m", path);
1292
1293 if (unlinkat(pfd, f, 0) < 0)
1294 return errno == ENOENT ? 0 : log_error_errno(errno, "Failed to remove %s: %m", path);
1295
1296 log_info("Removed %s", path);
1297 return 0;
1298 }
1299
1300 static int process_reset(int rfd) {
1301 int r;
1302
1303 assert(rfd >= 0);
1304
1305 if (!arg_reset)
1306 return 0;
1307
1308 FOREACH_STRING(p,
1309 etc_locale_conf(),
1310 etc_vconsole_conf(),
1311 etc_hostname(),
1312 "/etc/machine-id",
1313 "/etc/kernel/cmdline",
1314 etc_localtime()) {
1315 r = reset_one(rfd, p);
1316 if (r < 0)
1317 return r;
1318 }
1319
1320 return 0;
1321 }
1322
1323 static int help(void) {
1324 _cleanup_(table_unrefp) Table *options = NULL;
1325 int r;
1326
1327 r = option_parser_get_help_table(&options);
1328 if (r < 0)
1329 return r;
1330
1331 help_cmdline("[OPTIONS...]");
1332 help_abstract("Configures basic settings of the system.");
1333 help_section("Options");
1334
1335 r = table_print_or_warn(options);
1336 if (r < 0)
1337 return r;
1338
1339 help_man_page_reference("systemd-firstboot", "1");
1340 return 0;
1341 }
1342
1343 static int parse_argv(int argc, char *argv[]) {
1344 assert(argc >= 0);
1345 assert(argv);
1346
1347 OptionParser opts = { argc, argv };
1348 int r;
1349
1350 FOREACH_OPTION_OR_RETURN(c, &opts)
1351 switch (c) {
1352
1353 OPTION_COMMON_HELP:
1354 return help();
1355
1356 OPTION_COMMON_VERSION:
1357 return version();
1358
1359 OPTION_LONG("root", "PATH", "Operate on an alternate filesystem root"):
1360 r = parse_path_argument(opts.arg, true, &arg_root);
1361 if (r < 0)
1362 return r;
1363 break;
1364
1365 OPTION_LONG("image", "PATH", "Operate on disk image as filesystem root"):
1366 r = parse_path_argument(opts.arg, false, &arg_image);
1367 if (r < 0)
1368 return r;
1369 break;
1370
1371 OPTION_LONG("image-policy", "POLICY", "Specify disk image dissection policy"):
1372 r = parse_image_policy_argument(opts.arg, &arg_image_policy);
1373 if (r < 0)
1374 return r;
1375 break;
1376
1377 OPTION_LONG("locale", "LOCALE", "Set primary locale (LANG=)"):
1378 r = free_and_strdup_warn(&arg_locale, opts.arg);
1379 if (r < 0)
1380 return r;
1381 break;
1382
1383 OPTION_LONG("locale-messages", "LOCALE", "Set message locale (LC_MESSAGES=)"):
1384 r = free_and_strdup_warn(&arg_locale_messages, opts.arg);
1385 if (r < 0)
1386 return r;
1387 break;
1388
1389 OPTION_LONG("keymap", "KEYMAP", "Set keymap"):
1390 if (!keymap_is_valid(opts.arg))
1391 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1392 "Keymap %s is not valid.", opts.arg);
1393
1394 r = free_and_strdup_warn(&arg_keymap, opts.arg);
1395 if (r < 0)
1396 return r;
1397 break;
1398
1399 OPTION_LONG("timezone", "TIMEZONE", "Set timezone"):
1400 if (!timezone_is_valid(opts.arg, LOG_ERR))
1401 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1402 "Timezone %s is not valid.", opts.arg);
1403
1404 r = free_and_strdup_warn(&arg_timezone, opts.arg);
1405 if (r < 0)
1406 return r;
1407 break;
1408
1409 OPTION_LONG("hostname", "NAME", "Set hostname"):
1410 if (!hostname_is_valid(opts.arg, VALID_HOSTNAME_TRAILING_DOT|VALID_HOSTNAME_QUESTION_MARK))
1411 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1412 "Host name %s is not valid.", opts.arg);
1413
1414 r = free_and_strdup_warn(&arg_hostname, opts.arg);
1415 if (r < 0)
1416 return r;
1417
1418 hostname_cleanup(arg_hostname);
1419 break;
1420
1421 OPTION_LONG("setup-machine-id", NULL, "Set a random machine ID"):
1422 r = sd_id128_randomize(&arg_machine_id);
1423 if (r < 0)
1424 return log_error_errno(r, "Failed to generate randomized machine ID: %m");
1425 break;
1426
1427 OPTION_LONG("machine-id", "ID", "Set specified machine ID"):
1428 r = sd_id128_from_string(opts.arg, &arg_machine_id);
1429 if (r < 0)
1430 return log_error_errno(r, "Failed to parse machine id %s.", opts.arg);
1431 break;
1432
1433 OPTION_LONG("machine-tags", "TAG[:…]", "Set machine tags"): {
1434 _cleanup_strv_free_ char **tags = NULL;
1435 r = machine_tags_from_string(opts.arg, /* graceful= */ false, &tags);
1436 if (r < 0)
1437 return log_error_errno(r, "Failed to parse machine tags '%s': %m", opts.arg);
1438
1439 strv_free_and_replace(arg_machine_tags, tags);
1440 break;
1441 }
1442
1443 OPTION_LONG("root-password", "PASSWORD", "Set root password from plaintext password"):
1444 r = free_and_strdup_warn(&arg_root_password, opts.arg);
1445 if (r < 0)
1446 return r;
1447
1448 arg_root_password_is_hashed = false;
1449 break;
1450
1451 OPTION_LONG("root-password-file", "FILE", "Set root password from file"):
1452 arg_root_password = mfree(arg_root_password);
1453
1454 r = read_one_line_file(opts.arg, &arg_root_password);
1455 if (r < 0)
1456 return log_error_errno(r, "Failed to read %s: %m", opts.arg);
1457
1458 arg_root_password_is_hashed = false;
1459 break;
1460
1461 OPTION_LONG("root-password-hashed", "HASH", "Set root password from hashed password"):
1462 r = free_and_strdup_warn(&arg_root_password, opts.arg);
1463 if (r < 0)
1464 return r;
1465
1466 arg_root_password_is_hashed = true;
1467 break;
1468
1469 OPTION_LONG("root-shell", "SHELL", "Set root shell"):
1470 r = free_and_strdup_warn(&arg_root_shell, opts.arg);
1471 if (r < 0)
1472 return r;
1473 break;
1474
1475 OPTION_LONG("kernel-command-line", "CMDLINE", "Set kernel command line"):
1476 r = free_and_strdup_warn(&arg_kernel_cmdline, opts.arg);
1477 if (r < 0)
1478 return r;
1479 break;
1480
1481 OPTION_LONG("prompt-locale", NULL, "Prompt the user for locale settings"):
1482 arg_prompt_locale = true;
1483 break;
1484
1485 OPTION_LONG("prompt-keymap", NULL, "Prompt the user for keymap settings"):
1486 arg_prompt_keymap = true;
1487 arg_prompt_keymap_auto = false;
1488 break;
1489
1490 OPTION_LONG("prompt-keymap-auto", NULL,
1491 "Prompt the user for keymap settings if invoked on local console"):
1492 arg_prompt_keymap_auto = true;
1493 break;
1494
1495 OPTION_LONG("prompt-timezone", NULL, "Prompt the user for timezone"):
1496 arg_prompt_timezone = true;
1497 break;
1498
1499 OPTION_LONG("prompt-hostname", NULL, "Prompt the user for hostname"):
1500 arg_prompt_hostname = true;
1501 break;
1502
1503 OPTION_LONG("prompt-root-password", NULL, "Prompt the user for root password"):
1504 arg_prompt_root_password = true;
1505 break;
1506
1507 OPTION_LONG("prompt-root-shell", NULL, "Prompt the user for root shell"):
1508 arg_prompt_root_shell = true;
1509 break;
1510
1511 OPTION_LONG("prompt", NULL, "Prompt for all of the above"):
1512 arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname =
1513 arg_prompt_root_password = arg_prompt_root_shell = true;
1514 arg_prompt_keymap_auto = false;
1515 break;
1516
1517 OPTION_LONG("copy-locale", NULL, "Copy locale from host"):
1518 arg_copy_locale = true;
1519 break;
1520
1521 OPTION_LONG("copy-keymap", NULL, "Copy keymap from host"):
1522 arg_copy_keymap = true;
1523 break;
1524
1525 OPTION_LONG("copy-timezone", NULL, "Copy timezone from host"):
1526 arg_copy_timezone = true;
1527 break;
1528
1529 OPTION_LONG("copy-root-password", NULL, "Copy root password from host"):
1530 arg_copy_root_password = true;
1531 break;
1532
1533 OPTION_LONG("copy-root-shell", NULL, "Copy root shell from host"):
1534 arg_copy_root_shell = true;
1535 break;
1536
1537 OPTION_LONG("copy", NULL, "Copy all of the above"):
1538 arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password =
1539 arg_copy_root_shell = true;
1540 break;
1541
1542 OPTION_LONG("force", NULL, "Overwrite existing files"):
1543 arg_force = true;
1544 break;
1545
1546 OPTION_LONG("delete-root-password", NULL, "Delete root password"):
1547 arg_delete_root_password = true;
1548 break;
1549
1550 OPTION_LONG("welcome", "BOOL", "Whether to show the welcome text"):
1551 r = parse_boolean_argument("--welcome=", opts.arg, &arg_welcome);
1552 if (r < 0)
1553 return r;
1554 break;
1555
1556 OPTION_LONG("chrome", "BOOL",
1557 "Whether to show a color bar at top and bottom of terminal"):
1558 r = parse_boolean_argument("--chrome=", opts.arg, &arg_chrome);
1559 if (r < 0)
1560 return r;
1561 break;
1562
1563 OPTION_LONG("mute-console", "BOOL",
1564 "Whether to disallow kernel/PID 1 writes to the console while running"):
1565 r = parse_boolean_argument("--mute-console=", opts.arg, &arg_mute_console);
1566 if (r < 0)
1567 return r;
1568 break;
1569
1570 OPTION_LONG("reset", NULL, "Remove existing files"):
1571 arg_reset = true;
1572 break;
1573 }
1574
1575 if (arg_delete_root_password && (arg_copy_root_password || arg_root_password || arg_prompt_root_password))
1576 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1577 "--delete-root-password cannot be combined with other root password options.");
1578
1579 if (arg_image && arg_root)
1580 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1581 "--root= and --image= cannot be used together.");
1582
1583 if (!sd_id128_is_null(arg_machine_id) && !(arg_image || arg_root) && !arg_force)
1584 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1585 "--machine-id=/--setup-machine-id only works with --root= or --image=.");
1586
1587 return 1;
1588 }
1589
1590 static int reload_system_manager(sd_bus **bus) {
1591 int r;
1592
1593 assert(bus);
1594
1595 if (!*bus) {
1596 r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
1597 if (r < 0)
1598 return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
1599 }
1600
1601 r = bus_service_manager_reload(*bus);
1602 if (r < 0)
1603 return r;
1604
1605 log_info("Requested manager reload to apply locale configuration.");
1606 return 0;
1607 }
1608
1609 static int reload_vconsole(sd_bus **bus) {
1610 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1611 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1612 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
1613 const char *object;
1614 int r;
1615
1616 assert(bus);
1617
1618 if (!*bus) {
1619 r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
1620 if (r < 0)
1621 return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
1622 }
1623
1624 r = bus_wait_for_jobs_new(*bus, &w);
1625 if (r < 0)
1626 return log_error_errno(r, "Could not watch jobs: %m");
1627
1628 r = bus_call_method(*bus, bus_systemd_mgr, "RestartUnit", &error, &reply,
1629 "ss", "systemd-vconsole-setup.service", "replace");
1630 if (r < 0)
1631 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
1632
1633 r = sd_bus_message_read(reply, "o", &object);
1634 if (r < 0)
1635 return bus_log_parse_error(r);
1636
1637 r = bus_wait_for_jobs_one(w, object, BUS_WAIT_JOBS_LOG_ERROR, NULL);
1638 if (r < 0)
1639 return log_error_errno(r, "Failed to wait for systemd-vconsole-setup.service/restart: %m");
1640 return 0;
1641 }
1642
1643 static void end_marker(void) {
1644
1645 if (!welcome_done)
1646 return;
1647
1648 printf("\n%sExiting first boot settings tool.%s\n\n", ansi_grey(), ansi_normal());
1649 fflush(stdout);
1650 }
1651
1652 static int run(int argc, char *argv[]) {
1653 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1654 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
1655 _cleanup_(umount_and_freep) char *mounted_dir = NULL;
1656 _cleanup_close_ int rfd = -EBADF;
1657 int r;
1658
1659 r = parse_argv(argc, argv);
1660 if (r <= 0)
1661 return r;
1662
1663 log_setup();
1664
1665 umask(0022);
1666
1667 bool offline = arg_root || arg_image;
1668
1669 if (!offline) {
1670 /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1671 * command line option, because we are called to provision the host with basic settings (as
1672 * opposed to some other file system tree/image) */
1673
1674 bool enabled;
1675 r = proc_cmdline_get_bool("systemd.firstboot", /* flags= */ 0, &enabled);
1676 if (r < 0)
1677 return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
1678 if (r > 0 && !enabled) {
1679 log_debug("Found systemd.firstboot=no kernel command line argument, turning off all prompts.");
1680 arg_prompt_locale = arg_prompt_keymap = arg_prompt_keymap_auto = arg_prompt_timezone = arg_prompt_hostname = arg_prompt_root_password = arg_prompt_root_shell = false;
1681 }
1682 }
1683
1684 r = mac_init();
1685 if (r < 0)
1686 return r;
1687
1688 if (arg_image) {
1689 assert(!arg_root);
1690
1691 r = mount_image_privately_interactively(
1692 arg_image,
1693 arg_image_policy,
1694 DISSECT_IMAGE_GENERIC_ROOT |
1695 DISSECT_IMAGE_REQUIRE_ROOT |
1696 DISSECT_IMAGE_VALIDATE_OS |
1697 DISSECT_IMAGE_RELAX_VAR_CHECK |
1698 DISSECT_IMAGE_FSCK |
1699 DISSECT_IMAGE_GROWFS |
1700 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1701 &mounted_dir,
1702 &rfd,
1703 &loop_device);
1704 if (r < 0)
1705 return r;
1706
1707 arg_root = strdup(mounted_dir);
1708 if (!arg_root)
1709 return log_oom();
1710 } else {
1711 rfd = open(empty_to_root(arg_root), O_DIRECTORY|O_CLOEXEC);
1712 if (rfd < 0)
1713 return log_error_errno(errno, "Failed to open %s: %m", empty_to_root(arg_root));
1714 }
1715
1716 LOG_SET_PREFIX(arg_image ?: arg_root);
1717 DEFER_VOID_CALL(end_marker);
1718 DEFER_VOID_CALL(chrome_hide);
1719
1720 /* We check these conditions here instead of in parse_argv() so that we can take the root directory
1721 * into account. */
1722
1723 if (arg_keymap && !keymap_is_ok(arg_keymap, FD_TO_PTR(rfd)))
1724 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Keymap %s is not installed.", arg_keymap);
1725 if (arg_locale && !locale_is_ok(arg_locale, FD_TO_PTR(rfd)))
1726 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale);
1727 if (arg_locale_messages && !locale_is_ok(arg_locale_messages, FD_TO_PTR(rfd)))
1728 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale_messages);
1729
1730 if (arg_root_shell) {
1731 r = find_shell(rfd, arg_root_shell);
1732 if (r < 0)
1733 return r;
1734 }
1735
1736 r = process_reset(rfd);
1737 if (r < 0)
1738 return r;
1739
1740 _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *mute_console_link = NULL;
1741 r = process_locale(rfd, &mute_console_link);
1742 if (r < 0)
1743 return r;
1744 if (r > 0 && !offline)
1745 (void) reload_system_manager(&bus);
1746
1747 r = process_keymap(rfd, &mute_console_link);
1748 if (r < 0)
1749 return r;
1750 if (r > 0 && !offline)
1751 (void) reload_vconsole(&bus);
1752
1753 r = process_timezone(rfd, &mute_console_link);
1754 if (r < 0)
1755 return r;
1756
1757 r = process_hostname(rfd, &mute_console_link);
1758 if (r < 0)
1759 return r;
1760
1761 r = process_root_account(rfd, &mute_console_link);
1762 if (r < 0)
1763 return r;
1764
1765 r = process_kernel_cmdline(rfd);
1766 if (r < 0)
1767 return r;
1768
1769 r = process_machine_id(rfd);
1770 if (r < 0)
1771 return r;
1772
1773 r = process_machine_tags(rfd);
1774 if (r < 0)
1775 return r;
1776
1777 return 0;
1778 }
1779
1780 DEFINE_MAIN_FUNCTION(run);