57672d
--- a/check_password.c	2009-10-31 18:59:06.000000000 +0100
57672d
+++ b/check_password.c	2014-12-17 12:25:00.148900907 +0100
57672d
@@ -10,7 +10,7 @@
57672d
 #include <slap.h>
57672d
 
57672d
 #ifdef HAVE_CRACKLIB
57672d
-#include "crack.h"
57672d
+#include <crack.h>
57672d
 #endif
57672d
 
57672d
 #if defined(DEBUG)
57672d
@@ -34,18 +34,77 @@
57672d
 #define PASSWORD_TOO_SHORT_SZ \
57672d
 	"Password for dn=\"%s\" is too short (%d/6)"
57672d
 #define PASSWORD_QUALITY_SZ \
57672d
-	"Password for dn=\"%s\" does not pass required number of strength checks (%d of %d)"
57672d
+	"Password for dn=\"%s\" does not pass required number of strength checks for the required character sets (%d of %d)"
57672d
 #define BAD_PASSWORD_SZ \
57672d
 	"Bad password for dn=\"%s\" because %s"
57672d
+#define UNKNOWN_ERROR_SZ \
57672d
+	"An unknown error occurred, please see your systems administrator"
57672d
 
57672d
 typedef int (*validator) (char*);
57672d
-static int read_config_file (char *);
57672d
+static int read_config_file ();
57672d
 static validator valid_word (char *);
57672d
 static int set_quality (char *);
57672d
 static int set_cracklib (char *);
57672d
 
57672d
 int check_password (char *pPasswd, char **ppErrStr, Entry *pEntry);
57672d
 
57672d
+struct config_entry {
57672d
+	char* key;
57672d
+	char* value;
57672d
+	char* def_value;
57672d
+} config_entries[] = { { "minPoints", NULL, "3"},
57672d
+		       { "useCracklib", NULL, "1"},
57672d
+		       { "minUpper", NULL, "0"},
57672d
+		       { "minLower", NULL, "0"},
57672d
+		       { "minDigit", NULL, "0"},
57672d
+		       { "minPunct", NULL, "0"},
57672d
+		       { NULL, NULL, NULL }};
57672d
+
57672d
+int get_config_entry_int(char* entry) {
57672d
+	struct config_entry* centry = config_entries;
57672d
+
57672d
+	int i = 0;
57672d
+	char* key = centry[i].key;
57672d
+	while (key != NULL) {
57672d
+		if ( strncmp(key, entry, strlen(key)) == 0 ) {
57672d
+			if ( centry[i].value == NULL ) {
57672d
+				return atoi(centry[i].def_value);
57672d
+			}
57672d
+			else {
57672d
+				return atoi(centry[i].value);
57672d
+			}
57672d
+		}
57672d
+		i++;
57672d
+		key = centry[i].key;
57672d
+	}
57672d
+
57672d
+	return -1;
57672d
+}
57672d
+
57672d
+void dealloc_config_entries() {
57672d
+	struct config_entry* centry = config_entries;
57672d
+
57672d
+	int i = 0;
57672d
+	while (centry[i].key != NULL) {
57672d
+		if ( centry[i].value != NULL ) {
57672d
+			ber_memfree(centry[i].value);
57672d
+		}
57672d
+		i++;
57672d
+	}
57672d
+}
57672d
+
57672d
+char* chomp(char *s)
57672d
+{
57672d
+	char* t = ber_memalloc(strlen(s)+1);
57672d
+	strncpy (t,s,strlen(s)+1);
57672d
+
57672d
+	if ( t[strlen(t)-1] == '\n' ) {
57672d
+		t[strlen(t)-1] = '\0';
57672d
+	}
57672d
+
57672d
+	return t;
57672d
+}
57672d
+
57672d
 static int set_quality (char *value)
57672d
 {
57672d
 #if defined(DEBUG)
57672d
@@ -84,12 +143,12 @@
57672d
 		char * parameter;
57672d
 		validator dealer;
57672d
 	} list[] = { { "minPoints", set_quality },
57672d
-		{ "useCracklib", set_cracklib },
57672d
-		{ "minUpper", set_digit },
57672d
-		{ "minLower", set_digit },
57672d
-		{ "minDigit", set_digit },
57672d
-		{ "minPunct", set_digit },
57672d
-		{ NULL, NULL } };
57672d
+		     { "useCracklib", set_cracklib },
57672d
+		     { "minUpper", set_digit },
57672d
+		     { "minLower", set_digit },
57672d
+		     { "minDigit", set_digit },
57672d
+		     { "minPunct", set_digit },
57672d
+		     { NULL, NULL } };
57672d
 	int index = 0;
57672d
 
57672d
 #if defined(DEBUG)
57672d
@@ -98,7 +157,7 @@
57672d
 
57672d
 	while (list[index].parameter != NULL) {
57672d
 		if (strlen(word) == strlen(list[index].parameter) &&
57672d
-				strcmp(list[index].parameter, word) == 0) {
57672d
+		    strcmp(list[index].parameter, word) == 0) {
57672d
 #if defined(DEBUG)
57672d
 			syslog(LOG_NOTICE, "check_password: Parameter accepted.");
57672d
 #endif
57672d
@@ -114,13 +173,15 @@
57672d
 	return NULL;
57672d
 }
57672d
 
57672d
-static int read_config_file (char *keyWord)
57672d
+static int read_config_file ()
57672d
 {
57672d
 	FILE * config;
57672d
 	char * line;
57672d
 	int returnValue =  -1;
57672d
 
57672d
-	if ((line = ber_memcalloc(260, sizeof(char))) == NULL) {
57672d
+	line = ber_memcalloc(260, sizeof(char));
57672d
+
57672d
+	if ( line == NULL ) {
57672d
 		return returnValue;
57672d
 	}
57672d
 
57672d
@@ -133,6 +194,8 @@
57672d
 		return returnValue;
57672d
 	}
57672d
 
57672d
+	returnValue = 0;
57672d
+
57672d
 	while (fgets(line, 256, config) != NULL) {
57672d
 		char *start = line;
57672d
 		char *word, *value;
57672d
@@ -145,23 +208,40 @@
57672d
 
57672d
 		while (isspace(*start) && isascii(*start)) start++;
57672d
 
57672d
-		if (! isascii(*start))
57672d
+		/* If we've got punctuation, just skip the line. */
57672d
+		if ( ispunct(*start)) {
57672d
+#if defined(DEBUG)
57672d
+			/* Debug traces to syslog. */
57672d
+			syslog(LOG_NOTICE, "check_password: Skipped line |%s|", line);
57672d
+#endif
57672d
 			continue;
57672d
+		}
57672d
 
57672d
-		if ((word = strtok(start, " \t")) && (dealer = valid_word(word)) && (strcmp(keyWord,word)==0)) {
57672d
-			if ((value = strtok(NULL, " \t")) == NULL)
57672d
-				continue;
57672d
+		if( isascii(*start)) {
57672d
+
57672d
+			struct config_entry* centry = config_entries;
57672d
+			int i = 0;
57672d
+			char* keyWord = centry[i].key;
57672d
+			if ((word = strtok(start, " \t")) && (value = strtok(NULL, " \t"))) {
57672d
+				while ( keyWord != NULL ) {
57672d
+					if ((strncmp(keyWord,word,strlen(keyWord)) == 0) && (dealer = valid_word(word)) ) {
57672d
 
57672d
 #if defined(DEBUG)
57672d
-			syslog(LOG_NOTICE, "check_password: Word = %s, value = %s", word, value);
57672d
+						syslog(LOG_NOTICE, "check_password: Word = %s, value = %s", word, value);
57672d
 #endif
57672d
 
57672d
-			returnValue = (*dealer)(value);
57672d
+						centry[i].value = chomp(value);
57672d
+						break;
57672d
+					}
57672d
+					i++;
57672d
+					keyWord = centry[i].key;
57672d
+				}
57672d
+			}
57672d
 		}
57672d
 	}
57672d
-
57672d
 	fclose(config);
57672d
 	ber_memfree(line);
57672d
+
57672d
 	return returnValue;
57672d
 }
57672d
 
57672d
@@ -170,7 +250,7 @@
57672d
 	if (curlen < nextlen + MEMORY_MARGIN) {
57672d
 #if defined(DEBUG)
57672d
 		syslog(LOG_WARNING, "check_password: Reallocating szErrStr from %d to %d",
57672d
-				curlen, nextlen + MEMORY_MARGIN);
57672d
+		       curlen, nextlen + MEMORY_MARGIN);
57672d
 #endif
57672d
 		ber_memfree(*target);
57672d
 		curlen = nextlen + MEMORY_MARGIN;
57672d
@@ -180,7 +260,7 @@
57672d
 	return curlen;
57672d
 }
57672d
 
57672d
-	int
57672d
+int
57672d
 check_password (char *pPasswd, char **ppErrStr, Entry *pEntry)
57672d
 {
57672d
 
57672d
@@ -210,20 +290,22 @@
57672d
 	nLen = strlen (pPasswd);
57672d
 	if ( nLen < 6) {
57672d
 		mem_len = realloc_error_message(&szErrStr, mem_len,
57672d
-				strlen(PASSWORD_TOO_SHORT_SZ) +
57672d
-				strlen(pEntry->e_name.bv_val) + 1);
57672d
+						strlen(PASSWORD_TOO_SHORT_SZ) +
57672d
+						strlen(pEntry->e_name.bv_val) + 1);
57672d
 		sprintf (szErrStr, PASSWORD_TOO_SHORT_SZ, pEntry->e_name.bv_val, nLen);
57672d
 		goto fail;
57672d
 	}
57672d
 
57672d
-	/* Read config file */
57672d
-	minQuality = read_config_file("minPoints");
57672d
+	if (read_config_file() == -1) {
57672d
+		syslog(LOG_ERR, "Warning: Could not read values from config file %s. Using defaults.", CONFIG_FILE);
57672d
+	}
57672d
 
57672d
-	useCracklib = read_config_file("useCracklib");
57672d
-	minUpper = read_config_file("minUpper");
57672d
-	minLower = read_config_file("minLower");
57672d
-	minDigit = read_config_file("minDigit");
57672d
-	minPunct = read_config_file("minPunct");
57672d
+	minQuality = get_config_entry_int("minPoints");
57672d
+	useCracklib = get_config_entry_int("useCracklib");
57672d
+	minUpper = get_config_entry_int("minUpper");
57672d
+	minLower = get_config_entry_int("minLower");
57672d
+	minDigit = get_config_entry_int("minDigit");
57672d
+	minPunct = get_config_entry_int("minPunct");
57672d
 
57672d
 	/** The password must have at least minQuality strength points with one
57672d
 	 * point for the first occurrance of a lower, upper, digit and
57672d
@@ -232,8 +314,6 @@
57672d
 
57672d
 	for ( i = 0; i < nLen; i++ ) {
57672d
 
57672d
-		if ( nQuality >= minQuality ) break;
57672d
-
57672d
 		if ( islower (pPasswd[i]) ) {
57672d
 			minLower--;
57672d
 			if ( !nLower && (minLower < 1)) {
57672d
@@ -279,12 +359,23 @@
57672d
 		}
57672d
 	}
57672d
 
57672d
-	if ( nQuality < minQuality ) {
57672d
+	/*
57672d
+	 * If you have a required field, then it should be required in the strength
57672d
+	 * checks.
57672d
+	 */
57672d
+
57672d
+	if (
57672d
+		(minLower > 0 ) ||
57672d
+		(minUpper > 0 ) ||
57672d
+		(minDigit > 0 ) ||
57672d
+		(minPunct > 0 ) ||
57672d
+		(nQuality < minQuality)
57672d
+		) {
57672d
 		mem_len = realloc_error_message(&szErrStr, mem_len,
57672d
-				strlen(PASSWORD_QUALITY_SZ) +
57672d
-				strlen(pEntry->e_name.bv_val) + 2);
57672d
+						strlen(PASSWORD_QUALITY_SZ) +
57672d
+						strlen(pEntry->e_name.bv_val) + 2);
57672d
 		sprintf (szErrStr, PASSWORD_QUALITY_SZ, pEntry->e_name.bv_val,
57672d
-				nQuality, minQuality);
57672d
+			 nQuality, minQuality);
57672d
 		goto fail;
57672d
 	}
57672d
 
57672d
@@ -306,7 +397,7 @@
57672d
 		for ( j = 0; j < 3; j++ ) {
57672d
 
57672d
 			snprintf (filename, FILENAME_MAXLEN - 1, "%s.%s", \
57672d
-					CRACKLIB_DICTPATH, ext[j]);
57672d
+				  CRACKLIB_DICTPATH, ext[j]);
57672d
 
57672d
 			if (( fp = fopen ( filename, "r")) == NULL ) {
57672d
 
57672d
@@ -326,9 +417,9 @@
57672d
 			r = (char *) FascistCheck (pPasswd, CRACKLIB_DICTPATH);
57672d
 			if ( r != NULL ) {
57672d
 				mem_len = realloc_error_message(&szErrStr, mem_len,
57672d
-						strlen(BAD_PASSWORD_SZ) +
57672d
-						strlen(pEntry->e_name.bv_val) +
57672d
-						strlen(r));
57672d
+								strlen(BAD_PASSWORD_SZ) +
57672d
+								strlen(pEntry->e_name.bv_val) +
57672d
+								strlen(r));
57672d
 				sprintf (szErrStr, BAD_PASSWORD_SZ, pEntry->e_name.bv_val, r);
57672d
 				goto fail;
57672d
 			}
57672d
@@ -342,15 +433,15 @@
57672d
 	}
57672d
 
57672d
 #endif
57672d
-
57672d
+	dealloc_config_entries();
57672d
 	*ppErrStr = strdup ("");
57672d
 	ber_memfree(szErrStr);
57672d
 	return (LDAP_SUCCESS);
57672d
 
57672d
 fail:
57672d
+	dealloc_config_entries();
57672d
 	*ppErrStr = strdup (szErrStr);
57672d
 	ber_memfree(szErrStr);
57672d
 	return (EXIT_FAILURE);
57672d
 
57672d
 }
57672d
-