]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <fcntl.h> | |
4 | #include <getopt.h> | |
5 | #include <linux/loop.h> | |
6 | #include <unistd.h> | |
7 | ||
8 | #include "sd-id128.h" | |
9 | ||
10 | #include "alloc-util.h" | |
11 | #include "ask-password-api.h" | |
12 | #include "copy.h" | |
13 | #include "dissect-image.h" | |
14 | #include "env-file.h" | |
15 | #include "fd-util.h" | |
16 | #include "fileio.h" | |
17 | #include "fs-util.h" | |
18 | #include "hostname-util.h" | |
19 | #include "kbd-util.h" | |
20 | #include "libcrypt-util.h" | |
21 | #include "locale-util.h" | |
22 | #include "loop-util.h" | |
23 | #include "main-func.h" | |
24 | #include "memory-util.h" | |
25 | #include "mkdir.h" | |
26 | #include "mount-util.h" | |
27 | #include "namespace-util.h" | |
28 | #include "os-util.h" | |
29 | #include "parse-util.h" | |
30 | #include "path-util.h" | |
31 | #include "pretty-print.h" | |
32 | #include "proc-cmdline.h" | |
33 | #include "random-util.h" | |
34 | #include "string-util.h" | |
35 | #include "strv.h" | |
36 | #include "terminal-util.h" | |
37 | #include "time-util.h" | |
38 | #include "tmpfile-util-label.h" | |
39 | #include "tmpfile-util.h" | |
40 | #include "umask-util.h" | |
41 | #include "user-util.h" | |
42 | ||
43 | static char *arg_root = NULL; | |
44 | static char *arg_image = NULL; | |
45 | static char *arg_locale = NULL; /* $LANG */ | |
46 | static char *arg_keymap = NULL; | |
47 | static char *arg_locale_messages = NULL; /* $LC_MESSAGES */ | |
48 | static char *arg_timezone = NULL; | |
49 | static char *arg_hostname = NULL; | |
50 | static sd_id128_t arg_machine_id = {}; | |
51 | static char *arg_root_password = NULL; | |
52 | static char *arg_root_shell = NULL; | |
53 | static char *arg_kernel_cmdline = NULL; | |
54 | static bool arg_prompt_locale = false; | |
55 | static bool arg_prompt_keymap = false; | |
56 | static bool arg_prompt_timezone = false; | |
57 | static bool arg_prompt_hostname = false; | |
58 | static bool arg_prompt_root_password = false; | |
59 | static bool arg_prompt_root_shell = false; | |
60 | static bool arg_copy_locale = false; | |
61 | static bool arg_copy_keymap = false; | |
62 | static bool arg_copy_timezone = false; | |
63 | static bool arg_copy_root_password = false; | |
64 | static bool arg_copy_root_shell = false; | |
65 | static bool arg_force = false; | |
66 | static bool arg_delete_root_password = false; | |
67 | static bool arg_root_password_is_hashed = false; | |
68 | static bool arg_welcome = true; | |
69 | ||
70 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); | |
71 | STATIC_DESTRUCTOR_REGISTER(arg_image, freep); | |
72 | STATIC_DESTRUCTOR_REGISTER(arg_locale, freep); | |
73 | STATIC_DESTRUCTOR_REGISTER(arg_locale_messages, freep); | |
74 | STATIC_DESTRUCTOR_REGISTER(arg_keymap, freep); | |
75 | STATIC_DESTRUCTOR_REGISTER(arg_timezone, freep); | |
76 | STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep); | |
77 | STATIC_DESTRUCTOR_REGISTER(arg_root_password, erase_and_freep); | |
78 | ||
79 | static bool press_any_key(void) { | |
80 | char k = 0; | |
81 | bool need_nl = true; | |
82 | ||
83 | printf("-- Press any key to proceed --"); | |
84 | fflush(stdout); | |
85 | ||
86 | (void) read_one_char(stdin, &k, USEC_INFINITY, &need_nl); | |
87 | ||
88 | if (need_nl) | |
89 | putchar('\n'); | |
90 | ||
91 | return k != 'q'; | |
92 | } | |
93 | ||
94 | static void print_welcome(void) { | |
95 | _cleanup_free_ char *pretty_name = NULL, *ansi_color = NULL; | |
96 | static bool done = false; | |
97 | const char *pn; | |
98 | int r; | |
99 | ||
100 | if (!arg_welcome) | |
101 | return; | |
102 | ||
103 | if (done) | |
104 | return; | |
105 | ||
106 | r = parse_os_release( | |
107 | arg_root, | |
108 | "PRETTY_NAME", &pretty_name, | |
109 | "ANSI_COLOR", &ansi_color, | |
110 | NULL); | |
111 | if (r < 0) | |
112 | log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, | |
113 | "Failed to read os-release file, ignoring: %m"); | |
114 | ||
115 | pn = isempty(pretty_name) ? "Linux" : pretty_name; | |
116 | ||
117 | if (colors_enabled()) | |
118 | printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ansi_color, pn); | |
119 | else | |
120 | printf("\nWelcome to your new installation of %s!\n", pn); | |
121 | ||
122 | printf("\nPlease configure your system!\n\n"); | |
123 | ||
124 | press_any_key(); | |
125 | ||
126 | done = true; | |
127 | } | |
128 | ||
129 | static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) { | |
130 | unsigned break_lines, break_modulo; | |
131 | size_t n, per_column, i, j; | |
132 | ||
133 | assert(n_columns > 0); | |
134 | ||
135 | n = strv_length(x); | |
136 | per_column = DIV_ROUND_UP(n, n_columns); | |
137 | ||
138 | break_lines = lines(); | |
139 | if (break_lines > 2) | |
140 | break_lines--; | |
141 | ||
142 | /* The first page gets two extra lines, since we want to show | |
143 | * a title */ | |
144 | break_modulo = break_lines; | |
145 | if (break_modulo > 3) | |
146 | break_modulo -= 3; | |
147 | ||
148 | for (i = 0; i < per_column; i++) { | |
149 | ||
150 | for (j = 0; j < n_columns; j ++) { | |
151 | _cleanup_free_ char *e = NULL; | |
152 | ||
153 | if (j * per_column + i >= n) | |
154 | break; | |
155 | ||
156 | e = ellipsize(x[j * per_column + i], width, percentage); | |
157 | if (!e) | |
158 | return log_oom(); | |
159 | ||
160 | printf("%4zu) %-*s", j * per_column + i + 1, width, e); | |
161 | } | |
162 | ||
163 | putchar('\n'); | |
164 | ||
165 | /* on the first screen we reserve 2 extra lines for the title */ | |
166 | if (i % break_lines == break_modulo) { | |
167 | if (!press_any_key()) | |
168 | return 0; | |
169 | } | |
170 | } | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
175 | static int prompt_loop(const char *text, char **l, unsigned percentage, bool (*is_valid)(const char *name), char **ret) { | |
176 | int r; | |
177 | ||
178 | assert(text); | |
179 | assert(is_valid); | |
180 | assert(ret); | |
181 | ||
182 | for (;;) { | |
183 | _cleanup_free_ char *p = NULL; | |
184 | unsigned u; | |
185 | ||
186 | r = ask_string(&p, "%s %s (empty to skip, \"list\" to list options): ", | |
187 | special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), text); | |
188 | if (r < 0) | |
189 | return log_error_errno(r, "Failed to query user: %m"); | |
190 | ||
191 | if (isempty(p)) { | |
192 | log_warning("No data entered, skipping."); | |
193 | return 0; | |
194 | } | |
195 | ||
196 | if (streq(p, "list")) { | |
197 | r = show_menu(l, 3, 22, percentage); | |
198 | if (r < 0) | |
199 | return r; | |
200 | ||
201 | putchar('\n'); | |
202 | continue; | |
203 | }; | |
204 | ||
205 | r = safe_atou(p, &u); | |
206 | if (r >= 0) { | |
207 | if (u <= 0 || u > strv_length(l)) { | |
208 | log_error("Specified entry number out of range."); | |
209 | continue; | |
210 | } | |
211 | ||
212 | log_info("Selected '%s'.", l[u-1]); | |
213 | if (free_and_strdup(ret, l[u-1]) < 0) | |
214 | return log_oom(); | |
215 | ||
216 | return 0; | |
217 | } | |
218 | ||
219 | if (!is_valid(p)) { | |
220 | log_error("Entered data invalid."); | |
221 | continue; | |
222 | } | |
223 | ||
224 | return free_and_replace(*ret, p); | |
225 | } | |
226 | } | |
227 | ||
228 | static bool locale_is_ok(const char *name) { | |
229 | ||
230 | if (arg_root) | |
231 | return locale_is_valid(name); | |
232 | ||
233 | return locale_is_installed(name) > 0; | |
234 | } | |
235 | ||
236 | static int prompt_locale(void) { | |
237 | _cleanup_strv_free_ char **locales = NULL; | |
238 | int r; | |
239 | ||
240 | if (arg_locale || arg_locale_messages) | |
241 | return 0; | |
242 | ||
243 | if (!arg_prompt_locale) | |
244 | return 0; | |
245 | ||
246 | r = get_locales(&locales); | |
247 | if (r < 0) | |
248 | return log_error_errno(r, "Cannot query locales list: %m"); | |
249 | ||
250 | if (strv_isempty(locales)) | |
251 | log_debug("No locales found, skipping locale selection."); | |
252 | else if (strv_length(locales) == 1) { | |
253 | ||
254 | if (streq(locales[0], SYSTEMD_DEFAULT_LOCALE)) | |
255 | log_debug("Only installed locale is default locale anyway, not setting locale explicitly."); | |
256 | else { | |
257 | log_debug("Only a single locale available (%s), selecting it as default.", locales[0]); | |
258 | ||
259 | arg_locale = strdup(locales[0]); | |
260 | if (!arg_locale) | |
261 | return log_oom(); | |
262 | ||
263 | /* Not setting arg_locale_message here, since it defaults to LANG anyway */ | |
264 | } | |
265 | } else { | |
266 | print_welcome(); | |
267 | ||
268 | r = prompt_loop("Please enter system locale name or number", | |
269 | locales, 60, locale_is_ok, &arg_locale); | |
270 | if (r < 0) | |
271 | return r; | |
272 | ||
273 | if (isempty(arg_locale)) | |
274 | return 0; | |
275 | ||
276 | r = prompt_loop("Please enter system message locale name or number", | |
277 | locales, 60, locale_is_ok, &arg_locale_messages); | |
278 | if (r < 0) | |
279 | return r; | |
280 | ||
281 | /* Suppress the messages setting if it's the same as the main locale anyway */ | |
282 | if (streq_ptr(arg_locale, arg_locale_messages)) | |
283 | arg_locale_messages = mfree(arg_locale_messages); | |
284 | } | |
285 | ||
286 | return 0; | |
287 | } | |
288 | ||
289 | static int process_locale(void) { | |
290 | const char *etc_localeconf; | |
291 | char* locales[3]; | |
292 | unsigned i = 0; | |
293 | int r; | |
294 | ||
295 | etc_localeconf = prefix_roota(arg_root, "/etc/locale.conf"); | |
296 | if (laccess(etc_localeconf, F_OK) >= 0 && !arg_force) | |
297 | return 0; | |
298 | ||
299 | if (arg_copy_locale && arg_root) { | |
300 | ||
301 | (void) mkdir_parents(etc_localeconf, 0755); | |
302 | r = copy_file("/etc/locale.conf", etc_localeconf, 0, 0644, 0, 0, COPY_REFLINK); | |
303 | if (r != -ENOENT) { | |
304 | if (r < 0) | |
305 | return log_error_errno(r, "Failed to copy %s: %m", etc_localeconf); | |
306 | ||
307 | log_info("%s copied.", etc_localeconf); | |
308 | return 0; | |
309 | } | |
310 | } | |
311 | ||
312 | r = prompt_locale(); | |
313 | if (r < 0) | |
314 | return r; | |
315 | ||
316 | if (!isempty(arg_locale)) | |
317 | locales[i++] = strjoina("LANG=", arg_locale); | |
318 | if (!isempty(arg_locale_messages) && !streq(arg_locale_messages, arg_locale)) | |
319 | locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages); | |
320 | ||
321 | if (i == 0) | |
322 | return 0; | |
323 | ||
324 | locales[i] = NULL; | |
325 | ||
326 | (void) mkdir_parents(etc_localeconf, 0755); | |
327 | r = write_env_file(etc_localeconf, locales); | |
328 | if (r < 0) | |
329 | return log_error_errno(r, "Failed to write %s: %m", etc_localeconf); | |
330 | ||
331 | log_info("%s written.", etc_localeconf); | |
332 | return 0; | |
333 | } | |
334 | ||
335 | static int prompt_keymap(void) { | |
336 | _cleanup_strv_free_ char **kmaps = NULL; | |
337 | int r; | |
338 | ||
339 | if (arg_keymap) | |
340 | return 0; | |
341 | ||
342 | if (!arg_prompt_keymap) | |
343 | return 0; | |
344 | ||
345 | r = get_keymaps(&kmaps); | |
346 | if (r == -ENOENT) /* no keymaps installed */ | |
347 | return r; | |
348 | if (r < 0) | |
349 | return log_error_errno(r, "Failed to read keymaps: %m"); | |
350 | ||
351 | print_welcome(); | |
352 | ||
353 | return prompt_loop("Please enter system keymap name or number", | |
354 | kmaps, 60, keymap_is_valid, &arg_keymap); | |
355 | } | |
356 | ||
357 | static int process_keymap(void) { | |
358 | const char *etc_vconsoleconf; | |
359 | char **keymap; | |
360 | int r; | |
361 | ||
362 | etc_vconsoleconf = prefix_roota(arg_root, "/etc/vconsole.conf"); | |
363 | if (laccess(etc_vconsoleconf, F_OK) >= 0 && !arg_force) | |
364 | return 0; | |
365 | ||
366 | if (arg_copy_keymap && arg_root) { | |
367 | ||
368 | (void) mkdir_parents(etc_vconsoleconf, 0755); | |
369 | r = copy_file("/etc/vconsole.conf", etc_vconsoleconf, 0, 0644, 0, 0, COPY_REFLINK); | |
370 | if (r != -ENOENT) { | |
371 | if (r < 0) | |
372 | return log_error_errno(r, "Failed to copy %s: %m", etc_vconsoleconf); | |
373 | ||
374 | log_info("%s copied.", etc_vconsoleconf); | |
375 | return 0; | |
376 | } | |
377 | } | |
378 | ||
379 | r = prompt_keymap(); | |
380 | if (r == -ENOENT) | |
381 | return 0; /* don't fail if no keymaps are installed */ | |
382 | if (r < 0) | |
383 | return r; | |
384 | ||
385 | if (isempty(arg_keymap)) | |
386 | return 0; | |
387 | ||
388 | keymap = STRV_MAKE(strjoina("KEYMAP=", arg_keymap)); | |
389 | ||
390 | r = mkdir_parents(etc_vconsoleconf, 0755); | |
391 | if (r < 0) | |
392 | return log_error_errno(r, "Failed to create the parent directory of %s: %m", etc_vconsoleconf); | |
393 | ||
394 | r = write_env_file(etc_vconsoleconf, keymap); | |
395 | if (r < 0) | |
396 | return log_error_errno(r, "Failed to write %s: %m", etc_vconsoleconf); | |
397 | ||
398 | log_info("%s written.", etc_vconsoleconf); | |
399 | return 0; | |
400 | } | |
401 | ||
402 | static bool timezone_is_valid_log_error(const char *name) { | |
403 | return timezone_is_valid(name, LOG_ERR); | |
404 | } | |
405 | ||
406 | static int prompt_timezone(void) { | |
407 | _cleanup_strv_free_ char **zones = NULL; | |
408 | int r; | |
409 | ||
410 | if (arg_timezone) | |
411 | return 0; | |
412 | ||
413 | if (!arg_prompt_timezone) | |
414 | return 0; | |
415 | ||
416 | r = get_timezones(&zones); | |
417 | if (r < 0) | |
418 | return log_error_errno(r, "Cannot query timezone list: %m"); | |
419 | ||
420 | print_welcome(); | |
421 | ||
422 | r = prompt_loop("Please enter timezone name or number", | |
423 | zones, 30, timezone_is_valid_log_error, &arg_timezone); | |
424 | if (r < 0) | |
425 | return r; | |
426 | ||
427 | return 0; | |
428 | } | |
429 | ||
430 | static int process_timezone(void) { | |
431 | const char *etc_localtime, *e; | |
432 | int r; | |
433 | ||
434 | etc_localtime = prefix_roota(arg_root, "/etc/localtime"); | |
435 | if (laccess(etc_localtime, F_OK) >= 0 && !arg_force) | |
436 | return 0; | |
437 | ||
438 | if (arg_copy_timezone && arg_root) { | |
439 | _cleanup_free_ char *p = NULL; | |
440 | ||
441 | r = readlink_malloc("/etc/localtime", &p); | |
442 | if (r != -ENOENT) { | |
443 | if (r < 0) | |
444 | return log_error_errno(r, "Failed to read host timezone: %m"); | |
445 | ||
446 | (void) mkdir_parents(etc_localtime, 0755); | |
447 | if (symlink(p, etc_localtime) < 0) | |
448 | return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime); | |
449 | ||
450 | log_info("%s copied.", etc_localtime); | |
451 | return 0; | |
452 | } | |
453 | } | |
454 | ||
455 | r = prompt_timezone(); | |
456 | if (r < 0) | |
457 | return r; | |
458 | ||
459 | if (isempty(arg_timezone)) | |
460 | return 0; | |
461 | ||
462 | e = strjoina("../usr/share/zoneinfo/", arg_timezone); | |
463 | ||
464 | (void) mkdir_parents(etc_localtime, 0755); | |
465 | if (symlink(e, etc_localtime) < 0) | |
466 | return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime); | |
467 | ||
468 | log_info("%s written", etc_localtime); | |
469 | return 0; | |
470 | } | |
471 | ||
472 | static int prompt_hostname(void) { | |
473 | int r; | |
474 | ||
475 | if (arg_hostname) | |
476 | return 0; | |
477 | ||
478 | if (!arg_prompt_hostname) | |
479 | return 0; | |
480 | ||
481 | print_welcome(); | |
482 | putchar('\n'); | |
483 | ||
484 | for (;;) { | |
485 | _cleanup_free_ char *h = NULL; | |
486 | ||
487 | r = ask_string(&h, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET)); | |
488 | if (r < 0) | |
489 | return log_error_errno(r, "Failed to query hostname: %m"); | |
490 | ||
491 | if (isempty(h)) { | |
492 | log_warning("No hostname entered, skipping."); | |
493 | break; | |
494 | } | |
495 | ||
496 | if (!hostname_is_valid(h, true)) { | |
497 | log_error("Specified hostname invalid."); | |
498 | continue; | |
499 | } | |
500 | ||
501 | /* Get rid of the trailing dot that we allow, but don't want to see */ | |
502 | arg_hostname = hostname_cleanup(h); | |
503 | h = NULL; | |
504 | break; | |
505 | } | |
506 | ||
507 | return 0; | |
508 | } | |
509 | ||
510 | static int process_hostname(void) { | |
511 | const char *etc_hostname; | |
512 | int r; | |
513 | ||
514 | etc_hostname = prefix_roota(arg_root, "/etc/hostname"); | |
515 | if (laccess(etc_hostname, F_OK) >= 0 && !arg_force) | |
516 | return 0; | |
517 | ||
518 | r = prompt_hostname(); | |
519 | if (r < 0) | |
520 | return r; | |
521 | ||
522 | if (isempty(arg_hostname)) | |
523 | return 0; | |
524 | ||
525 | r = write_string_file(etc_hostname, arg_hostname, | |
526 | WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755 | | |
527 | (arg_force ? WRITE_STRING_FILE_ATOMIC : 0)); | |
528 | if (r < 0) | |
529 | return log_error_errno(r, "Failed to write %s: %m", etc_hostname); | |
530 | ||
531 | log_info("%s written.", etc_hostname); | |
532 | return 0; | |
533 | } | |
534 | ||
535 | static int process_machine_id(void) { | |
536 | const char *etc_machine_id; | |
537 | char id[SD_ID128_STRING_MAX]; | |
538 | int r; | |
539 | ||
540 | etc_machine_id = prefix_roota(arg_root, "/etc/machine-id"); | |
541 | if (laccess(etc_machine_id, F_OK) >= 0 && !arg_force) | |
542 | return 0; | |
543 | ||
544 | if (sd_id128_is_null(arg_machine_id)) | |
545 | return 0; | |
546 | ||
547 | r = write_string_file(etc_machine_id, sd_id128_to_string(arg_machine_id, id), | |
548 | WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755 | | |
549 | (arg_force ? WRITE_STRING_FILE_ATOMIC : 0)); | |
550 | if (r < 0) | |
551 | return log_error_errno(r, "Failed to write machine id: %m"); | |
552 | ||
553 | log_info("%s written.", etc_machine_id); | |
554 | return 0; | |
555 | } | |
556 | ||
557 | static int prompt_root_password(void) { | |
558 | const char *msg1, *msg2; | |
559 | int r; | |
560 | ||
561 | if (arg_root_password) | |
562 | return 0; | |
563 | ||
564 | if (!arg_prompt_root_password) | |
565 | return 0; | |
566 | ||
567 | print_welcome(); | |
568 | putchar('\n'); | |
569 | ||
570 | msg1 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter a new root password (empty to skip):"); | |
571 | msg2 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter new root password again:"); | |
572 | ||
573 | for (;;) { | |
574 | _cleanup_strv_free_erase_ char **a = NULL, **b = NULL; | |
575 | ||
576 | r = ask_password_tty(-1, msg1, NULL, 0, 0, NULL, &a); | |
577 | if (r < 0) | |
578 | return log_error_errno(r, "Failed to query root password: %m"); | |
579 | if (strv_length(a) != 1) | |
580 | return log_error_errno(SYNTHETIC_ERRNO(EIO), | |
581 | "Received multiple passwords, where we expected one."); | |
582 | ||
583 | if (isempty(*a)) { | |
584 | log_warning("No password entered, skipping."); | |
585 | break; | |
586 | } | |
587 | ||
588 | r = ask_password_tty(-1, msg2, NULL, 0, 0, NULL, &b); | |
589 | if (r < 0) | |
590 | return log_error_errno(r, "Failed to query root password: %m"); | |
591 | if (strv_length(b) != 1) | |
592 | return log_error_errno(SYNTHETIC_ERRNO(EIO), | |
593 | "Received multiple passwords, where we expected one."); | |
594 | ||
595 | if (!streq(*a, *b)) { | |
596 | log_error("Entered passwords did not match, please try again."); | |
597 | continue; | |
598 | } | |
599 | ||
600 | arg_root_password = TAKE_PTR(*a); | |
601 | break; | |
602 | } | |
603 | ||
604 | return 0; | |
605 | } | |
606 | ||
607 | static int find_shell(const char *path, const char *root) { | |
608 | int r; | |
609 | ||
610 | assert(path); | |
611 | ||
612 | if (!valid_shell(path)) | |
613 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "%s is not a valid shell", path); | |
614 | ||
615 | r = chase_symlinks(path, root, CHASE_PREFIX_ROOT, NULL, NULL); | |
616 | if (r < 0) { | |
617 | const char *p; | |
618 | p = prefix_roota(root, path); | |
619 | return log_error_errno(r, "Failed to resolve shell %s: %m", p); | |
620 | } | |
621 | ||
622 | return 0; | |
623 | } | |
624 | ||
625 | static int prompt_root_shell(void) { | |
626 | int r; | |
627 | ||
628 | if (arg_root_shell || !arg_prompt_root_shell) | |
629 | return 0; | |
630 | ||
631 | print_welcome(); | |
632 | putchar('\n'); | |
633 | ||
634 | for (;;) { | |
635 | _cleanup_free_ char *s = NULL; | |
636 | ||
637 | r = ask_string(&s, "%s Please enter root shell for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET)); | |
638 | if (r < 0) | |
639 | return log_error_errno(r, "Failed to query root shell: %m"); | |
640 | ||
641 | if (isempty(s)) { | |
642 | log_warning("No shell entered, skipping."); | |
643 | break; | |
644 | } | |
645 | ||
646 | r = find_shell(s, arg_root); | |
647 | if (r < 0) | |
648 | continue; | |
649 | ||
650 | arg_root_shell = TAKE_PTR(s); | |
651 | break; | |
652 | } | |
653 | ||
654 | return 0; | |
655 | } | |
656 | ||
657 | static int write_root_passwd(const char *passwd_path, const char *password, const char *shell) { | |
658 | _cleanup_fclose_ FILE *original = NULL, *passwd = NULL; | |
659 | _cleanup_(unlink_and_freep) char *passwd_tmp = NULL; | |
660 | int r; | |
661 | ||
662 | assert(password); | |
663 | ||
664 | r = fopen_temporary_label("/etc/passwd", passwd_path, &passwd, &passwd_tmp); | |
665 | if (r < 0) | |
666 | return r; | |
667 | ||
668 | original = fopen(passwd_path, "re"); | |
669 | if (original) { | |
670 | struct passwd *i; | |
671 | ||
672 | r = sync_rights(fileno(original), fileno(passwd)); | |
673 | if (r < 0) | |
674 | return r; | |
675 | ||
676 | while ((r = fgetpwent_sane(original, &i)) > 0) { | |
677 | ||
678 | if (streq(i->pw_name, "root")) { | |
679 | i->pw_passwd = (char *) password; | |
680 | if (shell) | |
681 | i->pw_shell = (char *) shell; | |
682 | } | |
683 | ||
684 | r = putpwent_sane(i, passwd); | |
685 | if (r < 0) | |
686 | return r; | |
687 | } | |
688 | if (r < 0) | |
689 | return r; | |
690 | ||
691 | } else { | |
692 | struct passwd root = { | |
693 | .pw_name = (char *) "root", | |
694 | .pw_passwd = (char *) password, | |
695 | .pw_uid = 0, | |
696 | .pw_gid = 0, | |
697 | .pw_gecos = (char *) "Super User", | |
698 | .pw_dir = (char *) "/root", | |
699 | .pw_shell = (char *) (shell ?: "/bin/sh"), | |
700 | }; | |
701 | ||
702 | if (errno != ENOENT) | |
703 | return -errno; | |
704 | ||
705 | r = fchmod(fileno(passwd), 0644); | |
706 | if (r < 0) | |
707 | return -errno; | |
708 | ||
709 | r = putpwent_sane(&root, passwd); | |
710 | if (r < 0) | |
711 | return r; | |
712 | } | |
713 | ||
714 | r = fflush_sync_and_check(passwd); | |
715 | if (r < 0) | |
716 | return r; | |
717 | ||
718 | r = rename_and_apply_smack_floor_label(passwd_tmp, passwd_path); | |
719 | if (r < 0) | |
720 | return r; | |
721 | ||
722 | return 0; | |
723 | } | |
724 | ||
725 | static int write_root_shadow(const char *shadow_path, const char *hashed_password) { | |
726 | _cleanup_fclose_ FILE *original = NULL, *shadow = NULL; | |
727 | _cleanup_(unlink_and_freep) char *shadow_tmp = NULL; | |
728 | int r; | |
729 | ||
730 | assert(hashed_password); | |
731 | ||
732 | r = fopen_temporary_label("/etc/shadow", shadow_path, &shadow, &shadow_tmp); | |
733 | if (r < 0) | |
734 | return r; | |
735 | ||
736 | original = fopen(shadow_path, "re"); | |
737 | if (original) { | |
738 | struct spwd *i; | |
739 | ||
740 | r = sync_rights(fileno(original), fileno(shadow)); | |
741 | if (r < 0) | |
742 | return r; | |
743 | ||
744 | while ((r = fgetspent_sane(original, &i)) > 0) { | |
745 | ||
746 | if (streq(i->sp_namp, "root")) { | |
747 | i->sp_pwdp = (char *) hashed_password; | |
748 | i->sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY); | |
749 | } | |
750 | ||
751 | r = putspent_sane(i, shadow); | |
752 | if (r < 0) | |
753 | return r; | |
754 | } | |
755 | if (r < 0) | |
756 | return r; | |
757 | ||
758 | } else { | |
759 | struct spwd root = { | |
760 | .sp_namp = (char*) "root", | |
761 | .sp_pwdp = (char *) hashed_password, | |
762 | .sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY), | |
763 | .sp_min = -1, | |
764 | .sp_max = -1, | |
765 | .sp_warn = -1, | |
766 | .sp_inact = -1, | |
767 | .sp_expire = -1, | |
768 | .sp_flag = (unsigned long) -1, /* this appears to be what everybody does ... */ | |
769 | }; | |
770 | ||
771 | if (errno != ENOENT) | |
772 | return -errno; | |
773 | ||
774 | r = fchmod(fileno(shadow), 0000); | |
775 | if (r < 0) | |
776 | return -errno; | |
777 | ||
778 | r = putspent_sane(&root, shadow); | |
779 | if (r < 0) | |
780 | return r; | |
781 | } | |
782 | ||
783 | r = fflush_sync_and_check(shadow); | |
784 | if (r < 0) | |
785 | return r; | |
786 | ||
787 | r = rename_and_apply_smack_floor_label(shadow_tmp, shadow_path); | |
788 | if (r < 0) | |
789 | return r; | |
790 | ||
791 | return 0; | |
792 | } | |
793 | ||
794 | static int process_root_args(void) { | |
795 | _cleanup_close_ int lock = -1; | |
796 | struct crypt_data cd = {}; | |
797 | const char *password, *hashed_password; | |
798 | const char *etc_passwd, *etc_shadow; | |
799 | int r; | |
800 | ||
801 | etc_passwd = prefix_roota(arg_root, "/etc/passwd"); | |
802 | etc_shadow = prefix_roota(arg_root, "/etc/shadow"); | |
803 | ||
804 | /* We only mess with passwd and shadow if both do not exist or --force is specified. These files are | |
805 | * tightly coupled and hence we make sure we have permission from the user to create/modify both | |
806 | * files. */ | |
807 | if ((laccess(etc_passwd, F_OK) >= 0 || laccess(etc_shadow, F_OK) >= 0) && !arg_force) | |
808 | return 0; | |
809 | ||
810 | (void) mkdir_parents(etc_passwd, 0755); | |
811 | ||
812 | lock = take_etc_passwd_lock(arg_root); | |
813 | if (lock < 0) | |
814 | return log_error_errno(lock, "Failed to take a lock on %s: %m", etc_passwd); | |
815 | ||
816 | if (arg_copy_root_shell && arg_root) { | |
817 | struct passwd *p; | |
818 | ||
819 | errno = 0; | |
820 | p = getpwnam("root"); | |
821 | if (!p) | |
822 | return log_error_errno(errno_or_else(EIO), "Failed to find passwd entry for root: %m"); | |
823 | ||
824 | r = free_and_strdup(&arg_root_shell, p->pw_shell); | |
825 | if (r < 0) | |
826 | return log_oom(); | |
827 | } | |
828 | ||
829 | r = prompt_root_shell(); | |
830 | if (r < 0) | |
831 | return r; | |
832 | ||
833 | if (arg_copy_root_password && arg_root) { | |
834 | struct spwd *p; | |
835 | ||
836 | errno = 0; | |
837 | p = getspnam("root"); | |
838 | if (!p) | |
839 | return log_error_errno(errno_or_else(EIO), "Failed to find shadow entry for root: %m"); | |
840 | ||
841 | r = free_and_strdup(&arg_root_password, p->sp_pwdp); | |
842 | if (r < 0) | |
843 | return log_oom(); | |
844 | ||
845 | arg_root_password_is_hashed = true; | |
846 | } | |
847 | ||
848 | r = prompt_root_password(); | |
849 | if (r < 0) | |
850 | return r; | |
851 | ||
852 | if (arg_root_password && arg_root_password_is_hashed) { | |
853 | password = "x"; | |
854 | hashed_password = arg_root_password; | |
855 | } else if (arg_root_password) { | |
856 | _cleanup_free_ char *salt = NULL; | |
857 | /* hashed_password points inside cd after crypt_r returns so cd has function scope. */ | |
858 | ||
859 | password = "x"; | |
860 | ||
861 | r = make_salt(&salt); | |
862 | if (r < 0) | |
863 | return log_error_errno(r, "Failed to get salt: %m"); | |
864 | ||
865 | errno = 0; | |
866 | hashed_password = crypt_r(arg_root_password, salt, &cd); | |
867 | if (!hashed_password) | |
868 | return log_error_errno(errno == 0 ? SYNTHETIC_ERRNO(EINVAL) : errno, | |
869 | "Failed to encrypt password: %m"); | |
870 | } else if (arg_delete_root_password) | |
871 | password = hashed_password = ""; | |
872 | else | |
873 | password = hashed_password = "!"; | |
874 | ||
875 | r = write_root_passwd(etc_passwd, password, arg_root_shell); | |
876 | if (r < 0) | |
877 | return log_error_errno(r, "Failed to write %s: %m", etc_passwd); | |
878 | ||
879 | log_info("%s written", etc_passwd); | |
880 | ||
881 | r = write_root_shadow(etc_shadow, hashed_password); | |
882 | if (r < 0) | |
883 | return log_error_errno(r, "Failed to write %s: %m", etc_shadow); | |
884 | ||
885 | log_info("%s written.", etc_shadow); | |
886 | return 0; | |
887 | } | |
888 | ||
889 | static int process_kernel_cmdline(void) { | |
890 | const char *etc_kernel_cmdline; | |
891 | int r; | |
892 | ||
893 | etc_kernel_cmdline = prefix_roota(arg_root, "/etc/kernel/cmdline"); | |
894 | if (laccess(etc_kernel_cmdline, F_OK) >= 0 && !arg_force) | |
895 | return 0; | |
896 | ||
897 | if (!arg_kernel_cmdline) | |
898 | return 0; | |
899 | ||
900 | r = write_string_file(etc_kernel_cmdline, arg_kernel_cmdline, | |
901 | WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC | WRITE_STRING_FILE_MKDIR_0755 | | |
902 | (arg_force ? WRITE_STRING_FILE_ATOMIC : 0)); | |
903 | if (r < 0) | |
904 | return log_error_errno(r, "Failed to write %s: %m", etc_kernel_cmdline); | |
905 | ||
906 | log_info("%s written.", etc_kernel_cmdline); | |
907 | return 0; | |
908 | } | |
909 | ||
910 | static int setup_image(char **ret_mount_dir, LoopDevice **ret_loop_device, DecryptedImage **ret_decrypted_image) { | |
911 | DissectImageFlags f = DISSECT_IMAGE_REQUIRE_ROOT|DISSECT_IMAGE_VALIDATE_OS|DISSECT_IMAGE_RELAX_VAR_CHECK|DISSECT_IMAGE_FSCK; | |
912 | _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; | |
913 | _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL; | |
914 | _cleanup_(dissected_image_unrefp) DissectedImage *dissected_image = NULL; | |
915 | _cleanup_(rmdir_and_freep) char *mount_dir = NULL; | |
916 | _cleanup_free_ char *temp = NULL; | |
917 | int r; | |
918 | ||
919 | if (!arg_image) { | |
920 | *ret_mount_dir = NULL; | |
921 | *ret_decrypted_image = NULL; | |
922 | *ret_loop_device = NULL; | |
923 | return 0; | |
924 | } | |
925 | ||
926 | assert(!arg_root); | |
927 | ||
928 | r = tempfn_random_child(NULL, "firstboot", &temp); | |
929 | if (r < 0) | |
930 | return log_error_errno(r, "Failed to generate temporary mount directory: %m"); | |
931 | ||
932 | r = loop_device_make_by_path(arg_image, O_RDWR, LO_FLAGS_PARTSCAN, &d); | |
933 | if (r < 0) | |
934 | return log_error_errno(r, "Failed to set up loopback device: %m"); | |
935 | ||
936 | r = dissect_image_and_warn(d->fd, arg_image, NULL, 0, NULL, f, &dissected_image); | |
937 | if (r < 0) | |
938 | return r; | |
939 | ||
940 | r = dissected_image_decrypt_interactively(dissected_image, NULL, NULL, 0, NULL, NULL, NULL, 0, f, &decrypted_image); | |
941 | if (r < 0) | |
942 | return r; | |
943 | ||
944 | r = detach_mount_namespace(); | |
945 | if (r < 0) | |
946 | return log_error_errno(r, "Failed to detach mount namespace: %m"); | |
947 | ||
948 | mount_dir = strdup(temp); | |
949 | if (!mount_dir) | |
950 | return log_oom(); | |
951 | ||
952 | r = mkdir_p(mount_dir, 0700); | |
953 | if (r < 0) { | |
954 | mount_dir = mfree(mount_dir); | |
955 | return log_error_errno(r, "Failed to create mount point: %m"); | |
956 | } | |
957 | ||
958 | r = dissected_image_mount(dissected_image, mount_dir, UID_INVALID, f); | |
959 | if (r < 0) | |
960 | return log_error_errno(r, "Failed to mount image: %m"); | |
961 | ||
962 | if (decrypted_image) { | |
963 | r = decrypted_image_relinquish(decrypted_image); | |
964 | if (r < 0) | |
965 | return log_error_errno(r, "Failed to relinquish DM devices: %m"); | |
966 | } | |
967 | ||
968 | loop_device_relinquish(d); | |
969 | ||
970 | arg_root = TAKE_PTR(temp); | |
971 | ||
972 | *ret_mount_dir = TAKE_PTR(mount_dir); | |
973 | *ret_decrypted_image = TAKE_PTR(decrypted_image); | |
974 | *ret_loop_device = TAKE_PTR(d); | |
975 | ||
976 | return 1; | |
977 | } | |
978 | ||
979 | static int help(void) { | |
980 | _cleanup_free_ char *link = NULL; | |
981 | int r; | |
982 | ||
983 | r = terminal_urlify_man("systemd-firstboot", "1", &link); | |
984 | if (r < 0) | |
985 | return log_oom(); | |
986 | ||
987 | printf("%s [OPTIONS...]\n\n" | |
988 | "Configures basic settings of the system.\n\n" | |
989 | " -h --help Show this help\n" | |
990 | " --version Show package version\n" | |
991 | " --root=PATH Operate on an alternate filesystem root\n" | |
992 | " --image=PATH Operate on an alternate filesystem image\n" | |
993 | " --locale=LOCALE Set primary locale (LANG=)\n" | |
994 | " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n" | |
995 | " --keymap=KEYMAP Set keymap\n" | |
996 | " --timezone=TIMEZONE Set timezone\n" | |
997 | " --hostname=NAME Set hostname\n" | |
998 | " --machine-ID=ID Set machine ID\n" | |
999 | " --root-password=PASSWORD Set root password from plaintext password\n" | |
1000 | " --root-password-file=FILE Set root password from file\n" | |
1001 | " --root-password-hashed=HASHED_PASSWORD Set root password from hashed password\n" | |
1002 | " --prompt-locale Prompt the user for locale settings\n" | |
1003 | " --prompt-keymap Prompt the user for keymap settings\n" | |
1004 | " --prompt-timezone Prompt the user for timezone\n" | |
1005 | " --prompt-hostname Prompt the user for hostname\n" | |
1006 | " --prompt-root-password Prompt the user for root password\n" | |
1007 | " --prompt Prompt for all of the above\n" | |
1008 | " --copy-locale Copy locale from host\n" | |
1009 | " --copy-keymap Copy keymap from host\n" | |
1010 | " --copy-timezone Copy timezone from host\n" | |
1011 | " --copy-root-password Copy root password from host\n" | |
1012 | " --copy Copy locale, keymap, timezone, root password\n" | |
1013 | " --setup-machine-id Generate a new random machine ID\n" | |
1014 | " --force Overwrite existing files\n" | |
1015 | " --delete-root-password Delete root password\n" | |
1016 | " --welcome=no Disable the welcome text\n" | |
1017 | "\nSee the %s for details.\n" | |
1018 | , program_invocation_short_name | |
1019 | , link | |
1020 | ); | |
1021 | ||
1022 | return 0; | |
1023 | } | |
1024 | ||
1025 | static int parse_argv(int argc, char *argv[]) { | |
1026 | ||
1027 | enum { | |
1028 | ARG_VERSION = 0x100, | |
1029 | ARG_ROOT, | |
1030 | ARG_IMAGE, | |
1031 | ARG_LOCALE, | |
1032 | ARG_LOCALE_MESSAGES, | |
1033 | ARG_KEYMAP, | |
1034 | ARG_TIMEZONE, | |
1035 | ARG_HOSTNAME, | |
1036 | ARG_MACHINE_ID, | |
1037 | ARG_ROOT_PASSWORD, | |
1038 | ARG_ROOT_PASSWORD_FILE, | |
1039 | ARG_ROOT_PASSWORD_HASHED, | |
1040 | ARG_ROOT_SHELL, | |
1041 | ARG_KERNEL_COMMAND_LINE, | |
1042 | ARG_PROMPT, | |
1043 | ARG_PROMPT_LOCALE, | |
1044 | ARG_PROMPT_KEYMAP, | |
1045 | ARG_PROMPT_TIMEZONE, | |
1046 | ARG_PROMPT_HOSTNAME, | |
1047 | ARG_PROMPT_ROOT_PASSWORD, | |
1048 | ARG_PROMPT_ROOT_SHELL, | |
1049 | ARG_COPY, | |
1050 | ARG_COPY_LOCALE, | |
1051 | ARG_COPY_KEYMAP, | |
1052 | ARG_COPY_TIMEZONE, | |
1053 | ARG_COPY_ROOT_PASSWORD, | |
1054 | ARG_COPY_ROOT_SHELL, | |
1055 | ARG_SETUP_MACHINE_ID, | |
1056 | ARG_FORCE, | |
1057 | ARG_DELETE_ROOT_PASSWORD, | |
1058 | ARG_WELCOME, | |
1059 | }; | |
1060 | ||
1061 | static const struct option options[] = { | |
1062 | { "help", no_argument, NULL, 'h' }, | |
1063 | { "version", no_argument, NULL, ARG_VERSION }, | |
1064 | { "root", required_argument, NULL, ARG_ROOT }, | |
1065 | { "image", required_argument, NULL, ARG_IMAGE }, | |
1066 | { "locale", required_argument, NULL, ARG_LOCALE }, | |
1067 | { "locale-messages", required_argument, NULL, ARG_LOCALE_MESSAGES }, | |
1068 | { "keymap", required_argument, NULL, ARG_KEYMAP }, | |
1069 | { "timezone", required_argument, NULL, ARG_TIMEZONE }, | |
1070 | { "hostname", required_argument, NULL, ARG_HOSTNAME }, | |
1071 | { "machine-id", required_argument, NULL, ARG_MACHINE_ID }, | |
1072 | { "root-password", required_argument, NULL, ARG_ROOT_PASSWORD }, | |
1073 | { "root-password-file", required_argument, NULL, ARG_ROOT_PASSWORD_FILE }, | |
1074 | { "root-password-hashed", required_argument, NULL, ARG_ROOT_PASSWORD_HASHED }, | |
1075 | { "root-shell", required_argument, NULL, ARG_ROOT_SHELL }, | |
1076 | { "kernel-command-line", required_argument, NULL, ARG_KERNEL_COMMAND_LINE }, | |
1077 | { "prompt", no_argument, NULL, ARG_PROMPT }, | |
1078 | { "prompt-locale", no_argument, NULL, ARG_PROMPT_LOCALE }, | |
1079 | { "prompt-keymap", no_argument, NULL, ARG_PROMPT_KEYMAP }, | |
1080 | { "prompt-timezone", no_argument, NULL, ARG_PROMPT_TIMEZONE }, | |
1081 | { "prompt-hostname", no_argument, NULL, ARG_PROMPT_HOSTNAME }, | |
1082 | { "prompt-root-password", no_argument, NULL, ARG_PROMPT_ROOT_PASSWORD }, | |
1083 | { "prompt-root-shell", no_argument, NULL, ARG_PROMPT_ROOT_SHELL }, | |
1084 | { "copy", no_argument, NULL, ARG_COPY }, | |
1085 | { "copy-locale", no_argument, NULL, ARG_COPY_LOCALE }, | |
1086 | { "copy-keymap", no_argument, NULL, ARG_COPY_KEYMAP }, | |
1087 | { "copy-timezone", no_argument, NULL, ARG_COPY_TIMEZONE }, | |
1088 | { "copy-root-password", no_argument, NULL, ARG_COPY_ROOT_PASSWORD }, | |
1089 | { "copy-root-shell", no_argument, NULL, ARG_COPY_ROOT_SHELL }, | |
1090 | { "setup-machine-id", no_argument, NULL, ARG_SETUP_MACHINE_ID }, | |
1091 | { "force", no_argument, NULL, ARG_FORCE }, | |
1092 | { "delete-root-password", no_argument, NULL, ARG_DELETE_ROOT_PASSWORD }, | |
1093 | { "welcome", required_argument, NULL, ARG_WELCOME }, | |
1094 | {} | |
1095 | }; | |
1096 | ||
1097 | int r, c; | |
1098 | ||
1099 | assert(argc >= 0); | |
1100 | assert(argv); | |
1101 | ||
1102 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) | |
1103 | ||
1104 | switch (c) { | |
1105 | ||
1106 | case 'h': | |
1107 | return help(); | |
1108 | ||
1109 | case ARG_VERSION: | |
1110 | return version(); | |
1111 | ||
1112 | case ARG_ROOT: | |
1113 | r = parse_path_argument_and_warn(optarg, true, &arg_root); | |
1114 | if (r < 0) | |
1115 | return r; | |
1116 | break; | |
1117 | ||
1118 | case ARG_IMAGE: | |
1119 | r = parse_path_argument_and_warn(optarg, false, &arg_image); | |
1120 | if (r < 0) | |
1121 | return r; | |
1122 | break; | |
1123 | ||
1124 | case ARG_LOCALE: | |
1125 | r = free_and_strdup(&arg_locale, optarg); | |
1126 | if (r < 0) | |
1127 | return log_oom(); | |
1128 | ||
1129 | break; | |
1130 | ||
1131 | case ARG_LOCALE_MESSAGES: | |
1132 | r = free_and_strdup(&arg_locale_messages, optarg); | |
1133 | if (r < 0) | |
1134 | return log_oom(); | |
1135 | ||
1136 | break; | |
1137 | ||
1138 | case ARG_KEYMAP: | |
1139 | if (!keymap_is_valid(optarg)) | |
1140 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1141 | "Keymap %s is not valid.", optarg); | |
1142 | ||
1143 | r = free_and_strdup(&arg_keymap, optarg); | |
1144 | if (r < 0) | |
1145 | return log_oom(); | |
1146 | ||
1147 | break; | |
1148 | ||
1149 | case ARG_TIMEZONE: | |
1150 | if (!timezone_is_valid(optarg, LOG_ERR)) | |
1151 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1152 | "Timezone %s is not valid.", optarg); | |
1153 | ||
1154 | r = free_and_strdup(&arg_timezone, optarg); | |
1155 | if (r < 0) | |
1156 | return log_oom(); | |
1157 | ||
1158 | break; | |
1159 | ||
1160 | case ARG_ROOT_PASSWORD: | |
1161 | r = free_and_strdup(&arg_root_password, optarg); | |
1162 | if (r < 0) | |
1163 | return log_oom(); | |
1164 | ||
1165 | arg_root_password_is_hashed = false; | |
1166 | break; | |
1167 | ||
1168 | case ARG_ROOT_PASSWORD_FILE: | |
1169 | arg_root_password = mfree(arg_root_password); | |
1170 | ||
1171 | r = read_one_line_file(optarg, &arg_root_password); | |
1172 | if (r < 0) | |
1173 | return log_error_errno(r, "Failed to read %s: %m", optarg); | |
1174 | ||
1175 | arg_root_password_is_hashed = false; | |
1176 | break; | |
1177 | ||
1178 | case ARG_ROOT_PASSWORD_HASHED: | |
1179 | r = free_and_strdup(&arg_root_password, optarg); | |
1180 | if (r < 0) | |
1181 | return log_oom(); | |
1182 | ||
1183 | arg_root_password_is_hashed = true; | |
1184 | break; | |
1185 | ||
1186 | case ARG_ROOT_SHELL: | |
1187 | r = find_shell(optarg, arg_root); | |
1188 | if (r < 0) | |
1189 | return r; | |
1190 | ||
1191 | r = free_and_strdup(&arg_root_shell, optarg); | |
1192 | if (r < 0) | |
1193 | return log_oom(); | |
1194 | ||
1195 | break; | |
1196 | ||
1197 | case ARG_HOSTNAME: | |
1198 | if (!hostname_is_valid(optarg, true)) | |
1199 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1200 | "Host name %s is not valid.", optarg); | |
1201 | ||
1202 | hostname_cleanup(optarg); | |
1203 | r = free_and_strdup(&arg_hostname, optarg); | |
1204 | if (r < 0) | |
1205 | return log_oom(); | |
1206 | ||
1207 | break; | |
1208 | ||
1209 | case ARG_MACHINE_ID: | |
1210 | if (sd_id128_from_string(optarg, &arg_machine_id) < 0) | |
1211 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1212 | "Failed to parse machine id %s.", optarg); | |
1213 | ||
1214 | break; | |
1215 | ||
1216 | case ARG_KERNEL_COMMAND_LINE: | |
1217 | r = free_and_strdup(&arg_kernel_cmdline, optarg); | |
1218 | if (r < 0) | |
1219 | return log_oom(); | |
1220 | ||
1221 | break; | |
1222 | ||
1223 | case ARG_PROMPT: | |
1224 | arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname = | |
1225 | arg_prompt_root_password = arg_prompt_root_shell = true; | |
1226 | break; | |
1227 | ||
1228 | case ARG_PROMPT_LOCALE: | |
1229 | arg_prompt_locale = true; | |
1230 | break; | |
1231 | ||
1232 | case ARG_PROMPT_KEYMAP: | |
1233 | arg_prompt_keymap = true; | |
1234 | break; | |
1235 | ||
1236 | case ARG_PROMPT_TIMEZONE: | |
1237 | arg_prompt_timezone = true; | |
1238 | break; | |
1239 | ||
1240 | case ARG_PROMPT_HOSTNAME: | |
1241 | arg_prompt_hostname = true; | |
1242 | break; | |
1243 | ||
1244 | case ARG_PROMPT_ROOT_PASSWORD: | |
1245 | arg_prompt_root_password = true; | |
1246 | break; | |
1247 | ||
1248 | case ARG_PROMPT_ROOT_SHELL: | |
1249 | arg_prompt_root_shell = true; | |
1250 | break; | |
1251 | ||
1252 | case ARG_COPY: | |
1253 | arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password = | |
1254 | arg_copy_root_shell = true; | |
1255 | break; | |
1256 | ||
1257 | case ARG_COPY_LOCALE: | |
1258 | arg_copy_locale = true; | |
1259 | break; | |
1260 | ||
1261 | case ARG_COPY_KEYMAP: | |
1262 | arg_copy_keymap = true; | |
1263 | break; | |
1264 | ||
1265 | case ARG_COPY_TIMEZONE: | |
1266 | arg_copy_timezone = true; | |
1267 | break; | |
1268 | ||
1269 | case ARG_COPY_ROOT_PASSWORD: | |
1270 | arg_copy_root_password = true; | |
1271 | break; | |
1272 | ||
1273 | case ARG_COPY_ROOT_SHELL: | |
1274 | arg_copy_root_shell = true; | |
1275 | break; | |
1276 | ||
1277 | case ARG_SETUP_MACHINE_ID: | |
1278 | r = sd_id128_randomize(&arg_machine_id); | |
1279 | if (r < 0) | |
1280 | return log_error_errno(r, "Failed to generate randomized machine ID: %m"); | |
1281 | ||
1282 | break; | |
1283 | ||
1284 | case ARG_FORCE: | |
1285 | arg_force = true; | |
1286 | break; | |
1287 | ||
1288 | case ARG_DELETE_ROOT_PASSWORD: | |
1289 | arg_delete_root_password = true; | |
1290 | break; | |
1291 | ||
1292 | case ARG_WELCOME: | |
1293 | r = parse_boolean(optarg); | |
1294 | if (r < 0) | |
1295 | return log_error_errno(r, "Failed to parse --welcome= argument: %s", optarg); | |
1296 | ||
1297 | arg_welcome = r; | |
1298 | break; | |
1299 | ||
1300 | case '?': | |
1301 | return -EINVAL; | |
1302 | ||
1303 | default: | |
1304 | assert_not_reached("Unhandled option"); | |
1305 | } | |
1306 | ||
1307 | /* We check if the specified locale strings are valid down here, so that we can take --root= into | |
1308 | * account when looking for the locale files. */ | |
1309 | ||
1310 | if (arg_locale && !locale_is_ok(arg_locale)) | |
1311 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale); | |
1312 | if (arg_locale_messages && !locale_is_ok(arg_locale_messages)) | |
1313 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale_messages); | |
1314 | ||
1315 | if (arg_delete_root_password && (arg_copy_root_password || arg_root_password || arg_prompt_root_password)) | |
1316 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1317 | "--delete-root-password cannot be combined with other root password options"); | |
1318 | ||
1319 | if (arg_image && arg_root) | |
1320 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported."); | |
1321 | ||
1322 | return 1; | |
1323 | } | |
1324 | ||
1325 | static int run(int argc, char *argv[]) { | |
1326 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; | |
1327 | _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL; | |
1328 | _cleanup_(umount_and_rmdir_and_freep) char *unlink_dir = NULL; | |
1329 | int r; | |
1330 | ||
1331 | r = parse_argv(argc, argv); | |
1332 | if (r <= 0) | |
1333 | return r; | |
1334 | ||
1335 | log_setup_service(); | |
1336 | ||
1337 | umask(0022); | |
1338 | ||
1339 | if (!arg_root && !arg_image) { | |
1340 | bool enabled; | |
1341 | ||
1342 | /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel | |
1343 | * command line option, because we are called to provision the host with basic settings (as | |
1344 | * opposed to some other file system tree/image) */ | |
1345 | ||
1346 | r = proc_cmdline_get_bool("systemd.firstboot", &enabled); | |
1347 | if (r < 0) | |
1348 | return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m"); | |
1349 | if (r > 0 && !enabled) | |
1350 | return 0; /* disabled */ | |
1351 | } | |
1352 | ||
1353 | r = setup_image(&unlink_dir, &loop_device, &decrypted_image); | |
1354 | if (r < 0) | |
1355 | return r; | |
1356 | ||
1357 | r = process_locale(); | |
1358 | if (r < 0) | |
1359 | return r; | |
1360 | ||
1361 | r = process_keymap(); | |
1362 | if (r < 0) | |
1363 | return r; | |
1364 | ||
1365 | r = process_timezone(); | |
1366 | if (r < 0) | |
1367 | return r; | |
1368 | ||
1369 | r = process_hostname(); | |
1370 | if (r < 0) | |
1371 | return r; | |
1372 | ||
1373 | r = process_machine_id(); | |
1374 | if (r < 0) | |
1375 | return r; | |
1376 | ||
1377 | r = process_root_args(); | |
1378 | if (r < 0) | |
1379 | return r; | |
1380 | ||
1381 | r = process_kernel_cmdline(); | |
1382 | if (r < 0) | |
1383 | return r; | |
1384 | ||
1385 | return 0; | |
1386 | } | |
1387 | ||
1388 | DEFINE_MAIN_FUNCTION(run); |