File 0017-retain-non-blocking-status-on-socket.patch of Package openldap2.5013

Patch is authored by Ales Novak <alnovak@suse.com> on 2017-04-07.

diff -rupN openldap-2.4.41/doc/man/man3/lber-sockbuf.3 openldap-2.4.41-patched/doc/man/man3/lber-sockbuf.3
--- openldap-2.4.41/doc/man/man3/lber-sockbuf.3	2017-04-19 11:32:49.848929439 +0200
+++ openldap-2.4.41-patched/doc/man/man3/lber-sockbuf.3	2017-04-19 14:14:14.135372384 +0200
@@ -103,6 +103,12 @@ the
 should be NULL to disable and non-NULL to enable the non-blocking state.
 The return value will be 1 for success, \-1 otherwise.
 .TP
+.B LBER_SB_OPT_GET_NONBLOCK
+Return non-blocking state of the file descriptor associated to
+the
+.BR Sockbuf .
+The return value will be 1 if the socket is non-blocking, -1 otherwise.
+.TP
 .B LBER_SB_OPT_DRAIN
 Flush (read and discard) all available input on the
 .BR Sockbuf .
diff -rupN openldap-2.4.41/include/lber.h openldap-2.4.41-patched/include/lber.h
--- openldap-2.4.41/include/lber.h	2017-04-19 11:32:49.832929370 +0200
+++ openldap-2.4.41-patched/include/lber.h	2017-04-19 14:14:34.919452048 +0200
@@ -135,6 +135,7 @@ typedef struct lber_memory_fns {
 #define LBER_SB_OPT_SET_FD		2
 #define LBER_SB_OPT_HAS_IO		3
 #define LBER_SB_OPT_SET_NONBLOCK	4
+#define LBER_SB_OPT_GET_NONBLOCK	5
 #define LBER_SB_OPT_GET_SSL		7
 #define LBER_SB_OPT_DATA_READY		8
 #define LBER_SB_OPT_SET_READAHEAD	9
diff -rupN openldap-2.4.41/libraries/liblber/sockbuf.c openldap-2.4.41-patched/libraries/liblber/sockbuf.c
--- openldap-2.4.41/libraries/liblber/sockbuf.c	2017-04-19 11:32:49.840929404 +0200
+++ openldap-2.4.41-patched/libraries/liblber/sockbuf.c	2017-04-19 14:12:30.978977411 +0200
@@ -117,6 +117,10 @@ ber_sockbuf_ctrl( Sockbuf *sb, int opt,
 				? -1 : 1;
 			break;
 
+        case LBER_SB_OPT_GET_NONBLOCK:
+            ret = ber_pvt_socket_get_nonblock( sb->sb_fd);
+            break;
+
 		case LBER_SB_OPT_DRAIN: {
 				/* Drain the data source to enable possible errors (e.g.
 				 * TLS) to be propagated to the upper layers
@@ -354,6 +358,17 @@ ber_pvt_socket_set_nonblock( ber_socket_
 #endif
 }
 
+int
+ber_pvt_socket_get_nonblock( ber_socket_t sd )
+{
+#ifdef HAVE_FCNTL
+   int flags = fcntl( sd, F_GETFL);
+   return (flags & O_NONBLOCK)?1:0;
+#elif defined( FIONBIO )
+   return -1;
+#endif
+}
+
 int
 ber_int_sb_init( Sockbuf *sb )
 {
diff -rupN openldap-2.4.41/libraries/libldap/tls2.c openldap-2.4.41-patched/libraries/libldap/tls2.c
--- openldap-2.4.41/libraries/libldap/tls2.c	2017-04-19 11:32:49.840929404 +0200
+++ openldap-2.4.41-patched/libraries/libldap/tls2.c	2017-04-19 14:13:45.583262990 +0200
@@ -818,6 +818,7 @@ ldap_int_tls_start ( LDAP *ld, LDAPConn
 #ifdef LDAP_USE_NON_BLOCKING_TLS
 	struct timeval start_time_tv, tv, tv0;
 	ber_socket_t	sd = AC_SOCKET_ERROR;
+    int was_nonblocking;
 #endif /* LDAP_USE_NON_BLOCKING_TLS */
 
 	if ( !conn )
@@ -838,6 +839,7 @@ ldap_int_tls_start ( LDAP *ld, LDAPConn
 	(void) tls_init( tls_imp );
 
 #ifdef LDAP_USE_NON_BLOCKING_TLS
+    was_nonblocking = ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_NONBLOCK, NULL );
 	/*
 	 * Use non-blocking io during SSL Handshake when a timeout is configured
 	 */
@@ -924,7 +926,7 @@ ldap_int_tls_start ( LDAP *ld, LDAPConn
 			}
 		}
 	}
-	if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
+	if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 && ! was_nonblocking ) {
 		ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_SET_NONBLOCK, NULL );
 	}
 #endif /* LDAP_USE_NON_BLOCKING_TLS */