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