From 46f5dab7fd7d1cb530c938d13ce9d401ba22f509 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Thu, 31 Jul 2014 14:53:46 +0200 Subject: [PATCH] CreateUserJob for Users viewmodule --- src/modules/users/CMakeLists.txt | 2 +- src/modules/users/CreateUserJob.cpp | 110 ++++++++++++++++++++++++++++ src/modules/users/CreateUserJob.h | 41 +++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 src/modules/users/CreateUserJob.cpp create mode 100644 src/modules/users/CreateUserJob.h diff --git a/src/modules/users/CMakeLists.txt b/src/modules/users/CMakeLists.txt index 7930d25d2..b057244f1 100644 --- a/src/modules/users/CMakeLists.txt +++ b/src/modules/users/CMakeLists.txt @@ -4,7 +4,7 @@ calamares_add_plugin( users EXPORT_MACRO PLUGINDLLEXPORT_PRO CONFIG_FILE module.conf SOURCES -# CreateUserJob.cpp + CreateUserJob.cpp UsersViewStep.cpp UsersPage.cpp UI diff --git a/src/modules/users/CreateUserJob.cpp b/src/modules/users/CreateUserJob.cpp new file mode 100644 index 000000000..21ad978f1 --- /dev/null +++ b/src/modules/users/CreateUserJob.cpp @@ -0,0 +1,110 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014, Teo Mrnjavac + * + * 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 + +#include "JobQueue.h" +#include "GlobalStorage.h" +#include "utils/Logger.h" +#include "utils/CalamaresUtilsSystem.h" + +#include +#include +#include +#include +#include + + +CreateUserJob::CreateUserJob( const QString& userName, const QString& fullName, bool autologin ) + : Calamares::Job() + , m_userName( userName ) + , m_fullName( fullName ) + , m_autologin( autologin ) +{ +} + + +QString +CreateUserJob::prettyName() const +{ + return tr( "Create user %1" ).arg( m_userName ); +} + + +Calamares::JobResult +CreateUserJob::exec() +{ + Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage(); + QDir destDir( gs->value( "rootMountPoint" ).toString() ); + QFileInfo sudoersFi( destDir.absoluteFilePath( "etc/sudoers.d/10-installer" ) ); + + if ( !sudoersFi.exists() || !sudoersFi.isWritable() ) + return Calamares::JobResult::error( tr( "Sudoers file is not writable." ) ); + + QFile sudoersFile( sudoersFi.absoluteFilePath() ); + if (!sudoersFile.open( QIODevice::Append | QIODevice::Text ) ) + return Calamares::JobResult::error( tr( "Cannot open sudoers file for writing." ) ); + + QTextStream sudoersOut( &sudoersFile ); + sudoersOut << QString( "%1 ALL=(ALL) ALL\n" ).arg( m_userName ); + + if ( QProcess::execute( "chmod", { "440", sudoersFi.absoluteFilePath() } ) ) + return Calamares::JobResult::error( tr( "Cannot chmod sudoers file." ) ); + + + QString defaultGroups( "lp,video,network,storage,wheel,audio" ); + if ( m_autologin ) + { + CalamaresUtils::chrootCall( { "groupadd", "autologin" } ); + defaultGroups.append( ",autologin" ); + } + + int ec = CalamaresUtils::chrootCall( { "useradd", + "-m", + "-s", + "/bin/bash", + "-g", + "users", + "-G", + defaultGroups, + m_userName } ); + if ( ec ) + return Calamares::JobResult::error( tr( "Cannot create user %1." ) + .arg( m_userName ), + tr( "useradd terminated with error code %1." ) + .arg( ec ) ); + + ec = CalamaresUtils::chrootCall( { "chfn", "-f", m_fullName, m_userName } ); + if ( ec ) + return Calamares::JobResult::error( tr( "Cannot set full name for user %1." ) + .arg( m_userName ), + tr( "chfn terminated with error code %1." ) + .arg( ec ) ); + + ec = CalamaresUtils::chrootCall( { "chown", + "-R", + QString( "%1:users" ).arg( m_userName ), + QString( "/home/%1" ).arg( m_userName ) } ); + if ( ec ) + return Calamares::JobResult::error( tr( "Cannot set home directory ownership for user %1." ) + .arg( m_userName ), + tr( "chown terminated with error code %1." ) + .arg( ec ) ); + + return Calamares::JobResult::ok(); +} diff --git a/src/modules/users/CreateUserJob.h b/src/modules/users/CreateUserJob.h new file mode 100644 index 000000000..25047c9c6 --- /dev/null +++ b/src/modules/users/CreateUserJob.h @@ -0,0 +1,41 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014, Teo Mrnjavac + * + * 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 CREATEUSERJOB_H +#define CREATEUSERJOB_H + +#include + + +class CreateUserJob : public Calamares::Job +{ + Q_OBJECT +public: + CreateUserJob( const QString& userName, + const QString& fullName, + bool autologin ); + QString prettyName() const override; + Calamares::JobResult exec() override; + +private: + QString m_userName; + QString m_fullName; + bool m_autologin; +}; + +#endif /* CREATEUSERJOB_H */