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 int palindrome (unused const char *old, const char *new)
59 for (j = 0; j < i; j++)
60 if (new[i - j - 1] != new[j])
67 * more than half of the characters are different ones.
70 static int similar (const char *old, const char *new)
75 * XXX - sometimes this fails when changing from a simple password
76 * to a really long one (MD5). For now, I just return success if
77 * the new password is long enough. Please feel free to suggest
78 * something better... --marekm
80 if (strlen (new) >= 8)
83 for (i = j = 0; new[i] && old[i]; i++)
84 if (strchr (new, old[i]))
94 * a nice mix of characters.
97 static int simple (unused const char *old, const char *new)
106 for (i = 0; new[i]; i++) {
107 if (isdigit (new[i]))
109 else if (isupper (new[i]))
111 else if (islower (new[i]))
118 * The scam is this - a password of only one character type
119 * must be 8 letters long. Two types, 7, and so on.
138 static char *str_lower (char *string)
142 for (cp = string; *cp; cp++)
147 static const char *password_check (const char *old, const char *new,
148 const struct passwd *pwdp)
150 const char *msg = NULL;
151 char *oldmono, *newmono, *wrapped;
156 #ifdef HAVE_LIBCRACK_PW
157 char *FascistCheckPw ();
159 char *FascistCheck ();
163 if (strcmp (new, old) == 0)
164 return _("no change");
166 newmono = str_lower (xstrdup (new));
167 oldmono = str_lower (xstrdup (old));
168 wrapped = xmalloc (strlen (oldmono) * 2 + 1);
169 strcpy (wrapped, oldmono);
170 strcat (wrapped, oldmono);
172 if (palindrome (oldmono, newmono))
173 msg = _("a palindrome");
175 if (!msg && strcmp (oldmono, newmono) == 0)
176 msg = _("case changes only");
178 if (!msg && similar (oldmono, newmono))
179 msg = _("too similar");
181 if (!msg && simple (old, new))
182 msg = _("too simple");
184 if (!msg && strstr (wrapped, newmono))
189 * Invoke Alec Muffett's cracklib routines.
192 if (!msg && (dictpath = getdef_str ("CRACKLIB_DICTPATH")))
193 #ifdef HAVE_LIBCRACK_PW
194 msg = FascistCheckPw (new, dictpath, pwdp);
196 msg = FascistCheck (new, dictpath);
210 static const char *obscure_msg (const char *old, const char *new,
211 const struct passwd *pwdp)
213 int maxlen, oldlen, newlen;
218 oldlen = strlen (old);
219 newlen = strlen (new);
221 if (newlen < getdef_num ("PASS_MIN_LEN", 0))
222 return _("too short");
225 * Remaining checks are optional.
227 if (!getdef_bool ("OBSCURE_CHECKS_ENAB"))
230 msg = password_check (old, new, pwdp);
234 if ((result = getdef_str ("ENCRYPT_METHOD")) == NULL) {
235 /* The traditional crypt() truncates passwords to 8 chars. It is
236 possible to circumvent the above checks by choosing an easy
237 8-char password and adding some random characters to it...
238 Example: "password$%^&*123". So check it again, this time
239 truncated to the maximum length. Idea from npasswd. --marekm */
241 if (getdef_bool ("MD5_CRYPT_ENAB"))
246 if ( !strcmp (result, "MD5")
248 || !strcmp (result, "SHA256")
249 || !strcmp (result, "SHA512")
255 maxlen = getdef_num ("PASS_MAX_LEN", 8);
256 if (oldlen <= maxlen && newlen <= maxlen)
259 new1 = xstrdup (new);
260 old1 = xstrdup (old);
266 msg = password_check (old1, new1, pwdp);
268 memzero (new1, newlen);
269 memzero (old1, oldlen);
277 * Obscure - see if password is obscure enough.
279 * The programmer is encouraged to add as much complexity to this
280 * routine as desired. Included are some of my favorite ways to
284 int obscure (const char *old, const char *new, const struct passwd *pwdp)
286 const char *msg = obscure_msg (old, new, pwdp);
289 printf (_("Bad password: %s. "), msg);
296 extern int errno; /* warning: ANSI C forbids an empty source file */
297 #endif /* !USE_PAM */