Debian Codegraph @

src/check.c

raw
1 /*
2 * libpwquality main API code for quality checking
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 <ctype.h>
12 #ifdef HAVE_CRACK_H
13 #include <crack.h>
14 #endif
15 #include <sys/types.h>
16 #include <pwd.h>
17 #include <unistd.h>
18
19 #include "pwquality.h"
20 #include "pwqprivate.h"
21
22 #ifdef MIN
23 #undef MIN
24 #endif
25 #define MIN(_a, _b) (((_a) < (_b)) ? (_a) : (_b))
26
27 /* Helper functions */
28
29 /*
30 * test for a palindrome - like `R A D A R' or `M A D A M'
31 */
32 static int
33 palindrome(const char *new)
34 {
35 int i, j;
36
37 i = strlen (new);
38
39 for (j = 0; j < i; j++)
40 if (new[i - j - 1] != new[j])
41 return 0;
42
43 return 1;
44 }
45
46 /*
47 * Calculate how different two strings are in terms of the number of
48 * character removals, additions, and changes needed to go from one to
49 * the other
50 */
51
52 static int
53 distdifferent(const char *old, const char *new,
54 size_t i, size_t j)
55 {
56 char c, d;
57
58 if ((i == 0) || (strlen(old) < i)) {
59 c = 0;
60 } else {
61 c = old[i - 1];
62 }
63
64 if ((j == 0) || (strlen(new) < j)) {
65 d = 0;
66 } else {
67 d = new[j - 1];
68 }
69 return (c != d);
70 }
71
72 static int
73 distcalculate(int **distances, const char *old, const char *new,
74 size_t i, size_t j)
75 {
76 int tmp = 0;
77
78 if (distances[i][j] != -1) {
79 return distances[i][j];
80 }
81
82 tmp = distcalculate(distances, old, new, i - 1, j - 1);
83 tmp = MIN(tmp, distcalculate(distances, old, new, i, j - 1));
84 tmp = MIN(tmp, distcalculate(distances, old, new, i - 1, j));
85 tmp += distdifferent(old, new, i, j);
86
87 distances[i][j] = tmp;
88
89 return tmp;
90 }
91
92 static int
93 distance(const char *old, const char *new)
94 {
95 int **distances = NULL;
96 size_t m, n, i, j;
97 int r = -1;
98
99 m = strlen(old);
100 n = strlen(new);
101 distances = calloc(m + 1, sizeof(int*));
102 if (distances == NULL)
103 return -1;
104
105 for (i = 0; i <= m; i++) {
106 distances[i] = calloc(n + 1, sizeof(int));
107 if (distances[i] == NULL)
108 goto allocfail;
109
110 for(j = 0; j <= n; j++) {
111 distances[i][j] = -1;
112 }
113 }
114
115 for (i = 0; i <= m; i++) {
116 distances[i][0] = i;
117 }
118
119 for (j = 0; j <= n; j++) {
120 distances[0][j] = j;
121 }
122
123 r = distcalculate(distances, old, new, m, n);
124
125 allocfail:
126 for (i = 0; i <= m; i++) {
127 if (distances[i]) {
128 memset(distances[i], 0, sizeof(int) * (n + 1));
129 free(distances[i]);
130 }
131 }
132 free(distances);
133
134 return r;
135 }
136
137 static int
138 similar(pwquality_settings_t *pwq,
139 const char *old, const char *new)
140 {
141 int dist;
142
143 dist = distance(old, new);
144
145 if (dist < 0)
146 return PWQ_ERROR_MEM_ALLOC;
147
148 if (dist >= pwq->diff_ok) {
149 return 0;
150 }
151
152 if (strlen(new) >= (strlen(old) * 2)) {
153 return 0;
154 }
155
156 /* passwords are too similar */
157 return PWQ_ERROR_TOO_SIMILAR;
158 }
159
160 /*
161 * count classes of charecters
162 */
163
164 static int
165 numclass(const char *new)
166 {
167 int digits = 0;
168 int uppers = 0;
169 int lowers = 0;
170 int others = 0;
171 int total_class;
172 int i;
173
174 for (i = 0; new[i]; i++) {
175 if (isdigit(new[i]))
176 digits = 1;
177 else if (isupper(new[i]))
178 uppers = 1;
179 else if (islower(new[i]))
180 lowers = 1;
181 else
182 others = 1;
183 }
184
185 total_class = digits + uppers + lowers + others;
186
187 return total_class;
188 }
189
190 /*
191 * a nice mix of characters
192 * the credit (if positive) is a maximum value that is subtracted from
193 * the minimum allowed size of the password if letters of the class are
194 * present in the password
195 */
196 static int
197 simple(pwquality_settings_t *pwq, const char *new, void **auxerror)
198 {
199 int digits = 0;
200 int uppers = 0;
201 int lowers = 0;
202 int others = 0;
203 int size;
204 int i;
205 enum { NONE, DIGIT, UCASE, LCASE, OTHER } prevclass = NONE;
206 int sameclass = 0;
207
208 for (i = 0; new[i]; i++) {
209 if (isdigit(new[i])) {
210 digits++;
211 if (prevclass != DIGIT) {
212 prevclass = DIGIT;
213 sameclass = 1;
214 } else
215 sameclass++;
216 }
217 else if (isupper(new[i])) {
218 uppers++;
219 if (prevclass != UCASE) {
220 prevclass = UCASE;
221 sameclass = 1;
222 } else
223 sameclass++;
224 }
225 else if (islower(new[i])) {
226 lowers++;
227 if (prevclass != LCASE) {
228 prevclass = LCASE;
229 sameclass = 1;
230 } else
231 sameclass++;
232 }
233 else {
234 others++;
235 if (prevclass != OTHER) {
236 prevclass = OTHER;
237 sameclass = 1;
238 } else
239 sameclass++;
240 }
241 if (pwq->max_class_repeat > 1 && sameclass > pwq->max_class_repeat) {
242 if (auxerror)
243 *auxerror = (void *)(long)pwq->max_class_repeat;
244 return PWQ_ERROR_MAX_CLASS_REPEAT;
245 }
246 }
247
248 if ((pwq->dig_credit >= 0) && (digits > pwq->dig_credit))
249 digits = pwq->dig_credit;
250
251 if ((pwq->up_credit >= 0) && (uppers > pwq->up_credit))
252 uppers = pwq->up_credit;
253
254 if ((pwq->low_credit >= 0) && (lowers > pwq->low_credit))
255 lowers = pwq->low_credit;
256
257 if ((pwq->oth_credit >= 0) && (others > pwq->oth_credit))
258 others = pwq->oth_credit;
259
260 size = pwq->min_length;
261
262 if (pwq->dig_credit >= 0)
263 size -= digits;
264 else if (digits < -pwq->dig_credit) {
265 if (auxerror)
266 *auxerror = (void *)(long)-pwq->dig_credit;
267 return PWQ_ERROR_MIN_DIGITS;
268 }
269
270 if (pwq->up_credit >= 0)
271 size -= uppers;
272 else if (uppers < -pwq->up_credit) {
273 if (auxerror)
274 *auxerror = (void *)(long)-pwq->up_credit;
275 return PWQ_ERROR_MIN_UPPERS;
276 }
277
278 if (pwq->low_credit >= 0)
279 size -= lowers;
280 else if (lowers < -pwq->low_credit) {
281 if (auxerror)
282 *auxerror = (void *)(long)-pwq->low_credit;
283 return PWQ_ERROR_MIN_LOWERS;
284 }
285
286 if (pwq->oth_credit >= 0)
287 size -= others;
288 else if (others < -pwq->oth_credit) {
289 if (auxerror)
290 *auxerror = (void *)(long)-pwq->oth_credit;
291 return PWQ_ERROR_MIN_OTHERS;
292 }
293
294 if (size <= i)
295 return 0;
296
297 if (auxerror)
298 *auxerror = (void *)(long)size;
299
300 return PWQ_ERROR_MIN_LENGTH;
301 }
302
303 /*
304 * too many same consecutive characters
305 */
306
307 static int
308 consecutive(pwquality_settings_t *pwq, const char *new, void **auxerror)
309 {
310 char c;
311 int i;
312 int same;
313
314 if (pwq->max_repeat == 0)
315 return 0;
316
317 for (i = 0; new[i]; i++) {
318 if (i > 0 && new[i] == c) {
319 ++same;
320 if (same > pwq->max_repeat) {
321 if (auxerror)
322 *auxerror = (void *)(long)pwq->max_repeat;
323 return 1;
324 }
325 } else {
326 c = new[i];
327 same = 1;
328 }
329 }
330 return 0;
331 }
332
333 static int sequence(pwquality_settings_t *pwq, const char *new, void **auxerror)
334 {
335 char c;
336 int i;
337 int sequp = 1;
338 int seqdown = 1;
339
340 if (pwq->max_sequence == 0)
341 return 0;
342
343 if (new[0] == '\0')
344 return 0;
345
346 for (i = 1; new[i]; i++) {
347 c = new[i-1];
348 if (new[i] == c+1) {
349 ++sequp;
350 if (sequp > pwq->max_sequence) {
351 if (auxerror)
352 *auxerror = (void *)(long)pwq->max_sequence;
353 return 1;
354 }
355 seqdown = 1;
356 } else if (new[i] == c-1) {
357 ++seqdown;
358 if (seqdown > pwq->max_sequence) {
359 if (auxerror)
360 *auxerror = (void *)(long)pwq->max_sequence;
361 return 1;
362 }
363 sequp = 1;
364 } else {
365 sequp = 1;
366 seqdown = 1;
367 }
368 }
369 return 0;
370 }
371
372 static int
373 wordcheck(pwquality_settings_t *pwq, const char *new,
374 char *word)
375 {
376 char *f, *b;
377 int dist, wordlen = strlen(word);
378
379 /* No point to check for word in password for 1-3 char
380 * words; it will be contained one way or another anyway. */
381 if (wordlen < PWQ_MIN_WORD_LENGTH)
382 return 0;
383
384 if (strstr(new, word) != NULL)
385 return PWQ_ERROR_BAD_WORDS;
386
387 dist = distance(new, word);
388 if (dist >= 0 && dist < PWQ_DEFAULT_DIFF_OK)
389 return PWQ_ERROR_BAD_WORDS;
390
391 /* now reverse the wordname, we can do that in place
392 as it is strdup-ed */
393 f = word;
394 b = word + wordlen - 1;
395 while (f < b) {
396 char c;
397
398 c = *f;
399 *f = *b;
400 *b = c;
401 --b;
402 ++f;
403 }
404
405 if (strstr(new, word) != NULL)
406 return PWQ_ERROR_BAD_WORDS;
407
408 dist = distance(new, word);
409 if (dist >= 0 && dist < PWQ_DEFAULT_DIFF_OK)
410 return PWQ_ERROR_BAD_WORDS;
411
412 return 0;
413 }
414
415 static int
416 usercheck(pwquality_settings_t *pwq, const char *new,
417 char *user)
418 {
419 int i, userlen;
420 int rv = 0;
421 char *subuser = calloc(pwq->user_substr+1, sizeof(char));
422
423 if (subuser == NULL) {
424 return PWQ_ERROR_MEM_ALLOC;
425 }
426
427 userlen = strlen(user);
428 if (pwq->user_substr >= PWQ_MIN_WORD_LENGTH &&
429 userlen > pwq->user_substr) {
430 for(i = 0; !rv && (i <= userlen - pwq->user_substr); i++) {
431 strncpy(subuser, user+i, pwq->user_substr+1);
432 subuser[pwq->user_substr] = '\0';
433 rv = wordcheck(pwq, new, subuser);
434 }
435 }
436 else {
437 // if we already tested substrings, there's no need to test
438 // the whole username; all substrings would've been found :)
439 if (!rv)
440 rv = wordcheck(pwq, new, user);
441 }
442 // translate wordcheck return
443 if (rv == PWQ_ERROR_BAD_WORDS)
444 rv = PWQ_ERROR_USER_CHECK;
445 free(subuser);
446 return rv;
447 }
448
449 static char *
450 str_lower(char *string)
451 {
452 char *cp;
453
454 if (!string)
455 return NULL;
456
457 for (cp = string; *cp; cp++)
458 *cp = tolower(*cp);
459 return string;
460 }
461
462 static int
463 wordlistcheck(pwquality_settings_t *pwq, const char *new,
464 const char *wordlist)
465 {
466 char *list;
467 char *p;
468 char *next;
469
470 if (wordlist == NULL)
471 return 0;
472
473 if ((list = strdup(wordlist)) == NULL) {
474 return PWQ_ERROR_MEM_ALLOC;
475 }
476
477 for (p = list;;p = next + 1) {
478 next = strchr(p, ' ');
479 if (next)
480 *next = '\0';
481
482 if (strlen(p) >= PWQ_MIN_WORD_LENGTH) {
483 str_lower(p);
484 if (wordcheck(pwq, new, p)) {
485 free(list);
486 return PWQ_ERROR_BAD_WORDS;
487 }
488 }
489
490 if (!next)
491 break;
492 }
493
494 free(list);
495 return 0;
496 }
497
498 static int
499 gecoscheck(pwquality_settings_t *pwq, const char *new,
500 const char *user)
501 {
502 struct passwd pwd;
503 struct passwd *result;
504 char *buf;
505 long bufsize;
506 int rv;
507
508 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
509 if (bufsize == -1 || bufsize > PWQ_MAX_PASSWD_BUF_LEN)
510 bufsize = PWQ_MAX_PASSWD_BUF_LEN;
511 buf = malloc(bufsize);
512 if (buf == NULL)
513 return PWQ_ERROR_MEM_ALLOC;
514
515 if (getpwnam_r(user, &pwd, buf, bufsize, &result) != 0 ||
516 result == NULL) {
517 free(buf);
518 return 0;
519 }
520
521 rv = wordlistcheck(pwq, new, result->pw_gecos);
522 if (rv == PWQ_ERROR_BAD_WORDS)
523 rv = PWQ_ERROR_GECOS_CHECK;
524
525 free(buf);
526 return rv;
527 }
528
529 static char *
530 x_strdup(const char *string)
531 {
532 if (!string)
533 return NULL;
534 return strdup(string);
535 }
536
537 static int
538 password_check(pwquality_settings_t *pwq,
539 const char *new, const char *old, const char *user,
540 void **auxerror)
541 {
542 int rv = 0;
543 char *oldmono = NULL, *newmono, *wrapped = NULL;
544 char *usermono = NULL;
545
546 newmono = str_lower(x_strdup(new));
547 if (!newmono)
548 rv = PWQ_ERROR_MEM_ALLOC;
549
550 if (!rv && user) {
551 usermono = str_lower(x_strdup(user));
552 if (!usermono)
553 rv = PWQ_ERROR_MEM_ALLOC;
554 }
555
556 if (!rv && old) {
557 oldmono = str_lower(x_strdup(old));
558 if (oldmono)
559 wrapped = malloc(strlen(oldmono) * 2 + 1);
560 if (wrapped) {
561 strcpy (wrapped, oldmono);
562 strcat (wrapped, oldmono);
563 } else {
564 rv = PWQ_ERROR_MEM_ALLOC;
565 }
566 }
567
568 if (!rv && oldmono && strcmp(oldmono, newmono) == 0)
569 rv = PWQ_ERROR_CASE_CHANGES_ONLY;
570
571 if (!rv && oldmono)
572 rv = similar(pwq, oldmono, newmono);
573
574 if (!rv)
575 rv = simple(pwq, new, auxerror);
576
577 if (!rv && wrapped && strstr(wrapped, newmono))
578 rv = PWQ_ERROR_ROTATED;
579
580 if (!rv && numclass(new) < pwq->min_class) {
581 rv = PWQ_ERROR_MIN_CLASSES;
582 if (auxerror) {
583 *auxerror = (void *)(long)pwq->min_class;
584 }
585 }
586
587 if (!rv && palindrome(newmono))
588 rv = PWQ_ERROR_PALINDROME;
589
590 if (!rv && consecutive(pwq, new, auxerror))
591 rv = PWQ_ERROR_MAX_CONSECUTIVE;
592
593 if (!rv && sequence(pwq, new, auxerror))
594 rv = PWQ_ERROR_MAX_SEQUENCE;
595
596 if (!rv && usermono && pwq->user_check)
597 rv = usercheck(pwq, newmono, usermono);
598
599 if (!rv && user && pwq->gecos_check)
600 rv = gecoscheck(pwq, newmono, user);
601
602 if (!rv)
603 rv = wordlistcheck(pwq, newmono, pwq->bad_words);
604
605 if (newmono) {
606 memset(newmono, 0, strlen(newmono));
607 free(newmono);
608 }
609
610 free(usermono);
611
612 if (oldmono) {
613 memset(oldmono, 0, strlen(oldmono));
614 free(oldmono);
615 }
616
617 if (wrapped) {
618 memset(wrapped, 0, strlen(wrapped));
619 free(wrapped);
620 }
621
622 return rv;
623 }
624
625 /* this algorithm is an arbitrary one, fine-tuned by testing */
626 static int
627 password_score(pwquality_settings_t *pwq, const char *password)
628 {
629 int len;
630 int score;
631 int i;
632 int j;
633 unsigned char freq[256];
634 unsigned char *buf;
635
636 len = strlen(password);
637
638 if ((buf = malloc(len)) == NULL)
639 /* should get enough memory to obtain a nice score */
640 return PWQ_ERROR_MEM_ALLOC;
641
642 score = (len - pwq->min_length) * 2;
643
644 memcpy(buf, password, len);
645
646 for (j = 0; j < 3; j++) {
647
648 memset(freq, 0, sizeof(freq));
649
650 for (i = 0; i < len - j; i++) {
651 ++freq[buf[i]];
652 if (i < len - j - 1)
653 buf[i] = abs(buf[i] - buf[i+1]);
654 }
655
656 for (i = 0; i < (int)sizeof(freq); i++) {
657 if (freq[i])
658 ++score;
659 }
660 }
661
662 memset(buf, 0, len);
663 free(buf);
664
665 score += numclass(password) * 2;
666
667 score = (score * 100)/(3 * pwq->min_length +
668 + PWQ_NUM_CLASSES * 2);
669
670 score -= 50;
671
672 if (score > 100)
673 score = 100;
674 if (score < 0)
675 score = 0;
676
677 return score;
678 }
679
680 /* check the password according to the settings
681 * it returns either score <0-100> or negative error number;
682 * the old password is optional */
683 int
684 pwquality_check(pwquality_settings_t *pwq, const char *password,
685 const char *oldpassword, const char *user, void **auxerror)
686 {
687 const char *msg;
688 int score;
689
690 if (auxerror)
691 *auxerror = NULL;
692
693 if (password == NULL || *password == '\0') {
694 return PWQ_ERROR_EMPTY_PASSWORD;
695 }
696
697 if (user && *user == '\0')
698 user = NULL;
699
700 if (oldpassword && *oldpassword == '\0')
701 oldpassword = NULL;
702
703 if (oldpassword && strcmp(oldpassword, password) == 0) {
704 return PWQ_ERROR_SAME_PASSWORD;
705 }
706
707 if (pwq->diff_ok == 0)
708 oldpassword = NULL;
709
710 score = password_check(pwq, password, oldpassword, user, auxerror);
711
712 if (score != 0)
713 return score;
714
715 #ifdef HAVE_CRACK_H
716 if (pwq->dict_check) {
717 msg = FascistCheck(password, pwq->dict_path);
718 if (msg) {
719 if (auxerror)
720 *auxerror = (void *)msg;
721 return PWQ_ERROR_CRACKLIB_CHECK;
722 }
723 }
724 #endif
725
726 score = password_score(pwq, password);
727
728 return score;
729 }
730
731 /*
732 * Copyright (c) Cristian Gafton <[email protected]>, 1996.
733 * All rights reserved
734 * Copyright (c) Red Hat, Inc, 2011, 2015
735 * Copyright (c) Tomas Mraz <[email protected]>, 2011, 2015
736 *
737 * Redistribution and use in source and binary forms, with or without
738 * modification, are permitted provided that the following conditions
739 * are met:
740 * 1. Redistributions of source code must retain the above copyright
741 * notice, and the entire permission notice in its entirety,
742 * including the disclaimer of warranties.
743 * 2. Redistributions in binary form must reproduce the above copyright
744 * notice, this list of conditions and the following disclaimer in the
745 * documentation and/or other materials provided with the distribution.
746 * 3. The name of the author may not be used to endorse or promote
747 * products derived from this software without specific prior
748 * written permission.
749 *
750 * ALTERNATIVELY, this product may be distributed under the terms of
751 * the GNU General Public License version 2 or later, in which case the
752 * provisions of the GPL are required INSTEAD OF the above restrictions.
753 *
754 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
755 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
756 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
757 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
758 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
759 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
760 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
761 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
762 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
763 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
764 * OF THE POSSIBILITY OF SUCH DAMAGE.
765 *
766 * The following copyright was appended for the long password support
767 * added with the libpam 0.58 release:
768 *
769 * Modificaton Copyright (c) Philip W. Dalrymple III <[email protected]>
770 * 1997. All rights reserved
771 *
772 * THE MODIFICATION THAT PROVIDES SUPPORT FOR LONG PASSWORD TYPE CHECKING TO
773 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
774 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
775 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
776 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
777 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
778 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
779 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
780 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
781 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
782 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
783 * OF THE POSSIBILITY OF SUCH DAMAGE.
784 */