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