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