From 7c75e4600db4947583e8268124adb5a50b1339eb Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Thu, 4 Jul 2024 11:34:25 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20move=20autologin=20into=20gucc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gucc/CMakeLists.txt | 1 + gucc/include/gucc/autologin.hpp | 13 +++++++++ gucc/meson.build | 1 + gucc/src/autologin.cpp | 48 +++++++++++++++++++++++++++++++++ src/utils.cpp | 25 +++++------------ src/utils.hpp | 2 +- 6 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 gucc/include/gucc/autologin.hpp create mode 100644 gucc/src/autologin.cpp diff --git a/gucc/CMakeLists.txt b/gucc/CMakeLists.txt index b031781..1114d14 100644 --- a/gucc/CMakeLists.txt +++ b/gucc/CMakeLists.txt @@ -26,6 +26,7 @@ add_library(${PROJECT_NAME} SHARED src/crypttab.cpp include/gucc/crypttab.hpp src/bootloader.cpp include/gucc/bootloader.hpp src/systemd_services.cpp include/gucc/systemd_services.hpp + src/autologin.cpp include/gucc/autologin.hpp #src/chwd_profiles.cpp src/chwd_profiles.hpp #src/disk.cpp src/disk.hpp ) diff --git a/gucc/include/gucc/autologin.hpp b/gucc/include/gucc/autologin.hpp new file mode 100644 index 0000000..f6dbdc1 --- /dev/null +++ b/gucc/include/gucc/autologin.hpp @@ -0,0 +1,13 @@ +#ifndef AUTOLOGIN_HPP +#define AUTOLOGIN_HPP + +#include // for string_view + +namespace gucc::user { + +// Enables autologin for user +auto enable_autologin(std::string_view displaymanager, std::string_view username, std::string_view root_mountpoint) noexcept -> bool; + +} // namespace gucc::user + +#endif // AUTOLOGIN_HPP diff --git a/gucc/meson.build b/gucc/meson.build index 0e1cf8d..6a7f7ec 100644 --- a/gucc/meson.build +++ b/gucc/meson.build @@ -16,6 +16,7 @@ gucc_lib = library('gucc', 'src/crypttab.cpp', 'src/bootloader.cpp', 'src/systemd_services.cpp', + 'src/autologin.cpp', ], include_directories : [include_directories('include')], dependencies: deps diff --git a/gucc/src/autologin.cpp b/gucc/src/autologin.cpp new file mode 100644 index 0000000..b50ffa0 --- /dev/null +++ b/gucc/src/autologin.cpp @@ -0,0 +1,48 @@ +#include "gucc/autologin.hpp" +#include "gucc/io_utils.hpp" +#include "gucc/user.hpp" + +#include +#include + +#include + +using namespace std::string_view_literals; + +namespace gucc::user { + +auto enable_autologin(std::string_view displaymanager, std::string_view username, std::string_view root_mountpoint) noexcept -> bool { + // TODO(vnepogodin): check for failed commands + + // enable autologin + if (displaymanager == "gdm"sv) { + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^AutomaticLogin=*/AutomaticLogin={}/g' {}/etc/gdm/custom.conf"), username, root_mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^AutomaticLoginEnable=*/AutomaticLoginEnable=true/g' {}/etc/gdm/custom.conf"), root_mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^TimedLoginEnable=*/TimedLoginEnable=true/g' {}/etc/gdm/custom.conf"), root_mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^TimedLogin=*/TimedLoginEnable={}/g' {}/etc/gdm/custom.conf"), username, root_mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^TimedLoginDelay=*/TimedLoginDelay=0/g' {}/etc/gdm/custom.conf"), root_mountpoint)); + } else if (displaymanager == "lightdm"sv) { + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^#autologin-user=/autologin-user={}/' {}/etc/lightdm/lightdm.conf"), username, root_mountpoint)); + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^#autologin-user-timeout=0/autologin-user-timeout=0/' {}/etc/lightdm/lightdm.conf"), root_mountpoint)); + + // create autologin group + if (!user::create_group("autologin"sv, root_mountpoint, true)) { + spdlog::error("Failed to create autologin group"); + return false; + } + // add group to user + const auto& groupadd_cmd = fmt::format(FMT_COMPILE("gpasswd -a {} autologin"), username); + if (!utils::arch_chroot_checked(groupadd_cmd, root_mountpoint)) { + spdlog::error("Failed to add autologin group to user: {}", username); + return false; + } + } else if (displaymanager == "sddm"sv) { + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^User=/User={}/g' {}/etc/sddm.conf"), username, root_mountpoint)); + } else if (displaymanager == "lxdm"sv) { + utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^# autologin=dgod/autologin={}/g' {}/etc/lxdm/lxdm.conf"), username, root_mountpoint)); + } + + return true; +} + +} // namespace gucc::user diff --git a/src/utils.cpp b/src/utils.cpp index 0751eb8..44d9483 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -6,6 +6,7 @@ #include "widgets.hpp" // import gucc +#include "gucc/autologin.hpp" #include "gucc/bootloader.hpp" #include "gucc/cpu.hpp" #include "gucc/file_utils.hpp" @@ -1746,26 +1747,14 @@ void set_keymap() noexcept { gucc::utils::exec(fmt::format(FMT_COMPILE("loadkeys {}"), keymap)); } -void enable_autologin([[maybe_unused]] const std::string_view& dm, [[maybe_unused]] const std::string_view& user) noexcept { +void enable_autologin([[maybe_unused]] const std::string_view& dm, [[maybe_unused]] const std::string_view& username) noexcept { #ifdef NDEVENV - // enable autologin - if (dm == "gdm") { - gucc::utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^AutomaticLogin=*/AutomaticLogin={}/g' /mnt/etc/gdm/custom.conf"), user)); - gucc::utils::exec("sed -i 's/^AutomaticLoginEnable=*/AutomaticLoginEnable=true/g' /mnt/etc/gdm/custom.conf"); - gucc::utils::exec("sed -i 's/^TimedLoginEnable=*/TimedLoginEnable=true/g' /mnt/etc/gdm/custom.conf"); - gucc::utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^TimedLogin=*/TimedLoginEnable={}/g' /mnt/etc/gdm/custom.conf"), user)); - gucc::utils::exec("sed -i 's/^TimedLoginDelay=*/TimedLoginDelay=0/g' /mnt/etc/gdm/custom.conf"); - } else if (dm == "lightdm") { - gucc::utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^#autologin-user=/autologin-user={}/' /mnt/etc/lightdm/lightdm.conf"), user)); - gucc::utils::exec("sed -i 's/^#autologin-user-timeout=0/autologin-user-timeout=0/' /mnt/etc/lightdm/lightdm.conf"); + auto* config_instance = Config::instance(); + auto& config_data = config_instance->data(); + const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); - // TODO(vnepogodin): refactor with gucc - utils::arch_chroot("groupadd -r autologin", false); - utils::arch_chroot(fmt::format(FMT_COMPILE("gpasswd -a {} autologin"), user), false); - } else if (dm == "sddm") { - gucc::utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^User=/User={}/g' /mnt/etc/sddm.conf"), user)); - } else if (dm == "lxdm") { - gucc::utils::exec(fmt::format(FMT_COMPILE("sed -i 's/^# autologin=dgod/autologin={}/g' /mnt/etc/lxdm/lxdm.conf"), user)); + if (!gucc::user::enable_autologin(dm, username, mountpoint)) { + spdlog::error("Failed to enable autologin"); } #endif } diff --git a/src/utils.hpp b/src/utils.hpp index 090d17b..f268b58 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -65,7 +65,7 @@ void id_system() noexcept; void show_iwctl() noexcept; void set_keymap() noexcept; -void enable_autologin(const std::string_view& dm, const std::string_view& user) noexcept; +void enable_autologin(const std::string_view& dm, const std::string_view& username) noexcept; bool parse_config() noexcept; void setup_luks_keyfile() noexcept; void grub_mkconfig() noexcept;