From b5b307ee495c4b780a284f0ca53c6f66e1d5777c Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Sat, 29 Jun 2024 18:46:47 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20gucc:=20refactor=20setting=20use?= =?UTF-8?q?r=20password?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gucc/include/gucc/user.hpp | 3 +++ gucc/src/user.cpp | 27 ++++++++++++++------------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/gucc/include/gucc/user.hpp b/gucc/include/gucc/user.hpp index 43344bc..d86f0be 100644 --- a/gucc/include/gucc/user.hpp +++ b/gucc/include/gucc/user.hpp @@ -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& default_groups, std::string_view mountpoint) noexcept -> bool; diff --git a/gucc/src/user.cpp b/gucc/src/user.cpp index d1e993f..ef5d321 100644 --- a/gucc/src/user.cpp +++ b/gucc/src/user.cpp @@ -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& 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 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