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