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