2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2020-07-25 12:47:01 +02:00
|
|
|
*
|
|
|
|
* SPDX-FileCopyrightText: 2020 Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2020-07-25 12:47:01 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Config.h"
|
|
|
|
|
2020-08-18 11:31:32 +02:00
|
|
|
#include "CreateUserJob.h"
|
2020-10-21 14:43:45 +02:00
|
|
|
#include "MiscJobs.h"
|
2020-08-18 11:31:32 +02:00
|
|
|
#include "SetHostNameJob.h"
|
|
|
|
#include "SetPasswordJob.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"
|
|
|
|
|
2022-04-11 14:12:05 +02:00
|
|
|
#include <KMacroExpander>
|
|
|
|
|
2020-08-05 13:03:18 +02:00
|
|
|
#include <QCoreApplication>
|
2020-07-27 15:54:52 +02:00
|
|
|
#include <QFile>
|
2021-03-14 13:30:26 +01:00
|
|
|
#include <QMetaProperty>
|
2020-07-27 15:34:59 +02:00
|
|
|
#include <QRegExp>
|
2021-03-14 12:27:53 +01:00
|
|
|
#include <QTimer>
|
2020-07-28 10:21:23 +02:00
|
|
|
|
2020-11-02 01:24:42 +01:00
|
|
|
#ifdef HAVE_ICU
|
|
|
|
#include <unicode/translit.h>
|
|
|
|
#include <unicode/unistr.h>
|
2020-11-04 01:21:31 +01:00
|
|
|
|
2020-11-09 23:40:08 +01:00
|
|
|
//Needed for ICU to apply some transliteration ruleset.
|
|
|
|
//Still needs to be adjusted to fit the needs of the most of users
|
2020-11-09 23:23:10 +01:00
|
|
|
static const char TRANSLITERATOR_ID[] = "Russian-Latin/BGN;"
|
2020-11-09 23:40:08 +01:00
|
|
|
"Greek-Latin/UNGEGN;"
|
|
|
|
"Any-Latin;"
|
|
|
|
"Latin-ASCII";
|
2020-11-02 01:24:42 +01:00
|
|
|
#endif
|
|
|
|
|
2021-02-02 13:38:52 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2020-07-28 10:21:23 +02:00
|
|
|
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-08-28 23:20:02 +02:00
|
|
|
static void
|
|
|
|
updateGSAutoLogin( bool doAutoLogin, const QString& login )
|
|
|
|
{
|
|
|
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
2020-10-14 01:15:16 +02:00
|
|
|
if ( !gs )
|
|
|
|
{
|
|
|
|
cWarning() << "No Global Storage available";
|
|
|
|
return;
|
|
|
|
}
|
2020-08-28 23:20:02 +02:00
|
|
|
|
|
|
|
if ( doAutoLogin && !login.isEmpty() )
|
|
|
|
{
|
2021-03-15 00:21:26 +01:00
|
|
|
gs->insert( "autoLoginUser", login );
|
2020-08-28 23:20:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-15 00:21:26 +01:00
|
|
|
gs->remove( "autoLoginUser" );
|
2020-08-28 23:20:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( login.isEmpty() )
|
|
|
|
{
|
|
|
|
gs->remove( "username" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gs->insert( "username", login );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 14:40:35 +02:00
|
|
|
static const QStringList&
|
|
|
|
alwaysForbiddenLoginNames()
|
|
|
|
{
|
|
|
|
static QStringList s { QStringLiteral( "root" ), QStringLiteral( "nobody" ) };
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const QStringList&
|
|
|
|
alwaysForbiddenHostNames()
|
|
|
|
{
|
|
|
|
static QStringList s { QStringLiteral( "localhost" ) };
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-08-05 10:29:13 +02:00
|
|
|
const NamedEnumTable< HostNameAction >&
|
2022-04-11 12:16:03 +02:00
|
|
|
hostnameActionNames()
|
2020-08-05 10:29:13 +02:00
|
|
|
{
|
|
|
|
// *INDENT-OFF*
|
|
|
|
// clang-format off
|
|
|
|
static const NamedEnumTable< HostNameAction > names {
|
|
|
|
{ QStringLiteral( "none" ), HostNameAction::None },
|
|
|
|
{ QStringLiteral( "etcfile" ), HostNameAction::EtcHostname },
|
2022-04-11 11:47:47 +02:00
|
|
|
{ QStringLiteral( "etc" ), HostNameAction::EtcHostname },
|
|
|
|
{ QStringLiteral( "hostnamed" ), HostNameAction::SystemdHostname },
|
|
|
|
{ QStringLiteral( "transient" ), HostNameAction::Transient },
|
2020-08-05 10:29:13 +02:00
|
|
|
};
|
|
|
|
// clang-format on
|
|
|
|
// *INDENT-ON*
|
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2020-07-25 12:47:01 +02:00
|
|
|
Config::Config( QObject* parent )
|
2021-03-12 13:54:06 +01:00
|
|
|
: Calamares::ModuleSystem::Config( parent )
|
2022-05-18 14:40:35 +02:00
|
|
|
, m_forbiddenHostNames( alwaysForbiddenHostNames() )
|
|
|
|
, m_forbiddenLoginNames( alwaysForbiddenLoginNames() )
|
2020-07-25 12:47:01 +02:00
|
|
|
{
|
2020-08-18 11:21:53 +02:00
|
|
|
emit readyChanged( m_isReady ); // false
|
|
|
|
|
|
|
|
// Gang together all the changes of status to one readyChanged() signal
|
2022-04-11 12:16:03 +02:00
|
|
|
connect( this, &Config::hostnameStatusChanged, this, &Config::checkReady );
|
2020-08-18 11:21:53 +02:00
|
|
|
connect( this, &Config::loginNameStatusChanged, this, &Config::checkReady );
|
|
|
|
connect( this, &Config::fullNameChanged, this, &Config::checkReady );
|
|
|
|
connect( this, &Config::userPasswordStatusChanged, this, &Config::checkReady );
|
|
|
|
connect( this, &Config::rootPasswordStatusChanged, this, &Config::checkReady );
|
|
|
|
connect( this, &Config::reuseUserPasswordForRootChanged, this, &Config::checkReady );
|
|
|
|
connect( this, &Config::requireStrongPasswordsChanged, this, &Config::checkReady );
|
2020-07-25 12:47:01 +02:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:25:16 +01:00
|
|
|
Config::~Config() {}
|
2020-07-25 12:47:01 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-10-14 12:52:47 +02:00
|
|
|
if ( shell != m_userShell )
|
2020-08-18 17:20:08 +02:00
|
|
|
{
|
2020-10-14 12:52:47 +02:00
|
|
|
m_userShell = shell;
|
2020-10-21 14:43:45 +02:00
|
|
|
emit userShellChanged( shell );
|
2020-10-14 12:52:47 +02:00
|
|
|
// The shell is put into GS as well.
|
|
|
|
auto* gs = Calamares::JobQueue::instance()->globalStorage();
|
|
|
|
if ( gs )
|
|
|
|
{
|
|
|
|
gs->insert( "userShell", shell );
|
|
|
|
}
|
2020-08-18 17:20:08 +02:00
|
|
|
}
|
2020-07-25 15:39:19 +02:00
|
|
|
}
|
|
|
|
|
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
|
2021-03-15 00:21:26 +01:00
|
|
|
Config::setAutoLoginGroup( const QString& group )
|
2020-07-25 16:39:13 +02:00
|
|
|
{
|
2021-03-15 00:21:26 +01:00
|
|
|
if ( group != m_autoLoginGroup )
|
2020-10-14 15:04:37 +02:00
|
|
|
{
|
2021-03-15 00:21:26 +01:00
|
|
|
m_autoLoginGroup = group;
|
|
|
|
insertInGlobalStorage( QStringLiteral( "autoLoginGroup" ), group );
|
|
|
|
emit autoLoginGroupChanged( group );
|
2020-10-14 15:04:37 +02:00
|
|
|
}
|
2020-07-25 16:39:13 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 14:07:40 +02:00
|
|
|
QStringList
|
|
|
|
Config::groupsForThisUser() const
|
|
|
|
{
|
|
|
|
QStringList l;
|
|
|
|
l.reserve( defaultGroups().size() + 1 );
|
|
|
|
|
|
|
|
for ( const auto& g : defaultGroups() )
|
|
|
|
{
|
|
|
|
l << g.name();
|
|
|
|
}
|
2021-03-15 00:21:26 +01:00
|
|
|
if ( doAutoLogin() && !autoLoginGroup().isEmpty() )
|
2020-10-22 14:07:40 +02:00
|
|
|
{
|
2021-03-15 00:21:26 +01:00
|
|
|
l << autoLoginGroup();
|
2020-10-22 14:07:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2020-07-25 16:39:13 +02:00
|
|
|
void
|
|
|
|
Config::setSudoersGroup( const QString& group )
|
|
|
|
{
|
2020-10-14 15:04:37 +02:00
|
|
|
if ( group != m_sudoersGroup )
|
|
|
|
{
|
|
|
|
m_sudoersGroup = group;
|
|
|
|
insertInGlobalStorage( QStringLiteral( "sudoersGroup" ), group );
|
|
|
|
emit sudoersGroupChanged( group );
|
|
|
|
}
|
2020-07-25 16:39:13 +02:00
|
|
|
}
|
|
|
|
|
2020-07-25 15:39:19 +02:00
|
|
|
|
2020-07-25 16:54:15 +02:00
|
|
|
void
|
|
|
|
Config::setLoginName( const QString& login )
|
|
|
|
{
|
2021-03-14 13:30:26 +01:00
|
|
|
CONFIG_PREVENT_EDITING( QString, "loginName" );
|
2021-03-14 12:27:53 +01:00
|
|
|
if ( login != m_loginName )
|
2020-07-25 16:54:15 +02:00
|
|
|
{
|
2020-07-27 15:34:59 +02:00
|
|
|
m_customLoginName = !login.isEmpty();
|
2020-07-25 16:54:15 +02:00
|
|
|
m_loginName = login;
|
2020-10-14 15:04:37 +02:00
|
|
|
updateGSAutoLogin( doAutoLogin(), login );
|
2020-07-25 16:54:15 +02:00
|
|
|
emit loginNameChanged( login );
|
2020-07-28 10:21:23 +02:00
|
|
|
emit loginNameStatusChanged( loginNameStatus() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const QStringList&
|
2022-05-09 14:49:15 +02:00
|
|
|
Config::forbiddenLoginNames() const
|
2020-07-28 10:21:23 +02:00
|
|
|
{
|
2022-05-09 14:49:15 +02:00
|
|
|
return m_forbiddenLoginNames;
|
2020-07-28 10:21:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-07-29 12:27:56 +02:00
|
|
|
QRegExp validateFirstLetter( "^[a-z_]" );
|
|
|
|
if ( validateFirstLetter.indexIn( m_loginName ) != 0 )
|
2020-07-28 10:21:23 +02:00
|
|
|
{
|
|
|
|
return tr( "Your username must start with a lowercase letter or underscore." );
|
|
|
|
}
|
2020-07-29 12:27:56 +02:00
|
|
|
if ( !USERNAME_RX.exactMatch( m_loginName ) )
|
2020-07-28 10:21:23 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2022-05-09 14:58:46 +02:00
|
|
|
// Although we've made the list lower-case, and the RE above forces lower-case, still pass the flag
|
|
|
|
if ( forbiddenLoginNames().contains( m_loginName, Qt::CaseInsensitive ) )
|
|
|
|
{
|
|
|
|
return tr( "'%1' is not allowed as username." ).arg( m_loginName );
|
|
|
|
}
|
|
|
|
|
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 )
|
|
|
|
{
|
2022-04-11 12:16:03 +02:00
|
|
|
if ( hostnameAction() != HostNameAction::EtcHostname && hostnameAction() != HostNameAction::SystemdHostname )
|
2022-04-11 11:18:30 +02:00
|
|
|
{
|
|
|
|
cDebug() << "Ignoring hostname" << host << "No hostname will be set.";
|
|
|
|
return;
|
|
|
|
}
|
2022-04-11 12:16:03 +02:00
|
|
|
if ( host != m_hostname )
|
2020-07-27 15:54:52 +02:00
|
|
|
{
|
2020-10-14 15:04:37 +02:00
|
|
|
m_customHostName = !host.isEmpty();
|
2022-04-11 12:16:03 +02:00
|
|
|
m_hostname = host;
|
2020-07-28 12:16:03 +02:00
|
|
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
|
|
|
if ( host.isEmpty() )
|
|
|
|
{
|
|
|
|
gs->remove( "hostname" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gs->insert( "hostname", host );
|
|
|
|
}
|
2022-04-11 12:16:03 +02:00
|
|
|
emit hostnameChanged( host );
|
|
|
|
emit hostnameStatusChanged( hostnameStatus() );
|
2020-07-28 10:45:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const QStringList&
|
2022-05-09 14:49:15 +02:00
|
|
|
Config::forbiddenHostNames() const
|
2020-07-28 10:45:38 +02:00
|
|
|
{
|
2022-05-09 14:49:15 +02:00
|
|
|
return m_forbiddenHostNames;
|
2020-07-28 10:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
2022-04-11 12:16:03 +02:00
|
|
|
Config::hostnameStatus() const
|
2020-07-28 10:45:38 +02:00
|
|
|
{
|
|
|
|
// An empty hostname is "ok", even if it isn't really
|
2022-04-11 12:16:03 +02:00
|
|
|
if ( m_hostname.isEmpty() )
|
2020-07-28 10:45:38 +02:00
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2022-04-11 12:16:03 +02:00
|
|
|
if ( m_hostname.length() < HOSTNAME_MIN_LENGTH )
|
2020-07-28 10:45:38 +02:00
|
|
|
{
|
|
|
|
return tr( "Your hostname is too short." );
|
2020-07-27 15:54:52 +02:00
|
|
|
}
|
2022-04-11 12:16:03 +02:00
|
|
|
if ( m_hostname.length() > HOSTNAME_MAX_LENGTH )
|
2020-07-28 10:45:38 +02:00
|
|
|
{
|
|
|
|
return tr( "Your hostname is too long." );
|
|
|
|
}
|
2022-05-09 14:58:46 +02:00
|
|
|
|
|
|
|
// "LocalHost" is just as forbidden as "localhost"
|
|
|
|
if ( forbiddenHostNames().contains( m_hostname, Qt::CaseInsensitive ) )
|
2020-07-28 10:45:38 +02:00
|
|
|
{
|
2022-05-09 14:58:46 +02:00
|
|
|
return tr( "'%1' is not allowed as hostname." ).arg( m_hostname );
|
2020-07-28 10:45:38 +02:00
|
|
|
}
|
|
|
|
|
2022-04-11 12:16:03 +02:00
|
|
|
if ( !HOSTNAME_RX.exactMatch( m_hostname ) )
|
2020-07-28 10:45:38 +02:00
|
|
|
{
|
|
|
|
return tr( "Only letters, numbers, underscore and hyphen are allowed." );
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString();
|
2020-07-27 15:54:52 +02:00
|
|
|
}
|
|
|
|
|
2022-04-11 14:12:05 +02:00
|
|
|
static QString
|
|
|
|
cleanupForHostname( const QString& s )
|
|
|
|
{
|
|
|
|
QRegExp dmirx( "[^a-zA-Z0-9]", Qt::CaseInsensitive );
|
|
|
|
return s.toLower().replace( dmirx, " " ).remove( ' ' );
|
|
|
|
}
|
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 )
|
|
|
|
{
|
|
|
|
QFile dmiFile( QStringLiteral( "/sys/devices/virtual/dmi/id/product_name" ) );
|
|
|
|
|
|
|
|
if ( dmiFile.exists() && dmiFile.open( QIODevice::ReadOnly ) )
|
|
|
|
{
|
2022-04-11 14:12:05 +02:00
|
|
|
dmiProduct = cleanupForHostname( QString::fromLocal8Bit( dmiFile.readAll().simplified().data() ) );
|
2020-07-27 15:54:52 +02:00
|
|
|
}
|
|
|
|
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-11-02 01:24:42 +01:00
|
|
|
#ifdef HAVE_ICU
|
|
|
|
static QString
|
|
|
|
transliterate( const QString& input )
|
|
|
|
{
|
2020-11-02 02:01:01 +01:00
|
|
|
static auto ue = UErrorCode::U_ZERO_ERROR;
|
2020-11-02 01:24:42 +01:00
|
|
|
static auto transliterator = std::unique_ptr< icu::Transliterator >(
|
2020-11-09 23:54:21 +01:00
|
|
|
icu::Transliterator::createInstance( TRANSLITERATOR_ID, UTRANS_FORWARD, ue ) );
|
2020-11-02 01:24:42 +01:00
|
|
|
|
2020-11-09 23:54:21 +01:00
|
|
|
if ( ue != UErrorCode::U_ZERO_ERROR )
|
|
|
|
{
|
2020-11-02 01:24:42 +01:00
|
|
|
cWarning() << "Can't create transliterator";
|
|
|
|
|
|
|
|
//it'll be checked later for non-ASCII characters
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2020-11-09 23:47:07 +01:00
|
|
|
icu::UnicodeString transliterable( input.utf16() );
|
2020-11-02 01:24:42 +01:00
|
|
|
transliterator->transliterate( transliterable );
|
|
|
|
return QString::fromUtf16( transliterable.getTerminatedBuffer() );
|
2020-11-09 23:47:07 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static QString
|
|
|
|
transliterate( const QString& input )
|
|
|
|
{
|
|
|
|
return input;
|
2020-11-02 01:24:42 +01:00
|
|
|
}
|
|
|
|
#endif
|
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();
|
|
|
|
}
|
|
|
|
|
2022-04-11 14:12:05 +02:00
|
|
|
/** @brief Return an invalid string for use in a hostname, if @p s is empty
|
|
|
|
*
|
|
|
|
* Maps empty to "^" (which is invalid in a hostname), everything else
|
|
|
|
* returns @p s itself.
|
|
|
|
*/
|
2020-07-27 15:54:52 +02:00
|
|
|
static QString
|
2022-04-11 14:12:05 +02:00
|
|
|
invalidEmpty( const QString& s )
|
2020-07-27 15:54:52 +02:00
|
|
|
{
|
2022-04-11 14:12:05 +02:00
|
|
|
return s.isEmpty() ? QStringLiteral( "^" ) : s;
|
|
|
|
}
|
2020-07-27 15:54:52 +02:00
|
|
|
|
2022-04-11 14:12:05 +02:00
|
|
|
STATICTEST QString
|
|
|
|
makeHostnameSuggestion( const QString& templateString, const QStringList& fullNameParts, const QString& loginName )
|
|
|
|
{
|
|
|
|
QHash< QString, QString > replace;
|
|
|
|
// User data
|
|
|
|
replace.insert( QStringLiteral( "first" ),
|
|
|
|
invalidEmpty( fullNameParts.isEmpty() ? QString() : cleanupForHostname( fullNameParts.first() ) ) );
|
|
|
|
replace.insert( QStringLiteral( "name" ), invalidEmpty( cleanupForHostname( fullNameParts.join( QString() ) ) ) );
|
|
|
|
replace.insert( QStringLiteral( "login" ), invalidEmpty( cleanupForHostname( loginName ) ) );
|
|
|
|
// Hardware data
|
|
|
|
replace.insert( QStringLiteral( "product" ), guessProductName() );
|
|
|
|
replace.insert( QStringLiteral( "product2" ), cleanupForHostname( QSysInfo::prettyProductName() ) );
|
|
|
|
replace.insert( QStringLiteral( "cpu" ), cleanupForHostname( QSysInfo::currentCpuArchitecture() ) );
|
|
|
|
// Hostname data
|
|
|
|
replace.insert( QStringLiteral( "host" ), invalidEmpty( cleanupForHostname( QSysInfo::machineHostName() ) ) );
|
|
|
|
|
|
|
|
QString hostnameSuggestion = KMacroExpander::expandMacros( templateString, replace, '$' );
|
|
|
|
|
|
|
|
// RegExp for valid hostnames; if the suggestion produces a valid name, return it
|
|
|
|
static const QRegExp HOSTNAME_RX( "^[a-zA-Z0-9][-a-zA-Z0-9_]*$" );
|
2020-07-27 15:54:52 +02:00
|
|
|
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
|
|
|
{
|
2021-03-14 13:30:26 +01:00
|
|
|
CONFIG_PREVENT_EDITING( QString, "fullName" );
|
2021-03-12 17:20:36 +01: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;
|
2022-04-13 05:38:51 +02:00
|
|
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
|
|
|
if ( name.isEmpty() )
|
|
|
|
{
|
|
|
|
gs->remove( "fullname" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gs->insert( "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
|
2020-11-04 01:24:58 +01:00
|
|
|
static QRegExp rx( "[^a-zA-Z0-9 ]", Qt::CaseInsensitive );
|
2020-11-09 23:47:07 +01:00
|
|
|
|
2022-04-11 14:12:05 +02:00
|
|
|
const QString cleanName = CalamaresUtils::removeDiacritics( transliterate( name ) )
|
|
|
|
.replace( QRegExp( "[-']" ), "" )
|
|
|
|
.replace( rx, " " )
|
|
|
|
.toLower()
|
|
|
|
.simplified();
|
2020-11-02 01:24:42 +01:00
|
|
|
|
|
|
|
|
2020-07-27 15:34:59 +02:00
|
|
|
QStringList cleanParts = cleanName.split( ' ' );
|
|
|
|
|
|
|
|
if ( !m_customLoginName )
|
|
|
|
{
|
2022-04-11 14:12:05 +02:00
|
|
|
const QString login = makeLoginNameSuggestion( cleanParts );
|
2020-07-27 15:34:59 +02:00
|
|
|
if ( !login.isEmpty() && login != m_loginName )
|
|
|
|
{
|
2020-08-28 23:20:02 +02:00
|
|
|
setLoginName( login );
|
|
|
|
// It's **still** not custom, though setLoginName() sets that
|
|
|
|
m_customLoginName = false;
|
2020-07-27 15:34:59 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-27 15:54:52 +02:00
|
|
|
if ( !m_customHostName )
|
|
|
|
{
|
2022-04-11 14:12:05 +02:00
|
|
|
const QString hostname = makeHostnameSuggestion( m_hostnameTemplate, cleanParts, loginName() );
|
2022-04-11 12:16:03 +02:00
|
|
|
if ( !hostname.isEmpty() && hostname != m_hostname )
|
2020-07-27 15:54:52 +02:00
|
|
|
{
|
2020-08-28 23:20:02 +02:00
|
|
|
setHostName( hostname );
|
|
|
|
// Still not custom
|
|
|
|
m_customHostName = false;
|
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;
|
2020-10-14 15:04:37 +02:00
|
|
|
updateGSAutoLogin( b, loginName() );
|
2020-07-28 11:41:52 +02:00
|
|
|
emit autoLoginChanged( b );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 22:16:48 +02:00
|
|
|
void
|
|
|
|
Config::setReuseUserPasswordForRoot( bool reuse )
|
|
|
|
{
|
|
|
|
if ( reuse != m_reuseUserPasswordForRoot )
|
|
|
|
{
|
|
|
|
m_reuseUserPasswordForRoot = reuse;
|
|
|
|
emit reuseUserPasswordForRootChanged( reuse );
|
2020-08-18 12:46:27 +02:00
|
|
|
{
|
|
|
|
auto rp = rootPasswordStatus();
|
|
|
|
emit rootPasswordStatusChanged( rp.first, rp.second );
|
|
|
|
}
|
2020-08-04 22:16:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Config::setRequireStrongPasswords( bool strong )
|
|
|
|
{
|
|
|
|
if ( strong != m_requireStrongPasswords )
|
|
|
|
{
|
|
|
|
m_requireStrongPasswords = strong;
|
|
|
|
emit requireStrongPasswordsChanged( strong );
|
2020-08-18 12:46:27 +02:00
|
|
|
{
|
|
|
|
auto rp = rootPasswordStatus();
|
|
|
|
emit rootPasswordStatusChanged( rp.first, rp.second );
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto up = userPasswordStatus();
|
|
|
|
emit userPasswordStatusChanged( up.first, up.second );
|
|
|
|
}
|
2020-08-04 22:16:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 13:29:06 +02:00
|
|
|
void
|
|
|
|
Config::setUserPassword( const QString& s )
|
|
|
|
{
|
2020-08-17 11:41:04 +02:00
|
|
|
if ( s != m_userPassword )
|
|
|
|
{
|
|
|
|
m_userPassword = s;
|
2020-08-17 14:08:59 +02:00
|
|
|
const auto p = passwordStatus( m_userPassword, m_userPasswordSecondary );
|
|
|
|
emit userPasswordStatusChanged( p.first, p.second );
|
2020-08-17 11:41:04 +02:00
|
|
|
emit userPasswordChanged( s );
|
|
|
|
}
|
2020-08-05 13:29:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Config::setUserPasswordSecondary( const QString& s )
|
|
|
|
{
|
2020-08-17 11:41:04 +02:00
|
|
|
if ( s != m_userPasswordSecondary )
|
|
|
|
{
|
|
|
|
m_userPasswordSecondary = s;
|
2020-08-17 14:08:59 +02:00
|
|
|
const auto p = passwordStatus( m_userPassword, m_userPasswordSecondary );
|
|
|
|
emit userPasswordStatusChanged( p.first, p.second );
|
2020-08-17 11:41:04 +02:00
|
|
|
emit userPasswordSecondaryChanged( s );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 11:21:53 +02:00
|
|
|
/** @brief Checks two copies of the password for validity
|
|
|
|
*
|
|
|
|
* Given two copies of the password -- generally the password and
|
|
|
|
* the secondary fields -- checks them for validity and returns
|
|
|
|
* a pair of <validity, message>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
Config::PasswordStatus
|
2020-08-17 11:41:04 +02:00
|
|
|
Config::passwordStatus( const QString& pw1, const QString& pw2 ) const
|
|
|
|
{
|
|
|
|
if ( pw1 != pw2 )
|
|
|
|
{
|
|
|
|
return qMakePair( PasswordValidity::Invalid, tr( "Your passwords do not match!" ) );
|
|
|
|
}
|
|
|
|
|
2020-08-17 13:22:44 +02:00
|
|
|
bool failureIsFatal = requireStrongPasswords();
|
|
|
|
for ( const auto& pc : m_passwordChecks )
|
|
|
|
{
|
|
|
|
QString message = pc.filter( pw1 );
|
|
|
|
|
|
|
|
if ( !message.isEmpty() )
|
|
|
|
{
|
|
|
|
return qMakePair( failureIsFatal ? PasswordValidity::Invalid : PasswordValidity::Weak, message );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 14:21:07 +02:00
|
|
|
return qMakePair( PasswordValidity::Valid, tr( "OK!" ) );
|
2020-08-17 11:41:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-17 14:20:54 +02:00
|
|
|
Config::PasswordStatus
|
|
|
|
Config::userPasswordStatus() const
|
|
|
|
{
|
|
|
|
return passwordStatus( m_userPassword, m_userPasswordSecondary );
|
|
|
|
}
|
|
|
|
|
2020-08-17 11:41:04 +02:00
|
|
|
int
|
|
|
|
Config::userPasswordValidity() const
|
|
|
|
{
|
2020-08-17 14:20:54 +02:00
|
|
|
auto p = userPasswordStatus();
|
2020-08-17 11:41:04 +02:00
|
|
|
return p.first;
|
2020-08-05 13:29:06 +02:00
|
|
|
}
|
|
|
|
|
2020-08-17 11:41:04 +02:00
|
|
|
QString
|
2020-08-17 14:20:54 +02:00
|
|
|
Config::userPasswordMessage() const
|
2020-08-17 11:41:04 +02:00
|
|
|
{
|
2020-08-17 14:20:54 +02:00
|
|
|
auto p = userPasswordStatus();
|
2020-08-17 11:41:04 +02:00
|
|
|
return p.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-05 13:29:06 +02:00
|
|
|
void
|
|
|
|
Config::setRootPassword( const QString& s )
|
|
|
|
{
|
2020-08-17 11:41:04 +02:00
|
|
|
if ( writeRootPassword() && s != m_rootPassword )
|
2020-08-05 13:29:06 +02:00
|
|
|
{
|
|
|
|
m_rootPassword = s;
|
2020-08-17 14:08:59 +02:00
|
|
|
const auto p = passwordStatus( m_rootPassword, m_rootPasswordSecondary );
|
|
|
|
emit rootPasswordStatusChanged( p.first, p.second );
|
2020-08-05 13:29:06 +02:00
|
|
|
emit rootPasswordChanged( s );
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 13:03:18 +02:00
|
|
|
|
2020-08-05 13:29:06 +02:00
|
|
|
void
|
|
|
|
Config::setRootPasswordSecondary( const QString& s )
|
|
|
|
{
|
2020-08-17 11:41:04 +02:00
|
|
|
if ( writeRootPassword() && s != m_rootPasswordSecondary )
|
2020-08-05 13:29:06 +02:00
|
|
|
{
|
|
|
|
m_rootPasswordSecondary = s;
|
2020-08-17 14:08:59 +02:00
|
|
|
const auto p = passwordStatus( m_rootPassword, m_rootPasswordSecondary );
|
|
|
|
emit rootPasswordStatusChanged( p.first, p.second );
|
2020-08-05 13:29:06 +02:00
|
|
|
emit rootPasswordSecondaryChanged( s );
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 13:03:18 +02:00
|
|
|
|
2020-08-17 11:41:04 +02:00
|
|
|
QString
|
|
|
|
Config::rootPassword() const
|
2020-08-05 13:42:18 +02:00
|
|
|
{
|
|
|
|
if ( writeRootPassword() )
|
|
|
|
{
|
|
|
|
if ( reuseUserPasswordForRoot() )
|
2020-08-17 11:41:04 +02:00
|
|
|
{
|
2020-08-05 13:42:18 +02:00
|
|
|
return userPassword();
|
2020-08-17 11:41:04 +02:00
|
|
|
}
|
2020-08-05 13:42:18 +02:00
|
|
|
return m_rootPassword;
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2020-08-17 11:41:04 +02:00
|
|
|
QString
|
|
|
|
Config::rootPasswordSecondary() const
|
2020-08-05 13:42:18 +02:00
|
|
|
{
|
|
|
|
if ( writeRootPassword() )
|
|
|
|
{
|
|
|
|
if ( reuseUserPasswordForRoot() )
|
2020-08-17 11:41:04 +02:00
|
|
|
{
|
2020-08-05 13:42:18 +02:00
|
|
|
return userPasswordSecondary();
|
2020-08-17 11:41:04 +02:00
|
|
|
}
|
2020-08-05 13:42:18 +02:00
|
|
|
return m_rootPasswordSecondary;
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2020-08-17 14:20:54 +02:00
|
|
|
Config::PasswordStatus
|
|
|
|
Config::rootPasswordStatus() const
|
2020-08-17 11:41:04 +02:00
|
|
|
{
|
|
|
|
if ( writeRootPassword() && !reuseUserPasswordForRoot() )
|
|
|
|
{
|
2020-08-17 14:20:54 +02:00
|
|
|
return passwordStatus( m_rootPassword, m_rootPasswordSecondary );
|
2020-08-17 11:41:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-17 14:20:54 +02:00
|
|
|
return userPasswordStatus();
|
2020-08-17 11:41:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 14:20:54 +02:00
|
|
|
int
|
|
|
|
Config::rootPasswordValidity() const
|
|
|
|
{
|
|
|
|
auto p = rootPasswordStatus();
|
|
|
|
return p.first;
|
|
|
|
}
|
|
|
|
|
2020-08-17 11:41:04 +02:00
|
|
|
QString
|
2020-08-17 14:20:54 +02:00
|
|
|
Config::rootPasswordMessage() const
|
2020-08-17 11:41:04 +02:00
|
|
|
{
|
2020-08-17 14:20:54 +02:00
|
|
|
auto p = rootPasswordStatus();
|
|
|
|
return p.second;
|
2020-08-17 11:41:04 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 11:21:53 +02:00
|
|
|
bool
|
|
|
|
Config::isReady() const
|
|
|
|
{
|
|
|
|
bool readyFullName = !fullName().isEmpty(); // Needs some text
|
2022-04-11 12:16:03 +02:00
|
|
|
bool readyHostname = hostnameStatus().isEmpty(); // .. no warning message
|
2020-10-17 15:48:12 +02:00
|
|
|
bool readyUsername = !loginName().isEmpty() && loginNameStatus().isEmpty(); // .. no warning message
|
2020-08-18 11:21:53 +02:00
|
|
|
bool readyUserPassword = userPasswordValidity() != Config::PasswordValidity::Invalid;
|
|
|
|
bool readyRootPassword = rootPasswordValidity() != Config::PasswordValidity::Invalid;
|
|
|
|
return readyFullName && readyHostname && readyUsername && readyUserPassword && readyRootPassword;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief Update ready status and emit signal
|
|
|
|
*
|
|
|
|
* This is a "concentrator" private slot for all the status-changed
|
|
|
|
* signals, so that readyChanged() is emitted only when needed.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Config::checkReady()
|
|
|
|
{
|
|
|
|
bool b = isReady();
|
|
|
|
if ( b != m_isReady )
|
|
|
|
{
|
|
|
|
m_isReady = b;
|
|
|
|
emit readyChanged( b );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 22:16:48 +02:00
|
|
|
|
2020-07-31 09:52:06 +02:00
|
|
|
STATICTEST void
|
2020-10-13 17:35:07 +02:00
|
|
|
setConfigurationDefaultGroups( const QVariantMap& map, QList< GroupDescription >& defaultGroups )
|
2020-07-29 12:18:25 +02:00
|
|
|
{
|
2020-10-13 17:35:07 +02:00
|
|
|
defaultGroups.clear();
|
2020-10-13 22:12:26 +02:00
|
|
|
|
|
|
|
const QString key( "defaultGroups" );
|
|
|
|
auto groupsFromConfig = map.value( key ).toList();
|
|
|
|
if ( groupsFromConfig.isEmpty() )
|
|
|
|
{
|
|
|
|
if ( map.contains( key ) && map.value( key ).isValid() && map.value( key ).canConvert( QVariant::List ) )
|
|
|
|
{
|
|
|
|
// Explicitly set, but empty: this is valid, but unusual.
|
|
|
|
cDebug() << key << "has explicit empty value.";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-13 23:30:28 +02:00
|
|
|
// By default give the user a handful of "traditional" groups, if
|
|
|
|
// none are specified at all. These are system (GID < 1000) groups.
|
2020-10-13 22:12:26 +02:00
|
|
|
cWarning() << "Using fallback groups. Please check *defaultGroups* value in users.conf";
|
|
|
|
for ( const auto& s : { "lp", "video", "network", "storage", "wheel", "audio" } )
|
|
|
|
{
|
2020-10-13 23:30:28 +02:00
|
|
|
defaultGroups.append(
|
|
|
|
GroupDescription( s, GroupDescription::CreateIfNeeded {}, GroupDescription::SystemGroup {} ) );
|
2020-10-13 22:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for ( const auto& v : groupsFromConfig )
|
|
|
|
{
|
|
|
|
if ( v.type() == QVariant::String )
|
|
|
|
{
|
|
|
|
defaultGroups.append( GroupDescription( v.toString() ) );
|
|
|
|
}
|
2020-10-13 23:30:28 +02:00
|
|
|
else if ( v.type() == QVariant::Map )
|
|
|
|
{
|
|
|
|
const auto innermap = v.toMap();
|
|
|
|
QString name = CalamaresUtils::getString( innermap, "name" );
|
|
|
|
if ( !name.isEmpty() )
|
|
|
|
{
|
|
|
|
defaultGroups.append( GroupDescription( name,
|
|
|
|
CalamaresUtils::getBool( innermap, "must_exist", false ),
|
|
|
|
CalamaresUtils::getBool( innermap, "system", false ) ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cWarning() << "Ignoring *defaultGroups* entry without a name" << v;
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 22:12:26 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cWarning() << "Unknown *defaultGroups* entry" << v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 12:18:25 +02:00
|
|
|
}
|
|
|
|
|
2022-04-11 11:10:40 +02:00
|
|
|
STATICTEST HostNameAction
|
|
|
|
getHostNameAction( const QVariantMap& configurationMap )
|
2020-08-05 10:29:13 +02:00
|
|
|
{
|
|
|
|
HostNameAction setHostName = HostNameAction::EtcHostname;
|
2022-04-08 10:45:50 +02:00
|
|
|
QString hostnameActionString = CalamaresUtils::getString( configurationMap, "location" );
|
2020-08-05 10:29:13 +02:00
|
|
|
if ( !hostnameActionString.isEmpty() )
|
|
|
|
{
|
|
|
|
bool ok = false;
|
2022-04-11 12:16:03 +02:00
|
|
|
setHostName = hostnameActionNames().find( hostnameActionString, ok );
|
2020-08-05 10:29:13 +02:00
|
|
|
if ( !ok )
|
|
|
|
{
|
|
|
|
setHostName = HostNameAction::EtcHostname; // Rather than none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-11 11:10:40 +02:00
|
|
|
return setHostName;
|
2020-08-05 10:29:13 +02:00
|
|
|
}
|
|
|
|
|
2020-08-05 13:03:18 +02:00
|
|
|
/** @brief Process entries in the passwordRequirements config entry
|
|
|
|
*
|
|
|
|
* Called once for each item in the config entry, which should
|
|
|
|
* be a key-value pair. What makes sense as a value depends on
|
|
|
|
* the key. Supported keys are documented in users.conf.
|
|
|
|
*
|
|
|
|
* @return if the check was added, returns @c true
|
|
|
|
*/
|
|
|
|
STATICTEST bool
|
|
|
|
addPasswordCheck( const QString& key, const QVariant& value, PasswordCheckList& passwordChecks )
|
|
|
|
{
|
|
|
|
if ( key == "minLength" )
|
|
|
|
{
|
|
|
|
add_check_minLength( passwordChecks, value );
|
|
|
|
}
|
|
|
|
else if ( key == "maxLength" )
|
|
|
|
{
|
|
|
|
add_check_maxLength( passwordChecks, value );
|
|
|
|
}
|
|
|
|
else if ( key == "nonempty" )
|
|
|
|
{
|
|
|
|
if ( value.toBool() )
|
|
|
|
{
|
|
|
|
passwordChecks.push_back(
|
|
|
|
PasswordCheck( []() { return QCoreApplication::translate( "PWQ", "Password is empty" ); },
|
|
|
|
[]( const QString& s ) { return !s.isEmpty(); },
|
|
|
|
PasswordCheck::Weight( 1 ) ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cDebug() << "nonempty check is mentioned but set to false";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef CHECK_PWQUALITY
|
|
|
|
else if ( key == "libpwquality" )
|
|
|
|
{
|
|
|
|
add_check_libpwquality( passwordChecks, value );
|
|
|
|
}
|
|
|
|
#endif // CHECK_PWQUALITY
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cWarning() << "Unknown password-check key" << key;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-29 12:18:25 +02:00
|
|
|
|
2021-04-13 15:08:13 +02:00
|
|
|
/** @brief Returns a value of either key from the map
|
|
|
|
*
|
|
|
|
* Takes a function (e.g. getBool, or getString) and two keys,
|
|
|
|
* returning the value in the map of the one that is there (or @p defaultArg)
|
|
|
|
*/
|
|
|
|
template < typename T, typename U >
|
|
|
|
T
|
|
|
|
either( T ( *f )( const QVariantMap&, const QString&, U ),
|
|
|
|
const QVariantMap& configurationMap,
|
|
|
|
const QString& oldKey,
|
|
|
|
const QString& newKey,
|
|
|
|
U defaultArg )
|
|
|
|
{
|
|
|
|
if ( configurationMap.contains( oldKey ) )
|
|
|
|
{
|
|
|
|
return f( configurationMap, oldKey, defaultArg );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return f( configurationMap, newKey, defaultArg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-11 10:14:32 +02:00
|
|
|
// TODO:3.3: Remove
|
2022-04-08 10:45:50 +02:00
|
|
|
static void
|
|
|
|
copyLegacy( const QVariantMap& source, const QString& sourceKey, QVariantMap& target, const QString& targetKey )
|
|
|
|
{
|
|
|
|
if ( source.contains( sourceKey ) )
|
|
|
|
{
|
2022-04-11 10:49:16 +02:00
|
|
|
if ( target.contains( targetKey ) )
|
|
|
|
{
|
|
|
|
cWarning() << "Legacy *users* key" << sourceKey << "ignored.";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const QVariant legacyValue = source.value( sourceKey );
|
|
|
|
cWarning() << "Legacy *users* key" << sourceKey << "overrides hostname-settings.";
|
|
|
|
target.insert( targetKey, legacyValue );
|
|
|
|
}
|
2022-04-08 10:45:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 14:49:15 +02:00
|
|
|
/** @brief Tidy up a list of names
|
|
|
|
*
|
|
|
|
* Remove duplicates, apply lowercase, sort.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
tidy( QStringList& l )
|
|
|
|
{
|
|
|
|
std::for_each( l.begin(), l.end(), []( QString& s ) { s = s.toLower(); } );
|
|
|
|
l.sort();
|
|
|
|
l.removeDuplicates();
|
|
|
|
}
|
|
|
|
|
2020-07-25 15:39:19 +02:00
|
|
|
void
|
|
|
|
Config::setConfigurationMap( const QVariantMap& configurationMap )
|
2020-07-25 12:47:01 +02:00
|
|
|
{
|
2022-05-09 14:23:53 +02:00
|
|
|
// Handle *user* key and subkeys and legacy settings
|
2020-07-25 15:39:19 +02:00
|
|
|
{
|
2022-05-09 14:23:53 +02:00
|
|
|
bool ok = false; // Ignored
|
|
|
|
QVariantMap userSettings = CalamaresUtils::getSubMap( configurationMap, "user", ok );
|
|
|
|
|
|
|
|
// TODO:3.3: Remove calls to copyLegacy
|
|
|
|
copyLegacy( configurationMap, "userShell", userSettings, "shell" );
|
|
|
|
|
|
|
|
QString shell( QLatin1String( "/bin/bash" ) ); // as if it's not set at all
|
|
|
|
if ( userSettings.contains( "shell" ) )
|
|
|
|
{
|
|
|
|
shell = CalamaresUtils::getString( userSettings, "shell" );
|
|
|
|
}
|
|
|
|
// Now it might be explicitly set to empty, which is ok
|
|
|
|
setUserShell( shell );
|
2022-05-09 14:49:15 +02:00
|
|
|
|
|
|
|
m_forbiddenLoginNames = CalamaresUtils::getStringList( userSettings, "forbidden_names" );
|
2022-05-18 14:40:35 +02:00
|
|
|
m_forbiddenLoginNames << alwaysForbiddenLoginNames();
|
2022-05-09 14:49:15 +02:00
|
|
|
tidy( m_forbiddenLoginNames );
|
2020-07-25 15:39:19 +02:00
|
|
|
}
|
2020-07-25 16:39:13 +02:00
|
|
|
|
2021-04-13 15:08:13 +02:00
|
|
|
setAutoLoginGroup( either< QString, const QString& >(
|
|
|
|
CalamaresUtils::getString, configurationMap, "autologinGroup", "autoLoginGroup", QString() ) );
|
2020-07-25 16:39:13 +02:00
|
|
|
setSudoersGroup( CalamaresUtils::getString( configurationMap, "sudoersGroup" ) );
|
2022-02-21 15:49:10 +01:00
|
|
|
m_sudoStyle = CalamaresUtils::getBool( configurationMap, "sudoersConfigureWithGroup", false )
|
|
|
|
? SudoStyle::UserAndGroup
|
|
|
|
: SudoStyle::UserOnly;
|
2020-07-28 11:41:52 +02:00
|
|
|
|
2022-04-08 10:45:50 +02:00
|
|
|
// Handle *hostname* key and subkeys and legacy settings
|
|
|
|
{
|
|
|
|
bool ok = false; // Ignored
|
|
|
|
QVariantMap hostnameSettings = CalamaresUtils::getSubMap( configurationMap, "hostname", ok );
|
|
|
|
|
2022-04-11 10:14:32 +02:00
|
|
|
// TODO:3.3: Remove calls to copyLegacy
|
2022-04-08 10:45:50 +02:00
|
|
|
copyLegacy( configurationMap, "setHostname", hostnameSettings, "location" );
|
|
|
|
copyLegacy( configurationMap, "writeHostsFile", hostnameSettings, "writeHostsFile" );
|
2022-04-11 12:16:03 +02:00
|
|
|
m_hostnameAction = getHostNameAction( hostnameSettings );
|
2022-04-11 11:10:40 +02:00
|
|
|
m_writeEtcHosts = CalamaresUtils::getBool( hostnameSettings, "writeHostsFile", true );
|
2022-04-11 14:12:05 +02:00
|
|
|
m_hostnameTemplate
|
|
|
|
= CalamaresUtils::getString( hostnameSettings, "template", QStringLiteral( "${first}-${product}" ) );
|
2022-05-09 14:49:15 +02:00
|
|
|
|
|
|
|
m_forbiddenHostNames = CalamaresUtils::getStringList( hostnameSettings, "forbidden_names" );
|
2022-05-18 14:40:35 +02:00
|
|
|
m_forbiddenHostNames << alwaysForbiddenHostNames();
|
2022-05-09 14:49:15 +02:00
|
|
|
tidy( m_forbiddenHostNames );
|
2022-04-08 10:45:50 +02:00
|
|
|
}
|
2020-08-05 10:29:13 +02:00
|
|
|
|
2020-07-29 12:18:25 +02:00
|
|
|
setConfigurationDefaultGroups( configurationMap, m_defaultGroups );
|
2021-04-02 13:11:16 +02:00
|
|
|
|
|
|
|
// Renaming of Autologin -> AutoLogin in 4ffa79d4cf also affected
|
|
|
|
// configuration keys, which was not intended. Accept both.
|
2021-04-13 15:08:13 +02:00
|
|
|
m_doAutoLogin = either( CalamaresUtils::getBool,
|
|
|
|
configurationMap,
|
|
|
|
QStringLiteral( "doAutologin" ),
|
|
|
|
QStringLiteral( "doAutoLogin" ),
|
|
|
|
false );
|
2020-07-28 11:59:53 +02:00
|
|
|
|
|
|
|
m_writeRootPassword = CalamaresUtils::getBool( configurationMap, "setRootPassword", true );
|
|
|
|
Calamares::JobQueue::instance()->globalStorage()->insert( "setRootPassword", m_writeRootPassword );
|
2020-08-04 22:16:48 +02:00
|
|
|
|
|
|
|
m_reuseUserPasswordForRoot = CalamaresUtils::getBool( configurationMap, "doReusePassword", false );
|
|
|
|
|
|
|
|
m_permitWeakPasswords = CalamaresUtils::getBool( configurationMap, "allowWeakPasswords", false );
|
|
|
|
m_requireStrongPasswords
|
|
|
|
= !m_permitWeakPasswords || !CalamaresUtils::getBool( configurationMap, "allowWeakPasswordsDefault", false );
|
2020-08-05 13:03:18 +02:00
|
|
|
|
|
|
|
// If the value doesn't exist, or isn't a map, this gives an empty map -- no problem
|
|
|
|
auto pr_checks( configurationMap.value( "passwordRequirements" ).toMap() );
|
|
|
|
for ( decltype( pr_checks )::const_iterator i = pr_checks.constBegin(); i != pr_checks.constEnd(); ++i )
|
|
|
|
{
|
|
|
|
addPasswordCheck( i.key(), i.value(), m_passwordChecks );
|
|
|
|
}
|
|
|
|
std::sort( m_passwordChecks.begin(), m_passwordChecks.end() );
|
2020-08-18 11:21:53 +02:00
|
|
|
|
2020-08-28 23:20:02 +02:00
|
|
|
updateGSAutoLogin( doAutoLogin(), loginName() );
|
2020-08-18 11:21:53 +02:00
|
|
|
checkReady();
|
2021-03-12 13:25:16 +01:00
|
|
|
|
2021-03-12 18:42:37 +01:00
|
|
|
ApplyPresets( *this, configurationMap ) << "fullName"
|
|
|
|
<< "loginName";
|
2020-07-25 12:47:01 +02:00
|
|
|
}
|
2020-08-17 15:30:09 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
Config::finalizeGlobalStorage() const
|
|
|
|
{
|
2020-08-28 23:20:02 +02:00
|
|
|
updateGSAutoLogin( doAutoLogin(), loginName() );
|
2020-08-17 15:30:09 +02:00
|
|
|
|
2020-08-28 23:20:02 +02:00
|
|
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
2020-08-17 15:30:09 +02:00
|
|
|
if ( writeRootPassword() )
|
|
|
|
{
|
|
|
|
gs->insert( "reuseRootPassword", reuseUserPasswordForRoot() );
|
|
|
|
}
|
|
|
|
gs->insert( "password", CalamaresUtils::obscure( userPassword() ) );
|
|
|
|
}
|
2020-08-18 11:31:32 +02:00
|
|
|
|
|
|
|
Calamares::JobList
|
|
|
|
Config::createJobs() const
|
|
|
|
{
|
|
|
|
Calamares::JobList jobs;
|
|
|
|
|
|
|
|
if ( !isReady() )
|
|
|
|
{
|
|
|
|
return jobs;
|
|
|
|
}
|
|
|
|
|
|
|
|
Calamares::Job* j;
|
|
|
|
|
2020-11-06 22:27:46 +01:00
|
|
|
if ( !m_sudoersGroup.isEmpty() )
|
2020-10-21 14:43:45 +02:00
|
|
|
{
|
2022-02-21 15:49:10 +01:00
|
|
|
j = new SetupSudoJob( m_sudoersGroup, m_sudoStyle );
|
2020-10-21 14:43:45 +02:00
|
|
|
jobs.append( Calamares::job_ptr( j ) );
|
|
|
|
}
|
|
|
|
|
2020-10-22 14:11:01 +02:00
|
|
|
j = new SetupGroupsJob( this );
|
|
|
|
jobs.append( Calamares::job_ptr( j ) );
|
|
|
|
|
2020-10-14 00:23:00 +02:00
|
|
|
j = new CreateUserJob( this );
|
2020-08-18 11:31:32 +02:00
|
|
|
jobs.append( Calamares::job_ptr( j ) );
|
|
|
|
|
|
|
|
j = new SetPasswordJob( loginName(), userPassword() );
|
|
|
|
jobs.append( Calamares::job_ptr( j ) );
|
|
|
|
|
|
|
|
j = new SetPasswordJob( "root", rootPassword() );
|
|
|
|
jobs.append( Calamares::job_ptr( j ) );
|
|
|
|
|
2022-04-11 11:10:40 +02:00
|
|
|
j = new SetHostNameJob( this );
|
2020-08-18 11:31:32 +02:00
|
|
|
jobs.append( Calamares::job_ptr( j ) );
|
|
|
|
|
|
|
|
return jobs;
|
|
|
|
}
|