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