mirror of https://github.com/cutefishos/calamares
commit
32b10f72e9
@ -0,0 +1,37 @@
|
||||
# Locate libpwquality
|
||||
# https://github.com/libpwquality/libpwquality
|
||||
#
|
||||
# This module defines
|
||||
# LibPWQuality_FOUND
|
||||
# LibPWQuality_LIBRARIES, where to find the library
|
||||
# LibPWQuality_INCLUDE_DIRS, where to find pwquality.h
|
||||
#
|
||||
include(FindPkgConfig)
|
||||
include(FindPackageHandleStandardArgs)
|
||||
|
||||
pkg_search_module(pc_pwquality QUIET pwquality)
|
||||
|
||||
find_path(LibPWQuality_INCLUDE_DIR
|
||||
NAMES pwquality.h
|
||||
PATHS ${pc_pwquality_INCLUDE_DIRS}
|
||||
)
|
||||
find_library(LibPWQuality_LIBRARY
|
||||
NAMES pwquality
|
||||
PATHS ${pc_pwquality_LIBRARY_DIRS}
|
||||
)
|
||||
if(pc_pwquality_FOUND)
|
||||
set(LibPWQuality_LIBRARIES ${LibPWQuality_LIBRARY})
|
||||
set(LibPWQuality_INCLUDE_DIRS ${LibPWQuality_INCLUDE_DIR} ${pc_pwquality_INCLUDE_DIRS})
|
||||
endif()
|
||||
|
||||
find_package_handle_standard_args(LibPWQuality DEFAULT_MSG
|
||||
LibPWQuality_INCLUDE_DIRS
|
||||
LibPWQuality_LIBRARIES
|
||||
)
|
||||
mark_as_advanced(LibPWQuality_INCLUDE_DIRS LibPWQuality_LIBRARIES)
|
||||
|
||||
set_package_properties(
|
||||
LibPWQuality PROPERTIES
|
||||
DESCRIPTION "Password quality checking library"
|
||||
URL "https://github.com/libpwquality/libpwquality"
|
||||
)
|
@ -0,0 +1,347 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* 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 "CheckPWQuality.h"
|
||||
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include <QString>
|
||||
#include <QWidget>
|
||||
|
||||
#ifdef HAVE_LIBPWQUALITY
|
||||
#include <pwquality.h>
|
||||
#endif
|
||||
|
||||
#include <memory>
|
||||
|
||||
static void _default_cleanup()
|
||||
{
|
||||
}
|
||||
|
||||
PasswordCheck::PasswordCheck()
|
||||
: m_message()
|
||||
, m_accept( []( const QString& s ){ return true; } )
|
||||
{
|
||||
}
|
||||
|
||||
PasswordCheck::PasswordCheck( const QString& m, AcceptFunc a )
|
||||
: m_message( [m](){ return m; } )
|
||||
, m_accept( a )
|
||||
{
|
||||
}
|
||||
|
||||
PasswordCheck::PasswordCheck( MessageFunc m, AcceptFunc a )
|
||||
: m_message( m )
|
||||
, m_accept( a )
|
||||
{
|
||||
}
|
||||
|
||||
// Try to trick Transifex into accepting these strings
|
||||
#define tr parent->tr
|
||||
struct LengthExplainer
|
||||
{
|
||||
static QString too_short( QWidget* parent )
|
||||
{
|
||||
return tr( "Password is too short" );
|
||||
}
|
||||
|
||||
static QString too_long( QWidget* parent )
|
||||
{
|
||||
return tr( "Password is too long" );
|
||||
}
|
||||
} ;
|
||||
#undef tr
|
||||
|
||||
DEFINE_CHECK_FUNC( minLength )
|
||||
{
|
||||
int minLength = -1;
|
||||
if ( value.canConvert( QVariant::Int ) )
|
||||
minLength = value.toInt();
|
||||
if ( minLength > 0 )
|
||||
{
|
||||
cDebug() << " .. minLength set to" << minLength;
|
||||
checks.push_back(
|
||||
PasswordCheck(
|
||||
[parent]()
|
||||
{
|
||||
return LengthExplainer::too_short( parent );
|
||||
},
|
||||
[minLength]( const QString& s )
|
||||
{
|
||||
return s.length() >= minLength;
|
||||
}
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
DEFINE_CHECK_FUNC( maxLength )
|
||||
{
|
||||
int maxLength = -1;
|
||||
if ( value.canConvert( QVariant::Int ) )
|
||||
maxLength = value.toInt();
|
||||
if ( maxLength > 0 )
|
||||
{
|
||||
cDebug() << " .. maxLength set to" << maxLength;
|
||||
checks.push_back(
|
||||
PasswordCheck(
|
||||
[parent]()
|
||||
{
|
||||
return LengthExplainer::too_long( parent );
|
||||
},
|
||||
[maxLength]( const QString& s )
|
||||
{
|
||||
return s.length() <= maxLength;
|
||||
}
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBPWQUALITY
|
||||
/**
|
||||
* Class that acts as a RAII placeholder for pwquality_settings_t pointers.
|
||||
* Gets a new pointer and ensures it is deleted only once; provides
|
||||
* convenience functions for setting options and checking passwords.
|
||||
*/
|
||||
class PWSettingsHolder
|
||||
{
|
||||
public:
|
||||
static constexpr int arbitrary_minimum_strength = 40;
|
||||
|
||||
PWSettingsHolder()
|
||||
: m_settings( pwquality_default_settings() )
|
||||
, m_auxerror( nullptr )
|
||||
{
|
||||
}
|
||||
|
||||
~PWSettingsHolder()
|
||||
{
|
||||
cDebug() << "Freeing PWQ@" << ( void* )m_settings;
|
||||
pwquality_free_settings( m_settings );
|
||||
}
|
||||
|
||||
/// Sets an option via the configuration string @p v, <key>=<value> style.
|
||||
int set( const QString& v )
|
||||
{
|
||||
return pwquality_set_option( m_settings, v.toUtf8().constData() );
|
||||
}
|
||||
|
||||
/// Checks the given password @p pwd against the current configuration
|
||||
int check( const QString& pwd )
|
||||
{
|
||||
void* auxerror = nullptr;
|
||||
int r = pwquality_check( m_settings, pwd.toUtf8().constData(), nullptr, nullptr, &auxerror );
|
||||
m_rv = r;
|
||||
return r;
|
||||
}
|
||||
|
||||
bool hasExplanation() const
|
||||
{
|
||||
return m_rv < 0;
|
||||
}
|
||||
|
||||
#define tr parent->tr
|
||||
/* This is roughly the same as the function pwquality_strerror,
|
||||
* only with QStrings instead, and using the Qt translation scheme.
|
||||
* It is used under the terms of the GNU GPL v3 or later, as
|
||||
* allowed by the libpwquality license (LICENSES/GPLv2+-libpwquality)
|
||||
*/
|
||||
QString explanation( QWidget* parent )
|
||||
{
|
||||
void* auxerror = m_auxerror;
|
||||
m_auxerror = nullptr;
|
||||
|
||||
if ( m_rv >= arbitrary_minimum_strength )
|
||||
return QString();
|
||||
if ( m_rv >= 0 )
|
||||
return tr( "Password is too weak" );
|
||||
|
||||
switch ( m_rv )
|
||||
{
|
||||
case PWQ_ERROR_MEM_ALLOC:
|
||||
if ( auxerror )
|
||||
{
|
||||
QString s = tr( "Memory allocation error when setting '%1'" ).arg( ( const char* )auxerror );
|
||||
free( auxerror );
|
||||
return s;
|
||||
}
|
||||
return tr( "Memory allocation error" );
|
||||
case PWQ_ERROR_SAME_PASSWORD:
|
||||
return tr( "The password is the same as the old one" );
|
||||
case PWQ_ERROR_PALINDROME:
|
||||
return tr( "The password is a palindrome" );
|
||||
case PWQ_ERROR_CASE_CHANGES_ONLY:
|
||||
return tr( "The password differs with case changes only" );
|
||||
case PWQ_ERROR_TOO_SIMILAR:
|
||||
return tr( "The password is too similar to the old one" );
|
||||
case PWQ_ERROR_USER_CHECK:
|
||||
return tr( "The password contains the user name in some form" );
|
||||
case PWQ_ERROR_GECOS_CHECK:
|
||||
return tr( "The password contains words from the real name of the user in some form" );
|
||||
case PWQ_ERROR_BAD_WORDS:
|
||||
return tr( "The password contains forbidden words in some form" );
|
||||
case PWQ_ERROR_MIN_DIGITS:
|
||||
if ( auxerror )
|
||||
return tr( "The password contains less than %1 digits" ).arg( ( long )auxerror );
|
||||
return tr( "The password contains too few digits" );
|
||||
case PWQ_ERROR_MIN_UPPERS:
|
||||
if ( auxerror )
|
||||
return tr( "The password contains less than %1 uppercase letters" ).arg( ( long )auxerror );
|
||||
return tr( "The password contains too few uppercase letters" );
|
||||
case PWQ_ERROR_MIN_LOWERS:
|
||||
if ( auxerror )
|
||||
return tr( "The password contains less than %1 lowercase letters" ).arg( ( long )auxerror );
|
||||
return tr( "The password contains too few lowercase letters" );
|
||||
case PWQ_ERROR_MIN_OTHERS:
|
||||
if ( auxerror )
|
||||
return tr( "The password contains less than %1 non-alphanumeric characters" ).arg( ( long )auxerror );
|
||||
return tr( "The password contains too few non-alphanumeric characters" );
|
||||
case PWQ_ERROR_MIN_LENGTH:
|
||||
if ( auxerror )
|
||||
return tr( "The password is shorter than %1 characters" ).arg( ( long )auxerror );
|
||||
return tr( "The password is too short" );
|
||||
case PWQ_ERROR_ROTATED:
|
||||
return tr( "The password is just rotated old one" );
|
||||
case PWQ_ERROR_MIN_CLASSES:
|
||||
if ( auxerror )
|
||||
return tr( "The password contains less than %1 character classes" ).arg( ( long )auxerror );
|
||||
return tr( "The password does not contain enough character classes" );
|
||||
case PWQ_ERROR_MAX_CONSECUTIVE:
|
||||
if ( auxerror )
|
||||
return tr( "The password contains more than %1 same characters consecutively" ).arg( ( long )auxerror );
|
||||
return tr( "The password contains too many same characters consecutively" );
|
||||
case PWQ_ERROR_MAX_CLASS_REPEAT:
|
||||
if ( auxerror )
|
||||
return tr( "The password contains more than %1 characters of the same class consecutively" ).arg( ( long )auxerror );
|
||||
return tr( "The password contains too many characters of the same class consecutively" );
|
||||
case PWQ_ERROR_MAX_SEQUENCE:
|
||||
if ( auxerror )
|
||||
return tr( "The password contains monotonic sequence longer than %1 characters" ).arg( ( long )auxerror );
|
||||
return tr( "The password contains too long of a monotonic character sequence" );
|
||||
case PWQ_ERROR_EMPTY_PASSWORD:
|
||||
return tr( "No password supplied" );
|
||||
case PWQ_ERROR_RNG:
|
||||
return tr( "Cannot obtain random numbers from the RNG device" );
|
||||
case PWQ_ERROR_GENERATION_FAILED:
|
||||
return tr( "Password generation failed - required entropy too low for settings" );
|
||||
case PWQ_ERROR_CRACKLIB_CHECK:
|
||||
if ( auxerror )
|
||||
{
|
||||
/* Here the string comes from cracklib, don't free? */
|
||||
return tr( "The password fails the dictionary check - %1" ).arg( ( const char* )auxerror );
|
||||
}
|
||||
return tr( "The password fails the dictionary check" );
|
||||
case PWQ_ERROR_UNKNOWN_SETTING:
|
||||
if ( auxerror )
|
||||
{
|
||||
QString s = tr( "Unknown setting - %1" ).arg( ( const char* )auxerror );
|
||||
free( auxerror );
|
||||
return s;
|
||||
}
|
||||
return tr( "Unknown setting" );
|
||||
case PWQ_ERROR_INTEGER:
|
||||
if ( auxerror )
|
||||
{
|
||||
QString s = tr( "Bad integer value of setting - %1" ).arg( ( const char* )auxerror );
|
||||
free( auxerror );
|
||||
return s;
|
||||
}
|
||||
return tr( "Bad integer value" );
|
||||
case PWQ_ERROR_NON_INT_SETTING:
|
||||
if ( auxerror )
|
||||
{
|
||||
QString s = tr( "Setting %1 is not of integer type" ).arg( ( const char* )auxerror );
|
||||
free( auxerror );
|
||||
return s;
|
||||
}
|
||||
return tr( "Setting is not of integer type" );
|
||||
case PWQ_ERROR_NON_STR_SETTING:
|
||||
if ( auxerror )
|
||||
{
|
||||
QString s = tr( "Setting %1 is not of string type" ).arg( ( const char* )auxerror );
|
||||
free( auxerror );
|
||||
return s;
|
||||
}
|
||||
return tr( "Setting is not of string type" );
|
||||
case PWQ_ERROR_CFGFILE_OPEN:
|
||||
return tr( "Opening the configuration file failed" );
|
||||
case PWQ_ERROR_CFGFILE_MALFORMED:
|
||||
return tr( "The configuration file is malformed" );
|
||||
case PWQ_ERROR_FATAL_FAILURE:
|
||||
return tr( "Fatal failure" );
|
||||
default:
|
||||
return tr( "Unknown error" );
|
||||
}
|
||||
}
|
||||
#undef tr
|
||||
private:
|
||||
pwquality_settings_t* m_settings;
|
||||
int m_rv;
|
||||
void* m_auxerror;
|
||||
} ;
|
||||
|
||||
DEFINE_CHECK_FUNC( libpwquality )
|
||||
{
|
||||
if ( !value.canConvert( QVariant::List ) )
|
||||
{
|
||||
cDebug() << "WARNING: libpwquality settings is not a list";
|
||||
return;
|
||||
}
|
||||
|
||||
QVariantList l = value.toList();
|
||||
unsigned int requirement_count = 0;
|
||||
auto settings = std::make_shared<PWSettingsHolder>();
|
||||
for ( const auto& v : l )
|
||||
{
|
||||
if ( v.type() == QVariant::String )
|
||||
{
|
||||
QString option = v.toString();
|
||||
int r = settings->set( option );
|
||||
if ( r )
|
||||
cDebug() << " .. WARNING: unrecognized libpwquality setting" << option;
|
||||
else
|
||||
{
|
||||
cDebug() << " .. libpwquality setting" << option;
|
||||
++requirement_count;
|
||||
}
|
||||
}
|
||||
else
|
||||
cDebug() << " .. WARNING: unrecognized libpwquality setting" << v;
|
||||
}
|
||||
|
||||
/* Something actually added? */
|
||||
if ( requirement_count )
|
||||
{
|
||||
checks.push_back(
|
||||
PasswordCheck(
|
||||
[parent,settings]()
|
||||
{
|
||||
return settings->explanation( parent );
|
||||
},
|
||||
[settings]( const QString& s )
|
||||
{
|
||||
int r = settings->check( s );
|
||||
if ( r < 0 )
|
||||
cDebug() << "WARNING: libpwquality error" << r;
|
||||
else if ( r < settings->arbitrary_minimum_strength )
|
||||
cDebug() << "Password strength" << r << "too low";
|
||||
return r >= settings->arbitrary_minimum_strength;
|
||||
}
|
||||
) );
|
||||
}
|
||||
}
|
||||
#endif
|
@ -0,0 +1,83 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef CHECKPWQUALITY_H
|
||||
#define CHECKPWQUALITY_H
|
||||
|
||||
#include <QString>
|
||||
#include <QVariant>
|
||||
#include <QVector>
|
||||
|
||||
#include <functional>
|
||||
|
||||
/**
|
||||
* Support for (dynamic) checks on the password's validity.
|
||||
* This can be used to implement password requirements like
|
||||
* "at least 6 characters". Function addPasswordCheck()
|
||||
* instantiates these and adds them to the list of checks.
|
||||
*/
|
||||
class PasswordCheck
|
||||
{
|
||||
public:
|
||||
/** Return true if the string is acceptable. */
|
||||
using AcceptFunc = std::function<bool( const QString& )>;
|
||||
using MessageFunc = std::function<QString()>;
|
||||
|
||||
/** Generate a @p message if @p filter returns true */
|
||||
PasswordCheck( MessageFunc message, AcceptFunc filter );
|
||||
/** Yields @p message if @p filter returns true */
|
||||
PasswordCheck( const QString& message, AcceptFunc filter );
|
||||
/** Null check, always returns empty */
|
||||
PasswordCheck();
|
||||
|
||||
/** Applies this check to the given password string @p s
|
||||
* and returns an empty string if the password is ok
|
||||
* according to this filter. Returns a message describing
|
||||
* what is wrong if not.
|
||||
*/
|
||||
QString filter( const QString& s ) const
|
||||
{
|
||||
return m_accept( s ) ? QString() : m_message();
|
||||
}
|
||||
|
||||
private:
|
||||
MessageFunc m_message;
|
||||
AcceptFunc m_accept;
|
||||
} ;
|
||||
|
||||
using PasswordCheckList = QVector<PasswordCheck>;
|
||||
|
||||
/* Each of these functions adds a check (if possible) to the list
|
||||
* of checks; they use the configuration value(s) from the
|
||||
* variant. If the value doesn't make sense, each function
|
||||
* may skip adding a check, and do nothing (it should log
|
||||
* an error, though).
|
||||
*/
|
||||
#define _xDEFINE_CHECK_FUNC(x) \
|
||||
add_check_##x( QWidget* parent, PasswordCheckList& checks, const QVariant& value )
|
||||
#define DEFINE_CHECK_FUNC(x) void _xDEFINE_CHECK_FUNC(x)
|
||||
#define DECLARE_CHECK_FUNC(x) void _xDEFINE_CHECK_FUNC(x);
|
||||
|
||||
DECLARE_CHECK_FUNC(minLength)
|
||||
DECLARE_CHECK_FUNC(maxLength)
|
||||
#ifdef HAVE_LIBPWQUALITY
|
||||
DECLARE_CHECK_FUNC(libpwquality)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue