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