]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/home/user-record-pwquality.c
home: make libpwquality dep a runtime dlopen() one
[thirdparty/systemd.git] / src / home / user-record-pwquality.c
CommitLineData
679badd7
LP
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include "bus-common-errors.h"
4#include "errno-util.h"
5#include "home-util.h"
6#include "pwquality-util.h"
7#include "strv.h"
8#include "user-record-pwquality.h"
9#include "user-record-util.h"
10
11#if HAVE_PWQUALITY
12
13int user_record_quality_check_password(
14 UserRecord *hr,
15 UserRecord *secret,
16 sd_bus_error *error) {
17
18 _cleanup_(sym_pwquality_free_settingsp) pwquality_settings_t *pwq = NULL;
19 char buf[PWQ_MAX_ERROR_MESSAGE_LEN], **pp;
20 void *auxerror;
21 int r;
22
23 assert(hr);
24 assert(secret);
25
26 r = pwq_allocate_context(&pwq);
27 if (ERRNO_IS_NOT_SUPPORTED(r))
28 return 0;
29 if (r < 0)
30 return log_debug_errno(r, "Failed to allocate libpwquality context: %m");
31
32 /* This is a bit more complex than one might think at first. pwquality_check() would like to know the
33 * old password to make security checks. We support arbitrary numbers of passwords however, hence we
34 * call the function once for each combination of old and new password. */
35
36 /* Iterate through all new passwords */
37 STRV_FOREACH(pp, secret->password) {
38 bool called = false;
39 char **old;
40
41 r = test_password_many(hr->hashed_password, *pp);
42 if (r < 0)
43 return r;
44 if (r == 0) /* This is an old password as it isn't listed in the hashedPassword field, skip it */
45 continue;
46
47 /* Check this password against all old passwords */
48 STRV_FOREACH(old, secret->password) {
49
50 if (streq(*pp, *old))
51 continue;
52
53 r = test_password_many(hr->hashed_password, *old);
54 if (r < 0)
55 return r;
56 if (r > 0) /* This is a new password, not suitable as old password */
57 continue;
58
59 r = sym_pwquality_check(pwq, *pp, *old, hr->user_name, &auxerror);
60 if (r < 0)
61 return sd_bus_error_setf(error, BUS_ERROR_LOW_PASSWORD_QUALITY, "Password too weak: %s",
62 sym_pwquality_strerror(buf, sizeof(buf), r, auxerror));
63
64 called = true;
65 }
66
67 if (called)
68 continue;
69
70 /* If there are no old passwords, let's call pwquality_check() without any. */
71 r = sym_pwquality_check(pwq, *pp, NULL, hr->user_name, &auxerror);
72 if (r < 0)
73 return sd_bus_error_setf(error, BUS_ERROR_LOW_PASSWORD_QUALITY, "Password too weak: %s",
74 sym_pwquality_strerror(buf, sizeof(buf), r, auxerror));
75 }
76
77 return 1;
78}
79
80#else
81
82int user_record_quality_check_password(
83 UserRecord *hr,
84 UserRecord *secret,
85 sd_bus_error *error) {
86
87 return 0;
88}
89
90#endif