zrhoffman / rpms / 389-ds-base

Forked from rpms/389-ds-base 3 years ago
Clone

Blame SOURCES/0069-Ticket-49327-password-expired-control-not-sent-durin.patch

61f723
From 3ab8a78cd27cc8d2ad7a2b322a4fe73c43a3db08 Mon Sep 17 00:00:00 2001
61f723
From: Mark Reynolds <mreynolds@redhat.com>
61f723
Date: Thu, 14 Sep 2017 15:47:53 -0400
61f723
Subject: [PATCH] Ticket 49327 - password expired control not sent during grace
61f723
 logins
61f723
61f723
Bug Description:  When a password is expired, but within the grace login limit,
61f723
                  we should still send the expired control even though we allowed
61f723
                  the bind.
61f723
61f723
Fix Description:  new_new_passwd() returned a variety of result codes that required
61f723
                  the caller to set the response controls.  This was hard to read and
61f723
                  process.  Instead I added all the controls inside the function, and
61f723
                  return success or failure to the caller.
61f723
61f723
https://pagure.io/389-ds-base/issue/49327
61f723
61f723
Reviewed by: gparente & tbordaz (Thanks!!)
61f723
61f723
(cherry picked from commit fbd32c4e27af9f331ee3a42dec944895a6efe2ad)
61f723
---
61f723
 ldap/servers/plugins/replication/repl_extop.c |   5 +-
61f723
 ldap/servers/slapd/bind.c                     |  18 +-
61f723
 ldap/servers/slapd/proto-slap.h               |   3 +-
61f723
 ldap/servers/slapd/pw_mgmt.c                  | 453 +++++++++++++-------------
61f723
 ldap/servers/slapd/saslbind.c                 |  20 +-
61f723
 5 files changed, 238 insertions(+), 261 deletions(-)
61f723
61f723
diff --git a/ldap/servers/plugins/replication/repl_extop.c b/ldap/servers/plugins/replication/repl_extop.c
61f723
index a39d918..96ad7dd 100644
61f723
--- a/ldap/servers/plugins/replication/repl_extop.c
61f723
+++ b/ldap/servers/plugins/replication/repl_extop.c
61f723
@@ -1173,8 +1173,9 @@ send_response:
61f723
 			 * On the supplier, we need to close the connection so
61f723
 			 * that the RA will restart a new session in a clear state 
61f723
 			 */
61f723
-			slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "multimaster_extop_StartNSDS50ReplicationRequest - "
61f723
-				"already acquired replica: disconnect conn=%d\n", connid);
61f723
+			slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, 
61f723
+			              "multimaster_extop_StartNSDS50ReplicationRequest - "
61f723
+			              "already acquired replica: disconnect conn=%" PRIu64 "\n", connid);
61f723
 			slapi_disconnect_server(conn);
61f723
             
61f723
 		}
61f723
diff --git a/ldap/servers/slapd/bind.c b/ldap/servers/slapd/bind.c
61f723
index d6c7668..e6cad7f 100644
61f723
--- a/ldap/servers/slapd/bind.c
61f723
+++ b/ldap/servers/slapd/bind.c
61f723
@@ -673,8 +673,7 @@ do_bind( Slapi_PBlock *pb )
61f723
             slapi_entry_free(referral);
61f723
             goto free_and_return;
61f723
         } else if (auto_bind || rc == SLAPI_BIND_SUCCESS || rc == SLAPI_BIND_ANONYMOUS) {
61f723
-            long t;
61f723
-            char* authtype = NULL;
61f723
+            char *authtype = NULL;
61f723
             /* rc is SLAPI_BIND_SUCCESS or SLAPI_BIND_ANONYMOUS */
61f723
             if(auto_bind) {
61f723
                 rc = SLAPI_BIND_SUCCESS;
61f723
@@ -761,19 +760,8 @@ do_bind( Slapi_PBlock *pb )
61f723
                                          slapi_ch_strdup(slapi_sdn_get_ndn(sdn)),
61f723
                                          NULL, NULL, NULL, bind_target_entry);
61f723
                     if (!slapi_be_is_flag_set(be, SLAPI_BE_FLAG_REMOTE_DATA)) {
61f723
-                        /* check if need new password before sending 
61f723
-                           the bind success result */
61f723
-                        myrc = need_new_pw(pb, &t, bind_target_entry, pw_response_requested);
61f723
-                        switch (myrc) {
61f723
-                        case 1:
61f723
-                            (void)slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
61f723
-                            break;
61f723
-                        case 2:
61f723
-                            (void)slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRING, t);
61f723
-                            break;
61f723
-                        default:
61f723
-                            break;
61f723
-                        }
61f723
+                        /* check if need new password before sending the bind success result */
61f723
+                        myrc = need_new_pw(pb, bind_target_entry, pw_response_requested);
61f723
                     }
61f723
                 }
61f723
                 if (auth_response_requested) {
61f723
diff --git a/ldap/servers/slapd/proto-slap.h b/ldap/servers/slapd/proto-slap.h
61f723
index 9696ead..0ba61d7 100644
61f723
--- a/ldap/servers/slapd/proto-slap.h
61f723
+++ b/ldap/servers/slapd/proto-slap.h
61f723
@@ -972,7 +972,7 @@ int plugin_call_acl_verify_syntax ( Slapi_PBlock *pb, Slapi_Entry *e, char **err
61f723
  * pw_mgmt.c
61f723
  */
61f723
 void pw_init( void );
61f723
-int need_new_pw( Slapi_PBlock *pb, long *t,  Slapi_Entry *e, int pwresponse_req );
61f723
+int need_new_pw(Slapi_PBlock *pb, Slapi_Entry *e, int pwresponse_req);
61f723
 int update_pw_info( Slapi_PBlock *pb , char *old_pw );
61f723
 int check_pw_syntax( Slapi_PBlock *pb, const Slapi_DN *sdn, Slapi_Value **vals, 
61f723
 	char **old_pw, Slapi_Entry *e, int mod_op );
61f723
@@ -982,7 +982,6 @@ void get_old_pw( Slapi_PBlock *pb, const Slapi_DN *sdn, char **old_pw);
61f723
 int check_account_lock( Slapi_PBlock *pb, Slapi_Entry * bind_target_entry, int pwresponse_req, int account_inactivation_only /*no wire/no pw policy*/);
61f723
 int check_pw_minage( Slapi_PBlock *pb, const Slapi_DN *sdn, struct berval **vals) ;
61f723
 void add_password_attrs( Slapi_PBlock *pb, Operation *op, Slapi_Entry *e );
61f723
-
61f723
 int add_shadow_ext_password_attrs(Slapi_PBlock *pb, Slapi_Entry **e);
61f723
 
61f723
 /*
61f723
diff --git a/ldap/servers/slapd/pw_mgmt.c b/ldap/servers/slapd/pw_mgmt.c
61f723
index 7252c08..b06e3f1 100644
61f723
--- a/ldap/servers/slapd/pw_mgmt.c
61f723
+++ b/ldap/servers/slapd/pw_mgmt.c
61f723
@@ -22,234 +22,239 @@
61f723
 /* prototypes                                                               */
61f723
 /****************************************************************************/
61f723
 
61f723
-/* need_new_pw() is called when non rootdn bind operation succeeds with authentication */ 
61f723
+/*
61f723
+ * need_new_pw() is called when non rootdn bind operation succeeds with authentication
61f723
+ *
61f723
+ * Return  0 - password is okay
61f723
+ * Return -1 - password is expired, abort bind
61f723
+ */
61f723
 int
61f723
-need_new_pw( Slapi_PBlock *pb, long *t, Slapi_Entry *e, int pwresponse_req )
61f723
+need_new_pw(Slapi_PBlock *pb, Slapi_Entry *e, int pwresponse_req)
61f723
 {
61f723
-	time_t 		cur_time, pw_exp_date;
61f723
-	Slapi_Mods smods;
61f723
-	double		diff_t = 0;
61f723
-	char 		*cur_time_str = NULL;
61f723
-	char *passwordExpirationTime = NULL;
61f723
-	char *timestring;
61f723
-	char *dn;
61f723
-	const Slapi_DN *sdn;
61f723
-	passwdPolicy *pwpolicy = NULL;
61f723
-	int	pwdGraceUserTime = 0;
61f723
-	char graceUserTime[8];
61f723
-
61f723
-	if (NULL == e) {
61f723
-		return (-1);
61f723
-	}
61f723
-	slapi_mods_init (&smods, 0);
61f723
-	sdn = slapi_entry_get_sdn_const( e );
61f723
-	dn = slapi_entry_get_ndn( e );
61f723
-	pwpolicy = new_passwdPolicy(pb, dn);
61f723
-
61f723
-	/* after the user binds with authentication, clear the retry count */
61f723
-	if ( pwpolicy->pw_lockout == 1)
61f723
-	{
61f723
-		if(slapi_entry_attr_get_int( e, "passwordRetryCount") > 0)
61f723
-		{
61f723
-			slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordRetryCount", "0");
61f723
-		}
61f723
-	}
61f723
-
61f723
-	cur_time = current_time();
61f723
-
61f723
-	/* get passwordExpirationTime attribute */
61f723
-	passwordExpirationTime= slapi_entry_attr_get_charptr(e, "passwordExpirationTime");
61f723
-
61f723
-	if (passwordExpirationTime == NULL)
61f723
-	{
61f723
-		/* password expiration date is not set.
61f723
-		 * This is ok for data that has been loaded via ldif2ldbm
61f723
-		 * Set expiration time if needed,
61f723
-		 * don't do further checking and return 0 */
61f723
-		if (pwpolicy->pw_exp == 1) {
61f723
-			pw_exp_date = time_plus_sec(cur_time, pwpolicy->pw_maxage);
61f723
-
61f723
-			timestring = format_genTime (pw_exp_date);
61f723
-			slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordExpirationTime", timestring);
61f723
-			slapi_ch_free_string(&timestring);
61f723
-			slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordExpWarned", "0");
61f723
-			
61f723
-			pw_apply_mods(sdn, &smods);
61f723
-		} else if (pwpolicy->pw_lockout == 1) {
61f723
-			pw_apply_mods(sdn, &smods);
61f723
-		}
61f723
-		slapi_mods_done(&smods);
61f723
-		return ( 0 );
61f723
-	}
61f723
-
61f723
-	pw_exp_date = parse_genTime(passwordExpirationTime);
61f723
-
61f723
-	slapi_ch_free_string(&passwordExpirationTime);
61f723
-
61f723
-	/* Check if password has been reset */
61f723
-	if ( pw_exp_date == NO_TIME ) {
61f723
-
61f723
-		/* check if changing password is required */  
61f723
-		if ( pwpolicy->pw_must_change ) {
61f723
-			/* set c_needpw for this connection to be true.  this client 
61f723
-			   now can only change its own password */
61f723
-			pb->pb_conn->c_needpw = 1;
61f723
-			*t=0;
61f723
-			/* We need to include "changeafterreset" error in
61f723
-			 * passwordpolicy response control. So, we will not be
61f723
-			 * done here. We remember this scenario by (c_needpw=1)
61f723
-			 * and check it before sending the control from various
61f723
-			 * places. We will also add LDAP_CONTROL_PWEXPIRED control
61f723
-			 * as the return value used to be (1).
61f723
-			 */
61f723
-			goto skip;
61f723
-		}
61f723
-		/* Mark that first login occured */
61f723
-		pw_exp_date = NOT_FIRST_TIME;
61f723
-		timestring = format_genTime(pw_exp_date);
61f723
-		slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordExpirationTime", timestring);
61f723
-		slapi_ch_free_string(&timestring);
61f723
-	}
61f723
+    time_t cur_time, pw_exp_date;
61f723
+    Slapi_Mods smods;
61f723
+    double diff_t = 0;
61f723
+    char *cur_time_str = NULL;
61f723
+    char *passwordExpirationTime = NULL;
61f723
+    char *timestring;
61f723
+    char *dn;
61f723
+    const Slapi_DN *sdn;
61f723
+    passwdPolicy *pwpolicy = NULL;
61f723
+    int pwdGraceUserTime = 0;
61f723
+    char graceUserTime[16] = {0};
61f723
+    Connection *pb_conn = NULL;
61f723
+    long t;
61f723
+
61f723
+    if (NULL == e) {
61f723
+        return (-1);
61f723
+    }
61f723
+    slapi_mods_init(&smods, 0);
61f723
+    sdn = slapi_entry_get_sdn_const(e);
61f723
+    dn = slapi_entry_get_ndn(e);
61f723
+    pwpolicy = new_passwdPolicy(pb, dn);
61f723
+
61f723
+    /* after the user binds with authentication, clear the retry count */
61f723
+    if (pwpolicy->pw_lockout == 1) {
61f723
+        if (slapi_entry_attr_get_int(e, "passwordRetryCount") > 0) {
61f723
+            slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordRetryCount", "0");
61f723
+        }
61f723
+    }
61f723
+
61f723
+    cur_time = current_time();
61f723
+
61f723
+    /* get passwordExpirationTime attribute */
61f723
+    passwordExpirationTime = slapi_entry_attr_get_charptr(e, "passwordExpirationTime");
61f723
+
61f723
+    if (passwordExpirationTime == NULL) {
61f723
+        /* password expiration date is not set.
61f723
+         * This is ok for data that has been loaded via ldif2ldbm
61f723
+         * Set expiration time if needed,
61f723
+         * don't do further checking and return 0 */
61f723
+        if (pwpolicy->pw_exp == 1) {
61f723
+            pw_exp_date = time_plus_sec(cur_time, pwpolicy->pw_maxage);
61f723
+
61f723
+            timestring = format_genTime(pw_exp_date);
61f723
+            slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordExpirationTime", timestring);
61f723
+            slapi_ch_free_string(&timestring);
61f723
+            slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordExpWarned", "0");
61f723
+
61f723
+            pw_apply_mods(sdn, &smods);
61f723
+        } else if (pwpolicy->pw_lockout == 1) {
61f723
+            pw_apply_mods(sdn, &smods);
61f723
+        }
61f723
+        slapi_mods_done(&smods);
61f723
+        return (0);
61f723
+    }
61f723
+
61f723
+    pw_exp_date = parse_genTime(passwordExpirationTime);
61f723
+
61f723
+    slapi_ch_free_string(&passwordExpirationTime);
61f723
+
61f723
+    slapi_pblock_get(pb, SLAPI_CONNECTION, &pb_conn);
61f723
+
61f723
+    /* Check if password has been reset */
61f723
+    if (pw_exp_date == NO_TIME) {
61f723
+
61f723
+        /* check if changing password is required */
61f723
+        if (pwpolicy->pw_must_change) {
61f723
+            /* set c_needpw for this connection to be true.  this client
61f723
+               now can only change its own password */
61f723
+            pb_conn->c_needpw = 1;
61f723
+            t = 0;
61f723
+            /* We need to include "changeafterreset" error in
61f723
+             * passwordpolicy response control. So, we will not be
61f723
+             * done here. We remember this scenario by (c_needpw=1)
61f723
+             * and check it before sending the control from various
61f723
+             * places. We will also add LDAP_CONTROL_PWEXPIRED control
61f723
+             * as the return value used to be (1).
61f723
+             */
61f723
+            goto skip;
61f723
+        }
61f723
+        /* Mark that first login occured */
61f723
+        pw_exp_date = NOT_FIRST_TIME;
61f723
+        timestring = format_genTime(pw_exp_date);
61f723
+        slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordExpirationTime", timestring);
61f723
+        slapi_ch_free_string(&timestring);
61f723
+    }
61f723
 
61f723
 skip:
61f723
-	/* if password never expires, don't need to go on; return 0 */
61f723
-	if ( pwpolicy->pw_exp == 0 ) {
61f723
-		/* check for "changeafterreset" condition */
61f723
-		if (pb->pb_conn->c_needpw == 1) {
61f723
-			if (pwresponse_req) {
61f723
-				slapi_pwpolicy_make_response_control ( pb, -1, -1, LDAP_PWPOLICY_CHGAFTERRESET );
61f723
-			} 
61f723
-			slapi_add_pwd_control ( pb, LDAP_CONTROL_PWEXPIRED, 0);
61f723
-		}
61f723
-		pw_apply_mods(sdn, &smods);
61f723
-		slapi_mods_done(&smods);
61f723
-		return ( 0 );
61f723
-	}
61f723
-
61f723
-	/* check if password expired.  If so, abort bind. */
61f723
-	cur_time_str = format_genTime ( cur_time );
61f723
-	if ((pw_exp_date != NO_TIME) && (pw_exp_date != NOT_FIRST_TIME) &&
61f723
-	    (diff_t = difftime(pw_exp_date, parse_genTime(cur_time_str))) <= 0) {
61f723
-		slapi_ch_free_string(&cur_time_str); /* only need this above */
61f723
-		/* password has expired. Check the value of 
61f723
-		 * passwordGraceUserTime and compare it
61f723
-		 * against the value of passwordGraceLimit */
61f723
-		pwdGraceUserTime = slapi_entry_attr_get_int( e, "passwordGraceUserTime");
61f723
-		if ( pwpolicy->pw_gracelimit > pwdGraceUserTime ) {
61f723
-			pwdGraceUserTime++;
61f723
-			sprintf ( graceUserTime, "%d", pwdGraceUserTime );
61f723
-			slapi_mods_add_string(&smods, LDAP_MOD_REPLACE,
61f723
-				"passwordGraceUserTime", graceUserTime);
61f723
-			pw_apply_mods(sdn, &smods);
61f723
-			slapi_mods_done(&smods);
61f723
-			if (pwresponse_req) {
61f723
-				/* check for "changeafterreset" condition */
61f723
-				if (pb->pb_conn->c_needpw == 1) {
61f723
-					slapi_pwpolicy_make_response_control( pb, -1, 
61f723
-						((pwpolicy->pw_gracelimit) - pwdGraceUserTime),
61f723
-						LDAP_PWPOLICY_CHGAFTERRESET);
61f723
-				} else {
61f723
-					slapi_pwpolicy_make_response_control( pb, -1, 
61f723
-						((pwpolicy->pw_gracelimit) - pwdGraceUserTime),
61f723
-						-1);
61f723
-				}
61f723
-			}
61f723
-			
61f723
-			if (pb->pb_conn->c_needpw == 1) {
61f723
-				slapi_add_pwd_control ( pb, LDAP_CONTROL_PWEXPIRED, 0);
61f723
-			}
61f723
-			return ( 0 );
61f723
-		}
61f723
-
61f723
-		/* password expired and user exceeded limit of grace attemps.
61f723
-		 * Send result and also the control */
61f723
-		slapi_add_pwd_control ( pb, LDAP_CONTROL_PWEXPIRED, 0);
61f723
-		if (pwresponse_req) {
61f723
-			slapi_pwpolicy_make_response_control ( pb, -1, -1, LDAP_PWPOLICY_PWDEXPIRED );
61f723
-		}
61f723
-		slapi_send_ldap_result ( pb, LDAP_INVALID_CREDENTIALS, NULL,
61f723
-			"password expired!", 0, NULL );
61f723
-		
61f723
-		/* abort bind */
61f723
-		/* pass pb to do_unbind().  pb->pb_op->o_opid and 
61f723
-		   pb->pb_op->o_tag are not right but I don't see 
61f723
-		   do_unbind() checking for those.   We might need to 
61f723
-		   create a pb for unbind operation.  Also do_unbind calls
61f723
-		   pre and post ops.  Maybe we don't want to call them */
61f723
-		if (pb->pb_conn && (LDAP_VERSION2 == pb->pb_conn->c_ldapversion)) {
61f723
-			/* We close the connection only with LDAPv2 connections */
61f723
-			disconnect_server( pb->pb_conn, pb->pb_op->o_connid,
61f723
-				pb->pb_op->o_opid, SLAPD_DISCONNECT_UNBIND, 0);
61f723
-		}
61f723
-		/* Apply current modifications */
61f723
-		pw_apply_mods(sdn, &smods);
61f723
-		slapi_mods_done(&smods);
61f723
-		return (-1);
61f723
-	}
61f723
-	slapi_ch_free((void **) &cur_time_str );
61f723
-
61f723
-	/* check if password is going to expire within "passwordWarning" */
61f723
-	/* Note that if pw_exp_date is NO_TIME or NOT_FIRST_TIME,
61f723
-	 * we must send warning first and this changes the expiration time.
61f723
-	 * This is done just below since diff_t is 0 
61f723
-	 */
61f723
-	if ( diff_t <= pwpolicy->pw_warning ) {
61f723
-		int pw_exp_warned = 0;
61f723
-		
61f723
-		pw_exp_warned = slapi_entry_attr_get_int( e, "passwordExpWarned");
61f723
-		if ( !pw_exp_warned ){
61f723
-			/* first time send out a warning */
61f723
-			/* reset the expiration time to current + warning time 
61f723
-			 * and set passwordExpWarned to true
61f723
-			 */
61f723
-			if (pb->pb_conn->c_needpw != 1) {
61f723
-				pw_exp_date = time_plus_sec(cur_time, pwpolicy->pw_warning);
61f723
-			}
61f723
-			
61f723
-			timestring = format_genTime(pw_exp_date);
61f723
-			slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordExpirationTime", timestring);
61f723
-			slapi_ch_free_string(&timestring);
61f723
-
61f723
-			slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordExpWarned", "1");
61f723
-			
61f723
-			*t = pwpolicy->pw_warning;
61f723
-
61f723
-		} else {
61f723
-			*t = (long)diff_t; /* jcm: had to cast double to long */
61f723
-		}
61f723
-
61f723
-		pw_apply_mods(sdn, &smods);
61f723
-		slapi_mods_done(&smods);
61f723
-		if (pwresponse_req) {
61f723
-			/* check for "changeafterreset" condition */
61f723
-			if (pb->pb_conn->c_needpw == 1) {
61f723
-					slapi_pwpolicy_make_response_control( pb, *t, -1,
61f723
-						LDAP_PWPOLICY_CHGAFTERRESET);
61f723
-				} else {
61f723
-					slapi_pwpolicy_make_response_control( pb, *t, -1,
61f723
-						-1);
61f723
-				}
61f723
-		}
61f723
-
61f723
-		if (pb->pb_conn->c_needpw == 1) {
61f723
-			slapi_add_pwd_control ( pb, LDAP_CONTROL_PWEXPIRED, 0);
61f723
-		}
61f723
-		return (2);
61f723
-	} else {
61f723
-		if (pwresponse_req && pwpolicy->pw_send_expiring) {
61f723
-			slapi_pwpolicy_make_response_control( pb, diff_t, -1, -1);
61f723
-			slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRING, diff_t);
61f723
-		}
61f723
-	}
61f723
-
61f723
-	pw_apply_mods(sdn, &smods);
61f723
-	slapi_mods_done(&smods);
61f723
-	/* Leftover from "changeafterreset" condition */
61f723
-	if (pb->pb_conn->c_needpw == 1) {
61f723
-		slapi_add_pwd_control ( pb, LDAP_CONTROL_PWEXPIRED, 0);
61f723
-	}
61f723
-	/* passes checking, return 0 */
61f723
-	return( 0 );
61f723
+    /* if password never expires, don't need to go on; return 0 */
61f723
+    if (pwpolicy->pw_exp == 0) {
61f723
+        /* check for "changeafterreset" condition */
61f723
+        if (pb_conn->c_needpw == 1) {
61f723
+            if (pwresponse_req) {
61f723
+                slapi_pwpolicy_make_response_control(pb, -1, -1, LDAP_PWPOLICY_CHGAFTERRESET);
61f723
+            }
61f723
+            slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
61f723
+        }
61f723
+        pw_apply_mods(sdn, &smods);
61f723
+        slapi_mods_done(&smods);
61f723
+        return (0);
61f723
+    }
61f723
+
61f723
+    /* check if password expired.  If so, abort bind. */
61f723
+    cur_time_str = format_genTime(cur_time);
61f723
+    if ((pw_exp_date != NO_TIME) && (pw_exp_date != NOT_FIRST_TIME) &&
61f723
+        (diff_t = difftime(pw_exp_date, parse_genTime(cur_time_str))) <= 0) {
61f723
+        slapi_ch_free_string(&cur_time_str); /* only need this above */
61f723
+        /* password has expired. Check the value of
61f723
+         * passwordGraceUserTime and compare it
61f723
+         * against the value of passwordGraceLimit */
61f723
+        pwdGraceUserTime = slapi_entry_attr_get_int(e, "passwordGraceUserTime");
61f723
+        if (pwpolicy->pw_gracelimit > pwdGraceUserTime) {
61f723
+            pwdGraceUserTime++;
61f723
+            sprintf(graceUserTime, "%d", pwdGraceUserTime);
61f723
+            slapi_mods_add_string(&smods, LDAP_MOD_REPLACE,
61f723
+                                  "passwordGraceUserTime", graceUserTime);
61f723
+            pw_apply_mods(sdn, &smods);
61f723
+            slapi_mods_done(&smods);
61f723
+            if (pwresponse_req) {
61f723
+                /* check for "changeafterreset" condition */
61f723
+                if (pb_conn->c_needpw == 1) {
61f723
+                    slapi_pwpolicy_make_response_control(pb, -1,
61f723
+                                                         ((pwpolicy->pw_gracelimit) - pwdGraceUserTime),
61f723
+                                                         LDAP_PWPOLICY_CHGAFTERRESET);
61f723
+                } else {
61f723
+                    slapi_pwpolicy_make_response_control(pb, -1,
61f723
+                                                         ((pwpolicy->pw_gracelimit) - pwdGraceUserTime),
61f723
+                                                         -1);
61f723
+                }
61f723
+            }
61f723
+            slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
61f723
+            return (0);
61f723
+        }
61f723
+
61f723
+        /* password expired and user exceeded limit of grace attemps.
61f723
+         * Send result and also the control */
61f723
+        slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
61f723
+        if (pwresponse_req) {
61f723
+            slapi_pwpolicy_make_response_control(pb, -1, -1, LDAP_PWPOLICY_PWDEXPIRED);
61f723
+        }
61f723
+        slapi_send_ldap_result(pb, LDAP_INVALID_CREDENTIALS, NULL,
61f723
+                               "password expired!", 0, NULL);
61f723
+
61f723
+        /* abort bind */
61f723
+        /* pass pb to do_unbind().  pb->pb_op->o_opid and
61f723
+           pb->pb_op->o_tag are not right but I don't see
61f723
+           do_unbind() checking for those.   We might need to
61f723
+           create a pb for unbind operation.  Also do_unbind calls
61f723
+           pre and post ops.  Maybe we don't want to call them */
61f723
+        if (pb_conn && (LDAP_VERSION2 == pb_conn->c_ldapversion)) {
61f723
+            Operation *pb_op = NULL;
61f723
+            slapi_pblock_get(pb, SLAPI_OPERATION, &pb_op);
61f723
+            /* We close the connection only with LDAPv2 connections */
61f723
+            disconnect_server(pb_conn, pb_op->o_connid,
61f723
+                              pb_op->o_opid, SLAPD_DISCONNECT_UNBIND, 0);
61f723
+        }
61f723
+        /* Apply current modifications */
61f723
+        pw_apply_mods(sdn, &smods);
61f723
+        slapi_mods_done(&smods);
61f723
+        return (-1);
61f723
+    }
61f723
+    slapi_ch_free((void **)&cur_time_str);
61f723
+
61f723
+    /* check if password is going to expire within "passwordWarning" */
61f723
+    /* Note that if pw_exp_date is NO_TIME or NOT_FIRST_TIME,
61f723
+     * we must send warning first and this changes the expiration time.
61f723
+     * This is done just below since diff_t is 0
61f723
+     */
61f723
+    if (diff_t <= pwpolicy->pw_warning) {
61f723
+        int pw_exp_warned = 0;
61f723
+
61f723
+        pw_exp_warned = slapi_entry_attr_get_int(e, "passwordExpWarned");
61f723
+        if (!pw_exp_warned) {
61f723
+            /* first time send out a warning */
61f723
+            /* reset the expiration time to current + warning time
61f723
+             * and set passwordExpWarned to true
61f723
+             */
61f723
+            if (pb_conn->c_needpw != 1) {
61f723
+                pw_exp_date = time_plus_sec(cur_time, pwpolicy->pw_warning);
61f723
+            }
61f723
+
61f723
+            timestring = format_genTime(pw_exp_date);
61f723
+            slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordExpirationTime", timestring);
61f723
+            slapi_ch_free_string(&timestring);
61f723
+
61f723
+            slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "passwordExpWarned", "1");
61f723
+
61f723
+            t = pwpolicy->pw_warning;
61f723
+
61f723
+        } else {
61f723
+            t = (long)diff_t; /* jcm: had to cast double to long */
61f723
+        }
61f723
+
61f723
+        pw_apply_mods(sdn, &smods);
61f723
+        slapi_mods_done(&smods);
61f723
+        if (pwresponse_req) {
61f723
+            /* check for "changeafterreset" condition */
61f723
+            if (pb_conn->c_needpw == 1) {
61f723
+                slapi_pwpolicy_make_response_control(pb, t, -1, LDAP_PWPOLICY_CHGAFTERRESET);
61f723
+            } else {
61f723
+                slapi_pwpolicy_make_response_control(pb, t, -1, -1);
61f723
+            }
61f723
+        }
61f723
+
61f723
+        if (pb_conn->c_needpw == 1) {
61f723
+            slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
61f723
+        } else {
61f723
+            slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRING, t);
61f723
+        }
61f723
+        return (0);
61f723
+    } else {
61f723
+        if (pwresponse_req && pwpolicy->pw_send_expiring) {
61f723
+            slapi_pwpolicy_make_response_control(pb, diff_t, -1, -1);
61f723
+            slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRING, diff_t);
61f723
+        }
61f723
+    }
61f723
+
61f723
+    pw_apply_mods(sdn, &smods);
61f723
+    slapi_mods_done(&smods);
61f723
+    /* Leftover from "changeafterreset" condition */
61f723
+    if (pb_conn->c_needpw == 1) {
61f723
+        slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
61f723
+    }
61f723
+    /* passes checking, return 0 */
61f723
+    return (0);
61f723
 }
61f723
 
61f723
 /* Called once from main */
61f723
diff --git a/ldap/servers/slapd/saslbind.c b/ldap/servers/slapd/saslbind.c
61f723
index dd0c4fb..134f5aa 100644
61f723
--- a/ldap/servers/slapd/saslbind.c
61f723
+++ b/ldap/servers/slapd/saslbind.c
61f723
@@ -859,7 +859,6 @@ ids_sasl_mech_supported(Slapi_PBlock *pb, const char *mech)
61f723
 void ids_sasl_check_bind(Slapi_PBlock *pb)
61f723
 {
61f723
     int rc, isroot;
61f723
-    long t;
61f723
     sasl_conn_t *sasl_conn;
61f723
     struct propctx *propctx;
61f723
     sasl_ssf_t *ssfp;
61f723
@@ -1096,23 +1095,8 @@ sasl_check_result:
61f723
         set_db_default_result_handlers(pb);
61f723
 
61f723
         /* check password expiry */
61f723
-        if (!isroot) {
61f723
-            int pwrc;
61f723
-
61f723
-            pwrc = need_new_pw(pb, &t, bind_target_entry, pwresponse_requested);
61f723
-            
61f723
-            switch (pwrc) {
61f723
-            case 1:
61f723
-                slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRED, 0);
61f723
-                break;
61f723
-            case 2:
61f723
-                slapi_add_pwd_control(pb, LDAP_CONTROL_PWEXPIRING, t);
61f723
-                break;
61f723
-            case -1:
61f723
-                goto out;
61f723
-            default:
61f723
-                break;
61f723
-            }
61f723
+        if (!isroot && need_new_pw(pb, bind_target_entry, pwresponse_requested) == -1) {
61f723
+            goto out;
61f723
         }
61f723
 
61f723
         /* attach the sasl data */
61f723
-- 
61f723
2.9.5
61f723