andykimpe / rpms / 389-ds-base

Forked from rpms/389-ds-base 5 months ago
Clone
dc8c34
From 785d862ed3617f04bf26403cfcc5676bb6860468 Mon Sep 17 00:00:00 2001
dc8c34
From: Noriko Hosoi <nhosoi@redhat.com>
dc8c34
Date: Mon, 3 Aug 2015 18:49:58 -0700
dc8c34
Subject: [PATCH 357/363] Ticket #48228 - wrong password check if
dc8c34
 passwordInHistory is decreased.
dc8c34
dc8c34
Bug Description: When N passwords to be remembered (passwordInHistroy)
dc8c34
and N passwords are remembered, decreasing the passwordInHistory value
dc8c34
to M (< N) does not allow to use the oldest password which should have
dc8c34
been discarded from the history and should be allowed.
dc8c34
dc8c34
Fix Description: Before checking if the password is in the history or
dc8c34
not, adding a check the passwordInHistory value (M) is less than the
dc8c34
count of passwords remembered (N).  If M < N, discard the (N-M) oldest
dc8c34
passwords.
dc8c34
dc8c34
https://fedorahosted.org/389/ticket/48228
dc8c34
dc8c34
Reviewed by mreynolds@redhat.com (Thank you, Mark!!)
dc8c34
dc8c34
(cherry picked from commit 1a119125856006543aae0520b5800a8b52c3b049)
dc8c34
(cherry picked from commit dd85ee9c9ac24f1b141dd806943de236d2e44c90)
dc8c34
(cherry picked from commit 67c816451dc331aed50808c61d00b5c9d4d20890)
dc8c34
(cherry picked from commit fd1c326539c5645d5c17df35bdec07c261a828db)
dc8c34
---
dc8c34
 ldap/servers/slapd/pw.c | 192 ++++++++++++++++++++++++++++--------------------
dc8c34
 1 file changed, 114 insertions(+), 78 deletions(-)
dc8c34
dc8c34
diff --git a/ldap/servers/slapd/pw.c b/ldap/servers/slapd/pw.c
dc8c34
index 782d1c8..14a4b0f 100644
dc8c34
--- a/ldap/servers/slapd/pw.c
dc8c34
+++ b/ldap/servers/slapd/pw.c
dc8c34
@@ -608,7 +608,7 @@ update_pw_info ( Slapi_PBlock *pb , char *old_pw) {
dc8c34
 
dc8c34
 	/* update passwordHistory */
dc8c34
 	if ( old_pw != NULL && pwpolicy->pw_history == 1 ) {
dc8c34
-		update_pw_history(pb, sdn, old_pw);
dc8c34
+		(void)update_pw_history(pb, sdn, old_pw);
dc8c34
 		slapi_ch_free ( (void**)&old_pw );
dc8c34
 	}
dc8c34
 
dc8c34
@@ -647,7 +647,7 @@ update_pw_info ( Slapi_PBlock *pb , char *old_pw) {
dc8c34
 	 * for this special case to ensure we reset the expiration date properly. */
dc8c34
 	if ((internal_op && pwpolicy->pw_must_change && (!pb->pb_conn || slapi_dn_isroot(pb->pb_conn->c_dn))) ||
dc8c34
 	    (!internal_op && pwpolicy->pw_must_change &&
dc8c34
-	    ((target_dn && bind_dn && strcasecmp(target_dn, bind_dn)) && pw_is_pwp_admin(pb, pwpolicy)))){
dc8c34
+	     ((target_dn && bind_dn && strcasecmp(target_dn, bind_dn)) && pw_is_pwp_admin(pb, pwpolicy)))) {
dc8c34
 		pw_exp_date = NO_TIME;
dc8c34
 	} else if ( pwpolicy->pw_exp == 1 ) {
dc8c34
 		Slapi_Entry *pse = NULL;
dc8c34
@@ -993,6 +993,7 @@ check_pw_syntax_ext ( Slapi_PBlock *pb, const Slapi_DN *sdn, Slapi_Value **vals,
dc8c34
 
dc8c34
 	/* get the entry and check for the password history if this is called by a modify operation */
dc8c34
 	if ( mod_op ) {
dc8c34
+retry:
dc8c34
 		/* retrieve the entry */
dc8c34
 		e = get_entry ( pb, dn );
dc8c34
 		if ( e == NULL ) {
dc8c34
@@ -1002,19 +1003,21 @@ check_pw_syntax_ext ( Slapi_PBlock *pb, const Slapi_DN *sdn, Slapi_Value **vals,
dc8c34
 
dc8c34
 		/* check for password history */
dc8c34
 		if ( pwpolicy->pw_history == 1 ) {
dc8c34
+			Slapi_Value **va = NULL;
dc8c34
 			attr = attrlist_find(e->e_attrs, "passwordHistory");
dc8c34
-			if (attr && 
dc8c34
-				!valueset_isempty(&attr->a_present_values))
dc8c34
-			{
dc8c34
-				Slapi_Value **va= attr_get_present_values(attr);
dc8c34
+			if (attr && !valueset_isempty(&attr->a_present_values)) {
dc8c34
+				/* Resetting password history array if necessary. */
dc8c34
+				if (0 == update_pw_history(pb, sdn, NULL)) {
dc8c34
+					/* There was an update in the password history.  Retry... */
dc8c34
+					slapi_entry_free(e); 
dc8c34
+					goto retry;
dc8c34
+				}
dc8c34
+				va = attr_get_present_values(attr);
dc8c34
 				if ( pw_in_history( va, vals[0] ) == 0 ) {
dc8c34
 					if ( pwresponse_req == 1 ) {
dc8c34
-						slapi_pwpolicy_make_response_control ( pb, -1, -1,
dc8c34
-							LDAP_PWPOLICY_PWDINHISTORY );
dc8c34
+						slapi_pwpolicy_make_response_control(pb, -1, -1, LDAP_PWPOLICY_PWDINHISTORY);
dc8c34
 					}
dc8c34
-					pw_send_ldap_result ( pb, 
dc8c34
-						LDAP_CONSTRAINT_VIOLATION, NULL,
dc8c34
-						"password in history", 0, NULL );
dc8c34
+					pw_send_ldap_result(pb, LDAP_CONSTRAINT_VIOLATION, NULL, "password in history", 0, NULL);
dc8c34
 					slapi_entry_free( e ); 
dc8c34
 					delete_passwdPolicy(&pwpolicy);
dc8c34
 					return ( 1 );
dc8c34
@@ -1023,27 +1026,18 @@ check_pw_syntax_ext ( Slapi_PBlock *pb, const Slapi_DN *sdn, Slapi_Value **vals,
dc8c34
 
dc8c34
 			/* get current password. check it and remember it  */
dc8c34
 			attr = attrlist_find(e->e_attrs, "userpassword");
dc8c34
-			if (attr && !valueset_isempty(&attr->a_present_values))
dc8c34
-			{
dc8c34
-				Slapi_Value **va= valueset_get_valuearray(&attr->a_present_values);
dc8c34
-				if (slapi_is_encoded((char*)slapi_value_get_string(vals[0]))) 
dc8c34
-				{
dc8c34
-					if (slapi_attr_value_find(attr, (struct berval *)slapi_value_get_berval(vals[0])) == 0 )
dc8c34
-					{
dc8c34
-						pw_send_ldap_result ( pb, 
dc8c34
-										   LDAP_CONSTRAINT_VIOLATION ,NULL,
dc8c34
-										   "password in history", 0, NULL);
dc8c34
+			if (attr && !valueset_isempty(&attr->a_present_values)) {
dc8c34
+				va = valueset_get_valuearray(&attr->a_present_values);
dc8c34
+				if (slapi_is_encoded((char*)slapi_value_get_string(vals[0]))) {
dc8c34
+					if (slapi_attr_value_find(attr, (struct berval *)slapi_value_get_berval(vals[0])) == 0 ) {
dc8c34
+						pw_send_ldap_result(pb, LDAP_CONSTRAINT_VIOLATION, NULL, "password in history", 0, NULL);
dc8c34
 						slapi_entry_free( e ); 
dc8c34
 						delete_passwdPolicy(&pwpolicy);
dc8c34
 						return ( 1 );
dc8c34
 					}
dc8c34
-				} else 
dc8c34
-				{
dc8c34
-					if ( slapi_pw_find_sv ( va, vals[0] ) == 0 )
dc8c34
-					{
dc8c34
-						pw_send_ldap_result ( pb, 
dc8c34
-										   LDAP_CONSTRAINT_VIOLATION ,NULL,
dc8c34
-										   "password in history", 0, NULL);
dc8c34
+				} else {
dc8c34
+					if ( slapi_pw_find_sv ( va, vals[0] ) == 0 ) {
dc8c34
+						pw_send_ldap_result(pb, LDAP_CONSTRAINT_VIOLATION, NULL, "password in history", 0, NULL);
dc8c34
 						slapi_entry_free( e ); 
dc8c34
 						delete_passwdPolicy(&pwpolicy);
dc8c34
 						return ( 1 );
dc8c34
@@ -1089,22 +1083,56 @@ check_pw_syntax_ext ( Slapi_PBlock *pb, const Slapi_DN *sdn, Slapi_Value **vals,
dc8c34
 
dc8c34
 }
dc8c34
 
dc8c34
+/*
dc8c34
+ * Basically, h0 and h1 must be longer than GENERALIZED_TIME_LENGTH.
dc8c34
+ */
dc8c34
+static int
dc8c34
+pw_history_cmp(const void *h0, const void *h1)
dc8c34
+{
dc8c34
+	size_t h0sz = 0;
dc8c34
+	size_t h1sz = 0;
dc8c34
+	if (!h0) {
dc8c34
+		if (!h1) {
dc8c34
+			return 0;
dc8c34
+		} else {
dc8c34
+			return -1;
dc8c34
+		}
dc8c34
+	} else {
dc8c34
+		if (!h1) {
dc8c34
+			return 1;
dc8c34
+		} else {
dc8c34
+			size_t delta;
dc8c34
+			h0sz = strlen(h0);
dc8c34
+			h1sz = strlen(h1);
dc8c34
+			delta = h0sz - h1sz;
dc8c34
+			if (!delta) {
dc8c34
+				return delta;
dc8c34
+			}
dc8c34
+			if (h0sz < GENERALIZED_TIME_LENGTH) {
dc8c34
+				/* too short for the history str. */
dc8c34
+				return 0;
dc8c34
+			}
dc8c34
+		}
dc8c34
+	}
dc8c34
+	return PL_strncmp(h0, h1, GENERALIZED_TIME_LENGTH);
dc8c34
+}
dc8c34
+
dc8c34
+
dc8c34
 static int
dc8c34
 update_pw_history( Slapi_PBlock *pb, const Slapi_DN *sdn, char *old_pw )
dc8c34
 {
dc8c34
-	time_t 		t, old_t, cur_time;
dc8c34
-	int 		i = 0, oldest = 0;
dc8c34
-	int res;
dc8c34
-	Slapi_Entry		*e;
dc8c34
-	Slapi_Attr 	*attr;
dc8c34
+	time_t cur_time;
dc8c34
+	int res = 1; /* no update, by default */
dc8c34
+	Slapi_Entry *e = NULL;
dc8c34
 	LDAPMod 	attribute;
dc8c34
-	char 		*values_replace[25]; /* 2-24 passwords in history */
dc8c34
 	LDAPMod 	*list_of_mods[2];
dc8c34
 	Slapi_PBlock 	mod_pb;
dc8c34
-	char		*history_str;
dc8c34
-	char		*str;
dc8c34
+	char		*str = NULL;
dc8c34
 	passwdPolicy *pwpolicy = NULL;
dc8c34
 	const char *dn = slapi_sdn_get_dn(sdn);
dc8c34
+	char **values_replace = NULL;
dc8c34
+	int vacnt = 0;
dc8c34
+	int vacnt_todelete = 0;
dc8c34
 
dc8c34
 	pwpolicy = new_passwdPolicy(pb, dn);
dc8c34
 
dc8c34
@@ -1112,46 +1140,56 @@ update_pw_history( Slapi_PBlock *pb, const Slapi_DN *sdn, char *old_pw )
dc8c34
 	e = get_entry ( pb, dn );
dc8c34
 	if ( e == NULL ) {
dc8c34
 		delete_passwdPolicy(&pwpolicy);
dc8c34
-		return ( 1 );
dc8c34
+		return res;
dc8c34
 	}
dc8c34
 
dc8c34
-	history_str = (char *)slapi_ch_malloc(GENERALIZED_TIME_LENGTH + strlen(old_pw) + 1);
dc8c34
-	/* get password history, and find the oldest password in history */
dc8c34
-	cur_time = current_time ();
dc8c34
-	old_t = cur_time;
dc8c34
-	str = format_genTime ( cur_time );
dc8c34
-	attr = attrlist_find(e->e_attrs, "passwordHistory");
dc8c34
-	if (attr && !valueset_isempty(&attr->a_present_values))
dc8c34
-	{
dc8c34
-		Slapi_Value **va= valueset_get_valuearray(&attr->a_present_values);
dc8c34
-		for ( i = oldest = 0 ; 
dc8c34
-			  (va[i] != NULL) && (slapi_value_get_length(va[i]) > 0) ;
dc8c34
-			  i++ ) {
dc8c34
-
dc8c34
-			values_replace[i] = (char*)slapi_value_get_string(va[i]);
dc8c34
-			strncpy( history_str, values_replace[i], GENERALIZED_TIME_LENGTH);
dc8c34
-			history_str[GENERALIZED_TIME_LENGTH] = '\0';
dc8c34
-			if (history_str[GENERALIZED_TIME_LENGTH - 1] != 'Z'){
dc8c34
-				/* The time is not a generalized Time. Probably a password history from 4.x */
dc8c34
-				history_str[GENERALIZED_TIME_LENGTH - 1] = '\0';
dc8c34
-			}
dc8c34
-			t = parse_genTime ( history_str ); 
dc8c34
-			if ( difftime ( t, old_t ) < 0 ) {
dc8c34
-				oldest = i;
dc8c34
-				old_t = t;
dc8c34
-			}
dc8c34
+	/* get password history */
dc8c34
+	values_replace = slapi_entry_attr_get_charray_ext(e, "passwordHistory", &vacnt);
dc8c34
+	if (old_pw) {
dc8c34
+		/* we have a password to replace with the oldest one in the history. */
dc8c34
+		if (!values_replace || !vacnt) { /* This is the first one to store */
dc8c34
+			values_replace = (char **)slapi_ch_calloc(2, sizeof(char *));
dc8c34
+		}
dc8c34
+	} else {
dc8c34
+		/* we are checking the history size if it stores more than the current inhistory count. */
dc8c34
+		if (!values_replace || !vacnt) { /* nothing to revise */
dc8c34
+			res = 1;
dc8c34
+			goto bail;
dc8c34
+		}
dc8c34
+		/* 
dc8c34
+		 * If revising the passwords in the passwordHistory values
dc8c34
+		 * and the password count in the value array is less than the inhistory,
dc8c34
+		 * we have nothing to do.
dc8c34
+		 */
dc8c34
+		if (vacnt <= pwpolicy->pw_inhistory) {
dc8c34
+			res = 1;
dc8c34
+			goto bail;
dc8c34
 		}
dc8c34
+		vacnt_todelete = vacnt - pwpolicy->pw_inhistory;
dc8c34
 	}
dc8c34
-	strcpy ( history_str, str );
dc8c34
-	strcat ( history_str, old_pw );
dc8c34
-	if ( i >= pwpolicy->pw_inhistory ) {
dc8c34
-		/* replace the oldest password in history */
dc8c34
-		values_replace[oldest] = history_str;
dc8c34
-		values_replace[pwpolicy->pw_inhistory] = NULL;
dc8c34
+
dc8c34
+	cur_time = current_time();
dc8c34
+	str = format_genTime(cur_time);
dc8c34
+	/* values_replace is sorted. */
dc8c34
+	if (old_pw) {
dc8c34
+		if ( vacnt >= pwpolicy->pw_inhistory ) {
dc8c34
+			slapi_ch_free_string(&values_replace[0]);
dc8c34
+			values_replace[0] = slapi_ch_smprintf("%s%s", str, old_pw);
dc8c34
+		} else {
dc8c34
+			/* add old_pw at the end of password history */
dc8c34
+			values_replace = (char **)slapi_ch_realloc((char *)values_replace, sizeof(char *) * (vacnt + 2));
dc8c34
+			values_replace[vacnt] = slapi_ch_smprintf("%s%s", str, old_pw);
dc8c34
+			values_replace[vacnt+1] = NULL;
dc8c34
+		}
dc8c34
+		qsort((void *)values_replace, vacnt, (size_t)sizeof(char *), pw_history_cmp);
dc8c34
 	} else {
dc8c34
-		/* add old_pw at the end of password history */
dc8c34
-		values_replace[i] =  history_str;
dc8c34
-		values_replace[++i]=NULL;
dc8c34
+		int i;
dc8c34
+		/* vacnt > pwpolicy->pw_inhistory */
dc8c34
+		for (i = 0; i < vacnt_todelete; i++) {
dc8c34
+			slapi_ch_free_string(&values_replace[i]);
dc8c34
+		}
dc8c34
+		memmove(values_replace, values_replace + vacnt_todelete, sizeof(char *) * pwpolicy->pw_inhistory);
dc8c34
+		values_replace[pwpolicy->pw_inhistory] = NULL;
dc8c34
 	}
dc8c34
 
dc8c34
 	/* modify the attribute */
dc8c34
@@ -1163,22 +1201,20 @@ update_pw_history( Slapi_PBlock *pb, const Slapi_DN *sdn, char *old_pw )
dc8c34
 	list_of_mods[1] = NULL;
dc8c34
 
dc8c34
 	pblock_init(&mod_pb);
dc8c34
-	slapi_modify_internal_set_pb_ext(&mod_pb, sdn, list_of_mods, NULL, NULL, 
dc8c34
-								 pw_get_componentID(), 0);
dc8c34
+	slapi_modify_internal_set_pb_ext(&mod_pb, sdn, list_of_mods, NULL, NULL, pw_get_componentID(), 0);
dc8c34
 	slapi_modify_internal_pb(&mod_pb);
dc8c34
 	slapi_pblock_get(&mod_pb, SLAPI_PLUGIN_INTOP_RESULT, &res;;
dc8c34
 	if (res != LDAP_SUCCESS){
dc8c34
 		LDAPDebug2Args(LDAP_DEBUG_ANY,
dc8c34
 		    "WARNING: passwordPolicy modify error %d on entry '%s'\n", res, dn);
dc8c34
 	}
dc8c34
-
dc8c34
 	pblock_done(&mod_pb);
dc8c34
-
dc8c34
-	slapi_ch_free((void **) &str );
dc8c34
-	slapi_ch_free((void **) &history_str );
dc8c34
+	slapi_ch_free_string(&str);
dc8c34
+bail:
dc8c34
+	slapi_ch_array_free(values_replace);
dc8c34
 	slapi_entry_free( e );
dc8c34
 	delete_passwdPolicy(&pwpolicy);
dc8c34
-	return 0;
dc8c34
+	return res;
dc8c34
 }
dc8c34
 
dc8c34
 static
dc8c34
-- 
dc8c34
2.4.3
dc8c34