Debian Codegraph @

src/pam_pwquality.c

raw
1 /*
2 * PAM module for password quality checking using libpwquality
3 *
4 * See the end of the file for Copyright and License Information
5 */
6
7 #include "config.h"
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdarg.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <ctype.h>
16 #include <limits.h>
17 #include <syslog.h>
18 #include <libintl.h>
19 #include <stdio.h>
20 #include <pwd.h>
21 #include <errno.h>
22 #include "pwquality.h"
23
24 /*
25 * here, we make a definition for the externally accessible function
26 * in this file (this definition is required for static a module
27 * but strongly encouraged generally) it is used to instruct the
28 * modules include file to define the function prototypes.
29 */
30
31 #define PAM_SM_PASSWORD
32
33 #include <security/pam_modules.h>
34 #include <security/_pam_macros.h>
35 #include <security/pam_ext.h>
36
37 #ifdef HAVE_PAM_CHECK_USER_IN_PASSWD
38 #include <security/pam_modutil.h>
39 #endif
40
41 /* argument parsing */
42 #define PAM_DEBUG_ARG 0x0001
43
44 struct module_options {
45 pwquality_settings_t *pwq;
46 };
47
48 #define CO_RETRY_TIMES 1
49
50 #define PATH_PASSWD "/etc/passwd"
51
52 static int
53 _pam_parse (pam_handle_t *pamh, struct module_options *opt,
54 int argc, const char **argv)
55 {
56 int ctrl = 0;
57 int rv;
58 pwquality_settings_t *pwq;
59 void *auxerror;
60 char buf[PWQ_MAX_ERROR_MESSAGE_LEN];
61
62 pwq = pwquality_default_settings();
63 if (pwq == NULL)
64 return -1;
65
66 /* just log error here */
67 if ((rv=pwquality_read_config(pwq, NULL, &auxerror)) != 0)
68 pam_syslog(pamh, LOG_ERR,
69 "Reading pwquality configuration file failed: %s",
70 pwquality_strerror(buf, sizeof(buf), rv, auxerror));
71
72 /* step through arguments */
73 for (ctrl = 0; argc-- > 0; ++argv) {
74 if (!strcmp(*argv, "debug"))
75 ctrl |= PAM_DEBUG_ARG;
76 else if (!strncmp(*argv, "type=", 5))
77 pam_set_item (pamh, PAM_AUTHTOK_TYPE, *argv+5);
78 else if (!strncmp(*argv, "difignore=", 10)) {
79 /* ignored for compatibility with pam_cracklib */
80 } else if (!strncmp(*argv, "reject_username", 15)) {
81 /* ignored for compatibility with pam_cracklib */
82 } else if (!strncmp(*argv, "authtok_type", 12)) {
83 /* for pam_get_authtok, ignore */;
84 } else if (!strncmp(*argv, "use_authtok", 11)) {
85 /* for pam_get_authtok, ignore */;
86 } else if (!strncmp(*argv, "use_first_pass", 14)) {
87 /* for pam_get_authtok, ignore */;
88 } else if (!strncmp(*argv, "try_first_pass", 14)) {
89 /* for pam_get_authtok, ignore */;
90 } else if (pwquality_set_option(pwq, *argv)) {
91 pam_syslog(pamh, LOG_ERR,
92 "pam_parse: unknown or broken option; %s", *argv);
93 }
94 }
95
96 opt->pwq = pwq;
97
98 return ctrl;
99 }
100
101 static int
102 check_local_user (pam_handle_t *pamh,
103 const char *user)
104 {
105 #ifdef HAVE_PAM_CHECK_USER_IN_PASSWD
106 return pam_modutil_check_user_in_passwd(pamh, user, NULL) == PAM_SUCCESS;
107 #else
108 struct passwd pw, *pwp;
109 char buf[4096];
110 int found = 0;
111 FILE *fp;
112 int errn;
113
114 fp = fopen(PATH_PASSWD, "r");
115 if (fp == NULL) {
116 pam_syslog(pamh, LOG_ERR, "unable to open %s: %s",
117 PATH_PASSWD, pam_strerror(pamh, errno));
118 return -1;
119 }
120
121 for (;;) {
122 errn = fgetpwent_r(fp, &pw, buf, sizeof (buf), &pwp);
123 if (errn == ERANGE) {
124 pam_syslog(pamh, LOG_WARNING, "%s contains very long lines; corrupted?",
125 PATH_PASSWD);
126 /* we can continue here as next call will read further */
127 continue;
128 }
129 if (errn != 0)
130 break;
131 if (strcmp(pwp->pw_name, user) == 0) {
132 found = 1;
133 break;
134 }
135 }
136
137 fclose (fp);
138
139 if (errn != 0 && errn != ENOENT) {
140 pam_syslog(pamh, LOG_ERR, "unable to enumerate local accounts: %s",
141 pam_strerror(pamh, errn));
142 return -1;
143 } else {
144 return found;
145 }
146 #endif
147 }
148
149 PAM_EXTERN int
150 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
151 int argc, const char **argv)
152 {
153 int ctrl;
154 struct module_options options;
155 int retry_times = CO_RETRY_TIMES;
156
157 memset(&options, 0, sizeof(options));
158
159 ctrl = _pam_parse(pamh, &options, argc, argv);
160 if (ctrl < 0)
161 return PAM_BUF_ERR;
162
163 pwquality_get_int_value(options.pwq, PWQ_SETTING_RETRY_TIMES, &retry_times);
164 if (retry_times < 1)
165 retry_times = CO_RETRY_TIMES;
166
167
168 if (flags & PAM_PRELIM_CHECK) {
169 /* Check for passwd dictionary
170 * We cannot do that, since the original path is compiled
171 * into the cracklib library and we don't know it.
172 */
173 pwquality_free_settings(options.pwq);
174 return PAM_SUCCESS;
175 } else if (flags & PAM_UPDATE_AUTHTOK) {
176 int retval;
177 const void *oldtoken;
178 const char *user;
179 int tries;
180
181 retval = pam_get_user(pamh, &user, NULL);
182 if (retval != PAM_SUCCESS || user == NULL) {
183 if (ctrl & PAM_DEBUG_ARG)
184 pam_syslog(pamh, LOG_ERR, "Can not get username");
185 pwquality_free_settings(options.pwq);
186 return PAM_AUTHTOK_ERR;
187 }
188
189 retval = pam_get_item(pamh, PAM_OLDAUTHTOK, &oldtoken);
190 if (retval != PAM_SUCCESS) {
191 if (ctrl & PAM_DEBUG_ARG)
192 pam_syslog(pamh, LOG_ERR, "Can not get old passwd");
193 oldtoken = NULL;
194 }
195
196 tries = 0;
197 while (tries < retry_times) {
198 void *auxerror;
199 const char *newtoken = NULL;
200 int local_users_only = 0;
201
202 tries++;
203
204 /* Planned modus operandi:
205 * Get a passwd.
206 * Verify it against libpwquality.
207 * If okay get it a second time.
208 * Check to be the same with the first one.
209 * set PAM_AUTHTOK and return
210 */
211
212 retval = pam_get_authtok_noverify(pamh, &newtoken, NULL);
213 if (retval != PAM_SUCCESS || newtoken == NULL) {
214 if (retval == PAM_AUTHTOK_ERR || newtoken == NULL)
215 pam_syslog(pamh, LOG_INFO, "user aborted password change");
216 else
217 pam_syslog(pamh, LOG_ERR, "pam_get_authtok_noverify returned error: %s",
218 pam_strerror(pamh, retval));
219 pwquality_free_settings(options.pwq);
220 return PAM_AUTHTOK_ERR;
221 }
222 pwquality_get_int_value(options.pwq, PWQ_SETTING_LOCAL_USERS, &local_users_only);
223 if ((ctrl & PAM_DEBUG_ARG) && local_users_only) {
224 pam_syslog(pamh, LOG_INFO, "Applying password quality checks to local users only");
225 }
226
227 if (local_users_only && check_local_user (pamh, user) == 0) {
228 /* skip the check if a non-local user */
229 retval = 0;
230 } else {
231 /* now test this passwd against libpwquality */
232 retval = pwquality_check(options.pwq, newtoken, oldtoken, user, &auxerror);
233 }
234
235 if (retval < 0) {
236 const char *msg;
237 char buf[PWQ_MAX_ERROR_MESSAGE_LEN];
238 int enforcing = 1;
239 int enforce_for_root = 0;
240
241 msg = pwquality_strerror(buf, sizeof(buf), retval, auxerror);
242 if (ctrl & PAM_DEBUG_ARG)
243 pam_syslog(pamh, LOG_DEBUG, "bad password: %s", msg);
244 pam_error(pamh, _("BAD PASSWORD: %s"), msg);
245 pwquality_get_int_value(options.pwq, PWQ_SETTING_ENFORCING, &enforcing);
246 pwquality_get_int_value(options.pwq, PWQ_SETTING_ENFORCE_ROOT, &enforce_for_root);
247
248 if (enforcing && (getuid() || enforce_for_root ||
249 (flags & PAM_CHANGE_EXPIRED_AUTHTOK))) {
250 pam_set_item(pamh, PAM_AUTHTOK, NULL);
251 continue;
252 }
253 } else {
254 if (ctrl & PAM_DEBUG_ARG)
255 pam_syslog(pamh, LOG_DEBUG, "password score: %d", retval);
256 }
257
258 retval = pam_get_authtok_verify(pamh, &newtoken, NULL);
259 if (retval != PAM_SUCCESS || newtoken == NULL) {
260 pam_set_item(pamh, PAM_AUTHTOK, NULL);
261 if (retval == PAM_TRY_AGAIN)
262 continue;
263 if (retval == PAM_AUTHTOK_ERR || newtoken == NULL)
264 pam_syslog(pamh, LOG_INFO, "user aborted password change");
265 else
266 pam_syslog(pamh, LOG_ERR, "pam_get_authtok_verify returned error: %s",
267 pam_strerror(pamh, retval));
268 pwquality_free_settings(options.pwq);
269 return PAM_AUTHTOK_ERR;
270 }
271
272 pwquality_free_settings(options.pwq);
273 return PAM_SUCCESS;
274 }
275
276 pwquality_free_settings(options.pwq);
277 pam_set_item (pamh, PAM_AUTHTOK, NULL);
278
279 /* if we have only one try, we can use the real reason,
280 * else say that there were too many tries. */
281 if (retry_times > 1)
282 return PAM_MAXTRIES;
283 else
284 return PAM_AUTHTOK_ERR;
285 } else {
286 pwquality_free_settings(options.pwq);
287 if (ctrl & PAM_DEBUG_ARG)
288 pam_syslog(pamh, LOG_NOTICE, "UNKNOWN flags setting %02X",flags);
289 }
290
291 return PAM_SERVICE_ERR;
292 }
293
294
295
296 #ifdef PAM_STATIC
297 /* static module data */
298 struct pam_module _pam_pwquality_modstruct = {
299 "pam_pwquality",
300 NULL,
301 NULL,
302 NULL,
303 NULL,
304 NULL,
305 pam_sm_chauthtok
306 };
307 #endif
308
309 /*
310 * Copyright (c) Cristian Gafton <[email protected]>, 1996.
311 * All rights reserved
312 * Copyright (c) Red Hat, Inc, 2011, 2012
313 * Copyright (c) Tomas Mraz <[email protected]>, 2011
314 *
315 * Redistribution and use in source and binary forms, with or without
316 * modification, are permitted provided that the following conditions
317 * are met:
318 * 1. Redistributions of source code must retain the above copyright
319 * notice, and the entire permission notice in its entirety,
320 * including the disclaimer of warranties.
321 * 2. Redistributions in binary form must reproduce the above copyright
322 * notice, this list of conditions and the following disclaimer in the
323 * documentation and/or other materials provided with the distribution.
324 * 3. The name of the author may not be used to endorse or promote
325 * products derived from this software without specific prior
326 * written permission.
327 *
328 * ALTERNATIVELY, this product may be distributed under the terms of
329 * the GNU General Public License version 2 or later, in which case the
330 * provisions of the GPL are required INSTEAD OF the above restrictions.
331 *
332 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
333 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
334 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
335 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
336 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
337 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
338 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
339 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
340 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
341 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
342 * OF THE POSSIBILITY OF SUCH DAMAGE.
343 *
344 * The following copyright was appended for the long password support
345 * added with the libpam 0.58 release:
346 *
347 * Modificaton Copyright (c) Philip W. Dalrymple III <[email protected]>
348 * 1997. All rights reserved
349 *
350 * THE MODIFICATION THAT PROVIDES SUPPORT FOR LONG PASSWORD TYPE CHECKING TO
351 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
352 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
353 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
354 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
355 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
356 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
357 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
358 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
359 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
360 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
361 * OF THE POSSIBILITY OF SUCH DAMAGE.
362 */