]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/firstboot/firstboot.c
units: make sure proc-sys-binfmt_misc.automount is actually stopped
[thirdparty/systemd.git] / src / firstboot / firstboot.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
418b9be5 2
418b9be5 3#include <fcntl.h>
418b9be5 4#include <getopt.h>
3ff9fa59 5#include <linux/loop.h>
3f6fd1ba 6#include <unistd.h>
418b9be5 7
dccca82b
LP
8#include "sd-id128.h"
9
b5efdb8a 10#include "alloc-util.h"
3f6fd1ba 11#include "ask-password-api.h"
d6b4d1c7 12#include "build.h"
f461a28d 13#include "chase.h"
418b9be5 14#include "copy.h"
416f7b3a 15#include "creds-util.h"
3ff9fa59 16#include "dissect-image.h"
686d13b9 17#include "env-file.h"
6bedfcbb 18#include "fd-util.h"
3f6fd1ba 19#include "fileio.h"
f4f15635 20#include "fs-util.h"
d8e32c47 21#include "glyph-util.h"
3f6fd1ba 22#include "hostname-util.h"
f05e1d0d 23#include "kbd-util.h"
42f3b2f9 24#include "libcrypt-util.h"
3f6fd1ba 25#include "locale-util.h"
b352e545 26#include "lock-util.h"
45f39418 27#include "main-func.h"
9ae4ef49 28#include "memory-util.h"
418b9be5 29#include "mkdir.h"
3ff9fa59 30#include "mount-util.h"
d58ad743 31#include "os-util.h"
614b022c 32#include "parse-argument.h"
6bedfcbb 33#include "parse-util.h"
418b9be5 34#include "path-util.h"
294bf0c3 35#include "pretty-print.h"
f582cbca 36#include "proc-cmdline.h"
7baf10a7 37#include "pwquality-util.h"
3df3e884 38#include "random-util.h"
ce0458be 39#include "smack-util.h"
6bedfcbb 40#include "string-util.h"
3f6fd1ba 41#include "strv.h"
288a74cc 42#include "terminal-util.h"
3f6fd1ba 43#include "time-util.h"
b4909a3f 44#include "tmpfile-util-label.h"
3ff9fa59 45#include "tmpfile-util.h"
affb60b1 46#include "umask-util.h"
e929bee0 47#include "user-util.h"
418b9be5
LP
48
49static char *arg_root = NULL;
3ff9fa59 50static char *arg_image = NULL;
418b9be5
LP
51static char *arg_locale = NULL; /* $LANG */
52static char *arg_locale_messages = NULL; /* $LC_MESSAGES */
f8fd0930 53static char *arg_keymap = NULL;
418b9be5
LP
54static char *arg_timezone = NULL;
55static char *arg_hostname = NULL;
56static sd_id128_t arg_machine_id = {};
57static char *arg_root_password = NULL;
28900a1b 58static char *arg_root_shell = NULL;
a5925354 59static char *arg_kernel_cmdline = NULL;
418b9be5 60static bool arg_prompt_locale = false;
ed457f13 61static bool arg_prompt_keymap = false;
418b9be5
LP
62static bool arg_prompt_timezone = false;
63static bool arg_prompt_hostname = false;
64static bool arg_prompt_root_password = false;
28900a1b 65static bool arg_prompt_root_shell = false;
418b9be5 66static bool arg_copy_locale = false;
ed457f13 67static bool arg_copy_keymap = false;
418b9be5
LP
68static bool arg_copy_timezone = false;
69static bool arg_copy_root_password = false;
28900a1b 70static bool arg_copy_root_shell = false;
b4909a3f 71static bool arg_force = false;
4926ceaf 72static bool arg_delete_root_password = false;
676339a1 73static bool arg_root_password_is_hashed = false;
a1225020 74static bool arg_welcome = true;
05eb2c60 75static bool arg_reset = false;
84be0c71 76static ImagePolicy *arg_image_policy = NULL;
418b9be5 77
45f39418 78STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
3ff9fa59 79STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
45f39418
YW
80STATIC_DESTRUCTOR_REGISTER(arg_locale, freep);
81STATIC_DESTRUCTOR_REGISTER(arg_locale_messages, freep);
82STATIC_DESTRUCTOR_REGISTER(arg_keymap, freep);
83STATIC_DESTRUCTOR_REGISTER(arg_timezone, freep);
84STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep);
9ae4ef49 85STATIC_DESTRUCTOR_REGISTER(arg_root_password, erase_and_freep);
84be0c71 86STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
45f39418 87
418b9be5
LP
88static bool press_any_key(void) {
89 char k = 0;
90 bool need_nl = true;
91
92 printf("-- Press any key to proceed --");
93 fflush(stdout);
94
94956f8f 95 (void) read_one_char(stdin, &k, USEC_INFINITY, &need_nl);
418b9be5
LP
96
97 if (need_nl)
98 putchar('\n');
99
100 return k != 'q';
101}
102
a0657479 103static void print_welcome(int rfd) {
02b7005e 104 _cleanup_free_ char *pretty_name = NULL, *os_name = NULL, *ansi_color = NULL;
418b9be5 105 static bool done = false;
ae0d36c1 106 const char *pn, *ac;
418b9be5
LP
107 int r;
108
a0657479
DDM
109 assert(rfd >= 0);
110
a1225020
LP
111 if (!arg_welcome)
112 return;
113
418b9be5
LP
114 if (done)
115 return;
116
a0657479
DDM
117 r = parse_os_release_at(rfd,
118 "PRETTY_NAME", &pretty_name,
119 "NAME", &os_name,
120 "ANSI_COLOR", &ansi_color);
d58ad743
LP
121 if (r < 0)
122 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
123 "Failed to read os-release file, ignoring: %m");
418b9be5 124
02b7005e 125 pn = os_release_pretty_name(pretty_name, os_name);
ae0d36c1 126 ac = isempty(ansi_color) ? "0" : ansi_color;
4aeadec7
LP
127
128 if (colors_enabled())
ae0d36c1 129 printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ac, pn);
4aeadec7
LP
130 else
131 printf("\nWelcome to your new installation of %s!\n", pn);
132
133 printf("\nPlease configure your system!\n\n");
418b9be5
LP
134
135 press_any_key();
136
137 done = true;
138}
139
140static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) {
418b9be5 141 unsigned break_lines, break_modulo;
da6053d0 142 size_t n, per_column, i, j;
418b9be5
LP
143
144 assert(n_columns > 0);
145
146 n = strv_length(x);
be6b0c21 147 per_column = DIV_ROUND_UP(n, n_columns);
418b9be5
LP
148
149 break_lines = lines();
150 if (break_lines > 2)
151 break_lines--;
152
153 /* The first page gets two extra lines, since we want to show
154 * a title */
155 break_modulo = break_lines;
156 if (break_modulo > 3)
157 break_modulo -= 3;
158
159 for (i = 0; i < per_column; i++) {
160
161 for (j = 0; j < n_columns; j ++) {
162 _cleanup_free_ char *e = NULL;
163
164 if (j * per_column + i >= n)
165 break;
166
167 e = ellipsize(x[j * per_column + i], width, percentage);
168 if (!e)
169 return log_oom();
170
f996072f 171 printf("%4zu) %-*s", j * per_column + i + 1, (int) width, e);
418b9be5
LP
172 }
173
174 putchar('\n');
175
176 /* on the first screen we reserve 2 extra lines for the title */
177 if (i % break_lines == break_modulo) {
178 if (!press_any_key())
179 return 0;
180 }
181 }
182
183 return 0;
184}
185
ecada8f2 186static int prompt_loop(const char *text, char **l, unsigned percentage, bool (*is_valid)(const char *name), char **ret) {
418b9be5
LP
187 int r;
188
189 assert(text);
190 assert(is_valid);
191 assert(ret);
192
193 for (;;) {
194 _cleanup_free_ char *p = NULL;
195 unsigned u;
196
ecada8f2
ZJS
197 r = ask_string(&p, "%s %s (empty to skip, \"list\" to list options): ",
198 special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), text);
23bbb0de
MS
199 if (r < 0)
200 return log_error_errno(r, "Failed to query user: %m");
418b9be5
LP
201
202 if (isempty(p)) {
203 log_warning("No data entered, skipping.");
204 return 0;
205 }
206
ecada8f2
ZJS
207 if (streq(p, "list")) {
208 r = show_menu(l, 3, 22, percentage);
209 if (r < 0)
210 return r;
211
212 putchar('\n');
213 continue;
214 };
215
418b9be5
LP
216 r = safe_atou(p, &u);
217 if (r >= 0) {
418b9be5
LP
218 if (u <= 0 || u > strv_length(l)) {
219 log_error("Specified entry number out of range.");
220 continue;
221 }
222
223 log_info("Selected '%s'.", l[u-1]);
b3f9c17a 224 return free_and_strdup_warn(ret, l[u-1]);
418b9be5
LP
225 }
226
227 if (!is_valid(p)) {
228 log_error("Entered data invalid.");
229 continue;
230 }
231
bfbf5f74 232 return free_and_replace(*ret, p);
418b9be5
LP
233 }
234}
235
b352e545 236static int should_configure(int dir_fd, const char *filename) {
b352e545
DDM
237 assert(dir_fd >= 0);
238 assert(filename);
239
df00c516
DDM
240 if (faccessat(dir_fd, filename, F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
241 if (errno != ENOENT)
242 return log_error_errno(errno, "Failed to access %s: %m", filename);
243
244 return true; /* missing */
245 }
b352e545 246
df00c516 247 return arg_force; /* exists, but if --force was given we should still configure the file. */
b352e545
DDM
248}
249
a0657479
DDM
250static bool locale_is_installed_bool(const char *name) {
251 return locale_is_installed(name) > 0;
252}
a00a78b8 253
a0657479
DDM
254static bool locale_is_ok(int rfd, const char *name) {
255 assert(rfd >= 0);
a00a78b8 256
a0657479 257 return dir_fd_is_root(rfd) ? locale_is_installed_bool(name) : locale_is_valid(name);
a00a78b8
LP
258}
259
a0657479 260static int prompt_locale(int rfd) {
418b9be5 261 _cleanup_strv_free_ char **locales = NULL;
416f7b3a 262 bool acquired_from_creds = false;
418b9be5
LP
263 int r;
264
a0657479
DDM
265 assert(rfd >= 0);
266
418b9be5
LP
267 if (arg_locale || arg_locale_messages)
268 return 0;
269
416f7b3a
LP
270 r = read_credential("firstboot.locale", (void**) &arg_locale, NULL);
271 if (r < 0)
272 log_debug_errno(r, "Failed to read credential firstboot.locale, ignoring: %m");
273 else
274 acquired_from_creds = true;
275
276 r = read_credential("firstboot.locale-messages", (void**) &arg_locale_messages, NULL);
277 if (r < 0)
278 log_debug_errno(r, "Failed to read credential firstboot.locale-message, ignoring: %m");
279 else
280 acquired_from_creds = true;
281
282 if (acquired_from_creds) {
283 log_debug("Acquired locale from credentials.");
284 return 0;
285 }
286
eb650ffe
ZJS
287 if (!arg_prompt_locale) {
288 log_debug("Prompting for locale was not requested.");
418b9be5 289 return 0;
eb650ffe 290 }
418b9be5
LP
291
292 r = get_locales(&locales);
23bbb0de
MS
293 if (r < 0)
294 return log_error_errno(r, "Cannot query locales list: %m");
418b9be5 295
bdd7cd26
LP
296 if (strv_isempty(locales))
297 log_debug("No locales found, skipping locale selection.");
298 else if (strv_length(locales) == 1) {
418b9be5 299
bdd7cd26
LP
300 if (streq(locales[0], SYSTEMD_DEFAULT_LOCALE))
301 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
302 else {
303 log_debug("Only a single locale available (%s), selecting it as default.", locales[0]);
418b9be5 304
bdd7cd26
LP
305 arg_locale = strdup(locales[0]);
306 if (!arg_locale)
307 return log_oom();
418b9be5 308
bdd7cd26
LP
309 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
310 }
311 } else {
a0657479
DDM
312 bool (*is_valid)(const char *name) = dir_fd_is_root(rfd) ? locale_is_installed_bool
313 : locale_is_valid;
314
315 print_welcome(rfd);
418b9be5 316
ecada8f2 317 r = prompt_loop("Please enter system locale name or number",
a0657479 318 locales, 60, is_valid, &arg_locale);
bdd7cd26
LP
319 if (r < 0)
320 return r;
321
322 if (isempty(arg_locale))
323 return 0;
324
ecada8f2 325 r = prompt_loop("Please enter system message locale name or number",
a0657479 326 locales, 60, is_valid, &arg_locale_messages);
bdd7cd26
LP
327 if (r < 0)
328 return r;
329
330 /* Suppress the messages setting if it's the same as the main locale anyway */
331 if (streq_ptr(arg_locale, arg_locale_messages))
332 arg_locale_messages = mfree(arg_locale_messages);
333 }
418b9be5
LP
334
335 return 0;
336}
337
b352e545
DDM
338static int process_locale(int rfd) {
339 _cleanup_close_ int pfd = -EBADF;
340 _cleanup_free_ char *f = NULL;
418b9be5
LP
341 char* locales[3];
342 unsigned i = 0;
343 int r;
344
b352e545
DDM
345 assert(rfd >= 0);
346
347 pfd = chase_and_open_parent_at(rfd, "/etc/locale.conf",
b39710cc 348 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
b352e545
DDM
349 &f);
350 if (pfd < 0)
351 return log_error_errno(pfd, "Failed to chase /etc/locale.conf: %m");
418b9be5 352
b352e545
DDM
353 r = should_configure(pfd, f);
354 if (r == 0)
355 log_debug("Found /etc/locale.conf, assuming locale information has been configured.");
356 if (r <= 0)
357 return r;
418b9be5 358
fe75d5bc
DDM
359 r = dir_fd_is_root(rfd);
360 if (r < 0)
361 return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
362
363 if (arg_copy_locale && r == 0) {
b352e545 364 r = copy_file_atomic_at(AT_FDCWD, "/etc/locale.conf", pfd, f, 0644, COPY_REFLINK);
418b9be5 365 if (r != -ENOENT) {
23bbb0de 366 if (r < 0)
b352e545 367 return log_error_errno(r, "Failed to copy host's /etc/locale.conf: %m");
418b9be5 368
b352e545 369 log_info("Copied host's /etc/locale.conf.");
418b9be5
LP
370 return 0;
371 }
372 }
373
a0657479 374 r = prompt_locale(rfd);
418b9be5
LP
375 if (r < 0)
376 return r;
377
378 if (!isempty(arg_locale))
63c372cb 379 locales[i++] = strjoina("LANG=", arg_locale);
4c4a73ce 380 if (!isempty(arg_locale_messages) && !streq_ptr(arg_locale_messages, arg_locale))
63c372cb 381 locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages);
418b9be5
LP
382
383 if (i == 0)
384 return 0;
385
386 locales[i] = NULL;
387
b352e545 388 r = write_env_file_at(pfd, f, locales);
23bbb0de 389 if (r < 0)
b352e545 390 return log_error_errno(r, "Failed to write /etc/locale.conf: %m");
418b9be5 391
b352e545 392 log_info("/etc/locale.conf written.");
418b9be5
LP
393 return 0;
394}
395
a0657479 396static int prompt_keymap(int rfd) {
ed457f13
TB
397 _cleanup_strv_free_ char **kmaps = NULL;
398 int r;
399
a0657479
DDM
400 assert(rfd >= 0);
401
ed457f13
TB
402 if (arg_keymap)
403 return 0;
404
416f7b3a
LP
405 r = read_credential("firstboot.keymap", (void**) &arg_keymap, NULL);
406 if (r < 0)
407 log_debug_errno(r, "Failed to read credential firstboot.keymap, ignoring: %m");
408 else {
409 log_debug("Acquired keymap from credential.");
410 return 0;
411 }
412
eb650ffe
ZJS
413 if (!arg_prompt_keymap) {
414 log_debug("Prompting for keymap was not requested.");
ed457f13 415 return 0;
eb650ffe 416 }
ed457f13
TB
417
418 r = get_keymaps(&kmaps);
419 if (r == -ENOENT) /* no keymaps installed */
eb650ffe 420 return log_debug_errno(r, "No keymaps are installed.");
ed457f13
TB
421 if (r < 0)
422 return log_error_errno(r, "Failed to read keymaps: %m");
423
a0657479 424 print_welcome(rfd);
ed457f13 425
8700b4da 426 return prompt_loop("Please enter system keymap name or number",
ecada8f2 427 kmaps, 60, keymap_is_valid, &arg_keymap);
ed457f13
TB
428}
429
b352e545
DDM
430static int process_keymap(int rfd) {
431 _cleanup_close_ int pfd = -EBADF;
432 _cleanup_free_ char *f = NULL;
ed457f13
TB
433 char **keymap;
434 int r;
435
b352e545 436 assert(rfd >= 0);
ed457f13 437
b352e545 438 pfd = chase_and_open_parent_at(rfd, "/etc/vconsole.conf",
b39710cc 439 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
b352e545
DDM
440 &f);
441 if (pfd < 0)
442 return log_error_errno(pfd, "Failed to chase /etc/vconsole.conf: %m");
ed457f13 443
b352e545
DDM
444 r = should_configure(pfd, f);
445 if (r == 0)
446 log_debug("Found /etc/vconsole.conf, assuming console has been configured.");
447 if (r <= 0)
448 return r;
449
fe75d5bc
DDM
450 r = dir_fd_is_root(rfd);
451 if (r < 0)
452 return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
453
454 if (arg_copy_keymap && r == 0) {
b352e545 455 r = copy_file_atomic_at(AT_FDCWD, "/etc/vconsole.conf", pfd, f, 0644, COPY_REFLINK);
ed457f13
TB
456 if (r != -ENOENT) {
457 if (r < 0)
b352e545 458 return log_error_errno(r, "Failed to copy host's /etc/vconsole.conf: %m");
ed457f13 459
b352e545 460 log_info("Copied host's /etc/vconsole.conf.");
ed457f13
TB
461 return 0;
462 }
463 }
464
a0657479 465 r = prompt_keymap(rfd);
ed457f13
TB
466 if (r == -ENOENT)
467 return 0; /* don't fail if no keymaps are installed */
468 if (r < 0)
469 return r;
470
a7353b4d 471 if (isempty(arg_keymap))
ed457f13
TB
472 return 0;
473
a7353b4d
YW
474 keymap = STRV_MAKE(strjoina("KEYMAP=", arg_keymap));
475
b352e545 476 r = write_env_file_at(pfd, f, keymap);
ed457f13 477 if (r < 0)
b352e545 478 return log_error_errno(r, "Failed to write /etc/vconsole.conf: %m");
ed457f13 479
b352e545 480 log_info("/etc/vconsole.conf written.");
ed457f13
TB
481 return 0;
482}
483
089fb865
MG
484static bool timezone_is_valid_log_error(const char *name) {
485 return timezone_is_valid(name, LOG_ERR);
486}
487
a0657479 488static int prompt_timezone(int rfd) {
418b9be5
LP
489 _cleanup_strv_free_ char **zones = NULL;
490 int r;
491
a0657479
DDM
492 assert(rfd >= 0);
493
418b9be5
LP
494 if (arg_timezone)
495 return 0;
496
416f7b3a
LP
497 r = read_credential("firstboot.timezone", (void**) &arg_timezone, NULL);
498 if (r < 0)
499 log_debug_errno(r, "Failed to read credential firstboot.timezone, ignoring: %m");
500 else {
501 log_debug("Acquired timezone from credential.");
502 return 0;
503 }
504
eb650ffe
ZJS
505 if (!arg_prompt_timezone) {
506 log_debug("Prompting for timezone was not requested.");
418b9be5 507 return 0;
eb650ffe 508 }
418b9be5
LP
509
510 r = get_timezones(&zones);
23bbb0de
MS
511 if (r < 0)
512 return log_error_errno(r, "Cannot query timezone list: %m");
418b9be5 513
a0657479 514 print_welcome(rfd);
418b9be5 515
ecada8f2
ZJS
516 r = prompt_loop("Please enter timezone name or number",
517 zones, 30, timezone_is_valid_log_error, &arg_timezone);
418b9be5
LP
518 if (r < 0)
519 return r;
520
521 return 0;
522}
523
b352e545
DDM
524static int process_timezone(int rfd) {
525 _cleanup_close_ int pfd = -EBADF;
526 _cleanup_free_ char *f = NULL;
527 const char *e;
418b9be5
LP
528 int r;
529
b352e545 530 assert(rfd >= 0);
418b9be5 531
b352e545
DDM
532 pfd = chase_and_open_parent_at(rfd, "/etc/localtime",
533 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
534 &f);
535 if (pfd < 0)
536 return log_error_errno(pfd, "Failed to chase /etc/localtime: %m");
418b9be5 537
b352e545
DDM
538 r = should_configure(pfd, f);
539 if (r == 0)
540 log_debug("Found /etc/localtime, assuming timezone has been configured.");
541 if (r <= 0)
542 return r;
543
fe75d5bc
DDM
544 r = dir_fd_is_root(rfd);
545 if (r < 0)
546 return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
547
548 if (arg_copy_timezone && r == 0) {
b352e545
DDM
549 _cleanup_free_ char *s = NULL;
550
551 r = readlink_malloc("/etc/localtime", &s);
418b9be5 552 if (r != -ENOENT) {
23bbb0de 553 if (r < 0)
b352e545 554 return log_error_errno(r, "Failed to read host's /etc/localtime: %m");
418b9be5 555
b352e545 556 r = symlinkat_atomic_full(s, pfd, f, /* make_relative= */ false);
e56dc320 557 if (r < 0)
b352e545 558 return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
418b9be5 559
b352e545 560 log_info("Copied host's /etc/localtime.");
418b9be5
LP
561 return 0;
562 }
563 }
564
a0657479 565 r = prompt_timezone(rfd);
418b9be5
LP
566 if (r < 0)
567 return r;
568
569 if (isempty(arg_timezone))
570 return 0;
571
63c372cb 572 e = strjoina("../usr/share/zoneinfo/", arg_timezone);
418b9be5 573
b352e545 574 r = symlinkat_atomic_full(e, pfd, f, /* make_relative= */ false);
e56dc320 575 if (r < 0)
b352e545 576 return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
418b9be5 577
b352e545 578 log_info("/etc/localtime written");
418b9be5
LP
579 return 0;
580}
581
a0657479 582static int prompt_hostname(int rfd) {
418b9be5
LP
583 int r;
584
a0657479
DDM
585 assert(rfd >= 0);
586
418b9be5
LP
587 if (arg_hostname)
588 return 0;
589
eb650ffe
ZJS
590 if (!arg_prompt_hostname) {
591 log_debug("Prompting for hostname was not requested.");
418b9be5 592 return 0;
eb650ffe 593 }
418b9be5 594
a0657479 595 print_welcome(rfd);
418b9be5
LP
596 putchar('\n');
597
598 for (;;) {
599 _cleanup_free_ char *h = NULL;
600
9a6f746f 601 r = ask_string(&h, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET));
23bbb0de
MS
602 if (r < 0)
603 return log_error_errno(r, "Failed to query hostname: %m");
418b9be5
LP
604
605 if (isempty(h)) {
606 log_warning("No hostname entered, skipping.");
607 break;
608 }
609
52ef5dd7 610 if (!hostname_is_valid(h, VALID_HOSTNAME_TRAILING_DOT)) {
418b9be5
LP
611 log_error("Specified hostname invalid.");
612 continue;
613 }
614
34ad6090 615 /* Get rid of the trailing dot that we allow, but don't want to see */
ae691c1d 616 arg_hostname = hostname_cleanup(h);
418b9be5
LP
617 h = NULL;
618 break;
619 }
620
621 return 0;
622}
623
b352e545
DDM
624static int process_hostname(int rfd) {
625 _cleanup_close_ int pfd = -EBADF;
626 _cleanup_free_ char *f = NULL;
418b9be5
LP
627 int r;
628
b352e545
DDM
629 assert(rfd >= 0);
630
631 pfd = chase_and_open_parent_at(rfd, "/etc/hostname",
632 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN,
633 &f);
634 if (pfd < 0)
635 return log_error_errno(pfd, "Failed to chase /etc/hostname: %m");
636
637 r = should_configure(pfd, f);
638 if (r == 0)
639 log_debug("Found /etc/hostname, assuming hostname has been configured.");
640 if (r <= 0)
641 return r;
418b9be5 642
a0657479 643 r = prompt_hostname(rfd);
418b9be5
LP
644 if (r < 0)
645 return r;
646
647 if (isempty(arg_hostname))
648 return 0;
649
b352e545
DDM
650 r = write_string_file_at(pfd, f, arg_hostname,
651 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC);
23bbb0de 652 if (r < 0)
b352e545 653 return log_error_errno(r, "Failed to write /etc/hostname: %m");
418b9be5 654
b352e545 655 log_info("/etc/hostname written.");
418b9be5
LP
656 return 0;
657}
658
b352e545
DDM
659static int process_machine_id(int rfd) {
660 _cleanup_close_ int pfd = -EBADF;
661 _cleanup_free_ char *f = NULL;
418b9be5
LP
662 int r;
663
b352e545
DDM
664 assert(rfd >= 0);
665
666 pfd = chase_and_open_parent_at(rfd, "/etc/machine-id",
b39710cc 667 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
b352e545
DDM
668 &f);
669 if (pfd < 0)
670 return log_error_errno(pfd, "Failed to chase /etc/machine-id: %m");
671
672 r = should_configure(pfd, f);
673 if (r == 0)
674 log_debug("Found /etc/machine-id, assuming machine-id has been configured.");
675 if (r <= 0)
676 return r;
418b9be5 677
eb650ffe
ZJS
678 if (sd_id128_is_null(arg_machine_id)) {
679 log_debug("Initialization of machine-id was not requested, skipping.");
418b9be5 680 return 0;
eb650ffe 681 }
418b9be5 682
b352e545
DDM
683 r = write_string_file_at(pfd, "machine-id", SD_ID128_TO_STRING(arg_machine_id),
684 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC);
23bbb0de 685 if (r < 0)
b352e545 686 return log_error_errno(r, "Failed to write /etc/machine id: %m");
418b9be5 687
b352e545 688 log_info("/etc/machine-id written.");
418b9be5
LP
689 return 0;
690}
691
a0657479 692static int prompt_root_password(int rfd) {
1fbc95d3 693 const char *msg1, *msg2;
418b9be5
LP
694 int r;
695
a0657479
DDM
696 assert(rfd >= 0);
697
418b9be5
LP
698 if (arg_root_password)
699 return 0;
700
ff86850b 701 if (get_credential_user_password("root", &arg_root_password, &arg_root_password_is_hashed) >= 0)
416f7b3a 702 return 0;
416f7b3a 703
eb650ffe
ZJS
704 if (!arg_prompt_root_password) {
705 log_debug("Prompting for root password was not requested.");
418b9be5 706 return 0;
eb650ffe 707 }
418b9be5 708
a0657479 709 print_welcome(rfd);
418b9be5
LP
710 putchar('\n');
711
3619634c
LP
712 msg1 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter a new root password (empty to skip):");
713 msg2 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter new root password again:");
418b9be5 714
7baf10a7
LP
715 suggest_passwords();
716
418b9be5 717 for (;;) {
c7b7d74e 718 _cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
7baf10a7 719 _cleanup_free_ char *error = NULL;
418b9be5 720
daa55720 721 r = ask_password_tty(-1, msg1, NULL, 0, 0, NULL, &a);
23bbb0de
MS
722 if (r < 0)
723 return log_error_errno(r, "Failed to query root password: %m");
39e96f84
ZJS
724 if (strv_length(a) != 1)
725 return log_error_errno(SYNTHETIC_ERRNO(EIO),
726 "Received multiple passwords, where we expected one.");
418b9be5 727
c7b7d74e 728 if (isempty(*a)) {
418b9be5
LP
729 log_warning("No password entered, skipping.");
730 break;
731 }
732
7baf10a7
LP
733 r = quality_check_password(*a, "root", &error);
734 if (r < 0)
735 return log_error_errno(r, "Failed to check quality of password: %m");
736 if (r == 0)
737 log_warning("Password is weak, accepting anyway: %s", error);
738
daa55720 739 r = ask_password_tty(-1, msg2, NULL, 0, 0, NULL, &b);
ab84f5b9 740 if (r < 0)
00843602 741 return log_error_errno(r, "Failed to query root password: %m");
39e96f84
ZJS
742 if (strv_length(b) != 1)
743 return log_error_errno(SYNTHETIC_ERRNO(EIO),
744 "Received multiple passwords, where we expected one.");
418b9be5 745
c7b7d74e 746 if (!streq(*a, *b)) {
418b9be5 747 log_error("Entered passwords did not match, please try again.");
418b9be5
LP
748 continue;
749 }
750
c7b7d74e 751 arg_root_password = TAKE_PTR(*a);
418b9be5
LP
752 break;
753 }
754
755 return 0;
756}
757
b352e545 758static int find_shell(int rfd, const char *path) {
31363bd5
DDM
759 int r;
760
761 assert(path);
762
763 if (!valid_shell(path))
764 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "%s is not a valid shell", path);
765
b352e545
DDM
766 r = chaseat(rfd, path, CHASE_AT_RESOLVE_IN_ROOT, NULL, NULL);
767 if (r < 0)
768 return log_error_errno(r, "Failed to resolve shell %s: %m", path);
31363bd5
DDM
769
770 return 0;
771}
772
b352e545 773static int prompt_root_shell(int rfd) {
28900a1b
DDM
774 int r;
775
a0657479
DDM
776 assert(rfd >= 0);
777
416f7b3a
LP
778 if (arg_root_shell)
779 return 0;
780
781 r = read_credential("passwd.shell.root", (void**) &arg_root_shell, NULL);
782 if (r < 0)
783 log_debug_errno(r, "Failed to read credential passwd.shell.root, ignoring: %m");
784 else {
785 log_debug("Acquired root shell from credential.");
786 return 0;
787 }
788
eb650ffe
ZJS
789 if (!arg_prompt_root_shell) {
790 log_debug("Prompting for root shell was not requested.");
28900a1b 791 return 0;
eb650ffe 792 }
28900a1b 793
a0657479 794 print_welcome(rfd);
28900a1b
DDM
795 putchar('\n');
796
797 for (;;) {
798 _cleanup_free_ char *s = NULL;
799
800 r = ask_string(&s, "%s Please enter root shell for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET));
801 if (r < 0)
802 return log_error_errno(r, "Failed to query root shell: %m");
803
804 if (isempty(s)) {
805 log_warning("No shell entered, skipping.");
806 break;
807 }
808
b352e545 809 r = find_shell(rfd, s);
31363bd5 810 if (r < 0)
28900a1b 811 continue;
28900a1b
DDM
812
813 arg_root_shell = TAKE_PTR(s);
814 break;
815 }
816
817 return 0;
818}
819
a0657479 820static int write_root_passwd(int rfd, int etc_fd, const char *password, const char *shell) {
4926ceaf
DDM
821 _cleanup_fclose_ FILE *original = NULL, *passwd = NULL;
822 _cleanup_(unlink_and_freep) char *passwd_tmp = NULL;
823 int r;
824
c4a53ebf
DDM
825 assert(password);
826
b352e545 827 r = fopen_temporary_at_label(etc_fd, "passwd", "passwd", &passwd, &passwd_tmp);
4926ceaf
DDM
828 if (r < 0)
829 return r;
830
b352e545
DDM
831 r = xfopenat(etc_fd, "passwd", "re", O_NOFOLLOW, &original);
832 if (r < 0 && r != -ENOENT)
833 return r;
834
4926ceaf
DDM
835 if (original) {
836 struct passwd *i;
837
bb72c434 838 r = copy_rights(fileno(original), fileno(passwd));
4926ceaf
DDM
839 if (r < 0)
840 return r;
841
842 while ((r = fgetpwent_sane(original, &i)) > 0) {
843
28900a1b 844 if (streq(i->pw_name, "root")) {
4926ceaf 845 i->pw_passwd = (char *) password;
28900a1b
DDM
846 if (shell)
847 i->pw_shell = (char *) shell;
848 }
4926ceaf
DDM
849
850 r = putpwent_sane(i, passwd);
851 if (r < 0)
852 return r;
853 }
854 if (r < 0)
855 return r;
856
857 } else {
858 struct passwd root = {
859 .pw_name = (char *) "root",
860 .pw_passwd = (char *) password,
861 .pw_uid = 0,
862 .pw_gid = 0,
863 .pw_gecos = (char *) "Super User",
864 .pw_dir = (char *) "/root",
a0657479 865 .pw_shell = (char *) (shell ?: default_root_shell_at(rfd)),
4926ceaf
DDM
866 };
867
868 if (errno != ENOENT)
869 return -errno;
870
b226422c 871 r = fchmod(fileno(passwd), 0644);
4926ceaf
DDM
872 if (r < 0)
873 return -errno;
874
875 r = putpwent_sane(&root, passwd);
876 if (r < 0)
877 return r;
878 }
879
880 r = fflush_sync_and_check(passwd);
881 if (r < 0)
882 return r;
883
b352e545 884 r = renameat_and_apply_smack_floor_label(etc_fd, passwd_tmp, etc_fd, "passwd");
4926ceaf
DDM
885 if (r < 0)
886 return r;
887
888 return 0;
889}
890
b352e545 891static int write_root_shadow(int etc_fd, const char *hashed_password) {
b4909a3f
DDM
892 _cleanup_fclose_ FILE *original = NULL, *shadow = NULL;
893 _cleanup_(unlink_and_freep) char *shadow_tmp = NULL;
100d5f6e
FB
894 int r;
895
c4a53ebf
DDM
896 assert(hashed_password);
897
b352e545 898 r = fopen_temporary_at_label(etc_fd, "shadow", "shadow", &shadow, &shadow_tmp);
b4909a3f
DDM
899 if (r < 0)
900 return r;
901
b352e545
DDM
902 r = xfopenat(etc_fd, "shadow", "re", O_NOFOLLOW, &original);
903 if (r < 0 && r != -ENOENT)
904 return r;
905
b4909a3f
DDM
906 if (original) {
907 struct spwd *i;
908
bb72c434 909 r = copy_rights(fileno(original), fileno(shadow));
b4909a3f
DDM
910 if (r < 0)
911 return r;
912
913 while ((r = fgetspent_sane(original, &i)) > 0) {
914
915 if (streq(i->sp_namp, "root")) {
916 i->sp_pwdp = (char *) hashed_password;
917 i->sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
918 }
919
920 r = putspent_sane(i, shadow);
921 if (r < 0)
922 return r;
923 }
924 if (r < 0)
925 return r;
926
927 } else {
928 struct spwd root = {
929 .sp_namp = (char*) "root",
930 .sp_pwdp = (char *) hashed_password,
931 .sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY),
932 .sp_min = -1,
933 .sp_max = -1,
934 .sp_warn = -1,
935 .sp_inact = -1,
936 .sp_expire = -1,
f5fbe71d 937 .sp_flag = ULONG_MAX, /* this appears to be what everybody does ... */
b4909a3f
DDM
938 };
939
940 if (errno != ENOENT)
941 return -errno;
418b9be5 942
b4909a3f
DDM
943 r = fchmod(fileno(shadow), 0000);
944 if (r < 0)
945 return -errno;
418b9be5 946
b4909a3f
DDM
947 r = putspent_sane(&root, shadow);
948 if (r < 0)
949 return r;
950 }
951
952 r = fflush_sync_and_check(shadow);
100d5f6e
FB
953 if (r < 0)
954 return r;
418b9be5 955
b352e545 956 r = renameat_and_apply_smack_floor_label(etc_fd, shadow_tmp, etc_fd, "shadow");
b4909a3f
DDM
957 if (r < 0)
958 return r;
959
960 return 0;
418b9be5
LP
961}
962
b352e545
DDM
963static int process_root_account(int rfd) {
964 _cleanup_close_ int pfd = -EBADF;
965 _cleanup_(release_lock_file) LockFile lock = LOCK_FILE_INIT;
0e98d17e 966 _cleanup_(erase_and_freep) char *_hashed_password = NULL;
c4a53ebf 967 const char *password, *hashed_password;
b352e545
DDM
968 int k = 0, r;
969
970 assert(rfd >= 0);
971
972 pfd = chase_and_open_parent_at(rfd, "/etc/passwd",
973 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
974 NULL);
975 if (pfd < 0)
976 return log_error_errno(pfd, "Failed to chase /etc/passwd: %m");
977
978 /* Ensure that passwd and shadow are in the same directory and are not symlinks. */
979
980 FOREACH_STRING(s, "passwd", "shadow") {
981 r = verify_regular_at(pfd, s, /* follow = */ false);
982 if (IN_SET(r, -EISDIR, -ELOOP, -EBADFD))
983 return log_error_errno(r, "/etc/%s is not a regular file", s);
984 if (r < 0 && r != -ENOENT)
985 return log_error_errno(r, "Failed to check whether /etc/%s is a regular file: %m", s);
986
987 r = should_configure(pfd, s);
988 if (r < 0)
989 return r;
418b9be5 990
b352e545
DDM
991 k += r;
992 }
c4a53ebf 993
b352e545
DDM
994 if (k == 0) {
995 log_debug("Found /etc/passwd and /etc/shadow, assuming root account has been initialized.");
418b9be5 996 return 0;
eb650ffe 997 }
87a3a4a8 998
67d5d9d5 999 /* Don't create/modify passwd and shadow if not asked */
1000 if (!(arg_root_password || arg_prompt_root_password || arg_copy_root_password || arg_delete_root_password ||
eb650ffe
ZJS
1001 arg_root_shell || arg_prompt_root_shell || arg_copy_root_shell)) {
1002 log_debug("Initialization of root account was not requested, skipping.");
67d5d9d5 1003 return 0;
eb650ffe 1004 }
418b9be5 1005
fe585662 1006 r = make_lock_file_at(pfd, ETC_PASSWD_LOCK_FILENAME, LOCK_EX, &lock);
b352e545
DDM
1007 if (r < 0)
1008 return log_error_errno(r, "Failed to take a lock on /etc/passwd: %m");
4926ceaf 1009
fe75d5bc
DDM
1010 k = dir_fd_is_root(rfd);
1011 if (k < 0)
1012 return log_error_errno(k, "Failed to check if directory file descriptor is root: %m");
1013
1014 if (arg_copy_root_shell && k == 0) {
28900a1b
DDM
1015 struct passwd *p;
1016
1017 errno = 0;
1018 p = getpwnam("root");
1019 if (!p)
1020 return log_error_errno(errno_or_else(EIO), "Failed to find passwd entry for root: %m");
1021
1022 r = free_and_strdup(&arg_root_shell, p->pw_shell);
1023 if (r < 0)
1024 return log_oom();
1025 }
1026
b352e545 1027 r = prompt_root_shell(rfd);
28900a1b
DDM
1028 if (r < 0)
1029 return r;
1030
fe75d5bc 1031 if (arg_copy_root_password && k == 0) {
418b9be5
LP
1032 struct spwd *p;
1033
1034 errno = 0;
1035 p = getspnam("root");
c4a53ebf
DDM
1036 if (!p)
1037 return log_error_errno(errno_or_else(EIO), "Failed to find shadow entry for root: %m");
418b9be5 1038
c4a53ebf
DDM
1039 r = free_and_strdup(&arg_root_password, p->sp_pwdp);
1040 if (r < 0)
1041 return log_oom();
418b9be5 1042
c4a53ebf 1043 arg_root_password_is_hashed = true;
418b9be5
LP
1044 }
1045
a0657479 1046 r = prompt_root_password(rfd);
418b9be5
LP
1047 if (r < 0)
1048 return r;
1049
c4a53ebf 1050 if (arg_root_password && arg_root_password_is_hashed) {
53c25ac9 1051 password = PASSWORD_SEE_SHADOW;
676339a1 1052 hashed_password = arg_root_password;
c4a53ebf 1053 } else if (arg_root_password) {
0e98d17e
ZJS
1054 r = hash_password(arg_root_password, &_hashed_password);
1055 if (r < 0)
1056 return log_error_errno(r, "Failed to hash password: %m");
676339a1 1057
53c25ac9 1058 password = PASSWORD_SEE_SHADOW;
0e98d17e 1059 hashed_password = _hashed_password;
c4a53ebf 1060
c4a53ebf 1061 } else if (arg_delete_root_password)
53c25ac9 1062 password = hashed_password = PASSWORD_NONE;
c4a53ebf 1063 else
53c25ac9 1064 password = hashed_password = PASSWORD_LOCKED_AND_INVALID;
c4a53ebf 1065
a0657479 1066 r = write_root_passwd(rfd, pfd, password, arg_root_shell);
c4a53ebf 1067 if (r < 0)
b352e545 1068 return log_error_errno(r, "Failed to write /etc/passwd: %m");
c4a53ebf 1069
b352e545 1070 log_info("/etc/passwd written.");
418b9be5 1071
b352e545 1072 r = write_root_shadow(pfd, hashed_password);
23bbb0de 1073 if (r < 0)
b352e545 1074 return log_error_errno(r, "Failed to write /etc/shadow: %m");
418b9be5 1075
b352e545 1076 log_info("/etc/shadow written.");
418b9be5
LP
1077 return 0;
1078}
1079
b352e545
DDM
1080static int process_kernel_cmdline(int rfd) {
1081 _cleanup_close_ int pfd = -EBADF;
1082 _cleanup_free_ char *f = NULL;
a5925354
DDM
1083 int r;
1084
b352e545
DDM
1085 assert(rfd >= 0);
1086
1087 pfd = chase_and_open_parent_at(rfd, "/etc/kernel/cmdline",
b39710cc 1088 CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
b352e545
DDM
1089 &f);
1090 if (pfd < 0)
1091 return log_error_errno(pfd, "Failed to chase /etc/kernel/cmdline: %m");
1092
1093 r = should_configure(pfd, f);
1094 if (r == 0)
1095 log_debug("Found /etc/kernel/cmdline, assuming kernel command line has been configured.");
1096 if (r <= 0)
1097 return r;
a5925354 1098
eb650ffe
ZJS
1099 if (!arg_kernel_cmdline) {
1100 log_debug("Creation of /etc/kernel/cmdline was not requested, skipping.");
a5925354 1101 return 0;
eb650ffe 1102 }
a5925354 1103
b352e545
DDM
1104 r = write_string_file_at(pfd, "cmdline", arg_kernel_cmdline,
1105 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC);
a5925354 1106 if (r < 0)
b352e545 1107 return log_error_errno(r, "Failed to write /etc/kernel/cmdline: %m");
a5925354 1108
b352e545 1109 log_info("/etc/kernel/cmdline written.");
a5925354
DDM
1110 return 0;
1111}
1112
05eb2c60
DDM
1113static int reset_one(int rfd, const char *path) {
1114 _cleanup_close_ int pfd = -EBADF;
1115 _cleanup_free_ char *f = NULL;
1116
1117 assert(rfd >= 0);
1118 assert(path);
1119
1120 pfd = chase_and_open_parent_at(rfd, path, CHASE_AT_RESOLVE_IN_ROOT|CHASE_WARN|CHASE_NOFOLLOW, &f);
1121 if (pfd == -ENOENT)
1122 return 0;
1123 if (pfd < 0)
1124 return log_error_errno(pfd, "Failed to resolve %s: %m", path);
1125
1126 if (unlinkat(pfd, f, 0) < 0)
1127 return errno == ENOENT ? 0 : log_error_errno(errno, "Failed to remove %s: %m", path);
1128
1129 log_info("Removed %s", path);
1130 return 0;
1131}
1132
1133static int process_reset(int rfd) {
1134 int r;
1135
1136 assert(rfd >= 0);
1137
1138 if (!arg_reset)
1139 return 0;
1140
1141 FOREACH_STRING(p,
1142 "/etc/locale.conf",
1143 "/etc/vconsole.conf",
1144 "/etc/hostname",
1145 "/etc/machine-id",
1146 "/etc/kernel/cmdline") {
1147 r = reset_one(rfd, p);
1148 if (r < 0)
1149 return r;
1150 }
1151
1152 return 0;
1153}
1154
37ec0fdd
LP
1155static int help(void) {
1156 _cleanup_free_ char *link = NULL;
1157 int r;
1158
1159 r = terminal_urlify_man("systemd-firstboot", "1", &link);
1160 if (r < 0)
1161 return log_oom();
1162
418b9be5
LP
1163 printf("%s [OPTIONS...]\n\n"
1164 "Configures basic settings of the system.\n\n"
e16793ee
ZJS
1165 " -h --help Show this help\n"
1166 " --version Show package version\n"
1167 " --root=PATH Operate on an alternate filesystem root\n"
84be0c71
LP
1168 " --image=PATH Operate on disk image as filesystem root\n"
1169 " --image-policy=POLICY Specify disk image dissection policy\n"
e16793ee
ZJS
1170 " --locale=LOCALE Set primary locale (LANG=)\n"
1171 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
1172 " --keymap=KEYMAP Set keymap\n"
1173 " --timezone=TIMEZONE Set timezone\n"
1174 " --hostname=NAME Set hostname\n"
1175 " --machine-ID=ID Set machine ID\n"
1176 " --root-password=PASSWORD Set root password from plaintext password\n"
1177 " --root-password-file=FILE Set root password from file\n"
1178 " --root-password-hashed=HASH Set root password from hashed password\n"
1179 " --root-shell=SHELL Set root shell\n"
1180 " --prompt-locale Prompt the user for locale settings\n"
1181 " --prompt-keymap Prompt the user for keymap settings\n"
1182 " --prompt-timezone Prompt the user for timezone\n"
1183 " --prompt-hostname Prompt the user for hostname\n"
1184 " --prompt-root-password Prompt the user for root password\n"
1185 " --prompt-root-shell Prompt the user for root shell\n"
1186 " --prompt Prompt for all of the above\n"
1187 " --copy-locale Copy locale from host\n"
1188 " --copy-keymap Copy keymap from host\n"
1189 " --copy-timezone Copy timezone from host\n"
1190 " --copy-root-password Copy root password from host\n"
1191 " --copy-root-shell Copy root shell from host\n"
1192 " --copy Copy locale, keymap, timezone, root password\n"
1193 " --setup-machine-id Generate a new random machine ID\n"
1194 " --force Overwrite existing files\n"
1195 " --delete-root-password Delete root password\n"
1196 " --welcome=no Disable the welcome text\n"
05eb2c60 1197 " --reset Remove existing files\n"
bc556335
DDM
1198 "\nSee the %s for details.\n",
1199 program_invocation_short_name,
1200 link);
37ec0fdd
LP
1201
1202 return 0;
418b9be5
LP
1203}
1204
1205static int parse_argv(int argc, char *argv[]) {
1206
1207 enum {
1208 ARG_VERSION = 0x100,
1209 ARG_ROOT,
3ff9fa59 1210 ARG_IMAGE,
06e78680 1211 ARG_IMAGE_POLICY,
418b9be5
LP
1212 ARG_LOCALE,
1213 ARG_LOCALE_MESSAGES,
ed457f13 1214 ARG_KEYMAP,
418b9be5
LP
1215 ARG_TIMEZONE,
1216 ARG_HOSTNAME,
1217 ARG_MACHINE_ID,
1218 ARG_ROOT_PASSWORD,
1219 ARG_ROOT_PASSWORD_FILE,
676339a1 1220 ARG_ROOT_PASSWORD_HASHED,
28900a1b 1221 ARG_ROOT_SHELL,
a5925354 1222 ARG_KERNEL_COMMAND_LINE,
418b9be5
LP
1223 ARG_PROMPT,
1224 ARG_PROMPT_LOCALE,
ed457f13 1225 ARG_PROMPT_KEYMAP,
418b9be5
LP
1226 ARG_PROMPT_TIMEZONE,
1227 ARG_PROMPT_HOSTNAME,
1228 ARG_PROMPT_ROOT_PASSWORD,
28900a1b 1229 ARG_PROMPT_ROOT_SHELL,
418b9be5
LP
1230 ARG_COPY,
1231 ARG_COPY_LOCALE,
ed457f13 1232 ARG_COPY_KEYMAP,
418b9be5
LP
1233 ARG_COPY_TIMEZONE,
1234 ARG_COPY_ROOT_PASSWORD,
28900a1b 1235 ARG_COPY_ROOT_SHELL,
418b9be5 1236 ARG_SETUP_MACHINE_ID,
b4909a3f 1237 ARG_FORCE,
4926ceaf 1238 ARG_DELETE_ROOT_PASSWORD,
a1225020 1239 ARG_WELCOME,
05eb2c60 1240 ARG_RESET,
418b9be5
LP
1241 };
1242
1243 static const struct option options[] = {
676339a1
DDM
1244 { "help", no_argument, NULL, 'h' },
1245 { "version", no_argument, NULL, ARG_VERSION },
1246 { "root", required_argument, NULL, ARG_ROOT },
3ff9fa59 1247 { "image", required_argument, NULL, ARG_IMAGE },
06e78680 1248 { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY },
676339a1
DDM
1249 { "locale", required_argument, NULL, ARG_LOCALE },
1250 { "locale-messages", required_argument, NULL, ARG_LOCALE_MESSAGES },
1251 { "keymap", required_argument, NULL, ARG_KEYMAP },
1252 { "timezone", required_argument, NULL, ARG_TIMEZONE },
1253 { "hostname", required_argument, NULL, ARG_HOSTNAME },
1254 { "machine-id", required_argument, NULL, ARG_MACHINE_ID },
1255 { "root-password", required_argument, NULL, ARG_ROOT_PASSWORD },
1256 { "root-password-file", required_argument, NULL, ARG_ROOT_PASSWORD_FILE },
1257 { "root-password-hashed", required_argument, NULL, ARG_ROOT_PASSWORD_HASHED },
28900a1b 1258 { "root-shell", required_argument, NULL, ARG_ROOT_SHELL },
a5925354 1259 { "kernel-command-line", required_argument, NULL, ARG_KERNEL_COMMAND_LINE },
676339a1
DDM
1260 { "prompt", no_argument, NULL, ARG_PROMPT },
1261 { "prompt-locale", no_argument, NULL, ARG_PROMPT_LOCALE },
1262 { "prompt-keymap", no_argument, NULL, ARG_PROMPT_KEYMAP },
1263 { "prompt-timezone", no_argument, NULL, ARG_PROMPT_TIMEZONE },
1264 { "prompt-hostname", no_argument, NULL, ARG_PROMPT_HOSTNAME },
1265 { "prompt-root-password", no_argument, NULL, ARG_PROMPT_ROOT_PASSWORD },
28900a1b 1266 { "prompt-root-shell", no_argument, NULL, ARG_PROMPT_ROOT_SHELL },
676339a1
DDM
1267 { "copy", no_argument, NULL, ARG_COPY },
1268 { "copy-locale", no_argument, NULL, ARG_COPY_LOCALE },
1269 { "copy-keymap", no_argument, NULL, ARG_COPY_KEYMAP },
1270 { "copy-timezone", no_argument, NULL, ARG_COPY_TIMEZONE },
1271 { "copy-root-password", no_argument, NULL, ARG_COPY_ROOT_PASSWORD },
28900a1b 1272 { "copy-root-shell", no_argument, NULL, ARG_COPY_ROOT_SHELL },
676339a1
DDM
1273 { "setup-machine-id", no_argument, NULL, ARG_SETUP_MACHINE_ID },
1274 { "force", no_argument, NULL, ARG_FORCE },
1275 { "delete-root-password", no_argument, NULL, ARG_DELETE_ROOT_PASSWORD },
a1225020 1276 { "welcome", required_argument, NULL, ARG_WELCOME },
05eb2c60 1277 { "reset", no_argument, NULL, ARG_RESET },
418b9be5
LP
1278 {}
1279 };
1280
1281 int r, c;
1282
1283 assert(argc >= 0);
1284 assert(argv);
1285
601185b4 1286 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
418b9be5
LP
1287
1288 switch (c) {
1289
1290 case 'h':
37ec0fdd 1291 return help();
418b9be5
LP
1292
1293 case ARG_VERSION:
3f6fd1ba 1294 return version();
418b9be5
LP
1295
1296 case ARG_ROOT:
614b022c 1297 r = parse_path_argument(optarg, true, &arg_root);
0f474365 1298 if (r < 0)
0f03c2a4 1299 return r;
418b9be5
LP
1300 break;
1301
3ff9fa59 1302 case ARG_IMAGE:
614b022c 1303 r = parse_path_argument(optarg, false, &arg_image);
3ff9fa59
LP
1304 if (r < 0)
1305 return r;
1306 break;
1307
06e78680
YW
1308 case ARG_IMAGE_POLICY:
1309 r = parse_image_policy_argument(optarg, &arg_image_policy);
1310 if (r < 0)
1311 return r;
1312 break;
1313
418b9be5 1314 case ARG_LOCALE:
2fc09a9c
DM
1315 r = free_and_strdup(&arg_locale, optarg);
1316 if (r < 0)
418b9be5
LP
1317 return log_oom();
1318
1319 break;
1320
1321 case ARG_LOCALE_MESSAGES:
2fc09a9c
DM
1322 r = free_and_strdup(&arg_locale_messages, optarg);
1323 if (r < 0)
418b9be5
LP
1324 return log_oom();
1325
1326 break;
1327
ed457f13 1328 case ARG_KEYMAP:
baaa35ad
ZJS
1329 if (!keymap_is_valid(optarg))
1330 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1331 "Keymap %s is not valid.", optarg);
ed457f13
TB
1332
1333 r = free_and_strdup(&arg_keymap, optarg);
1334 if (r < 0)
1335 return log_oom();
1336
1337 break;
1338
418b9be5 1339 case ARG_TIMEZONE:
baaa35ad
ZJS
1340 if (!timezone_is_valid(optarg, LOG_ERR))
1341 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1342 "Timezone %s is not valid.", optarg);
418b9be5 1343
2fc09a9c
DM
1344 r = free_and_strdup(&arg_timezone, optarg);
1345 if (r < 0)
418b9be5
LP
1346 return log_oom();
1347
1348 break;
1349
1350 case ARG_ROOT_PASSWORD:
2fc09a9c
DM
1351 r = free_and_strdup(&arg_root_password, optarg);
1352 if (r < 0)
418b9be5 1353 return log_oom();
676339a1
DDM
1354
1355 arg_root_password_is_hashed = false;
418b9be5
LP
1356 break;
1357
1358 case ARG_ROOT_PASSWORD_FILE:
0da16248 1359 arg_root_password = mfree(arg_root_password);
418b9be5
LP
1360
1361 r = read_one_line_file(optarg, &arg_root_password);
23bbb0de
MS
1362 if (r < 0)
1363 return log_error_errno(r, "Failed to read %s: %m", optarg);
418b9be5 1364
676339a1
DDM
1365 arg_root_password_is_hashed = false;
1366 break;
1367
1368 case ARG_ROOT_PASSWORD_HASHED:
1369 r = free_and_strdup(&arg_root_password, optarg);
1370 if (r < 0)
1371 return log_oom();
1372
1373 arg_root_password_is_hashed = true;
418b9be5
LP
1374 break;
1375
28900a1b 1376 case ARG_ROOT_SHELL:
28900a1b
DDM
1377 r = free_and_strdup(&arg_root_shell, optarg);
1378 if (r < 0)
1379 return log_oom();
1380
1381 break;
1382
418b9be5 1383 case ARG_HOSTNAME:
52ef5dd7 1384 if (!hostname_is_valid(optarg, VALID_HOSTNAME_TRAILING_DOT))
baaa35ad
ZJS
1385 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1386 "Host name %s is not valid.", optarg);
418b9be5 1387
2fc09a9c
DM
1388 r = free_and_strdup(&arg_hostname, optarg);
1389 if (r < 0)
418b9be5
LP
1390 return log_oom();
1391
79485fc2 1392 hostname_cleanup(arg_hostname);
418b9be5
LP
1393 break;
1394
1395 case ARG_MACHINE_ID:
7fb1d980
YW
1396 r = sd_id128_from_string(optarg, &arg_machine_id);
1397 if (r < 0)
1398 return log_error_errno(r, "Failed to parse machine id %s.", optarg);
418b9be5
LP
1399
1400 break;
1401
a5925354
DDM
1402 case ARG_KERNEL_COMMAND_LINE:
1403 r = free_and_strdup(&arg_kernel_cmdline, optarg);
1404 if (r < 0)
1405 return log_oom();
1406
1407 break;
1408
418b9be5 1409 case ARG_PROMPT:
28900a1b
DDM
1410 arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname =
1411 arg_prompt_root_password = arg_prompt_root_shell = true;
418b9be5
LP
1412 break;
1413
1414 case ARG_PROMPT_LOCALE:
1415 arg_prompt_locale = true;
1416 break;
1417
ed457f13
TB
1418 case ARG_PROMPT_KEYMAP:
1419 arg_prompt_keymap = true;
1420 break;
1421
418b9be5
LP
1422 case ARG_PROMPT_TIMEZONE:
1423 arg_prompt_timezone = true;
1424 break;
1425
1426 case ARG_PROMPT_HOSTNAME:
1427 arg_prompt_hostname = true;
1428 break;
1429
1430 case ARG_PROMPT_ROOT_PASSWORD:
1431 arg_prompt_root_password = true;
1432 break;
1433
28900a1b
DDM
1434 case ARG_PROMPT_ROOT_SHELL:
1435 arg_prompt_root_shell = true;
1436 break;
1437
418b9be5 1438 case ARG_COPY:
28900a1b
DDM
1439 arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password =
1440 arg_copy_root_shell = true;
e926f647 1441 break;
418b9be5
LP
1442
1443 case ARG_COPY_LOCALE:
1444 arg_copy_locale = true;
1445 break;
1446
ed457f13
TB
1447 case ARG_COPY_KEYMAP:
1448 arg_copy_keymap = true;
1449 break;
1450
418b9be5
LP
1451 case ARG_COPY_TIMEZONE:
1452 arg_copy_timezone = true;
1453 break;
1454
1455 case ARG_COPY_ROOT_PASSWORD:
1456 arg_copy_root_password = true;
1457 break;
1458
28900a1b
DDM
1459 case ARG_COPY_ROOT_SHELL:
1460 arg_copy_root_shell = true;
1461 break;
1462
418b9be5 1463 case ARG_SETUP_MACHINE_ID:
418b9be5 1464 r = sd_id128_randomize(&arg_machine_id);
23bbb0de
MS
1465 if (r < 0)
1466 return log_error_errno(r, "Failed to generate randomized machine ID: %m");
418b9be5
LP
1467
1468 break;
1469
b4909a3f
DDM
1470 case ARG_FORCE:
1471 arg_force = true;
1472 break;
1473
4926ceaf
DDM
1474 case ARG_DELETE_ROOT_PASSWORD:
1475 arg_delete_root_password = true;
1476 break;
1477
a1225020
LP
1478 case ARG_WELCOME:
1479 r = parse_boolean(optarg);
1480 if (r < 0)
1481 return log_error_errno(r, "Failed to parse --welcome= argument: %s", optarg);
1482
1483 arg_welcome = r;
1484 break;
1485
05eb2c60
DDM
1486 case ARG_RESET:
1487 arg_reset = true;
1488 break;
1489
418b9be5
LP
1490 case '?':
1491 return -EINVAL;
1492
1493 default:
04499a70 1494 assert_not_reached();
418b9be5 1495 }
418b9be5 1496
4926ceaf
DDM
1497 if (arg_delete_root_password && (arg_copy_root_password || arg_root_password || arg_prompt_root_password))
1498 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1499 "--delete-root-password cannot be combined with other root password options");
1500
3ff9fa59
LP
1501 if (arg_image && arg_root)
1502 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
1503
418b9be5
LP
1504 return 1;
1505}
1506
45f39418 1507static int run(int argc, char *argv[]) {
3ff9fa59 1508 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
a4b3e942 1509 _cleanup_(umount_and_freep) char *mounted_dir = NULL;
b352e545 1510 _cleanup_close_ int rfd = -EBADF;
418b9be5
LP
1511 int r;
1512
1513 r = parse_argv(argc, argv);
1514 if (r <= 0)
45f39418 1515 return r;
418b9be5 1516
d2acb93d 1517 log_setup();
418b9be5
LP
1518
1519 umask(0022);
1520
3ff9fa59
LP
1521 if (!arg_root && !arg_image) {
1522 bool enabled;
1523
1524 /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1525 * command line option, because we are called to provision the host with basic settings (as
1526 * opposed to some other file system tree/image) */
1527
1528 r = proc_cmdline_get_bool("systemd.firstboot", &enabled);
1529 if (r < 0)
1530 return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
eb650ffe
ZJS
1531 if (r > 0 && !enabled) {
1532 log_debug("Found systemd.firstboot=no kernel command line argument, terminating.");
3ff9fa59 1533 return 0; /* disabled */
eb650ffe 1534 }
3ff9fa59
LP
1535 }
1536
6aa05ebd
LP
1537 if (arg_image) {
1538 assert(!arg_root);
1539
1540 r = mount_image_privately_interactively(
1541 arg_image,
84be0c71 1542 arg_image_policy,
4b5de5dd
LP
1543 DISSECT_IMAGE_GENERIC_ROOT |
1544 DISSECT_IMAGE_REQUIRE_ROOT |
1545 DISSECT_IMAGE_VALIDATE_OS |
1546 DISSECT_IMAGE_RELAX_VAR_CHECK |
c65f854a
LP
1547 DISSECT_IMAGE_FSCK |
1548 DISSECT_IMAGE_GROWFS,
a4b3e942 1549 &mounted_dir,
b352e545 1550 &rfd,
e330f97a 1551 &loop_device);
6aa05ebd
LP
1552 if (r < 0)
1553 return r;
1554
a4b3e942 1555 arg_root = strdup(mounted_dir);
6aa05ebd
LP
1556 if (!arg_root)
1557 return log_oom();
b352e545
DDM
1558 } else {
1559 rfd = open(empty_to_root(arg_root), O_DIRECTORY|O_CLOEXEC);
1560 if (rfd < 0)
1561 return log_error_errno(errno, "Failed to open %s: %m", empty_to_root(arg_root));
1562 }
1563
1564 LOG_SET_PREFIX(arg_image ?: arg_root);
1565
a0657479
DDM
1566 /* We check these conditions here instead of in parse_argv() so that we can take the root directory
1567 * into account. */
1568
1569 if (arg_locale && !locale_is_ok(rfd, arg_locale))
1570 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale);
1571 if (arg_locale_messages && !locale_is_ok(rfd, arg_locale_messages))
1572 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale_messages);
1573
b352e545
DDM
1574 if (arg_root_shell) {
1575 r = find_shell(rfd, arg_root_shell);
1576 if (r < 0)
1577 return r;
6aa05ebd 1578 }
f582cbca 1579
05eb2c60
DDM
1580 r = process_reset(rfd);
1581 if (r < 0)
1582 return r;
1583
b352e545 1584 r = process_locale(rfd);
418b9be5 1585 if (r < 0)
45f39418 1586 return r;
418b9be5 1587
b352e545 1588 r = process_keymap(rfd);
ed457f13 1589 if (r < 0)
45f39418 1590 return r;
ed457f13 1591
b352e545 1592 r = process_timezone(rfd);
418b9be5 1593 if (r < 0)
45f39418 1594 return r;
418b9be5 1595
b352e545 1596 r = process_hostname(rfd);
418b9be5 1597 if (r < 0)
45f39418 1598 return r;
418b9be5 1599
b352e545 1600 r = process_machine_id(rfd);
418b9be5 1601 if (r < 0)
45f39418 1602 return r;
418b9be5 1603
b352e545 1604 r = process_root_account(rfd);
418b9be5 1605 if (r < 0)
45f39418
YW
1606 return r;
1607
b352e545 1608 r = process_kernel_cmdline(rfd);
a5925354
DDM
1609 if (r < 0)
1610 return r;
1611
45f39418 1612 return 0;
418b9be5 1613}
45f39418
YW
1614
1615DEFINE_MAIN_FUNCTION(run);