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