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