🧹 gucc: refactor setting user password

This commit is contained in:
Vladislav Nepogodin 2024-06-29 18:46:47 +04:00
parent 606fc3e6af
commit b5b307ee49
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
2 changed files with 17 additions and 13 deletions

View File

@ -17,6 +17,9 @@ struct UserInfo final {
// Create group on the system
auto create_group(std::string_view group, std::string_view mountpoint) noexcept -> bool;
// Set user password on the system
auto set_user_password(std::string_view username, std::string_view password, std::string_view mountpoint) noexcept -> bool;
// Create user on the system
auto create_new_user(const user::UserInfo& user_info, const std::vector<std::string>& default_groups, std::string_view mountpoint) noexcept -> bool;

View File

@ -42,6 +42,17 @@ auto create_group(std::string_view group, std::string_view mountpoint) noexcept
return utils::arch_chroot_checked(cmd, mountpoint);
}
auto set_user_password(std::string_view username, std::string_view password, std::string_view mountpoint) noexcept -> bool {
// TODO(vnepogodin): should encrypt user 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 '{}' {}"), encrypted_passwd, username);
if (!utils::arch_chroot_checked(password_set_cmd, mountpoint)) {
spdlog::error("Failed to set password for user {}", username);
return false;
}
return true;
}
auto create_new_user(const user::UserInfo& user_info, const std::vector<std::string>& default_groups, std::string_view mountpoint) noexcept -> bool {
if (!user_info.sudoers_group.empty() && !ranges::contains(default_groups, user_info.sudoers_group)) {
spdlog::error("Failed to create user {}! User default groups doesn't contain sudoers group({})", user_info.username, user_info.sudoers_group);
@ -90,11 +101,8 @@ auto create_new_user(const user::UserInfo& user_info, const std::vector<std::str
return false;
}
// TODO(vnepogodin): should encrypt user password properly here
const auto& encrypted_passwd = utils::exec(fmt::format(FMT_COMPILE("openssl passwd {}"), user_info.password));
const auto& password_set_cmd = fmt::format(FMT_COMPILE("usermod -p '{}' {}"), encrypted_passwd, user_info.username);
if (!utils::arch_chroot_checked(password_set_cmd, mountpoint)) {
spdlog::error("Failed to set password for user {}", user_info.username);
// Set user password
if (!set_user_password(user_info.username, user_info.password, mountpoint)) {
return false;
}
@ -170,14 +178,7 @@ ff02::2 ip6-allrouters
}
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;
return set_user_password("root"sv, password, mountpoint);
}
} // namespace gucc::user