From 623a8c2d43b94476ec077bfcb0391edd6c08ad6b Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 3 Mar 2020 14:22:59 +0100 Subject: [PATCH] [removeuser] Port to C++ No changes in functionality; add a little description in the .conf file. --- src/modules/removeuser/CMakeLists.txt | 9 +++ src/modules/removeuser/RemoveUserJob.cpp | 74 ++++++++++++++++++++++++ src/modules/removeuser/RemoveUserJob.h | 49 ++++++++++++++++ src/modules/removeuser/main.py | 50 ---------------- src/modules/removeuser/module.desc | 6 -- src/modules/removeuser/removeuser.conf | 4 ++ 6 files changed, 136 insertions(+), 56 deletions(-) create mode 100644 src/modules/removeuser/CMakeLists.txt create mode 100644 src/modules/removeuser/RemoveUserJob.cpp create mode 100644 src/modules/removeuser/RemoveUserJob.h delete mode 100644 src/modules/removeuser/main.py delete mode 100644 src/modules/removeuser/module.desc diff --git a/src/modules/removeuser/CMakeLists.txt b/src/modules/removeuser/CMakeLists.txt new file mode 100644 index 000000000..55798feac --- /dev/null +++ b/src/modules/removeuser/CMakeLists.txt @@ -0,0 +1,9 @@ +calamares_add_plugin( removeuser + TYPE job + EXPORT_MACRO PLUGINDLLEXPORT_PRO + SOURCES + RemoveUserJob.cpp + LINK_PRIVATE_LIBRARIES + calamares + SHARED_LIB +) diff --git a/src/modules/removeuser/RemoveUserJob.cpp b/src/modules/removeuser/RemoveUserJob.cpp new file mode 100644 index 000000000..427d89729 --- /dev/null +++ b/src/modules/removeuser/RemoveUserJob.cpp @@ -0,0 +1,74 @@ +/* === This file is part of Calamares - === + * + * Copyright 2015, Teo Mrnjavac + * Copyright 2017. Alf Gaida + * Copyright 2019-2020, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "RemoveUserJob.h" + +#include "GlobalStorage.h" +#include "JobQueue.h" +#include "utils/CalamaresUtilsSystem.h" +#include "utils/Logger.h" +#include "utils/Variant.h" + +#include + +RemoveUserJob::RemoveUserJob( QObject* parent ) + : Calamares::CppJob( parent ) +{ +} + + +RemoveUserJob::~RemoveUserJob() {} + + +QString +RemoveUserJob::prettyName() const +{ + return tr( "Remove live user from target system" ); +} + +Calamares::JobResult +RemoveUserJob::exec() +{ + if ( m_username.isEmpty() ) + { + cWarning() << "Ignoring an empty username."; + return Calamares::JobResult::ok(); + } + + auto* s = CalamaresUtils::System::instance(); + auto r = s->targetEnvCommand( { QStringLiteral( "userdel" ), + QStringLiteral( "-f" ), // force + QStringLiteral( "-r" ), // remove home-dir and mail + m_username } ); + if ( r.getExitCode() != 0 ) + { + cWarning() << "Cannot remove user `" << m_username << "`. userdel terminated with exit code" << r.getExitCode(); + } + return Calamares::JobResult::ok(); +} + + +void +RemoveUserJob::setConfigurationMap( const QVariantMap& map ) +{ + m_username = CalamaresUtils::getString( map, "username" ); +} + +CALAMARES_PLUGIN_FACTORY_DEFINITION( RemoveUserJobFactory, registerPlugin< RemoveUserJob >(); ) diff --git a/src/modules/removeuser/RemoveUserJob.h b/src/modules/removeuser/RemoveUserJob.h new file mode 100644 index 000000000..d13e834f0 --- /dev/null +++ b/src/modules/removeuser/RemoveUserJob.h @@ -0,0 +1,49 @@ +/* === This file is part of Calamares - === + * + * Copyright 2020, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef REMOVEUSERJOB_H +#define REMOVEUSERJOB_H + +#include "CppJob.h" +#include "DllMacro.h" +#include "utils/PluginFactory.h" + +#include +#include + +class PLUGINDLLEXPORT RemoveUserJob : public Calamares::CppJob +{ + Q_OBJECT + +public: + explicit RemoveUserJob( QObject* parent = nullptr ); + virtual ~RemoveUserJob() override; + + QString prettyName() const override; + + Calamares::JobResult exec() override; + + void setConfigurationMap( const QVariantMap& configurationMap ) override; + +private: + QString m_username; +}; + +CALAMARES_PLUGIN_FACTORY_DECLARATION( RemoveUserJobFactory ) + +#endif // REMOVEUSERJOB_H diff --git a/src/modules/removeuser/main.py b/src/modules/removeuser/main.py deleted file mode 100644 index bd876edcd..000000000 --- a/src/modules/removeuser/main.py +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -# -# === This file is part of Calamares - === -# -# Copyright 2015, Teo Mrnjavac -# Copyright 2017. Alf Gaida -# Copyright 2019, Adriaan de Groot -# -# Calamares is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Calamares is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Calamares. If not, see . - -import subprocess -import libcalamares - -import gettext -_ = gettext.translation("calamares-python", - localedir=libcalamares.utils.gettext_path(), - languages=libcalamares.utils.gettext_languages(), - fallback=True).gettext - - -def pretty_name(): - return _("Remove live user from target system") - - -def run(): - """ - Remove live user from target system - """ - username = libcalamares.job.configuration["username"] - - try: - libcalamares.utils.check_target_env_call(["userdel", "-f", - "-r", username]) - except subprocess.CalledProcessError as e: - libcalamares.utils.debug("Cannot remove user. " - "'userdel' terminated with exit code " - "{}.".format(e.returncode)) - return None diff --git a/src/modules/removeuser/module.desc b/src/modules/removeuser/module.desc deleted file mode 100644 index 5c6fc6fb7..000000000 --- a/src/modules/removeuser/module.desc +++ /dev/null @@ -1,6 +0,0 @@ ---- -type: "job" -name: "removeuser" -interface: "python" -requires: [] -script: "main.py" diff --git a/src/modules/removeuser/removeuser.conf b/src/modules/removeuser/removeuser.conf index dab4b2526..d266e6952 100644 --- a/src/modules/removeuser/removeuser.conf +++ b/src/modules/removeuser/removeuser.conf @@ -1,6 +1,10 @@ # Removes a single user (with userdel) from the system. # This is typically used in OEM setups or if the live user # spills into the target system. +# +# The module never fails; if userdel fails, this is logged +# but the module still reports success and installation / setup +# continues as normal. --- # Username in the target system to be removed. username: live