HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/firstboot/firstboot.c
Git blob: 3941618db9569a3f41b4efb85058c4022d6f8bb1
Lines 1–1000 of 1,871
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <fcntl.h>
#include <unistd.h>
#include "sd-bus.h"
#include "sd-id128.h"
#include "sd-json.h"
#include "sd-varlink.h"
#include "alloc-util.h"
#include "ansi-color.h"
#include "ask-password-api.h"
#include "build.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-unit-util.h"
#include "bus-util.h"
#include "bus-wait-for-jobs.h"
#include "chase.h"
#include "copy.h"
#include "creds-util.h"
#include "dissect-image.h"
#include "dlopen-note.h"
#include "env-file.h"
#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "firstboot-util.h"
#include "fs-util.h"
#include "glyph-util.h"
#include "hostname-setup.h"
#include "hostname-util.h"
#include "image-policy.h"
#include "kbd-util.h"
#include "label-util.h"
#include "libcrypt-util.h"
#include "locale-setup.h"
#include "locale-util.h"
#include "lock-util.h"
#include "loop-util.h"
#include "main-func.h"
#include "memory-util.h"
#include "mount-util.h"
#include "os-util.h"
#include "parse-argument.h"
#include "password-quality-util.h"
#include "path-util.h"
#include "plymouth-util.h"
#include "prompt-util.h"
#include "runtime-scope.h"
#include "smack-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "time-util.h"
#include "tmpfile-util.h"
#include "user-util.h"
#include "vconsole-util.h"
#include "verbs.h"
static char *arg_root = NULL;
static char *arg_image = NULL;
static char *arg_locale = NULL; /* $LANG */
static char *arg_locale_messages = NULL; /* $LC_MESSAGES */
static char *arg_keymap = NULL;
static char *arg_timezone = NULL;
static char *arg_hostname = NULL;
static sd_id128_t arg_machine_id = {};
static char **arg_machine_tags = NULL;
static char *arg_root_password = NULL;
static char *arg_root_shell = NULL;
static char *arg_kernel_cmdline = NULL;
static bool arg_prompt_locale = false;
static bool arg_prompt_keymap = false;
static bool arg_prompt_keymap_auto = false;
static bool arg_prompt_timezone = false;
static bool arg_prompt_hostname = false;
static bool arg_prompt_root_password = false;
static bool arg_prompt_root_shell = false;
static bool arg_headless = false;
static bool arg_copy_locale = false;
static bool arg_copy_keymap = false;
static bool arg_copy_timezone = false;
static bool arg_copy_root_password = false;
static bool arg_copy_root_shell = false;
static bool arg_force = false;
static bool arg_delete_root_password = false;
static bool arg_root_password_is_hashed = false;
static bool arg_welcome = true;
static bool arg_reset = false;
static ImagePolicy *arg_image_policy = NULL;
static bool arg_chrome = true;
static bool arg_mute_console = false;
static LabelContext *arg_label_context = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
STATIC_DESTRUCTOR_REGISTER(arg_locale, freep);
STATIC_DESTRUCTOR_REGISTER(arg_locale_messages, freep);
STATIC_DESTRUCTOR_REGISTER(arg_keymap, freep);
STATIC_DESTRUCTOR_REGISTER(arg_timezone, freep);
STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep);
STATIC_DESTRUCTOR_REGISTER(arg_machine_tags, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_root_password, erase_and_freep);
STATIC_DESTRUCTOR_REGISTER(arg_root_shell, freep);
STATIC_DESTRUCTOR_REGISTER(arg_kernel_cmdline, freep);
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
STATIC_DESTRUCTOR_REGISTER(arg_label_context, mac_label_context_freep);
COMMAND(
"systemd-firstboot\0",
"Configure basic settings of the system.",
.man_pages = "systemd-firstboot(1)\0",
);
static bool welcome_done = false;
static void print_welcome(int rfd, sd_varlink **mute_console_link) {
_cleanup_free_ char *pretty_name = NULL, *os_name = NULL, *ansi_color = NULL, *fancy_name = NULL;
const char *pn, *ac;
int r;
assert(rfd >= 0);
assert(mute_console_link);
/* Needs to be called before mute_console or it will garble the screen */
if (arg_welcome)
(void) plymouth_hide_splash();
if (!*mute_console_link && arg_mute_console)
(void) mute_console(mute_console_link);
if (!arg_welcome)
return;
if (welcome_done) {
putchar('\n'); /* Add some breathing room between multiple prompts */
return;
}
(void) terminal_reset_defensive_locked(STDOUT_FILENO, /* flags= */ 0);
if (arg_chrome)
chrome_show("Initial Setup", /* bottom= */ NULL);
r = parse_os_release_at(rfd,
"PRETTY_NAME", &pretty_name,
"FANCY_NAME", &fancy_name,
"NAME", &os_name,
"ANSI_COLOR", &ansi_color);
if (r < 0)
log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
"Failed to read os-release file, ignoring: %m");
pn = os_release_pretty_name(pretty_name, os_name);
ac = isempty(ansi_color) ? "0" : ansi_color;
if (use_fancy_name(unescape_fancy_name(&fancy_name)))
printf(ANSI_HIGHLIGHT "Welcome to " ANSI_NORMAL "%s" ANSI_HIGHLIGHT "!" ANSI_NORMAL "\n", fancy_name);
else if (colors_enabled())
printf(ANSI_HIGHLIGHT "Welcome to " ANSI_NORMAL "\x1B[%sm%s" ANSI_HIGHLIGHT "!" ANSI_NORMAL "\n", ac, pn);
else
printf("Welcome to %s!\n", pn);
putchar('\n');
if (emoji_enabled()) {
fputs(glyph(GLYPH_SPARKLES), stdout);
putchar(' ');
}
printf("Please configure the system!\n\n");
welcome_done = true;
}
static int should_configure(int dir_fd, const char *filename) {
_cleanup_fclose_ FILE *passwd = NULL, *shadow = NULL;
int r;
assert(dir_fd >= 0);
assert(filename);
if (streq(filename, "passwd") && !arg_force)
/* We may need to do additional checks, so open the file. */
r = xfopenat(dir_fd, filename, "re", O_NOFOLLOW, &passwd);
else
r = RET_NERRNO(faccessat(dir_fd, filename, F_OK, AT_SYMLINK_NOFOLLOW));
if (r == -ENOENT)
return true; /* missing */
if (r < 0)
return log_error_errno(r, "Failed to access %s: %m", filename);
if (arg_force)
return true; /* exists, but if --force was given we should still configure the file. */
if (!passwd)
return false;
/* In case of /etc/passwd, do an additional check for the root password field.
* We first check that passwd redirects to shadow, and then we check shadow.
*/
struct passwd *i;
while ((r = fgetpwent_sane(passwd, &i)) > 0) {
if (!streq(i->pw_name, "root"))
continue;
if (streq_ptr(i->pw_passwd, PASSWORD_SEE_SHADOW))
break;
log_debug("passwd: root account with non-shadow password found, treating root as configured");
return false;
}
if (r < 0)
return log_error_errno(r, "Failed to read %s: %m", filename);
if (r == 0) {
log_debug("No root account found in %s, assuming root is not configured.", filename);
return true;
}
r = xfopenat(dir_fd, "shadow", "re", O_NOFOLLOW, &shadow);
if (r == -ENOENT) {
log_debug("No shadow file found, assuming root is not configured.");
return true; /* missing */
}
if (r < 0)
return log_error_errno(r, "Failed to access shadow: %m");
struct spwd *j;
while ((r = fgetspent_sane(shadow, &j)) > 0) {
if (!streq(j->sp_namp, "root"))
continue;
bool unprovisioned = streq_ptr(j->sp_pwdp, PASSWORD_UNPROVISIONED);
log_debug("Root account found, %s.",
unprovisioned ? "with unprovisioned password, treating root as not configured" :
"treating root as configured");
return unprovisioned;
}
if (r < 0)
return log_error_errno(r, "Failed to read shadow: %m");
assert(r == 0);
log_debug("No root account found in shadow, assuming root is not configured.");
return true;
}
static int locale_is_ok(const char *name, void *userdata) {
int rfd = ASSERT_FD(PTR_TO_FD(userdata)), r;
r = dir_fd_is_root(rfd);
if (r < 0)
log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m");
return r != 0 ? locale_is_installed(name) > 0 : locale_is_valid(name);
}
static bool headless_skips_prompt_for(const char *what) {
assert(what);
if (!arg_headless)
return false;
log_debug("Running headless, not prompting for %s.", what);
return true;
}
static int prompt_locale(int rfd, sd_varlink **mute_console_link) {
_cleanup_strv_free_ char **locales = NULL;
bool acquired_from_creds = false;
int r;
assert(rfd >= 0);
if (arg_locale || arg_locale_messages)
return 0;
r = read_credential("firstboot.locale", (void**) &arg_locale, NULL);
if (r < 0)
log_debug_errno(r, "Failed to read credential firstboot.locale, ignoring: %m");
else
acquired_from_creds = true;
r = read_credential("firstboot.locale-messages", (void**) &arg_locale_messages, NULL);
if (r < 0)
log_debug_errno(r, "Failed to read credential firstboot.locale-messages, ignoring: %m");
else
acquired_from_creds = true;
if (acquired_from_creds) {
log_debug("Acquired locale from credentials.");
return 0;
}
if (!arg_prompt_locale) {
log_debug("Prompting for locale was not requested.");
return 0;
}
r = get_locales(&locales);
if (r < 0)
return log_error_errno(r, "Cannot query locales list: %m");
if (strv_isempty(locales))
log_debug("No locales found, skipping locale selection.");
else if (strv_length(locales) == 1) {
if (streq(locales[0], SYSTEMD_DEFAULT_LOCALE))
log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
else {
log_debug("Only a single locale available (%s), selecting it as default.", locales[0]);
arg_locale = strdup(locales[0]);
if (!arg_locale)
return log_oom();
/* Not setting arg_locale_message here, since it defaults to LANG anyway */
}
} else {
if (headless_skips_prompt_for("locale"))
return 0;
print_welcome(rfd, mute_console_link);
_cleanup_free_ char *prefill = NULL;
(void) locale_lang_from_efi(&prefill, LOCALE_REQUIRE_INSTALLED|LOCALE_SUPPRESS_EN_US);
r = prompt_loop("Please enter the new system locale name or number",
GLYPH_WORLD,
prefill,
locales,
/* accepted= */ NULL,
/* ellipsize_percentage= */ 60,
/* n_columns= */ 3,
/* column_width= */ 20,
locale_is_ok,
/* refresh= */ NULL,
FD_TO_PTR(rfd),
PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
&arg_locale);
if (r < 0)
return r;
if (isempty(arg_locale))
return 0;
r = prompt_loop("Please enter the new system message locale name or number",
GLYPH_WORLD,
/* prefill= */ NULL,
locales,
/* accepted= */ NULL,
/* ellipsize_percentage= */ 60,
/* n_columns= */ 3,
/* column_width= */ 20,
locale_is_ok,
/* refresh= */ NULL,
FD_TO_PTR(rfd),
PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
&arg_locale_messages);
if (r < 0)
return r;
/* Suppress the messages setting if it's the same as the main locale anyway */
if (streq_ptr(arg_locale, arg_locale_messages))
arg_locale_messages = mfree(arg_locale_messages);
}
return 0;
}
static int process_locale(int rfd, sd_varlink **mute_console_link) {
_cleanup_close_ int pfd = -EBADF;
_cleanup_free_ char *f = NULL;
char* locales[3];
unsigned i = 0;
int r;
assert(rfd >= 0);
pfd = chase_and_open_parent_at(rfd, rfd, etc_locale_conf(),
CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
&f);
if (pfd < 0)
return log_error_errno(pfd, "Failed to chase /etc/locale.conf: %m");
r = should_configure(pfd, f);
if (r == 0)
log_debug("Found /etc/locale.conf, assuming locale information has been configured.");
if (r <= 0)
return r;
r = dir_fd_is_root(rfd);
if (r < 0)
return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
if (arg_copy_locale && r == 0) {
r = copy_file_atomic_at(AT_FDCWD, etc_locale_conf(), pfd, f, 0644, /* copy_flags= */ 0);
if (r != -ENOENT) {
if (r < 0)
return log_error_errno(r, "Failed to copy host's /etc/locale.conf: %m");
log_info("Copied host's /etc/locale.conf.");
return 0;
}
}
r = prompt_locale(rfd, mute_console_link);
if (r < 0)
return r;
if (!isempty(arg_locale))
locales[i++] = strjoina("LANG=", arg_locale);
if (!isempty(arg_locale_messages) && !streq_ptr(arg_locale_messages, arg_locale))
locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages);
if (i == 0)
return 0;
locales[i] = NULL;
r = write_env_file_label(
pfd,
f,
/* headers= */ NULL,
locales,
WRITE_ENV_FILE_LABEL,
arg_label_context);
if (r < 0)
return log_error_errno(r, "Failed to write /etc/locale.conf: %m");
log_info("/etc/locale.conf written.");
return 1;
}
static int keymap_is_ok(const char* name, void *userdata) {
int rfd = ASSERT_FD(PTR_TO_FD(userdata)), r;
r = dir_fd_is_root(rfd);
if (r < 0)
log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m");
return r != 0 ? keymap_exists(name) > 0 : keymap_is_valid(name);
}
static int prompt_keymap(int rfd, sd_varlink **mute_console_link) {
_cleanup_strv_free_ char **kmaps = NULL;
int r;
assert(rfd >= 0);
if (arg_keymap)
return 0;
_cleanup_free_ char *km = NULL;
r = read_credential("firstboot.keymap", (void**) &km, NULL);
if (r < 0)
log_debug_errno(r, "Failed to read credential firstboot.keymap, ignoring: %m");
else if (!keymap_is_valid(km))
log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "Keymap '%s' supplied via credential is not valid, ignoring.", km);
else {
log_debug("Acquired keymap from credential.");
arg_keymap = TAKE_PTR(km);
return 0;
}
bool b;
if (arg_prompt_keymap_auto) {
_cleanup_free_ char *ttyname = NULL;
r = getttyname_harder(STDOUT_FILENO, &ttyname);
if (r < 0) {
log_debug_errno(r, "Cannot determine TTY we are connected, ignoring: %m");
b = false; /* if we can't resolve this, it's probably not a VT */
} else {
b = tty_is_vc_resolve(ttyname);
log_debug("Detected connection to local console: %s", yes_no(b));
}
} else
b = arg_prompt_keymap;
if (!b) {
log_debug("Prompting for keymap was not requested.");
return 0;
}
if (headless_skips_prompt_for("keymap"))
return 0;
r = get_keymaps(&kmaps);
if (r == -ENOENT) /* no keymaps installed */
return log_debug_errno(r, "No keymaps are installed.");
if (r < 0)
return log_error_errno(r, "Failed to read keymaps: %m");
print_welcome(rfd, mute_console_link);
_cleanup_free_ char *prefill = NULL;
(void) vconsole_keymap_from_efi(&prefill);
return prompt_loop(
"Please enter the new keymap name or number",
GLYPH_KEYBOARD,
prefill,
kmaps,
/* accepted= */ NULL,
/* ellipsize_percentage= */ 60,
/* n_columns= */ 3,
/* column_width= */ 20,
keymap_is_ok,
/* refresh= */ NULL,
FD_TO_PTR(rfd),
PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
&arg_keymap);
}
static int process_keymap(int rfd, sd_varlink **mute_console_link) {
_cleanup_close_ int pfd = -EBADF;
_cleanup_free_ char *f = NULL;
_cleanup_strv_free_ char **keymap = NULL;
int r;
assert(rfd >= 0);
pfd = chase_and_open_parent_at(rfd, rfd, etc_vconsole_conf(),
CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
&f);
if (pfd < 0)
return log_error_errno(pfd, "Failed to chase /etc/vconsole.conf: %m");
r = should_configure(pfd, f);
if (r == 0)
log_debug("Found /etc/vconsole.conf, assuming console has been configured.");
if (r <= 0)
return r;
r = dir_fd_is_root(rfd);
if (r < 0)
return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
if (arg_copy_keymap && r == 0) {
r = copy_file_atomic_at(AT_FDCWD, etc_vconsole_conf(), pfd, f, 0644, /* copy_flags= */ 0);
if (r != -ENOENT) {
if (r < 0)
return log_error_errno(r, "Failed to copy host's /etc/vconsole.conf: %m");
log_info("Copied host's /etc/vconsole.conf.");
return 0;
}
}
r = prompt_keymap(rfd, mute_console_link);
if (r == -ENOENT)
return 0; /* don't fail if no keymaps are installed */
if (r < 0)
return r;
if (isempty(arg_keymap))
return 0;
VCContext vc = {
.keymap = arg_keymap,
};
_cleanup_(x11_context_clear) X11Context xc = {};
r = vconsole_convert_to_x11(&vc, /* verify= */ NULL, &xc);
if (r < 0)
return log_error_errno(r, "Failed to convert keymap data: %m");
r = vconsole_serialize(&vc, &xc, &keymap);
if (r < 0)
return log_error_errno(r, "Failed to serialize keymap data: %m");
r = write_vconsole_conf(pfd, f, keymap);
if (r < 0)
return log_error_errno(r, "Failed to write /etc/vconsole.conf: %m");
log_info("/etc/vconsole.conf written.");
return 1;
}
static int timezone_is_ok(const char *name, void *userdata) {
return timezone_is_valid(name, LOG_DEBUG);
}
static int prompt_timezone(int rfd, sd_varlink **mute_console_link) {
_cleanup_strv_free_ char **zones = NULL;
int r;
assert(rfd >= 0);
if (arg_timezone)
return 0;
_cleanup_free_ char *tz = NULL;
r = read_credential("firstboot.timezone", (void**) &tz, NULL);
if (r < 0)
log_debug_errno(r, "Failed to read credential firstboot.timezone, ignoring: %m");
else if (!timezone_is_valid(tz, LOG_DEBUG))
log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "Timezone '%s' supplied via credential is not valid, ignoring.", tz);
else {
log_debug("Acquired timezone from credential.");
arg_timezone = TAKE_PTR(tz);
return 0;
}
if (!arg_prompt_timezone) {
log_debug("Prompting for timezone was not requested.");
return 0;
}
if (headless_skips_prompt_for("timezone"))
return 0;
r = get_timezones(&zones);
if (r < 0)
return log_error_errno(r, "Cannot query timezone list: %m");
print_welcome(rfd, mute_console_link);
return prompt_loop(
"Please enter the new timezone name or number",
GLYPH_CLOCK,
/* prefill= */ NULL,
zones,
/* accepted= */ NULL,
/* ellipsize_percentage= */ 30,
/* n_columns= */ 3,
/* column_width= */ 20,
timezone_is_ok,
/* refresh= */ NULL,
FD_TO_PTR(rfd),
PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
&arg_timezone);
}
static int process_timezone(int rfd, sd_varlink **mute_console_link) {
_cleanup_close_ int pfd = -EBADF;
_cleanup_free_ char *f = NULL, *relpath = NULL;
const char *e;
int r;
assert(rfd >= 0);
pfd = chase_and_open_parent_at(rfd, rfd, etc_localtime(),
CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
&f);
if (pfd < 0)
return log_error_errno(pfd, "Failed to chase /etc/localtime: %m");
r = should_configure(pfd, f);
if (r == 0)
log_debug("Found /etc/localtime, assuming timezone has been configured.");
if (r <= 0)
return r;
r = dir_fd_is_root(rfd);
if (r < 0)
return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
if (arg_copy_timezone && r == 0) {
_cleanup_free_ char *s = NULL;
r = readlink_malloc(etc_localtime(), &s);
if (r != -ENOENT) {
if (r < 0)
return log_error_errno(r, "Failed to read host's /etc/localtime: %m");
r = symlinkat_atomic_full_label(s, pfd, f, SYMLINK_LABEL, arg_label_context);
if (r < 0)
return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
log_info("Copied host's /etc/localtime.");
return 0;
}
}
r = prompt_timezone(rfd, mute_console_link);
if (r < 0)
return r;
if (isempty(arg_timezone))
return 0;
e = strjoina("/usr/share/zoneinfo/", arg_timezone);
r = path_make_relative_parent(etc_localtime(), e, &relpath);
if (r < 0)
return r;
r = symlinkat_atomic_full_label(relpath, pfd, f, SYMLINK_LABEL, arg_label_context);
if (r < 0)
return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
log_info("/etc/localtime written");
return 0;
}
static int hostname_is_ok(const char *name, void *userdata) {
return hostname_is_valid(name, VALID_HOSTNAME_TRAILING_DOT);
}
static int prompt_hostname(int rfd, sd_varlink **mute_console_link) {
int r;
assert(rfd >= 0);
if (arg_hostname)
return 0;
_cleanup_free_ char *hn = NULL;
r = read_credential("firstboot.hostname", (void**) &hn, NULL);
if (r < 0)
log_debug_errno(r, "Failed to read credential firstboot.hostname, ignoring: %m");
else if (!hostname_is_valid(hn, VALID_HOSTNAME_TRAILING_DOT|VALID_HOSTNAME_QUESTION_MARK|VALID_HOSTNAME_WORD_TOKEN))
log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "Hostname '%s' supplied via credential is not valid, ignoring.", hn);
else {
log_debug("Acquired hostname from credentials.");
arg_hostname = TAKE_PTR(hn);
hostname_cleanup(arg_hostname);
return 0;
}
if (!arg_prompt_hostname) {
log_debug("Prompting for hostname was not requested.");
return 0;
}
if (headless_skips_prompt_for("hostname"))
return 0;
print_welcome(rfd, mute_console_link);
r = prompt_loop("Please enter the new hostname",
GLYPH_LABEL,
/* prefill= */ NULL,
/* menu= */ NULL,
/* accepted= */ NULL,
/* ellipsize_percentage= */ 100,
/* n_columns= */ 3,
/* column_width= */ 20,
hostname_is_ok,
/* refresh= */ NULL,
FD_TO_PTR(rfd),
PROMPT_MAY_SKIP,
&arg_hostname);
if (r < 0)
return r;
if (arg_hostname)
hostname_cleanup(arg_hostname);
return 0;
}
static int process_hostname(int rfd, sd_varlink **mute_console_link) {
_cleanup_close_ int pfd = -EBADF;
_cleanup_free_ char *f = NULL;
int r;
assert(rfd >= 0);
pfd = chase_and_open_parent_at(rfd, rfd, etc_hostname(), CHASE_MKDIR_0755|CHASE_WARN, &f);
if (pfd < 0)
return log_error_errno(pfd, "Failed to chase /etc/hostname: %m");
r = should_configure(pfd, f);
if (r == 0)
log_debug("Found /etc/hostname, assuming hostname has been configured.");
if (r <= 0)
return r;
r = prompt_hostname(rfd, mute_console_link);
if (r < 0)
return r;
if (isempty(arg_hostname))
return 0;
/* On running systems we have a machine ID, so resolve any '?'/'$' wildcards now and persist them.
* This "freezes" the name, so later word list updates do not change it. When operating on an offline
* image (--root=/--image=) the target's machine ID is not known yet, so write the template verbatim
* and let it be resolved on each first boot. */
const char *hostname = arg_hostname;
_cleanup_free_ char *resolved = NULL;
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL;
if (!arg_root) {
r = hostname_substitute_wildcards(arg_hostname, &resolved);
if (r < 0)
log_warning_errno(r, "Failed to resolve wildcards in hostname '%s', writing it verbatim: %m", arg_hostname);
else if (!hostname_is_valid(resolved, VALID_HOSTNAME_TRAILING_DOT))
log_warning("Resolved hostname '%s' is invalid, writing template '%s' verbatim instead.", resolved, arg_hostname);
else {
hostname = resolved;
r = sd_varlink_connect_address(&vl, "/run/systemd/io.systemd.Hostname");
if (r < 0)
log_warning_errno(r, "Failed to connect to systemd-hostnamed, writing /etc/hostname directly: %m");
}
}
if (vl) {
sd_json_variant *reply = NULL;
const char *error_id = NULL;
r = sd_varlink_callbo(
vl,
"io.systemd.Hostname.SetStaticHostname",
&reply,
&error_id,
SD_JSON_BUILD_PAIR_STRING("newValue", hostname));
if (r < 0)
log_warning_errno(r, "Failed to call io.systemd.Hostname.SetStaticHostname, writing /etc/hostname directly: %m");
else if (error_id)
log_warning_errno(sd_varlink_error_to_errno(error_id, reply),
"Failed to set static hostname, writing /etc/hostname directly: %s", error_id);
else {
log_info("Static hostname configured via systemd-hostnamed.");
return 0;
}
}
r = write_string_file_full_label(
pfd, f, hostname,
WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL,
/* ts= */ NULL, /* label_fn= */ NULL, arg_label_context);
if (r < 0)
return log_error_errno(r, "Failed to write /etc/hostname: %m");
log_info("/etc/hostname written.");
return 0;
}
static int process_machine_id(int rfd) {
_cleanup_close_ int pfd = -EBADF;
_cleanup_free_ char *f = NULL;
int r;
assert(rfd >= 0);
pfd = chase_and_open_parent_at(rfd, rfd, "/etc/machine-id",
CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
&f);
if (pfd < 0)
return log_error_errno(pfd, "Failed to chase /etc/machine-id: %m");
r = should_configure(pfd, f);
if (r == 0)
log_debug("Found /etc/machine-id, assuming machine-id has been configured.");
if (r <= 0)
return r;
if (sd_id128_is_null(arg_machine_id)) {
log_debug("Initialization of machine-id was not requested, skipping.");
return 0;
}
r = write_string_file_full_label(pfd, "machine-id", SD_ID128_TO_STRING(arg_machine_id),
WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL,
/* ts= */ NULL, /* label_fn= */ NULL, arg_label_context);
if (r < 0)
return log_error_errno(r, "Failed to write /etc/machine-id: %m");
log_info("/etc/machine-id written.");
return 0;
}
static int process_machine_tags(int rfd) {
int r;
assert(rfd >= 0);
_cleanup_free_ char *f = NULL;
_cleanup_close_ int pfd = chase_and_open_parent_at(
/* root_fd= */ rfd,
/* dir_fd= */ rfd,
"/etc/machine-info",
CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
&f);
if (pfd < 0)
return log_error_errno(pfd, "Failed to chase /etc/machine-info parent: %m");
r = should_configure(pfd, f);
if (r == 0)
log_debug("Found /etc/machine-info, assuming machine tags have been configured.");
if (r <= 0)
return r;
if (!arg_machine_tags) {
_cleanup_free_ char *tags = NULL;
r = read_credential("firstboot.machine-tags", (void**) &tags, /* ret_size= */ NULL);
if (r < 0)
log_debug_errno(r, "Failed to read credential firstboot.machine-tags, ignoring: %m");
else {
_cleanup_strv_free_ char **l = NULL;
r = machine_tags_from_string(tags, /* graceful= */ false, &l);
if (r < 0)
log_warning_errno(r, "Failed to parse machine tags '%s', ignoring credential: %m", tags);
else {
strv_free_and_replace(arg_machine_tags, l);
log_debug("Acquired machine tags list from credentials.");
}
}
}
/* NB: We do not prompt for machine tags, at least not for now */
if (!arg_machine_tags) {
log_debug("Initialization of machine tags was not requested, skipping.");
return 0;
}
_cleanup_free_ char *j = strv_join(arg_machine_tags, ":");
if (!j)
return log_oom();
_cleanup_free_ char *c = strjoin("TAGS=\"", j, "\"\n");
if (!c)
return log_oom();
r = write_string_file_full_label(
pfd,
"machine-info",
c,
WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL,
/* ts= */ NULL, /* label_fn= */ NULL, arg_label_context);
if (r < 0)
return log_error_errno(r, "Failed to write /etc/machine-info: %m");
log_info("/etc/machine-info written.");
return 0;
}
static int prompt_root_password(int rfd, sd_varlink **mute_console_link) {
const char *msg1, *msg2;
int r;
assert(rfd >= 0);
if (arg_root_password)
return 0;
if (get_credential_user_password("root", &arg_root_password, &arg_root_password_is_hashed) >= 0)
return 0;
if (!arg_prompt_root_password) {
log_debug("Prompting for root password was not requested.");
return 0;
}
if (headless_skips_prompt_for("root password"))
return 0;
print_welcome(rfd, mute_console_link);
msg1 = "Please enter the new root password (empty to skip):";
msg2 = "Please enter the new root password again:";
suggest_passwords();
for (;;) {
_cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
_cleanup_free_ char *error = NULL;
AskPasswordRequest req = {
.tty_fd = -EBADF,
.message = msg1,
.until = USEC_INFINITY,
.hup_fd = -EBADF,
};
r = ask_password_tty(&req, /* flags= */ 0, &a);
if (r < 0)
return log_error_errno(r, "Failed to query root password: %m");
if (strv_length(a) != 1)
return log_error_errno(SYNTHETIC_ERRNO(EIO),
"Received multiple passwords, where we expected one.");
if (isempty(*a)) {
log_info("No password entered, skipping.");
break;
}
r = check_password_quality(*a, /* old= */ NULL, "root", &error);
if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
log_warning("Password quality check is not supported, proceeding anyway.");
else if (r < 0)
return log_error_errno(r, "Failed to check password quality: %m");
else if (r == 0)
log_warning("Password is weak, accepting anyway: %s", error);
req.message = msg2;
r = ask_password_tty(&req, /* flags= */ 0, &b);
if (r < 0)
return log_error_errno(r, "Failed to query root password: %m");
if (strv_length(b) != 1)
return log_error_errno(SYNTHETIC_ERRNO(EIO),
"Received multiple passwords, where we expected one.");
if (!streq(*a, *b)) {
log_error("Entered passwords did not match, please try again.");
continue;
}
arg_root_password = TAKE_PTR(*a);
break;