6 #include "validatorpwquality_p.h"
10 #include <QLoggingCategory>
40 pwquality_settings_t *pwq = pwquality_default_settings();
43 bool optionsSet =
false;
46 const QVariantMap map = options.
toMap();
48 auto i = map.constBegin();
49 while (i != map.constEnd()) {
53 QList<char> buf(ValidatorPwQualityPrivate::errStrBufSize);
54 qCWarning(C_VALIDATOR).noquote().nospace()
55 <<
"ValidatorPwQuality: Failed to set pwquality option " << opt
57 << pwquality_strerror(buf.
data(), buf.
size(), orv,
nullptr);
66 if (C_VALIDATOR().isWarningEnabled()) {
67 void *auxerror =
nullptr;
68 const int rcrv = pwquality_read_config(
71 QList<char> buf(ValidatorPwQualityPrivate::errStrBufSize);
72 qCWarning(C_VALIDATOR).noquote().nospace()
73 <<
"ValidatorPwQuality: Failed to read configuration file "
75 << pwquality_strerror(buf.
data(), buf.
size(), rcrv, auxerror);
86 if (C_VALIDATOR().isWarningEnabled()) {
87 void *auxerror =
nullptr;
88 const int rcrv = pwquality_read_config(pwq,
nullptr, &auxerror);
90 QList<char> buf(ValidatorPwQualityPrivate::errStrBufSize);
91 qCWarning(C_VALIDATOR).noquote()
92 <<
"VaidatorPwQuality: Failed to read default configuration file:"
93 << pwquality_strerror(buf.
data(), buf.
size(), rcrv, auxerror);
96 pwquality_read_config(pwq,
nullptr,
nullptr);
107 rv = pwquality_check(pwq, pw, opw, u,
nullptr);
109 pwquality_free_settings(pwq);
112 rv = PWQ_ERROR_MEM_ALLOC;
115 rv = PWQ_ERROR_EMPTY_PASSWORD;
127 switch (returnValue) {
128 case PWQ_ERROR_MEM_ALLOC:
130 return c->
qtTrId(
"cutelyst-valpwq-err-memalloc");
131 case PWQ_ERROR_SAME_PASSWORD:
133 return c->
qtTrId(
"cutelyst-valpwq-err-samepass");
134 case PWQ_ERROR_PALINDROME:
136 return c->
qtTrId(
"cutelyst-valpwq-err-palindrome");
137 case PWQ_ERROR_CASE_CHANGES_ONLY:
139 return c->
qtTrId(
"cutelyst-valpwq-err-casechangesonly");
140 case PWQ_ERROR_TOO_SIMILAR:
142 return c->
qtTrId(
"cutelyst-valpwq-err-toosimilar");
143 case PWQ_ERROR_USER_CHECK:
145 return c->
qtTrId(
"cutelyst-valpwq-err-usercheck");
146 case PWQ_ERROR_GECOS_CHECK:
148 return c->
qtTrId(
"cutelyst-valpwq-err-gecoscheck");
149 case PWQ_ERROR_BAD_WORDS:
151 return c->
qtTrId(
"cutelyst-valpwq-err-badwords");
152 case PWQ_ERROR_MIN_DIGITS:
154 return c->
qtTrId(
"cutelyst-valpwq-err-mindigits");
155 case PWQ_ERROR_MIN_UPPERS:
157 return c->
qtTrId(
"cutelyst-valpwq-err-minuppers");
158 case PWQ_ERROR_MIN_LOWERS:
160 return c->
qtTrId(
"cutelyst-valpwq-err-minlowers");
161 case PWQ_ERROR_MIN_OTHERS:
163 return c->
qtTrId(
"cutelyst-valpwq-err-minothers");
164 case PWQ_ERROR_MIN_LENGTH:
166 return c->
qtTrId(
"cutelyst-valpwq-err-minlength");
167 case PWQ_ERROR_ROTATED:
169 return c->
qtTrId(
"cutelyst-valpwq-err-rotated");
170 case PWQ_ERROR_MIN_CLASSES:
172 return c->
qtTrId(
"cutelyst-valpwq-err-minclasses");
173 case PWQ_ERROR_MAX_CONSECUTIVE:
175 return c->
qtTrId(
"cutelyst-valpwq-err-maxconsecutive");
176 case PWQ_ERROR_MAX_CLASS_REPEAT:
178 return c->
qtTrId(
"cutelyst-valpwq-err-maxclassrepeat");
179 case PWQ_ERROR_MAX_SEQUENCE:
181 return c->
qtTrId(
"cutelyst-valpwq-err-maxsequence");
182 case PWQ_ERROR_EMPTY_PASSWORD:
184 return c->
qtTrId(
"cutelyst-valpwq-err-emptypw");
188 return c->
qtTrId(
"cutelyst-valpwq-err-rng");
189 case PWQ_ERROR_CRACKLIB_CHECK:
191 return c->
qtTrId(
"cutelyst-valpwq-err-cracklibcheck");
192 case PWQ_ERROR_UNKNOWN_SETTING:
194 return c->
qtTrId(
"cutelyst-valpwq-err-unknownsetting");
195 case PWQ_ERROR_INTEGER:
197 return c->
qtTrId(
"cutelyst-valpwq-err-integer");
198 case PWQ_ERROR_NON_INT_SETTING:
201 return c->
qtTrId(
"cutelyst-valpwq-err-nonintsetting");
202 case PWQ_ERROR_NON_STR_SETTING:
204 return c->
qtTrId(
"cutelyst-valpwq-err-nonstrsetting");
205 case PWQ_ERROR_CFGFILE_OPEN:
207 return c->
qtTrId(
"cutelyst-valpwq-err-cfgfileopen");
208 case PWQ_ERROR_CFGFILE_MALFORMED:
210 return c->
qtTrId(
"cutelyst-valpwq-err-cfgfilemalformed");
211 case PWQ_ERROR_FATAL_FAILURE:
213 return c->
qtTrId(
"cutelyst-valpwq-err-fatalfailure");
216 if (returnValue < 0) {
218 return c->
qtTrId(
"cutelyst-valpwq-err-unknown");
220 if (returnValue < threshold) {
222 return c->
qtTrId(
"cutelyst-valpwq-err-belowthreshold")
231 switch (returnValue) {
232 case PWQ_ERROR_MEM_ALLOC:
235 return c->
qtTrId(
"cutelyst-valpwq-err-memalloc-label").
arg(
label);
236 case PWQ_ERROR_SAME_PASSWORD:
238 return c->
qtTrId(
"cutelyst-valpwq-err-samepass-label").
arg(
label);
239 case PWQ_ERROR_PALINDROME:
241 return c->
qtTrId(
"cutelyst-valpwq-err-palindrome-label").
arg(
label);
242 case PWQ_ERROR_CASE_CHANGES_ONLY:
244 return c->
qtTrId(
"cutelyst-valpwq-err-casechangesonly-label").
arg(
label);
245 case PWQ_ERROR_TOO_SIMILAR:
247 return c->
qtTrId(
"cutelyst-valpwq-err-toosimilar-label").
arg(
label);
248 case PWQ_ERROR_USER_CHECK:
250 return c->
qtTrId(
"cutelyst-valpwq-err-usercheck-label").
arg(
label);
251 case PWQ_ERROR_GECOS_CHECK:
254 return c->
qtTrId(
"cutelyst-valpwq-err-gecoscheck-label").
arg(
label);
255 case PWQ_ERROR_BAD_WORDS:
257 return c->
qtTrId(
"cutelyst-valpwq-err-badwords-label").
arg(
label);
258 case PWQ_ERROR_MIN_DIGITS:
260 return c->
qtTrId(
"cutelyst-valpwq-err-mindigits-label").
arg(
label);
261 case PWQ_ERROR_MIN_UPPERS:
263 return c->
qtTrId(
"cutelyst-valpwq-err-minuppers-label").
arg(
label);
264 case PWQ_ERROR_MIN_LOWERS:
266 return c->
qtTrId(
"cutelyst-valpwq-err-minlowers-label").
arg(
label);
267 case PWQ_ERROR_MIN_OTHERS:
270 return c->
qtTrId(
"cutelyst-valpwq-err-minothers-label").
arg(
label);
271 case PWQ_ERROR_MIN_LENGTH:
273 return c->
qtTrId(
"cutelyst-valpwq-err-minlength-label").
arg(
label);
274 case PWQ_ERROR_ROTATED:
277 case PWQ_ERROR_MIN_CLASSES:
279 return c->
qtTrId(
"cutelyst-valpwq-err-minclasses-label").
arg(
label);
280 case PWQ_ERROR_MAX_CONSECUTIVE:
283 return c->
qtTrId(
"cutelyst-valpwq-err-maxconsecutive-label").
arg(
label);
284 case PWQ_ERROR_MAX_CLASS_REPEAT:
287 return c->
qtTrId(
"cutelyst-valpwq-err-maxclassrepeat-label").
arg(
label);
288 case PWQ_ERROR_MAX_SEQUENCE:
291 return c->
qtTrId(
"cutelyst-valpwq-err-maxsequence-label").
arg(
label);
292 case PWQ_ERROR_EMPTY_PASSWORD:
299 case PWQ_ERROR_CRACKLIB_CHECK:
301 return c->
qtTrId(
"cutelyst-valpwq-err-cracklibcheck-label").
arg(
label);
302 case PWQ_ERROR_UNKNOWN_SETTING:
305 return c->
qtTrId(
"cutelyst-valpwq-err-unknownsetting-label").
arg(
label);
306 case PWQ_ERROR_INTEGER:
310 case PWQ_ERROR_NON_INT_SETTING:
313 return c->
qtTrId(
"cutelyst-valpwq-err-nonintsetting-label").
arg(
label);
314 case PWQ_ERROR_NON_STR_SETTING:
317 return c->
qtTrId(
"cutelyst-valpwq-err-nonstrsetting-label").
arg(
label);
318 case PWQ_ERROR_CFGFILE_OPEN:
321 return c->
qtTrId(
"cutelyst-valpwq-err-cfgfileopen-label").
arg(
label);
322 case PWQ_ERROR_CFGFILE_MALFORMED:
325 return c->
qtTrId(
"cutelyst-valpwq-err-cfgfilemalformed-label").
arg(
label);
326 case PWQ_ERROR_FATAL_FAILURE:
328 return c->
qtTrId(
"cutelyst-valpwq-err-fatalfailure-label").
arg(
label);
331 if (returnValue < 0) {
336 if (returnValue < threshold) {
339 return c->
qtTrId(
"cutelyst-valpwq-err-belowthreshold-label")
359 if (d->options.isValid()) {
363 const QString optString = d->options.toString();
364 if (c->
stash().contains(optString)) {
365 opts = c->
stash(optString);
372 if (!d->userName.isEmpty()) {
373 un = params.
value(d->userName);
375 un = c->
stash(d->userName).toString();
379 if (!d->oldPassword.isEmpty()) {
380 opw = params.
value(d->oldPassword);
382 opw = c->
stash(d->oldPassword).toString();
385 int rv =
validate(v, opts, opw, un);
386 if (rv < d->threshold) {
388 if (C_VALIDATOR().isDebugEnabled()) {
390 QList<char> buf(ValidatorPwQualityPrivate::errStrBufSize);
391 qCDebug(C_VALIDATOR).noquote()
393 << pwquality_strerror(buf.
data(), buf.
size(), rv,
nullptr);
395 qCDebug(C_VALIDATOR).noquote() <<
debugString(c) <<
"The quality score" << rv
396 <<
"is below the threshold of" << d->threshold;
400 qCDebug(C_VALIDATOR).noquote()
401 <<
"ValidatorPwQuality: \"" << v <<
"\" got a quality score of" << rv;
void stash(const QVariantHash &unite)
QString qtTrId(const char *id, int n=-1) const
Validates an input field with libpwquality to check password quality.
static QString errorString(Context *c, int returnValue, const QString &label=QString(), int threshold=0)
QString genericValidationError(Context *c, const QVariant &errorData) const override
~ValidatorPwQuality() override
ValidatorPwQuality(const QString &field, int threshold=ValidatorPwQuality::defaultThreshold, const QVariant &options=QVariant(), const QString &userName=QString(), const QString &oldPassword=QString(), const ValidatorMessages &messages=ValidatorMessages())
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
QString value(const ParamsMultiMap ¶ms) const
QString debugString(Context *c) const
static int validate(const QString &value, const QVariant &options=QVariant(), const QString &oldPassword=QString(), const QString &user=QString())
Returns the password quality score for value.
The Cutelyst namespace holds all public Cutelyst API.
const char * constData() const const
bool isEmpty() const const
qsizetype size() const const
T value(const Key &key, const T &defaultValue) const const
QString arg(Args &&... args) const const
bool isEmpty() const const
QString number(double n, char format, int precision)
QByteArray toUtf8() const const
bool isValid() const const
int toInt(bool *ok) const const
QMap< QString, QVariant > toMap() const const
QString toString() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.