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