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