Debian Codegraph @

src/shared/password-quality-util-pwquality.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "password-quality-util-pwquality.h"
4
5 #include "errno-util.h"
6 #include "log.h"
7
8 #if HAVE_PWQUALITY
9
10 #include <pwquality.h>
11 #include <stdio.h>
12 #include <unistd.h>
13
14 #include "sd-dlopen.h"
15
16 #include "alloc-util.h"
17 #include "dlfcn-util.h"
18 #include "password-quality-util.h"
19 #include "string-util.h"
20 #include "strv.h"
21
22 static DLSYM_PROTOTYPE(pwquality_check) = NULL;
23 static DLSYM_PROTOTYPE(pwquality_default_settings) = NULL;
24 static DLSYM_PROTOTYPE(pwquality_free_settings) = NULL;
25 static DLSYM_PROTOTYPE(pwquality_generate) = NULL;
26 static DLSYM_PROTOTYPE(pwquality_get_str_value) = NULL;
27 static DLSYM_PROTOTYPE(pwquality_read_config) = NULL;
28 static DLSYM_PROTOTYPE(pwquality_set_int_value) = NULL;
29 static DLSYM_PROTOTYPE(pwquality_strerror) = NULL;
30
31 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_RENAME(pwquality_settings_t*, sym_pwquality_free_settings, pwquality_free_settingsp, NULL);
32
33 static void pwq_maybe_disable_dictionary(pwquality_settings_t *pwq) {
34 char buf[PWQ_MAX_ERROR_MESSAGE_LEN];
35 const char *path;
36 int r;
37
38 assert(pwq);
39
40 r = sym_pwquality_get_str_value(pwq, PWQ_SETTING_DICT_PATH, &path);
41 if (r < 0) {
42 log_debug("Failed to read libpwquality dictionary path, ignoring: %s",
43 sym_pwquality_strerror(buf, sizeof(buf), r, NULL));
44 return;
45 }
46
47 if (isempty(path)) {
48 log_debug("Weird, no dictionary file configured, ignoring.");
49 return;
50 }
51
52 if (access(path, F_OK) >= 0)
53 return;
54
55 if (errno != ENOENT) {
56 log_debug_errno(errno, "Failed to check if dictionary file %s exists, ignoring: %m", path);
57 return;
58 }
59
60 r = sym_pwquality_set_int_value(pwq, PWQ_SETTING_DICT_CHECK, 0);
61 if (r < 0)
62 log_debug("Failed to disable libpwquality dictionary check, ignoring: %s",
63 sym_pwquality_strerror(buf, sizeof(buf), r, NULL));
64 }
65
66 static int pwq_allocate_context(pwquality_settings_t **ret) {
67 _cleanup_(pwquality_free_settingsp) pwquality_settings_t *pwq = NULL;
68 char buf[PWQ_MAX_ERROR_MESSAGE_LEN];
69 void *auxerror;
70 int r;
71
72 assert(ret);
73
74 r = dlopen_pwquality(LOG_DEBUG);
75 if (r < 0)
76 return r;
77
78 pwq = sym_pwquality_default_settings();
79 if (!pwq)
80 return -ENOMEM;
81
82 r = sym_pwquality_read_config(pwq, NULL, &auxerror);
83 if (r < 0)
84 log_debug("Failed to read libpwquality configuration, ignoring: %s",
85 sym_pwquality_strerror(buf, sizeof(buf), r, auxerror));
86
87 pwq_maybe_disable_dictionary(pwq);
88
89 *ret = TAKE_PTR(pwq);
90 return 0;
91 }
92
93 int suggest_passwords(void) {
94 _cleanup_(pwquality_free_settingsp) pwquality_settings_t *pwq = NULL;
95 _cleanup_strv_free_erase_ char **suggestions = NULL;
96 _cleanup_(erase_and_freep) char *joined = NULL;
97 char buf[PWQ_MAX_ERROR_MESSAGE_LEN];
98 int r;
99
100 r = pwq_allocate_context(&pwq);
101 if (r < 0) {
102 if (ERRNO_IS_NOT_SUPPORTED(r))
103 return 0;
104 return log_error_errno(r, "Failed to allocate libpwquality context: %m");
105 }
106
107 suggestions = new0(char*, N_SUGGESTIONS+1);
108 if (!suggestions)
109 return log_oom();
110
111 for (size_t i = 0; i < N_SUGGESTIONS; i++) {
112 r = sym_pwquality_generate(pwq, 64, suggestions + i);
113 if (r < 0)
114 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to generate password, ignoring: %s",
115 sym_pwquality_strerror(buf, sizeof(buf), r, NULL));
116 }
117
118 joined = strv_join(suggestions, " ");
119 if (!joined)
120 return log_oom();
121
122 printf("Password suggestions: %s\n", joined);
123 return 1;
124 }
125
126 int check_password_quality(const char *password, const char *old, const char *username, char **ret_error) {
127 _cleanup_(pwquality_free_settingsp) pwquality_settings_t *pwq = NULL;
128 char buf[PWQ_MAX_ERROR_MESSAGE_LEN];
129 void *auxerror;
130 int r;
131
132 assert(password);
133
134 r = pwq_allocate_context(&pwq);
135 if (r < 0)
136 return log_debug_errno(r, "Failed to allocate libpwquality context: %m");
137
138 r = sym_pwquality_check(pwq, password, old, username, &auxerror);
139 if (r < 0) {
140 if (ret_error) {
141 r = strdup_to(ret_error,
142 sym_pwquality_strerror(buf, sizeof(buf), r, auxerror));
143 if (r < 0)
144 return r;
145 }
146
147 return 0; /* all bad */
148 }
149
150 return 1; /* all good */
151 }
152
153 #endif
154
155 int dlopen_pwquality(int log_level) {
156 #if HAVE_PWQUALITY
157 static void *pwquality_dl = NULL;
158
159 SD_ELF_NOTE_DLOPEN(
160 "pwquality",
161 "Support for password quality checks",
162 SD_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
163 "libpwquality.so.1");
164
165 return dlopen_many_sym_or_warn(
166 &pwquality_dl, "libpwquality.so.1", log_level,
167 DLSYM_ARG(pwquality_check),
168 DLSYM_ARG(pwquality_default_settings),
169 DLSYM_ARG(pwquality_free_settings),
170 DLSYM_ARG(pwquality_generate),
171 DLSYM_ARG(pwquality_get_str_value),
172 DLSYM_ARG(pwquality_read_config),
173 DLSYM_ARG(pwquality_set_int_value),
174 DLSYM_ARG(pwquality_strerror));
175 #else
176 return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
177 "libpwquality support is not compiled in.");
178 #endif
179 }