]>
Commit | Line | Data |
---|---|---|
418b9be5 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2014 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
418b9be5 | 20 | #include <fcntl.h> |
418b9be5 LP |
21 | #include <getopt.h> |
22 | #include <shadow.h> | |
3f6fd1ba | 23 | #include <unistd.h> |
418b9be5 | 24 | |
b5efdb8a | 25 | #include "alloc-util.h" |
3f6fd1ba | 26 | #include "ask-password-api.h" |
418b9be5 | 27 | #include "copy.h" |
6bedfcbb | 28 | #include "fd-util.h" |
3f6fd1ba | 29 | #include "fileio.h" |
f4f15635 | 30 | #include "fs-util.h" |
3f6fd1ba LP |
31 | #include "hostname-util.h" |
32 | #include "locale-util.h" | |
418b9be5 | 33 | #include "mkdir.h" |
6bedfcbb | 34 | #include "parse-util.h" |
418b9be5 | 35 | #include "path-util.h" |
f582cbca | 36 | #include "proc-cmdline.h" |
3df3e884 | 37 | #include "random-util.h" |
6bedfcbb | 38 | #include "string-util.h" |
3f6fd1ba | 39 | #include "strv.h" |
288a74cc | 40 | #include "terminal-util.h" |
3f6fd1ba | 41 | #include "time-util.h" |
affb60b1 | 42 | #include "umask-util.h" |
e929bee0 | 43 | #include "user-util.h" |
418b9be5 LP |
44 | |
45 | static char *arg_root = NULL; | |
46 | static char *arg_locale = NULL; /* $LANG */ | |
47 | static char *arg_locale_messages = NULL; /* $LC_MESSAGES */ | |
48 | static char *arg_timezone = NULL; | |
49 | static char *arg_hostname = NULL; | |
50 | static sd_id128_t arg_machine_id = {}; | |
51 | static char *arg_root_password = NULL; | |
52 | static bool arg_prompt_locale = false; | |
53 | static bool arg_prompt_timezone = false; | |
54 | static bool arg_prompt_hostname = false; | |
55 | static bool arg_prompt_root_password = false; | |
56 | static bool arg_copy_locale = false; | |
57 | static bool arg_copy_timezone = false; | |
58 | static bool arg_copy_root_password = false; | |
59 | ||
418b9be5 LP |
60 | static bool press_any_key(void) { |
61 | char k = 0; | |
62 | bool need_nl = true; | |
63 | ||
64 | printf("-- Press any key to proceed --"); | |
65 | fflush(stdout); | |
66 | ||
94956f8f | 67 | (void) read_one_char(stdin, &k, USEC_INFINITY, &need_nl); |
418b9be5 LP |
68 | |
69 | if (need_nl) | |
70 | putchar('\n'); | |
71 | ||
72 | return k != 'q'; | |
73 | } | |
74 | ||
75 | static void print_welcome(void) { | |
76 | _cleanup_free_ char *pretty_name = NULL; | |
77 | const char *os_release = NULL; | |
78 | static bool done = false; | |
79 | int r; | |
80 | ||
81 | if (done) | |
82 | return; | |
83 | ||
1d13f648 | 84 | os_release = prefix_roota(arg_root, "/etc/os-release"); |
418b9be5 LP |
85 | r = parse_env_file(os_release, NEWLINE, |
86 | "PRETTY_NAME", &pretty_name, | |
87 | NULL); | |
88 | if (r == -ENOENT) { | |
89 | ||
1d13f648 | 90 | os_release = prefix_roota(arg_root, "/usr/lib/os-release"); |
418b9be5 LP |
91 | r = parse_env_file(os_release, NEWLINE, |
92 | "PRETTY_NAME", &pretty_name, | |
93 | NULL); | |
94 | } | |
95 | ||
96 | if (r < 0 && r != -ENOENT) | |
da927ba9 | 97 | log_warning_errno(r, "Failed to read os-release file: %m"); |
418b9be5 LP |
98 | |
99 | printf("\nWelcome to your new installation of %s!\nPlease configure a few basic system settings:\n\n", | |
100 | isempty(pretty_name) ? "Linux" : pretty_name); | |
101 | ||
102 | press_any_key(); | |
103 | ||
104 | done = true; | |
105 | } | |
106 | ||
107 | static int show_menu(char **x, unsigned n_columns, unsigned width, unsigned percentage) { | |
108 | unsigned n, per_column, i, j; | |
109 | unsigned break_lines, break_modulo; | |
110 | ||
111 | assert(n_columns > 0); | |
112 | ||
113 | n = strv_length(x); | |
114 | per_column = (n + n_columns - 1) / n_columns; | |
115 | ||
116 | break_lines = lines(); | |
117 | if (break_lines > 2) | |
118 | break_lines--; | |
119 | ||
120 | /* The first page gets two extra lines, since we want to show | |
121 | * a title */ | |
122 | break_modulo = break_lines; | |
123 | if (break_modulo > 3) | |
124 | break_modulo -= 3; | |
125 | ||
126 | for (i = 0; i < per_column; i++) { | |
127 | ||
128 | for (j = 0; j < n_columns; j ++) { | |
129 | _cleanup_free_ char *e = NULL; | |
130 | ||
131 | if (j * per_column + i >= n) | |
132 | break; | |
133 | ||
134 | e = ellipsize(x[j * per_column + i], width, percentage); | |
135 | if (!e) | |
136 | return log_oom(); | |
137 | ||
138 | printf("%4u) %-*s", j * per_column + i + 1, width, e); | |
139 | } | |
140 | ||
141 | putchar('\n'); | |
142 | ||
143 | /* on the first screen we reserve 2 extra lines for the title */ | |
144 | if (i % break_lines == break_modulo) { | |
145 | if (!press_any_key()) | |
146 | return 0; | |
147 | } | |
148 | } | |
149 | ||
150 | return 0; | |
151 | } | |
152 | ||
153 | static int prompt_loop(const char *text, char **l, bool (*is_valid)(const char *name), char **ret) { | |
154 | int r; | |
155 | ||
156 | assert(text); | |
157 | assert(is_valid); | |
158 | assert(ret); | |
159 | ||
160 | for (;;) { | |
161 | _cleanup_free_ char *p = NULL; | |
162 | unsigned u; | |
163 | ||
323b7dc9 | 164 | r = ask_string(&p, "%s %s (empty to skip): ", special_glyph(TRIANGULAR_BULLET), text); |
23bbb0de MS |
165 | if (r < 0) |
166 | return log_error_errno(r, "Failed to query user: %m"); | |
418b9be5 LP |
167 | |
168 | if (isempty(p)) { | |
169 | log_warning("No data entered, skipping."); | |
170 | return 0; | |
171 | } | |
172 | ||
173 | r = safe_atou(p, &u); | |
174 | if (r >= 0) { | |
175 | char *c; | |
176 | ||
177 | if (u <= 0 || u > strv_length(l)) { | |
178 | log_error("Specified entry number out of range."); | |
179 | continue; | |
180 | } | |
181 | ||
182 | log_info("Selected '%s'.", l[u-1]); | |
183 | ||
184 | c = strdup(l[u-1]); | |
185 | if (!c) | |
186 | return log_oom(); | |
187 | ||
188 | free(*ret); | |
189 | *ret = c; | |
190 | return 0; | |
191 | } | |
192 | ||
193 | if (!is_valid(p)) { | |
194 | log_error("Entered data invalid."); | |
195 | continue; | |
196 | } | |
197 | ||
198 | free(*ret); | |
199 | *ret = p; | |
200 | p = 0; | |
201 | return 0; | |
202 | } | |
203 | } | |
204 | ||
205 | static int prompt_locale(void) { | |
206 | _cleanup_strv_free_ char **locales = NULL; | |
207 | int r; | |
208 | ||
209 | if (arg_locale || arg_locale_messages) | |
210 | return 0; | |
211 | ||
212 | if (!arg_prompt_locale) | |
213 | return 0; | |
214 | ||
215 | r = get_locales(&locales); | |
23bbb0de MS |
216 | if (r < 0) |
217 | return log_error_errno(r, "Cannot query locales list: %m"); | |
418b9be5 LP |
218 | |
219 | print_welcome(); | |
220 | ||
221 | printf("\nAvailable Locales:\n\n"); | |
222 | r = show_menu(locales, 3, 22, 60); | |
223 | if (r < 0) | |
224 | return r; | |
225 | ||
226 | putchar('\n'); | |
227 | ||
228 | r = prompt_loop("Please enter system locale name or number", locales, locale_is_valid, &arg_locale); | |
229 | if (r < 0) | |
230 | return r; | |
231 | ||
232 | if (isempty(arg_locale)) | |
233 | return 0; | |
234 | ||
235 | r = prompt_loop("Please enter system message locale name or number", locales, locale_is_valid, &arg_locale_messages); | |
236 | if (r < 0) | |
237 | return r; | |
238 | ||
239 | return 0; | |
240 | } | |
241 | ||
242 | static int process_locale(void) { | |
243 | const char *etc_localeconf; | |
244 | char* locales[3]; | |
245 | unsigned i = 0; | |
246 | int r; | |
247 | ||
1d13f648 | 248 | etc_localeconf = prefix_roota(arg_root, "/etc/locale.conf"); |
b3cd687d | 249 | if (laccess(etc_localeconf, F_OK) >= 0) |
418b9be5 LP |
250 | return 0; |
251 | ||
252 | if (arg_copy_locale && arg_root) { | |
253 | ||
254 | mkdir_parents(etc_localeconf, 0755); | |
1c876927 | 255 | r = copy_file("/etc/locale.conf", etc_localeconf, 0, 0644, 0, COPY_REFLINK); |
418b9be5 | 256 | if (r != -ENOENT) { |
23bbb0de MS |
257 | if (r < 0) |
258 | return log_error_errno(r, "Failed to copy %s: %m", etc_localeconf); | |
418b9be5 LP |
259 | |
260 | log_info("%s copied.", etc_localeconf); | |
261 | return 0; | |
262 | } | |
263 | } | |
264 | ||
265 | r = prompt_locale(); | |
266 | if (r < 0) | |
267 | return r; | |
268 | ||
269 | if (!isempty(arg_locale)) | |
63c372cb | 270 | locales[i++] = strjoina("LANG=", arg_locale); |
418b9be5 | 271 | if (!isempty(arg_locale_messages) && !streq(arg_locale_messages, arg_locale)) |
63c372cb | 272 | locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages); |
418b9be5 LP |
273 | |
274 | if (i == 0) | |
275 | return 0; | |
276 | ||
277 | locales[i] = NULL; | |
278 | ||
279 | mkdir_parents(etc_localeconf, 0755); | |
280 | r = write_env_file(etc_localeconf, locales); | |
23bbb0de MS |
281 | if (r < 0) |
282 | return log_error_errno(r, "Failed to write %s: %m", etc_localeconf); | |
418b9be5 LP |
283 | |
284 | log_info("%s written.", etc_localeconf); | |
285 | return 0; | |
286 | } | |
287 | ||
288 | static int prompt_timezone(void) { | |
289 | _cleanup_strv_free_ char **zones = NULL; | |
290 | int r; | |
291 | ||
292 | if (arg_timezone) | |
293 | return 0; | |
294 | ||
295 | if (!arg_prompt_timezone) | |
296 | return 0; | |
297 | ||
298 | r = get_timezones(&zones); | |
23bbb0de MS |
299 | if (r < 0) |
300 | return log_error_errno(r, "Cannot query timezone list: %m"); | |
418b9be5 LP |
301 | |
302 | print_welcome(); | |
303 | ||
304 | printf("\nAvailable Time Zones:\n\n"); | |
305 | r = show_menu(zones, 3, 22, 30); | |
306 | if (r < 0) | |
307 | return r; | |
308 | ||
309 | putchar('\n'); | |
310 | ||
311 | r = prompt_loop("Please enter timezone name or number", zones, timezone_is_valid, &arg_timezone); | |
312 | if (r < 0) | |
313 | return r; | |
314 | ||
315 | return 0; | |
316 | } | |
317 | ||
318 | static int process_timezone(void) { | |
319 | const char *etc_localtime, *e; | |
320 | int r; | |
321 | ||
1d13f648 | 322 | etc_localtime = prefix_roota(arg_root, "/etc/localtime"); |
b3cd687d | 323 | if (laccess(etc_localtime, F_OK) >= 0) |
418b9be5 LP |
324 | return 0; |
325 | ||
326 | if (arg_copy_timezone && arg_root) { | |
327 | _cleanup_free_ char *p = NULL; | |
328 | ||
329 | r = readlink_malloc("/etc/localtime", &p); | |
330 | if (r != -ENOENT) { | |
23bbb0de MS |
331 | if (r < 0) |
332 | return log_error_errno(r, "Failed to read host timezone: %m"); | |
418b9be5 LP |
333 | |
334 | mkdir_parents(etc_localtime, 0755); | |
4a62c710 MS |
335 | if (symlink(p, etc_localtime) < 0) |
336 | return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime); | |
418b9be5 LP |
337 | |
338 | log_info("%s copied.", etc_localtime); | |
339 | return 0; | |
340 | } | |
341 | } | |
342 | ||
343 | r = prompt_timezone(); | |
344 | if (r < 0) | |
345 | return r; | |
346 | ||
347 | if (isempty(arg_timezone)) | |
348 | return 0; | |
349 | ||
63c372cb | 350 | e = strjoina("../usr/share/zoneinfo/", arg_timezone); |
418b9be5 LP |
351 | |
352 | mkdir_parents(etc_localtime, 0755); | |
4a62c710 MS |
353 | if (symlink(e, etc_localtime) < 0) |
354 | return log_error_errno(errno, "Failed to create %s symlink: %m", etc_localtime); | |
418b9be5 LP |
355 | |
356 | log_info("%s written", etc_localtime); | |
357 | return 0; | |
358 | } | |
359 | ||
360 | static int prompt_hostname(void) { | |
361 | int r; | |
362 | ||
363 | if (arg_hostname) | |
364 | return 0; | |
365 | ||
366 | if (!arg_prompt_hostname) | |
367 | return 0; | |
368 | ||
369 | print_welcome(); | |
370 | putchar('\n'); | |
371 | ||
372 | for (;;) { | |
373 | _cleanup_free_ char *h = NULL; | |
374 | ||
323b7dc9 | 375 | r = ask_string(&h, "%s Please enter hostname for new system (empty to skip): ", special_glyph(TRIANGULAR_BULLET)); |
23bbb0de MS |
376 | if (r < 0) |
377 | return log_error_errno(r, "Failed to query hostname: %m"); | |
418b9be5 LP |
378 | |
379 | if (isempty(h)) { | |
380 | log_warning("No hostname entered, skipping."); | |
381 | break; | |
382 | } | |
383 | ||
34ad6090 | 384 | if (!hostname_is_valid(h, true)) { |
418b9be5 LP |
385 | log_error("Specified hostname invalid."); |
386 | continue; | |
387 | } | |
388 | ||
34ad6090 | 389 | /* Get rid of the trailing dot that we allow, but don't want to see */ |
ae691c1d | 390 | arg_hostname = hostname_cleanup(h); |
418b9be5 LP |
391 | h = NULL; |
392 | break; | |
393 | } | |
394 | ||
395 | return 0; | |
396 | } | |
397 | ||
398 | static int process_hostname(void) { | |
399 | const char *etc_hostname; | |
400 | int r; | |
401 | ||
1d13f648 | 402 | etc_hostname = prefix_roota(arg_root, "/etc/hostname"); |
b3cd687d | 403 | if (laccess(etc_hostname, F_OK) >= 0) |
418b9be5 LP |
404 | return 0; |
405 | ||
406 | r = prompt_hostname(); | |
407 | if (r < 0) | |
408 | return r; | |
409 | ||
410 | if (isempty(arg_hostname)) | |
411 | return 0; | |
412 | ||
413 | mkdir_parents(etc_hostname, 0755); | |
4c1fc3e4 | 414 | r = write_string_file(etc_hostname, arg_hostname, WRITE_STRING_FILE_CREATE); |
23bbb0de MS |
415 | if (r < 0) |
416 | return log_error_errno(r, "Failed to write %s: %m", etc_hostname); | |
418b9be5 LP |
417 | |
418 | log_info("%s written.", etc_hostname); | |
419 | return 0; | |
420 | } | |
421 | ||
422 | static int process_machine_id(void) { | |
423 | const char *etc_machine_id; | |
424 | char id[SD_ID128_STRING_MAX]; | |
425 | int r; | |
426 | ||
1d13f648 | 427 | etc_machine_id = prefix_roota(arg_root, "/etc/machine-id"); |
b3cd687d | 428 | if (laccess(etc_machine_id, F_OK) >= 0) |
418b9be5 LP |
429 | return 0; |
430 | ||
3bbaff3e | 431 | if (sd_id128_is_null(arg_machine_id)) |
418b9be5 LP |
432 | return 0; |
433 | ||
434 | mkdir_parents(etc_machine_id, 0755); | |
4c1fc3e4 | 435 | r = write_string_file(etc_machine_id, sd_id128_to_string(arg_machine_id, id), WRITE_STRING_FILE_CREATE); |
23bbb0de MS |
436 | if (r < 0) |
437 | return log_error_errno(r, "Failed to write machine id: %m"); | |
418b9be5 LP |
438 | |
439 | log_info("%s written.", etc_machine_id); | |
440 | return 0; | |
441 | } | |
442 | ||
443 | static int prompt_root_password(void) { | |
444 | const char *msg1, *msg2, *etc_shadow; | |
445 | int r; | |
446 | ||
447 | if (arg_root_password) | |
448 | return 0; | |
449 | ||
450 | if (!arg_prompt_root_password) | |
451 | return 0; | |
452 | ||
1d13f648 | 453 | etc_shadow = prefix_roota(arg_root, "/etc/shadow"); |
b3cd687d | 454 | if (laccess(etc_shadow, F_OK) >= 0) |
418b9be5 LP |
455 | return 0; |
456 | ||
457 | print_welcome(); | |
458 | putchar('\n'); | |
459 | ||
323b7dc9 ZJS |
460 | msg1 = strjoina(special_glyph(TRIANGULAR_BULLET), " Please enter a new root password (empty to skip): "); |
461 | msg2 = strjoina(special_glyph(TRIANGULAR_BULLET), " Please enter new root password again: "); | |
418b9be5 LP |
462 | |
463 | for (;;) { | |
ab84f5b9 | 464 | _cleanup_string_free_erase_ char *a = NULL, *b = NULL; |
418b9be5 | 465 | |
e287086b | 466 | r = ask_password_tty(msg1, NULL, 0, 0, NULL, &a); |
23bbb0de MS |
467 | if (r < 0) |
468 | return log_error_errno(r, "Failed to query root password: %m"); | |
418b9be5 LP |
469 | |
470 | if (isempty(a)) { | |
471 | log_warning("No password entered, skipping."); | |
472 | break; | |
473 | } | |
474 | ||
e287086b | 475 | r = ask_password_tty(msg2, NULL, 0, 0, NULL, &b); |
ab84f5b9 | 476 | if (r < 0) |
00843602 | 477 | return log_error_errno(r, "Failed to query root password: %m"); |
418b9be5 LP |
478 | |
479 | if (!streq(a, b)) { | |
480 | log_error("Entered passwords did not match, please try again."); | |
418b9be5 LP |
481 | continue; |
482 | } | |
483 | ||
418b9be5 LP |
484 | arg_root_password = a; |
485 | a = NULL; | |
486 | break; | |
487 | } | |
488 | ||
489 | return 0; | |
490 | } | |
491 | ||
492 | static int write_root_shadow(const char *path, const struct spwd *p) { | |
493 | _cleanup_fclose_ FILE *f = NULL; | |
494 | assert(path); | |
495 | assert(p); | |
496 | ||
3250929b LP |
497 | RUN_WITH_UMASK(0777) |
498 | f = fopen(path, "wex"); | |
418b9be5 LP |
499 | if (!f) |
500 | return -errno; | |
501 | ||
502 | errno = 0; | |
503 | if (putspent(p, f) != 0) | |
f5e5c28f | 504 | return errno > 0 ? -errno : -EIO; |
418b9be5 LP |
505 | |
506 | return fflush_and_check(f); | |
507 | } | |
508 | ||
509 | static int process_root_password(void) { | |
510 | ||
511 | static const char table[] = | |
512 | "abcdefghijklmnopqrstuvwxyz" | |
513 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
514 | "0123456789" | |
515 | "./"; | |
516 | ||
517 | struct spwd item = { | |
518 | .sp_namp = (char*) "root", | |
ad525df8 IS |
519 | .sp_min = -1, |
520 | .sp_max = -1, | |
521 | .sp_warn = -1, | |
418b9be5 LP |
522 | .sp_inact = -1, |
523 | .sp_expire = -1, | |
524 | .sp_flag = (unsigned long) -1, /* this appears to be what everybody does ... */ | |
525 | }; | |
45035609 LP |
526 | |
527 | _cleanup_close_ int lock = -1; | |
418b9be5 LP |
528 | char salt[3+16+1+1]; |
529 | uint8_t raw[16]; | |
530 | unsigned i; | |
531 | char *j; | |
532 | ||
533 | const char *etc_shadow; | |
534 | int r; | |
535 | ||
1d13f648 | 536 | etc_shadow = prefix_roota(arg_root, "/etc/shadow"); |
b3cd687d | 537 | if (laccess(etc_shadow, F_OK) >= 0) |
418b9be5 LP |
538 | return 0; |
539 | ||
45035609 LP |
540 | mkdir_parents(etc_shadow, 0755); |
541 | ||
e929bee0 | 542 | lock = take_etc_passwd_lock(arg_root); |
45035609 | 543 | if (lock < 0) |
db260eed | 544 | return log_error_errno(lock, "Failed to take a lock: %m"); |
45035609 | 545 | |
418b9be5 LP |
546 | if (arg_copy_root_password && arg_root) { |
547 | struct spwd *p; | |
548 | ||
549 | errno = 0; | |
550 | p = getspnam("root"); | |
551 | if (p || errno != ENOENT) { | |
552 | if (!p) { | |
553 | if (!errno) | |
554 | errno = EIO; | |
555 | ||
e1427b13 | 556 | return log_error_errno(errno, "Failed to find shadow entry for root: %m"); |
418b9be5 LP |
557 | } |
558 | ||
559 | r = write_root_shadow(etc_shadow, p); | |
23bbb0de MS |
560 | if (r < 0) |
561 | return log_error_errno(r, "Failed to write %s: %m", etc_shadow); | |
418b9be5 LP |
562 | |
563 | log_info("%s copied.", etc_shadow); | |
564 | return 0; | |
565 | } | |
566 | } | |
567 | ||
568 | r = prompt_root_password(); | |
569 | if (r < 0) | |
570 | return r; | |
571 | ||
572 | if (!arg_root_password) | |
573 | return 0; | |
574 | ||
f0d09059 | 575 | r = acquire_random_bytes(raw, 16, true); |
23bbb0de MS |
576 | if (r < 0) |
577 | return log_error_errno(r, "Failed to get salt: %m"); | |
418b9be5 LP |
578 | |
579 | /* We only bother with SHA512 hashed passwords, the rest is legacy, and we don't do legacy. */ | |
580 | assert_cc(sizeof(table) == 64 + 1); | |
581 | j = stpcpy(salt, "$6$"); | |
582 | for (i = 0; i < 16; i++) | |
583 | j[i] = table[raw[i] & 63]; | |
584 | j[i++] = '$'; | |
585 | j[i] = 0; | |
586 | ||
587 | errno = 0; | |
588 | item.sp_pwdp = crypt(arg_root_password, salt); | |
589 | if (!item.sp_pwdp) { | |
590 | if (!errno) | |
4546c341 | 591 | errno = EINVAL; |
418b9be5 | 592 | |
e1427b13 | 593 | return log_error_errno(errno, "Failed to encrypt password: %m"); |
418b9be5 LP |
594 | } |
595 | ||
596 | item.sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY); | |
597 | ||
598 | r = write_root_shadow(etc_shadow, &item); | |
23bbb0de MS |
599 | if (r < 0) |
600 | return log_error_errno(r, "Failed to write %s: %m", etc_shadow); | |
418b9be5 LP |
601 | |
602 | log_info("%s written.", etc_shadow); | |
603 | return 0; | |
604 | } | |
605 | ||
601185b4 | 606 | static void help(void) { |
418b9be5 LP |
607 | printf("%s [OPTIONS...]\n\n" |
608 | "Configures basic settings of the system.\n\n" | |
609 | " -h --help Show this help\n" | |
610 | " --version Show package version\n" | |
611 | " --root=PATH Operate on an alternate filesystem root\n" | |
612 | " --locale=LOCALE Set primary locale (LANG=)\n" | |
613 | " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n" | |
614 | " --timezone=TIMEZONE Set timezone\n" | |
615 | " --hostname=NAME Set host name\n" | |
616 | " --machine-ID=ID Set machine ID\n" | |
617 | " --root-password=PASSWORD Set root password\n" | |
618 | " --root-password-file=FILE Set root password from file\n" | |
619 | " --prompt-locale Prompt the user for locale settings\n" | |
620 | " --prompt-timezone Prompt the user for timezone\n" | |
621 | " --prompt-hostname Prompt the user for hostname\n" | |
622 | " --prompt-root-password Prompt the user for root password\n" | |
b57b0625 | 623 | " --prompt Prompt for all of the above\n" |
418b9be5 LP |
624 | " --copy-locale Copy locale from host\n" |
625 | " --copy-timezone Copy timezone from host\n" | |
626 | " --copy-root-password Copy root password from host\n" | |
627 | " --copy Copy locale, timezone, root password\n" | |
601185b4 ZJS |
628 | " --setup-machine-id Generate a new random machine ID\n" |
629 | , program_invocation_short_name); | |
418b9be5 LP |
630 | } |
631 | ||
632 | static int parse_argv(int argc, char *argv[]) { | |
633 | ||
634 | enum { | |
635 | ARG_VERSION = 0x100, | |
636 | ARG_ROOT, | |
637 | ARG_LOCALE, | |
638 | ARG_LOCALE_MESSAGES, | |
639 | ARG_TIMEZONE, | |
640 | ARG_HOSTNAME, | |
641 | ARG_MACHINE_ID, | |
642 | ARG_ROOT_PASSWORD, | |
643 | ARG_ROOT_PASSWORD_FILE, | |
644 | ARG_PROMPT, | |
645 | ARG_PROMPT_LOCALE, | |
646 | ARG_PROMPT_TIMEZONE, | |
647 | ARG_PROMPT_HOSTNAME, | |
648 | ARG_PROMPT_ROOT_PASSWORD, | |
649 | ARG_COPY, | |
650 | ARG_COPY_LOCALE, | |
651 | ARG_COPY_TIMEZONE, | |
652 | ARG_COPY_ROOT_PASSWORD, | |
653 | ARG_SETUP_MACHINE_ID, | |
654 | }; | |
655 | ||
656 | static const struct option options[] = { | |
657 | { "help", no_argument, NULL, 'h' }, | |
658 | { "version", no_argument, NULL, ARG_VERSION }, | |
659 | { "root", required_argument, NULL, ARG_ROOT }, | |
660 | { "locale", required_argument, NULL, ARG_LOCALE }, | |
661 | { "locale-messages", required_argument, NULL, ARG_LOCALE_MESSAGES }, | |
662 | { "timezone", required_argument, NULL, ARG_TIMEZONE }, | |
663 | { "hostname", required_argument, NULL, ARG_HOSTNAME }, | |
664 | { "machine-id", required_argument, NULL, ARG_MACHINE_ID }, | |
665 | { "root-password", required_argument, NULL, ARG_ROOT_PASSWORD }, | |
666 | { "root-password-file", required_argument, NULL, ARG_ROOT_PASSWORD_FILE }, | |
667 | { "prompt", no_argument, NULL, ARG_PROMPT }, | |
668 | { "prompt-locale", no_argument, NULL, ARG_PROMPT_LOCALE }, | |
669 | { "prompt-timezone", no_argument, NULL, ARG_PROMPT_TIMEZONE }, | |
670 | { "prompt-hostname", no_argument, NULL, ARG_PROMPT_HOSTNAME }, | |
671 | { "prompt-root-password", no_argument, NULL, ARG_PROMPT_ROOT_PASSWORD }, | |
672 | { "copy", no_argument, NULL, ARG_COPY }, | |
673 | { "copy-locale", no_argument, NULL, ARG_COPY_LOCALE }, | |
674 | { "copy-timezone", no_argument, NULL, ARG_COPY_TIMEZONE }, | |
675 | { "copy-root-password", no_argument, NULL, ARG_COPY_ROOT_PASSWORD }, | |
676 | { "setup-machine-id", no_argument, NULL, ARG_SETUP_MACHINE_ID }, | |
677 | {} | |
678 | }; | |
679 | ||
680 | int r, c; | |
681 | ||
682 | assert(argc >= 0); | |
683 | assert(argv); | |
684 | ||
601185b4 | 685 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
418b9be5 LP |
686 | |
687 | switch (c) { | |
688 | ||
689 | case 'h': | |
601185b4 ZJS |
690 | help(); |
691 | return 0; | |
418b9be5 LP |
692 | |
693 | case ARG_VERSION: | |
3f6fd1ba | 694 | return version(); |
418b9be5 LP |
695 | |
696 | case ARG_ROOT: | |
0f03c2a4 | 697 | r = parse_path_argument_and_warn(optarg, true, &arg_root); |
0f474365 | 698 | if (r < 0) |
0f03c2a4 | 699 | return r; |
418b9be5 LP |
700 | break; |
701 | ||
702 | case ARG_LOCALE: | |
703 | if (!locale_is_valid(optarg)) { | |
704 | log_error("Locale %s is not valid.", optarg); | |
705 | return -EINVAL; | |
706 | } | |
707 | ||
2fc09a9c DM |
708 | r = free_and_strdup(&arg_locale, optarg); |
709 | if (r < 0) | |
418b9be5 LP |
710 | return log_oom(); |
711 | ||
712 | break; | |
713 | ||
714 | case ARG_LOCALE_MESSAGES: | |
715 | if (!locale_is_valid(optarg)) { | |
716 | log_error("Locale %s is not valid.", optarg); | |
717 | return -EINVAL; | |
718 | } | |
719 | ||
2fc09a9c DM |
720 | r = free_and_strdup(&arg_locale_messages, optarg); |
721 | if (r < 0) | |
418b9be5 LP |
722 | return log_oom(); |
723 | ||
724 | break; | |
725 | ||
726 | case ARG_TIMEZONE: | |
727 | if (!timezone_is_valid(optarg)) { | |
728 | log_error("Timezone %s is not valid.", optarg); | |
729 | return -EINVAL; | |
730 | } | |
731 | ||
2fc09a9c DM |
732 | r = free_and_strdup(&arg_timezone, optarg); |
733 | if (r < 0) | |
418b9be5 LP |
734 | return log_oom(); |
735 | ||
736 | break; | |
737 | ||
738 | case ARG_ROOT_PASSWORD: | |
2fc09a9c DM |
739 | r = free_and_strdup(&arg_root_password, optarg); |
740 | if (r < 0) | |
418b9be5 | 741 | return log_oom(); |
418b9be5 LP |
742 | break; |
743 | ||
744 | case ARG_ROOT_PASSWORD_FILE: | |
0da16248 | 745 | arg_root_password = mfree(arg_root_password); |
418b9be5 LP |
746 | |
747 | r = read_one_line_file(optarg, &arg_root_password); | |
23bbb0de MS |
748 | if (r < 0) |
749 | return log_error_errno(r, "Failed to read %s: %m", optarg); | |
418b9be5 LP |
750 | |
751 | break; | |
752 | ||
753 | case ARG_HOSTNAME: | |
34ad6090 | 754 | if (!hostname_is_valid(optarg, true)) { |
418b9be5 LP |
755 | log_error("Host name %s is not valid.", optarg); |
756 | return -EINVAL; | |
757 | } | |
758 | ||
ae691c1d | 759 | hostname_cleanup(optarg); |
2fc09a9c DM |
760 | r = free_and_strdup(&arg_hostname, optarg); |
761 | if (r < 0) | |
418b9be5 LP |
762 | return log_oom(); |
763 | ||
764 | break; | |
765 | ||
766 | case ARG_MACHINE_ID: | |
767 | if (sd_id128_from_string(optarg, &arg_machine_id) < 0) { | |
768 | log_error("Failed to parse machine id %s.", optarg); | |
769 | return -EINVAL; | |
770 | } | |
771 | ||
772 | break; | |
773 | ||
774 | case ARG_PROMPT: | |
775 | arg_prompt_locale = arg_prompt_timezone = arg_prompt_hostname = arg_prompt_root_password = true; | |
776 | break; | |
777 | ||
778 | case ARG_PROMPT_LOCALE: | |
779 | arg_prompt_locale = true; | |
780 | break; | |
781 | ||
782 | case ARG_PROMPT_TIMEZONE: | |
783 | arg_prompt_timezone = true; | |
784 | break; | |
785 | ||
786 | case ARG_PROMPT_HOSTNAME: | |
787 | arg_prompt_hostname = true; | |
788 | break; | |
789 | ||
790 | case ARG_PROMPT_ROOT_PASSWORD: | |
791 | arg_prompt_root_password = true; | |
792 | break; | |
793 | ||
794 | case ARG_COPY: | |
795 | arg_copy_locale = arg_copy_timezone = arg_copy_root_password = true; | |
e926f647 | 796 | break; |
418b9be5 LP |
797 | |
798 | case ARG_COPY_LOCALE: | |
799 | arg_copy_locale = true; | |
800 | break; | |
801 | ||
802 | case ARG_COPY_TIMEZONE: | |
803 | arg_copy_timezone = true; | |
804 | break; | |
805 | ||
806 | case ARG_COPY_ROOT_PASSWORD: | |
807 | arg_copy_root_password = true; | |
808 | break; | |
809 | ||
810 | case ARG_SETUP_MACHINE_ID: | |
811 | ||
812 | r = sd_id128_randomize(&arg_machine_id); | |
23bbb0de MS |
813 | if (r < 0) |
814 | return log_error_errno(r, "Failed to generate randomized machine ID: %m"); | |
418b9be5 LP |
815 | |
816 | break; | |
817 | ||
818 | case '?': | |
819 | return -EINVAL; | |
820 | ||
821 | default: | |
822 | assert_not_reached("Unhandled option"); | |
823 | } | |
418b9be5 LP |
824 | |
825 | return 1; | |
826 | } | |
827 | ||
828 | int main(int argc, char *argv[]) { | |
1d84ad94 | 829 | bool enabled; |
418b9be5 LP |
830 | int r; |
831 | ||
832 | r = parse_argv(argc, argv); | |
833 | if (r <= 0) | |
834 | goto finish; | |
835 | ||
836 | log_set_target(LOG_TARGET_AUTO); | |
837 | log_parse_environment(); | |
838 | log_open(); | |
839 | ||
840 | umask(0022); | |
841 | ||
1d84ad94 LP |
842 | r = proc_cmdline_get_bool("systemd.firstboot", &enabled); |
843 | if (r < 0) { | |
844 | log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring."); | |
845 | goto finish; | |
846 | } | |
847 | if (r > 0 && !enabled) { | |
848 | r = 0; /* disabled */ | |
849 | goto finish; | |
f582cbca LP |
850 | } |
851 | ||
418b9be5 LP |
852 | r = process_locale(); |
853 | if (r < 0) | |
854 | goto finish; | |
855 | ||
856 | r = process_timezone(); | |
857 | if (r < 0) | |
858 | goto finish; | |
859 | ||
860 | r = process_hostname(); | |
861 | if (r < 0) | |
862 | goto finish; | |
863 | ||
864 | r = process_machine_id(); | |
865 | if (r < 0) | |
866 | goto finish; | |
867 | ||
868 | r = process_root_password(); | |
869 | if (r < 0) | |
870 | goto finish; | |
871 | ||
872 | finish: | |
873 | free(arg_root); | |
874 | free(arg_locale); | |
875 | free(arg_locale_messages); | |
876 | free(arg_timezone); | |
877 | free(arg_hostname); | |
1602b008 | 878 | string_erase(arg_root_password); |
418b9be5 LP |
879 | free(arg_root_password); |
880 | ||
881 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
882 | } |