🧹 move setting root password into gucc

This commit is contained in:
Vladislav Nepogodin 2024-06-29 18:33:09 +04:00
parent 0a70f0a9dd
commit 606fc3e6af
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
3 changed files with 17 additions and 4 deletions

View File

@ -26,6 +26,9 @@ auto set_hostname(std::string_view hostname, std::string_view mountpoint) noexce
// Set system hosts
auto set_hosts(std::string_view hostname, std::string_view mountpoint) noexcept -> bool;
// Set password for root user
auto set_root_password(std::string_view password, std::string_view mountpoint) noexcept -> bool;
} // namespace gucc::user
#endif // USER_HPP

View File

@ -169,4 +169,15 @@ ff02::2 ip6-allrouters
return true;
}
auto set_root_password(std::string_view password, std::string_view mountpoint) noexcept -> bool {
// TODO(vnepogodin): should encrypt password properly here
const auto& encrypted_passwd = utils::exec(fmt::format(FMT_COMPILE("openssl passwd {}"), password));
const auto& password_set_cmd = fmt::format(FMT_COMPILE("usermod -p '{}' root"), encrypted_passwd);
if (!utils::arch_chroot_checked(password_set_cmd, mountpoint)) {
spdlog::error("Failed to set password for root user");
return false;
}
return true;
}
} // namespace gucc::user

View File

@ -497,10 +497,9 @@ void set_root_password(const std::string_view& password) noexcept {
auto& config_data = config_instance->data();
const auto& mountpoint = std::get<std::string>(config_data["MOUNTPOINT"]);
std::error_code err{};
gucc::utils::exec(fmt::format(FMT_COMPILE("echo -e \"{0}\n{0}\" > /tmp/.passwd"), password));
gucc::utils::exec(fmt::format(FMT_COMPILE("arch-chroot {} passwd root < /tmp/.passwd &>/dev/null"), mountpoint));
fs::remove("/tmp/.passwd", err);
if (!gucc::user::set_root_password(password, mountpoint)) {
spdlog::error("Failed to set root password");
}
#else
spdlog::debug("root password := {}", password);
#endif