]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
418b9be5 | 2 | |
418b9be5 | 3 | #include <fcntl.h> |
418b9be5 | 4 | #include <getopt.h> |
3ff9fa59 | 5 | #include <linux/loop.h> |
3f6fd1ba | 6 | #include <unistd.h> |
418b9be5 | 7 | |
dccca82b LP |
8 | #include "sd-id128.h" |
9 | ||
b5efdb8a | 10 | #include "alloc-util.h" |
3f6fd1ba | 11 | #include "ask-password-api.h" |
d6b4d1c7 | 12 | #include "build.h" |
cea32691 ZJS |
13 | #include "bus-error.h" |
14 | #include "bus-locator.h" | |
a9399358 | 15 | #include "bus-unit-util.h" |
cea32691 | 16 | #include "bus-util.h" |
8eb668b9 | 17 | #include "bus-wait-for-jobs.h" |
f461a28d | 18 | #include "chase.h" |
418b9be5 | 19 | #include "copy.h" |
416f7b3a | 20 | #include "creds-util.h" |
3ff9fa59 | 21 | #include "dissect-image.h" |
686d13b9 | 22 | #include "env-file.h" |
7fc3f9c0 | 23 | #include "errno-util.h" |
6bedfcbb | 24 | #include "fd-util.h" |
3f6fd1ba | 25 | #include "fileio.h" |
f4f15635 | 26 | #include "fs-util.h" |
d8e32c47 | 27 | #include "glyph-util.h" |
3f6fd1ba | 28 | #include "hostname-util.h" |
f05e1d0d | 29 | #include "kbd-util.h" |
42f3b2f9 | 30 | #include "libcrypt-util.h" |
3f6fd1ba | 31 | #include "locale-util.h" |
b352e545 | 32 | #include "lock-util.h" |
45f39418 | 33 | #include "main-func.h" |
9ae4ef49 | 34 | #include "memory-util.h" |
418b9be5 | 35 | #include "mkdir.h" |
3ff9fa59 | 36 | #include "mount-util.h" |
d58ad743 | 37 | #include "os-util.h" |
614b022c | 38 | #include "parse-argument.h" |
6bedfcbb | 39 | #include "parse-util.h" |
d34b1823 | 40 | #include "password-quality-util.h" |
418b9be5 | 41 | #include "path-util.h" |
294bf0c3 | 42 | #include "pretty-print.h" |
f582cbca | 43 | #include "proc-cmdline.h" |
3df3e884 | 44 | #include "random-util.h" |
ce0458be | 45 | #include "smack-util.h" |
6bedfcbb | 46 | #include "string-util.h" |
3f6fd1ba | 47 | #include "strv.h" |
288a74cc | 48 | #include "terminal-util.h" |
3f6fd1ba | 49 | #include "time-util.h" |
b4909a3f | 50 | #include "tmpfile-util-label.h" |
3ff9fa59 | 51 | #include "tmpfile-util.h" |
affb60b1 | 52 | #include "umask-util.h" |
e929bee0 | 53 | #include "user-util.h" |
418b9be5 LP |
54 | |
55 | static char *arg_root = NULL; | |
3ff9fa59 | 56 | static char *arg_image = NULL; |
418b9be5 LP |
57 | static char *arg_locale = NULL; /* $LANG */ |
58 | static char *arg_locale_messages = NULL; /* $LC_MESSAGES */ | |
f8fd0930 | 59 | static char *arg_keymap = NULL; |
418b9be5 LP |
60 | static char *arg_timezone = NULL; |
61 | static char *arg_hostname = NULL; | |
62 | static sd_id128_t arg_machine_id = {}; | |
63 | static char *arg_root_password = NULL; | |
28900a1b | 64 | static char *arg_root_shell = NULL; |
a5925354 | 65 | static char *arg_kernel_cmdline = NULL; |
418b9be5 | 66 | static bool arg_prompt_locale = false; |
ed457f13 | 67 | static bool arg_prompt_keymap = false; |
418b9be5 LP |
68 | static bool arg_prompt_timezone = false; |
69 | static bool arg_prompt_hostname = false; | |
70 | static bool arg_prompt_root_password = false; | |
28900a1b | 71 | static bool arg_prompt_root_shell = false; |
418b9be5 | 72 | static bool arg_copy_locale = false; |
ed457f13 | 73 | static bool arg_copy_keymap = false; |
418b9be5 LP |
74 | static bool arg_copy_timezone = false; |
75 | static bool arg_copy_root_password = false; | |
28900a1b | 76 | static bool arg_copy_root_shell = false; |
b4909a3f | 77 | static bool arg_force = false; |
4926ceaf | 78 | static bool arg_delete_root_password = false; |
676339a1 | 79 | static bool arg_root_password_is_hashed = false; |
a1225020 | 80 | static bool arg_welcome = true; |
05eb2c60 | 81 | static bool arg_reset = false; |
84be0c71 | 82 | static ImagePolicy *arg_image_policy = NULL; |
418b9be5 | 83 | |
45f39418 | 84 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); |
3ff9fa59 | 85 | STATIC_DESTRUCTOR_REGISTER(arg_image, freep); |
45f39418 YW |
86 | STATIC_DESTRUCTOR_REGISTER(arg_locale, freep); |
87 | STATIC_DESTRUCTOR_REGISTER(arg_locale_messages, freep); | |
88 | STATIC_DESTRUCTOR_REGISTER(arg_keymap, freep); | |
89 | STATIC_DESTRUCTOR_REGISTER(arg_timezone, freep); | |
90 | STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep); | |
9ae4ef49 | 91 | STATIC_DESTRUCTOR_REGISTER(arg_root_password, erase_and_freep); |
4f464e74 AAF |
92 | STATIC_DESTRUCTOR_REGISTER(arg_root_shell, freep); |
93 | STATIC_DESTRUCTOR_REGISTER(arg_kernel_cmdline, freep); | |
84be0c71 | 94 | STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep); |
45f39418 | 95 | |
418b9be5 LP |
96 | static bool press_any_key(void) { |
97 | char k = 0; | |
98 | bool need_nl = true; | |
99 | ||
100 | printf("-- Press any key to proceed --"); | |
101 | fflush(stdout); | |
102 | ||
94956f8f | 103 | (void) read_one_char(stdin, &k, USEC_INFINITY, &need_nl); |
418b9be5 LP |
104 | |
105 | if (need_nl) | |
106 | putchar('\n'); | |
107 | ||
108 | return k != 'q'; | |
109 | } | |
110 | ||
a0657479 | 111 | static void print_welcome(int rfd) { |
02b7005e | 112 | _cleanup_free_ char *pretty_name = NULL, *os_name = NULL, *ansi_color = NULL; |
418b9be5 | 113 | static bool done = false; |
ae0d36c1 | 114 | const char *pn, *ac; |
418b9be5 LP |
115 | int r; |
116 | ||
a0657479 DDM |
117 | assert(rfd >= 0); |
118 | ||
a1225020 LP |
119 | if (!arg_welcome) |
120 | return; | |
121 | ||
418b9be5 LP |
122 | if (done) |
123 | return; | |
124 | ||
a0657479 DDM |
125 | r = parse_os_release_at(rfd, |
126 | "PRETTY_NAME", &pretty_name, | |
127 | "NAME", &os_name, | |
128 | "ANSI_COLOR", &ansi_color); | |
d58ad743 LP |
129 | if (r < 0) |
130 | log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, | |
131 | "Failed to read os-release file, ignoring: %m"); | |
418b9be5 | 132 | |
02b7005e | 133 | pn = os_release_pretty_name(pretty_name, os_name); |
ae0d36c1 | 134 | ac = isempty(ansi_color) ? "0" : ansi_color; |
4aeadec7 | 135 | |
5fee4ac0 LP |
136 | (void) reset_terminal_fd(STDIN_FILENO, /* switch_to_text= */ false); |
137 | ||
4aeadec7 | 138 | if (colors_enabled()) |
ae0d36c1 | 139 | printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ac, pn); |
4aeadec7 LP |
140 | else |
141 | printf("\nWelcome to your new installation of %s!\n", pn); | |
142 | ||
143 | printf("\nPlease configure your system!\n\n"); | |
418b9be5 LP |
144 | |
145 | press_any_key(); | |
146 | ||
147 | done = true; | |
148 | } | |
149 | ||
150 | static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) { | |
418b9be5 | 151 | unsigned break_lines, break_modulo; |
da6053d0 | 152 | size_t n, per_column, i, j; |
418b9be5 LP |
153 | |
154 | assert(n_columns > 0); | |
155 | ||
156 | n = strv_length(x); | |
be6b0c21 | 157 | per_column = DIV_ROUND_UP(n, n_columns); |
418b9be5 LP |
158 | |
159 | break_lines = lines(); | |
160 | if (break_lines > 2) | |
161 | break_lines--; | |
162 | ||
163 | /* The first page gets two extra lines, since we want to show | |
164 | * a title */ | |
165 | break_modulo = break_lines; | |
166 | if (break_modulo > 3) | |
167 | break_modulo -= 3; | |
168 | ||
169 | for (i = 0; i < per_column; i++) { | |
170 | ||
b3a9d980 | 171 | for (j = 0; j < n_columns; j++) { |
418b9be5 LP |
172 | _cleanup_free_ char *e = NULL; |
173 | ||
174 | if (j * per_column + i >= n) | |
175 | break; | |
176 | ||
177 | e = ellipsize(x[j * per_column + i], width, percentage); | |
178 | if (!e) | |
179 | return log_oom(); | |
180 | ||
f996072f | 181 | printf("%4zu) %-*s", j * per_column + i + 1, (int) width, e); |
418b9be5 LP |
182 | } |
183 | ||
184 | putchar('\n'); | |
185 | ||
186 | /* on the first screen we reserve 2 extra lines for the title */ | |
187 | if (i % break_lines == break_modulo) { | |
188 | if (!press_any_key()) | |
189 | return 0; | |
190 | } | |
191 | } | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
ecada8f2 | 196 | static int prompt_loop(const char *text, char **l, unsigned percentage, bool (*is_valid)(const char *name), char **ret) { |
418b9be5 LP |
197 | int r; |
198 | ||
199 | assert(text); | |
200 | assert(is_valid); | |
201 | assert(ret); | |
202 | ||
203 | for (;;) { | |
204 | _cleanup_free_ char *p = NULL; | |
205 | unsigned u; | |
206 | ||
ecada8f2 ZJS |
207 | r = ask_string(&p, "%s %s (empty to skip, \"list\" to list options): ", |
208 | special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), text); | |
23bbb0de MS |
209 | if (r < 0) |
210 | return log_error_errno(r, "Failed to query user: %m"); | |
418b9be5 LP |
211 | |
212 | if (isempty(p)) { | |
213 | log_warning("No data entered, skipping."); | |
214 | return 0; | |
215 | } | |
216 | ||
ecada8f2 ZJS |
217 | if (streq(p, "list")) { |
218 | r = show_menu(l, 3, 22, percentage); | |
219 | if (r < 0) | |
220 | return r; | |
221 | ||
222 | putchar('\n'); | |
223 | continue; | |
224 | }; | |
225 | ||
418b9be5 LP |
226 | r = safe_atou(p, &u); |
227 | if (r >= 0) { | |
418b9be5 LP |
228 | if (u <= 0 || u > strv_length(l)) { |
229 | log_error("Specified entry number out of range."); | |
230 | continue; | |
231 | } | |
232 | ||
233 | log_info("Selected '%s'.", l[u-1]); | |
b3f9c17a | 234 | return free_and_strdup_warn(ret, l[u-1]); |
418b9be5 LP |
235 | } |
236 | ||
237 | if (!is_valid(p)) { | |
238 | log_error("Entered data invalid."); | |
239 | continue; | |
240 | } | |
241 | ||
bfbf5f74 | 242 | return free_and_replace(*ret, p); |
418b9be5 LP |
243 | } |
244 | } | |
245 | ||
b352e545 | 246 | static int should_configure(int dir_fd, const char *filename) { |
a777a592 ZJS |
247 | _cleanup_fclose_ FILE *passwd = NULL, *shadow = NULL; |
248 | int r; | |
249 | ||
b352e545 DDM |
250 | assert(dir_fd >= 0); |
251 | assert(filename); | |
252 | ||
a777a592 ZJS |
253 | if (streq(filename, "passwd") && !arg_force) |
254 | /* We may need to do additional checks, so open the file. */ | |
255 | r = xfopenat(dir_fd, filename, "re", O_NOFOLLOW, &passwd); | |
256 | else | |
257 | r = RET_NERRNO(faccessat(dir_fd, filename, F_OK, AT_SYMLINK_NOFOLLOW)); | |
258 | ||
259 | if (r == -ENOENT) | |
260 | return true; /* missing */ | |
261 | if (r < 0) | |
262 | return log_error_errno(r, "Failed to access %s: %m", filename); | |
263 | if (arg_force) | |
264 | return true; /* exists, but if --force was given we should still configure the file. */ | |
265 | ||
266 | if (!passwd) | |
267 | return false; | |
268 | ||
269 | /* In case of /etc/passwd, do an additional check for the root password field. | |
270 | * We first check that passwd redirects to shadow, and then we check shadow. | |
271 | */ | |
272 | struct passwd *i; | |
273 | while ((r = fgetpwent_sane(passwd, &i)) > 0) { | |
274 | if (!streq(i->pw_name, "root")) | |
275 | continue; | |
276 | ||
277 | if (streq_ptr(i->pw_passwd, PASSWORD_SEE_SHADOW)) | |
278 | break; | |
279 | log_debug("passwd: root account with non-shadow password found, treating root as configured"); | |
280 | return false; | |
281 | } | |
282 | if (r < 0) | |
283 | return log_error_errno(r, "Failed to read %s: %m", filename); | |
284 | if (r == 0) { | |
285 | log_debug("No root account found in %s, assuming root is not configured.", filename); | |
286 | return true; | |
287 | } | |
df00c516 | 288 | |
a777a592 ZJS |
289 | r = xfopenat(dir_fd, "shadow", "re", O_NOFOLLOW, &shadow); |
290 | if (r == -ENOENT) { | |
291 | log_debug("No shadow file found, assuming root is not configured."); | |
df00c516 DDM |
292 | return true; /* missing */ |
293 | } | |
a777a592 ZJS |
294 | if (r < 0) |
295 | return log_error_errno(r, "Failed to access shadow: %m"); | |
b352e545 | 296 | |
a777a592 ZJS |
297 | struct spwd *j; |
298 | while ((r = fgetspent_sane(shadow, &j)) > 0) { | |
299 | if (!streq(j->sp_namp, "root")) | |
300 | continue; | |
301 | ||
302 | bool unprovisioned = streq_ptr(j->sp_pwdp, PASSWORD_UNPROVISIONED); | |
303 | log_debug("Root account found, %s.", | |
304 | unprovisioned ? "with unprovisioned password, treating root as not configured" : | |
305 | "treating root as configured"); | |
306 | return unprovisioned; | |
307 | } | |
308 | if (r < 0) | |
309 | return log_error_errno(r, "Failed to read shadow: %m"); | |
310 | assert(r == 0); | |
311 | log_debug("No root account found in shadow, assuming root is not configured."); | |
312 | return true; | |
b352e545 DDM |
313 | } |
314 | ||
a0657479 DDM |
315 | static bool locale_is_installed_bool(const char *name) { |
316 | return locale_is_installed(name) > 0; | |
317 | } | |
a00a78b8 | 318 | |
a0657479 DDM |
319 | static bool locale_is_ok(int rfd, const char *name) { |
320 | assert(rfd >= 0); | |
a00a78b8 | 321 | |
a0657479 | 322 | return dir_fd_is_root(rfd) ? locale_is_installed_bool(name) : locale_is_valid(name); |
a00a78b8 LP |
323 | } |
324 | ||
a0657479 | 325 | static int prompt_locale(int rfd) { |
418b9be5 | 326 | _cleanup_strv_free_ char **locales = NULL; |
416f7b3a | 327 | bool acquired_from_creds = false; |
418b9be5 LP |
328 | int r; |
329 | ||
a0657479 DDM |
330 | assert(rfd >= 0); |
331 | ||
418b9be5 LP |
332 | if (arg_locale || arg_locale_messages) |
333 | return 0; | |
334 | ||
416f7b3a LP |
335 | r = read_credential("firstboot.locale", (void**) &arg_locale, NULL); |
336 | if (r < 0) | |
337 | log_debug_errno(r, "Failed to read credential firstboot.locale, ignoring: %m"); | |
338 | else | |
339 | acquired_from_creds = true; | |
340 | ||
341 | r = read_credential("firstboot.locale-messages", (void**) &arg_locale_messages, NULL); | |
342 | if (r < 0) | |
8914f7e8 | 343 | log_debug_errno(r, "Failed to read credential firstboot.locale-messages, ignoring: %m"); |
416f7b3a LP |
344 | else |
345 | acquired_from_creds = true; | |
346 | ||
347 | if (acquired_from_creds) { | |
348 | log_debug("Acquired locale from credentials."); | |
349 | return 0; | |
350 | } | |
351 | ||
eb650ffe ZJS |
352 | if (!arg_prompt_locale) { |
353 | log_debug("Prompting for locale was not requested."); | |
418b9be5 | 354 | return 0; |
eb650ffe | 355 | } |
418b9be5 LP |
356 | |
357 | r = get_locales(&locales); | |
23bbb0de MS |
358 | if (r < 0) |
359 | return log_error_errno(r, "Cannot query locales list: %m"); | |
418b9be5 | 360 | |
bdd7cd26 LP |
361 | if (strv_isempty(locales)) |
362 | log_debug("No locales found, skipping locale selection."); | |
363 | else if (strv_length(locales) == 1) { | |
418b9be5 | 364 | |
bdd7cd26 LP |
365 | if (streq(locales[0], SYSTEMD_DEFAULT_LOCALE)) |
366 | log_debug("Only installed locale is default locale anyway, not setting locale explicitly."); | |
367 | else { | |
368 | log_debug("Only a single locale available (%s), selecting it as default.", locales[0]); | |
418b9be5 | 369 | |
bdd7cd26 LP |
370 | arg_locale = strdup(locales[0]); |
371 | if (!arg_locale) | |
372 | return log_oom(); | |
418b9be5 | 373 | |
bdd7cd26 LP |
374 | /* Not setting arg_locale_message here, since it defaults to LANG anyway */ |
375 | } | |
376 | } else { | |
a0657479 DDM |
377 | bool (*is_valid)(const char *name) = dir_fd_is_root(rfd) ? locale_is_installed_bool |
378 | : locale_is_valid; | |
379 | ||
380 | print_welcome(rfd); | |
418b9be5 | 381 | |
ecada8f2 | 382 | r = prompt_loop("Please enter system locale name or number", |
a0657479 | 383 | locales, 60, is_valid, &arg_locale); |
bdd7cd26 LP |
384 | if (r < 0) |
385 | return r; | |
386 | ||
387 | if (isempty(arg_locale)) | |
388 | return 0; | |
389 | ||
ecada8f2 | 390 | r = prompt_loop("Please enter system message locale name or number", |
a0657479 | 391 | locales, 60, is_valid, &arg_locale_messages); |
bdd7cd26 LP |
392 | if (r < 0) |
393 | return r; | |
394 | ||
395 | /* Suppress the messages setting if it's the same as the main locale anyway */ | |
396 | if (streq_ptr(arg_locale, arg_locale_messages)) | |
397 | arg_locale_messages = mfree(arg_locale_messages); | |
398 | } | |
418b9be5 LP |
399 | |
400 | return 0; | |
401 | } | |
402 | ||
b352e545 DDM |
403 | static int process_locale(int rfd) { |
404 | _cleanup_close_ int pfd = -EBADF; | |
405 | _cleanup_free_ char *f = NULL; | |
418b9be5 LP |
406 | char* locales[3]; |
407 | unsigned i = 0; | |
408 | int r; | |
409 | ||
b352e545 DDM |
410 | assert(rfd >= 0); |
411 | ||
412 | pfd = chase_and_open_parent_at(rfd, "/etc/locale.conf", | |
b39710cc | 413 | CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW, |
b352e545 DDM |
414 | &f); |
415 | if (pfd < 0) | |
416 | return log_error_errno(pfd, "Failed to chase /etc/locale.conf: %m"); | |
418b9be5 | 417 | |
b352e545 DDM |
418 | r = should_configure(pfd, f); |
419 | if (r == 0) | |
420 | log_debug("Found /etc/locale.conf, assuming locale information has been configured."); | |
421 | if (r <= 0) | |
422 | return r; | |
418b9be5 | 423 | |
fe75d5bc DDM |
424 | r = dir_fd_is_root(rfd); |
425 | if (r < 0) | |
426 | return log_error_errno(r, "Failed to check if directory file descriptor is root: %m"); | |
427 | ||
428 | if (arg_copy_locale && r == 0) { | |
b352e545 | 429 | r = copy_file_atomic_at(AT_FDCWD, "/etc/locale.conf", pfd, f, 0644, COPY_REFLINK); |
418b9be5 | 430 | if (r != -ENOENT) { |
23bbb0de | 431 | if (r < 0) |
b352e545 | 432 | return log_error_errno(r, "Failed to copy host's /etc/locale.conf: %m"); |
418b9be5 | 433 | |
b352e545 | 434 | log_info("Copied host's /etc/locale.conf."); |
418b9be5 LP |
435 | return 0; |
436 | } | |
437 | } | |
438 | ||
a0657479 | 439 | r = prompt_locale(rfd); |
418b9be5 LP |
440 | if (r < 0) |
441 | return r; | |
442 | ||
443 | if (!isempty(arg_locale)) | |
63c372cb | 444 | locales[i++] = strjoina("LANG=", arg_locale); |
4c4a73ce | 445 | if (!isempty(arg_locale_messages) && !streq_ptr(arg_locale_messages, arg_locale)) |
63c372cb | 446 | locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages); |
418b9be5 LP |
447 | |
448 | if (i == 0) | |
449 | return 0; | |
450 | ||
451 | locales[i] = NULL; | |
452 | ||
f155cb6d | 453 | r = write_env_file(pfd, f, NULL, locales); |
23bbb0de | 454 | if (r < 0) |
b352e545 | 455 | return log_error_errno(r, "Failed to write /etc/locale.conf: %m"); |
418b9be5 | 456 | |
b352e545 | 457 | log_info("/etc/locale.conf written."); |
d0c50d8d | 458 | return 1; |
418b9be5 LP |
459 | } |
460 | ||
321a8c59 ED |
461 | static bool keymap_exists_bool(const char *name) { |
462 | return keymap_exists(name) > 0; | |
463 | } | |
464 | ||
465 | static typeof(&keymap_is_valid) determine_keymap_validity_func(int rfd) { | |
466 | int r; | |
467 | ||
468 | r = dir_fd_is_root(rfd); | |
469 | if (r < 0) | |
470 | log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m"); | |
471 | ||
472 | return r != 0 ? keymap_exists_bool : keymap_is_valid; | |
473 | } | |
474 | ||
a0657479 | 475 | static int prompt_keymap(int rfd) { |
ed457f13 TB |
476 | _cleanup_strv_free_ char **kmaps = NULL; |
477 | int r; | |
478 | ||
a0657479 DDM |
479 | assert(rfd >= 0); |
480 | ||
ed457f13 TB |
481 | if (arg_keymap) |
482 | return 0; | |
483 | ||
416f7b3a LP |
484 | r = read_credential("firstboot.keymap", (void**) &arg_keymap, NULL); |
485 | if (r < 0) | |
486 | log_debug_errno(r, "Failed to read credential firstboot.keymap, ignoring: %m"); | |
487 | else { | |
488 | log_debug("Acquired keymap from credential."); | |
489 | return 0; | |
490 | } | |
491 | ||
eb650ffe ZJS |
492 | if (!arg_prompt_keymap) { |
493 | log_debug("Prompting for keymap was not requested."); | |
ed457f13 | 494 | return 0; |
eb650ffe | 495 | } |
ed457f13 TB |
496 | |
497 | r = get_keymaps(&kmaps); | |
498 | if (r == -ENOENT) /* no keymaps installed */ | |
eb650ffe | 499 | return log_debug_errno(r, "No keymaps are installed."); |
ed457f13 TB |
500 | if (r < 0) |
501 | return log_error_errno(r, "Failed to read keymaps: %m"); | |
502 | ||
a0657479 | 503 | print_welcome(rfd); |
ed457f13 | 504 | |
8700b4da | 505 | return prompt_loop("Please enter system keymap name or number", |
321a8c59 | 506 | kmaps, 60, determine_keymap_validity_func(rfd), &arg_keymap); |
ed457f13 TB |
507 | } |
508 | ||
b352e545 DDM |
509 | static int process_keymap(int rfd) { |
510 | _cleanup_close_ int pfd = -EBADF; | |
511 | _cleanup_free_ char *f = NULL; | |
ed457f13 TB |
512 | char **keymap; |
513 | int r; | |
514 | ||
b352e545 | 515 | assert(rfd >= 0); |
ed457f13 | 516 | |
b352e545 | 517 | pfd = chase_and_open_parent_at(rfd, "/etc/vconsole.conf", |
b39710cc | 518 | CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW, |
b352e545 DDM |
519 | &f); |
520 | if (pfd < 0) | |
521 | return log_error_errno(pfd, "Failed to chase /etc/vconsole.conf: %m"); | |
ed457f13 | 522 | |
b352e545 DDM |
523 | r = should_configure(pfd, f); |
524 | if (r == 0) | |
525 | log_debug("Found /etc/vconsole.conf, assuming console has been configured."); | |
526 | if (r <= 0) | |
527 | return r; | |
528 | ||
fe75d5bc DDM |
529 | r = dir_fd_is_root(rfd); |
530 | if (r < 0) | |
531 | return log_error_errno(r, "Failed to check if directory file descriptor is root: %m"); | |
532 | ||
533 | if (arg_copy_keymap && r == 0) { | |
b352e545 | 534 | r = copy_file_atomic_at(AT_FDCWD, "/etc/vconsole.conf", pfd, f, 0644, COPY_REFLINK); |
ed457f13 TB |
535 | if (r != -ENOENT) { |
536 | if (r < 0) | |
b352e545 | 537 | return log_error_errno(r, "Failed to copy host's /etc/vconsole.conf: %m"); |
ed457f13 | 538 | |
b352e545 | 539 | log_info("Copied host's /etc/vconsole.conf."); |
ed457f13 TB |
540 | return 0; |
541 | } | |
542 | } | |
543 | ||
a0657479 | 544 | r = prompt_keymap(rfd); |
ed457f13 TB |
545 | if (r == -ENOENT) |
546 | return 0; /* don't fail if no keymaps are installed */ | |
547 | if (r < 0) | |
548 | return r; | |
549 | ||
a7353b4d | 550 | if (isempty(arg_keymap)) |
ed457f13 TB |
551 | return 0; |
552 | ||
a7353b4d YW |
553 | keymap = STRV_MAKE(strjoina("KEYMAP=", arg_keymap)); |
554 | ||
0e7a7cd4 | 555 | r = write_vconsole_conf(pfd, f, keymap); |
ed457f13 | 556 | if (r < 0) |
b352e545 | 557 | return log_error_errno(r, "Failed to write /etc/vconsole.conf: %m"); |
ed457f13 | 558 | |
b352e545 | 559 | log_info("/etc/vconsole.conf written."); |
cea32691 | 560 | return 1; |
ed457f13 TB |
561 | } |
562 | ||
089fb865 MG |
563 | static bool timezone_is_valid_log_error(const char *name) { |
564 | return timezone_is_valid(name, LOG_ERR); | |
565 | } | |
566 | ||
a0657479 | 567 | static int prompt_timezone(int rfd) { |
418b9be5 LP |
568 | _cleanup_strv_free_ char **zones = NULL; |
569 | int r; | |
570 | ||
a0657479 DDM |
571 | assert(rfd >= 0); |
572 | ||
418b9be5 LP |
573 | if (arg_timezone) |
574 | return 0; | |
575 | ||
416f7b3a LP |
576 | r = read_credential("firstboot.timezone", (void**) &arg_timezone, NULL); |
577 | if (r < 0) | |
578 | log_debug_errno(r, "Failed to read credential firstboot.timezone, ignoring: %m"); | |
579 | else { | |
580 | log_debug("Acquired timezone from credential."); | |
581 | return 0; | |
582 | } | |
583 | ||
eb650ffe ZJS |
584 | if (!arg_prompt_timezone) { |
585 | log_debug("Prompting for timezone was not requested."); | |
418b9be5 | 586 | return 0; |
eb650ffe | 587 | } |
418b9be5 LP |
588 | |
589 | r = get_timezones(&zones); | |
23bbb0de MS |
590 | if (r < 0) |
591 | return log_error_errno(r, "Cannot query timezone list: %m"); | |
418b9be5 | 592 | |
a0657479 | 593 | print_welcome(rfd); |
418b9be5 | 594 | |
ecada8f2 ZJS |
595 | r = prompt_loop("Please enter timezone name or number", |
596 | zones, 30, timezone_is_valid_log_error, &arg_timezone); | |
418b9be5 LP |
597 | if (r < 0) |
598 | return r; | |
599 | ||
600 | return 0; | |
601 | } | |
602 | ||
b352e545 DDM |
603 | static int process_timezone(int rfd) { |
604 | _cleanup_close_ int pfd = -EBADF; | |
605 | _cleanup_free_ char *f = NULL; | |
606 | const char *e; | |
418b9be5 LP |
607 | int r; |
608 | ||
b352e545 | 609 | assert(rfd >= 0); |
418b9be5 | 610 | |
b352e545 DDM |
611 | pfd = chase_and_open_parent_at(rfd, "/etc/localtime", |
612 | CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW, | |
613 | &f); | |
614 | if (pfd < 0) | |
615 | return log_error_errno(pfd, "Failed to chase /etc/localtime: %m"); | |
418b9be5 | 616 | |
b352e545 DDM |
617 | r = should_configure(pfd, f); |
618 | if (r == 0) | |
619 | log_debug("Found /etc/localtime, assuming timezone has been configured."); | |
620 | if (r <= 0) | |
621 | return r; | |
622 | ||
fe75d5bc DDM |
623 | r = dir_fd_is_root(rfd); |
624 | if (r < 0) | |
625 | return log_error_errno(r, "Failed to check if directory file descriptor is root: %m"); | |
626 | ||
627 | if (arg_copy_timezone && r == 0) { | |
b352e545 DDM |
628 | _cleanup_free_ char *s = NULL; |
629 | ||
630 | r = readlink_malloc("/etc/localtime", &s); | |
418b9be5 | 631 | if (r != -ENOENT) { |
23bbb0de | 632 | if (r < 0) |
b352e545 | 633 | return log_error_errno(r, "Failed to read host's /etc/localtime: %m"); |
418b9be5 | 634 | |
b352e545 | 635 | r = symlinkat_atomic_full(s, pfd, f, /* make_relative= */ false); |
e56dc320 | 636 | if (r < 0) |
b352e545 | 637 | return log_error_errno(r, "Failed to create /etc/localtime symlink: %m"); |
418b9be5 | 638 | |
b352e545 | 639 | log_info("Copied host's /etc/localtime."); |
418b9be5 LP |
640 | return 0; |
641 | } | |
642 | } | |
643 | ||
a0657479 | 644 | r = prompt_timezone(rfd); |
418b9be5 LP |
645 | if (r < 0) |
646 | return r; | |
647 | ||
648 | if (isempty(arg_timezone)) | |
649 | return 0; | |
650 | ||
63c372cb | 651 | e = strjoina("../usr/share/zoneinfo/", arg_timezone); |
418b9be5 | 652 | |
b352e545 | 653 | r = symlinkat_atomic_full(e, pfd, f, /* make_relative= */ false); |
e56dc320 | 654 | if (r < 0) |
b352e545 | 655 | return log_error_errno(r, "Failed to create /etc/localtime symlink: %m"); |
418b9be5 | 656 | |
b352e545 | 657 | log_info("/etc/localtime written"); |
418b9be5 LP |
658 | return 0; |
659 | } | |
660 | ||
a0657479 | 661 | static int prompt_hostname(int rfd) { |
418b9be5 LP |
662 | int r; |
663 | ||
a0657479 DDM |
664 | assert(rfd >= 0); |
665 | ||
418b9be5 LP |
666 | if (arg_hostname) |
667 | return 0; | |
668 | ||
eb650ffe ZJS |
669 | if (!arg_prompt_hostname) { |
670 | log_debug("Prompting for hostname was not requested."); | |
418b9be5 | 671 | return 0; |
eb650ffe | 672 | } |
418b9be5 | 673 | |
a0657479 | 674 | print_welcome(rfd); |
418b9be5 LP |
675 | putchar('\n'); |
676 | ||
677 | for (;;) { | |
678 | _cleanup_free_ char *h = NULL; | |
679 | ||
9a6f746f | 680 | r = ask_string(&h, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET)); |
23bbb0de MS |
681 | if (r < 0) |
682 | return log_error_errno(r, "Failed to query hostname: %m"); | |
418b9be5 LP |
683 | |
684 | if (isempty(h)) { | |
685 | log_warning("No hostname entered, skipping."); | |
686 | break; | |
687 | } | |
688 | ||
52ef5dd7 | 689 | if (!hostname_is_valid(h, VALID_HOSTNAME_TRAILING_DOT)) { |
418b9be5 LP |
690 | log_error("Specified hostname invalid."); |
691 | continue; | |
692 | } | |
693 | ||
34ad6090 | 694 | /* Get rid of the trailing dot that we allow, but don't want to see */ |
ae691c1d | 695 | arg_hostname = hostname_cleanup(h); |
418b9be5 LP |
696 | h = NULL; |
697 | break; | |
698 | } | |
699 | ||
700 | return 0; | |
701 | } | |
702 | ||
b352e545 DDM |
703 | static int process_hostname(int rfd) { |
704 | _cleanup_close_ int pfd = -EBADF; | |
705 | _cleanup_free_ char *f = NULL; | |
418b9be5 LP |
706 | int r; |
707 | ||
b352e545 DDM |
708 | assert(rfd >= 0); |
709 | ||
710 | pfd = chase_and_open_parent_at(rfd, "/etc/hostname", | |
711 | CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN, | |
712 | &f); | |
713 | if (pfd < 0) | |
714 | return log_error_errno(pfd, "Failed to chase /etc/hostname: %m"); | |
715 | ||
716 | r = should_configure(pfd, f); | |
717 | if (r == 0) | |
718 | log_debug("Found /etc/hostname, assuming hostname has been configured."); | |
719 | if (r <= 0) | |
720 | return r; | |
418b9be5 | 721 | |
a0657479 | 722 | r = prompt_hostname(rfd); |
418b9be5 LP |
723 | if (r < 0) |
724 | return r; | |
725 | ||
726 | if (isempty(arg_hostname)) | |
727 | return 0; | |
728 | ||
b352e545 DDM |
729 | r = write_string_file_at(pfd, f, arg_hostname, |
730 | WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC); | |
23bbb0de | 731 | if (r < 0) |
b352e545 | 732 | return log_error_errno(r, "Failed to write /etc/hostname: %m"); |
418b9be5 | 733 | |
b352e545 | 734 | log_info("/etc/hostname written."); |
418b9be5 LP |
735 | return 0; |
736 | } | |
737 | ||
b352e545 DDM |
738 | static int process_machine_id(int rfd) { |
739 | _cleanup_close_ int pfd = -EBADF; | |
740 | _cleanup_free_ char *f = NULL; | |
418b9be5 LP |
741 | int r; |
742 | ||
b352e545 DDM |
743 | assert(rfd >= 0); |
744 | ||
745 | pfd = chase_and_open_parent_at(rfd, "/etc/machine-id", | |
b39710cc | 746 | CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW, |
b352e545 DDM |
747 | &f); |
748 | if (pfd < 0) | |
749 | return log_error_errno(pfd, "Failed to chase /etc/machine-id: %m"); | |
750 | ||
751 | r = should_configure(pfd, f); | |
752 | if (r == 0) | |
753 | log_debug("Found /etc/machine-id, assuming machine-id has been configured."); | |
754 | if (r <= 0) | |
755 | return r; | |
418b9be5 | 756 | |
eb650ffe ZJS |
757 | if (sd_id128_is_null(arg_machine_id)) { |
758 | log_debug("Initialization of machine-id was not requested, skipping."); | |
418b9be5 | 759 | return 0; |
eb650ffe | 760 | } |
418b9be5 | 761 | |
b352e545 DDM |
762 | r = write_string_file_at(pfd, "machine-id", SD_ID128_TO_STRING(arg_machine_id), |
763 | WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC); | |
23bbb0de | 764 | if (r < 0) |
c7c1edd6 | 765 | return log_error_errno(r, "Failed to write /etc/machine-id: %m"); |
418b9be5 | 766 | |
b352e545 | 767 | log_info("/etc/machine-id written."); |
418b9be5 LP |
768 | return 0; |
769 | } | |
770 | ||
a0657479 | 771 | static int prompt_root_password(int rfd) { |
1fbc95d3 | 772 | const char *msg1, *msg2; |
418b9be5 LP |
773 | int r; |
774 | ||
a0657479 DDM |
775 | assert(rfd >= 0); |
776 | ||
418b9be5 LP |
777 | if (arg_root_password) |
778 | return 0; | |
779 | ||
ff86850b | 780 | if (get_credential_user_password("root", &arg_root_password, &arg_root_password_is_hashed) >= 0) |
416f7b3a | 781 | return 0; |
416f7b3a | 782 | |
eb650ffe ZJS |
783 | if (!arg_prompt_root_password) { |
784 | log_debug("Prompting for root password was not requested."); | |
418b9be5 | 785 | return 0; |
eb650ffe | 786 | } |
418b9be5 | 787 | |
a0657479 | 788 | print_welcome(rfd); |
418b9be5 LP |
789 | putchar('\n'); |
790 | ||
3619634c LP |
791 | msg1 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter a new root password (empty to skip):"); |
792 | msg2 = strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET), " Please enter new root password again:"); | |
418b9be5 | 793 | |
7baf10a7 LP |
794 | suggest_passwords(); |
795 | ||
418b9be5 | 796 | for (;;) { |
c7b7d74e | 797 | _cleanup_strv_free_erase_ char **a = NULL, **b = NULL; |
7baf10a7 | 798 | _cleanup_free_ char *error = NULL; |
418b9be5 | 799 | |
d08fd4c3 LP |
800 | AskPasswordRequest req = { |
801 | .message = msg1, | |
802 | }; | |
803 | ||
804 | r = ask_password_tty(-EBADF, &req, /* until= */ 0, /* flags= */ 0, /* flag_file= */ NULL, &a); | |
23bbb0de MS |
805 | if (r < 0) |
806 | return log_error_errno(r, "Failed to query root password: %m"); | |
39e96f84 ZJS |
807 | if (strv_length(a) != 1) |
808 | return log_error_errno(SYNTHETIC_ERRNO(EIO), | |
809 | "Received multiple passwords, where we expected one."); | |
418b9be5 | 810 | |
c7b7d74e | 811 | if (isempty(*a)) { |
418b9be5 LP |
812 | log_warning("No password entered, skipping."); |
813 | break; | |
814 | } | |
815 | ||
d34b1823 | 816 | r = check_password_quality(*a, /* old */ NULL, "root", &error); |
bb44fd07 ZJS |
817 | if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
818 | log_warning("Password quality check is not supported, proceeding anyway."); | |
819 | else if (r < 0) | |
820 | return log_error_errno(r, "Failed to check password quality: %m"); | |
821 | else if (r == 0) | |
7baf10a7 LP |
822 | log_warning("Password is weak, accepting anyway: %s", error); |
823 | ||
d08fd4c3 LP |
824 | req.message = msg2; |
825 | ||
826 | r = ask_password_tty(-EBADF, &req, /* until= */ 0, /* flags= */ 0, /* flag_file= */ NULL, &b); | |
ab84f5b9 | 827 | if (r < 0) |
00843602 | 828 | return log_error_errno(r, "Failed to query root password: %m"); |
39e96f84 ZJS |
829 | if (strv_length(b) != 1) |
830 | return log_error_errno(SYNTHETIC_ERRNO(EIO), | |
831 | "Received multiple passwords, where we expected one."); | |
418b9be5 | 832 | |
c7b7d74e | 833 | if (!streq(*a, *b)) { |
418b9be5 | 834 | log_error("Entered passwords did not match, please try again."); |
418b9be5 LP |
835 | continue; |
836 | } | |
837 | ||
c7b7d74e | 838 | arg_root_password = TAKE_PTR(*a); |
418b9be5 LP |
839 | break; |
840 | } | |
841 | ||
842 | return 0; | |
843 | } | |
844 | ||
b352e545 | 845 | static int find_shell(int rfd, const char *path) { |
31363bd5 DDM |
846 | int r; |
847 | ||
848 | assert(path); | |
849 | ||
850 | if (!valid_shell(path)) | |
851 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "%s is not a valid shell", path); | |
852 | ||
b352e545 DDM |
853 | r = chaseat(rfd, path, CHASE_AT_RESOLVE_IN_ROOT, NULL, NULL); |
854 | if (r < 0) | |
855 | return log_error_errno(r, "Failed to resolve shell %s: %m", path); | |
31363bd5 DDM |
856 | |
857 | return 0; | |
858 | } | |
859 | ||
b352e545 | 860 | static int prompt_root_shell(int rfd) { |
28900a1b DDM |
861 | int r; |
862 | ||
a0657479 DDM |
863 | assert(rfd >= 0); |
864 | ||
416f7b3a LP |
865 | if (arg_root_shell) |
866 | return 0; | |
867 | ||
868 | r = read_credential("passwd.shell.root", (void**) &arg_root_shell, NULL); | |
869 | if (r < 0) | |
870 | log_debug_errno(r, "Failed to read credential passwd.shell.root, ignoring: %m"); | |
871 | else { | |
872 | log_debug("Acquired root shell from credential."); | |
873 | return 0; | |
874 | } | |
875 | ||
eb650ffe ZJS |
876 | if (!arg_prompt_root_shell) { |
877 | log_debug("Prompting for root shell was not requested."); | |
28900a1b | 878 | return 0; |
eb650ffe | 879 | } |
28900a1b | 880 | |
a0657479 | 881 | print_welcome(rfd); |
28900a1b DDM |
882 | putchar('\n'); |
883 | ||
884 | for (;;) { | |
885 | _cleanup_free_ char *s = NULL; | |
886 | ||
887 | r = ask_string(&s, "%s Please enter root shell for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET)); | |
888 | if (r < 0) | |
889 | return log_error_errno(r, "Failed to query root shell: %m"); | |
890 | ||
891 | if (isempty(s)) { | |
892 | log_warning("No shell entered, skipping."); | |
893 | break; | |
894 | } | |
895 | ||
b352e545 | 896 | r = find_shell(rfd, s); |
31363bd5 | 897 | if (r < 0) |
28900a1b | 898 | continue; |
28900a1b DDM |
899 | |
900 | arg_root_shell = TAKE_PTR(s); | |
901 | break; | |
902 | } | |
903 | ||
904 | return 0; | |
905 | } | |
906 | ||
a0657479 | 907 | static int write_root_passwd(int rfd, int etc_fd, const char *password, const char *shell) { |
4926ceaf DDM |
908 | _cleanup_fclose_ FILE *original = NULL, *passwd = NULL; |
909 | _cleanup_(unlink_and_freep) char *passwd_tmp = NULL; | |
910 | int r; | |
911 | ||
c4a53ebf DDM |
912 | assert(password); |
913 | ||
b352e545 | 914 | r = fopen_temporary_at_label(etc_fd, "passwd", "passwd", &passwd, &passwd_tmp); |
4926ceaf DDM |
915 | if (r < 0) |
916 | return r; | |
917 | ||
b352e545 DDM |
918 | r = xfopenat(etc_fd, "passwd", "re", O_NOFOLLOW, &original); |
919 | if (r < 0 && r != -ENOENT) | |
920 | return r; | |
921 | ||
4926ceaf DDM |
922 | if (original) { |
923 | struct passwd *i; | |
924 | ||
bb72c434 | 925 | r = copy_rights(fileno(original), fileno(passwd)); |
4926ceaf DDM |
926 | if (r < 0) |
927 | return r; | |
928 | ||
929 | while ((r = fgetpwent_sane(original, &i)) > 0) { | |
930 | ||
28900a1b | 931 | if (streq(i->pw_name, "root")) { |
4926ceaf | 932 | i->pw_passwd = (char *) password; |
28900a1b DDM |
933 | if (shell) |
934 | i->pw_shell = (char *) shell; | |
935 | } | |
4926ceaf DDM |
936 | |
937 | r = putpwent_sane(i, passwd); | |
938 | if (r < 0) | |
939 | return r; | |
940 | } | |
941 | if (r < 0) | |
942 | return r; | |
943 | ||
944 | } else { | |
945 | struct passwd root = { | |
946 | .pw_name = (char *) "root", | |
947 | .pw_passwd = (char *) password, | |
948 | .pw_uid = 0, | |
949 | .pw_gid = 0, | |
950 | .pw_gecos = (char *) "Super User", | |
951 | .pw_dir = (char *) "/root", | |
a0657479 | 952 | .pw_shell = (char *) (shell ?: default_root_shell_at(rfd)), |
4926ceaf DDM |
953 | }; |
954 | ||
955 | if (errno != ENOENT) | |
956 | return -errno; | |
957 | ||
b226422c | 958 | r = fchmod(fileno(passwd), 0644); |
4926ceaf DDM |
959 | if (r < 0) |
960 | return -errno; | |
961 | ||
962 | r = putpwent_sane(&root, passwd); | |
963 | if (r < 0) | |
964 | return r; | |
965 | } | |
966 | ||
967 | r = fflush_sync_and_check(passwd); | |
968 | if (r < 0) | |
969 | return r; | |
970 | ||
b352e545 | 971 | r = renameat_and_apply_smack_floor_label(etc_fd, passwd_tmp, etc_fd, "passwd"); |
4926ceaf DDM |
972 | if (r < 0) |
973 | return r; | |
974 | ||
975 | return 0; | |
976 | } | |
977 | ||
b352e545 | 978 | static int write_root_shadow(int etc_fd, const char *hashed_password) { |
b4909a3f DDM |
979 | _cleanup_fclose_ FILE *original = NULL, *shadow = NULL; |
980 | _cleanup_(unlink_and_freep) char *shadow_tmp = NULL; | |
100d5f6e FB |
981 | int r; |
982 | ||
c4a53ebf DDM |
983 | assert(hashed_password); |
984 | ||
b352e545 | 985 | r = fopen_temporary_at_label(etc_fd, "shadow", "shadow", &shadow, &shadow_tmp); |
b4909a3f DDM |
986 | if (r < 0) |
987 | return r; | |
988 | ||
b352e545 DDM |
989 | r = xfopenat(etc_fd, "shadow", "re", O_NOFOLLOW, &original); |
990 | if (r < 0 && r != -ENOENT) | |
991 | return r; | |
992 | ||
b4909a3f DDM |
993 | if (original) { |
994 | struct spwd *i; | |
995 | ||
bb72c434 | 996 | r = copy_rights(fileno(original), fileno(shadow)); |
b4909a3f DDM |
997 | if (r < 0) |
998 | return r; | |
999 | ||
1000 | while ((r = fgetspent_sane(original, &i)) > 0) { | |
1001 | ||
1002 | if (streq(i->sp_namp, "root")) { | |
1003 | i->sp_pwdp = (char *) hashed_password; | |
1004 | i->sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY); | |
1005 | } | |
1006 | ||
1007 | r = putspent_sane(i, shadow); | |
1008 | if (r < 0) | |
1009 | return r; | |
1010 | } | |
1011 | if (r < 0) | |
1012 | return r; | |
1013 | ||
1014 | } else { | |
1015 | struct spwd root = { | |
1016 | .sp_namp = (char*) "root", | |
1017 | .sp_pwdp = (char *) hashed_password, | |
1018 | .sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY), | |
1019 | .sp_min = -1, | |
1020 | .sp_max = -1, | |
1021 | .sp_warn = -1, | |
1022 | .sp_inact = -1, | |
1023 | .sp_expire = -1, | |
f5fbe71d | 1024 | .sp_flag = ULONG_MAX, /* this appears to be what everybody does ... */ |
b4909a3f DDM |
1025 | }; |
1026 | ||
1027 | if (errno != ENOENT) | |
1028 | return -errno; | |
418b9be5 | 1029 | |
b4909a3f DDM |
1030 | r = fchmod(fileno(shadow), 0000); |
1031 | if (r < 0) | |
1032 | return -errno; | |
418b9be5 | 1033 | |
b4909a3f DDM |
1034 | r = putspent_sane(&root, shadow); |
1035 | if (r < 0) | |
1036 | return r; | |
1037 | } | |
1038 | ||
1039 | r = fflush_sync_and_check(shadow); | |
100d5f6e FB |
1040 | if (r < 0) |
1041 | return r; | |
418b9be5 | 1042 | |
b352e545 | 1043 | r = renameat_and_apply_smack_floor_label(etc_fd, shadow_tmp, etc_fd, "shadow"); |
b4909a3f DDM |
1044 | if (r < 0) |
1045 | return r; | |
1046 | ||
1047 | return 0; | |
418b9be5 LP |
1048 | } |
1049 | ||
b352e545 DDM |
1050 | static int process_root_account(int rfd) { |
1051 | _cleanup_close_ int pfd = -EBADF; | |
1052 | _cleanup_(release_lock_file) LockFile lock = LOCK_FILE_INIT; | |
0e98d17e | 1053 | _cleanup_(erase_and_freep) char *_hashed_password = NULL; |
c4a53ebf | 1054 | const char *password, *hashed_password; |
b352e545 DDM |
1055 | int k = 0, r; |
1056 | ||
1057 | assert(rfd >= 0); | |
1058 | ||
1059 | pfd = chase_and_open_parent_at(rfd, "/etc/passwd", | |
1060 | CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW, | |
1061 | NULL); | |
1062 | if (pfd < 0) | |
1063 | return log_error_errno(pfd, "Failed to chase /etc/passwd: %m"); | |
1064 | ||
1065 | /* Ensure that passwd and shadow are in the same directory and are not symlinks. */ | |
1066 | ||
1067 | FOREACH_STRING(s, "passwd", "shadow") { | |
1068 | r = verify_regular_at(pfd, s, /* follow = */ false); | |
1069 | if (IN_SET(r, -EISDIR, -ELOOP, -EBADFD)) | |
1070 | return log_error_errno(r, "/etc/%s is not a regular file", s); | |
1071 | if (r < 0 && r != -ENOENT) | |
1072 | return log_error_errno(r, "Failed to check whether /etc/%s is a regular file: %m", s); | |
1073 | ||
1074 | r = should_configure(pfd, s); | |
1075 | if (r < 0) | |
1076 | return r; | |
418b9be5 | 1077 | |
b352e545 DDM |
1078 | k += r; |
1079 | } | |
c4a53ebf | 1080 | |
b352e545 DDM |
1081 | if (k == 0) { |
1082 | log_debug("Found /etc/passwd and /etc/shadow, assuming root account has been initialized."); | |
418b9be5 | 1083 | return 0; |
eb650ffe | 1084 | } |
87a3a4a8 | 1085 | |
67d5d9d5 | 1086 | /* Don't create/modify passwd and shadow if not asked */ |
1087 | if (!(arg_root_password || arg_prompt_root_password || arg_copy_root_password || arg_delete_root_password || | |
eb650ffe ZJS |
1088 | arg_root_shell || arg_prompt_root_shell || arg_copy_root_shell)) { |
1089 | log_debug("Initialization of root account was not requested, skipping."); | |
67d5d9d5 | 1090 | return 0; |
eb650ffe | 1091 | } |
418b9be5 | 1092 | |
fe585662 | 1093 | r = make_lock_file_at(pfd, ETC_PASSWD_LOCK_FILENAME, LOCK_EX, &lock); |
b352e545 DDM |
1094 | if (r < 0) |
1095 | return log_error_errno(r, "Failed to take a lock on /etc/passwd: %m"); | |
4926ceaf | 1096 | |
fe75d5bc DDM |
1097 | k = dir_fd_is_root(rfd); |
1098 | if (k < 0) | |
1099 | return log_error_errno(k, "Failed to check if directory file descriptor is root: %m"); | |
1100 | ||
1101 | if (arg_copy_root_shell && k == 0) { | |
75673cd8 | 1102 | _cleanup_free_ struct passwd *p = NULL; |
28900a1b | 1103 | |
75673cd8 LP |
1104 | r = getpwnam_malloc("root", &p); |
1105 | if (r < 0) | |
1106 | return log_error_errno(r, "Failed to find passwd entry for root: %m"); | |
28900a1b DDM |
1107 | |
1108 | r = free_and_strdup(&arg_root_shell, p->pw_shell); | |
1109 | if (r < 0) | |
1110 | return log_oom(); | |
1111 | } | |
1112 | ||
b352e545 | 1113 | r = prompt_root_shell(rfd); |
28900a1b DDM |
1114 | if (r < 0) |
1115 | return r; | |
1116 | ||
fe75d5bc | 1117 | if (arg_copy_root_password && k == 0) { |
418b9be5 LP |
1118 | struct spwd *p; |
1119 | ||
1120 | errno = 0; | |
1121 | p = getspnam("root"); | |
c4a53ebf DDM |
1122 | if (!p) |
1123 | return log_error_errno(errno_or_else(EIO), "Failed to find shadow entry for root: %m"); | |
418b9be5 | 1124 | |
c4a53ebf DDM |
1125 | r = free_and_strdup(&arg_root_password, p->sp_pwdp); |
1126 | if (r < 0) | |
1127 | return log_oom(); | |
418b9be5 | 1128 | |
c4a53ebf | 1129 | arg_root_password_is_hashed = true; |
418b9be5 LP |
1130 | } |
1131 | ||
a0657479 | 1132 | r = prompt_root_password(rfd); |
418b9be5 LP |
1133 | if (r < 0) |
1134 | return r; | |
1135 | ||
c4a53ebf | 1136 | if (arg_root_password && arg_root_password_is_hashed) { |
53c25ac9 | 1137 | password = PASSWORD_SEE_SHADOW; |
676339a1 | 1138 | hashed_password = arg_root_password; |
c4a53ebf | 1139 | } else if (arg_root_password) { |
0e98d17e ZJS |
1140 | r = hash_password(arg_root_password, &_hashed_password); |
1141 | if (r < 0) | |
1142 | return log_error_errno(r, "Failed to hash password: %m"); | |
676339a1 | 1143 | |
53c25ac9 | 1144 | password = PASSWORD_SEE_SHADOW; |
0e98d17e | 1145 | hashed_password = _hashed_password; |
c4a53ebf | 1146 | |
c4a53ebf | 1147 | } else if (arg_delete_root_password) |
53c25ac9 | 1148 | password = hashed_password = PASSWORD_NONE; |
c4a53ebf | 1149 | else |
53c25ac9 | 1150 | password = hashed_password = PASSWORD_LOCKED_AND_INVALID; |
c4a53ebf | 1151 | |
a0657479 | 1152 | r = write_root_passwd(rfd, pfd, password, arg_root_shell); |
c4a53ebf | 1153 | if (r < 0) |
b352e545 | 1154 | return log_error_errno(r, "Failed to write /etc/passwd: %m"); |
c4a53ebf | 1155 | |
b352e545 | 1156 | log_info("/etc/passwd written."); |
418b9be5 | 1157 | |
b352e545 | 1158 | r = write_root_shadow(pfd, hashed_password); |
23bbb0de | 1159 | if (r < 0) |
b352e545 | 1160 | return log_error_errno(r, "Failed to write /etc/shadow: %m"); |
418b9be5 | 1161 | |
b352e545 | 1162 | log_info("/etc/shadow written."); |
418b9be5 LP |
1163 | return 0; |
1164 | } | |
1165 | ||
b352e545 DDM |
1166 | static int process_kernel_cmdline(int rfd) { |
1167 | _cleanup_close_ int pfd = -EBADF; | |
1168 | _cleanup_free_ char *f = NULL; | |
a5925354 DDM |
1169 | int r; |
1170 | ||
b352e545 DDM |
1171 | assert(rfd >= 0); |
1172 | ||
1173 | pfd = chase_and_open_parent_at(rfd, "/etc/kernel/cmdline", | |
b39710cc | 1174 | CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW, |
b352e545 DDM |
1175 | &f); |
1176 | if (pfd < 0) | |
1177 | return log_error_errno(pfd, "Failed to chase /etc/kernel/cmdline: %m"); | |
1178 | ||
1179 | r = should_configure(pfd, f); | |
1180 | if (r == 0) | |
1181 | log_debug("Found /etc/kernel/cmdline, assuming kernel command line has been configured."); | |
1182 | if (r <= 0) | |
1183 | return r; | |
a5925354 | 1184 | |
eb650ffe ZJS |
1185 | if (!arg_kernel_cmdline) { |
1186 | log_debug("Creation of /etc/kernel/cmdline was not requested, skipping."); | |
a5925354 | 1187 | return 0; |
eb650ffe | 1188 | } |
a5925354 | 1189 | |
b352e545 DDM |
1190 | r = write_string_file_at(pfd, "cmdline", arg_kernel_cmdline, |
1191 | WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC); | |
a5925354 | 1192 | if (r < 0) |
b352e545 | 1193 | return log_error_errno(r, "Failed to write /etc/kernel/cmdline: %m"); |
a5925354 | 1194 | |
b352e545 | 1195 | log_info("/etc/kernel/cmdline written."); |
a5925354 DDM |
1196 | return 0; |
1197 | } | |
1198 | ||
05eb2c60 DDM |
1199 | static int reset_one(int rfd, const char *path) { |
1200 | _cleanup_close_ int pfd = -EBADF; | |
1201 | _cleanup_free_ char *f = NULL; | |
1202 | ||
1203 | assert(rfd >= 0); | |
1204 | assert(path); | |
1205 | ||
1206 | pfd = chase_and_open_parent_at(rfd, path, CHASE_AT_RESOLVE_IN_ROOT|CHASE_WARN|CHASE_NOFOLLOW, &f); | |
1207 | if (pfd == -ENOENT) | |
1208 | return 0; | |
1209 | if (pfd < 0) | |
1210 | return log_error_errno(pfd, "Failed to resolve %s: %m", path); | |
1211 | ||
1212 | if (unlinkat(pfd, f, 0) < 0) | |
1213 | return errno == ENOENT ? 0 : log_error_errno(errno, "Failed to remove %s: %m", path); | |
1214 | ||
1215 | log_info("Removed %s", path); | |
1216 | return 0; | |
1217 | } | |
1218 | ||
1219 | static int process_reset(int rfd) { | |
1220 | int r; | |
1221 | ||
1222 | assert(rfd >= 0); | |
1223 | ||
1224 | if (!arg_reset) | |
1225 | return 0; | |
1226 | ||
1227 | FOREACH_STRING(p, | |
1228 | "/etc/locale.conf", | |
1229 | "/etc/vconsole.conf", | |
1230 | "/etc/hostname", | |
1231 | "/etc/machine-id", | |
cd320749 NR |
1232 | "/etc/kernel/cmdline", |
1233 | "/etc/localtime") { | |
05eb2c60 DDM |
1234 | r = reset_one(rfd, p); |
1235 | if (r < 0) | |
1236 | return r; | |
1237 | } | |
1238 | ||
1239 | return 0; | |
1240 | } | |
1241 | ||
37ec0fdd LP |
1242 | static int help(void) { |
1243 | _cleanup_free_ char *link = NULL; | |
1244 | int r; | |
1245 | ||
1246 | r = terminal_urlify_man("systemd-firstboot", "1", &link); | |
1247 | if (r < 0) | |
1248 | return log_oom(); | |
1249 | ||
418b9be5 LP |
1250 | printf("%s [OPTIONS...]\n\n" |
1251 | "Configures basic settings of the system.\n\n" | |
e16793ee ZJS |
1252 | " -h --help Show this help\n" |
1253 | " --version Show package version\n" | |
1254 | " --root=PATH Operate on an alternate filesystem root\n" | |
84be0c71 LP |
1255 | " --image=PATH Operate on disk image as filesystem root\n" |
1256 | " --image-policy=POLICY Specify disk image dissection policy\n" | |
e16793ee ZJS |
1257 | " --locale=LOCALE Set primary locale (LANG=)\n" |
1258 | " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n" | |
1259 | " --keymap=KEYMAP Set keymap\n" | |
1260 | " --timezone=TIMEZONE Set timezone\n" | |
1261 | " --hostname=NAME Set hostname\n" | |
fd6ee7ed | 1262 | " --setup-machine-id Set a random machine ID\n" |
981644ed | 1263 | " --machine-id=ID Set specified machine ID\n" |
e16793ee ZJS |
1264 | " --root-password=PASSWORD Set root password from plaintext password\n" |
1265 | " --root-password-file=FILE Set root password from file\n" | |
1266 | " --root-password-hashed=HASH Set root password from hashed password\n" | |
1267 | " --root-shell=SHELL Set root shell\n" | |
981644ed AAF |
1268 | " --kernel-command-line=CMDLINE\n" |
1269 | " Set kernel command line\n" | |
e16793ee ZJS |
1270 | " --prompt-locale Prompt the user for locale settings\n" |
1271 | " --prompt-keymap Prompt the user for keymap settings\n" | |
1272 | " --prompt-timezone Prompt the user for timezone\n" | |
1273 | " --prompt-hostname Prompt the user for hostname\n" | |
1274 | " --prompt-root-password Prompt the user for root password\n" | |
1275 | " --prompt-root-shell Prompt the user for root shell\n" | |
1276 | " --prompt Prompt for all of the above\n" | |
1277 | " --copy-locale Copy locale from host\n" | |
1278 | " --copy-keymap Copy keymap from host\n" | |
1279 | " --copy-timezone Copy timezone from host\n" | |
1280 | " --copy-root-password Copy root password from host\n" | |
1281 | " --copy-root-shell Copy root shell from host\n" | |
1282 | " --copy Copy locale, keymap, timezone, root password\n" | |
e16793ee ZJS |
1283 | " --force Overwrite existing files\n" |
1284 | " --delete-root-password Delete root password\n" | |
1285 | " --welcome=no Disable the welcome text\n" | |
05eb2c60 | 1286 | " --reset Remove existing files\n" |
bc556335 DDM |
1287 | "\nSee the %s for details.\n", |
1288 | program_invocation_short_name, | |
1289 | link); | |
37ec0fdd LP |
1290 | |
1291 | return 0; | |
418b9be5 LP |
1292 | } |
1293 | ||
1294 | static int parse_argv(int argc, char *argv[]) { | |
1295 | ||
1296 | enum { | |
1297 | ARG_VERSION = 0x100, | |
1298 | ARG_ROOT, | |
3ff9fa59 | 1299 | ARG_IMAGE, |
06e78680 | 1300 | ARG_IMAGE_POLICY, |
418b9be5 LP |
1301 | ARG_LOCALE, |
1302 | ARG_LOCALE_MESSAGES, | |
ed457f13 | 1303 | ARG_KEYMAP, |
418b9be5 LP |
1304 | ARG_TIMEZONE, |
1305 | ARG_HOSTNAME, | |
fd6ee7ed | 1306 | ARG_SETUP_MACHINE_ID, |
418b9be5 LP |
1307 | ARG_MACHINE_ID, |
1308 | ARG_ROOT_PASSWORD, | |
1309 | ARG_ROOT_PASSWORD_FILE, | |
676339a1 | 1310 | ARG_ROOT_PASSWORD_HASHED, |
28900a1b | 1311 | ARG_ROOT_SHELL, |
a5925354 | 1312 | ARG_KERNEL_COMMAND_LINE, |
418b9be5 LP |
1313 | ARG_PROMPT, |
1314 | ARG_PROMPT_LOCALE, | |
ed457f13 | 1315 | ARG_PROMPT_KEYMAP, |
418b9be5 LP |
1316 | ARG_PROMPT_TIMEZONE, |
1317 | ARG_PROMPT_HOSTNAME, | |
1318 | ARG_PROMPT_ROOT_PASSWORD, | |
28900a1b | 1319 | ARG_PROMPT_ROOT_SHELL, |
418b9be5 LP |
1320 | ARG_COPY, |
1321 | ARG_COPY_LOCALE, | |
ed457f13 | 1322 | ARG_COPY_KEYMAP, |
418b9be5 LP |
1323 | ARG_COPY_TIMEZONE, |
1324 | ARG_COPY_ROOT_PASSWORD, | |
28900a1b | 1325 | ARG_COPY_ROOT_SHELL, |
b4909a3f | 1326 | ARG_FORCE, |
4926ceaf | 1327 | ARG_DELETE_ROOT_PASSWORD, |
a1225020 | 1328 | ARG_WELCOME, |
05eb2c60 | 1329 | ARG_RESET, |
418b9be5 LP |
1330 | }; |
1331 | ||
1332 | static const struct option options[] = { | |
676339a1 DDM |
1333 | { "help", no_argument, NULL, 'h' }, |
1334 | { "version", no_argument, NULL, ARG_VERSION }, | |
1335 | { "root", required_argument, NULL, ARG_ROOT }, | |
3ff9fa59 | 1336 | { "image", required_argument, NULL, ARG_IMAGE }, |
06e78680 | 1337 | { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY }, |
676339a1 DDM |
1338 | { "locale", required_argument, NULL, ARG_LOCALE }, |
1339 | { "locale-messages", required_argument, NULL, ARG_LOCALE_MESSAGES }, | |
1340 | { "keymap", required_argument, NULL, ARG_KEYMAP }, | |
1341 | { "timezone", required_argument, NULL, ARG_TIMEZONE }, | |
1342 | { "hostname", required_argument, NULL, ARG_HOSTNAME }, | |
fd6ee7ed | 1343 | { "setup-machine-id", no_argument, NULL, ARG_SETUP_MACHINE_ID }, |
676339a1 DDM |
1344 | { "machine-id", required_argument, NULL, ARG_MACHINE_ID }, |
1345 | { "root-password", required_argument, NULL, ARG_ROOT_PASSWORD }, | |
1346 | { "root-password-file", required_argument, NULL, ARG_ROOT_PASSWORD_FILE }, | |
1347 | { "root-password-hashed", required_argument, NULL, ARG_ROOT_PASSWORD_HASHED }, | |
28900a1b | 1348 | { "root-shell", required_argument, NULL, ARG_ROOT_SHELL }, |
a5925354 | 1349 | { "kernel-command-line", required_argument, NULL, ARG_KERNEL_COMMAND_LINE }, |
676339a1 DDM |
1350 | { "prompt", no_argument, NULL, ARG_PROMPT }, |
1351 | { "prompt-locale", no_argument, NULL, ARG_PROMPT_LOCALE }, | |
1352 | { "prompt-keymap", no_argument, NULL, ARG_PROMPT_KEYMAP }, | |
1353 | { "prompt-timezone", no_argument, NULL, ARG_PROMPT_TIMEZONE }, | |
1354 | { "prompt-hostname", no_argument, NULL, ARG_PROMPT_HOSTNAME }, | |
1355 | { "prompt-root-password", no_argument, NULL, ARG_PROMPT_ROOT_PASSWORD }, | |
28900a1b | 1356 | { "prompt-root-shell", no_argument, NULL, ARG_PROMPT_ROOT_SHELL }, |
676339a1 DDM |
1357 | { "copy", no_argument, NULL, ARG_COPY }, |
1358 | { "copy-locale", no_argument, NULL, ARG_COPY_LOCALE }, | |
1359 | { "copy-keymap", no_argument, NULL, ARG_COPY_KEYMAP }, | |
1360 | { "copy-timezone", no_argument, NULL, ARG_COPY_TIMEZONE }, | |
1361 | { "copy-root-password", no_argument, NULL, ARG_COPY_ROOT_PASSWORD }, | |
28900a1b | 1362 | { "copy-root-shell", no_argument, NULL, ARG_COPY_ROOT_SHELL }, |
676339a1 DDM |
1363 | { "force", no_argument, NULL, ARG_FORCE }, |
1364 | { "delete-root-password", no_argument, NULL, ARG_DELETE_ROOT_PASSWORD }, | |
a1225020 | 1365 | { "welcome", required_argument, NULL, ARG_WELCOME }, |
05eb2c60 | 1366 | { "reset", no_argument, NULL, ARG_RESET }, |
418b9be5 LP |
1367 | {} |
1368 | }; | |
1369 | ||
1370 | int r, c; | |
1371 | ||
1372 | assert(argc >= 0); | |
1373 | assert(argv); | |
1374 | ||
601185b4 | 1375 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
418b9be5 LP |
1376 | |
1377 | switch (c) { | |
1378 | ||
1379 | case 'h': | |
37ec0fdd | 1380 | return help(); |
418b9be5 LP |
1381 | |
1382 | case ARG_VERSION: | |
3f6fd1ba | 1383 | return version(); |
418b9be5 LP |
1384 | |
1385 | case ARG_ROOT: | |
614b022c | 1386 | r = parse_path_argument(optarg, true, &arg_root); |
0f474365 | 1387 | if (r < 0) |
0f03c2a4 | 1388 | return r; |
418b9be5 LP |
1389 | break; |
1390 | ||
3ff9fa59 | 1391 | case ARG_IMAGE: |
614b022c | 1392 | r = parse_path_argument(optarg, false, &arg_image); |
3ff9fa59 LP |
1393 | if (r < 0) |
1394 | return r; | |
1395 | break; | |
1396 | ||
06e78680 YW |
1397 | case ARG_IMAGE_POLICY: |
1398 | r = parse_image_policy_argument(optarg, &arg_image_policy); | |
1399 | if (r < 0) | |
1400 | return r; | |
1401 | break; | |
1402 | ||
418b9be5 | 1403 | case ARG_LOCALE: |
2fc09a9c DM |
1404 | r = free_and_strdup(&arg_locale, optarg); |
1405 | if (r < 0) | |
418b9be5 LP |
1406 | return log_oom(); |
1407 | ||
1408 | break; | |
1409 | ||
1410 | case ARG_LOCALE_MESSAGES: | |
2fc09a9c DM |
1411 | r = free_and_strdup(&arg_locale_messages, optarg); |
1412 | if (r < 0) | |
418b9be5 LP |
1413 | return log_oom(); |
1414 | ||
1415 | break; | |
1416 | ||
ed457f13 | 1417 | case ARG_KEYMAP: |
baaa35ad ZJS |
1418 | if (!keymap_is_valid(optarg)) |
1419 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1420 | "Keymap %s is not valid.", optarg); | |
ed457f13 TB |
1421 | |
1422 | r = free_and_strdup(&arg_keymap, optarg); | |
1423 | if (r < 0) | |
1424 | return log_oom(); | |
1425 | ||
1426 | break; | |
1427 | ||
418b9be5 | 1428 | case ARG_TIMEZONE: |
baaa35ad ZJS |
1429 | if (!timezone_is_valid(optarg, LOG_ERR)) |
1430 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1431 | "Timezone %s is not valid.", optarg); | |
418b9be5 | 1432 | |
2fc09a9c DM |
1433 | r = free_and_strdup(&arg_timezone, optarg); |
1434 | if (r < 0) | |
418b9be5 LP |
1435 | return log_oom(); |
1436 | ||
1437 | break; | |
1438 | ||
1439 | case ARG_ROOT_PASSWORD: | |
2fc09a9c DM |
1440 | r = free_and_strdup(&arg_root_password, optarg); |
1441 | if (r < 0) | |
418b9be5 | 1442 | return log_oom(); |
676339a1 DDM |
1443 | |
1444 | arg_root_password_is_hashed = false; | |
418b9be5 LP |
1445 | break; |
1446 | ||
1447 | case ARG_ROOT_PASSWORD_FILE: | |
0da16248 | 1448 | arg_root_password = mfree(arg_root_password); |
418b9be5 LP |
1449 | |
1450 | r = read_one_line_file(optarg, &arg_root_password); | |
23bbb0de MS |
1451 | if (r < 0) |
1452 | return log_error_errno(r, "Failed to read %s: %m", optarg); | |
418b9be5 | 1453 | |
676339a1 DDM |
1454 | arg_root_password_is_hashed = false; |
1455 | break; | |
1456 | ||
1457 | case ARG_ROOT_PASSWORD_HASHED: | |
1458 | r = free_and_strdup(&arg_root_password, optarg); | |
1459 | if (r < 0) | |
1460 | return log_oom(); | |
1461 | ||
1462 | arg_root_password_is_hashed = true; | |
418b9be5 LP |
1463 | break; |
1464 | ||
28900a1b | 1465 | case ARG_ROOT_SHELL: |
28900a1b DDM |
1466 | r = free_and_strdup(&arg_root_shell, optarg); |
1467 | if (r < 0) | |
1468 | return log_oom(); | |
1469 | ||
1470 | break; | |
1471 | ||
418b9be5 | 1472 | case ARG_HOSTNAME: |
52ef5dd7 | 1473 | if (!hostname_is_valid(optarg, VALID_HOSTNAME_TRAILING_DOT)) |
baaa35ad ZJS |
1474 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
1475 | "Host name %s is not valid.", optarg); | |
418b9be5 | 1476 | |
2fc09a9c DM |
1477 | r = free_and_strdup(&arg_hostname, optarg); |
1478 | if (r < 0) | |
418b9be5 LP |
1479 | return log_oom(); |
1480 | ||
79485fc2 | 1481 | hostname_cleanup(arg_hostname); |
418b9be5 LP |
1482 | break; |
1483 | ||
fd6ee7ed ZJS |
1484 | case ARG_SETUP_MACHINE_ID: |
1485 | r = sd_id128_randomize(&arg_machine_id); | |
1486 | if (r < 0) | |
1487 | return log_error_errno(r, "Failed to generate randomized machine ID: %m"); | |
1488 | ||
1489 | break; | |
1490 | ||
418b9be5 | 1491 | case ARG_MACHINE_ID: |
7fb1d980 YW |
1492 | r = sd_id128_from_string(optarg, &arg_machine_id); |
1493 | if (r < 0) | |
1494 | return log_error_errno(r, "Failed to parse machine id %s.", optarg); | |
418b9be5 LP |
1495 | |
1496 | break; | |
1497 | ||
a5925354 DDM |
1498 | case ARG_KERNEL_COMMAND_LINE: |
1499 | r = free_and_strdup(&arg_kernel_cmdline, optarg); | |
1500 | if (r < 0) | |
1501 | return log_oom(); | |
1502 | ||
1503 | break; | |
1504 | ||
418b9be5 | 1505 | case ARG_PROMPT: |
28900a1b DDM |
1506 | arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname = |
1507 | arg_prompt_root_password = arg_prompt_root_shell = true; | |
418b9be5 LP |
1508 | break; |
1509 | ||
1510 | case ARG_PROMPT_LOCALE: | |
1511 | arg_prompt_locale = true; | |
1512 | break; | |
1513 | ||
ed457f13 TB |
1514 | case ARG_PROMPT_KEYMAP: |
1515 | arg_prompt_keymap = true; | |
1516 | break; | |
1517 | ||
418b9be5 LP |
1518 | case ARG_PROMPT_TIMEZONE: |
1519 | arg_prompt_timezone = true; | |
1520 | break; | |
1521 | ||
1522 | case ARG_PROMPT_HOSTNAME: | |
1523 | arg_prompt_hostname = true; | |
1524 | break; | |
1525 | ||
1526 | case ARG_PROMPT_ROOT_PASSWORD: | |
1527 | arg_prompt_root_password = true; | |
1528 | break; | |
1529 | ||
28900a1b DDM |
1530 | case ARG_PROMPT_ROOT_SHELL: |
1531 | arg_prompt_root_shell = true; | |
1532 | break; | |
1533 | ||
418b9be5 | 1534 | case ARG_COPY: |
28900a1b DDM |
1535 | arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password = |
1536 | arg_copy_root_shell = true; | |
e926f647 | 1537 | break; |
418b9be5 LP |
1538 | |
1539 | case ARG_COPY_LOCALE: | |
1540 | arg_copy_locale = true; | |
1541 | break; | |
1542 | ||
ed457f13 TB |
1543 | case ARG_COPY_KEYMAP: |
1544 | arg_copy_keymap = true; | |
1545 | break; | |
1546 | ||
418b9be5 LP |
1547 | case ARG_COPY_TIMEZONE: |
1548 | arg_copy_timezone = true; | |
1549 | break; | |
1550 | ||
1551 | case ARG_COPY_ROOT_PASSWORD: | |
1552 | arg_copy_root_password = true; | |
1553 | break; | |
1554 | ||
28900a1b DDM |
1555 | case ARG_COPY_ROOT_SHELL: |
1556 | arg_copy_root_shell = true; | |
1557 | break; | |
1558 | ||
b4909a3f DDM |
1559 | case ARG_FORCE: |
1560 | arg_force = true; | |
1561 | break; | |
1562 | ||
4926ceaf DDM |
1563 | case ARG_DELETE_ROOT_PASSWORD: |
1564 | arg_delete_root_password = true; | |
1565 | break; | |
1566 | ||
a1225020 LP |
1567 | case ARG_WELCOME: |
1568 | r = parse_boolean(optarg); | |
1569 | if (r < 0) | |
1570 | return log_error_errno(r, "Failed to parse --welcome= argument: %s", optarg); | |
1571 | ||
1572 | arg_welcome = r; | |
1573 | break; | |
1574 | ||
05eb2c60 DDM |
1575 | case ARG_RESET: |
1576 | arg_reset = true; | |
1577 | break; | |
1578 | ||
418b9be5 LP |
1579 | case '?': |
1580 | return -EINVAL; | |
1581 | ||
1582 | default: | |
04499a70 | 1583 | assert_not_reached(); |
418b9be5 | 1584 | } |
418b9be5 | 1585 | |
4926ceaf DDM |
1586 | if (arg_delete_root_password && (arg_copy_root_password || arg_root_password || arg_prompt_root_password)) |
1587 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
fd6ee7ed | 1588 | "--delete-root-password cannot be combined with other root password options."); |
4926ceaf | 1589 | |
3ff9fa59 | 1590 | if (arg_image && arg_root) |
fd6ee7ed ZJS |
1591 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
1592 | "--root= and --image= cannot be used together."); | |
1593 | ||
1594 | if (!sd_id128_is_null(arg_machine_id) && !(arg_image || arg_root) && !arg_force) | |
1595 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1596 | "--machine-id=/--setup-machine-id only works with --root= or --image=."); | |
3ff9fa59 | 1597 | |
418b9be5 LP |
1598 | return 1; |
1599 | } | |
1600 | ||
d0c50d8d | 1601 | static int reload_system_manager(sd_bus **bus) { |
d0c50d8d ZJS |
1602 | int r; |
1603 | ||
1604 | assert(bus); | |
1605 | ||
1606 | if (!*bus) { | |
1607 | r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus); | |
1608 | if (r < 0) | |
1609 | return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL); | |
1610 | } | |
1611 | ||
a9399358 | 1612 | r = bus_service_manager_reload(*bus); |
d0c50d8d | 1613 | if (r < 0) |
a9399358 LP |
1614 | return r; |
1615 | ||
d0c50d8d ZJS |
1616 | log_info("Requested manager reload to apply locale configuration."); |
1617 | return 0; | |
1618 | } | |
1619 | ||
cea32691 ZJS |
1620 | static int reload_vconsole(sd_bus **bus) { |
1621 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
8eb668b9 ZJS |
1622 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
1623 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; | |
1624 | const char *object; | |
cea32691 ZJS |
1625 | int r; |
1626 | ||
1627 | assert(bus); | |
1628 | ||
1629 | if (!*bus) { | |
1630 | r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus); | |
1631 | if (r < 0) | |
1632 | return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL); | |
1633 | } | |
1634 | ||
8eb668b9 ZJS |
1635 | r = bus_wait_for_jobs_new(*bus, &w); |
1636 | if (r < 0) | |
1637 | return log_error_errno(r, "Could not watch jobs: %m"); | |
1638 | ||
1639 | r = bus_call_method(*bus, bus_systemd_mgr, "RestartUnit", &error, &reply, | |
1640 | "ss", "systemd-vconsole-setup.service", "replace"); | |
cea32691 ZJS |
1641 | if (r < 0) |
1642 | return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r)); | |
8eb668b9 ZJS |
1643 | |
1644 | r = sd_bus_message_read(reply, "o", &object); | |
1645 | if (r < 0) | |
1646 | return bus_log_parse_error(r); | |
1647 | ||
e22ad53d | 1648 | r = bus_wait_for_jobs_one(w, object, BUS_WAIT_JOBS_LOG_ERROR, NULL); |
8eb668b9 ZJS |
1649 | if (r < 0) |
1650 | return log_error_errno(r, "Failed to wait for systemd-vconsole-setup.service/restart: %m"); | |
cea32691 ZJS |
1651 | return 0; |
1652 | } | |
1653 | ||
45f39418 | 1654 | static int run(int argc, char *argv[]) { |
cea32691 | 1655 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
3ff9fa59 | 1656 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
a4b3e942 | 1657 | _cleanup_(umount_and_freep) char *mounted_dir = NULL; |
b352e545 | 1658 | _cleanup_close_ int rfd = -EBADF; |
418b9be5 LP |
1659 | int r; |
1660 | ||
1661 | r = parse_argv(argc, argv); | |
1662 | if (r <= 0) | |
45f39418 | 1663 | return r; |
418b9be5 | 1664 | |
d2acb93d | 1665 | log_setup(); |
418b9be5 LP |
1666 | |
1667 | umask(0022); | |
1668 | ||
cea32691 | 1669 | bool offline = arg_root || arg_image; |
3ff9fa59 | 1670 | |
cea32691 | 1671 | if (!offline) { |
3ff9fa59 LP |
1672 | /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel |
1673 | * command line option, because we are called to provision the host with basic settings (as | |
1674 | * opposed to some other file system tree/image) */ | |
1675 | ||
cea32691 | 1676 | bool enabled; |
3787934b | 1677 | r = proc_cmdline_get_bool("systemd.firstboot", /* flags = */ 0, &enabled); |
3ff9fa59 LP |
1678 | if (r < 0) |
1679 | return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m"); | |
eb650ffe | 1680 | if (r > 0 && !enabled) { |
0a9c4a10 LP |
1681 | log_debug("Found systemd.firstboot=no kernel command line argument, turning off all prompts."); |
1682 | arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname = arg_prompt_root_password = arg_prompt_root_shell = false; | |
eb650ffe | 1683 | } |
3ff9fa59 LP |
1684 | } |
1685 | ||
6aa05ebd LP |
1686 | if (arg_image) { |
1687 | assert(!arg_root); | |
1688 | ||
1689 | r = mount_image_privately_interactively( | |
1690 | arg_image, | |
84be0c71 | 1691 | arg_image_policy, |
4b5de5dd LP |
1692 | DISSECT_IMAGE_GENERIC_ROOT | |
1693 | DISSECT_IMAGE_REQUIRE_ROOT | | |
1694 | DISSECT_IMAGE_VALIDATE_OS | | |
1695 | DISSECT_IMAGE_RELAX_VAR_CHECK | | |
c65f854a | 1696 | DISSECT_IMAGE_FSCK | |
f4a63ce2 LP |
1697 | DISSECT_IMAGE_GROWFS | |
1698 | DISSECT_IMAGE_ALLOW_USERSPACE_VERITY, | |
a4b3e942 | 1699 | &mounted_dir, |
b352e545 | 1700 | &rfd, |
e330f97a | 1701 | &loop_device); |
6aa05ebd LP |
1702 | if (r < 0) |
1703 | return r; | |
1704 | ||
a4b3e942 | 1705 | arg_root = strdup(mounted_dir); |
6aa05ebd LP |
1706 | if (!arg_root) |
1707 | return log_oom(); | |
b352e545 DDM |
1708 | } else { |
1709 | rfd = open(empty_to_root(arg_root), O_DIRECTORY|O_CLOEXEC); | |
1710 | if (rfd < 0) | |
1711 | return log_error_errno(errno, "Failed to open %s: %m", empty_to_root(arg_root)); | |
1712 | } | |
1713 | ||
1714 | LOG_SET_PREFIX(arg_image ?: arg_root); | |
1715 | ||
a0657479 DDM |
1716 | /* We check these conditions here instead of in parse_argv() so that we can take the root directory |
1717 | * into account. */ | |
1718 | ||
321a8c59 ED |
1719 | if (arg_keymap && !determine_keymap_validity_func(rfd)(arg_keymap)) |
1720 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Keymap %s is not installed.", arg_keymap); | |
a0657479 DDM |
1721 | if (arg_locale && !locale_is_ok(rfd, arg_locale)) |
1722 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale); | |
1723 | if (arg_locale_messages && !locale_is_ok(rfd, arg_locale_messages)) | |
1724 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale_messages); | |
1725 | ||
b352e545 DDM |
1726 | if (arg_root_shell) { |
1727 | r = find_shell(rfd, arg_root_shell); | |
1728 | if (r < 0) | |
1729 | return r; | |
6aa05ebd | 1730 | } |
f582cbca | 1731 | |
05eb2c60 DDM |
1732 | r = process_reset(rfd); |
1733 | if (r < 0) | |
1734 | return r; | |
1735 | ||
b352e545 | 1736 | r = process_locale(rfd); |
418b9be5 | 1737 | if (r < 0) |
45f39418 | 1738 | return r; |
d0c50d8d ZJS |
1739 | if (r > 0 && !offline) |
1740 | (void) reload_system_manager(&bus); | |
418b9be5 | 1741 | |
b352e545 | 1742 | r = process_keymap(rfd); |
ed457f13 | 1743 | if (r < 0) |
45f39418 | 1744 | return r; |
cea32691 ZJS |
1745 | if (r > 0 && !offline) |
1746 | (void) reload_vconsole(&bus); | |
ed457f13 | 1747 | |
b352e545 | 1748 | r = process_timezone(rfd); |
418b9be5 | 1749 | if (r < 0) |
45f39418 | 1750 | return r; |
418b9be5 | 1751 | |
b352e545 | 1752 | r = process_hostname(rfd); |
418b9be5 | 1753 | if (r < 0) |
45f39418 | 1754 | return r; |
418b9be5 | 1755 | |
b352e545 | 1756 | r = process_machine_id(rfd); |
418b9be5 | 1757 | if (r < 0) |
45f39418 | 1758 | return r; |
418b9be5 | 1759 | |
b352e545 | 1760 | r = process_root_account(rfd); |
418b9be5 | 1761 | if (r < 0) |
45f39418 YW |
1762 | return r; |
1763 | ||
b352e545 | 1764 | r = process_kernel_cmdline(rfd); |
a5925354 DDM |
1765 | if (r < 0) |
1766 | return r; | |
1767 | ||
45f39418 | 1768 | return 0; |
418b9be5 | 1769 | } |
45f39418 YW |
1770 | |
1771 | DEFINE_MAIN_FUNCTION(run); |