[Pkg-openldap-devel] r742 - in openldap/vendor/openldap-release: . build doc/man/man5 include/ac libraries/liblber libraries/libldap libraries/liblunicode/ucdata servers/slapd servers/slapd/overlays tests/scripts

Matthijs Mohlmann matthijs at alioth.debian.org
Fri Nov 10 22:34:04 CET 2006


Author: matthijs
Date: 2006-11-10 22:34:01 +0100 (Fri, 10 Nov 2006)
New Revision: 742

Modified:
   openldap/vendor/openldap-release/CHANGES
   openldap/vendor/openldap-release/build/version.var
   openldap/vendor/openldap-release/configure
   openldap/vendor/openldap-release/configure.in
   openldap/vendor/openldap-release/doc/man/man5/slapd-ldap.5
   openldap/vendor/openldap-release/doc/man/man5/slapd-meta.5
   openldap/vendor/openldap-release/include/ac/socket.h
   openldap/vendor/openldap-release/libraries/liblber/io.c
   openldap/vendor/openldap-release/libraries/liblber/sockbuf.c
   openldap/vendor/openldap-release/libraries/libldap/cyrus.c
   openldap/vendor/openldap-release/libraries/libldap/getdn.c
   openldap/vendor/openldap-release/libraries/libldap/os-ip.c
   openldap/vendor/openldap-release/libraries/libldap/request.c
   openldap/vendor/openldap-release/libraries/libldap/result.c
   openldap/vendor/openldap-release/libraries/libldap/tls.c
   openldap/vendor/openldap-release/libraries/liblunicode/ucdata/ucdata.c
   openldap/vendor/openldap-release/servers/slapd/connection.c
   openldap/vendor/openldap-release/servers/slapd/daemon.c
   openldap/vendor/openldap-release/servers/slapd/operation.c
   openldap/vendor/openldap-release/servers/slapd/overlays/accesslog.c
   openldap/vendor/openldap-release/servers/slapd/overlays/dynlist.c
   openldap/vendor/openldap-release/servers/slapd/overlays/ppolicy.c
   openldap/vendor/openldap-release/servers/slapd/overlays/syncprov.c
   openldap/vendor/openldap-release/servers/slapd/passwd.c
   openldap/vendor/openldap-release/servers/slapd/proto-slap.h
   openldap/vendor/openldap-release/servers/slapd/slap.h
   openldap/vendor/openldap-release/tests/scripts/test043-delta-syncrepl
Log:
Load openldap-2.3.29 into openldap/vendor/openldap-release.


Modified: openldap/vendor/openldap-release/CHANGES
===================================================================
--- openldap/vendor/openldap-release/CHANGES	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/CHANGES	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,22 @@
 OpenLDAP 2.3 Change Log
 
+OpenLDAP 2.3.29 Release
+	Fixed liblber/libldap error codes on Windows (ITS#4606)
+	Fixed libldap string length assert (ITS#4740)
+	Fixed liblunicode case mapping (ITS#4724)
+	Fixed slapd ldapi:// socket permissions (ITS#4709)
+	Fixed slapd c_writewaiters assert (ITS#4696, #4736)
+	Fixed slapo-accesslog purge contextCSN bug (ITS#4704)
+	Fixed slapo-accesslog modify/replace bug (ITS#4728)
+	Fixed slapo-dynlist leaks (ITS#4664)
+	Fixed slapo-ppolicy leaks (ITS#4665)
+	Fixed slapo-syncprov deadlock (ITS#4720)
+	Build environment
+		Added selection of ODBC (ITS#4735)
+	Documentation
+		Fixed slapd-ldap/meta(5) rebind-as-user usage (ITS#4715)
+		Fixed slapd-ldap/meta(5) missing network-timeout (ITS#4718)
+
 OpenLDAP 2.3.28 Release
 	Fixed libldap ldap.conf max line length (ITS#4669)
 	Fixed libldap use keepalive for syncrepl (ITS#4708)

Modified: openldap/vendor/openldap-release/build/version.var
===================================================================
--- openldap/vendor/openldap-release/build/version.var	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/build/version.var	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $OpenLDAP: pkg/ldap/build/version.var,v 1.7.2.69 2006/10/21 16:49:35 kurt Exp $
+# $OpenLDAP: pkg/ldap/build/version.var,v 1.7.2.71 2006/11/10 01:56:15 kurt Exp $
 ## This work is part of OpenLDAP Software <http://www.openldap.org/>.
 ##
 ## Copyright 1998-2006 The OpenLDAP Foundation.
@@ -15,9 +15,9 @@
 ol_package=OpenLDAP
 ol_major=2
 ol_minor=3
-ol_patch=28
-ol_api_inc=20328
+ol_patch=29
+ol_api_inc=20329
 ol_api_current=2
-ol_api_revision=16
+ol_api_revision=17
 ol_api_age=2
-ol_release_date="2006/10/21"
+ol_release_date="2006/11/10"

Modified: openldap/vendor/openldap-release/configure
===================================================================
--- openldap/vendor/openldap-release/configure	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/configure	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,5 @@
 #! /bin/sh
-# From configure.in OpenLDAP: pkg/ldap/configure.in,v 1.560.2.29 2006/04/05 21:36:14 kurt Exp .
+# From configure.in OpenLDAP: pkg/ldap/configure.in,v 1.560.2.31 2006/11/07 20:57:24 ando Exp .
 # Guess values for system-dependent variables and create Makefiles.
 # Generated by GNU Autoconf 2.59.
 #
@@ -1087,6 +1087,7 @@
   --with-tls		  with TLS/SSL support [auto]
   --with-yielding-select  with implicitly yielding select [auto]
   --with-mp               with multiple precision statistics auto|longlong|long|bignum|gmp [auto]
+  --with-odbc             with specific ODBC support iodbc|unixodbc|auto [auto]
 
   --with-gnu-ld           assume the C compiler uses GNU ld [default=no]
   --with-pic              try to use only PIC/non-PIC objects [default=use
@@ -2449,8 +2450,31 @@
   	ol_with_mp="auto"
 fi; # end --with-mp
 
+# OpenLDAP --with-odbc
 
+# Check whether --with-odbc or --without-odbc was given.
+if test "${with_odbc+set}" = set; then
+  withval="$with_odbc"
 
+	ol_arg=invalid
+	for ol_val in auto iodbc unixodbc  ; do
+		if test "$withval" = "$ol_val" ; then
+			ol_arg="$ol_val"
+		fi
+	done
+	if test "$ol_arg" = "invalid" ; then
+		{ { echo "$as_me:$LINENO: error: bad value $withval for --with-odbc" >&5
+echo "$as_me: error: bad value $withval for --with-odbc" >&2;}
+   { (exit 1); exit 1; }; }
+	fi
+	ol_with_odbc="$ol_arg"
+
+else
+  	ol_with_odbc="auto"
+fi; # end --with-odbc
+
+
+
 # Check whether --enable-xxslapdoptions or --disable-xxslapdoptions was given.
 if test "${enable_xxslapdoptions+set}" = set; then
   enableval="$enable_xxslapdoptions"
@@ -5713,7 +5737,7 @@
   ;;
 *-*-irix6*)
   # Find out which ABI we are using.
-  echo '#line 5716 "configure"' > conftest.$ac_ext
+  echo '#line 5740 "configure"' > conftest.$ac_ext
   if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5
   (eval $ac_compile) 2>&5
   ac_status=$?
@@ -7693,11 +7717,11 @@
    -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
    -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
    -e 's:$: $lt_compiler_flag:'`
-   (eval echo "\"\$as_me:7696: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:7720: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>conftest.err)
    ac_status=$?
    cat conftest.err >&5
-   echo "$as_me:7700: \$? = $ac_status" >&5
+   echo "$as_me:7724: \$? = $ac_status" >&5
    if (exit $ac_status) && test -s "$ac_outfile"; then
      # The compiler can only warn and ignore the option if not recognized
      # So say no if there are warnings other than the usual output.
@@ -7955,11 +7979,11 @@
    -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
    -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
    -e 's:$: $lt_compiler_flag:'`
-   (eval echo "\"\$as_me:7958: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:7982: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>conftest.err)
    ac_status=$?
    cat conftest.err >&5
-   echo "$as_me:7962: \$? = $ac_status" >&5
+   echo "$as_me:7986: \$? = $ac_status" >&5
    if (exit $ac_status) && test -s "$ac_outfile"; then
      # The compiler can only warn and ignore the option if not recognized
      # So say no if there are warnings other than the usual output.
@@ -8017,11 +8041,11 @@
    -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
    -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
    -e 's:$: $lt_compiler_flag:'`
-   (eval echo "\"\$as_me:8020: $lt_compile\"" >&5)
+   (eval echo "\"\$as_me:8044: $lt_compile\"" >&5)
    (eval "$lt_compile" 2>out/conftest.err)
    ac_status=$?
    cat out/conftest.err >&5
-   echo "$as_me:8024: \$? = $ac_status" >&5
+   echo "$as_me:8048: \$? = $ac_status" >&5
    if (exit $ac_status) && test -s out/conftest2.$ac_objext
    then
      # The compiler can only warn and ignore the option if not recognized
@@ -10265,7 +10289,7 @@
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<EOF
-#line 10268 "configure"
+#line 10292 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -10363,7 +10387,7 @@
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<EOF
-#line 10366 "configure"
+#line 10390 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -36738,7 +36762,15 @@
 	sql_LIBS="$LIBS"
 	LIBS="$LTHREAD_LIBS"
 
-	echo "$as_me:$LINENO: checking for SQLDriverConnect in -liodbc" >&5
+	if test $ol_with_odbc = auto ; then
+		ol_with_odbc="iodbc unixodbc"
+	fi
+
+	for odbc in $ol_with_odbc ; do
+		if test $ol_link_sql = no ; then
+			case $odbc in
+			iodbc)
+				echo "$as_me:$LINENO: checking for SQLDriverConnect in -liodbc" >&5
 echo $ECHO_N "checking for SQLDriverConnect in -liodbc... $ECHO_C" >&6
 if test "${ac_cv_lib_iodbc_SQLDriverConnect+set}" = set; then
   echo $ECHO_N "(cached) $ECHO_C" >&6
@@ -36808,10 +36840,13 @@
   have_iodbc=no
 fi
 
-	if test $have_iodbc = yes ; then
-		ol_link_sql="-liodbc"
-	else
-		echo "$as_me:$LINENO: checking for SQLDriverConnect in -lodbc" >&5
+				if test $have_iodbc = yes ; then
+					ol_link_sql="-liodbc"
+				fi
+				;;
+
+			unixodbc)
+				echo "$as_me:$LINENO: checking for SQLDriverConnect in -lodbc" >&5
 echo $ECHO_N "checking for SQLDriverConnect in -lodbc... $ECHO_C" >&6
 if test "${ac_cv_lib_odbc_SQLDriverConnect+set}" = set; then
   echo $ECHO_N "(cached) $ECHO_C" >&6
@@ -36881,10 +36916,19 @@
   have_odbc=no
 fi
 
-		if test $have_odbc = yes ; then
-			ol_link_sql="-lodbc"
+				if test $have_odbc = yes ; then
+					ol_link_sql="-lodbc"
+				fi
+				;;
+
+			*)
+				{ { echo "$as_me:$LINENO: error: unknown ODBC library" >&5
+echo "$as_me: error: unknown ODBC library" >&2;}
+   { (exit 1); exit 1; }; }
+				;;
+			esac
 		fi
-	fi
+	done
 
 	LIBS="$sql_LIBS"
 

Modified: openldap/vendor/openldap-release/configure.in
===================================================================
--- openldap/vendor/openldap-release/configure.in	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/configure.in	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,4 +1,4 @@
-dnl $OpenLDAP: pkg/ldap/configure.in,v 1.560.2.30 2006/07/28 14:53:03 kurt Exp $
+dnl $OpenLDAP: pkg/ldap/configure.in,v 1.560.2.31 2006/11/07 20:57:24 ando Exp $
 dnl This work is part of OpenLDAP Software <http://www.openldap.org/>.
 dnl
 dnl Copyright 1998-2006 The OpenLDAP Foundation.
@@ -25,7 +25,7 @@
 dnl Configure.in for OpenLDAP
 AC_COPYRIGHT([[Copyright 1998-2006 The OpenLDAP Foundation. All rights reserved.
 Restrictions apply, see COPYRIGHT and LICENSE files.]])
-AC_REVISION([$OpenLDAP: pkg/ldap/configure.in,v 1.560.2.30 2006/07/28 14:53:03 kurt Exp $])
+AC_REVISION([$OpenLDAP: pkg/ldap/configure.in,v 1.560.2.31 2006/11/07 20:57:24 ando Exp $])
 AC_INIT([OpenLDAP],,[http://www.openldap.org/its/])
 m4_define([AC_PACKAGE_BUGREPORT],[<http://www.openldap.org/its/>])
 AC_CONFIG_SRCDIR(build/version.sh)dnl
@@ -253,6 +253,9 @@
 OL_ARG_WITH(mp,
 	[  --with-mp               with multiple precision statistics auto|longlong|long|bignum|gmp],
 	auto, [auto longlong long bignum gmp yes no])
+OL_ARG_WITH(odbc,
+	[  --with-odbc             with specific ODBC support iodbc|unixodbc|auto],
+	auto, [auto iodbc unixodbc] )
 
 dnl ----------------------------------------------------------------
 dnl Server options
@@ -2169,16 +2172,34 @@
 	sql_LIBS="$LIBS"
 	LIBS="$LTHREAD_LIBS"
 
-	AC_CHECK_LIB(iodbc,SQLDriverConnect,[have_iodbc=yes],[have_iodbc=no])
-	if test $have_iodbc = yes ; then
-		ol_link_sql="-liodbc"
-	else
-		AC_CHECK_LIB(odbc,SQLDriverConnect,[have_odbc=yes],[have_odbc=no])
-		if test $have_odbc = yes ; then
-			ol_link_sql="-lodbc"
-		fi
+	if test $ol_with_odbc = auto ; then
+		ol_with_odbc="iodbc unixodbc"
 	fi
 
+	for odbc in $ol_with_odbc ; do
+		if test $ol_link_sql = no ; then
+			case $odbc in
+			iodbc)
+				AC_CHECK_LIB(iodbc, SQLDriverConnect, [have_iodbc=yes], [have_iodbc=no])
+				if test $have_iodbc = yes ; then
+					ol_link_sql="-liodbc"
+				fi
+				;;
+
+			unixodbc)
+				AC_CHECK_LIB(odbc, SQLDriverConnect, [have_odbc=yes], [have_odbc=no])
+				if test $have_odbc = yes ; then
+					ol_link_sql="-lodbc"
+				fi
+				;;
+
+			*)
+				AC_MSG_ERROR([unknown ODBC library])
+				;;
+			esac
+		fi
+	done
+
 	LIBS="$sql_LIBS"
 
 	if test $ol_link_sql != no ; then

Modified: openldap/vendor/openldap-release/doc/man/man5/slapd-ldap.5
===================================================================
--- openldap/vendor/openldap-release/doc/man/man5/slapd-ldap.5	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/doc/man/man5/slapd-ldap.5	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,7 +1,7 @@
 .TH SLAPD-LDAP 5 "RELEASEDATE" "OpenLDAP LDVERSION"
 .\" Copyright 1998-2006 The OpenLDAP Foundation All Rights Reserved.
 .\" Copying restrictions apply.  See COPYRIGHT/LICENSE.
-.\" $OpenLDAP: pkg/ldap/doc/man/man5/slapd-ldap.5,v 1.24.2.14 2006/05/09 20:00:36 ando Exp $
+.\" $OpenLDAP: pkg/ldap/doc/man/man5/slapd-ldap.5,v 1.24.2.16 2006/10/24 18:02:38 ando Exp $
 .SH NAME
 slapd-ldap \- LDAP backend to slapd
 .SH SYNOPSIS
@@ -305,6 +305,16 @@
 after it has been idle for the specified time.
 
 .TP
+.B network-timeout <time>
+Sets the network timeout value after which
+.BR poll (2)/ select (2) 
+following a 
+.BR connect (2) 
+returns in case of no activity.
+The value is in seconds, and it can be specified as for
+.BR idle-timeout .
+
+.TP
 .B protocol\-version {0,2,3}
 This directive indicates what protocol version must be used to contact
 the remote server.
@@ -325,8 +335,11 @@
 .TP
 .B rebind-as-user {NO|yes}
 If this option is given, the client's bind credentials are remembered
-for rebinds when chasing referrals.  Useful when
-\fBchase-referrals\fP is set to \fByes\fP, useless otherwise.
+for rebinds, when trying to re-establish a broken connection,
+or when chasing a referral, if 
+.B chase-referrals
+is set to
+.IR yes .
 
 .TP
 .B t-f-support {NO|yes|discover}

Modified: openldap/vendor/openldap-release/doc/man/man5/slapd-meta.5
===================================================================
--- openldap/vendor/openldap-release/doc/man/man5/slapd-meta.5	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/doc/man/man5/slapd-meta.5	2006-11-10 21:34:01 UTC (rev 742)
@@ -2,7 +2,7 @@
 .\" Copyright 1998-2006 The OpenLDAP Foundation, All Rights Reserved.
 .\" Copying restrictions apply.  See the COPYRIGHT file.
 .\" Copyright 2001, Pierangelo Masarati, All rights reserved. <ando at sys-net.it>
-.\" $OpenLDAP: pkg/ldap/doc/man/man5/slapd-meta.5,v 1.29.2.12 2006/05/09 20:00:36 ando Exp $
+.\" $OpenLDAP: pkg/ldap/doc/man/man5/slapd-meta.5,v 1.29.2.14 2006/10/24 18:02:38 ando Exp $
 .\"
 .\" Portions of this document should probably be moved to slapd-ldap(5)
 .\" and maybe manual pages for librewrite.
@@ -146,7 +146,11 @@
 .TP
 .B rebind-as-user {NO|yes}
 If this option is given, the client's bind credentials are remembered
-for rebinds when chasing referrals.
+for rebinds, when trying to re-establish a broken connection,
+or when chasing a referral, if 
+.B chase-referrals
+is set to
+.IR yes .
 
 .SH TARGET SPECIFICATION
 Target specification starts with a "uri" directive:
@@ -262,6 +266,18 @@
 .BR slapd-ldap (5).
 
 .TP
+.B network-timeout <time>
+Sets the network timeout value after which
+.BR poll (2)/ select (2) 
+following a 
+.BR connect (2) 
+returns in case of no activity.
+The value is in seconds, and it can be specified as for
+.BR idle-timeout .
+If set before any target specification, it affects all targets, unless
+overridden by any per-target directive.
+
+.TP
 .B nretries {forever|never|<nretries>}
 This directive defines how many times a bind should be retried
 in case of temporary failure in contacting a target.  If defined

Modified: openldap/vendor/openldap-release/include/ac/socket.h
===================================================================
--- openldap/vendor/openldap-release/include/ac/socket.h	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/include/ac/socket.h	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,5 @@
 /* Generic socket.h */
-/* $OpenLDAP: pkg/ldap/include/ac/socket.h,v 1.65.2.2 2006/01/03 22:16:06 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/include/ac/socket.h,v 1.65.2.3 2006/11/07 04:06:21 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -80,6 +80,7 @@
 #undef	sock_errstr
 #define sock_errno()	errno
 #define sock_errstr(e)	STRERROR(e)
+#define sock_errset(e)	errno = (e)
 
 #ifdef HAVE_WINSOCK
 #	define tcp_read( s, buf, len )	recv( s, buf, len, 0 )
@@ -100,8 +101,10 @@
 
 #undef	sock_errno
 #undef	sock_errstr
+#undef	sock_errset
 #define	sock_errno()	WSAGetLastError()
 #define	sock_errstr(e)	ber_pvt_wsa_err2string(e)
+#define	sock_errset(e)	WSASetLastError(e)
 
 LBER_F( char * ) ber_pvt_wsa_err2string LDAP_P((int));
 

Modified: openldap/vendor/openldap-release/libraries/liblber/io.c
===================================================================
--- openldap/vendor/openldap-release/libraries/liblber/io.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/libraries/liblber/io.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,5 @@
 /* io.c - ber general i/o routines */
-/* $OpenLDAP: pkg/ldap/libraries/liblber/io.c,v 1.107.2.4 2006/01/03 22:16:07 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/libraries/liblber/io.c,v 1.107.2.5 2006/11/07 04:06:21 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -500,7 +500,7 @@
 		char buf[sizeof(ber->ber_len)-1];
 		ber_len_t tlen = 0;
 
-		errno = 0;
+		sock_errset(0);
 		sblen=ber_int_sb_read( sb, ber->ber_rwptr,
 			((char *)&ber->ber_len + LENSIZE*2 - 1)-ber->ber_rwptr);
 		if (sblen<=0) return LBER_DEFAULT;
@@ -520,16 +520,16 @@
 						break;
 					/* Is the tag too big? */
 					if (i == sizeof(ber_tag_t)-1) {
-						errno = ERANGE;
+						sock_errset(ERANGE);
 						return LBER_DEFAULT;
 					}
 				}
 				/* Did we run out of bytes? */
 				if ((char *)p == ber->ber_rwptr) {
 #if defined( EWOULDBLOCK )
-					errno = EWOULDBLOCK;
+					sock_errset(EWOULDBLOCK);
 #elif defined( EAGAIN )
-					errno = EAGAIN;
+					sock_errset(EAGAIN);
 #endif			
 					return LBER_DEFAULT;
 				}
@@ -540,9 +540,9 @@
 
 		if ( ber->ber_ptr == ber->ber_rwptr ) {
 #if defined( EWOULDBLOCK )
-			errno = EWOULDBLOCK;
+			sock_errset(EWOULDBLOCK);
 #elif defined( EAGAIN )
-			errno = EAGAIN;
+			sock_errset(EAGAIN);
 #endif			
 			return LBER_DEFAULT;
 		}
@@ -553,15 +553,15 @@
 			unsigned char *p = (unsigned char *)ber->ber_ptr;
 			int llen = *p++ & 0x7f;
 			if (llen > (int)sizeof(ber_len_t)) {
-				errno = ERANGE;
+				sock_errset(ERANGE);
 				return LBER_DEFAULT;
 			}
 			/* Not enough bytes? */
 			if (ber->ber_rwptr - (char *)p < llen) {
 #if defined( EWOULDBLOCK )
-				errno = EWOULDBLOCK;
+				sock_errset(EWOULDBLOCK);
 #elif defined( EAGAIN )
-				errno = EAGAIN;
+				sock_errset(EAGAIN);
 #endif			
 				return LBER_DEFAULT;
 			}
@@ -592,7 +592,7 @@
 
 		/* make sure length is reasonable */
 		if ( ber->ber_len == 0 ) {
-			errno = ERANGE;
+			sock_errset(ERANGE);
 			return LBER_DEFAULT;
 		}
 
@@ -600,7 +600,7 @@
 			ber_log_printf( LDAP_DEBUG_CONNS, ber->ber_debug,
 				"ber_get_next: sockbuf_max_incoming exceeded "
 				"(%ld > %ld)\n", ber->ber_len, sb->sb_max_incoming );
-			errno = ERANGE;
+			sock_errset(ERANGE);
 			return LBER_DEFAULT;
 		}
 
@@ -611,7 +611,7 @@
 			 * already read.
 			 */
 			if ( ber->ber_len < sblen + l ) {
-				errno = ERANGE;
+				sock_errset(ERANGE);
 				return LBER_DEFAULT;
 			}
 			ber->ber_buf = (char *) ber_memalloc_x( ber->ber_len + 1, ber->ber_memctx );
@@ -643,16 +643,16 @@
 		to_go = ber->ber_end - ber->ber_rwptr;
 		assert( to_go > 0 );
 		
-		errno = 0;
+		sock_errset(0);
 		res = ber_int_sb_read( sb, ber->ber_rwptr, to_go );
 		if (res<=0) return LBER_DEFAULT;
 		ber->ber_rwptr+=res;
 		
 		if (res<to_go) {
 #if defined( EWOULDBLOCK )
-			errno = EWOULDBLOCK;
+			sock_errset(EWOULDBLOCK);
 #elif defined( EAGAIN )
-			errno = EAGAIN;
+			sock_errset(EAGAIN);
 #endif			
 			return LBER_DEFAULT;
 		}

Modified: openldap/vendor/openldap-release/libraries/liblber/sockbuf.c
===================================================================
--- openldap/vendor/openldap-release/libraries/liblber/sockbuf.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/libraries/liblber/sockbuf.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,5 @@
 /* sockbuf.c - i/o routines with support for adding i/o layers. */
-/* $OpenLDAP: pkg/ldap/libraries/liblber/sockbuf.c,v 1.60.2.5 2006/01/03 22:16:08 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/libraries/liblber/sockbuf.c,v 1.60.2.6 2006/11/07 04:06:21 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -467,23 +467,8 @@
 /*
  * 32-bit Windows Socket API (under Windows NT or Windows 95)
  */
-	{
-		int rc;
+	return recv( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
 
-		rc = recv( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
-
-#ifdef HAVE_WINSOCK
-		if ( rc < 0 ) {
-			int err;
-
-			err = WSAGetLastError();
-			errno = err;
-		}
-#endif
-
-		return rc;
-	}
-
 #elif defined( HAVE_NCSA )
 /*
  * NCSA Telnet TCP/IP stack (under DOS)
@@ -520,19 +505,8 @@
 /*
  * 32-bit Windows Socket API (under Windows NT or Windows 95)
  */
-	{
-		int rc = send( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
+	return send( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
 
-#ifdef HAVE_WINSOCK
-		if ( rc < 0 ) {
-			int err;
-			err = WSAGetLastError();
-			errno = err;
-		}
-#endif
-		return rc;
-	}
-
 #elif defined(HAVE_NCSA)
 	return netwrite( sbiod->sbiod_sb->sb_fd, buf, len );
 
@@ -820,11 +794,11 @@
 
 	ret = LBER_SBIOD_READ_NEXT( sbiod, buf, len );
 	if (sbiod->sbiod_sb->sb_debug & LDAP_DEBUG_PACKETS) {
-		int err = errno;
+		int err = sock_errno();
 		if ( ret < 0 ) {
 			ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
 				"%sread: want=%ld error=%s\n", (char *)sbiod->sbiod_pvt,
-				(long)len, AC_STRERROR_R( errno, ebuf, sizeof ebuf ) );
+				(long)len, AC_STRERROR_R( err, ebuf, sizeof ebuf ) );
 		} else {
 			ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
 				"%sread: want=%ld, got=%ld\n", (char *)sbiod->sbiod_pvt,
@@ -832,7 +806,7 @@
 			ber_log_bprint( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
 				(const char *)buf, ret );
 		}
-		errno = err;
+		sock_errset(err);
 	}
 	return ret;
 }
@@ -845,12 +819,12 @@
 
 	ret = LBER_SBIOD_WRITE_NEXT( sbiod, buf, len );
 	if (sbiod->sbiod_sb->sb_debug & LDAP_DEBUG_PACKETS) {
-		int err = errno;
+		int err = sock_errno();
 		if ( ret < 0 ) {
 			ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
 				"%swrite: want=%ld error=%s\n",
 				(char *)sbiod->sbiod_pvt, (long)len,
-				AC_STRERROR_R( errno, ebuf, sizeof ebuf ) );
+				AC_STRERROR_R( err, ebuf, sizeof ebuf ) );
 		} else {
 			ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
 				"%swrite: want=%ld, written=%ld\n",
@@ -858,7 +832,7 @@
 			ber_log_bprint( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
 				(const char *)buf, ret );
 		}
-		errno = err;
+		sock_errset(err);
 	}
 
 	return ret;

Modified: openldap/vendor/openldap-release/libraries/libldap/cyrus.c
===================================================================
--- openldap/vendor/openldap-release/libraries/libldap/cyrus.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/libraries/libldap/cyrus.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,4 +1,4 @@
-/* $OpenLDAP: pkg/ldap/libraries/libldap/cyrus.c,v 1.112.2.14 2006/05/15 15:26:46 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/libraries/libldap/cyrus.c,v 1.112.2.15 2006/11/07 04:06:21 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -161,7 +161,7 @@
 	ber_pvt_sb_buf_init( &p->buf_out );
 	if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
 		LBER_FREE( p );
-		errno = ENOMEM;
+		sock_errset(ENOMEM);
 		return -1;
 	}
 	sasl_getprop( p->sasl_context, SASL_MAXOUTBUF,
@@ -288,7 +288,7 @@
 	if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) && 
 		ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
 	{
-		errno = ENOMEM;
+		sock_errset(ENOMEM);
 		return -1;
 	}
 	p->sec_buf_in.buf_end = ret;
@@ -327,7 +327,7 @@
 		ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
 			"sb_sasl_read: failed to decode packet: %s\n",
 			sasl_errstring( ret, NULL, NULL ) );
-		errno = EIO;
+		sock_errset(EIO);
 		return -1;
 	}
 	
@@ -356,7 +356,7 @@
 
 		/* Still have something left?? */
 		if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
-			errno = EAGAIN;
+			sock_errset(EAGAIN);
 			return -1;
 		}
 	}
@@ -383,7 +383,7 @@
 		ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
 			"sb_sasl_write: failed to encode packet: %s\n",
 			sasl_errstring( ret, NULL, NULL ) );
-		errno = EIO;
+		sock_errset(EIO);
 		return -1;
 	}
 	p->buf_out.buf_end = p->buf_out.buf_size;

Modified: openldap/vendor/openldap-release/libraries/libldap/getdn.c
===================================================================
--- openldap/vendor/openldap-release/libraries/libldap/getdn.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/libraries/libldap/getdn.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,4 +1,4 @@
-/* $OpenLDAP: pkg/ldap/libraries/libldap/getdn.c,v 1.124.2.4 2006/01/16 19:06:12 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/libraries/libldap/getdn.c,v 1.124.2.5 2006/11/08 22:57:33 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -2025,7 +2025,7 @@
 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
 {
 	ber_len_t	l, cl = 1;
-	char		*p;
+	char		*p, *end;
 	int		escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
 #ifdef PRETTY_ESCAPE
 	int		escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
@@ -2039,7 +2039,8 @@
 		return( 0 );
 	}
 
-	for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
+	end = val->bv_val + val->bv_len - 1;
+	for ( l = 0, p = val->bv_val; p <= end; p += cl ) {
 
 		/* 
 		 * escape '%x00' 
@@ -2068,7 +2069,7 @@
 		} else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
 				|| LDAP_DN_SHOULDESCAPE( p[ 0 ] )
 				|| ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
-				|| ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
+				|| ( p == end && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
 #ifdef PRETTY_ESCAPE
 #if 0
 			if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {

Modified: openldap/vendor/openldap-release/libraries/libldap/os-ip.c
===================================================================
--- openldap/vendor/openldap-release/libraries/libldap/os-ip.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/libraries/libldap/os-ip.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,5 @@
 /* os-ip.c -- platform-specific TCP & UDP related code */
-/* $OpenLDAP: pkg/ldap/libraries/libldap/os-ip.c,v 1.108.2.11 2006/10/21 04:56:53 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/libraries/libldap/os-ip.c,v 1.108.2.12 2006/11/07 04:06:21 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -63,7 +63,7 @@
 static void
 ldap_pvt_set_errno(int err)
 {
-	errno = err;
+	sock_errset(err);
 }
 
 int
@@ -219,7 +219,7 @@
 	struct sockaddr *sin, socklen_t addrlen,
 	int async)
 {
-	int rc;
+	int rc, err;
 	struct timeval	tv = { 0 },
 			*opt_tv = NULL;
 
@@ -252,11 +252,8 @@
 		return ( 0 );
 	}
 
-#ifdef HAVE_WINSOCK
-	ldap_pvt_set_errno( WSAGetLastError() );
-#endif
-
-	if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
+	err = sock_errno();
+	if ( err != EINPROGRESS && err != EWOULDBLOCK ) {
 		return ( -1 );
 	}
 	

Modified: openldap/vendor/openldap-release/libraries/libldap/request.c
===================================================================
--- openldap/vendor/openldap-release/libraries/libldap/request.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/libraries/libldap/request.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,4 +1,4 @@
-/* $OpenLDAP: pkg/ldap/libraries/libldap/request.c,v 1.103.2.13 2006/05/15 15:26:46 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/libraries/libldap/request.c,v 1.103.2.14 2006/11/07 04:06:21 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -149,7 +149,7 @@
 	LDAPConn *lc = lr->lr_conn;
 
 	if ( ber_flush( lc->lconn_sb, lr->lr_ber, 0 ) != 0 ) {
-		if ( errno == EAGAIN ) {
+		if ( sock_errno() == EAGAIN ) {
 			/* need to continue write later */
 			lr->lr_status = LDAP_REQST_WRITING;
 			ldap_mark_select_write( ld, lc->lconn_sb );

Modified: openldap/vendor/openldap-release/libraries/libldap/result.c
===================================================================
--- openldap/vendor/openldap-release/libraries/libldap/result.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/libraries/libldap/result.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,5 @@
 /* result.c - wait for an ldap result */
-/* $OpenLDAP: pkg/ldap/libraries/libldap/result.c,v 1.99.2.17 2006/08/18 15:14:38 ando Exp $ */
+/* $OpenLDAP: pkg/ldap/libraries/libldap/result.c,v 1.99.2.18 2006/11/07 04:06:22 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -301,13 +301,13 @@
 				if ( rc == -1 ) {
 					Debug( LDAP_DEBUG_TRACE,
 						"ldap_int_select returned -1: errno %d\n",
-						errno, 0, 0 );
+						sock_errno(), 0, 0 );
 				}
 #endif
 
 				if ( rc == 0 || ( rc == -1 && (
 					!LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
-						|| errno != EINTR )))
+						|| sock_errno() != EINTR )))
 				{
 					ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
 						LDAP_TIMEOUT);
@@ -445,7 +445,7 @@
 	assert( LBER_VALID (ber) );
 
 	/* get the next message */
-	errno = 0;
+	sock_errset(0);
 #ifdef LDAP_CONNECTIONLESS
 	if ( LDAP_IS_UDP(ld) ) {
 		struct sockaddr from;
@@ -469,10 +469,10 @@
 				"ber_get_next failed.\n", 0, 0, 0 );
 #endif		   
 #ifdef EWOULDBLOCK			
-			if (errno==EWOULDBLOCK) return LDAP_MSG_X_KEEP_LOOKING;
+			if ( sock_errno() == EWOULDBLOCK ) return LDAP_MSG_X_KEEP_LOOKING;
 #endif
 #ifdef EAGAIN
-			if (errno == EAGAIN) return LDAP_MSG_X_KEEP_LOOKING;
+			if ( sock_errno() == EAGAIN ) return LDAP_MSG_X_KEEP_LOOKING;
 #endif
 			ld->ld_errno = LDAP_SERVER_DOWN;
 			return -1;

Modified: openldap/vendor/openldap-release/libraries/libldap/tls.c
===================================================================
--- openldap/vendor/openldap-release/libraries/libldap/tls.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/libraries/libldap/tls.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,5 @@
 /* tls.c - Handle tls/ssl using SSLeay or OpenSSL. */
-/* $OpenLDAP: pkg/ldap/libraries/libldap/tls.c,v 1.118.2.13 2006/07/28 13:01:35 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/libraries/libldap/tls.c,v 1.118.2.14 2006/11/07 04:06:22 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -590,7 +590,7 @@
 	err = SSL_get_error( p->ssl, ret );
 	if (err == SSL_ERROR_WANT_READ ) {
 		sbiod->sbiod_sb->sb_trans_needs_read = 1;
-		errno = EWOULDBLOCK;
+		sock_errset(EWOULDBLOCK);
 	}
 	else
 		sbiod->sbiod_sb->sb_trans_needs_read = 0;
@@ -616,7 +616,7 @@
 	err = SSL_get_error( p->ssl, ret );
 	if (err == SSL_ERROR_WANT_WRITE ) {
 		sbiod->sbiod_sb->sb_trans_needs_write = 1;
-		errno = EWOULDBLOCK;
+		sock_errset(EWOULDBLOCK);
 
 	} else {
 		sbiod->sbiod_sb->sb_trans_needs_write = 0;
@@ -672,7 +672,7 @@
 
 	BIO_clear_retry_flags( b );
 	if ( ret < 0 ) {
-		int err = errno;
+		int err = sock_errno();
 		if ( err == EAGAIN || err == EWOULDBLOCK ) {
 			BIO_set_retry_read( b );
 		}
@@ -699,7 +699,7 @@
 
 	BIO_clear_retry_flags( b );
 	if ( ret < 0 ) {
-		int err = errno;
+		int err = sock_errno();
 		if ( err == EAGAIN || err == EWOULDBLOCK ) {
 			BIO_set_retry_write( b );
 		}

Modified: openldap/vendor/openldap-release/libraries/liblunicode/ucdata/ucdata.c
===================================================================
--- openldap/vendor/openldap-release/libraries/liblunicode/ucdata/ucdata.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/libraries/liblunicode/ucdata/ucdata.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,4 +1,4 @@
-/* $OpenLDAP: pkg/ldap/libraries/liblunicode/ucdata/ucdata.c,v 1.30.2.2 2006/01/03 22:16:10 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/libraries/liblunicode/ucdata/ucdata.c,v 1.30.2.3 2006/10/30 17:45:45 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -313,6 +313,9 @@
 
 #if !HARDCODE_DATA
 
+/* These record the number of slots in the map.
+ * There are 3 words per slot.
+ */
 static ac_uint4 _uccase_size;
 static ac_uint2 _uccase_len[2];
 static ac_uint4 *_uccase_map;
@@ -356,23 +359,23 @@
      * Set the node count and lengths of the upper and lower case mapping
      * tables.
      */
-    _uccase_size = hdr.cnt * 3;
-    _uccase_len[0] = hdr.size.len[0] * 3;
-    _uccase_len[1] = hdr.size.len[1] * 3;
+    _uccase_size = hdr.cnt;
+    _uccase_len[0] = hdr.size.len[0];
+    _uccase_len[1] = hdr.size.len[1];
 
     _uccase_map = (ac_uint4 *)
-        malloc(_uccase_size * sizeof(ac_uint4));
+        malloc(_uccase_size * 3 * sizeof(ac_uint4));
 
     /*
      * Load the case mapping table.
      */
-    fread((char *) _uccase_map, sizeof(ac_uint4), _uccase_size, in);
+    fread((char *) _uccase_map, sizeof(ac_uint4), _uccase_size * 3, in);
 
     /*
      * Do an endian swap if necessary.
      */
     if (hdr.bom == 0xfffe) {
-        for (i = 0; i < _uccase_size; i++)
+        for (i = 0; i < _uccase_size * 3; i++)
           _uccase_map[i] = endian_long(_uccase_map[i]);
     }
     fclose(in);
@@ -394,6 +397,7 @@
 _uccase_lookup(ac_uint4 code, long l, long r, int field)
 {
     long m;
+	ac_uint4 *tmp;
 
     /*
      * Do the binary search.
@@ -404,13 +408,13 @@
          * the beginning of a case mapping triple.
          */
         m = (l + r) >> 1;
-        m -= (m % 3);
-        if (code > _uccase_map[m])
-          l = m + 3;
-        else if (code < _uccase_map[m])
-          r = m - 3;
-        else if (code == _uccase_map[m])
-          return _uccase_map[m + field];
+		tmp = &_uccase_map[m*3];
+        if (code > *tmp)
+          l = m + 1;
+        else if (code < *tmp)
+          r = m - 1;
+        else if (code == *tmp)
+          return tmp[field];
     }
 
     return code;
@@ -431,14 +435,14 @@
          */
         field = 2;
         l = _uccase_len[0];
-        r = (l + _uccase_len[1]) - 3;
+        r = (l + _uccase_len[1]) - 1;
     } else {
         /*
          * The character is title case.
          */
         field = 1;
         l = _uccase_len[0] + _uccase_len[1];
-        r = _uccase_size - 3;
+        r = _uccase_size - 1;
     }
     return _uccase_lookup(code, l, r, field);
 }
@@ -458,14 +462,14 @@
          */
         field = 1;
         l = 0;
-        r = _uccase_len[0] - 3;
+        r = _uccase_len[0] - 1;
     } else {
         /*
          * The character is title case.
          */
         field = 2;
         l = _uccase_len[0] + _uccase_len[1];
-        r = _uccase_size - 3;
+        r = _uccase_size - 1;
     }
     return _uccase_lookup(code, l, r, field);
 }
@@ -489,13 +493,13 @@
          * The character is upper case.
          */
         l = 0;
-        r = _uccase_len[0] - 3;
+        r = _uccase_len[0] - 1;
     } else {
         /*
          * The character is lower case.
          */
         l = _uccase_len[0];
-        r = (l + _uccase_len[1]) - 3;
+        r = (l + _uccase_len[1]) - 1;
     }
     return _uccase_lookup(code, l, r, field);
 }

Modified: openldap/vendor/openldap-release/servers/slapd/connection.c
===================================================================
--- openldap/vendor/openldap-release/servers/slapd/connection.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/servers/slapd/connection.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,4 +1,4 @@
-/* $OpenLDAP: pkg/ldap/servers/slapd/connection.c,v 1.296.2.16 2006/04/05 18:08:48 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/servers/slapd/connection.c,v 1.296.2.19 2006/11/09 05:47:20 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -43,33 +43,10 @@
 #include "slapi/slapi.h"
 #endif
 
-#ifdef SLAP_MULTI_CONN_ARRAY
-/* for Multiple Connection Arrary (MCA) Support */
-static ldap_pvt_thread_mutex_t* connections_mutex;
-static Connection **connections = NULL;
-
-/* set to the number of processors (round up to a power of 2) */
-#	define NUM_CONNECTION_ARRAY 4
-
-/* partition the array in a modulo manner */
-#	define MCA_conn_array_id(fd)		((int)(fd)%NUM_CONNECTION_ARRAY)
-#	define MCA_conn_array_element_id(fd)	((int)(fd)/NUM_CONNECTION_ARRAY)
-#	define MCA_ARRAY_SIZE			((int)(MCA_conn_array_element_id(dtblsize) + (MCA_conn_array_id(dtblsize) ? 1 : 0)))
-#	define MCA_conn_check(fd)		(dtblsize > 0 && (fd) >= 0 && (fd) < (MCA_ARRAY_SIZE*NUM_CONNECTION_ARRAY))
-#	define MCA_GET_CONNECTION(fd) (&(connections[MCA_conn_array_id(fd)]) \
-		[MCA_conn_array_element_id(fd)])
-#	define MCA_GET_CONN_MUTEX(fd) (&connections_mutex[MCA_conn_array_id(fd)])
-
-#else
 /* protected by connections_mutex */
 static ldap_pvt_thread_mutex_t connections_mutex;
 static Connection *connections = NULL;
 
-#	define MCA_conn_check(fd)		(dtblsize > 0 && (fd) < dtblsize)
-#	define MCA_GET_CONNECTION(fd) (&connections[s])
-#	define MCA_GET_CONN_MUTEX(fd) (&connections_mutex)
-#endif
-
 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
 static unsigned long conn_nextid = 0;
 
@@ -79,6 +56,7 @@
 #define SLAP_C_UNINITIALIZED	0x00	/* MUST BE ZERO (0) */
 #define SLAP_C_UNUSED			0x01
 #define SLAP_C_USED				0x02
+#define	SLAP_C_PENDING			0x03
 
 /* connection state (protected by c_mutex ) */
 #define SLAP_C_INVALID			0x00	/* MUST BE ZERO (0) */
@@ -134,70 +112,7 @@
  * Initialize connection management infrastructure.
  */
 int connections_init(void)
-#ifdef SLAP_MULTI_CONN_ARRAY
 {
-	int		i, j;
-	Connection*	conn;
-
-	assert( connections == NULL );
-
-	if( connections != NULL) {
-		Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
-			0, 0, 0 );
-		return -1;
-	}
-
-	connections_mutex = (ldap_pvt_thread_mutex_t*) ch_calloc(
-		NUM_CONNECTION_ARRAY, sizeof(ldap_pvt_thread_mutex_t) );
-	if( connections_mutex == NULL ) {
-		Debug( LDAP_DEBUG_ANY, "connections_init: "
-			"allocation of connection mutexes failed\n", 0, 0, 0 );
-		return -1;
-	}
-
-	connections = (Connection**) ch_calloc(
-		NUM_CONNECTION_ARRAY, sizeof(Connection*));
-	if( connections == NULL ) {
-		Debug( LDAP_DEBUG_ANY, "connections_init: "
-			"allocation of connection[%d] failed\n", 0, 0, 0 );
-		return -1;
-	}
-
-	for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
-		ldap_pvt_thread_mutex_init( connections_mutex+i );
-		connections[i] = (Connection*) ch_calloc(
-			MCA_ARRAY_SIZE, sizeof(Connection) );
-		if( connections[i] == NULL ) {
-			Debug( LDAP_DEBUG_ANY, "connections_init: "
-				"allocation (%d*%ld) of connection array[%d] failed\n",
-				dtblsize, (long) sizeof(Connection), i );
-			return -1;
-		}
-	}
-
-	/* should check return of every call */
-	ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
-
-	assert( connections[0]->c_struct_state == SLAP_C_UNINITIALIZED );
-	assert( connections[NUM_CONNECTION_ARRAY-1]->c_struct_state ==
-		SLAP_C_UNINITIALIZED );
-
-	for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
-		conn = connections[i];
-		for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
-			conn[j].c_conn_idx = j;
-		}
-	}
-
-	/*
-	 * per entry initialization of the Connection array initialization
-	 * will be done by connection_init()
-	 */ 
-
-	return 0;
-}
-#else
-{
 	int i;
 
 	assert( connections == NULL );
@@ -233,59 +148,13 @@
 
 	return 0;
 }
-#endif
 
 /*
  * Destroy connection management infrastructure.
  */
 
 int connections_destroy(void)
-#ifdef SLAP_MULTI_CONN_ARRAY
 {
-	int i;
-	ber_socket_t j;
-
-	if( connections == NULL) {
-		Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
-			0, 0, 0 );
-		return -1;
-	}
-
-    for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
-		Connection* conn = connections[i];
-		for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
-			if( conn[j].c_struct_state != SLAP_C_UNINITIALIZED ) {
-				ber_sockbuf_free( conn[j].c_sb );
-				ldap_pvt_thread_mutex_destroy( &conn[j].c_mutex );
-				ldap_pvt_thread_mutex_destroy( &conn[j].c_write_mutex );
-				ldap_pvt_thread_cond_destroy( &conn[j].c_write_cv );
-#ifdef LDAP_SLAPI
-				/* FIX ME!! */
-				if ( slapi_plugins_used ) {
-					slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
-						&connections[i] );
-				}
-#endif
-			}
-		}
-	}
-
-	for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
-		free( connections[i] );
-		connections[i] = NULL;
-		ldap_pvt_thread_mutex_destroy( &connections_mutex[i] );
-	}
-
-	free( connections );
-	free( connections_mutex );
-
-	ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
-
-	return 0;
-
-}
-#else
-{
 	ber_socket_t i;
 
 	/* should check return of every call */
@@ -318,50 +187,14 @@
 	ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
 	return 0;
 }
-#endif
 
 /*
  * shutdown all connections
  */
 int connections_shutdown(void)
-#ifdef SLAP_MULTI_CONN_ARRAY
 {
-	int i;
-	ber_socket_t j;
-
-	for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) {
-		Connection* conn = connections[i];
-		ldap_pvt_thread_mutex_lock( &connections_mutex[i] );
-		for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) {
-			if( conn[j].c_struct_state != SLAP_C_USED ) {
-				continue;
-			}
-			/* give persistent clients a chance to cleanup */
-			if( conn[j].c_conn_state == SLAP_C_CLIENT ) {
-				ldap_pvt_thread_pool_submit( &connection_pool,
-				conn[j].c_clientfunc, conn[j].c_clientarg );
-				continue;
-			}
-
-			ldap_pvt_thread_mutex_lock( &conn[j].c_mutex );
-			/* connections_mutex and c_mutex are locked */
-			connection_closing( &conn[j], "connection shutdown" );
-			connection_close( &conn[j] );
-			ldap_pvt_thread_mutex_unlock( &conn[j].c_mutex );
-		}
-
-		ldap_pvt_thread_mutex_unlock( &connections_mutex[i] );
-	}
-
-	return 0;
-
-}
-#else
-{
 	ber_socket_t i;
 
-	ldap_pvt_thread_mutex_lock( &connections_mutex );
-
 	for ( i = 0; i < dtblsize; i++ ) {
 		if( connections[i].c_struct_state != SLAP_C_USED ) {
 			continue;
@@ -375,18 +208,15 @@
 
 		ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
 
-		/* connections_mutex and c_mutex are locked */
+		/* c_mutex is locked */
 		connection_closing( &connections[i], "slapd shutdown" );
 		connection_close( &connections[i] );
 
 		ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
 	}
 
-	ldap_pvt_thread_mutex_unlock( &connections_mutex );
-
 	return 0;
 }
-#endif
 
 /*
  * Timeout idle connections.
@@ -421,8 +251,6 @@
 
 static Connection* connection_get( ber_socket_t s )
 {
-	/* connections_mutex should be locked by caller */
-
 	Connection *c;
 
 	Debug( LDAP_DEBUG_ARGS,
@@ -434,17 +262,19 @@
 	if(s == AC_SOCKET_INVALID) return NULL;
 
 #ifndef HAVE_WINSOCK
-	assert( MCA_conn_check( s ) );
-	c = MCA_GET_CONNECTION(s);
+	assert( s < dtblsize );
+	c = &connections[s];
 
-	assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
-
 #else
 	c = NULL;
 	{
 		ber_socket_t i, sd;
 
+		ldap_pvt_thread_mutex_lock( &connections_mutex );
 		for(i=0; i<dtblsize; i++) {
+			if( connections[i].c_struct_state == SLAP_C_PENDING )
+				continue;
+
 			if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
 				assert( connections[i].c_conn_state == SLAP_C_INVALID );
 				assert( connections[i].c_sb == 0 );
@@ -469,12 +299,15 @@
 				break;
 			}
 		}
+		ldap_pvt_thread_mutex_unlock( &connections_mutex );
 	}
 #endif
 
 	if( c != NULL ) {
 		ber_socket_t	sd;
 
+		assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
+
 		ldap_pvt_thread_mutex_lock( &c->c_mutex );
 
 		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
@@ -529,6 +362,7 @@
 {
 	unsigned long id;
 	Connection *c;
+	int doinit = 0;
 
 	assert( connections != NULL );
 
@@ -549,24 +383,29 @@
 	assert( s >= 0 );
 #ifndef HAVE_WINSOCK
 	assert( s < dtblsize );
-#endif
-
-	ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
-
-#ifndef HAVE_WINSOCK
-	assert( MCA_conn_check( s ) );
-	c = MCA_GET_CONNECTION(s);
+	c = &connections[s];
+	if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
+		doinit = 1;
+	} else {
+		assert( c->c_struct_state == SLAP_C_UNUSED );
+	}
 #else
 	{
 		ber_socket_t i;
 		c = NULL;
 
+		ldap_pvt_thread_mutex_lock( &connections_mutex );
 		for( i=0; i < dtblsize; i++) {
 			ber_socket_t	sd;
 
+			if ( connections[i].c_struct_state == SLAP_C_PENDING )
+				continue;
+
 			if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
 				assert( connections[i].c_sb == 0 );
 				c = &connections[i];
+				c->c_struct_state = SLAP_C_PENDING;
+				doinit = 1;
 				break;
 			}
 
@@ -579,6 +418,7 @@
 			if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
 				assert( sd == AC_SOCKET_INVALID );
 				c = &connections[i];
+				c->c_struct_state = SLAP_C_PENDING;
 				break;
 			}
 
@@ -588,20 +428,18 @@
 			assert( connections[i].c_conn_state != SLAP_C_INVALID );
 			assert( sd != AC_SOCKET_INVALID );
 		}
+		ldap_pvt_thread_mutex_unlock( &connections_mutex );
 
 		if( c == NULL ) {
 			Debug( LDAP_DEBUG_ANY,
 				"connection_init(%d): connection table full "
 				"(%d/%d)\n", s, i, dtblsize);
-			ldap_pvt_thread_mutex_unlock( &connections_mutex );
 			return -1;
 		}
 	}
 #endif
 
-	assert( c != NULL );
-
-	if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
+	if( doinit ) {
 		c->c_send_ldap_result = slap_send_ldap_result;
 		c->c_send_search_entry = slap_send_search_entry;
 		c->c_send_search_reference = slap_send_search_reference;
@@ -619,6 +457,12 @@
 		LDAP_STAILQ_INIT(&c->c_ops);
 		LDAP_STAILQ_INIT(&c->c_pending_ops);
 
+#ifdef LDAP_X_TXN
+		c->c_txn = CONN_TXN_INACTIVE;
+		c->c_txn_backend = NULL;
+		LDAP_STAILQ_INIT(&c->c_txn_ops);
+#endif
+
 		BER_BVZERO( &c->c_sasl_bind_mech );
 		c->c_sasl_done = 0;
 		c->c_sasl_authctx = NULL;
@@ -645,13 +489,10 @@
 			slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
 		}
 #endif
-
-		c->c_struct_state = SLAP_C_UNUSED;
 	}
 
 	ldap_pvt_thread_mutex_lock( &c->c_mutex );
 
-	assert( c->c_struct_state == SLAP_C_UNUSED );
 	assert( BER_BVISNULL( &c->c_authmech ) );
 	assert( BER_BVISNULL( &c->c_dn ) );
 	assert( BER_BVISNULL( &c->c_ndn ) );
@@ -660,6 +501,11 @@
 	assert( BER_BVISNULL( &c->c_peer_name ) );
 	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
 	assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
+#ifdef LDAP_X_TXN
+	assert( c->c_txn == CONN_TXN_INACTIVE );
+	assert( c->c_txn_backend == NULL );
+	assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
+#endif
 	assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
 	assert( c->c_sasl_done == 0 );
 	assert( c->c_sasl_authctx == NULL );
@@ -677,7 +523,6 @@
 		c->c_close_reason = "?";			/* should never be needed */
 		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
 		ldap_pvt_thread_mutex_unlock( &c->c_mutex );
-		ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
 
 		return 0;
 	}
@@ -766,7 +611,6 @@
 
 	slapd_add_internal( s, 1 );
 	ldap_pvt_thread_mutex_unlock( &c->c_mutex );
-	ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
 
 	backend_connection_init(c);
 
@@ -809,22 +653,32 @@
 static void
 connection_destroy( Connection *c )
 {
-	/* note: connections_mutex should be locked by caller */
 	ber_socket_t	sd;
 	unsigned long	connid;
 	const char		*close_reason;
+	Sockbuf			*sb;
 
 	assert( connections != NULL );
 	assert( c != NULL );
 	assert( c->c_struct_state != SLAP_C_UNUSED );
 	assert( c->c_conn_state != SLAP_C_INVALID );
 	assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
+	assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
+#ifdef LDAP_X_TXN
+	assert( c->c_txn == CONN_TXN_INACTIVE );
+	assert( c->c_txn_backend == NULL );
+	assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
+#endif
 	assert( c->c_writewaiter == 0);
 
 	/* only for stats (print -1 as "%lu" may give unexpected results ;) */
 	connid = c->c_connid;
 	close_reason = c->c_close_reason;
 
+	ldap_pvt_thread_mutex_lock( &connections_mutex );
+	c->c_struct_state = SLAP_C_PENDING;
+	ldap_pvt_thread_mutex_unlock( &connections_mutex );
+
 	backend_connection_destroy(c);
 
 	c->c_protocol = 0;
@@ -857,37 +711,38 @@
 		c->c_currentber = NULL;
 	}
 
-	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
-	slapd_sd_lock();
-	ber_sockbuf_free( c->c_sb );
-	if ( sd != AC_SOCKET_INVALID ) {
-		slapd_remove( sd, 1, 0, 1 );
 
-		Statslog( LDAP_DEBUG_STATS, (close_reason
-									 ? "conn=%lu fd=%ld closed (%s)\n"
-									 : "conn=%lu fd=%ld closed\n"),
-			connid, (long) sd, close_reason, 0, 0 );
-	} else {
-		slapd_sd_unlock();
+#ifdef LDAP_SLAPI
+	/* call destructors, then constructors; avoids unnecessary allocation */
+	if ( slapi_plugins_used ) {
+		slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
 	}
+#endif
 
-	c->c_sb = ber_sockbuf_alloc( );
+	c->c_conn_state = SLAP_C_INVALID;
+	c->c_struct_state = SLAP_C_UNUSED;
+	c->c_close_reason = "?";			/* should never be needed */
 
+	sb = c->c_sb;
+	c->c_sb = ber_sockbuf_alloc( );
 	{
 		ber_len_t max = sockbuf_max_incoming;
 		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
 	}
 
-	c->c_conn_state = SLAP_C_INVALID;
-	c->c_struct_state = SLAP_C_UNUSED;
-	c->c_close_reason = "?";			/* should never be needed */
+	ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
 
-#ifdef LDAP_SLAPI
-	/* call destructors, then constructors; avoids unnecessary allocation */
-	if ( slapi_plugins_used ) {
-		slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
+	/* c must be fully reset by this point; when we call slapd_remove
+	 * it may get immediately reused by a new connection.
+	 */
+	if ( sd != AC_SOCKET_INVALID ) {
+		slapd_remove( sd, sb, 1, 0, 0 );
+
+		Statslog( LDAP_DEBUG_STATS, (close_reason
+									 ? "conn=%lu fd=%ld closed (%s)\n"
+									 : "conn=%lu fd=%ld closed\n"),
+			connid, (long) sd, close_reason, 0, 0 );
 	}
-#endif
 }
 
 int connection_state_closing( Connection *c )
@@ -917,6 +772,7 @@
 	op.o_conn = c;
 	op.o_connid = c->c_connid;
 	op.o_tag = LDAP_REQ_ABANDON;
+
 	for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
 		next = LDAP_STAILQ_NEXT( o, o_next );
 		op.orn_msgid = o->o_msgid;
@@ -925,6 +781,19 @@
 		frontendDB->be_abandon( &op, &rs );
 	}
 
+#ifdef LDAP_X_TXN
+	/* remove operations in pending transaction */
+	while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
+		LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
+		LDAP_STAILQ_NEXT(o, o_next) = NULL;
+		slap_op_free( o );
+	}
+
+	/* clear transaction */
+	c->c_txn_backend = NULL;
+	c->c_txn = CONN_TXN_INACTIVE;
+#endif
+
 	/* remove pending operations */
 	while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
 		LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
@@ -954,38 +823,60 @@
 		c->c_close_reason = why;
 
 		/* don't listen on this port anymore */
-		slapd_clr_read( sd, 1 );
+		slapd_clr_read( sd, 0 );
 
 		/* abandon active operations */
 		connection_abandon( c );
 
 		/* wake write blocked operations */
-		slapd_clr_write( sd, 1 );
 		if ( c->c_writewaiter ) {
 			ldap_pvt_thread_cond_signal( &c->c_write_cv );
+			/* ITS#4667 this may allow another thread to drop into
+			 * connection_resched / connection_close before we
+			 * finish, but that's OK.
+			 */
+			slapd_clr_write( sd, 1 );
 			ldap_pvt_thread_mutex_unlock( &c->c_mutex );
-			ldap_pvt_thread_yield();
+			ldap_pvt_thread_mutex_lock( &c->c_write_mutex );
 			ldap_pvt_thread_mutex_lock( &c->c_mutex );
+			ldap_pvt_thread_mutex_unlock( &c->c_write_mutex );
+		} else {
+			slapd_clr_write( sd, 1 );
 		}
+
 	} else if( why == NULL && c->c_close_reason == conn_lost_str ) {
 		/* Client closed connection after doing Unbind. */
 		c->c_close_reason = NULL;
 	}
 }
 
-static void connection_close( Connection *c )
+static void
+connection_close( Connection *c )
 {
-	ber_socket_t	sd;
+	ber_socket_t	sd = AC_SOCKET_INVALID;
 
 	assert( connections != NULL );
 	assert( c != NULL );
+
+	/* ITS#4667 we may have gotten here twice */
+	if ( c->c_conn_state == SLAP_C_INVALID )
+		return;
+
 	assert( c->c_struct_state == SLAP_C_USED );
 	assert( c->c_conn_state == SLAP_C_CLOSING );
 
-	/* note: connections_mutex and c_mutex should be locked by caller */
+	/* NOTE: c_mutex should be locked by caller */
 
-	ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
-	if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
+	/* NOTE: don't get the file descriptor if not needed */
+#ifdef LDAP_DEBUG
+	if ( slap_debug & LDAP_DEBUG_TRACE ) {
+		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
+	}
+#endif /* LDAP_DEBUG */
+
+	if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
+		!LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
+	{
 		Debug( LDAP_DEBUG_TRACE,
 			"connection_close: deferring conn=%lu sd=%d\n",
 			c->c_connid, sd, 0 );
@@ -994,6 +885,7 @@
 
 	Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
 		c->c_connid, sd, 0 );
+
 	connection_destroy( c );
 }
 
@@ -1013,125 +905,75 @@
 
 Connection* connection_first( ber_socket_t *index )
 {
-#ifdef SLAP_MULTI_CONN_ARRAY
-	int conn_array_id;
-#endif
-
 	assert( connections != NULL );
 	assert( index != NULL );
 
-#ifdef SLAP_MULTI_CONN_ARRAY
-	for ( conn_array_id = 0;
-		conn_array_id < NUM_CONNECTION_ARRAY;
-		conn_array_id++ )
-	{
-		ldap_pvt_thread_mutex_lock( &connections_mutex[ conn_array_id ] );
-	}
-#else
 	ldap_pvt_thread_mutex_lock( &connections_mutex );
-#endif
+	for( *index = 0; *index < dtblsize; (*index)++) {
+		if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
+			break;
+		}
+	}
+	ldap_pvt_thread_mutex_unlock( &connections_mutex );
 
-	*index = 0;
-
 	return connection_next(NULL, index);
 }
 
 Connection* connection_next( Connection *c, ber_socket_t *index )
-#ifdef SLAP_MULTI_CONN_ARRAY
 {
-	Connection* conn;
-
 	assert( connections != NULL );
 	assert( index != NULL );
-	assert( *index >= 0 && *index <= dtblsize );
-
-	if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
-
-	c = NULL;
-
-	for(; *index < dtblsize; (*index)++) {
-		assert( MCA_conn_check( *index ) );
-		conn = MCA_GET_CONNECTION(*index);
-		if( conn->c_struct_state == SLAP_C_UNINITIALIZED ) {
-			assert( conn->c_conn_state == SLAP_C_INVALID );
-#ifndef HAVE_WINSOCK
-			continue;
-#else
-			break;
-#endif
-		}
-
-		if( conn->c_struct_state == SLAP_C_USED ) {
-			assert( conn->c_conn_state != SLAP_C_INVALID );
-			c = conn;
-			(*index)++;
-			break;
-		}
-
-		assert( conn->c_struct_state == SLAP_C_UNUSED );
-		assert( conn->c_conn_state == SLAP_C_INVALID );
-	}
-
-	if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
-
-	return c;
-
-}
-#else
-{
-	assert( connections != NULL );
-	assert( index != NULL );
 	assert( *index <= dtblsize );
 
 	if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
 
 	c = NULL;
 
+	ldap_pvt_thread_mutex_lock( &connections_mutex );
 	for(; *index < dtblsize; (*index)++) {
+		int c_struct;
 		if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
 			assert( connections[*index].c_conn_state == SLAP_C_INVALID );
-#ifndef HAVE_WINSOCK
-			continue;
-#else
+#ifdef HAVE_WINSOCK
 			break;
+#else
+			continue;
 #endif
 		}
 
 		if( connections[*index].c_struct_state == SLAP_C_USED ) {
 			assert( connections[*index].c_conn_state != SLAP_C_INVALID );
 			c = &connections[(*index)++];
+			if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
+				/* avoid deadlock */
+				ldap_pvt_thread_mutex_unlock( &connections_mutex );
+				ldap_pvt_thread_mutex_lock( &c->c_mutex );
+				ldap_pvt_thread_mutex_lock( &connections_mutex );
+				if ( c->c_struct_state != SLAP_C_USED ) {
+					ldap_pvt_thread_mutex_unlock( &c->c_mutex );
+					c = NULL;
+					continue;
+				}
+			}
 			break;
 		}
 
-		assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
+		c_struct = connections[*index].c_struct_state;
+		if ( c_struct == SLAP_C_PENDING )
+			continue;
+		assert( c_struct == SLAP_C_UNUSED );
 		assert( connections[*index].c_conn_state == SLAP_C_INVALID );
 	}
 
-	if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
+	ldap_pvt_thread_mutex_unlock( &connections_mutex );
 	return c;
 }
-#endif
 
 void connection_done( Connection *c )
 {
-#ifdef SLAP_MULTI_CONN_ARRAY
-	int conn_array_id;
-#endif
-
 	assert( connections != NULL );
 
 	if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
-
-#ifdef SLAP_MULTI_CONN_ARRAY
-	for ( conn_array_id = 0;
-		conn_array_id < NUM_CONNECTION_ARRAY;
-		conn_array_id++ )
-	{
-		ldap_pvt_thread_mutex_unlock( &connections_mutex[ conn_array_id ] );
-	}
-#else
-	ldap_pvt_thread_mutex_unlock( &connections_mutex );
-#endif
 }
 
 /*
@@ -1168,7 +1010,7 @@
 /*
  * NOTE: keep in sync with enum in slapd.h
  */
-static int (*opfun[])( Operation *op, SlapReply *rs ) = {
+static BI_op_func *opfun[] = {
 	do_bind,
 	do_unbind,
 	do_add,
@@ -1189,7 +1031,7 @@
 	Operation *op = arg_v;
 	SlapReply rs = {REP_RESULT};
 	ber_tag_t tag = op->o_tag;
-	int opidx = -1;
+	slap_op_t opidx = SLAP_OP_LAST;
 	Connection *conn = op->o_conn;
 	void *memctx = NULL;
 	void *memctx_null = NULL;
@@ -1201,6 +1043,9 @@
 	ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
 
 	op->o_threadctx = ctx;
+#ifdef LDAP_DEVEL
+	op->o_tid = ldap_pvt_thread_pool_tid( ctx );
+#endif /* LDAP_DEVEL */
 
 	switch ( tag ) {
 	case LDAP_REQ_BIND:
@@ -1236,6 +1081,20 @@
 		goto operations_error;
 	}
 
+#ifdef LDAP_X_TXN
+	if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
+		( tag == LDAP_REQ_ADD ) ||
+		( tag == LDAP_REQ_DELETE ) ||
+		( tag == LDAP_REQ_MODIFY ) ||
+		( tag == LDAP_REQ_MODRDN )))
+	{
+		/* Disable SLAB allocator for all update operations
+			issued inside of a transaction */
+		op->o_tmpmemctx = NULL;
+		op->o_tmpmfuncs = &ch_mfuncs;
+	} else
+#endif
+	{
 	/* We can use Thread-Local storage for most mallocs. We can
 	 * also use TL for ber parsing, but not on Add or Modify.
 	 */
@@ -1255,54 +1114,10 @@
 		 */
 		ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
 	}
-
-	switch ( tag ) {
-	case LDAP_REQ_BIND:
-		opidx = SLAP_OP_BIND;
-		break;
-
-	case LDAP_REQ_UNBIND:
-		opidx = SLAP_OP_UNBIND;
-		break;
-
-	case LDAP_REQ_ADD:
-		opidx = SLAP_OP_ADD;
-		break;
-
-	case LDAP_REQ_DELETE:
-		opidx = SLAP_OP_DELETE;
-		break;
-
-	case LDAP_REQ_MODRDN:
-		opidx = SLAP_OP_MODRDN;
-		break;
-
-	case LDAP_REQ_MODIFY:
-		opidx = SLAP_OP_MODIFY;
-		break;
-
-	case LDAP_REQ_COMPARE:
-		opidx = SLAP_OP_COMPARE;
-		break;
-
-	case LDAP_REQ_SEARCH:
-		opidx = SLAP_OP_SEARCH;
-		break;
-
-	case LDAP_REQ_ABANDON:
-		opidx = SLAP_OP_ABANDON;
-		break;
-
-	case LDAP_REQ_EXTENDED:
-		opidx = SLAP_OP_EXTENDED;
-		break;
-
-	default:
-		/* not reachable */
-		assert( 0 );
 	}
 
-	assert( opidx > -1 );
+	opidx = slap_req2op( tag );
+	assert( opidx != SLAP_OP_LAST );
 	INCR_OP_INITIATED( opidx );
 	rc = (*(opfun[opidx]))( op, &rs );
 
@@ -1310,7 +1125,7 @@
 	if ( rc == SLAPD_DISCONNECT ) {
 		tag = LBER_ERROR;
 
-	} else if ( opidx > -1 ) {
+	} else if ( opidx != SLAP_OP_LAST ) {
 		/* increment completed operations count 
 		 * only if operation was initiated
 		 * and rc != SLAPD_DISCONNECT */
@@ -1324,6 +1139,7 @@
 			op->o_cancel = LDAP_TOO_LATE;
 		}
 	}
+
 	while ( op->o_cancel != SLAP_CANCEL_NONE &&
 		op->o_cancel != SLAP_CANCEL_DONE )
 	{
@@ -1388,6 +1204,7 @@
 	ber_socket_t s )
 {
 	Connection *c;
+	Sockbuf *sb;
 
 	/* get (locked) connection */
 	c = connection_get( s );
@@ -1398,14 +1215,13 @@
 	c->c_conn_state = SLAP_C_INVALID;
 	c->c_struct_state = SLAP_C_UNUSED;
 	c->c_close_reason = "?";			/* should never be needed */
-	slapd_sd_lock();
-	ber_sockbuf_free( c->c_sb );
-	slapd_remove( s, 0, 1, 1 );
+	sb = c->c_sb;
 	c->c_sb = ber_sockbuf_alloc( );
 	{
 		ber_len_t max = sockbuf_max_incoming;
 		ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
 	}
+	slapd_remove( s, sb, 0, 1, 0 );
 
 	connection_return( c );
 }
@@ -1477,8 +1293,6 @@
 
 	assert( connections != NULL );
 
-	ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
-
 	/* get (locked) connection */
 	c = connection_get( s );
 
@@ -1487,7 +1301,6 @@
 			"connection_read(%ld): no connection!\n",
 			(long) s, 0, 0 );
 
-		ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
 		return -1;
 	}
 
@@ -1497,12 +1310,7 @@
 		Debug( LDAP_DEBUG_TRACE,
 			"connection_read(%d): closing, ignoring input for id=%lu\n",
 			s, c->c_connid, 0 );
-
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
-		slapd_set_read( s, 1 );
-#endif
 		connection_return( c );
-		ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
 		return 0;
 	}
 
@@ -1517,7 +1325,6 @@
 			c->c_clientfunc, c->c_clientarg );
 #endif
 		connection_return( c );
-		ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
 		return 0;
 	}
 
@@ -1535,31 +1342,10 @@
 				s, rc, c->c_connid );
 
 			c->c_needs_tls_accept = 0;
-			/* connections_mutex and c_mutex are locked */
+			/* c_mutex is locked */
 			connection_closing( c, "TLS negotiation failure" );
-
-#if 0
-			{
-				struct timeval tv;
-				fd_set rfd;
-				/* Drain input before close, to allow SSL error codes
-				 * to propagate to client. */
-				FD_ZERO(&rfd);
-				FD_SET(s, &rfd);
-				for (rc=1; rc>0;) {
-					tv.tv_sec = 1;
-					tv.tv_usec = 0;
-					rc = select(s+1, &rfd, NULL, NULL, &tv);
-					if (rc == 1) {
-						ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
-					}
-				}
-			}
-#endif
-
 			connection_close( c );
 			connection_return( c );
-			ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
 			return 0;
 
 		} else if ( rc == 0 ) {
@@ -1590,15 +1376,13 @@
 		}
 
 		/* if success and data is ready, fall thru to data input loop */
-		if( rc != 0 ||
-			!ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
+		if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
 		{
 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
 			slapd_set_read( s, 1 );
 #endif
 
 			connection_return( c );
-			ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
 			return 0;
 		}
 	}
@@ -1613,7 +1397,6 @@
 #endif
 
 			connection_return( c );
-			ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
 			return 0;
 		}
 
@@ -1626,11 +1409,10 @@
 				"error=%d id=%lu, closing\n",
 				s, rc, c->c_connid );
 
-			/* connections_mutex and c_mutex are locked */
+			/* c_mutex is locked */
 			connection_closing( c, "SASL layer install failure" );
 			connection_close( c );
 			connection_return( c );
-			ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
 			return 0;
 		}
 	}
@@ -1656,15 +1438,14 @@
 #endif
 
 	if( rc < 0 ) {
-		Debug( LDAP_DEBUG_TRACE,
+		Debug( LDAP_DEBUG_CONNS,
 			"connection_read(%d): input error=%d id=%lu, closing.\n",
 			s, rc, c->c_connid );
 
-		/* connections_mutex and c_mutex are locked */
+		/* c_mutex is locked */
 		connection_closing( c, conn_lost_str );
 		connection_close( c );
 		connection_return( c );
-		ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
 		return 0;
 	}
 
@@ -1685,7 +1466,6 @@
 #endif
 
 	connection_return( c );
-	ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
 
 	return 0;
 }
@@ -1716,7 +1496,7 @@
 		return -1;
 	}
 
-	errno = 0;
+	sock_errset(0);
 
 #ifdef LDAP_CONNECTIONLESS
 	if ( conn->c_is_udp ) {
@@ -1736,7 +1516,7 @@
 
 	tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
 	if ( tag != LDAP_TAG_MESSAGE ) {
-		int err = errno;
+		int err = sock_errno();
 		ber_socket_t	sd;
 
 		ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
@@ -1922,39 +1702,13 @@
 	Operation *op;
 
 	if( conn->c_conn_state == SLAP_C_CLOSING ) {
-		int rc;
 		ber_socket_t	sd;
 		ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
 
-		/* use trylock to avoid possible deadlock */
-		rc = ldap_pvt_thread_mutex_trylock( MCA_GET_CONN_MUTEX( sd ) );
-
-		if( rc ) {
-			Debug( LDAP_DEBUG_TRACE,
-				"connection_resched: reaquiring locks conn=%lu sd=%d\n",
-				conn->c_connid, sd, 0 );
-			/*
-			 * reaquire locks in the right order...
-			 * this may allow another thread to close this connection,
-			 * so recheck state below.
-			 */
-			ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
-			ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX ( sd ) );
-			ldap_pvt_thread_mutex_lock( &conn->c_mutex );
-		}
-
-		if( conn->c_conn_state != SLAP_C_CLOSING ) {
-			Debug( LDAP_DEBUG_TRACE, "connection_resched: "
-				"closed by other thread conn=%lu sd=%d\n",
-				conn->c_connid, sd, 0 );
-		} else {
-			Debug( LDAP_DEBUG_TRACE, "connection_resched: "
-				"attempting closing conn=%lu sd=%d\n",
-				conn->c_connid, sd, 0 );
-			connection_close( conn );
-		}
-
-		ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( sd ) );
+		Debug( LDAP_DEBUG_TRACE, "connection_resched: "
+			"attempting closing conn=%lu sd=%d\n",
+			conn->c_connid, sd, 0 );
+		connection_close( conn );
 		return 0;
 	}
 
@@ -2004,11 +1758,9 @@
 
 static int connection_bind_cb( Operation *op, SlapReply *rs )
 {
-	slap_callback *cb = op->o_callback;
-	op->o_callback = cb->sc_next;
-
 	ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
-	op->o_conn->c_conn_state = SLAP_C_ACTIVE;
+	if ( op->o_conn->c_conn_state == SLAP_C_BINDING )
+		op->o_conn->c_conn_state = SLAP_C_ACTIVE;
 	op->o_conn->c_sasl_bind_in_progress =
 		( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
 
@@ -2059,7 +1811,7 @@
 	}
 	ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
 
-	ch_free( cb );
+	ch_free( op->o_callback );
 	op->o_callback = NULL;
 
 	return SLAP_CB_CONTINUE;
@@ -2128,37 +1880,6 @@
 	return rc;
 }
 
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
-static int connection_write( ber_socket_t s );
-static void *connection_write_thread( void *ctx, void *arg )
-{
-	return (void *)(long)connection_write((long)arg);
-}
-
-int connection_write_activate( ber_socket_t s )
-{
-	int rc;
-
-	/*
-	 * suspend reading on this file descriptor until a connection processing
-	 * thread write data on it. Otherwise the listener thread will repeatedly
-	 * submit the same event on it to the pool.
-	 */
-	slapd_clr_write( s, 0);
-
-	rc = ldap_pvt_thread_pool_submit( &connection_pool,
-		connection_write_thread, (void *)(long)s );
-
-	if( rc != 0 ) {
-		Debug( LDAP_DEBUG_ANY,
-			"connection_write_activate(%d): submit failed (%d)\n",
-			(int) s, rc, 0 );
-	}
-	return rc;
-}
-
-static
-#endif
 int connection_write(ber_socket_t s)
 {
 	Connection *c;
@@ -2166,20 +1887,16 @@
 
 	assert( connections != NULL );
 
-	ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX( s ) );
+	slapd_clr_write( s, 0 );
 
 	c = connection_get( s );
 	if( c == NULL ) {
 		Debug( LDAP_DEBUG_ANY,
 			"connection_write(%ld): no connection!\n",
 			(long)s, 0, 0 );
-		ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( s ) );
 		return -1;
 	}
 
-#ifndef SLAP_LIGHTWEIGHT_DISPATCHER
-	slapd_clr_write( s, 0);
-#endif
 	c->c_n_write++;
 
 	Debug( LDAP_DEBUG_TRACE,
@@ -2216,7 +1933,6 @@
 	}
 
 	connection_return( c );
-	ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
 	return 0;
 }
 
@@ -2241,6 +1957,9 @@
 	op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
 	op->o_tmpmfuncs = &slap_sl_mfuncs;
 	op->o_threadctx = ctx;
+#ifdef LDAP_DEVEL
+	op->o_tid = ldap_pvt_thread_pool_tid( ctx );
+#endif /* LDAP_DEVEL */
 
 	op->o_conn = conn;
 	op->o_connid = op->o_conn->c_connid;

Modified: openldap/vendor/openldap-release/servers/slapd/daemon.c
===================================================================
--- openldap/vendor/openldap-release/servers/slapd/daemon.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/servers/slapd/daemon.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,4 +1,4 @@
-/* $OpenLDAP: pkg/ldap/servers/slapd/daemon.c,v 1.318.2.25 2006/05/11 18:33:31 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/servers/slapd/daemon.c,v 1.318.2.26 2006/11/07 04:25:01 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -42,7 +42,12 @@
 
 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL)
 # include <sys/epoll.h>
-#endif
+#elif defined(SLAP_X_DEVPOLL) && defined(HAVE_SYS_DEVPOLL_H) && defined(HAVE_DEVPOLL)
+# include <sys/types.h>
+# include <sys/stat.h>
+# include <fcntl.h>
+# include <sys/devpoll.h>
+#endif /* ! epoll && ! /dev/poll */
 
 #ifdef HAVE_TCPD
 # include <tcpd.h>
@@ -62,9 +67,9 @@
 
 #ifdef LDAP_PF_INET6
 int slap_inet4or6 = AF_UNSPEC;
-#else
+#else /* ! INETv6 */
 int slap_inet4or6 = AF_INET;
-#endif
+#endif /* ! INETv6 */
 
 /* globals */
 time_t starttime;
@@ -76,7 +81,7 @@
 
 #ifndef SLAPD_LISTEN_BACKLOG
 #define SLAPD_LISTEN_BACKLOG 1024
-#endif
+#endif /* ! SLAPD_LISTEN_BACKLOG */
 
 static ber_socket_t wake_sds[2];
 static int emfile;
@@ -87,14 +92,14 @@
 	if ((w) && ++waking < 5) { \
 		tcp_write( wake_sds[1], "0", 1 ); \
 	} \
-} while(0)
-#else
+} while (0)
+#else /* ! NO_THREADS */
 #define WAKE_LISTENER(w)	do { \
 	if (w) { \
 		tcp_write( wake_sds[1], "0", 1 ); \
 	} \
-} while(0)
-#endif
+} while (0)
+#endif /* ! NO_THREADS */
 
 volatile sig_atomic_t slapd_shutdown = 0;
 volatile sig_atomic_t slapd_gentle_shutdown = 0;
@@ -103,89 +108,110 @@
 static struct slap_daemon {
 	ldap_pvt_thread_mutex_t	sd_mutex;
 #ifdef HAVE_TCPD
-	ldap_pvt_thread_mutex_t tcpd_mutex;
-#endif
+	ldap_pvt_thread_mutex_t	sd_tcpd_mutex;
+#endif /* TCP Wrappers */
 
-	ber_socket_t sd_nactives;
-	int sd_nwriters;
+	ber_socket_t		sd_nactives;
+	int			sd_nwriters;
 
-#ifdef HAVE_EPOLL
-	struct epoll_event *sd_epolls;
-	int	sd_nepolls;
-	int	*sd_index;
-	int	sd_epfd;
-	int	sd_nfds;
-#else
+#if defined(HAVE_EPOLL)
+	struct epoll_event	*sd_epolls;
+	int			*sd_index;
+	int			sd_epfd;
+	int			sd_nfds;
+#elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
+	/* eXperimental */
+	struct pollfd		*sd_pollfd;
+	int			*sd_index;
+	Listener		**sd_l;
+	int			sd_dpfd;
+	int			sd_nfds;
+#else /* ! epoll && ! /dev/poll */
 #ifndef HAVE_WINSOCK
 	/* In winsock, accept() returns values higher than dtblsize
 		so don't bother with this optimization */
-	int sd_nfds;
-#endif
-	fd_set sd_actives;
-	fd_set sd_readers;
-	fd_set sd_writers;
-#endif
+	int			sd_nfds;
+#endif /* ! HAVE_WINSOCK */
+	fd_set			sd_actives;
+	fd_set			sd_readers;
+	fd_set			sd_writers;
+#endif /* ! epoll && ! /dev/poll */
 } slap_daemon;
 
-#ifdef HAVE_EPOLL
+/*
+ * NOTE: naming convention for macros:
+ *
+ * - SLAP_SOCK_* and SLAP_EVENT_* for public interface that deals
+ *   with file descriptors and events respectively
+ *
+ * - SLAP_<type>_* for private interface; type by now is one of
+ *   EPOLL, DEVPOLL, SELECT
+ *
+ * private interface should not be used in the code.
+ */
+#if defined(HAVE_EPOLL)
+/***************************************
+ * Use epoll infrastructure - epoll(4) *
+ ***************************************/
+# define SLAP_EVENT_FNAME		"epoll"
 # define SLAP_EVENTS_ARE_INDEXED	0
-# define SLAP_SOCK_IX(s)	(slap_daemon.sd_index[(s)])
-# define SLAP_SOCK_EP(s)	(slap_daemon.sd_epolls[SLAP_SOCK_IX(s)])
-# define SLAP_SOCK_EV(s)	(SLAP_SOCK_EP(s).events)
-# define SLAP_SOCK_IS_ACTIVE(s)	(SLAP_SOCK_IX(s) != -1)
-# define SLAP_SOCK_NOT_ACTIVE(s)	(SLAP_SOCK_IX(s) == -1)
-# define SLAP_SOCK_IS_SET(s, mode)	(SLAP_SOCK_EV(s) & (mode))
+# define SLAP_EPOLL_SOCK_IX(s)		(slap_daemon.sd_index[(s)])
+# define SLAP_EPOLL_SOCK_EP(s)		(slap_daemon.sd_epolls[SLAP_EPOLL_SOCK_IX(s)])
+# define SLAP_EPOLL_SOCK_EV(s)		(SLAP_EPOLL_SOCK_EP(s).events)
+# define SLAP_SOCK_IS_ACTIVE(s)		(SLAP_EPOLL_SOCK_IX(s) != -1)
+# define SLAP_SOCK_NOT_ACTIVE(s)	(SLAP_EPOLL_SOCK_IX(s) == -1)
+# define SLAP_EPOLL_SOCK_IS_SET(s, mode)	(SLAP_EPOLL_SOCK_EV(s) & (mode))
 
-# define SLAP_SOCK_IS_READ(s)	SLAP_SOCK_IS_SET((s), EPOLLIN)
-# define SLAP_SOCK_IS_WRITE(s)	SLAP_SOCK_IS_SET((s), EPOLLOUT)
+# define SLAP_SOCK_IS_READ(s)		SLAP_EPOLL_SOCK_IS_SET((s), EPOLLIN)
+# define SLAP_SOCK_IS_WRITE(s)		SLAP_EPOLL_SOCK_IS_SET((s), EPOLLOUT)
 
-# define SLAP_SET_SOCK(s, mode) do { \
-	if ((SLAP_SOCK_EV(s) & (mode)) != (mode)) {	\
-		SLAP_SOCK_EV(s) |= (mode); \
-		epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, (s), \
-			&SLAP_SOCK_EP(s)); \
+# define SLAP_EPOLL_SOCK_SET(s, mode)	do { \
+	if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) != (mode) ) {	\
+		SLAP_EPOLL_SOCK_EV(s) |= (mode); \
+		epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, (s), \
+			&SLAP_EPOLL_SOCK_EP(s) ); \
 	} \
-} while(0)
+} while (0)
 
-# define SLAP_CLR_SOCK(s, mode) do { \
-	if ((SLAP_SOCK_EV(s) & (mode))) { \
-		SLAP_SOCK_EV(s) &= ~(mode);	\
-		epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
-			&SLAP_SOCK_EP(s)); \
+# define SLAP_EPOLL_SOCK_CLR(s, mode)	do { \
+	if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) ) { \
+		SLAP_EPOLL_SOCK_EV(s) &= ~(mode);	\
+		epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \
+			&SLAP_EPOLL_SOCK_EP(s) ); \
 	} \
-} while(0)
+} while (0)
 
-# define SLAP_SOCK_SET_READ(s)	SLAP_SET_SOCK(s, EPOLLIN)
-# define SLAP_SOCK_SET_WRITE(s)	SLAP_SET_SOCK(s, EPOLLOUT)
+# define SLAP_SOCK_SET_READ(s)		SLAP_EPOLL_SOCK_SET(s, EPOLLIN)
+# define SLAP_SOCK_SET_WRITE(s)		SLAP_EPOLL_SOCK_SET(s, EPOLLOUT)
 
+# define SLAP_SOCK_CLR_READ(s)		SLAP_EPOLL_SOCK_CLR((s), EPOLLIN)
+# define SLAP_SOCK_CLR_WRITE(s)		SLAP_EPOLL_SOCK_CLR((s), EPOLLOUT)
+
 # ifdef SLAP_LIGHTWEIGHT_DISPATCHER
 #  define SLAP_SOCK_SET_SUSPEND(s) \
-	( slap_daemon.sd_suspend[SLAP_SOCK_IX(s)] = 1 )
+	( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 1 )
 #  define SLAP_SOCK_CLR_SUSPEND(s) \
-	( slap_daemon.sd_suspend[SLAP_SOCK_IX(s)] = 0 )
+	( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 0 )
 #  define SLAP_SOCK_IS_SUSPEND(s) \
-	( slap_daemon.sd_suspend[SLAP_SOCK_IX(s)] == 1 )
-# endif
+	( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] == 1 )
+# endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
 
-# define SLAP_SOCK_CLR_READ(s)	SLAP_CLR_SOCK((s), EPOLLIN)
-# define SLAP_SOCK_CLR_WRITE(s)	SLAP_CLR_SOCK((s), EPOLLOUT)
+# define SLAP_EPOLL_EVENT_CLR(i, mode)	(revents[(i)].events &= ~(mode))
 
-# define SLAP_CLR_EVENT(i, mode)	(revents[(i)].events &= ~(mode))
+# define SLAP_EVENT_MAX			slap_daemon.sd_nfds
 
-# define SLAP_EVENT_MAX	slap_daemon.sd_nfds
-
 /* If a Listener address is provided, store that as the epoll data.
  * Otherwise, store the address of this socket's slot in the
  * index array. If we can't do this add, the system is out of
  * resources and we need to shutdown.
  */
-# define SLAP_ADD_SOCK(s, l) do { \
+# define SLAP_SOCK_ADD(s, l)		do { \
 	int rc; \
-	SLAP_SOCK_IX((s)) = slap_daemon.sd_nfds; \
-	SLAP_SOCK_EP((s)).data.ptr = (l) ? (l) : (void *)(&SLAP_SOCK_IX(s)); \
-	SLAP_SOCK_EV((s)) = EPOLLIN; \
+	SLAP_EPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
+	SLAP_EPOLL_SOCK_EP((s)).data.ptr = (l) ? (l) : (void *)(&SLAP_EPOLL_SOCK_IX(s)); \
+	SLAP_EPOLL_SOCK_EV((s)) = EPOLLIN; \
 	rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_ADD, \
-		(s), &SLAP_SOCK_EP((s))); \
+		(s), &SLAP_EPOLL_SOCK_EP((s))); \
 	if ( rc == 0 ) { \
 		slap_daemon.sd_nfds++; \
 	} else { \
@@ -196,63 +222,260 @@
 	} \
 } while (0)
 
-# define SLAP_EV_LISTENER(ptr) (((int *)(ptr) >= slap_daemon.sd_index && \
-	(int *)(ptr) <= (slap_daemon.sd_index+dtblsize)) ? 0 : 1 )
+# define SLAP_EPOLL_EV_LISTENER(ptr) \
+	(((int *)(ptr) >= slap_daemon.sd_index && \
+	(int *)(ptr) <= &slap_daemon.sd_index[dtblsize]) ? 0 : 1 )
 
-# define SLAP_EV_PTRFD(ptr) (SLAP_EV_LISTENER(ptr) ? \
+# define SLAP_EPOLL_EV_PTRFD(ptr)		(SLAP_EPOLL_EV_LISTENER(ptr) ? \
 	((Listener *)ptr)->sl_sd : (int *)(ptr) - slap_daemon.sd_index)
 
-# define SLAP_DEL_SOCK(s) do { \
-	int fd, rc, index = SLAP_SOCK_IX((s)); \
+# define SLAP_SOCK_DEL(s)		do { \
+	int fd, rc, index = SLAP_EPOLL_SOCK_IX((s)); \
 	if ( index < 0 ) break; \
 	rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, \
-		(s), &SLAP_SOCK_EP((s))); \
+		(s), &SLAP_EPOLL_SOCK_EP((s))); \
 	slap_daemon.sd_epolls[index] = \
 		slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \
-	fd = SLAP_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \
+	fd = SLAP_EPOLL_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \
 	slap_daemon.sd_index[fd] = index; \
 	slap_daemon.sd_index[(s)] = -1; \
 	slap_daemon.sd_nfds--; \
 } while (0)
 
-# define SLAP_EVENT_CLR_READ(i)	SLAP_CLR_EVENT((i), EPOLLIN)
-# define SLAP_EVENT_CLR_WRITE(i)	SLAP_CLR_EVENT((i), EPOLLOUT)
+# define SLAP_EVENT_CLR_READ(i)		SLAP_EPOLL_EVENT_CLR((i), EPOLLIN)
+# define SLAP_EVENT_CLR_WRITE(i)	SLAP_EPOLL_EVENT_CLR((i), EPOLLOUT)
 
-# define SLAP_CHK_EVENT(i, mode)	(revents[(i)].events & mode)
+# define SLAP_EPOLL_EVENT_CHK(i, mode)	(revents[(i)].events & mode)
 
-# define SLAP_EVENT_IS_READ(i)	SLAP_CHK_EVENT((i), EPOLLIN)
-# define SLAP_EVENT_IS_WRITE(i)	SLAP_CHK_EVENT((i), EPOLLOUT)
-# define SLAP_EVENT_IS_LISTENER(i)	SLAP_EV_LISTENER(revents[(i)].data.ptr)
-# define SLAP_EVENT_LISTENER(i)	((Listener *)(revents[(i)].data.ptr))
+# define SLAP_EVENT_IS_READ(i)		SLAP_EPOLL_EVENT_CHK((i), EPOLLIN)
+# define SLAP_EVENT_IS_WRITE(i)		SLAP_EPOLL_EVENT_CHK((i), EPOLLOUT)
+# define SLAP_EVENT_IS_LISTENER(i)	SLAP_EPOLL_EV_LISTENER(revents[(i)].data.ptr)
+# define SLAP_EVENT_LISTENER(i)		((Listener *)(revents[(i)].data.ptr))
 
-# define SLAP_EVENT_FD(i)	SLAP_EV_PTRFD(revents[(i)].data.ptr)
+# define SLAP_EVENT_FD(i)		SLAP_EPOLL_EV_PTRFD(revents[(i)].data.ptr)
 
-# define SLAP_SOCK_SET_INIT do { \
+# define SLAP_SOCK_INIT		do { \
 	slap_daemon.sd_epolls = ch_calloc(1, \
-		sizeof(struct epoll_event) * dtblsize * 2); \
-	slap_daemon.sd_index = ch_malloc(sizeof(int) * dtblsize); \
+		( sizeof(struct epoll_event) * 2 \
+			+ sizeof(int) ) * dtblsize * 2); \
+	slap_daemon.sd_index = (int *)&slap_daemon.sd_epolls[ 2 * dtblsize ]; \
 	slap_daemon.sd_epfd = epoll_create( dtblsize ); \
-	for (i=0; i<dtblsize; i++) slap_daemon.sd_index[i] = -1; \
+	for ( i = 0; i < dtblsize; i++ ) slap_daemon.sd_index[i] = -1; \
 } while (0)
 
-# define SLAP_EVENT_DECL struct epoll_event *revents
+# define SLAP_SOCK_DESTROY		do { \
+	if ( slap_daemon.sd_epolls != NULL ) { \
+		ch_free( slap_daemon.sd_epolls ); \
+		slap_daemon.sd_epolls = NULL; \
+		slap_daemon.sd_index = NULL; \
+		close( slap_daemon.sd_epfd ); \
+	} \
+} while ( 0 )
 
-# define SLAP_EVENT_INIT do { \
+# define SLAP_EVENT_DECL		struct epoll_event *revents
+
+# define SLAP_EVENT_INIT		do { \
 	revents = slap_daemon.sd_epolls + dtblsize; \
 } while (0)
 
-# define SLAP_EVENT_WAIT(tvp) \
-	epoll_wait( slap_daemon.sd_epfd, revents, \
-		dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 )
+# define SLAP_EVENT_WAIT(tvp, nsp)	do { \
+	*(nsp) = epoll_wait( slap_daemon.sd_epfd, revents, \
+		dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 ); \
+} while (0)
 
-#else
+#elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL)
+
+/*************************************************************
+ * Use Solaris' (>= 2.7) /dev/poll infrastructure - poll(7d) *
+ *************************************************************/
+# define SLAP_EVENT_FNAME		"/dev/poll"
+# define SLAP_EVENTS_ARE_INDEXED	0
+/*
+ * - sd_index	is used much like with epoll()
+ * - sd_l	is maintained as an array containing the address
+ *		of the listener; the index is the fd itself
+ * - sd_pollfd	is used to keep track of what data has been
+ *		registered in /dev/poll
+ */
+# define SLAP_DEVPOLL_SOCK_IX(s)	(slap_daemon.sd_index[(s)])
+# define SLAP_DEVPOLL_SOCK_LX(s)	(slap_daemon.sd_l[(s)])
+# define SLAP_DEVPOLL_SOCK_EP(s)	(slap_daemon.sd_pollfd[SLAP_DEVPOLL_SOCK_IX((s))])
+# define SLAP_DEVPOLL_SOCK_FD(s)	(SLAP_DEVPOLL_SOCK_EP((s)).fd)
+# define SLAP_DEVPOLL_SOCK_EV(s)	(SLAP_DEVPOLL_SOCK_EP((s)).events)
+# define SLAP_SOCK_IS_ACTIVE(s)		(SLAP_DEVPOLL_SOCK_IX((s)) != -1)
+# define SLAP_SOCK_NOT_ACTIVE(s)	(SLAP_DEVPOLL_SOCK_IX((s)) == -1)
+# define SLAP_SOCK_IS_SET(s, mode)	(SLAP_DEVPOLL_SOCK_EV((s)) & (mode))
+
+# define SLAP_SOCK_IS_READ(s)		SLAP_SOCK_IS_SET((s), POLLIN)
+# define SLAP_SOCK_IS_WRITE(s)		SLAP_SOCK_IS_SET((s), POLLOUT)
+
+/* as far as I understand, any time we need to communicate with the kernel
+ * about the number and/or properties of a file descriptor we need it to
+ * wait for, we have to rewrite the whole set */
+# define SLAP_DEVPOLL_WRITE_POLLFD(s, pfd, n, what, shdn)	do { \
+	int rc; \
+	size_t size = (n) * sizeof( struct pollfd ); \
+	/* FIXME: use pwrite? */ \
+	rc = write( slap_daemon.sd_dpfd, (pfd), size ); \
+	if ( rc != size ) { \
+		Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
+			"%s fd=%d failed errno=%d\n", \
+			(what), (s), errno ); \
+		if ( (shdn) ) { \
+			slapd_shutdown = 2; \
+		} \
+	} \
+} while (0)
+
+# define SLAP_DEVPOLL_SOCK_SET(s, mode) 	do { \
+	Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_SET_%s(%d)=%d\n", \
+		(mode) == POLLIN ? "READ" : "WRITE", (s), \
+		( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) ); \
+	if ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) { \
+		struct pollfd pfd; \
+		SLAP_DEVPOLL_SOCK_EV((s)) |= (mode); \
+		pfd.fd = SLAP_DEVPOLL_SOCK_FD((s)); \
+		pfd.events = /* (mode) */ SLAP_DEVPOLL_SOCK_EV((s)); \
+		SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd, 1, "SET", 0); \
+	} \
+} while (0)
+
+# define SLAP_DEVPOLL_SOCK_CLR(s, mode)		do { \
+	Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_CLR_%s(%d)=%d\n", \
+		(mode) == POLLIN ? "READ" : "WRITE", (s), \
+		( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) ); \
+	if ((SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) { \
+		struct pollfd pfd[2]; \
+		SLAP_DEVPOLL_SOCK_EV((s)) &= ~(mode); \
+		pfd[0].fd = SLAP_DEVPOLL_SOCK_FD((s)); \
+		pfd[0].events = POLLREMOVE; \
+		pfd[1] = SLAP_DEVPOLL_SOCK_EP((s)); \
+		SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd[0], 2, "CLR", 0); \
+	} \
+} while (0)
+
+# define SLAP_SOCK_SET_READ(s)		SLAP_DEVPOLL_SOCK_SET(s, POLLIN)
+# define SLAP_SOCK_SET_WRITE(s)		SLAP_DEVPOLL_SOCK_SET(s, POLLOUT)
+
+# define SLAP_SOCK_CLR_READ(s)		SLAP_DEVPOLL_SOCK_CLR((s), POLLIN)
+# define SLAP_SOCK_CLR_WRITE(s)		SLAP_DEVPOLL_SOCK_CLR((s), POLLOUT)
+
+# ifdef SLAP_LIGHTWEIGHT_DISPATCHER
+#  define SLAP_SOCK_SET_SUSPEND(s) \
+	( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 1 )
+#  define SLAP_SOCK_CLR_SUSPEND(s) \
+	( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 0 )
+#  define SLAP_SOCK_IS_SUSPEND(s) \
+	( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] == 1 )
+# endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
+
+# define SLAP_DEVPOLL_EVENT_CLR(i, mode)	(revents[(i)].events &= ~(mode))
+
+# define SLAP_EVENT_MAX			slap_daemon.sd_nfds
+
+/* If a Listener address is provided, store that in the sd_l array.
+ * If we can't do this add, the system is out of resources and we 
+ * need to shutdown.
+ */
+# define SLAP_SOCK_ADD(s, l)		do { \
+	Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_ADD(%d, %p)\n", (s), (l), 0 ); \
+	SLAP_DEVPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \
+	SLAP_DEVPOLL_SOCK_LX((s)) = (l); \
+	SLAP_DEVPOLL_SOCK_FD((s)) = (s); \
+	SLAP_DEVPOLL_SOCK_EV((s)) = POLLIN; \
+	SLAP_DEVPOLL_WRITE_POLLFD((s), &SLAP_DEVPOLL_SOCK_EP((s)), 1, "ADD", 1); \
+	slap_daemon.sd_nfds++; \
+} while (0)
+
+# define SLAP_DEVPOLL_EV_LISTENER(ptr)	((ptr) != NULL)
+
+# define SLAP_SOCK_DEL(s)		do { \
+	int fd, index = SLAP_DEVPOLL_SOCK_IX((s)); \
+	Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_DEL(%d)\n", (s), 0, 0 ); \
+	if ( index < 0 ) break; \
+	if ( index < slap_daemon.sd_nfds - 1 ) { \
+		struct pollfd pfd = slap_daemon.sd_pollfd[index]; \
+		fd = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].fd; \
+		slap_daemon.sd_pollfd[index] = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1]; \
+		slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1] = pfd; \
+		slap_daemon.sd_index[fd] = index; \
+	} \
+	slap_daemon.sd_index[(s)] = -1; \
+	slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = POLLREMOVE; \
+	SLAP_DEVPOLL_WRITE_POLLFD((s), &slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1], 1, "DEL", 0); \
+	slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = 0; \
+	slap_daemon.sd_nfds--; \
+} while (0)
+
+# define SLAP_EVENT_CLR_READ(i)		SLAP_DEVPOLL_EVENT_CLR((i), POLLIN)
+# define SLAP_EVENT_CLR_WRITE(i)	SLAP_DEVPOLL_EVENT_CLR((i), POLLOUT)
+
+# define SLAP_DEVPOLL_EVENT_CHK(i, mode)	(revents[(i)].events & (mode))
+
+# define SLAP_EVENT_FD(i)		(revents[(i)].fd)
+
+# define SLAP_EVENT_IS_READ(i)		SLAP_DEVPOLL_EVENT_CHK((i), POLLIN)
+# define SLAP_EVENT_IS_WRITE(i)		SLAP_DEVPOLL_EVENT_CHK((i), POLLOUT)
+# define SLAP_EVENT_IS_LISTENER(i)	SLAP_DEVPOLL_EV_LISTENER(SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i))))
+# define SLAP_EVENT_LISTENER(i)		SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i)))
+
+# define SLAP_SOCK_INIT		do { \
+	slap_daemon.sd_pollfd = ch_calloc( 1, \
+		( sizeof(struct pollfd) * 2 \
+			+ sizeof( int ) \
+			+ sizeof( Listener * ) ) * dtblsize ); \
+	slap_daemon.sd_index = (int *)&slap_daemon.sd_pollfd[ 2 * dtblsize ]; \
+	slap_daemon.sd_l = (Listener **)&slap_daemon.sd_index[ dtblsize ]; \
+	slap_daemon.sd_dpfd = open( SLAP_EVENT_FNAME, O_RDWR ); \
+	if ( slap_daemon.sd_dpfd == -1 ) { \
+		Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \
+			"open(\"" SLAP_EVENT_FNAME "\") failed errno=%d\n", \
+			errno, 0, 0 ); \
+		SLAP_SOCK_DESTROY; \
+		return -1; \
+	} \
+	for ( i = 0; i < dtblsize; i++ ) { \
+		slap_daemon.sd_pollfd[i].fd = -1; \
+		slap_daemon.sd_index[i] = -1; \
+	} \
+} while (0)
+
+# define SLAP_SOCK_DESTROY		do { \
+	if ( slap_daemon.sd_pollfd != NULL ) { \
+		ch_free( slap_daemon.sd_pollfd ); \
+		slap_daemon.sd_pollfd = NULL; \
+		slap_daemon.sd_index = NULL; \
+		slap_daemon.sd_l = NULL; \
+		close( slap_daemon.sd_dpfd ); \
+	} \
+} while ( 0 )
+
+# define SLAP_EVENT_DECL		struct pollfd *revents
+
+# define SLAP_EVENT_INIT		do { \
+	revents = &slap_daemon.sd_pollfd[ dtblsize ]; \
+} while (0)
+
+# define SLAP_EVENT_WAIT(tvp, nsp)	do { \
+	struct dvpoll		sd_dvpoll; \
+	sd_dvpoll.dp_timeout = (tvp) ? (tvp)->tv_sec * 1000 : -1; \
+	sd_dvpoll.dp_nfds = dtblsize; \
+	sd_dvpoll.dp_fds = revents; \
+	*(nsp) = ioctl( slap_daemon.sd_dpfd, DP_POLL, &sd_dvpoll ); \
+} while (0)
+
+#else /* ! epoll && ! /dev/poll */
+
+/**************************************
+ * Use select system call - select(2) *
+ **************************************/
+# define SLAP_EVENT_FNAME		"select"
 /* select */
 
-# define SLAP_EVENTS_ARE_INDEXED 1
-# define SLAP_EVENT_DECL	\
-	fd_set readfds, writefds
+# define SLAP_EVENTS_ARE_INDEXED	1
+# define SLAP_EVENT_DECL		fd_set readfds, writefds
 
-# define SLAP_EVENT_INIT do { \
+# define SLAP_EVENT_INIT		do { \
 	AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) );	\
 	if ( nwriters )	{ \
 		AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) ); \
@@ -262,19 +485,21 @@
 } while (0)
 
 # ifdef FD_SETSIZE
-#  define	CHK_SETSIZE do { \
+#  define SLAP_SELECT_CHK_SETSIZE	do { \
 	if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE; \
 } while (0)
-# else
-#  define	 CHK_SETSIZE do { ; } while (0)
-# endif
+# else /* ! FD_SETSIZE */
+#  define SLAP_SELECT_CHK_SETSIZE	do { ; } while (0)
+# endif /* ! FD_SETSIZE */
 
-# define	SLAP_SOCK_SET_INIT do { \
-	CHK_SETSIZE; \
+# define SLAP_SOCK_INIT			do { \
+	SLAP_SELECT_CHK_SETSIZE; \
 	FD_ZERO(&slap_daemon.sd_readers); \
 	FD_ZERO(&slap_daemon.sd_writers); \
 } while (0)
 
+# define SLAP_SOCK_DESTROY
+
 # define SLAP_SOCK_IS_ACTIVE(fd)	FD_ISSET((fd), &slap_daemon.sd_actives)
 # define SLAP_SOCK_IS_READ(fd)		FD_ISSET((fd), &slap_daemon.sd_readers)
 # define SLAP_SOCK_IS_WRITE(fd)		FD_ISSET((fd), &slap_daemon.sd_writers)
@@ -285,37 +510,37 @@
 # ifdef HAVE_WINSOCK
 #  define SLAP_SOCK_SET_READ(fd)	do { \
 	if (!SLAP_SOCK_IS_READ(fd)) { FD_SET((fd), &slap_daemon.sd_readers); } \
-} while(0)
+} while (0)
 #  define SLAP_SOCK_SET_WRITE(fd)	do { \
 	if (!SLAP_SOCK_IS_WRITE(fd)) { FD_SET((fd), &slap_daemon.sd_writers); } \
-} while(0)
+} while (0)
 
-#  define SLAP_ADDTEST(s)	
-#  define SLAP_EVENT_MAX	dtblsize
-# else
+#  define SLAP_SELECT_ADDTEST(s)	
+#  define SLAP_EVENT_MAX		dtblsize
+# else /* ! HAVE_WINSOCK */
 #  define SLAP_SOCK_SET_READ(fd)	FD_SET((fd), &slap_daemon.sd_readers)
 #  define SLAP_SOCK_SET_WRITE(fd)	FD_SET((fd), &slap_daemon.sd_writers)
 
-#  define SLAP_EVENT_MAX	slap_daemon.sd_nfds
-#  define SLAP_ADDTEST(s)	do { \
+#  define SLAP_EVENT_MAX		slap_daemon.sd_nfds
+#  define SLAP_SELECT_ADDTEST(s)	do { \
 	if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
 } while (0)
-# endif
+# endif /* ! HAVE_WINSOCK */
 
 # define SLAP_SOCK_CLR_READ(fd)		FD_CLR((fd), &slap_daemon.sd_readers)
 # define SLAP_SOCK_CLR_WRITE(fd)	FD_CLR((fd), &slap_daemon.sd_writers)
 
-# define SLAP_ADD_SOCK(s, l) do { \
-	SLAP_ADDTEST((s)); \
+# define SLAP_SOCK_ADD(s, l)		do { \
+	SLAP_SELECT_ADDTEST((s)); \
 	FD_SET((s), &slap_daemon.sd_actives); \
 	FD_SET((s), &slap_daemon.sd_readers); \
-} while(0)
+} while (0)
 
-# define SLAP_DEL_SOCK(s) do { \
+# define SLAP_SOCK_DEL(s)		do { \
 	FD_CLR((s), &slap_daemon.sd_actives); \
 	FD_CLR((s), &slap_daemon.sd_readers); \
 	FD_CLR((s), &slap_daemon.sd_writers); \
-} while(0)
+} while (0)
 
 # define SLAP_EVENT_IS_READ(fd)		FD_ISSET((fd), &readfds)
 # define SLAP_EVENT_IS_WRITE(fd)	FD_ISSET((fd), &writefds)
@@ -323,10 +548,11 @@
 # define SLAP_EVENT_CLR_READ(fd) 	FD_CLR((fd), &readfds)
 # define SLAP_EVENT_CLR_WRITE(fd)	FD_CLR((fd), &writefds)
 
-# define SLAP_EVENT_WAIT(tvp) \
-	select( SLAP_EVENT_MAX, &readfds, \
-		nwriters > 0 ? &writefds : NULL, NULL, (tvp) )
-#endif
+# define SLAP_EVENT_WAIT(tvp, nsp)	do { \
+	*(nsp) = select( SLAP_EVENT_MAX, &readfds, \
+		nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
+} while (0)
+#endif /* ! epoll && ! /dev/poll */
 
 #ifdef HAVE_SLP
 /*
@@ -339,17 +565,23 @@
 static char** slapd_srvurls = NULL;
 static SLPHandle slapd_hslp = 0;
 int slapd_register_slp = 0;
+const char *slapd_slp_attrs = NULL;
 
-void slapd_slp_init( const char* urls ) {
+static SLPError slapd_slp_cookie;
+
+static void
+slapd_slp_init( const char* urls )
+{
 	int i;
+	SLPError err;
 
 	slapd_srvurls = ldap_str2charray( urls, " " );
 
-	if( slapd_srvurls == NULL ) return;
+	if ( slapd_srvurls == NULL ) return;
 
 	/* find and expand INADDR_ANY URLs */
-	for( i=0; slapd_srvurls[i] != NULL; i++ ) {
-		if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) {
+	for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
+		if ( strcmp( slapd_srvurls[i], "ldap:///" ) == 0 ) {
 			char *host = ldap_pvt_get_fqdn( NULL );
 			if ( host != NULL ) {
 				slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
@@ -361,7 +593,7 @@
 				ch_free( host );
 			}
 
-		} else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) {
+		} else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0 ) {
 			char *host = ldap_pvt_get_fqdn( NULL );
 			if ( host != NULL ) {
 				slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
@@ -376,11 +608,18 @@
 	}
 
 	/* open the SLP handle */
-	SLPOpen( "en", 0, &slapd_hslp );
+	err = SLPOpen( "en", 0, &slapd_hslp );
+
+	if ( err != SLP_OK ) {
+		Debug( LDAP_DEBUG_CONNS, "daemon: SLPOpen() failed with %ld\n",
+			(long)err, 0, 0 );
+	}
 }
 
-void slapd_slp_deinit() {
-	if( slapd_srvurls == NULL ) return;
+static void
+slapd_slp_deinit( void )
+{
+	if ( slapd_srvurls == NULL ) return;
 
 	ldap_charray_free( slapd_srvurls );
 	slapd_srvurls = NULL;
@@ -389,47 +628,67 @@
 	SLPClose( slapd_hslp );
 }
 
-void slapd_slp_regreport(
-	SLPHandle hslp,
-	SLPError errcode,
-	void* cookie )
+static void
+slapd_slp_regreport(
+	SLPHandle	hslp,
+	SLPError	errcode,
+	void		*cookie )
 {
-	/* empty report */
+	/* return the error code in the cookie */
+	*(SLPError*)cookie = errcode; 
 }
 
-void slapd_slp_reg() {
+static void
+slapd_slp_reg()
+{
 	int i;
+	SLPError err;
 
-	if( slapd_srvurls == NULL ) return;
+	if ( slapd_srvurls == NULL ) return;
 
-	for( i=0; slapd_srvurls[i] != NULL; i++ ) {
-		if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
+	for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
+		if ( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX,
 				sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 ||
-		    strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
+			strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX,
 				sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 )
 		{
-			SLPReg( slapd_hslp,
+			err = SLPReg( slapd_hslp,
 				slapd_srvurls[i],
 				SLP_LIFETIME_MAXIMUM,
 				"ldap",
-				"",
-				1,
+				(slapd_slp_attrs) ? slapd_slp_attrs : "",
+				SLP_TRUE,
 				slapd_slp_regreport,
-				NULL );
+				&slapd_slp_cookie );
+
+			if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
+				Debug( LDAP_DEBUG_CONNS,
+					"daemon: SLPReg(%s) failed with %ld, cookie = %ld\n",
+					slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
+			}	
 		}
 	}
 }
 
-void slapd_slp_dereg() {
+static void
+slapd_slp_dereg( void )
+{
 	int i;
+	SLPError err;
 
-	if( slapd_srvurls == NULL ) return;
+	if ( slapd_srvurls == NULL ) return;
 
-	for( i=0; slapd_srvurls[i] != NULL; i++ ) {
-		SLPDereg( slapd_hslp,
+	for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
+		err = SLPDereg( slapd_hslp,
 			slapd_srvurls[i],
 			slapd_slp_regreport,
-			NULL );
+			&slapd_slp_cookie );
+		
+		if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) {
+			Debug( LDAP_DEBUG_CONNS,
+				"daemon: SLPDereg(%s) failed with %ld, cookie = %ld\n",
+				slapd_srvurls[i], (long)err, (long)slapd_slp_cookie );
+		}
 	}
 }
 #endif /* HAVE_SLP */
@@ -443,31 +702,41 @@
  * idletimeout. The underlying event handler may record the Listener
  * argument to differentiate Listener's from real sessions.
  */
-static void slapd_add(ber_socket_t s, int isactive, Listener *sl) {
+static void
+slapd_add( ber_socket_t s, int isactive, Listener *sl )
+{
 	ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 
 	assert( SLAP_SOCK_NOT_ACTIVE(s) );
 
 	if ( isactive ) slap_daemon.sd_nactives++;
 
-	SLAP_ADD_SOCK(s, sl);
+	SLAP_SOCK_ADD(s, sl);
 
-	Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr\n",
-		(long) s, 0, 0 );
+	Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr%s listener=%p\n",
+		(long) s, isactive ? " (active)" : "", (void *)sl );
 
 	ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
 
 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
 	WAKE_LISTENER(1);
-#endif
+#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
 }
 
-void slapd_sd_lock()
+/*
+ * NOTE: unused
+ */
+void
+slapd_sd_lock( void )
 {
 	ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 }
 
-void slapd_sd_unlock()
+/*
+ * NOTE: unused
+ */
+void
+slapd_sd_unlock( void )
 {
 	ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
 }
@@ -475,8 +744,10 @@
 /*
  * Remove the descriptor from daemon control
  */
-void slapd_remove(
+void
+slapd_remove(
 	ber_socket_t s,
+	Sockbuf *sb,
 	int wasactive,
 	int wake,
 	int locked )
@@ -501,8 +772,11 @@
 
 	if ( waswriter ) slap_daemon.sd_nwriters--;
 
-	SLAP_DEL_SOCK(s);
+	SLAP_SOCK_DEL(s);
 
+	if ( sb )
+		ber_sockbuf_free(sb);
+
 	/* If we ran out of file descriptors, we dropped a listener from
 	 * the select() loop. Now that we're removing a session from our
 	 * control, we can try to resume a dropped listener to use.
@@ -529,7 +803,9 @@
 	WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
 }
 
-void slapd_clr_write(ber_socket_t s, int wake) {
+void
+slapd_clr_write( ber_socket_t s, int wake )
+{
 	ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 
 	assert( SLAP_SOCK_IS_ACTIVE( s ));
@@ -543,7 +819,9 @@
 	WAKE_LISTENER(wake);
 }
 
-void slapd_set_write(ber_socket_t s, int wake) {
+void
+slapd_set_write( ber_socket_t s, int wake )
+{
 	ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 
 	assert( SLAP_SOCK_IS_ACTIVE( s ));
@@ -557,7 +835,9 @@
 	WAKE_LISTENER(wake);
 }
 
-int slapd_clr_read(ber_socket_t s, int wake) {
+int
+slapd_clr_read( ber_socket_t s, int wake )
+{
 	int rc = 1;
 	ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 
@@ -571,7 +851,9 @@
 	return rc;
 }
 
-void slapd_set_read(ber_socket_t s, int wake) {
+void
+slapd_set_read( ber_socket_t s, int wake )
+{
 	ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 
 	assert( SLAP_SOCK_IS_ACTIVE( s ));
@@ -581,13 +863,17 @@
 	WAKE_LISTENER(wake);
 }
 
-static void slapd_close(ber_socket_t s) {
+static void
+slapd_close( ber_socket_t s )
+{
 	Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
 		(long) s, 0, 0 );
 	tcp_close(s);
 }
 
-static void slap_free_listener_addresses(struct sockaddr **sal) {
+static void
+slap_free_listener_addresses( struct sockaddr **sal )
+{
 	struct sockaddr **sap;
 	if (sal == NULL) return;
 	for (sap = sal; *sap != NULL; sap++) ch_free(*sap);
@@ -595,7 +881,8 @@
 }
 
 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
-static int get_url_perms(
+static int
+get_url_perms(
 	char 	**exts,
 	mode_t	*perms,
 	int	*crit )
@@ -675,10 +962,11 @@
 #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */
 
 /* port = 0 indicates AF_LOCAL */
-static int slap_get_listener_addresses(
+static int
+slap_get_listener_addresses(
 	const char *host,
 	unsigned short port,
-	struct sockaddr ***sal)
+	struct sockaddr ***sal )
 {
 	struct sockaddr **sap;
 
@@ -705,7 +993,7 @@
 		(*sap)->sa_family = AF_LOCAL;
 		strcpy( ((struct sockaddr_un *)*sap)->sun_path, host );
 	} else
-#endif
+#endif /* LDAP_PF_LOCAL */
 	{
 #ifdef HAVE_GETADDRINFO
 		struct addrinfo hints, *res, *sai;
@@ -753,7 +1041,7 @@
 				*(struct sockaddr_in6 *)*sap =
 					*((struct sockaddr_in6 *)sai->ai_addr);
 				break;
-#  endif
+#  endif /* LDAP_PF_INET6 */
 			case AF_INET:
 				*sap = ch_malloc(sizeof(struct sockaddr_in));
 				if (*sap == NULL) {
@@ -777,7 +1065,7 @@
 
 		freeaddrinfo(res);
 
-#else
+#else /* ! HAVE_GETADDRINFO */
 		int i, n = 1;
 		struct in_addr in;
 		struct hostent *he = NULL;
@@ -811,7 +1099,7 @@
 				sizeof(struct in_addr) );
 		}
 		sap[i] = NULL;
-#endif
+#endif /* ! HAVE_GETADDRINFO */
 	}
 
 	return 0;
@@ -821,11 +1109,11 @@
 	return -1;
 }
 
-static int slap_open_listener(
+static int
+slap_open_listener(
 	const char* url,
 	int *listeners,
-	int *cur
-	)
+	int *cur )
 {
 	int	num, tmp, rc;
 	Listener l;
@@ -856,7 +1144,7 @@
 	l.sl_mute = 0;
 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
 	l.sl_busy = 0;
-#endif
+#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
 
 #ifndef HAVE_TLS
 	if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
@@ -868,13 +1156,13 @@
 
 	if(! lud->lud_port ) lud->lud_port = LDAP_PORT;
 
-#else
+#else /* HAVE_TLS */
 	l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme );
 
 	if(! lud->lud_port ) {
 		lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT;
 	}
-#endif
+#endif /* HAVE_TLS */
 
 	port = (unsigned short) lud->lud_port;
 
@@ -886,13 +1174,13 @@
 		} else {
 			err = slap_get_listener_addresses(lud->lud_host, 0, &sal);
 		}
-#else
+#else /* ! LDAP_PF_LOCAL */
 
 		Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s",
 			url, 0, 0);
 		ldap_free_urldesc( lud );
 		return -1;
-#endif
+#endif /* ! LDAP_PF_LOCAL */
 	} else {
 		if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
 			|| strcmp(lud->lud_host, "*") == 0 )
@@ -905,7 +1193,7 @@
 
 #ifdef LDAP_CONNECTIONLESS
 	l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
-#endif
+#endif /* LDAP_CONNECTIONLESS */
 
 #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
 	if ( lud->lud_exts ) {
@@ -939,12 +1227,12 @@
 		case AF_INET6:
 			af = "IPv6";
 			break;
-#endif
+#endif /* LDAP_PF_INET6 */
 #ifdef LDAP_PF_LOCAL
 		case AF_LOCAL:
 			af = "Local";
 			break;
-#endif
+#endif /* LDAP_PF_LOCAL */
 		default:
 			sal++;
 			continue;
@@ -952,7 +1240,7 @@
 
 #ifdef LDAP_CONNECTIONLESS
 		if( l.sl_is_udp ) socktype = SOCK_DGRAM;
-#endif
+#endif /* LDAP_CONNECTIONLESS */
 
 		l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
 		if ( l.sl_sd == AC_SOCKET_INVALID ) {
@@ -973,13 +1261,13 @@
 			sal++;
 			continue;
 		}
-#endif
+#endif /* ! HAVE_WINSOCK */
 
 #ifdef LDAP_PF_LOCAL
 		if ( (*sal)->sa_family == AF_LOCAL ) {
 			unlink( ((struct sockaddr_un *)*sal)->sun_path );
 		} else
-#endif
+#endif /* LDAP_PF_LOCAL */
 		{
 #ifdef SO_REUSEADDR
 			/* enable address reuse */
@@ -992,7 +1280,7 @@
 					"setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
 					(long) l.sl_sd, err, sock_errstr(err) );
 			}
-#endif
+#endif /* SO_REUSEADDR */
 		}
 
 		switch( (*sal)->sa_family ) {
@@ -1012,29 +1300,52 @@
 					"setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n",
 					(long) l.sl_sd, err, sock_errstr(err) );
 			}
-#endif
+#endif /* IPV6_V6ONLY */
 			addrlen = sizeof(struct sockaddr_in6);
 			break;
-#endif
+#endif /* LDAP_PF_INET6 */
 
 #ifdef LDAP_PF_LOCAL
 		case AF_LOCAL:
 #ifdef LOCAL_CREDS
+			{
+				int one = 1;
+				setsockopt( l.sl_sd, 0, LOCAL_CREDS, &one, sizeof( one ) );
+			}
+#endif /* LOCAL_CREDS */
+
+			addrlen = sizeof( struct sockaddr_un );
+			break;
+#endif /* LDAP_PF_LOCAL */
+		}
+
+#ifdef LDAP_PF_LOCAL
+		/* create socket with all permissions set for those systems
+		 * that honor permissions on sockets (e.g. Linux); typically,
+		 * only write is required.  To exploit filesystem permissions,
+		 * place the socket in a directory and use directory's
+		 * permissions.  Need write perms to the directory to 
+		 * create/unlink the socket; likely need exec perms to access
+		 * the socket (ITS#4709) */
 		{
-			int one = 1;
-			setsockopt(l.sl_sd, 0, LOCAL_CREDS, &one, sizeof one);
+			mode_t old_umask;
+
+			if ( (*sal)->sa_family == AF_LOCAL ) {
+				old_umask = umask( 0 );
+			}
+#endif /* LDAP_PF_LOCAL */
+			rc = bind( l.sl_sd, *sal, addrlen );
+#ifdef LDAP_PF_LOCAL
+			if ( (*sal)->sa_family == AF_LOCAL ) {
+				umask( old_umask );
+			}
 		}
-#endif
-		addrlen = sizeof(struct sockaddr_un);
-		break;
-#endif
-		}
-
-		if (bind(l.sl_sd, *sal, addrlen)) {
+#endif /* LDAP_PF_LOCAL */
+		if ( rc ) {
 			err = sock_errno();
 			Debug( LDAP_DEBUG_ANY,
 				"daemon: bind(%ld) failed errno=%d (%s)\n",
-				(long) l.sl_sd, err, sock_errstr(err) );
+				(long)l.sl_sd, err, sock_errstr( err ) );
 			tcp_close( l.sl_sd );
 			sal++;
 			continue;
@@ -1058,9 +1369,9 @@
 			inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
 				addr, sizeof(addr) );
 			s = addr;
-#else
+#else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
 			s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
-#endif
+#endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
 			port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port );
 			l.sl_name.bv_val =
 				ber_memalloc( sizeof("IP=255.255.255.255:65535") );
@@ -1115,7 +1426,8 @@
 static int sockinit(void);
 static int sockdestroy(void);
 
-int slapd_daemon_init( const char *urls )
+int
+slapd_daemon_init( const char *urls )
 {
 	int i, j, n, rc;
 	char **u;
@@ -1125,8 +1437,8 @@
 
 	ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
 #ifdef HAVE_TCPD
-	ldap_pvt_thread_mutex_init( &slap_daemon.tcpd_mutex );
-#endif
+	ldap_pvt_thread_mutex_init( &slap_daemon.sd_tcpd_mutex );
+#endif /* TCP Wrappers */
 
 	if( (rc = sockinit()) != 0 ) return rc;
 
@@ -1134,9 +1446,9 @@
 	dtblsize = sysconf( _SC_OPEN_MAX );
 #elif HAVE_GETDTABLESIZE
 	dtblsize = getdtablesize();
-#else
+#else /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
 	dtblsize = FD_SETSIZE;
-#endif
+#endif /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */
 
 	/* open a pipe (or something equivalent connected to itself).
 	 * we write a byte on this fd whenever we catch a signal. The main
@@ -1149,7 +1461,7 @@
 		return rc;
 	}
 
-	SLAP_SOCK_SET_INIT;
+	SLAP_SOCK_INIT;
 
 	if( urls == NULL ) urls = "ldap:///";
 
@@ -1158,6 +1470,8 @@
 	if( u == NULL || u[0] == NULL ) {
 		Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n",
 			urls, 0, 0 );
+		if ( u )
+			ldap_charray_free( u );
 		return -1;
 	}
 
@@ -1194,7 +1508,7 @@
 		slapd_slp_init( urls );
 		slapd_slp_reg();
 	}
-#endif
+#endif /* HAVE_SLP */
 
 	ldap_charray_free( u );
 
@@ -1203,7 +1517,7 @@
 
 
 int
-slapd_daemon_destroy(void)
+slapd_daemon_destroy( void )
 {
 	connections_destroy();
 	tcp_close( wake_sds[1] );
@@ -1215,11 +1529,11 @@
 		slapd_slp_dereg();
 		slapd_slp_deinit();
 	}
-#endif
+#endif /* HAVE_SLP */
 
 #ifdef HAVE_TCPD
-	ldap_pvt_thread_mutex_destroy( &slap_daemon.tcpd_mutex );
-#endif
+	ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_tcpd_mutex );
+#endif /* TCP Wrappers */
 
 	ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_mutex );
 	return 0;
@@ -1236,7 +1550,7 @@
 		Listener *lr = slap_listeners[l];
 
 		if ( lr->sl_sd != AC_SOCKET_INVALID ) {
-			if ( remove ) slapd_remove( lr->sl_sd, 0, 0, 0 );
+			if ( remove ) slapd_remove( lr->sl_sd, NULL, 0, 0, 0 );
 
 #ifdef LDAP_PF_LOCAL
 			if ( lr->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
@@ -1273,7 +1587,7 @@
 	struct berval authid = BER_BVNULL;
 #ifdef SLAPD_RLOOKUPS
 	char hbuf[NI_MAXHOST];
-#endif
+#endif /* SLAPD_RLOOKUPS */
 
 	char	*dnsname = NULL;
 	char	*peeraddr = NULL;
@@ -1281,15 +1595,19 @@
 	char peername[MAXPATHLEN + sizeof("PATH=")];
 #elif defined(LDAP_PF_INET6)
 	char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
-#else
+#else /* ! LDAP_PF_LOCAL && ! LDAP_PF_INET6 */
 	char peername[sizeof("IP=255.255.255.255:65336")];
 #endif /* LDAP_PF_LOCAL */
 
+	Debug( LDAP_DEBUG_TRACE,
+		">>> slap_listener(%s)",
+		sl->sl_url.bv_val, 0, 0 );
+
 	peername[0] = '\0';
 
 #ifdef LDAP_CONNECTIONLESS
 	if ( sl->sl_is_udp ) return 1;
-#endif
+#endif /* LDAP_CONNECTIONLESS */
 
 #  ifdef LDAP_PF_LOCAL
 	/* FIXME: apparently accept doesn't fill
@@ -1305,7 +1623,7 @@
 	 */
 	sl->sl_busy = 0;
 	WAKE_LISTENER(1);
-#endif
+#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
 
 	if ( s == AC_SOCKET_INVALID ) {
 		int err = sock_errno();
@@ -1313,10 +1631,10 @@
 		if(
 #ifdef EMFILE
 		    err == EMFILE ||
-#endif
+#endif /* EMFILE */
 #ifdef ENFILE
 		    err == ENFILE ||
-#endif
+#endif /* ENFILE */
 		    0 )
 		{
 			ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
@@ -1344,14 +1662,14 @@
 		ldap_pvt_thread_yield();
 		return 0;
 	}
-#endif
+#endif /* ! HAVE_WINSOCK */
 
 #ifdef LDAP_DEBUG
 	ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 	/* newly accepted stream should not be in any of the FD SETS */
 	assert( SLAP_SOCK_NOT_ACTIVE( s ));
 	ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
-#endif
+#endif /* LDAP_DEBUG */
 
 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
 #ifdef LDAP_PF_LOCAL
@@ -1372,7 +1690,7 @@
 				"slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
 				"errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
 		}
-#endif
+#endif /* SO_KEEPALIVE */
 #ifdef TCP_NODELAY
 		/* enable no delay */
 		tmp = 1;
@@ -1384,9 +1702,9 @@
 				"slapd(%ld): setsockopt(TCP_NODELAY) failed "
 				"errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
 		}
-#endif
+#endif /* TCP_NODELAY */
 	}
-#endif
+#endif /* SO_KEEPALIVE || TCP_NODELAY */
 
 	Debug( LDAP_DEBUG_CONNS,
 		"daemon: listen=%ld, new connection on %ld\n",
@@ -1462,7 +1780,7 @@
 	if ( ( from.sa_addr.sa_family == AF_INET )
 #ifdef LDAP_PF_INET6
 		|| ( from.sa_addr.sa_family == AF_INET6 )
-#endif
+#endif /* LDAP_PF_INET6 */
 		)
 	{
 		dnsname = NULL;
@@ -1480,12 +1798,12 @@
 #ifdef HAVE_TCPD
 		{
 			int rc;
-			ldap_pvt_thread_mutex_lock( &slap_daemon.tcpd_mutex );
+			ldap_pvt_thread_mutex_lock( &slap_daemon.sd_tcpd_mutex );
 			rc = hosts_ctl("slapd",
 				dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
 				peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
 				SLAP_STRING_UNKNOWN );
-			ldap_pvt_thread_mutex_unlock( &slap_daemon.tcpd_mutex );
+			ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_tcpd_mutex );
 			if ( !rc ) {
 				/* DENY ACCESS */
 				Statslog( LDAP_DEBUG_STATS,
@@ -1506,9 +1824,9 @@
 		peername,
 #ifdef HAVE_TLS
 		sl->sl_is_tls ? CONN_IS_TLS : 0,
-#else
+#else /* ! HAVE_TLS */
 		0,
-#endif
+#endif /* ! HAVE_TLS */
 		ssf,
 		authid.bv_val ? &authid : NULL );
 
@@ -1536,13 +1854,15 @@
 	void* ctx,
 	void* ptr )
 {
-	int rc;
+	int		rc;
+	Listener	*sl = (Listener *)ptr;
 
-	rc = slap_listener( (Listener*)ptr );
+	rc = slap_listener( sl );
 
 	if( rc != LDAP_SUCCESS ) {
 		Debug( LDAP_DEBUG_ANY,
-			"listener_thread: failed %d", rc, 0, 0 );
+			"slap_listener_thread(%s): failed err=%d",
+			sl->sl_url.bv_val, rc, 0 );
 	}
 
 	return (void*)NULL;
@@ -1569,7 +1889,7 @@
 	}
 	return rc;
 }
-#endif
+#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
 
 static void *
 slapd_daemon_task(
@@ -1609,7 +1929,7 @@
 		 */
 		if ( slap_listeners[l]->sl_is_udp )
 			continue;
-#endif
+#endif /* LDAP_CONNECTIONLESS */
 
 		if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN_BACKLOG ) == -1 ) {
 			int err = sock_errno();
@@ -1648,7 +1968,7 @@
 					}
 				}
 			}
-#endif				
+#endif /* LDAP_PF_INET6 */
 			Debug( LDAP_DEBUG_ANY,
 				"daemon: listen(%s, 5) failed errno=%d (%s)\n",
 					slap_listeners[l]->sl_url.bv_val, err,
@@ -1665,7 +1985,7 @@
 			slapd_shutdown = 2;
 			return (void*)-1;
 		}
-#endif
+#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
 
 		slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
 	}
@@ -1674,7 +1994,7 @@
 	if ( started_event != NULL ) {
 		ldap_pvt_thread_cond_signal( &started_event );
 	}
-#endif
+#endif /* HAVE_NT_SERVICE_MANAGER */
 
 #ifdef SLAP_SEM_LOAD_CONTROL
 	/*
@@ -1683,21 +2003,21 @@
 	(void) ldap_lazy_sem_init(
 		SLAP_MAX_WORKER_THREADS + 4 /* max workers + margin */,
 		4 /* lazyness */ );
-#endif
+#endif /* SLAP_SEM_LOAD_CONTROL */
 
 	/* initialization complete. Here comes the loop. */
 
 	while ( !slapd_shutdown ) {
-		ber_socket_t i;
-		int ns, nwriters;
-		int at;
-		ber_socket_t nfds;
+		ber_socket_t		i;
+		int			ns, nwriters;
+		int			at;
+		ber_socket_t		nfds;
 #if SLAP_EVENTS_ARE_INDEXED
-		ber_socket_t nrfds, nwfds;
-#endif
+		ber_socket_t		nrfds, nwfds;
+#endif /* SLAP_EVENTS_ARE_INDEXED */
 #define SLAPD_EBADF_LIMIT 16
 
-		time_t	now;
+		time_t			now;
 
 		SLAP_EVENT_DECL;
 
@@ -1705,23 +2025,25 @@
 		struct timeval		*tvp;
 
 		struct timeval		cat;
-		time_t				tdelta = 1;
+		time_t			tdelta = 1;
 		struct re_s*		rtask;
+
 		now = slap_get_time();
 
-		if( ( global_idletimeout > 0 ) &&
+		if ( ( global_idletimeout > 0 ) &&
 			difftime( last_idle_check +
-			global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
+				global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 )
+		{
 			connections_timeout_idle( now );
 			last_idle_check = now;
 		}
 		tv = idle;
 
 #ifdef SIGHUP
-		if( slapd_gentle_shutdown ) {
+		if ( slapd_gentle_shutdown ) {
 			ber_socket_t active;
 
-			if( slapd_gentle_shutdown == 1 ) {
+			if ( slapd_gentle_shutdown == 1 ) {
 				BackendDB *be;
 				Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
 				close_listeners( 1 );
@@ -1735,12 +2057,12 @@
 			ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 			active = slap_daemon.sd_nactives;
 			ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
-			if( active == 0 ) {
+			if ( active == 0 ) {
 				slapd_shutdown = 1;
 				break;
 			}
 		}
-#endif
+#endif /* SIGHUP */
 		at = 0;
 
 		ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
@@ -1754,11 +2076,11 @@
 
 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
 			if ( lr->sl_mute || lr->sl_busy )
-#else
+#else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
 			if ( lr->sl_mute )
-#endif
+#endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
 			{
-			    SLAP_SOCK_CLR_READ( lr->sl_sd );
+				SLAP_SOCK_CLR_READ( lr->sl_sd );
 			} else {
 				SLAP_SOCK_SET_READ( lr->sl_sd );
 			}
@@ -1775,7 +2097,7 @@
 		if ( at 
 #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
 			&&  ( tv.tv_sec || tv.tv_usec )
-#endif
+#endif /* HAVE_YIELDING_SELECT || NO_THREADS */
 			)
 		{
 			tvp = &tv;
@@ -1793,7 +2115,7 @@
 				ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
 				ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
 				ldap_pvt_thread_pool_submit( &connection_pool,
-											rtask->routine, (void *) rtask );
+					rtask->routine, (void *) rtask );
 				ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
 			}
 			rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
@@ -1819,7 +2141,8 @@
 
 			if ( lr->sl_mute ) {
 				Debug( LDAP_DEBUG_CONNS,
-					"daemon: select: listen=%d muted\n",
+					"daemon: " SLAP_EVENT_FNAME ": "
+					"listen=%d muted\n",
 					lr->sl_sd, 0, 0 );
 				continue;
 			}
@@ -1827,32 +2150,40 @@
 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
 			if ( lr->sl_busy ) {
 				Debug( LDAP_DEBUG_CONNS,
-					"daemon: select: listen=%d busy\n",
+					"daemon: " SLAP_EVENT_FNAME ": "
+					"listen=%d busy\n",
 					lr->sl_sd, 0, 0 );
 				continue;
 			}
-#endif
+#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
 
 			Debug( LDAP_DEBUG_CONNS,
-				"daemon: select: listen=%d active_threads=%d tvp=%s\n",
+				"daemon: " SLAP_EVENT_FNAME ": "
+				"listen=%d active_threads=%d tvp=%s\n",
 				lr->sl_sd, at, tvp == NULL ? "NULL" : "zero" );
 		}
 
-		switch(ns = SLAP_EVENT_WAIT(tvp)) {
+		SLAP_EVENT_WAIT( tvp, &ns );
+		switch ( ns ) {
 		case -1: {	/* failure - try again */
 				int err = sock_errno();
 
-				if( err != EINTR ) {
+				if ( err != EINTR ) {
 					ebadf++;
 
 					/* Don't log unless we got it twice in a row */
-					if ( !( ebadf & 1 )) {
+					if ( !( ebadf & 1 ) ) {
 						Debug( LDAP_DEBUG_ANY,
-							"daemon: select failed count %d err (%d): %s\n",
-							ebadf, err, sock_errstr(err) );
+							"daemon: "
+							SLAP_EVENT_FNAME
+							"failed count %d "
+							"err (%d): %s\n",
+							ebadf, err,
+							sock_errstr( err ) );
 					}
-					if ( ebadf >= SLAPD_EBADF_LIMIT )
+					if ( ebadf >= SLAPD_EBADF_LIMIT ) {
 						slapd_shutdown = 2;
+					}
 				}
 			}
 			continue;
@@ -1860,15 +2191,16 @@
 		case 0:		/* timeout - let threads run */
 			ebadf = 0;
 #ifndef HAVE_YIELDING_SELECT
-			Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n",
-			    0, 0, 0 );
+			Debug( LDAP_DEBUG_CONNS, "daemon: " SLAP_EVENT_FNAME
+				"timeout - yielding\n",
+				0, 0, 0 );
 
 			ldap_pvt_thread_yield();
-#endif
+#endif /* ! HAVE_YIELDING_SELECT */
 			continue;
 
 		default:	/* something happened - deal with it */
-			if( slapd_shutdown ) continue;
+			if ( slapd_shutdown ) continue;
 
 			ebadf = 0;
 			Debug( LDAP_DEBUG_CONNS,
@@ -1878,7 +2210,7 @@
 		}
 
 #if SLAP_EVENTS_ARE_INDEXED
-		if ( SLAP_EVENT_IS_READ( wake_sds[0] )) {
+		if ( SLAP_EVENT_IS_READ( wake_sds[0] ) ) {
 			char c[BUFSIZ];
 			SLAP_EVENT_CLR_READ( wake_sds[0] );
 			waking = 0;
@@ -1898,8 +2230,8 @@
 			if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue;
 #ifdef LDAP_CONNECTIONLESS
 			if ( slap_listeners[l]->sl_is_udp ) continue;
-#endif
-			if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd )) continue;
+#endif /* LDAP_CONNECTIONLESS */
+			if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ) ) continue;
 			
 			/* clear events */
 			SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd );
@@ -1907,17 +2239,17 @@
 			ns--;
 
 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
-			rc = slap_listener_activate(slap_listeners[l]);
-#else
-			rc = slap_listener(slap_listeners[l]);
-#endif
+			rc = slap_listener_activate( slap_listeners[l] );
+#else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
+			rc = slap_listener( slap_listeners[l] );
+#endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
 		}
 
 		/* bypass the following tests if no descriptors left */
 		if ( ns <= 0 ) {
 #ifndef HAVE_YIELDING_SELECT
 			ldap_pvt_thread_yield();
-#endif
+#endif /* HAVE_YIELDING_SELECT */
 			continue;
 		}
 
@@ -1934,7 +2266,7 @@
 				writefds.fd_array[i], "w", 0 );
 		}
 
-#else
+#else /* ! HAVE_WINSOCK */
 		nrfds = 0;
 		nwfds = 0;
 		for ( i = 0; i < nfds; i++ ) {
@@ -1957,19 +2289,18 @@
 			}
 			if ( ns <= 0 ) break;
 		}
-#endif
+#endif /* ! HAVE_WINSOCK */
 		Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
 
-
 		/* loop through the writers */
 		for ( i = 0; nwfds > 0; i++ ) {
 			ber_socket_t wd;
 #ifdef HAVE_WINSOCK
 			wd = writefds.fd_array[i];
-#else
-			if( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
+#else /* ! HAVE_WINSOCK */
+			if ( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
 			wd = i;
-#endif
+#endif /* ! HAVE_WINSOCK */
 
 			SLAP_EVENT_CLR_WRITE( wd );
 			nwfds--;
@@ -1978,9 +2309,6 @@
 				"daemon: write active on %d\n",
 				wd, 0, 0 );
 
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
-			connection_write_activate( wd );
-#else
 			/*
 			 * NOTE: it is possible that the connection was closed
 			 * and that the stream is now inactive.
@@ -1991,22 +2319,21 @@
 			 * close it here. It has already been closed in connection.c.
 			 */
 			if ( connection_write( wd ) < 0 ) {
-				if ( SLAP_EVENT_IS_READ( wd )) {
+				if ( SLAP_EVENT_IS_READ( wd ) ) {
 					SLAP_EVENT_CLR_READ( (unsigned) wd );
 					nrfds--;
 				}
 			}
-#endif
 		}
 
 		for ( i = 0; nrfds > 0; i++ ) {
 			ber_socket_t rd;
 #ifdef HAVE_WINSOCK
 			rd = readfds.fd_array[i];
-#else
-			if( ! SLAP_EVENT_IS_READ( i ) ) continue;
+#else /* ! HAVE_WINSOCK */
+			if ( ! SLAP_EVENT_IS_READ( i ) ) continue;
 			rd = i;
-#endif
+#endif /* ! HAVE_WINSOCK */
 			SLAP_EVENT_CLR_READ( rd );
 			nrfds--;
 
@@ -2021,9 +2348,9 @@
 
 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
 			connection_read_activate( rd );
-#else
+#else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
 			connection_read( rd );
-#endif
+#endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
 		}
 #else	/* !SLAP_EVENTS_ARE_INDEXED */
 	/* FIXME */
@@ -2045,14 +2372,14 @@
 #ifdef LDAP_DEBUG
 		Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
 
-		for (i=0; i<ns; i++) {
+		for ( i = 0; i < ns; i++ ) {
 			int	r, w;
 
 			/* Don't log listener events */
-			if ( SLAP_EVENT_IS_LISTENER(i)
+			if ( SLAP_EVENT_IS_LISTENER( i )
 #ifdef LDAP_CONNECTIONLESS
-				&& !((SLAP_EVENT_LISTENER(i))->sl_is_udp)
-#endif
+				&& !( (SLAP_EVENT_LISTENER( i ))->sl_is_udp )
+#endif /* LDAP_CONNECTIONLESS */
 				)
 			{
 				continue;
@@ -2069,17 +2396,17 @@
 			}
 		}
 		Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
-#endif
+#endif /* LDAP_DEBUG */
 
-		for (i=0; i<ns; i++) {
-			int rc = 1, fd, waswrite = 0;
+		for ( i = 0; i < ns; i++ ) {
+			int rc = 1, fd;
 
-			if ( SLAP_EVENT_IS_LISTENER(i) ) {
+			if ( SLAP_EVENT_IS_LISTENER( i ) ) {
 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
-				rc = slap_listener_activate( SLAP_EVENT_LISTENER( i ));
-#else
-				rc = slap_listener( SLAP_EVENT_LISTENER( i ));
-#endif
+				rc = slap_listener_activate( SLAP_EVENT_LISTENER( i ) );
+#else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
+				rc = slap_listener( SLAP_EVENT_LISTENER( i ) );
+#endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
 			}
 
 			/* If we found a regular listener, rc is now zero, and we
@@ -2097,16 +2424,13 @@
 					break;
 				}
 
-				if( SLAP_EVENT_IS_WRITE( i ) ) {
+				if ( SLAP_EVENT_IS_WRITE( i ) ) {
 					Debug( LDAP_DEBUG_CONNS,
 						"daemon: write active on %d\n",
 						fd, 0, 0 );
 
-					waswrite = 1;
+					SLAP_EVENT_CLR_WRITE( i );
 
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
-					connection_write_activate( fd );
-#else
 					/*
 					 * NOTE: it is possible that the connection was closed
 					 * and that the stream is now inactive.
@@ -2116,17 +2440,17 @@
 					if ( connection_write( fd ) < 0 ) {
 						continue;
 					}
-#endif
 				}
-				/* If event is a read or an error */
-				if( SLAP_EVENT_IS_READ( i ) || !waswrite ) {
+				/* If event is a read */
+				if ( SLAP_EVENT_IS_READ( i ) ) {
 					Debug( LDAP_DEBUG_CONNS,
 						"daemon: read active on %d\n",
 						fd, 0, 0 );
 
+					SLAP_EVENT_CLR_READ( i );
 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
 					connection_read_activate( fd );
-#else
+#else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
 					/*
 					 * NOTE: it is possible that the connection was closed
 					 * and that the stream is now inactive.
@@ -2134,7 +2458,10 @@
 					 * active.
 					 */
 					connection_read( fd );
-#endif
+#endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
+				} else {
+					Debug( LDAP_DEBUG_CONNS,
+						"daemon: hangup on %d\n", fd, 0, 0 );
 				}
 			}
 		}
@@ -2142,10 +2469,10 @@
 
 #ifndef HAVE_YIELDING_SELECT
 		ldap_pvt_thread_yield();
-#endif
+#endif /* ! HAVE_YIELDING_SELECT */
 	}
 
-	if( slapd_shutdown == 1 ) {
+	if ( slapd_shutdown == 1 ) {
 		Debug( LDAP_DEBUG_ANY,
 			"daemon: shutdown requested and initiated.\n",
 			0, 0, 0 );
@@ -2166,19 +2493,19 @@
 		       0, 0, 0 );
 	}
 
-	if( slapd_gentle_shutdown != 2 ) close_listeners ( 0 );
+	if ( slapd_gentle_shutdown != 2 ) close_listeners ( 0 );
 
-	if( !slapd_gentle_shutdown ) {
+	if ( !slapd_gentle_shutdown ) {
 		slapd_abrupt_shutdown = 1;
 		connections_shutdown();
 	}
 
 	Debug( LDAP_DEBUG_ANY,
-	    "slapd shutdown: waiting for %d threads to terminate\n",
-	    ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
-	ldap_pvt_thread_pool_destroy(&connection_pool, 1);
+		"slapd shutdown: waiting for %d threads to terminate\n",
+		ldap_pvt_thread_pool_backload( &connection_pool ), 0, 0 );
+	ldap_pvt_thread_pool_destroy( &connection_pool, 1 );
 
-	free ( slap_listeners );
+	free( slap_listeners );
 	slap_listeners = NULL;
 
 	return NULL;
@@ -2186,7 +2513,8 @@
 
 
 #ifdef LDAP_CONNECTIONLESS
-static int connectionless_init(void)
+static int
+connectionless_init( void )
 {
 	int l;
 
@@ -2194,15 +2522,17 @@
 		Listener *lr = slap_listeners[l];
 		long id;
 
-		if( !lr->sl_is_udp ) {
+		if ( !lr->sl_is_udp ) {
 			continue;
 		}
 
-		id = connection_init( lr->sl_sd, lr, "", "", CONN_IS_UDP, (slap_ssf_t) 0, NULL );
+		id = connection_init( lr->sl_sd, lr, "", "",
+			CONN_IS_UDP, (slap_ssf_t) 0, NULL );
 
-		if( id < 0 ) {
+		if ( id < 0 ) {
 			Debug( LDAP_DEBUG_TRACE,
-				"connectionless_init: failed on %s (%d)\n", lr->sl_url, lr->sl_sd, 0 );
+				"connectionless_init: failed on %s (%d)\n",
+				lr->sl_url, lr->sl_sd, 0 );
 			return -1;
 		}
 		lr->sl_is_udp++;
@@ -2212,14 +2542,15 @@
 }
 #endif /* LDAP_CONNECTIONLESS */
 
-int slapd_daemon( void )
+int
+slapd_daemon( void )
 {
 	int rc;
 
 	connections_init();
 #ifdef LDAP_CONNECTIONLESS
 	connectionless_init();
-#endif
+#endif /* LDAP_CONNECTIONLESS */
 
 #define SLAPD_LISTENER_THREAD 1
 #if defined( SLAPD_LISTENER_THREAD )
@@ -2237,21 +2568,21 @@
 		}
  
   		/* wait for the listener thread to complete */
-  		ldap_pvt_thread_join( listener_tid, (void *) NULL );
+  		ldap_pvt_thread_join( listener_tid, (void *)NULL );
 	}
-#else
+#else /* ! SLAPD_LISTENER_THREAD */
 	/* experimental code */
 	slapd_daemon_task( NULL );
-#endif
+#endif /* ! SLAPD_LISTENER_THREAD */
 
 	return 0;
-
 }
 
-static int sockinit(void)
+static int
+sockinit( void )
 {
 #if defined( HAVE_WINSOCK2 )
-    WORD wVersionRequested;
+	WORD wVersionRequested;
 	WSADATA wsaData;
 	int err;
 
@@ -2283,16 +2614,19 @@
 #elif defined( HAVE_WINSOCK )
 	WSADATA wsaData;
 	if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1;
-#endif
+#endif /* ! HAVE_WINSOCK2 && ! HAVE_WINSOCK */
 
 	return 0;
 }
 
-static int sockdestroy(void)
+static int
+sockdestroy( void )
 {
 #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
 	WSACleanup();
-#endif
+#endif /* HAVE_WINSOCK2 || HAVE_WINSOCK */
+	SLAP_SOCK_DESTROY;
+
 	return 0;
 }
 
@@ -2313,12 +2647,12 @@
 	if (is_NT_Service && sig == SIGBREAK) {
 		/* empty */;
 	} else
-#endif
+#endif /* HAVE_NT_SERVICE_MANAGER && SIGBREAK */
 #ifdef SIGHUP
 	if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) {
 		slapd_gentle_shutdown = 1;
 	} else
-#endif
+#endif /* SIGHUP */
 	{
 		slapd_shutdown = 1;
 	}
@@ -2339,14 +2673,20 @@
 }
 
 
-void slapd_add_internal(ber_socket_t s, int isactive) {
-	slapd_add(s, isactive, NULL);
+void
+slapd_add_internal( ber_socket_t s, int isactive )
+{
+	slapd_add( s, isactive, NULL );
 }
 
-Listener ** slapd_get_listeners(void) {
+Listener **
+slapd_get_listeners( void )
+{
 	return slap_listeners;
 }
 
-void slap_wake_listener() {
+void
+slap_wake_listener()
+{
 	WAKE_LISTENER(1);
 }

Modified: openldap/vendor/openldap-release/servers/slapd/operation.c
===================================================================
--- openldap/vendor/openldap-release/servers/slapd/operation.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/servers/slapd/operation.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,5 @@
 /* operation.c - routines to deal with pending ldap operations */
-/* $OpenLDAP: pkg/ldap/servers/slapd/operation.c,v 1.63.2.6 2006/01/03 22:16:15 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/servers/slapd/operation.c,v 1.63.2.7 2006/11/07 04:25:01 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -166,3 +166,32 @@
 
 	return( op );
 }
+
+slap_op_t
+slap_req2op( ber_tag_t tag )
+{
+	switch ( tag ) {
+	case LDAP_REQ_BIND:
+		return SLAP_OP_BIND;
+	case LDAP_REQ_UNBIND:
+		return SLAP_OP_UNBIND;
+	case LDAP_REQ_ADD:
+		return SLAP_OP_ADD;
+	case LDAP_REQ_DELETE:
+		return SLAP_OP_DELETE;
+	case LDAP_REQ_MODRDN:
+		return SLAP_OP_MODRDN;
+	case LDAP_REQ_MODIFY:
+		return SLAP_OP_MODIFY;
+	case LDAP_REQ_COMPARE:
+		return SLAP_OP_COMPARE;
+	case LDAP_REQ_SEARCH:
+		return SLAP_OP_SEARCH;
+	case LDAP_REQ_ABANDON:
+		return SLAP_OP_ABANDON;
+	case LDAP_REQ_EXTENDED:
+		return SLAP_OP_EXTENDED;
+	}
+
+	return SLAP_OP_LAST;
+}

Modified: openldap/vendor/openldap-release/servers/slapd/overlays/accesslog.c
===================================================================
--- openldap/vendor/openldap-release/servers/slapd/overlays/accesslog.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/servers/slapd/overlays/accesslog.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,5 @@
 /* accesslog.c - log operations for audit/history purposes */
-/* $OpenLDAP: pkg/ldap/servers/slapd/overlays/accesslog.c,v 1.2.2.18 2006/10/07 19:45:08 hyc Exp $ */
+/* $OpenLDAP: pkg/ldap/servers/slapd/overlays/accesslog.c,v 1.2.2.21 2006/11/07 03:03:12 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 2005-2006 The OpenLDAP Foundation.
@@ -484,6 +484,7 @@
 	int used;
 	BerVarray dn;
 	BerVarray ndn;
+	struct berval csn;	/* an arbitrary old CSN */
 } purge_data;
 
 static int
@@ -495,6 +496,18 @@
 
 	if ( slapd_shutdown ) return 0;
 
+	/* Remember old CSN */
+	if ( pd->csn.bv_val[0] == '\0' ) {
+		Attribute *a = attr_find( rs->sr_entry->e_attrs,
+			slap_schema.si_ad_entryCSN );
+		if ( a ) {
+			int len = a->a_vals[0].bv_len;
+			if ( len > pd->csn.bv_len )
+				len = pd->csn.bv_len;
+			AC_MEMCPY( pd->csn.bv_val, a->a_vals[0].bv_val, len );
+			pd->csn.bv_len = len;
+		}
+	}
 	if ( pd->used >= pd->slots ) {
 		pd->slots += PURGE_INCREMENT;
 		pd->dn = ch_realloc( pd->dn, pd->slots * sizeof( struct berval ));
@@ -522,6 +535,7 @@
 	AttributeAssertion ava = {0};
 	purge_data pd = {0};
 	char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
+	char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
 	time_t old = slap_get_time();
 
 	connection_fake_init( &conn, op, ctx );
@@ -553,6 +567,9 @@
 	op->ors_attrs = slap_anlist_no_attrs;
 	op->ors_attrsonly = 1;
 	
+	pd.csn.bv_len = sizeof( csnbuf );
+	pd.csn.bv_val = csnbuf;
+	csnbuf[0] = '\0';
 	cb.sc_private = &pd;
 
 	op->o_bd->be_search( op, &rs );
@@ -563,6 +580,7 @@
 
 		op->o_tag = LDAP_REQ_DELETE;
 		op->o_callback = &nullsc;
+		op->o_csn = pd.csn;
 
 		for (i=0; i<pd.used; i++) {
 			op->o_req_dn = pd.dn[i];
@@ -966,7 +984,8 @@
 				for (b=m->sml_values; !BER_BVISNULL( b ); b++) {
 					i++;
 				}
-			} else if ( m->sml_op == LDAP_MOD_DELETE ) {
+			} else if ( m->sml_op == LDAP_MOD_DELETE ||
+				m->sml_op == LDAP_MOD_REPLACE ) {
 				i++;
 			}
 		}
@@ -1004,13 +1023,17 @@
 					}
 					accesslog_val2val( m->sml_desc, b, c_op, &vals[i] );
 				}
-			} else if ( m->sml_op == LDAP_MOD_DELETE ) {
+			} else if ( m->sml_op == LDAP_MOD_DELETE ||
+				m->sml_op == LDAP_MOD_REPLACE ) {
 				vals[i].bv_len = m->sml_desc->ad_cname.bv_len + 2;
 				vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
 				ptr = lutil_strcopy( vals[i].bv_val,
 					m->sml_desc->ad_cname.bv_val );
 				*ptr++ = ':';
-				*ptr++ = '-';
+				if ( m->sml_op == LDAP_MOD_DELETE )
+					*ptr++ = '-';
+				else
+					*ptr++ = '=';
 				*ptr = '\0';
 				i++;
 			}

Modified: openldap/vendor/openldap-release/servers/slapd/overlays/dynlist.c
===================================================================
--- openldap/vendor/openldap-release/servers/slapd/overlays/dynlist.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/servers/slapd/overlays/dynlist.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,4 +1,5 @@
 /* dynlist.c - dynamic list overlay */
+/* $OpenLDAP: pkg/ldap/servers/slapd/overlays/dynlist.c,v 1.5.2.8 2006/11/03 07:36:35 ando Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 2003-2006 The OpenLDAP Foundation.
@@ -22,11 +23,15 @@
 
 #ifdef SLAPD_OVER_DYNLIST
 
-#if LDAP_VENDOR_VERSION_MINOR != X && LDAP_VENDOR_VERSION_MINOR < 3
-#define OL_2_2_COMPAT
-#elif defined(LDAP_DEVEL) && SLAPD_OVER_DYNGROUP != SLAPD_MOD_STATIC
+#if LDAP_VENDOR_VERSION_MINOR == X || LDAP_VENDOR_VERSION_MINOR > 3
+#if SLAPD_OVER_DYNGROUP != SLAPD_MOD_STATIC
 #define TAKEOVER_DYNGROUP
 #endif
+#else
+#if LDAP_VENDOR_VERSION_MINOR < 3
+#define OL_2_2_COMPAT
+#endif
+#endif
 
 #include <stdio.h>
 
@@ -350,7 +355,7 @@
 			continue;
 		}
 
-		if ( lud->lud_host ) {
+		if ( lud->lud_host != NULL ) {
 			/* FIXME: host not allowed; reject as illegal? */
 			Debug( LDAP_DEBUG_ANY, "dynlist_send_entry(\"%s\"): "
 				"illegal URI \"%s\"\n",
@@ -365,6 +370,7 @@
 			 * this can be useful in case of a database serving
 			 * the empty suffix */
 			BER_BVSTR( &dn, "" );
+
 		} else {
 			ber_str2bv( lud->lud_dn, 0, 0, &dn );
 		}
@@ -435,6 +441,7 @@
 		if ( lud->lud_filter == NULL ) {
 			ber_dupbv_x( &o.ors_filterstr,
 					&dli->dli_default_filter, op->o_tmpmemctx );
+
 		} else {
 			struct berval	flt;
 			ber_str2bv( lud->lud_filter, 0, 0, &flt );
@@ -471,13 +478,9 @@
 		if ( !BER_BVISNULL( &o.o_req_ndn ) ) {
 			op->o_tmpfree( o.o_req_ndn.bv_val, op->o_tmpmemctx );
 		}
-		if ( o.ors_filterstr.bv_val != lud->lud_filter ) {
-			op->o_tmpfree( o.ors_filterstr.bv_val, op->o_tmpmemctx );
-			lud->lud_filter = NULL;
-		}
-		if ( lud ) {
-			ldap_free_urldesc( lud );
-		}
+		assert( o.ors_filterstr.bv_val != lud->lud_filter );
+		op->o_tmpfree( o.ors_filterstr.bv_val, op->o_tmpmemctx );
+		ldap_free_urldesc( lud );
 	}
 
 	rs->sr_entry = e;
@@ -696,7 +699,7 @@
 
 	dli->dli_default_filter.bv_len = STRLENOF( "(!(objectClass=" "))" )
 		+ dli->dli_oc->soc_cname.bv_len;
-	dli->dli_default_filter.bv_val = SLAP_MALLOC( dli->dli_default_filter.bv_len + 1 );
+	dli->dli_default_filter.bv_val = ch_malloc( dli->dli_default_filter.bv_len + 1 );
 	if ( dli->dli_default_filter.bv_val == NULL ) {
 		Debug( LDAP_DEBUG_ANY, "dynlist_db_open: malloc failed.\n",
 			0, 0, 0 );
@@ -1345,9 +1348,11 @@
 			dli->dli_ad = ad;			
 		}
 
-		rc = dynlist_build_def_filter( dli );
-		if ( rc != 0 ) {
-			return rc;
+		if ( BER_BVISNULL( &dli->dli_default_filter ) ) {
+			rc = dynlist_build_def_filter( dli );
+			if ( rc != 0 ) {
+				return rc;
+			}
 		}
 	}
 

Modified: openldap/vendor/openldap-release/servers/slapd/overlays/ppolicy.c
===================================================================
--- openldap/vendor/openldap-release/servers/slapd/overlays/ppolicy.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/servers/slapd/overlays/ppolicy.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,4 +1,4 @@
-/* $OpenLDAP: pkg/ldap/servers/slapd/overlays/ppolicy.c,v 1.31.2.25 2006/10/06 16:54:22 hyc Exp $ */
+/* $OpenLDAP: pkg/ldap/servers/slapd/overlays/ppolicy.c,v 1.31.2.27 2006/11/10 06:47:51 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 2004-2006 The OpenLDAP Foundation.
@@ -261,7 +261,7 @@
 
 /* IMPLICIT TAGS, all context-specific */
 #define PPOLICY_WARNING 0xa0L	/* constructed + 0 */
-#define PPOLICY_ERROR 0x81L	/* primitive + 1 */
+#define PPOLICY_ERROR 0x81L		/* primitive + 1 */
  
 #define PPOLICY_EXPIRE 0x80L	/* primitive + 0 */
 #define PPOLICY_GRACE  0x81L	/* primitive + 1 */
@@ -274,25 +274,29 @@
 	LDAPControl *c;
 	struct berval bv;
 
-	if ((c = ch_calloc( sizeof( LDAPControl ), 1 )) == NULL) return NULL;
+	c = ch_calloc( sizeof( LDAPControl ), 1 );
+	if ( c == NULL ) {
+		return NULL;
+	}
 	c->ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYRESPONSE;
 	c->ldctl_iscritical = 0;
-	c->ldctl_value.bv_val = NULL;
-	c->ldctl_value.bv_len = 0;
+	BER_BVZERO( &c->ldctl_value );
 
 	ber_init2( ber, NULL, LBER_USE_DER );
-	ber_printf(ber, "{" /*}*/ );
+	ber_printf( ber, "{" /*}*/ );
 
-	if (exptime >= 0) {
+	if ( exptime >= 0 ) {
 		ber_init2( b2, NULL, LBER_USE_DER );
 		ber_printf( b2, "ti", PPOLICY_EXPIRE, exptime );
 		ber_flatten2( b2, &bv, 1 );
+		(void)ber_free_buf(b2);
 		ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
 		ch_free( bv.bv_val );
-	} else if (grace > 0) {
+	} else if ( grace > 0 ) {
 		ber_init2( b2, NULL, LBER_USE_DER );
 		ber_printf( b2, "ti", PPOLICY_GRACE, grace );
 		ber_flatten2( b2, &bv, 1 );
+		(void)ber_free_buf(b2);
 		ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
 		ch_free( bv.bv_val );
 	}
@@ -304,13 +308,41 @@
 
 	if (ber_flatten2( ber, &(c->ldctl_value), 1 ) == LBER_DEFAULT) {
 		ch_free(c);
-		(void)ber_free_buf(ber);
-		return NULL;
+		c = NULL;
 	}
 	(void)ber_free_buf(ber);
 	return c;
 }
 
+static LDAPControl **
+add_passcontrol( Operation *op, SlapReply *rs, LDAPControl *ctrl )
+{
+	LDAPControl **ctrls, **oldctrls = rs->sr_ctrls;
+	int n;
+
+	n = 0;
+	if ( oldctrls ) {
+		for ( ; oldctrls[n]; n++ )
+			;
+	}
+	n += 2;
+
+	ctrls = op->o_tmpcalloc( sizeof( LDAPControl * ), n, op->o_tmpmemctx );
+
+	n = 0;
+	if ( oldctrls ) {
+		for ( ; oldctrls[n]; n++ ) {
+			ctrls[n] = oldctrls[n];
+		}
+	}
+	ctrls[n] = ctrl;
+	ctrls[n+1] = NULL;
+
+	rs->sr_ctrls = ctrls;
+
+	return oldctrls;
+}
+
 static void
 ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
 {
@@ -396,13 +428,13 @@
 	}
 
 	if ((a = attr_find( pe->e_attrs, ad_pwdLockout )))
-    	pp->pwdLockout = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
+    		pp->pwdLockout = bvmatch( &a->a_nvals[0], &slap_true_bv );
 	if ((a = attr_find( pe->e_attrs, ad_pwdMustChange )))
-    	pp->pwdMustChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
+    		pp->pwdMustChange = bvmatch( &a->a_nvals[0], &slap_true_bv );
 	if ((a = attr_find( pe->e_attrs, ad_pwdAllowUserChange )))
-    	pp->pwdAllowUserChange = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
+	    	pp->pwdAllowUserChange = bvmatch( &a->a_nvals[0], &slap_true_bv );
 	if ((a = attr_find( pe->e_attrs, ad_pwdSafeModify )))
-    	pp->pwdSafeModify = !strcmp( a->a_nvals[0].bv_val, "TRUE" );
+	    	pp->pwdSafeModify = bvmatch( &a->a_nvals[0], &slap_true_bv );
     
 	op->o_bd->bd_info = (BackendInfo *)on->on_info;
 	be_entry_release_r( op, pe );
@@ -521,13 +553,12 @@
 				ldap_pvt_thread_mutex_lock( &chk_syntax_mutex );
 				ok = prog( cred->bv_val, &txt, e );
 				ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex );
-				if (txt) {
+				if (ok != LDAP_SUCCESS) {
 					Debug(LDAP_DEBUG_ANY,
 						"check_password_quality: module error: (%s) %s.[%d]\n",
-						pp->pwdCheckModule, txt, ok );
+						pp->pwdCheckModule, txt ? txt : "", ok );
 					free(txt);
-				} else
-					ok = LDAP_SUCCESS;
+				}
 			}
 			    
 			lt_dlclose( mod );
@@ -556,50 +587,75 @@
 	
 	assert (bv && (bv->bv_len > 0) && (bv->bv_val) && oldtime && oldpw );
 
-	if ( oid ) *oid = 0;
+	if ( oid ) {
+		*oid = 0;
+	}
 	*oldtime = (time_t)-1;
-	oldpw->bv_val = NULL;
-	oldpw->bv_len = 0;
+	BER_BVZERO( oldpw );
 	
 	ber_dupbv( &nv, bv );
 
 	/* first get the time field */
-	for(i=0; (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
-	if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
+	for ( i = 0; (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++ )
+		;
+	if ( i == nv.bv_len ) {
+		goto exit_failure; /* couldn't locate the '#' separator */
+	}
 	nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
 	ptr = nv.bv_val;
 	*oldtime = parse_time( ptr );
-	if (*oldtime == (time_t)-1) goto exit_failure;
+	if (*oldtime == (time_t)-1) {
+		goto exit_failure;
+	}
 
 	/* get the OID field */
-	for(ptr = &(nv.bv_val[i]);(i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
-	if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
+	for (ptr = &(nv.bv_val[i]); (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++ )
+		;
+	if ( i == nv.bv_len ) {
+		goto exit_failure; /* couldn't locate the '#' separator */
+	}
 	nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
-	if ( oid ) *oid = ber_strdup( ptr );
+	if ( oid ) {
+		*oid = ber_strdup( ptr );
+	}
 	
 	/* get the length field */
-	for(ptr = &(nv.bv_val[i]);(i < nv.bv_len) && (nv.bv_val[i] != '#'); i++);
-	if ( i == nv.bv_len) goto exit_failure; /* couldn't locate the '#' separator */
+	for ( ptr = &(nv.bv_val[i]); (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++ )
+		;
+	if ( i == nv.bv_len ) {
+		goto exit_failure; /* couldn't locate the '#' separator */
+	}
 	nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
 	oldpw->bv_len = strtol( ptr, NULL, 10 );
-	if (errno == ERANGE) goto exit_failure;
+	if (errno == ERANGE) {
+		goto exit_failure;
+	}
 
 	/* lastly, get the octets of the string */
-	for(j=i, ptr = &(nv.bv_val[i]);i < nv.bv_len; i++);
-	if (i-j != oldpw->bv_len) goto exit_failure; /* length is wrong */
+	for ( j = i, ptr = &(nv.bv_val[i]); i < nv.bv_len; i++ )
+		;
+	if ( i - j != oldpw->bv_len) {
+		goto exit_failure; /* length is wrong */
+	}
 
 	npw.bv_val = ptr;
 	npw.bv_len = oldpw->bv_len;
 	ber_dupbv( oldpw, &npw );
+	ber_memfree( nv.bv_val );
 	
 	return LDAP_SUCCESS;
-exit_failure:
-	if (oid && *oid) { ber_memfree(*oid); *oid = NULL; }
-	if (oldpw->bv_val) {
-		ber_memfree( oldpw->bv_val); oldpw->bv_val = NULL;
-		oldpw->bv_len = 0;
+
+exit_failure:;
+	if ( oid && *oid ) {
+		ber_memfree(*oid);
+		*oid = NULL;
 	}
-	ber_memfree(nv.bv_val);
+	if ( oldpw->bv_val ) {
+		ber_memfree( oldpw->bv_val);
+		BER_BVZERO( oldpw );
+	}
+	ber_memfree( nv.bv_val );
+
 	return LDAP_OTHER;
 }
 
@@ -687,15 +743,52 @@
 typedef struct ppbind {
 	slap_overinst *on;
 	int send_ctrl;
+	LDAPControl **oldctrls;
 	Modifications *mod;
 	LDAPPasswordPolicyError pErr;
 	PassPolicy pp;
 } ppbind;
 
+static void
+ctrls_cleanup( Operation *op, SlapReply *rs, LDAPControl **oldctrls )
+{
+	int n;
+
+	assert( rs->sr_ctrls != NULL );
+	assert( rs->sr_ctrls[0] != NULL );
+
+	for ( n = 0; rs->sr_ctrls[n]; n++ ) {
+		if ( rs->sr_ctrls[n]->ldctl_oid == LDAP_CONTROL_PASSWORDPOLICYRESPONSE ) {
+			ch_free( rs->sr_ctrls[n]->ldctl_value.bv_val );
+			ch_free( rs->sr_ctrls[n] );
+			rs->sr_ctrls[n] = (LDAPControl *)(-1);
+			break;
+		}
+	}
+
+	if ( rs->sr_ctrls[n] == NULL ) {
+		/* missed? */
+	}
+
+	op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
+
+	rs->sr_ctrls = oldctrls;
+}
+
 static int
-ppolicy_bind_resp( Operation *op, SlapReply *rs )
+ppolicy_ctrls_cleanup( Operation *op, SlapReply *rs )
 {
 	ppbind *ppb = op->o_callback->sc_private;
+	if ( ppb->send_ctrl ) {
+		ctrls_cleanup( op, rs, ppb->oldctrls );
+	}
+	return SLAP_CB_CONTINUE;
+}
+
+static int
+ppolicy_bind_response( Operation *op, SlapReply *rs )
+{
+	ppbind *ppb = op->o_callback->sc_private;
 	slap_overinst *on = ppb->on;
 	Modifications *mod = ppb->mod, *m;
 	int pwExpired = 0;
@@ -809,7 +902,8 @@
 		 */
 		if ( ppb->pp.pwdMustChange &&
 			(a = attr_find( e->e_attrs, ad_pwdReset )) &&
-			!strcmp( a->a_nvals[0].bv_val, "TRUE" ) ) {
+			bvmatch( &a->a_nvals[0], &slap_true_bv ) )
+		{
 			/*
 			 * need to inject client controls here to give
 			 * more information. For the moment, we ensure
@@ -939,17 +1033,16 @@
 	}
 
 	if ( ppb->send_ctrl ) {
-		LDAPControl **ctrls = NULL;
+		LDAPControl *ctrl = NULL;
 		pp_info *pi = on->on_bi.bi_private;
 
 		/* Do we really want to tell that the account is locked? */
 		if ( ppb->pErr == PP_accountLocked && !pi->use_lockout ) {
 			ppb->pErr = PP_noError;
 		}
-		ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
-		ctrls[0] = create_passcontrol( warn, ngut, ppb->pErr );
-		ctrls[1] = NULL;
-		rs->sr_ctrls = ctrls;
+		ctrl = create_passcontrol( warn, ngut, ppb->pErr );
+		ppb->oldctrls = add_passcontrol( op, rs, ctrl );
+		op->o_callback->sc_cleanup = ppolicy_ctrls_cleanup;
 	}
 	op->o_bd->bd_info = bi;
 	return SLAP_CB_CONTINUE;
@@ -988,7 +1081,7 @@
 
 		/* Setup a callback so we can munge the result */
 
-		cb->sc_response = ppolicy_bind_resp;
+		cb->sc_response = ppolicy_bind_response;
 		cb->sc_next = op->o_callback->sc_next;
 		cb->sc_private = ppb;
 		op->o_callback->sc_next = cb;
@@ -1044,6 +1137,7 @@
 	}
 
 	if ( op->o_conn && !BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) {
+		LDAPControl **oldctrls;
 		/* if the current authcDN doesn't match the one we recorded,
 		 * then an intervening Bind has succeeded and the restriction
 		 * no longer applies. (ITS#4516)
@@ -1058,16 +1152,16 @@
 		Debug( LDAP_DEBUG_TRACE,
 			"connection restricted to password changing only\n", 0, 0, 0);
 		if ( send_ctrl ) {
-			LDAPControl **ctrls = NULL;
-
-			ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
-			ctrls[0] = create_passcontrol( -1, -1, PP_changeAfterReset );
-			ctrls[1] = NULL;
-			rs->sr_ctrls = ctrls;
+			LDAPControl *ctrl = NULL;
+			ctrl = create_passcontrol( -1, -1, PP_changeAfterReset );
+			oldctrls = add_passcontrol( op, rs, ctrl );
 		}
 		op->o_bd->bd_info = (BackendInfo *)on->on_info;
 		send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, 
 			"Operations are restricted to bind/unbind/abandon/StartTLS/modify password" );
+		if ( send_ctrl ) {
+			ctrls_cleanup( op, rs, oldctrls );
+		}
 		return rs->sr_err;
 	}
 
@@ -1096,6 +1190,14 @@
 	if ((pa = attr_find( op->oq_add.rs_e->e_attrs,
 		slap_schema.si_ad_userPassword )))
 	{
+		assert( pa->a_vals );
+		assert( !BER_BVISNULL( &pa->a_vals[ 0 ] ) );
+
+		if ( !BER_BVISNULL( &pa->a_vals[ 1 ] ) ) {
+			send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION, "Password policy only allows one password value" );
+			return rs->sr_err;
+		}
+
 		/*
 		 * new entry contains a password - if we're not the root user
 		 * then we need to check that the password fits in with the
@@ -1113,16 +1215,17 @@
 			}
 			rc = check_password_quality( bv, &pp, &pErr, op->ora_e );
 			if (rc != LDAP_SUCCESS) {
+				LDAPControl **oldctrls = NULL;
 				op->o_bd->bd_info = (BackendInfo *)on->on_info;
 				if ( send_ctrl ) {
-					LDAPControl **ctrls = NULL;
-
-					ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
-					ctrls[0] = create_passcontrol( -1, -1, pErr );
-					ctrls[1] = NULL;
-					rs->sr_ctrls = ctrls;
+					LDAPControl *ctrl = NULL;
+					ctrl = create_passcontrol( -1, -1, pErr );
+					oldctrls = add_passcontrol( op, rs, ctrl );
 				}
 				send_ldap_error( op, rs, rc, "Password fails quality checking policy" );
+				if ( send_ctrl ) {
+					ctrls_cleanup( op, rs, oldctrls );
+				}
 				return rs->sr_err;
 			}
 		}
@@ -1205,6 +1308,7 @@
 	struct berval		newpw = BER_BVNULL, oldpw = BER_BVNULL,
 				*bv, cr[2];
 	LDAPPasswordPolicyError pErr = PP_noError;
+	LDAPControl 		**oldctrls = NULL;
 
 	op->o_bd->bd_info = (BackendInfo *)on->on_info;
 	rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
@@ -1224,7 +1328,7 @@
 		a_lock = attr_find( e->e_attrs, ad_pwdAccountLockedTime );
 		a_fail = attr_find( e->e_attrs, ad_pwdFailureTime );
 
-		for( prev = &op->oq_modify.rs_modlist, ml = *prev; ml; ml = *prev ) {
+		for( prev = &op->orm_modlist, ml = *prev; ml; ml = *prev ) {
 
 			if ( ml->sml_desc == slap_schema.si_ad_userPassword )
 				got_pw = 1;
@@ -1328,7 +1432,7 @@
 
 	ppolicy_get( op, e, &pp );
 
-	for ( ml = op->oq_modify.rs_modlist,
+	for ( ml = op->orm_modlist,
 			pwmod = 0, mod_pw_only = 1,
 			deladd = 0, delmod = NULL,
 			addmod = NULL,
@@ -1339,19 +1443,44 @@
 			pwmod = 1;
 			pwmop = ml->sml_op;
 			if ((deladd == 0) && (ml->sml_op == LDAP_MOD_DELETE) &&
-				(ml->sml_values) && (ml->sml_values[0].bv_val != NULL)) {
+				(ml->sml_values) && !BER_BVISNULL( &ml->sml_values[0] ))
+			{
 				deladd = 1;
 				delmod = ml;
 			}
 
 			if ((deladd == 1) && ((ml->sml_op == LDAP_MOD_ADD) ||
-								  (ml->sml_op == LDAP_MOD_REPLACE)))
+				  (ml->sml_op == LDAP_MOD_REPLACE)))
+			{
 				deladd = 2;
+			}
 
 			if ((ml->sml_op == LDAP_MOD_ADD) ||
 				(ml->sml_op == LDAP_MOD_REPLACE))
+			{
 				addmod = ml;
-		} else if (! is_at_operational( ml->sml_desc->ad_type )) {
+
+				/* FIXME: there's no easy way to ensure
+				 * that add does not cause multiple
+				 * userPassword values; one way (that 
+				 * would be consistent with the single
+				 * password constraint) would be to turn
+				 * add into replace); another would be
+				 * to disallow add.
+				 *
+				 * Let's check at least that a single value
+				 * is being added
+				 */
+				assert( addmod->sml_values != NULL );
+				assert( !BER_BVISNULL( &addmod->sml_values[ 0 ] ) );
+				if ( !BER_BVISNULL( &addmod->sml_values[ 1 ] ) ) {
+					rs->sr_err = LDAP_CONSTRAINT_VIOLATION; 
+					rs->sr_text = "Password policy only allows one password value";
+					goto return_results;
+				}
+			}
+
+		} else if ( !is_at_operational( ml->sml_desc->ad_type ) ) {
 			mod_pw_only = 0;
 			/* modifying something other than password */
 		}
@@ -1442,16 +1571,14 @@
 	 * let the matching value get found later
 	 */
 	if (pp.pwdSafeModify && oldpw.bv_val ) {
-		ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
+		ml = (Modifications *)ch_calloc( sizeof( Modifications ), 1 );
 		ml->sml_op = LDAP_MOD_DELETE;
 		ml->sml_flags = SLAP_MOD_INTERNAL;
 		ml->sml_desc = pp.ad;
 		ml->sml_type = pp.ad->ad_cname;
 		ml->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
 		ber_dupbv( &ml->sml_values[0], &oldpw );
-		ml->sml_values[1].bv_len = 0;
-		ml->sml_values[1].bv_val = NULL;
-		ml->sml_nvalues = NULL;
+		BER_BVZERO( &ml->sml_values[1] );
 		ml->sml_next = op->orm_modlist;
 		op->orm_modlist = ml;
 		delmod = ml;
@@ -1517,19 +1644,18 @@
 			 * replace the delete value with the (possibly hashed)
 			 * value which is currently in the password.
 			 */
-			for(i=0; delmod->sml_values[i].bv_val; i++) {
-				free(delmod->sml_values[i].bv_val);
-				delmod->sml_values[i].bv_len = 0;
+			for ( i = 0; !BER_BVISNULL( &delmod->sml_values[i] ); i++ ) {
+				free( delmod->sml_values[i].bv_val );
+				BER_BVZERO( &delmod->sml_values[i] );
 			}
-			free(delmod->sml_values);
+			free( delmod->sml_values );
 			delmod->sml_values = ch_calloc( sizeof(struct berval), 2 );
-			delmod->sml_values[1].bv_len = 0;
-			delmod->sml_values[1].bv_val = NULL;
-			ber_dupbv(&(delmod->sml_values[0]),  &(pa->a_nvals[0]));
+			BER_BVZERO( &delmod->sml_values[1] );
+			ber_dupbv( &(delmod->sml_values[0]),  &(pa->a_nvals[0]) );
 		}
 	}
 
-	bv = newpw.bv_val ? &newpw : addmod->sml_values;
+	bv = newpw.bv_val ? &newpw : &addmod->sml_values[0];
 	if (pp.pwdCheckQuality > 0) {
 
 		rc = check_password_quality( bv, &pp, &pErr, e );
@@ -1610,62 +1736,48 @@
 		timestamp.bv_len = sizeof(timebuf);
 		slap_timestamp( &now, &timestamp );
 
-		mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
-		mods->sml_type.bv_val = NULL;
+		mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
 		mods->sml_desc = ad_pwdChangedTime;
 		if (pwmop != LDAP_MOD_DELETE) {
 			mods->sml_op = LDAP_MOD_REPLACE;
 			mods->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
-			mods->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
 			ber_dupbv( &mods->sml_values[0], &timestamp );
-			ber_dupbv( &mods->sml_nvalues[0], &timestamp );
-			mods->sml_values[1].bv_len = 0;
-			mods->sml_values[1].bv_val = NULL;
-			assert( mods->sml_values[0].bv_val != NULL );
+			BER_BVZERO( &mods->sml_values[1] );
+			assert( !BER_BVISNULL( &mods->sml_values[0] ) );
+
 		} else {
 			mods->sml_op = LDAP_MOD_DELETE;
-			mods->sml_values = NULL;
 		}
 		mods->sml_flags = SLAP_MOD_INTERNAL;
-		mods->sml_nvalues = NULL;
 		mods->sml_next = NULL;
 		modtail->sml_next = mods;
 		modtail = mods;
 
 		if (attr_find(e->e_attrs, ad_pwdGraceUseTime )) {
-			mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
+			mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
 			mods->sml_op = LDAP_MOD_DELETE;
+			mods->sml_desc = ad_pwdGraceUseTime;
 			mods->sml_flags = SLAP_MOD_INTERNAL;
-			mods->sml_type.bv_val = NULL;
-			mods->sml_desc = ad_pwdGraceUseTime;
-			mods->sml_values = NULL;
-			mods->sml_nvalues = NULL;
 			mods->sml_next = NULL;
 			modtail->sml_next = mods;
 			modtail = mods;
 		}
 
 		if (attr_find(e->e_attrs, ad_pwdAccountLockedTime )) {
-			mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
+			mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
 			mods->sml_op = LDAP_MOD_DELETE;
+			mods->sml_desc = ad_pwdAccountLockedTime;
 			mods->sml_flags = SLAP_MOD_INTERNAL;
-			mods->sml_type.bv_val = NULL;
-			mods->sml_desc = ad_pwdAccountLockedTime;
-			mods->sml_values = NULL;
-			mods->sml_nvalues = NULL;
 			mods->sml_next = NULL;
 			modtail->sml_next = mods;
 			modtail = mods;
 		}
 
 		if (attr_find(e->e_attrs, ad_pwdFailureTime )) {
-			mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
+			mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
 			mods->sml_op = LDAP_MOD_DELETE;
+			mods->sml_desc = ad_pwdFailureTime;
 			mods->sml_flags = SLAP_MOD_INTERNAL;
-			mods->sml_type.bv_val = NULL;
-			mods->sml_desc = ad_pwdFailureTime;
-			mods->sml_values = NULL;
-			mods->sml_nvalues = NULL;
 			mods->sml_next = NULL;
 			modtail->sml_next = mods;
 			modtail = mods;
@@ -1673,13 +1785,10 @@
 
 		/* Delete the pwdReset attribute, since it's being reset */
 		if ((zapReset) && (attr_find(e->e_attrs, ad_pwdReset ))) {
-			mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
+			mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
 			mods->sml_op = LDAP_MOD_DELETE;
+			mods->sml_desc = ad_pwdReset;
 			mods->sml_flags = SLAP_MOD_INTERNAL;
-			mods->sml_type.bv_val = NULL;
-			mods->sml_desc = ad_pwdReset;
-			mods->sml_values = NULL;
-			mods->sml_nvalues = NULL;
 			mods->sml_next = NULL;
 			modtail->sml_next = mods;
 			modtail = mods;
@@ -1703,19 +1812,15 @@
 				 * This is easily evaluated, since the linked list is
 				 * created in ascending time order.
 				 */
-				mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
+				mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
 				mods->sml_op = LDAP_MOD_DELETE;
 				mods->sml_flags = SLAP_MOD_INTERNAL;
-				mods->sml_type.bv_val = NULL;
 				mods->sml_desc = ad_pwdHistory;
-				mods->sml_nvalues = NULL;
 				mods->sml_values = ch_calloc( sizeof( struct berval ),
-											   hsize - pp.pwdInHistory + 2 );
-				mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_val = NULL;
-				mods->sml_values[ hsize - pp.pwdInHistory + 1 ].bv_len = 0;
+					hsize - pp.pwdInHistory + 2 );
+				BER_BVZERO( &mods->sml_values[ hsize - pp.pwdInHistory + 1 ] );
 				for(i=0,p=tl; i < (hsize - pp.pwdInHistory + 1); i++, p=p->next) {
-					mods->sml_values[i].bv_val = NULL;
-					mods->sml_values[i].bv_len = 0;
+					BER_BVZERO( &mods->sml_values[i] );
 					ber_dupbv( &(mods->sml_values[i]), &p->bv );
 				}
 				mods->sml_next = NULL;
@@ -1749,6 +1854,7 @@
 				mods->sml_next = NULL;
 				modtail->sml_next = mods;
 				modtail = mods;
+
 			} else {
 				Debug( LDAP_DEBUG_TRACE,
 				"ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
@@ -1773,7 +1879,8 @@
 		 */
 
 		if ((pi->hash_passwords) && (addmod) && !newpw.bv_val && 
-			(password_scheme( &(addmod->sml_values[0]), NULL ) != LDAP_SUCCESS)) {
+			(password_scheme( &(addmod->sml_values[0]), NULL ) != LDAP_SUCCESS))
+		{
 			struct berval hpw, bv;
 			
 			slap_passwd_hash( &(addmod->sml_values[0]), &hpw, &txt );
@@ -1785,13 +1892,11 @@
 				rs->sr_text = txt;
 				goto return_results;
 			}
-			bv.bv_val = addmod->sml_values[0].bv_val;
-			bv.bv_len = addmod->sml_values[0].bv_len;
+			bv = addmod->sml_values[0];
 				/* clear and discard the clear password */
 			memset(bv.bv_val, 0, bv.bv_len);
 			ber_memfree(bv.bv_val);
-			addmod->sml_values[0].bv_val = hpw.bv_val;
-			addmod->sml_values[0].bv_len = hpw.bv_len;
+			addmod->sml_values[0] = hpw;
 		}
 	}
 	op->o_bd->bd_info = (BackendInfo *)on->on_info;
@@ -1803,14 +1908,15 @@
 	op->o_bd->bd_info = (BackendInfo *)on->on_info;
 	be_entry_release_r( op, e );
 	if ( send_ctrl ) {
-		LDAPControl **ctrls = NULL;
+		LDAPControl *ctrl = NULL;
 
-		ctrls = ch_calloc( sizeof( LDAPControl *) , 2 );
-		ctrls[0] = create_passcontrol( -1, -1, pErr );
-		ctrls[1] = NULL;
-		rs->sr_ctrls = ctrls;
+		ctrl = create_passcontrol( -1, -1, pErr );
+		oldctrls = add_passcontrol( op, rs, ctrl );
 	}
 	send_ldap_result( op, rs );
+	if ( send_ctrl ) {
+		ctrls_cleanup( op, rs, oldctrls );
+	}
 	return rs->sr_err;
 }
 

Modified: openldap/vendor/openldap-release/servers/slapd/overlays/syncprov.c
===================================================================
--- openldap/vendor/openldap-release/servers/slapd/overlays/syncprov.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/servers/slapd/overlays/syncprov.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,4 +1,4 @@
-/* $OpenLDAP: pkg/ldap/servers/slapd/overlays/syncprov.c,v 1.56.2.37 2006/08/15 19:50:10 hyc Exp $ */
+/* $OpenLDAP: pkg/ldap/servers/slapd/overlays/syncprov.c,v 1.56.2.38 2006/10/31 18:32:36 hyc Exp $ */
 /* syncprov.c - syncrepl provider */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
@@ -1243,9 +1243,9 @@
 }
 
 static void
-syncprov_checkpoint( Operation *op, SlapReply *rs, slap_overinst *on )
+syncprov_checkpoint( Operation *op, SlapReply *rs, slap_overinst *on,
+	struct berval *csn )
 {
-	syncprov_info_t		*si = on->on_bi.bi_private;
 	Modifications mod;
 	Operation opm;
 	SlapReply rsm = { 0 };
@@ -1253,12 +1253,12 @@
 	slap_callback cb = {0};
 
 	/* If ctxcsn is empty, delete it */
-	if ( BER_BVISEMPTY( &si->si_ctxcsn )) {
+	if ( BER_BVISEMPTY( csn )) {
 		mod.sml_values = NULL;
 	} else {
 		mod.sml_values = bv;
 		bv[1].bv_val = NULL;
-		bv[0] = si->si_ctxcsn;
+		bv[0] = *csn;
 	}
 	mod.sml_nvalues = NULL;
 	mod.sml_desc = slap_schema.si_ad_contextCSN;
@@ -1493,6 +1493,7 @@
 	{
 		struct berval maxcsn = BER_BVNULL;
 		char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
+		int do_check = 0;
 
 		/* Update our context CSN */
 		cbuf[0] = '\0';
@@ -1515,7 +1516,6 @@
 
 		si->si_numops++;
 		if ( si->si_chkops || si->si_chktime ) {
-			int do_check=0;
 			if ( si->si_chkops && si->si_numops >= si->si_chkops ) {
 				do_check = 1;
 				si->si_numops = 0;
@@ -1525,15 +1525,16 @@
 				do_check = 1;
 				si->si_chklast = op->o_time;
 			}
-			if ( do_check ) {
-				syncprov_checkpoint( op, rs, on );
-			}
 		}
 		ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
 
 		opc->sctxcsn.bv_len = maxcsn.bv_len;
 		opc->sctxcsn.bv_val = cbuf;
 
+		if ( do_check ) {
+			syncprov_checkpoint( op, rs, on, &opc->sctxcsn );
+		}
+
 		/* Handle any persistent searches */
 		if ( si->si_ops ) {
 			switch(op->o_tag) {
@@ -2458,7 +2459,7 @@
 		op->o_bd = be;
 		op->o_dn = be->be_rootdn;
 		op->o_ndn = be->be_rootndn;
-		syncprov_checkpoint( op, &rs, on );
+		syncprov_checkpoint( op, &rs, on, &si->si_ctxcsn );
 		ldap_pvt_thread_pool_context_reset( thrctx );
 	}
 

Modified: openldap/vendor/openldap-release/servers/slapd/passwd.c
===================================================================
--- openldap/vendor/openldap-release/servers/slapd/passwd.c	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/servers/slapd/passwd.c	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,5 @@
 /* passwd.c - password extended operation routines */
-/* $OpenLDAP: pkg/ldap/servers/slapd/passwd.c,v 1.95.2.19 2006/01/03 22:16:15 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/servers/slapd/passwd.c,v 1.95.2.20 2006/10/26 21:49:44 ando Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -282,8 +282,13 @@
 					 * if it cares... */
 
 		rs->sr_err = op->o_bd->be_modify( op, rs );
+
+		/* be_modify() might have shuffled modifications */
+		qpw->rs_mods = op->orm_modlist;
+
 		if ( rs->sr_err == LDAP_SUCCESS ) {
 			rs->sr_rspdata = rsp;
+
 		} else if ( rsp ) {
 			ber_bvfree( rsp );
 			rsp = NULL;
@@ -520,8 +525,7 @@
 slap_passwd_generate( struct berval *pass )
 {
 	Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
-	pass->bv_val = NULL;
-	pass->bv_len = 0;
+	BER_BVZERO( pass );
 
 	/*
 	 * generate passwords of only 8 characters as some getpass(3)

Modified: openldap/vendor/openldap-release/servers/slapd/proto-slap.h
===================================================================
--- openldap/vendor/openldap-release/servers/slapd/proto-slap.h	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/servers/slapd/proto-slap.h	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,4 +1,4 @@
-/* $OpenLDAP: pkg/ldap/servers/slapd/proto-slap.h,v 1.552.2.38 2006/08/15 17:11:09 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/servers/slapd/proto-slap.h,v 1.552.2.39 2006/11/07 04:25:02 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -720,8 +720,8 @@
 LDAP_SLAPD_F (int) slapd_daemon_destroy(void);
 LDAP_SLAPD_F (int) slapd_daemon(void);
 LDAP_SLAPD_F (Listener **)	slapd_get_listeners LDAP_P((void));
-LDAP_SLAPD_F (void) slapd_remove LDAP_P((ber_socket_t s, int wasactive,
-	int wake, int locked ));
+LDAP_SLAPD_F (void) slapd_remove LDAP_P((ber_socket_t s, Sockbuf *sb,
+	int wasactive, int wake, int locked ));
 LDAP_SLAPD_F (void) slapd_sd_lock();
 LDAP_SLAPD_F (void) slapd_sd_unlock();
 
@@ -1241,6 +1241,7 @@
 LDAP_SLAPD_F (int) slap_op_add LDAP_P(( Operation **olist, Operation *op ));
 LDAP_SLAPD_F (int) slap_op_remove LDAP_P(( Operation **olist, Operation *op ));
 LDAP_SLAPD_F (Operation *) slap_op_pop LDAP_P(( Operation **olist ));
+LDAP_SLAPD_F (slap_op_t) slap_req2op LDAP_P(( ber_tag_t tag ));
 
 /*
  * operational.c

Modified: openldap/vendor/openldap-release/servers/slapd/slap.h
===================================================================
--- openldap/vendor/openldap-release/servers/slapd/slap.h	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/servers/slapd/slap.h	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,5 @@
 /* slap.h - stand alone ldap server include file */
-/* $OpenLDAP: pkg/ldap/servers/slapd/slap.h,v 1.612.2.39 2006/04/04 22:34:42 kurt Exp $ */
+/* $OpenLDAP: pkg/ldap/servers/slapd/slap.h,v 1.612.2.40 2006/11/07 04:25:02 hyc Exp $ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
  * Copyright 1998-2006 The OpenLDAP Foundation.
@@ -2718,7 +2718,7 @@
 /*
  * Operation indices
  */
-enum {
+typedef enum {
 	SLAP_OP_BIND = 0,
 	SLAP_OP_UNBIND,
 	SLAP_OP_ADD,
@@ -2730,7 +2730,7 @@
 	SLAP_OP_ABANDON,
 	SLAP_OP_EXTENDED,
 	SLAP_OP_LAST
-};
+} slap_op_t;
 
 typedef struct slap_counters_t {
 	ldap_pvt_thread_mutex_t	sc_sent_mutex;

Modified: openldap/vendor/openldap-release/tests/scripts/test043-delta-syncrepl
===================================================================
--- openldap/vendor/openldap-release/tests/scripts/test043-delta-syncrepl	2006-11-10 20:48:24 UTC (rev 741)
+++ openldap/vendor/openldap-release/tests/scripts/test043-delta-syncrepl	2006-11-10 21:34:01 UTC (rev 742)
@@ -1,5 +1,5 @@
 #! /bin/sh
-# $OpenLDAP: pkg/ldap/tests/scripts/test043-delta-syncrepl,v 1.1.2.3 2006/01/03 22:16:29 kurt Exp $
+# $OpenLDAP: pkg/ldap/tests/scripts/test043-delta-syncrepl,v 1.1.2.4 2006/11/02 18:15:05 hyc Exp $
 ## This work is part of OpenLDAP Software <http://www.openldap.org/>.
 ##
 ## Copyright 1998-2006 The OpenLDAP Foundation.
@@ -268,6 +268,9 @@
 uid: rosco
 cn: Rosco P. Coltrane
 
+dn: cn=Mark Elliot,ou=Alumni Association,ou=People,dc=example,dc=com
+changetype: modify
+replace: drink
 EOMODS
 
 echo "Restarting consumer..."




More information about the Pkg-openldap-devel mailing list