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