* libmisc/obscure.c: Add brackets and parenthesis.

* libmisc/obscure.c: Avoid implicit conversion of pointers / chars to
	booleans.
	* libmisc/obscure.c: Simplify the list of if.
This commit is contained in:
nekral-guest 2008-08-31 17:28:49 +00:00
parent 6b3266f228
commit 0fcae007a0
2 changed files with 96 additions and 66 deletions

View File

@ -1,3 +1,10 @@
2008-08-29 Nicolas François <nicolas.francois@centraliens.net>
* libmisc/obscure.c: Add brackets and parenthesis.
* libmisc/obscure.c: Avoid implicit conversion of pointers / chars to
booleans.
* libmisc/obscure.c: Simplify the list of if.
2008-08-28 Nicolas François <nicolas.francois@centraliens.net> 2008-08-28 Nicolas François <nicolas.francois@centraliens.net>
* src/passwd.c: Fix a typo in the Usage string. * src/passwd.c: Fix a typo in the Usage string.

View File

@ -50,24 +50,26 @@
/* /*
* can't be a palindrome - like `R A D A R' or `M A D A M' * can't be a palindrome - like `R A D A R' or `M A D A M'
*/ */
static int palindrome (unused const char *old, const char *new) static bool palindrome (unused const char *old, const char *new)
{ {
int i, j; int i, j;
i = strlen (new); i = strlen (new);
for (j = 0; j < i; j++) for (j = 0; j < i; j++) {
if (new[i - j - 1] != new[j]) if (new[i - j - 1] != new[j]) {
return 0; return false;
}
}
return 1; return true;
} }
/* /*
* more than half of the characters are different ones. * more than half of the characters are different ones.
*/ */
static int similar (const char *old, const char *new) static bool similar (const char *old, const char *new)
{ {
int i, j; int i, j;
@ -77,17 +79,21 @@ static int similar (const char *old, const char *new)
* the new password is long enough. Please feel free to suggest * the new password is long enough. Please feel free to suggest
* something better... --marekm * something better... --marekm
*/ */
if (strlen (new) >= 8) if (strlen (new) >= 8) {
return 0; return false;
}
for (i = j = 0; new[i] && old[i]; i++) for (i = j = 0; ('\0' != new[i]) && ('\0' != old[i]); i++) {
if (strchr (new, old[i])) if (strchr (new, old[i]) != NULL) {
j++; j++;
}
}
if (i >= j * 2) if (i >= j * 2) {
return 0; return false;
}
return 1; return true;
} }
/* /*
@ -96,22 +102,23 @@ static int similar (const char *old, const char *new)
static int simple (unused const char *old, const char *new) static int simple (unused const char *old, const char *new)
{ {
int digits = 0; bool digits = false;
int uppers = 0; bool uppers = false;
int lowers = 0; bool lowers = false;
int others = 0; bool others = false;
int size; int size;
int i; int i;
for (i = 0; new[i]; i++) { for (i = 0; NULL != new[i]; i++) {
if (isdigit (new[i])) if (isdigit (new[i])) {
digits++; digits = true;
else if (isupper (new[i])) } else if (isupper (new[i])) {
uppers++; uppers = true;
else if (islower (new[i])) } else if (islower (new[i])) {
lowers++; lowers = true;
else } else {
others++; others = true;
}
} }
/* /*
@ -120,27 +127,33 @@ static int simple (unused const char *old, const char *new)
*/ */
size = 9; size = 9;
if (digits) if (digits) {
size--; size--;
if (uppers) }
if (uppers) {
size--; size--;
if (lowers) }
if (lowers) {
size--; size--;
if (others) }
if (others) {
size--; size--;
}
if (size <= i) if (size <= i) {
return 0; return false;
}
return 1; return true;
} }
static char *str_lower (char *string) static char *str_lower (char *string)
{ {
char *cp; char *cp;
for (cp = string; *cp; cp++) for (cp = string; NULL != *cp; cp++) {
*cp = tolower (*cp); *cp = tolower (*cp);
}
return string; return string;
} }
@ -160,8 +173,9 @@ static const char *password_check (const char *old, const char *new,
#endif #endif
#endif #endif
if (strcmp (new, old) == 0) if (strcmp (new, old) == 0) {
return _("no change"); return _("no change");
}
newmono = str_lower (xstrdup (new)); newmono = str_lower (xstrdup (new));
oldmono = str_lower (xstrdup (old)); oldmono = str_lower (xstrdup (old));
@ -169,33 +183,32 @@ static const char *password_check (const char *old, const char *new,
strcpy (wrapped, oldmono); strcpy (wrapped, oldmono);
strcat (wrapped, oldmono); strcat (wrapped, oldmono);
if (palindrome (oldmono, newmono)) if (palindrome (oldmono, newmono)) {
msg = _("a palindrome"); msg = _("a palindrome");
} else if (strcmp (oldmono, newmono) == 0) {
if (!msg && strcmp (oldmono, newmono) == 0)
msg = _("case changes only"); msg = _("case changes only");
} else if (similar (oldmono, newmono)) {
if (!msg && similar (oldmono, newmono))
msg = _("too similar"); msg = _("too similar");
} else if (simple (old, new)) {
if (!msg && simple (old, new))
msg = _("too simple"); msg = _("too simple");
} else if (strstr (wrapped, newmono) != NULL) {
if (!msg && strstr (wrapped, newmono))
msg = _("rotated"); msg = _("rotated");
} else {
#ifdef HAVE_LIBCRACK #ifdef HAVE_LIBCRACK
/* /*
* Invoke Alec Muffett's cracklib routines. * Invoke Alec Muffett's cracklib routines.
*/ */
if (!msg && (dictpath = getdef_str ("CRACKLIB_DICTPATH"))) dictpath = getdef_str ("CRACKLIB_DICTPATH");
if (NULL != dictpath) {
#ifdef HAVE_LIBCRACK_PW #ifdef HAVE_LIBCRACK_PW
msg = FascistCheckPw (new, dictpath, pwdp); msg = FascistCheckPw (new, dictpath, pwdp);
#else #else
msg = FascistCheck (new, dictpath); msg = FascistCheck (new, dictpath);
#endif #endif
}
#endif #endif
}
strzero (newmono); strzero (newmono);
strzero (oldmono); strzero (oldmono);
strzero (wrapped); strzero (wrapped);
@ -206,8 +219,8 @@ static const char *password_check (const char *old, const char *new,
return msg; return msg;
} }
/*ARGSUSED*/ /*ARGSUSED*/
static const char *obscure_msg (const char *old, const char *new, static const char *obscure_msg (const char *old, const char *new,
const struct passwd *pwdp) const struct passwd *pwdp)
{ {
int maxlen, oldlen, newlen; int maxlen, oldlen, newlen;
@ -218,50 +231,60 @@ static const char *password_check (const char *old, const char *new,
oldlen = strlen (old); oldlen = strlen (old);
newlen = strlen (new); newlen = strlen (new);
if (newlen < getdef_num ("PASS_MIN_LEN", 0)) if (newlen < getdef_num ("PASS_MIN_LEN", 0)) {
return _("too short"); return _("too short");
}
/* /*
* Remaining checks are optional. * Remaining checks are optional.
*/ */
if (!getdef_bool ("OBSCURE_CHECKS_ENAB")) if (!getdef_bool ("OBSCURE_CHECKS_ENAB")) {
return NULL; return NULL;
}
msg = password_check (old, new, pwdp); msg = password_check (old, new, pwdp);
if (msg) if (NULL != msg) {
return msg; return msg;
}
if ((result = getdef_str ("ENCRYPT_METHOD")) == NULL) { result = getdef_str ("ENCRYPT_METHOD");
if (NULL == result) {
/* The traditional crypt() truncates passwords to 8 chars. It is /* The traditional crypt() truncates passwords to 8 chars. It is
possible to circumvent the above checks by choosing an easy possible to circumvent the above checks by choosing an easy
8-char password and adding some random characters to it... 8-char password and adding some random characters to it...
Example: "password$%^&*123". So check it again, this time Example: "password$%^&*123". So check it again, this time
truncated to the maximum length. Idea from npasswd. --marekm */ truncated to the maximum length. Idea from npasswd. --marekm */
if (getdef_bool ("MD5_CRYPT_ENAB")) if (getdef_bool ("MD5_CRYPT_ENAB")) {
return NULL; return NULL;
}
} else { } else {
if ( !strcmp (result, "MD5") if ( (strcmp (result, "MD5") == 0)
#ifdef USE_SHA_CRYPT #ifdef USE_SHA_CRYPT
|| !strcmp (result, "SHA256") || (strcmp (result, "SHA256") == 0)
|| !strcmp (result, "SHA512") || (strcmp (result, "SHA512") == 0)
#endif #endif
) ) {
return NULL; return NULL;
}
} }
maxlen = getdef_num ("PASS_MAX_LEN", 8); maxlen = getdef_num ("PASS_MAX_LEN", 8);
if (oldlen <= maxlen && newlen <= maxlen) if ( (oldlen <= maxlen)
&& (newlen <= maxlen)) {
return NULL; return NULL;
}
new1 = xstrdup (new); new1 = xstrdup (new);
old1 = xstrdup (old); old1 = xstrdup (old);
if (newlen > maxlen) if (newlen > maxlen) {
new1[maxlen] = '\0'; new1[maxlen] = '\0';
if (oldlen > maxlen) }
if (oldlen > maxlen) {
old1[maxlen] = '\0'; old1[maxlen] = '\0';
}
msg = password_check (old1, new1, pwdp); msg = password_check (old1, new1, pwdp);
@ -285,7 +308,7 @@ int obscure (const char *old, const char *new, const struct passwd *pwdp)
{ {
const char *msg = obscure_msg (old, new, pwdp); const char *msg = obscure_msg (old, new, pwdp);
if (msg) { if (NULL != msg) {
printf (_("Bad password: %s. "), msg); printf (_("Bad password: %s. "), msg);
return 0; return 0;
} }