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