mrc0mmand / rpms / openldap

Forked from rpms/openldap 3 years ago
Clone

Blame SOURCES/check-password.patch

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