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