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