Blame SOURCES/check-password.patch

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