2 * Copyright (c) 1989 - 1994, Julianne Frances Haugh
3 * Copyright (c) 1996 - 1999, Marek Michałkiewicz
4 * Copyright (c) 2003 - 2005, Tomasz Kłoczko
5 * Copyright (c) 2007 - 2008, Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the copyright holders or contributors may not be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * This version of obscure.c contains modifications to support "cracklib"
42 * by Alec Muffet (alec.muffett@uk.sun.com). You must obtain the Cracklib
43 * library source code for this function to operate.
47 #include "prototypes.h"
51 * can't be a palindrome - like `R A D A R' or `M A D A M'
53 static bool palindrome (unused const char *old, const char *new)
59 for (j = 0; j < i; j++) {
60 if (new[i - j - 1] != new[j]) {
69 * more than half of the characters are different ones.
72 static bool similar (const char *old, const char *new)
77 * XXX - sometimes this fails when changing from a simple password
78 * to a really long one (MD5). For now, I just return success if
79 * the new password is long enough. Please feel free to suggest
80 * something better... --marekm
82 if (strlen (new) >= 8) {
86 for (i = j = 0; ('\0' != new[i]) && ('\0' != old[i]); i++) {
87 if (strchr (new, old[i]) != NULL) {
100 * a nice mix of characters.
103 static int simple (unused const char *old, const char *new)
112 for (i = 0; '\0' != new[i]; i++) {
113 if (isdigit (new[i])) {
115 } else if (isupper (new[i])) {
117 } else if (islower (new[i])) {
125 * The scam is this - a password of only one character type
126 * must be 8 letters long. Two types, 7, and so on.
150 static char *str_lower (char *string)
154 for (cp = string; '\0' != *cp; cp++) {
160 static const char *password_check (const char *old, const char *new,
161 const struct passwd *pwdp)
163 const char *msg = NULL;
164 char *oldmono, *newmono, *wrapped;
169 #ifdef HAVE_LIBCRACK_PW
170 char *FascistCheckPw ();
172 char *FascistCheck ();
176 if (strcmp (new, old) == 0) {
177 return _("no change");
180 newmono = str_lower (xstrdup (new));
181 oldmono = str_lower (xstrdup (old));
182 wrapped = xmalloc (strlen (oldmono) * 2 + 1);
183 strcpy (wrapped, oldmono);
184 strcat (wrapped, oldmono);
186 if (palindrome (oldmono, newmono)) {
187 msg = _("a palindrome");
188 } else if (strcmp (oldmono, newmono) == 0) {
189 msg = _("case changes only");
190 } else if (similar (oldmono, newmono)) {
191 msg = _("too similar");
192 } else if (simple (old, new)) {
193 msg = _("too simple");
194 } else if (strstr (wrapped, newmono) != NULL) {
199 * Invoke Alec Muffett's cracklib routines.
202 dictpath = getdef_str ("CRACKLIB_DICTPATH");
203 if (NULL != dictpath) {
204 #ifdef HAVE_LIBCRACK_PW
205 msg = FascistCheckPw (new, dictpath, pwdp);
207 msg = FascistCheck (new, dictpath);
223 static const char *obscure_msg (const char *old, const char *new,
224 const struct passwd *pwdp)
226 int maxlen, oldlen, newlen;
231 oldlen = strlen (old);
232 newlen = strlen (new);
234 if (newlen < getdef_num ("PASS_MIN_LEN", 0)) {
235 return _("too short");
239 * Remaining checks are optional.
241 if (!getdef_bool ("OBSCURE_CHECKS_ENAB")) {
245 msg = password_check (old, new, pwdp);
250 result = getdef_str ("ENCRYPT_METHOD");
251 if (NULL == result) {
252 /* The traditional crypt() truncates passwords to 8 chars. It is
253 possible to circumvent the above checks by choosing an easy
254 8-char password and adding some random characters to it...
255 Example: "password$%^&*123". So check it again, this time
256 truncated to the maximum length. Idea from npasswd. --marekm */
258 if (getdef_bool ("MD5_CRYPT_ENAB")) {
264 if ( (strcmp (result, "MD5") == 0)
266 || (strcmp (result, "SHA256") == 0)
267 || (strcmp (result, "SHA512") == 0)
274 maxlen = getdef_num ("PASS_MAX_LEN", 8);
275 if ( (oldlen <= maxlen)
276 && (newlen <= maxlen)) {
280 new1 = xstrdup (new);
281 old1 = xstrdup (old);
282 if (newlen > maxlen) {
285 if (oldlen > maxlen) {
289 msg = password_check (old1, new1, pwdp);
291 memzero (new1, newlen);
292 memzero (old1, oldlen);
300 * Obscure - see if password is obscure enough.
302 * The programmer is encouraged to add as much complexity to this
303 * routine as desired. Included are some of my favorite ways to
307 int obscure (const char *old, const char *new, const struct passwd *pwdp)
309 const char *msg = obscure_msg (old, new, pwdp);
312 printf (_("Bad password: %s. "), msg);
319 extern int errno; /* warning: ANSI C forbids an empty source file */
320 #endif /* !USE_PAM */