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