2020-07-25 12:47:01 +02:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* SPDX-FileCopyrightText: 2020 Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
* License-Filename: LICENSE
|
|
|
|
*
|
|
|
|
* 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 "Config.h"
|
|
|
|
|
2020-07-25 15:39:19 +02:00
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "JobQueue.h"
|
|
|
|
#include "utils/Logger.h"
|
2020-07-27 15:34:59 +02:00
|
|
|
#include "utils/String.h"
|
2020-07-25 15:39:19 +02:00
|
|
|
#include "utils/Variant.h"
|
|
|
|
|
2020-07-27 15:54:52 +02:00
|
|
|
#include <QFile>
|
2020-07-27 15:34:59 +02:00
|
|
|
#include <QRegExp>
|
2020-07-28 10:21:23 +02:00
|
|
|
#include <QRegExpValidator>
|
|
|
|
|
|
|
|
static const QRegExp USERNAME_RX( "^[a-z_][a-z0-9_-]*[$]?$" );
|
|
|
|
static constexpr const int USERNAME_MAX_LENGTH = 31;
|
2020-07-27 15:34:59 +02:00
|
|
|
|
2020-07-28 10:45:38 +02:00
|
|
|
static const QRegExp HOSTNAME_RX( "^[a-zA-Z0-9][-a-zA-Z0-9_]*$" );
|
|
|
|
static constexpr const int HOSTNAME_MIN_LENGTH = 2;
|
|
|
|
static constexpr const int HOSTNAME_MAX_LENGTH = 63;
|
|
|
|
|
2020-07-25 12:47:01 +02:00
|
|
|
Config::Config( QObject* parent )
|
|
|
|
: QObject( parent )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Config::~Config() {}
|
|
|
|
|
|
|
|
void
|
2020-07-25 15:39:19 +02:00
|
|
|
Config::setUserShell( const QString& shell )
|
|
|
|
{
|
|
|
|
if ( !shell.isEmpty() && !shell.startsWith( '/' ) )
|
|
|
|
{
|
|
|
|
cWarning() << "User shell" << shell << "is not an absolute path.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The shell is put into GS because the CreateUser job expects it there
|
|
|
|
Calamares::JobQueue::instance()->globalStorage()->insert( "userShell", shell );
|
|
|
|
}
|
|
|
|
|
2020-07-25 16:39:13 +02:00
|
|
|
static inline void
|
2020-07-28 10:21:23 +02:00
|
|
|
insertInGlobalStorage( const QString& key, const QString& group )
|
2020-07-25 16:39:13 +02:00
|
|
|
{
|
|
|
|
auto* gs = Calamares::JobQueue::instance()->globalStorage();
|
|
|
|
if ( !gs || group.isEmpty() )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gs->insert( key, group );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Config::setAutologinGroup( const QString& group )
|
|
|
|
{
|
2020-07-28 10:21:23 +02:00
|
|
|
insertInGlobalStorage( QStringLiteral( "autologinGroup" ), group );
|
2020-07-25 16:39:13 +02:00
|
|
|
emit autologinGroupChanged( group );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Config::setSudoersGroup( const QString& group )
|
|
|
|
{
|
2020-07-28 10:21:23 +02:00
|
|
|
insertInGlobalStorage( QStringLiteral( "sudoersGroup" ), group );
|
2020-07-25 16:39:13 +02:00
|
|
|
emit sudoersGroupChanged( group );
|
|
|
|
}
|
|
|
|
|
2020-07-25 15:39:19 +02:00
|
|
|
|
2020-07-25 16:54:15 +02:00
|
|
|
void
|
|
|
|
Config::setLoginName( const QString& login )
|
|
|
|
{
|
|
|
|
if ( login != m_loginName )
|
|
|
|
{
|
2020-07-27 15:34:59 +02:00
|
|
|
m_customLoginName = !login.isEmpty();
|
2020-07-25 16:54:15 +02:00
|
|
|
m_loginName = login;
|
|
|
|
emit loginNameChanged( login );
|
2020-07-28 10:21:23 +02:00
|
|
|
emit loginNameStatusChanged( loginNameStatus() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const QStringList&
|
|
|
|
Config::forbiddenLoginNames()
|
|
|
|
{
|
|
|
|
static QStringList forbidden { "root" };
|
|
|
|
return forbidden;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
Config::loginNameStatus() const
|
|
|
|
{
|
|
|
|
// An empty login is "ok", even if it isn't really
|
|
|
|
if ( m_loginName.isEmpty() )
|
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_loginName.length() > USERNAME_MAX_LENGTH )
|
|
|
|
{
|
|
|
|
return tr( "Your username is too long." );
|
|
|
|
}
|
2020-07-28 10:45:38 +02:00
|
|
|
for ( const QString& badName : forbiddenLoginNames() )
|
|
|
|
{
|
|
|
|
if ( 0 == QString::compare( badName, m_loginName, Qt::CaseSensitive ) )
|
|
|
|
{
|
|
|
|
return tr( "'%1' is not allowed as username." ).arg( badName );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:21:23 +02:00
|
|
|
QString login( m_loginName ); // make a copy because validate() doesn't take const&
|
2020-07-28 10:45:38 +02:00
|
|
|
QRegExpValidator validateEntireLoginName( USERNAME_RX );
|
|
|
|
QRegExpValidator validateFirstLetter( QRegExp( "[a-z_].*" ) ); // anchors are implicit in QRegExpValidator
|
|
|
|
int pos = -1;
|
2020-07-28 10:21:23 +02:00
|
|
|
if ( validateFirstLetter.validate( login, pos ) == QValidator::Invalid )
|
|
|
|
{
|
|
|
|
return tr( "Your username must start with a lowercase letter or underscore." );
|
|
|
|
}
|
|
|
|
if ( validateEntireLoginName.validate( login, pos ) == QValidator::Invalid )
|
|
|
|
{
|
|
|
|
return tr( "Only lowercase letters, numbers, underscore and hyphen are allowed." );
|
2020-07-25 16:54:15 +02:00
|
|
|
}
|
2020-07-28 10:21:23 +02:00
|
|
|
|
|
|
|
return QString();
|
2020-07-25 16:54:15 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 15:54:52 +02:00
|
|
|
void
|
|
|
|
Config::setHostName( const QString& host )
|
|
|
|
{
|
|
|
|
if ( host != m_hostName )
|
|
|
|
{
|
|
|
|
m_customHostName = !host.isEmpty();
|
|
|
|
m_hostName = host;
|
|
|
|
emit hostNameChanged( host );
|
2020-07-28 10:45:38 +02:00
|
|
|
emit hostNameStatusChanged( hostNameStatus() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const QStringList&
|
|
|
|
Config::forbiddenHostNames()
|
|
|
|
{
|
|
|
|
static QStringList forbidden { "localhost" };
|
|
|
|
return forbidden;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
Config::hostNameStatus() const
|
|
|
|
{
|
|
|
|
// An empty hostname is "ok", even if it isn't really
|
|
|
|
if ( m_hostName.isEmpty() )
|
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_hostName.length() < HOSTNAME_MIN_LENGTH )
|
|
|
|
{
|
|
|
|
return tr( "Your hostname is too short." );
|
2020-07-27 15:54:52 +02:00
|
|
|
}
|
2020-07-28 10:45:38 +02:00
|
|
|
if ( m_hostName.length() > HOSTNAME_MAX_LENGTH )
|
|
|
|
{
|
|
|
|
return tr( "Your hostname is too long." );
|
|
|
|
}
|
|
|
|
for ( const QString& badName : forbiddenHostNames() )
|
|
|
|
{
|
|
|
|
if ( 0 == QString::compare( badName, m_hostName, Qt::CaseSensitive ) )
|
|
|
|
{
|
|
|
|
return tr( "'%1' is not allowed as hostname." ).arg( badName );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString text = m_hostName;
|
|
|
|
QRegExpValidator val( HOSTNAME_RX );
|
|
|
|
int pos = -1;
|
|
|
|
|
|
|
|
if ( val.validate( text, pos ) == QValidator::Invalid )
|
|
|
|
{
|
|
|
|
return tr( "Only letters, numbers, underscore and hyphen are allowed." );
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString();
|
2020-07-27 15:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief Guess the machine's name
|
|
|
|
*
|
|
|
|
* If there is DMI data, use that; otherwise, just call the machine "-pc".
|
|
|
|
* Reads the DMI data just once.
|
|
|
|
*/
|
|
|
|
static QString
|
|
|
|
guessProductName()
|
|
|
|
{
|
|
|
|
static bool tried = false;
|
|
|
|
static QString dmiProduct;
|
|
|
|
|
|
|
|
if ( !tried )
|
|
|
|
{
|
|
|
|
// yes validateHostnameText() but these files can be a mess
|
|
|
|
QRegExp dmirx( "[^a-zA-Z0-9]", Qt::CaseInsensitive );
|
|
|
|
QFile dmiFile( QStringLiteral( "/sys/devices/virtual/dmi/id/product_name" ) );
|
|
|
|
|
|
|
|
if ( dmiFile.exists() && dmiFile.open( QIODevice::ReadOnly ) )
|
|
|
|
{
|
|
|
|
dmiProduct = QString::fromLocal8Bit( dmiFile.readAll().simplified().data() )
|
|
|
|
.toLower()
|
|
|
|
.replace( dmirx, " " )
|
|
|
|
.remove( ' ' );
|
|
|
|
}
|
|
|
|
if ( dmiProduct.isEmpty() )
|
|
|
|
{
|
2020-07-28 10:49:12 +02:00
|
|
|
dmiProduct = QStringLiteral( "pc" );
|
2020-07-27 15:54:52 +02:00
|
|
|
}
|
|
|
|
tried = true;
|
|
|
|
}
|
|
|
|
return dmiProduct;
|
|
|
|
}
|
2020-07-27 15:34:59 +02:00
|
|
|
|
|
|
|
static QString
|
|
|
|
makeLoginNameSuggestion( const QStringList& parts )
|
|
|
|
{
|
2020-07-27 15:54:52 +02:00
|
|
|
if ( parts.isEmpty() || parts.first().isEmpty() )
|
2020-07-27 15:34:59 +02:00
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString usernameSuggestion = parts.first();
|
|
|
|
for ( int i = 1; i < parts.length(); ++i )
|
|
|
|
{
|
|
|
|
if ( !parts.value( i ).isEmpty() )
|
|
|
|
{
|
|
|
|
usernameSuggestion.append( parts.value( i ).at( 0 ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return USERNAME_RX.indexIn( usernameSuggestion ) != -1 ? usernameSuggestion : QString();
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:54:52 +02:00
|
|
|
static QString
|
|
|
|
makeHostnameSuggestion( const QStringList& parts )
|
|
|
|
{
|
|
|
|
static const QRegExp HOSTNAME_RX( "^[a-zA-Z0-9][-a-zA-Z0-9_]*$" );
|
|
|
|
if ( parts.isEmpty() || parts.first().isEmpty() )
|
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString productName = guessProductName();
|
|
|
|
QString hostnameSuggestion = QStringLiteral( "%1-%2" ).arg( parts.first() ).arg( productName );
|
|
|
|
return HOSTNAME_RX.indexIn( hostnameSuggestion ) != -1 ? hostnameSuggestion : QString();
|
|
|
|
}
|
|
|
|
|
2020-07-25 16:54:15 +02:00
|
|
|
void
|
2020-07-27 17:52:46 +02:00
|
|
|
Config::setFullName( const QString& name )
|
2020-07-25 16:54:15 +02:00
|
|
|
{
|
2020-07-27 17:52:46 +02:00
|
|
|
if ( name.isEmpty() && !m_fullName.isEmpty() )
|
|
|
|
{
|
|
|
|
if ( !m_customHostName )
|
|
|
|
{
|
|
|
|
setHostName( name );
|
|
|
|
}
|
|
|
|
if ( !m_customLoginName )
|
|
|
|
{
|
|
|
|
setLoginName( name );
|
|
|
|
}
|
|
|
|
m_fullName = name;
|
|
|
|
emit fullNameChanged( name );
|
|
|
|
}
|
|
|
|
|
2020-07-25 16:54:15 +02:00
|
|
|
if ( name != m_fullName )
|
|
|
|
{
|
|
|
|
m_fullName = name;
|
2020-07-27 17:52:46 +02:00
|
|
|
emit fullNameChanged( name );
|
2020-07-27 15:34:59 +02:00
|
|
|
|
|
|
|
// Build login and hostname, if needed
|
|
|
|
QRegExp rx( "[^a-zA-Z0-9 ]", Qt::CaseInsensitive );
|
|
|
|
QString cleanName = CalamaresUtils::removeDiacritics( name ).toLower().replace( rx, " " ).simplified();
|
|
|
|
QStringList cleanParts = cleanName.split( ' ' );
|
|
|
|
|
|
|
|
if ( !m_customLoginName )
|
|
|
|
{
|
|
|
|
QString login = makeLoginNameSuggestion( cleanParts );
|
|
|
|
if ( !login.isEmpty() && login != m_loginName )
|
|
|
|
{
|
|
|
|
m_loginName = login;
|
|
|
|
emit loginNameChanged( login );
|
2020-07-28 10:21:23 +02:00
|
|
|
emit loginNameStatusChanged( loginNameStatus() );
|
2020-07-27 15:34:59 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-27 15:54:52 +02:00
|
|
|
if ( !m_customHostName )
|
|
|
|
{
|
|
|
|
QString hostname = makeHostnameSuggestion( cleanParts );
|
|
|
|
if ( !hostname.isEmpty() && hostname != m_hostName )
|
|
|
|
{
|
|
|
|
m_hostName = hostname;
|
|
|
|
emit hostNameChanged( hostname );
|
2020-07-28 10:45:38 +02:00
|
|
|
emit hostNameStatusChanged( hostNameStatus() );
|
2020-07-27 15:54:52 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-25 16:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:41:52 +02:00
|
|
|
void
|
|
|
|
Config::setAutoLogin( bool b )
|
|
|
|
{
|
|
|
|
if ( b != m_doAutoLogin )
|
|
|
|
{
|
|
|
|
m_doAutoLogin = b;
|
|
|
|
emit autoLoginChanged( b );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 15:39:19 +02:00
|
|
|
void
|
|
|
|
Config::setConfigurationMap( const QVariantMap& configurationMap )
|
2020-07-25 12:47:01 +02:00
|
|
|
{
|
2020-07-25 15:39:19 +02:00
|
|
|
QString shell( QLatin1String( "/bin/bash" ) ); // as if it's not set at all
|
|
|
|
if ( configurationMap.contains( "userShell" ) )
|
|
|
|
{
|
|
|
|
shell = CalamaresUtils::getString( configurationMap, "userShell" );
|
|
|
|
}
|
|
|
|
// Now it might be explicitly set to empty, which is ok
|
|
|
|
setUserShell( shell );
|
2020-07-25 16:39:13 +02:00
|
|
|
|
|
|
|
setAutologinGroup( CalamaresUtils::getString( configurationMap, "autologinGroup" ) );
|
|
|
|
setSudoersGroup( CalamaresUtils::getString( configurationMap, "sudoersGroup" ) );
|
2020-07-28 11:41:52 +02:00
|
|
|
|
|
|
|
m_doAutoLogin = CalamaresUtils::getBool( configurationMap, "doAutologin", false );
|
2020-07-25 12:47:01 +02:00
|
|
|
}
|