2014-07-31 14:53:46 +02:00
|
|
|
/* === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
*
|
2016-07-15 17:48:29 +02:00
|
|
|
* Copyright 2014-2016, Teo Mrnjavac <teo@kde.org>
|
2014-07-31 14:53:46 +02:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <CreateUserJob.h>
|
|
|
|
|
|
|
|
#include "JobQueue.h"
|
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "utils/Logger.h"
|
|
|
|
#include "utils/CalamaresUtilsSystem.h"
|
|
|
|
|
2016-07-15 17:48:29 +02:00
|
|
|
#include <QDateTime>
|
2014-07-31 14:53:46 +02:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QTextStream>
|
|
|
|
|
|
|
|
|
2014-10-08 15:05:23 +02:00
|
|
|
CreateUserJob::CreateUserJob( const QString& userName,
|
|
|
|
const QString& fullName,
|
|
|
|
bool autologin,
|
|
|
|
const QStringList& defaultGroups )
|
2014-07-31 14:53:46 +02:00
|
|
|
: Calamares::Job()
|
|
|
|
, m_userName( userName )
|
|
|
|
, m_fullName( fullName )
|
|
|
|
, m_autologin( autologin )
|
2014-10-08 15:05:23 +02:00
|
|
|
, m_defaultGroups( defaultGroups )
|
2014-07-31 14:53:46 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
CreateUserJob::prettyName() const
|
|
|
|
{
|
|
|
|
return tr( "Create user %1" ).arg( m_userName );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-13 02:22:03 +02:00
|
|
|
QString
|
|
|
|
CreateUserJob::prettyDescription() const
|
|
|
|
{
|
|
|
|
return tr( "Create user <strong>%1</strong>." ).arg( m_userName );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
CreateUserJob::prettyStatusMessage() const
|
|
|
|
{
|
|
|
|
return tr( "Creating user %1." ).arg( m_userName );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-31 14:53:46 +02:00
|
|
|
Calamares::JobResult
|
|
|
|
CreateUserJob::exec()
|
|
|
|
{
|
|
|
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
|
|
|
QDir destDir( gs->value( "rootMountPoint" ).toString() );
|
|
|
|
|
2014-10-08 15:35:27 +02:00
|
|
|
if ( gs->contains( "sudoersGroup" ) &&
|
|
|
|
!gs->value( "sudoersGroup" ).toString().isEmpty() )
|
|
|
|
{
|
|
|
|
QFileInfo sudoersFi( destDir.absoluteFilePath( "etc/sudoers.d/10-installer" ) );
|
2014-07-31 14:53:46 +02:00
|
|
|
|
2014-10-08 15:35:27 +02:00
|
|
|
if ( !sudoersFi.absoluteDir().exists() )
|
|
|
|
return Calamares::JobResult::error( tr( "Sudoers dir is not writable." ) );
|
2014-07-31 14:53:46 +02:00
|
|
|
|
2014-10-08 15:35:27 +02:00
|
|
|
QFile sudoersFile( sudoersFi.absoluteFilePath() );
|
|
|
|
if (!sudoersFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
|
|
|
|
return Calamares::JobResult::error( tr( "Cannot create sudoers file for writing." ) );
|
2014-07-31 14:53:46 +02:00
|
|
|
|
2014-10-08 15:35:27 +02:00
|
|
|
QString sudoersGroup = gs->value( "sudoersGroup" ).toString();
|
|
|
|
|
|
|
|
QTextStream sudoersOut( &sudoersFile );
|
|
|
|
sudoersOut << QString( "%%1 ALL=(ALL) ALL\n" ).arg( sudoersGroup );
|
|
|
|
|
|
|
|
if ( QProcess::execute( "chmod", { "440", sudoersFi.absoluteFilePath() } ) )
|
|
|
|
return Calamares::JobResult::error( tr( "Cannot chmod sudoers file." ) );
|
|
|
|
}
|
2014-07-31 14:53:46 +02:00
|
|
|
|
2014-10-08 15:05:23 +02:00
|
|
|
QFileInfo groupsFi( destDir.absoluteFilePath( "etc/group" ) );
|
|
|
|
QFile groupsFile( groupsFi.absoluteFilePath() );
|
|
|
|
if ( !groupsFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
|
|
|
|
return Calamares::JobResult::error( tr( "Cannot open groups file for reading." ) );
|
|
|
|
QString groupsData = QString::fromLocal8Bit( groupsFile.readAll() );
|
|
|
|
QStringList groupsLines = groupsData.split( '\n' );
|
|
|
|
for ( QStringList::iterator it = groupsLines.begin();
|
|
|
|
it != groupsLines.end(); ++it )
|
|
|
|
{
|
|
|
|
int indexOfFirstToDrop = it->indexOf( ':' );
|
|
|
|
it->truncate( indexOfFirstToDrop );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( const QString& group, m_defaultGroups )
|
|
|
|
if ( !groupsLines.contains( group ) )
|
2015-08-06 19:10:04 +02:00
|
|
|
CalamaresUtils::System::instance()->
|
|
|
|
targetEnvCall( { "groupadd", group } );
|
2014-07-31 14:53:46 +02:00
|
|
|
|
2014-10-08 15:05:23 +02:00
|
|
|
QString defaultGroups = m_defaultGroups.join( ',' );
|
2014-07-31 14:53:46 +02:00
|
|
|
if ( m_autologin )
|
|
|
|
{
|
2014-10-08 15:05:23 +02:00
|
|
|
QString autologinGroup;
|
|
|
|
if ( gs->contains( "autologinGroup" ) &&
|
|
|
|
!gs->value( "autologinGroup" ).toString().isEmpty() )
|
|
|
|
autologinGroup = gs->value( "autologinGroup" ).toString();
|
|
|
|
else
|
|
|
|
autologinGroup = QStringLiteral( "autologin" );
|
|
|
|
|
2015-08-06 19:10:04 +02:00
|
|
|
CalamaresUtils::System::instance()->targetEnvCall( { "groupadd", autologinGroup } );
|
2014-10-08 15:05:23 +02:00
|
|
|
defaultGroups.append( QString( ",%1" ).arg( autologinGroup ) );
|
2014-07-31 14:53:46 +02:00
|
|
|
}
|
|
|
|
|
2016-07-15 17:48:29 +02:00
|
|
|
// If we're looking to reuse the contents of an existing /home
|
|
|
|
if ( gs->value( "reuseHome" ).toBool() )
|
|
|
|
{
|
|
|
|
QString shellFriendlyHome = "/home/" + m_userName;
|
|
|
|
QDir existingHome( destDir.absolutePath() + shellFriendlyHome );
|
|
|
|
if ( existingHome.exists() )
|
|
|
|
{
|
|
|
|
QString backupDirName = "dotfiles_backup_" +
|
|
|
|
QDateTime::currentDateTime()
|
|
|
|
.toString( "yyyy-MM-dd_HH-mm-ss" );
|
|
|
|
existingHome.mkdir( backupDirName );
|
|
|
|
|
|
|
|
CalamaresUtils::System::instance()->
|
|
|
|
targetEnvCall( { "sh",
|
|
|
|
"-c",
|
|
|
|
"mv -f " +
|
|
|
|
shellFriendlyHome + "/.* " +
|
|
|
|
shellFriendlyHome + "/" +
|
|
|
|
backupDirName
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 19:10:04 +02:00
|
|
|
int ec = CalamaresUtils::System::instance()->
|
|
|
|
targetEnvCall( { "useradd",
|
|
|
|
"-m",
|
|
|
|
"-s",
|
|
|
|
"/bin/bash",
|
2015-10-16 16:45:34 +02:00
|
|
|
"-U",
|
2015-08-06 19:10:04 +02:00
|
|
|
"-G",
|
|
|
|
defaultGroups,
|
2016-09-16 07:55:04 +02:00
|
|
|
"-c",
|
|
|
|
m_fullName,
|
2015-08-06 19:10:04 +02:00
|
|
|
m_userName } );
|
2014-07-31 14:53:46 +02:00
|
|
|
if ( ec )
|
|
|
|
return Calamares::JobResult::error( tr( "Cannot create user %1." )
|
|
|
|
.arg( m_userName ),
|
|
|
|
tr( "useradd terminated with error code %1." )
|
|
|
|
.arg( ec ) );
|
|
|
|
|
2015-08-06 19:10:04 +02:00
|
|
|
ec = CalamaresUtils::System::instance()->
|
|
|
|
targetEnvCall( { "chown",
|
2014-07-31 14:53:46 +02:00
|
|
|
"-R",
|
2014-10-08 15:05:23 +02:00
|
|
|
QString( "%1:%2" ).arg( m_userName )
|
2015-11-17 21:55:50 +01:00
|
|
|
.arg( m_userName ),
|
2014-07-31 14:53:46 +02:00
|
|
|
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();
|
|
|
|
}
|