diff --git a/acconfig.h b/acconfig.h
index 545db86416e0593716e19a674179d48d50b35c4b..ec41bf6bd8f12c0709768ced15d6ca2073177ca3 100644
--- a/acconfig.h
+++ b/acconfig.h
@@ -5,6 +5,9 @@
    Leave the following blank line there!!  Autoheader needs it.  */
 
 
+/* define this if toupper() requires tolower() check */
+#undef C_UPPER_LOWER
+
 /* define this if sys_errlist is not defined in stdio.h or errno.h */
 #undef DECL_SYS_ERRLIST
 
diff --git a/aclocal.m4 b/aclocal.m4
index 1df969aa996518e4dab085698a2cf1e778e118a6..b6e6ca9700480569597b0a3251581883d6602206 100644
--- a/aclocal.m4
+++ b/aclocal.m4
@@ -113,7 +113,7 @@ AC_DEFUN([OL_BERKELEY_DB2],
 	fi
 ])
  if test $ol_cv_berkeley_db2 = yes ; then
-	AC_DEFINE(HAVE_BERKELEY_DB2)
+	AC_DEFINE(HAVE_BERKELEY_DB2,1)
  fi
 ])dnl
 dnl
@@ -179,7 +179,7 @@ AC_DEFUN([OL_BERKELEY_DB],
 	fi
 ])
  if test $ol_cv_berkeley_db = yes ; then
-	AC_DEFINE(HAVE_BERKELEY_DB)
+	AC_DEFINE(HAVE_BERKELEY_DB,1)
  fi
 ])dnl
 dnl
@@ -224,7 +224,7 @@ AC_DEFUN([OL_GDBM],
 	fi
 ])
  if test $ol_cv_gdbm = yes ; then
-	AC_DEFINE(HAVE_GDBM)
+	AC_DEFINE(HAVE_GDBM,1)
  fi
 ])dnl
 dnl
@@ -280,7 +280,7 @@ AC_DEFUN([OL_NDBM],
 	fi
 ])
  if test $ol_cv_ndbm = yes ; then
-	AC_DEFINE(HAVE_NDBM)
+	AC_DEFINE(HAVE_NDBM,1)
  fi
 ])dnl
 dnl
@@ -340,6 +340,30 @@ AC_CACHE_CHECK([for LinuxThreads], [ol_cv_linux_threads], [
 ])
 ])dnl
 dnl
+dnl ====================================================================
+dnl Check if toupper() requires islower() to be called first
+AC_DEFUN([OL_C_UPPER_LOWER],
+[
+AC_MSG_CHECKING([if toupper() requires islower()])
+AC_CACHE_VAL(ol_cv_c_upper_lower,[
+	AC_TRY_RUN([
+#include <ctypes.h>
+main()
+{
+	if ('C' == toupper('C'))
+		exit 0;
+	else
+		exit 1;
+}],
+	[ol_cv_c_upper_lower=no],
+	[ol_cv_c_upper_lower=yes],
+	[ol_cv_c_upper_lower=safe])])
+AC_MSG_RESULT($ol_cv_c_upper_lower)
+if test $ol_cv_c_upper_lower != no ; then
+	AC_DEFINE(C_UPPER_LOWER,1)
+fi
+])
+
 dnl ====================================================================
 dnl Check for declaration of sys_errlist in one of stdio.h and errno.h.
 dnl Declaration of sys_errlist on BSD4.4 interferes with our declaration.
@@ -359,7 +383,7 @@ AC_MSG_RESULT($ol_cv_dcl_sys_errlist)
 
 # It's possible (for near-UNIX clones) that sys_errlist doesn't exist
 if test $ol_cv_dcl_sys_errlist = no ; then
-	AC_DEFINE(DECL_SYS_ERRLIST)
+	AC_DEFINE(DECL_SYS_ERRLIST,1)
 	AC_MSG_CHECKING([existence of sys_errlist])
 	AC_CACHE_VAL(ol_cv_have_sys_errlist,[
 		AC_TRY_LINK([#include <errno.h>],
diff --git a/configure b/configure
index 2cd35bfb9aab6a443f0f45b779b653c730c6573a..516047020cee796d2148667252c98025098e99ab 100755
--- a/configure
+++ b/configure
@@ -5430,103 +5430,58 @@ EOF
 fi
 
 
-echo $ac_n "checking whether byte ordering is bigendian""... $ac_c" 1>&6
-echo "configure:5435: checking whether byte ordering is bigendian" >&5
-if eval "test \"`echo '$''{'ac_cv_c_bigendian'+set}'`\" = set"; then
-  echo $ac_n "(cached) $ac_c" 1>&6
-else
-  ac_cv_c_bigendian=unknown
-# See if sys/param.h defines the BYTE_ORDER macro.
-cat > conftest.$ac_ext <<EOF
-#line 5442 "configure"
-#include "confdefs.h"
-#include <sys/types.h>
-#include <sys/param.h>
-int main() {
-
-#if !BYTE_ORDER || !BIG_ENDIAN || !LITTLE_ENDIAN
- bogus endian macros
-#endif
-; return 0; }
-EOF
-if { (eval echo configure:5453: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
-  rm -rf conftest*
-  # It does; now see whether it defined to BIG_ENDIAN or not.
-cat > conftest.$ac_ext <<EOF
-#line 5457 "configure"
-#include "confdefs.h"
-#include <sys/types.h>
-#include <sys/param.h>
-int main() {
 
-#if BYTE_ORDER != BIG_ENDIAN
- not big endian
-#endif
-; return 0; }
-EOF
-if { (eval echo configure:5468: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
-  rm -rf conftest*
-  ac_cv_c_bigendian=yes
-else
-  echo "configure: failed program was:" >&5
-  cat conftest.$ac_ext >&5
-  rm -rf conftest*
-  ac_cv_c_bigendian=no
-fi
-rm -f conftest*
+echo $ac_n "checking if toupper() requires islower()""... $ac_c" 1>&6
+echo "configure:5436: checking if toupper() requires islower()" >&5
+if eval "test \"`echo '$''{'ol_cv_c_upper_lower'+set}'`\" = set"; then
+  echo $ac_n "(cached) $ac_c" 1>&6
 else
-  echo "configure: failed program was:" >&5
-  cat conftest.$ac_ext >&5
-fi
-rm -f conftest*
-if test $ac_cv_c_bigendian = unknown; then
-if test "$cross_compiling" = yes; then
-    { echo "configure: error: can not run test program while cross compiling" 1>&2; exit 1; }
+  
+	if test "$cross_compiling" = yes; then
+  ol_cv_c_upper_lower=safe
 else
   cat > conftest.$ac_ext <<EOF
-#line 5488 "configure"
+#line 5445 "configure"
 #include "confdefs.h"
-main () {
-  /* Are we little or big endian?  From Harbison&Steele.  */
-  union
-  {
-    long l;
-    char c[sizeof (long)];
-  } u;
-  u.l = 1;
-  exit (u.c[sizeof (long) - 1] == 1);
+
+#include <ctypes.h>
+main()
+{
+	if ('C' == toupper('C'))
+		exit 0;
+	else
+		exit 1;
 }
 EOF
-if { (eval echo configure:5501: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:5457: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
 then
-  ac_cv_c_bigendian=no
+  ol_cv_c_upper_lower=no
 else
   echo "configure: failed program was:" >&5
   cat conftest.$ac_ext >&5
   rm -fr conftest*
-  ac_cv_c_bigendian=yes
+  ol_cv_c_upper_lower=yes
 fi
 rm -fr conftest*
 fi
 
-fi
 fi
 
-echo "$ac_t""$ac_cv_c_bigendian" 1>&6
-if test $ac_cv_c_bigendian = yes; then
-  cat >> confdefs.h <<\EOF
-#define WORDS_BIGENDIAN 1
+echo "$ac_t""$ol_cv_c_upper_lower" 1>&6
+if test $ol_cv_c_upper_lower != no ; then
+	cat >> confdefs.h <<\EOF
+#define C_UPPER_LOWER 1
 EOF
 
 fi
 
 echo $ac_n "checking for working const""... $ac_c" 1>&6
-echo "configure:5525: checking for working const" >&5
+echo "configure:5480: checking for working const" >&5
 if eval "test \"`echo '$''{'ac_cv_c_const'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5530 "configure"
+#line 5485 "configure"
 #include "confdefs.h"
 
 int main() {
@@ -5575,7 +5530,7 @@ ccp = (char const *const *) p;
 
 ; return 0; }
 EOF
-if { (eval echo configure:5579: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:5534: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   ac_cv_c_const=yes
 else
@@ -5598,7 +5553,7 @@ fi
 
 
 echo $ac_n "checking for 8-bit clean memcmp""... $ac_c" 1>&6
-echo "configure:5602: checking for 8-bit clean memcmp" >&5
+echo "configure:5557: checking for 8-bit clean memcmp" >&5
 if eval "test \"`echo '$''{'ac_cv_func_memcmp_clean'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -5606,7 +5561,7 @@ else
   ac_cv_func_memcmp_clean=no
 else
   cat > conftest.$ac_ext <<EOF
-#line 5610 "configure"
+#line 5565 "configure"
 #include "confdefs.h"
 
 main()
@@ -5616,7 +5571,7 @@ main()
 }
 
 EOF
-if { (eval echo configure:5620: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:5575: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
 then
   ac_cv_func_memcmp_clean=yes
 else
@@ -5634,12 +5589,12 @@ echo "$ac_t""$ac_cv_func_memcmp_clean" 1>&6
 test $ac_cv_func_memcmp_clean = no && LIBOBJS="$LIBOBJS memcmp.o"
 
 echo $ac_n "checking for strftime""... $ac_c" 1>&6
-echo "configure:5638: checking for strftime" >&5
+echo "configure:5593: checking for strftime" >&5
 if eval "test \"`echo '$''{'ac_cv_func_strftime'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5643 "configure"
+#line 5598 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char strftime(); below.  */
@@ -5662,7 +5617,7 @@ strftime();
 
 ; return 0; }
 EOF
-if { (eval echo configure:5666: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:5621: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   eval "ac_cv_func_strftime=yes"
 else
@@ -5684,7 +5639,7 @@ else
   echo "$ac_t""no" 1>&6
 # strftime is in -lintl on SCO UNIX.
 echo $ac_n "checking for strftime in -lintl""... $ac_c" 1>&6
-echo "configure:5688: checking for strftime in -lintl" >&5
+echo "configure:5643: checking for strftime in -lintl" >&5
 ac_lib_var=`echo intl'_'strftime | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
@@ -5692,7 +5647,7 @@ else
   ac_save_LIBS="$LIBS"
 LIBS="-lintl  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 5696 "configure"
+#line 5651 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
@@ -5703,7 +5658,7 @@ int main() {
 strftime()
 ; return 0; }
 EOF
-if { (eval echo configure:5707: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:5662: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
@@ -5730,12 +5685,12 @@ fi
 fi
 
 echo $ac_n "checking for vprintf""... $ac_c" 1>&6
-echo "configure:5734: checking for vprintf" >&5
+echo "configure:5689: checking for vprintf" >&5
 if eval "test \"`echo '$''{'ac_cv_func_vprintf'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5739 "configure"
+#line 5694 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char vprintf(); below.  */
@@ -5758,7 +5713,7 @@ vprintf();
 
 ; return 0; }
 EOF
-if { (eval echo configure:5762: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:5717: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   eval "ac_cv_func_vprintf=yes"
 else
@@ -5782,12 +5737,12 @@ fi
 
 if test "$ac_cv_func_vprintf" != yes; then
 echo $ac_n "checking for _doprnt""... $ac_c" 1>&6
-echo "configure:5786: checking for _doprnt" >&5
+echo "configure:5741: checking for _doprnt" >&5
 if eval "test \"`echo '$''{'ac_cv_func__doprnt'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5791 "configure"
+#line 5746 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char _doprnt(); below.  */
@@ -5810,7 +5765,7 @@ _doprnt();
 
 ; return 0; }
 EOF
-if { (eval echo configure:5814: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:5769: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   eval "ac_cv_func__doprnt=yes"
 else
@@ -5835,7 +5790,7 @@ fi
 fi
 
 echo $ac_n "checking for wait3 that fills in rusage""... $ac_c" 1>&6
-echo "configure:5839: checking for wait3 that fills in rusage" >&5
+echo "configure:5794: checking for wait3 that fills in rusage" >&5
 if eval "test \"`echo '$''{'ac_cv_func_wait3_rusage'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -5843,7 +5798,7 @@ else
   ac_cv_func_wait3_rusage=no
 else
   cat > conftest.$ac_ext <<EOF
-#line 5847 "configure"
+#line 5802 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <sys/time.h>
@@ -5874,7 +5829,7 @@ main() {
   }
 }
 EOF
-if { (eval echo configure:5878: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:5833: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
 then
   ac_cv_func_wait3_rusage=yes
 else
@@ -5926,12 +5881,12 @@ for ac_func in \
 
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:5930: checking for $ac_func" >&5
+echo "configure:5885: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5935 "configure"
+#line 5890 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -5954,7 +5909,7 @@ $ac_func();
 
 ; return 0; }
 EOF
-if { (eval echo configure:5958: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:5913: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
@@ -5982,12 +5937,12 @@ done
 for ac_func in getopt strdup
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:5986: checking for $ac_func" >&5
+echo "configure:5941: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5991 "configure"
+#line 5946 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
@@ -6010,7 +5965,7 @@ $ac_func();
 
 ; return 0; }
 EOF
-if { (eval echo configure:6014: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:5969: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
@@ -6040,13 +5995,13 @@ done
 # Check Configuration
 
 echo $ac_n "checking declaration of sys_errlist""... $ac_c" 1>&6
-echo "configure:6044: checking declaration of sys_errlist" >&5
+echo "configure:5999: checking declaration of sys_errlist" >&5
 if eval "test \"`echo '$''{'ol_cv_dcl_sys_errlist'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   
 	cat > conftest.$ac_ext <<EOF
-#line 6050 "configure"
+#line 6005 "configure"
 #include "confdefs.h"
 
 #include <stdio.h>
@@ -6056,7 +6011,7 @@ int main() {
 char *c = (char *) *sys_errlist
 ; return 0; }
 EOF
-if { (eval echo configure:6060: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:6015: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   ol_cv_dcl_sys_errlist=yes
 else
@@ -6077,20 +6032,20 @@ if test $ol_cv_dcl_sys_errlist = no ; then
 EOF
 
 	echo $ac_n "checking existence of sys_errlist""... $ac_c" 1>&6
-echo "configure:6081: checking existence of sys_errlist" >&5
+echo "configure:6036: checking existence of sys_errlist" >&5
 	if eval "test \"`echo '$''{'ol_cv_have_sys_errlist'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   
 		cat > conftest.$ac_ext <<EOF
-#line 6087 "configure"
+#line 6042 "configure"
 #include "confdefs.h"
 #include <errno.h>
 int main() {
 char *c = (char *) *sys_errlist
 ; return 0; }
 EOF
-if { (eval echo configure:6094: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
+if { (eval echo configure:6049: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest; then
   rm -rf conftest*
   ol_cv_have_sys_errlist=yes
 else
diff --git a/configure.in b/configure.in
index 725008a5e82668cdffaf7b93e43b35ab3fc35501..e112c59a5259fa32816b89d3580082299a85cee4 100644
--- a/configure.in
+++ b/configure.in
@@ -716,7 +716,7 @@ AC_STRUCT_ST_BLKSIZE
 AC_HEADER_TIME
 AC_STRUCT_TM
 
-AC_C_BIGENDIAN
+OL_C_UPPER_LOWER
 AC_C_CONST
 
 dnl AC_CHECK_SIZEOF(short) 
diff --git a/include/portable.h.in b/include/portable.h.in
index 67a861f0554542320dcfca05497b23e6bec4f73d..3b8fd80dac7437e705fed4b8a8e16d69461b6fbf 100644
--- a/include/portable.h.in
+++ b/include/portable.h.in
@@ -104,9 +104,8 @@ is provided ``as is'' without express or implied warranty.
 /* Define to `int' if <sys/types.h> doesn't define.  */
 #undef uid_t
 
-/* Define if your processor stores words with the most significant
-   byte first (like Motorola and SPARC, unlike Intel and VAX).  */
-#undef WORDS_BIGENDIAN
+/* define this if toupper() requires tolower() check */
+#undef C_UPPER_LOWER
 
 /* define this if sys_errlist is not defined in stdio.h or errno.h */
 #undef DECL_SYS_ERRLIST
diff --git a/servers/slapd/abandon.c b/servers/slapd/abandon.c
index d6f7291c7817f83558d1e222cadbd6bc0eb5e647..fb9d1be4b265fd3ef68d56a686b4a4885a88914b 100644
--- a/servers/slapd/abandon.c
+++ b/servers/slapd/abandon.c
@@ -12,9 +12,11 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+#include <ac/socket.h>
+
 #include "slap.h"
 
 extern Backend	*select_backend();
diff --git a/servers/slapd/acl.c b/servers/slapd/acl.c
index a5fd38abbdb3edffb99493c1ba321efd0526566b..10985533190df937542169770f0443e30b080c11 100644
--- a/servers/slapd/acl.c
+++ b/servers/slapd/acl.c
@@ -1,5 +1,7 @@
 /* acl.c - routines to parse and check acl's */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/aclparse.c b/servers/slapd/aclparse.c
index 10fa6a07210cd7681b2599953df1ceabeee7d107..a8ba9510896255886cec94d2c9eacfa87fe80ed6 100644
--- a/servers/slapd/aclparse.c
+++ b/servers/slapd/aclparse.c
@@ -1,5 +1,6 @@
 /* acl.c - routines to parse and check acl's */
 
+#include "portable.h"
 
 #include <stdio.h>
 #include <string.h>
@@ -7,12 +8,13 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netdb.h>
-#include "regex.h"
+#include <unistd.h>
+#include <regex.h>
+
 #include "slap.h"
 #include "portable.h"
 
 extern Filter		*str2filter();
-extern char		*re_comp();
 extern struct acl	*global_acl;
 extern char		**str2charray();
 extern char		*dn_upcase();
@@ -26,6 +28,62 @@ static void		print_acl();
 static void		print_access();
 #endif
 
+int
+regtest(char *fname, int lineno, char *pat) {
+	int e;
+	regex_t re;
+
+	char buf[512];
+	int size;
+
+	char *sp;
+	char *dp;
+	int  flag;
+
+	sp = pat;
+	dp = buf;
+	size = 0;
+	buf[0] = '\0';
+
+	for (size = 0, flag = 0; (size < sizeof(buf)) && *sp; sp++) {
+		if (flag) {
+			if (*sp == '$'|| (*sp >= '0' && *sp <= '9')) {
+				*dp++ = *sp;
+				size++;
+			}
+			flag = 0;
+
+		} else {
+			if (*sp == '$') {
+				flag = 1;
+			} else {
+				*dp++ = *sp;
+				size++;
+			}
+		}
+	}
+
+	*dp = '\0';
+	if ( size >= (sizeof(buf)-1) ) {
+		fprintf( stderr,
+			"%s: line %d: regular expression \"%s\" too large\n",
+			fname, lineno, pat, 0 );
+		acl_usage();
+	}
+
+	if ((e = regcomp(&re, buf, REG_EXTENDED|REG_ICASE))) {
+		char error[512];
+		regerror(e, &re, error, sizeof(error));
+		fprintf( stderr,
+			"%s: line %d: regular expression \"%s\" bad because of %s\n",
+			fname, lineno, pat, error );
+		acl_usage();
+		return(0);
+	}
+	regfree(&re);
+	return(1);
+}
+
 void
 parse_acl(
     Backend	*be,
@@ -58,6 +116,17 @@ parse_acl(
 				}
 
 				if ( strcasecmp( argv[i], "*" ) == 0 ) {
+					int e;
+					if ((e = regcomp( &a->acl_dnre, ".*",
+						REG_EXTENDED|REG_ICASE)))
+					{
+						char buf[512];
+						regerror(e, &a->acl_dnre, buf, sizeof(buf));
+						fprintf( stderr,
+							"%s: line %d: regular expression \"%s\" bad because of %s\n",
+							fname, lineno, right, buf );
+						acl_usage();
+					}
 					a->acl_dnpat = strdup( ".*" );
 					continue;
 				}
@@ -79,14 +148,19 @@ parse_acl(
 						acl_usage();
 					}
 				} else if ( strcasecmp( left, "dn" ) == 0 ) {
-					if ( (e = re_comp( right )) != NULL ) {
+					int e;
+					if ((e = regcomp(&a->acl_dnre, right,
+						REG_EXTENDED|REG_ICASE))) {
+						char buf[512];
+						regerror(e, &a->acl_dnre, buf, sizeof(buf));
 						fprintf( stderr,
-		"%s: line %d: regular expression \"%s\" bad because of %s\n",
-						    fname, lineno, right, e );
+				"%s: line %d: regular expression \"%s\" bad because of %s\n",
+							fname, lineno, right, buf );
 						acl_usage();
+
+					} else {
+						a->acl_dnpat = dn_upcase(strdup( right ));
 					}
-					a->acl_dnpat = dn_upcase( strdup(
-					    right ) );
 				} else if ( strncasecmp( left, "attr", 4 )
 				    == 0 ) {
 					char	**alist;
@@ -96,7 +170,7 @@ parse_acl(
 					free( alist );
 				} else {
 					fprintf( stderr,
-				"%s: line %d: expecting <what> got \"%s\"\n",
+						"%s: line %d: expecting <what> got \"%s\"\n",
 					    fname, lineno, left );
 					acl_usage();
 				}
@@ -106,7 +180,7 @@ parse_acl(
 		} else if ( strcasecmp( argv[i], "by" ) == 0 ) {
 			if ( a == NULL ) {
 				fprintf( stderr,
-	"%s: line %d: to clause required before by clause in access line\n",
+					"%s: line %d: to clause required before by clause in access line\n",
 				    fname, lineno );
 				acl_usage();
 			}
@@ -131,38 +205,27 @@ parse_acl(
 			} else if ( strcasecmp( argv[i], "self" ) == 0 ) {
 				b->a_dnpat = strdup( "self" );
 			} else if ( strcasecmp( left, "dn" ) == 0 ) {
-				if ( (e = re_comp( right )) != NULL ) {
-					fprintf( stderr,
-			"%s: line %d: regular expression \"%s\" bad: %s\n",
-					    fname, lineno, right, e );
-					acl_usage();
-				}
+				regtest(fname, lineno, right);
 				b->a_dnpat = dn_upcase( strdup( right ) );
-			} else if ( strcasecmp( left, "dnattr" )
-			    == 0 ) {
+			} else if ( strcasecmp( left, "dnattr" ) == 0 ) {
 				b->a_dnattr = strdup( right );
-			} else if ( strcasecmp( left, "domain" )
-			    == 0 ) {
-				char	*s;
 
-				if ( (e = re_comp( right )) != NULL ) {
-					fprintf( stderr,
-			"%s: line %d: regular expression \"%s\" bad: %s\n",
-					    fname, lineno, right, e );
-					acl_usage();
-				}
+#ifdef ACLGROUP
+			} else if ( strcasecmp( left, "group" ) == 0 ) {
+				regtest(fname, lineno, right);
+				b->a_group = dn_upcase(strdup( right ));
+#endif /* ACLGROUP */
+			} else if ( strcasecmp( left, "domain" ) == 0 ) {
+				char	*s;
+				regtest(fname, lineno, right);
 				b->a_domainpat = strdup( right );
+
 				/* normalize the domain */
 				for ( s = b->a_domainpat; *s; s++ ) {
 					*s = TOLOWER( *s );
 				}
 			} else if ( strcasecmp( left, "addr" ) == 0 ) {
-				if ( (e = re_comp( right )) != NULL ) {
-					fprintf( stderr,
-			"%s: line %d: regular expression \"%s\" bad: %s\n",
-					    fname, lineno, right, e );
-					acl_usage();
-				}
+				regtest(fname, lineno, right);
 				b->a_addrpat = strdup( right );
 			} else {
 				fprintf( stderr,
@@ -198,16 +261,15 @@ parse_acl(
 
 	/* if we have no real access clause, complain and do nothing */
 	if ( a == NULL ) {
-	
 			fprintf( stderr,
-		    "%s: line %d: warning: no access clause(s) specified in access line\n",
+				"%s: line %d: warning: no access clause(s) specified in access line\n",
 			    fname, lineno );
 
 	} else {
 	
 		if ( a->acl_access == NULL ) {
 			fprintf( stderr,
-		    "%s: line %d: warning: no by clause(s) specified in access line\n",
+		    	"%s: line %d: warning: no by clause(s) specified in access line\n",
 			    fname, lineno );
 		}
 
@@ -373,4 +435,4 @@ print_acl( struct acl *a )
 	}
 }
 
-#endif
+#endif /* LDAP_DEBUG */
diff --git a/servers/slapd/add.c b/servers/slapd/add.c
index 93cc3cdcd3b3c74e7eab51c8c35d54632fc5bb19..feecf522b5e4f9730c4696b1efd8edb58f28d054 100644
--- a/servers/slapd/add.c
+++ b/servers/slapd/add.c
@@ -10,6 +10,8 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
diff --git a/servers/slapd/attr.c b/servers/slapd/attr.c
index fb9add64bc277aac01b5094eb36e12fc738bb169..5842813b96b4b03246c8298c72a995ddee72a667 100644
--- a/servers/slapd/attr.c
+++ b/servers/slapd/attr.c
@@ -1,5 +1,7 @@
 /* attr.c - routines for dealing with attributes */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
diff --git a/servers/slapd/ava.c b/servers/slapd/ava.c
index ef0487c7aafcc0c0f05b0f252c7f2a27ae7b4294..12ef18cbf4f9acfd27c6e2165a39aafc68acb47b 100644
--- a/servers/slapd/ava.c
+++ b/servers/slapd/ava.c
@@ -1,5 +1,7 @@
 /* ava.c - routines for dealing with attribute value assertions */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-ldbm/abandon.c b/servers/slapd/back-ldbm/abandon.c
index 8ed2dfd5cb4693185e3cd11a6847193bb4970406..8f1ca0e040ee1b75629def37acf0de6a0a84a80e 100644
--- a/servers/slapd/back-ldbm/abandon.c
+++ b/servers/slapd/back-ldbm/abandon.c
@@ -1,5 +1,7 @@
 /* abandon.c - ldbm backend abandon routine */
 
+#include "portable.h"
+
 ldbm_back_abandon()
 {
 }
diff --git a/servers/slapd/back-ldbm/add.c b/servers/slapd/back-ldbm/add.c
index 797398ad4b5cd72d14f409874fad2dd7752bae2b..37b6a4063536263b53437637d94cee47acd196b3 100644
--- a/servers/slapd/back-ldbm/add.c
+++ b/servers/slapd/back-ldbm/add.c
@@ -1,5 +1,7 @@
 /* add.c - ldap ldbm back-end add routine */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-ldbm/attr.c b/servers/slapd/back-ldbm/attr.c
index dfe72f6e2f67d5b0b35c9f8ca6e68e0b18f58cc2..ca59ba309090332fe4f99337e7012b597551d998 100644
--- a/servers/slapd/back-ldbm/attr.c
+++ b/servers/slapd/back-ldbm/attr.c
@@ -1,5 +1,7 @@
 /* attr.c - backend routines for dealing with attributes */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-ldbm/back-ldbm.h b/servers/slapd/back-ldbm/back-ldbm.h
index 28243301907197768cd7e076f56c776301ff8e73..3d656f4dd91f1cc8f865d3f28e2a910f98031de0 100644
--- a/servers/slapd/back-ldbm/back-ldbm.h
+++ b/servers/slapd/back-ldbm/back-ldbm.h
@@ -5,8 +5,16 @@
 
 #include "ldbm.h"
 
+LDAP_BEGIN_DECL
+
 #define DEFAULT_CACHE_SIZE	1000
-#define DEFAULT_DBCACHE_SIZE	100000
+
+#ifdef LDBM_USE_DB2
+#	define DEFAULT_DBCACHE_SIZE (100 * DEFAULT_DB_PAGE_SIZE)
+#else
+#	define DEFAULT_DBCACHE_SIZE 100000
+#endif
+
 #define DEFAULT_DB_DIRECTORY	"/usr/tmp"
 #define DEFAULT_MODE		0600
 
@@ -102,13 +110,14 @@ struct ldbminfo {
 	struct cache		li_cache;
 	Avlnode			*li_attrs;
 	int			li_dbcachesize;
+	int			li_flush_wrt;
 	struct dbcache		li_dbcache[MAXDBCACHE];
 	pthread_mutex_t		li_dbcache_mutex;
 	pthread_cond_t		li_dbcache_cv;
 };
 
-#ifdef NEEDPROTOS
 #include "proto-back-ldbm.h"
-#endif
+
+LDAP_END_DECL
 
 #endif /* _back_ldbm_h_ */
diff --git a/servers/slapd/back-ldbm/bind.c b/servers/slapd/back-ldbm/bind.c
index 66dfddbceb55fff38dc5908f1f2c953635238a09..90aa228f089b44e7cd90c3eef85b63def75caac1 100644
--- a/servers/slapd/back-ldbm/bind.c
+++ b/servers/slapd/back-ldbm/bind.c
@@ -1,5 +1,7 @@
 /* bind.c - ldbm backend bind and unbind routines */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-ldbm/cache.c b/servers/slapd/back-ldbm/cache.c
index 819b1f2712cd7ea1e7bd65050bafc062c9998633..4c5e7c32038f51eb2562273d28a1ef01fc2b0dd4 100644
--- a/servers/slapd/back-ldbm/cache.c
+++ b/servers/slapd/back-ldbm/cache.c
@@ -1,5 +1,7 @@
 /* cache.c - routines to maintain an in-core cache of entries */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
diff --git a/servers/slapd/back-ldbm/close.c b/servers/slapd/back-ldbm/close.c
index d6758e143bf2113ef2d35b297eef002bad38332b..66ef3e860640eff4d9842497f0c8e9bff84a5d6b 100644
--- a/servers/slapd/back-ldbm/close.c
+++ b/servers/slapd/back-ldbm/close.c
@@ -1,5 +1,7 @@
 /* close.c - close ldbm backend */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
diff --git a/servers/slapd/back-ldbm/compare.c b/servers/slapd/back-ldbm/compare.c
index b0974ffdbbd396867ad3ff1effc6e3237654d38a..714923e1774e09eb71d613c9d2adb217be5e1213 100644
--- a/servers/slapd/back-ldbm/compare.c
+++ b/servers/slapd/back-ldbm/compare.c
@@ -1,5 +1,7 @@
 /* compare.c - ldbm backend compare routine */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-ldbm/config.c b/servers/slapd/back-ldbm/config.c
index d6aca2c413fcd20213bd8f75e8d04b630ee5868f..5cbfd6e924413c85c11f5013b3bef518130e1c6c 100644
--- a/servers/slapd/back-ldbm/config.c
+++ b/servers/slapd/back-ldbm/config.c
@@ -1,5 +1,7 @@
 /* config.c - ldbm backend configuration file routine */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -77,6 +79,10 @@ ldbm_back_config(
 		}
 		li->li_dbcachesize = atoi( argv[1] );
 
+	/* flush on writes */
+	} else if ( strcasecmp( argv[0], "flushwrites" ) == 0 ) {
+		li->li_flush_wrt = 1;
+
 	/* anything else */
 	} else {
 		fprintf( stderr,
diff --git a/servers/slapd/back-ldbm/dbcache.c b/servers/slapd/back-ldbm/dbcache.c
index 269e3d152c924d97b50b33141ef00152add61374..bef299be86f319f87dd56793d26c3f2c17051d5b 100644
--- a/servers/slapd/back-ldbm/dbcache.c
+++ b/servers/slapd/back-ldbm/dbcache.c
@@ -1,6 +1,5 @@
 /* ldbmcache.c - maintain a cache of open ldbm files */
 
-#define DISABLE_BRIDGE /* disable LDAP_BRIDGE code */
 #include "portable.h"
 
 #include <stdio.h>
diff --git a/servers/slapd/back-ldbm/delete.c b/servers/slapd/back-ldbm/delete.c
index b6ffa79e0e5b3edae3fb47be86c4af1937d6f0b3..ee5fbabca6ba1db807c60f7a26541c5ceeb79a68 100644
--- a/servers/slapd/back-ldbm/delete.c
+++ b/servers/slapd/back-ldbm/delete.c
@@ -1,5 +1,7 @@
 /* delete.c - ldbm backend delete routine */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-ldbm/dn2id.c b/servers/slapd/back-ldbm/dn2id.c
index 0a7edac280e7fdb39f8cae9873fa46e4ba828841..89a5ea13eac76794c64eda97415471a6da033a65 100644
--- a/servers/slapd/back-ldbm/dn2id.c
+++ b/servers/slapd/back-ldbm/dn2id.c
@@ -1,5 +1,7 @@
 /* dn2id.c - routines to deal with the dn2id index */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-ldbm/filterindex.c b/servers/slapd/back-ldbm/filterindex.c
index 55aada54e45e9286fa9caf83de36bdb987f9acbd..b65b841e2440a7353790b9081fe723eac337bca2 100644
--- a/servers/slapd/back-ldbm/filterindex.c
+++ b/servers/slapd/back-ldbm/filterindex.c
@@ -1,5 +1,7 @@
 /* filterindex.c - generate the list of candidate entries from a filter */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-ldbm/group.c b/servers/slapd/back-ldbm/group.c
index 8ada479ea47eaa5a4b1cd40f4b8f0f23a46577fb..e3e3436ceb9e9f864e07f97e22cfda78d5b6b5f1 100644
--- a/servers/slapd/back-ldbm/group.c
+++ b/servers/slapd/back-ldbm/group.c
@@ -1,5 +1,7 @@
 /* compare.c - ldbm backend compare routine */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-ldbm/id2children.c b/servers/slapd/back-ldbm/id2children.c
index 3b2c55d202170c4f13ef836525b711037b752502..2906c5b2cb97b91a4666dec5946cc9cb37aaf8d3 100644
--- a/servers/slapd/back-ldbm/id2children.c
+++ b/servers/slapd/back-ldbm/id2children.c
@@ -1,5 +1,7 @@
 /* id2children.c - routines to deal with the id2children index */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -23,6 +25,11 @@ id2children_add(
 	IDList		*idl;
 	char		buf[20];
 
+#ifdef LDBM_USE_DB2
+	memset( &key, 0, sizeof( key ) );
+	memset( &data, 0, sizeof( data ) );
+#endif
+
 	Debug( LDAP_DEBUG_TRACE, "=> id2children_add( %d, %d )\n", p ? p->e_id
 	    : 0, e->e_id, 0 );
 
@@ -63,6 +70,10 @@ has_children(
 	IDList		*idl;
 	char		buf[20];
 
+#ifdef LDBM_USE_DB2
+	memset( &key, 0, sizeof( key ) );
+#endif
+
 	Debug( LDAP_DEBUG_TRACE, "=> has_children( %d )\n", p->e_id , 0, 0 );
 
 	if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
diff --git a/servers/slapd/back-ldbm/id2entry.c b/servers/slapd/back-ldbm/id2entry.c
index 38522cb54ac392dba04f87fecc86bf6d7748eba1..140555c0c32cabc9b44245fd09f47e2e5702743e 100644
--- a/servers/slapd/back-ldbm/id2entry.c
+++ b/servers/slapd/back-ldbm/id2entry.c
@@ -1,5 +1,7 @@
 /* id2entry.c - routines to deal with the id2entry index */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
diff --git a/servers/slapd/back-ldbm/idl.c b/servers/slapd/back-ldbm/idl.c
index d00b025d523e0b21e287ceaad87541d4f9613d89..d68c7b834bc28d947822e7217f0c0bedd5314105 100644
--- a/servers/slapd/back-ldbm/idl.c
+++ b/servers/slapd/back-ldbm/idl.c
@@ -1,6 +1,5 @@
 /* idl.c - ldap id list handling routines */
 
-#define DISABLE_BRIDGE
 #include "portable.h"
 
 #include <stdio.h>
diff --git a/servers/slapd/back-ldbm/index.c b/servers/slapd/back-ldbm/index.c
index 3d75a091d3166f497ebbc9fa9a58bbff92521014..1e58ab8c91b29bf80fbf68b35f3595d18015eb24 100644
--- a/servers/slapd/back-ldbm/index.c
+++ b/servers/slapd/back-ldbm/index.c
@@ -1,5 +1,7 @@
 /* index.c - routines for dealing with attribute indexes */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -105,6 +107,10 @@ index_read(
 	char		*realval, *tmpval;
 	char		buf[BUFSIZ];
 
+#ifdef LDBM_USE_DB2
+	memset( &key, 0, sizeof( key ) );
+#endif
+
 	prefix = index2prefix( indextype );
 	Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
 	    type, prefix, val );
@@ -130,25 +136,26 @@ index_read(
 	realval = val;
 	tmpval = NULL;
 	if ( prefix != '\0' ) {
-		int	len;
+              int     len = strlen( val );
 
-		if ( (len = strlen( val )) < sizeof(buf) ) {
-			buf[0] = prefix;
-			strcpy( &buf[1], val );
+              if ( (len + 2) < sizeof(buf) ) {
 			realval = buf;
 		} else {
 			/* value + prefix + null */
 			tmpval = (char *) ch_malloc( len + 2 );
-			tmpval[0] = prefix;
-			strcat( &tmpval[1], val );
 			realval = tmpval;
 		}
+              realval[0] = prefix;
+              strcpy( &realval[1], val );
 	}
 
 	key.dptr = realval;
 	key.dsize = strlen( realval ) + 1;
 
 	idl = idl_fetch( be, db, key );
+      if ( tmpval != NULL ) {
+              free( tmpval );
+      }
 
 	ldbm_cache_close( be, db );
 
@@ -174,6 +181,10 @@ add_value(
 	char	*realval, *tmpval, *s;
 	char	buf[BUFSIZ];
 
+#ifdef LDBM_USE_DB2
+	memset( &key, 0, sizeof( key ) );
+#endif
+
 	prefix = index2prefix( indextype );
 	Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
 
@@ -181,19 +192,17 @@ add_value(
 	tmpval = NULL;
 	idl = NULL;
 	if ( prefix != '\0' ) {
-		int	len;
+              int     len = strlen( val );
 
-		if ( (len = strlen( val )) < sizeof(buf) ) {
-			buf[0] = prefix;
-			strcpy( &buf[1], val );
+              if ( (len + 2) < sizeof(buf) ) {
 			realval = buf;
 		} else {
 			/* value + prefix + null */
 			tmpval = (char *) ch_malloc( len + 2 );
-			tmpval[0] = prefix;
-			strcat( &tmpval[1], val );
 			realval = tmpval;
 		}
+              realval[0] = prefix;
+              strcpy( &realval[1], val );
 	}
 
 	key.dptr = realval;
diff --git a/servers/slapd/back-ldbm/init.c b/servers/slapd/back-ldbm/init.c
index b9c5c2f3fa1feac501b93271679a854ce25819ec..eaf886baf847c6828dfec76715c6702cf5154d30 100644
--- a/servers/slapd/back-ldbm/init.c
+++ b/servers/slapd/back-ldbm/init.c
@@ -1,5 +1,7 @@
 /* init.c - initialize ldbm backend */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -15,6 +17,10 @@ ldbm_back_init(
 	char		*argv[ 4 ];
 	int		i;
 
+#ifdef LDAP_CRYPT
+	extern pthread_mutex_t crypt_mutex;
+#endif /* LDAP_CRYPT */
+
 	/* allocate backend-specific stuff */
 	li = (struct ldbminfo *) ch_calloc( 1, sizeof(struct ldbminfo) );
 
@@ -59,6 +65,9 @@ ldbm_back_init(
 	pthread_mutex_init( &li->li_cache.c_mutex, pthread_mutexattr_default );
 	pthread_mutex_init( &li->li_nextid_mutex, pthread_mutexattr_default );
 	pthread_mutex_init( &li->li_dbcache_mutex, pthread_mutexattr_default );
+#ifdef LDAP_CRYPT
+	pthread_mutex_init( &crypt_mutex, pthread_mutexattr_default );
+#endif /* LDAP_CRYPT */
 	pthread_cond_init( &li->li_dbcache_cv, pthread_condattr_default );
 	for ( i = 0; i < MAXDBCACHE; i++ ) {
 		pthread_mutex_init( &li->li_dbcache[i].dbc_mutex,
diff --git a/servers/slapd/back-ldbm/kerberos.c b/servers/slapd/back-ldbm/kerberos.c
index d07138bed7f6fd5747087fedab09e40914328fd6..c369c2bc1394b30d7426650c98bbc9c040b6c3ef 100644
--- a/servers/slapd/back-ldbm/kerberos.c
+++ b/servers/slapd/back-ldbm/kerberos.c
@@ -1,5 +1,7 @@
 /* kerberos.c - ldbm backend kerberos bind routines */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-ldbm/modify.c b/servers/slapd/back-ldbm/modify.c
index df9414673648bb609383319d89d8c3ae97c2dfee..455dd8c175f3d087a50b82a6439c43940efaa9fe 100644
--- a/servers/slapd/back-ldbm/modify.c
+++ b/servers/slapd/back-ldbm/modify.c
@@ -1,5 +1,7 @@
 /* modify.c - ldbm backend modify routine */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-ldbm/modrdn.c b/servers/slapd/back-ldbm/modrdn.c
index 26c96709aed041d384cd3dd9a4588f6c5cab1638..62a2acd18bf626c694c0a2a6c47fa161865a7824 100644
--- a/servers/slapd/back-ldbm/modrdn.c
+++ b/servers/slapd/back-ldbm/modrdn.c
@@ -1,5 +1,7 @@
 /* modrdn.c - ldbm backend modrdn routine */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-ldbm/nextid.c b/servers/slapd/back-ldbm/nextid.c
index e51f4d84e76ce4de24f4dc6a4819befdc4cc3deb..b680890a79ef7e71ec190608971d7e265b7a7c73 100644
--- a/servers/slapd/back-ldbm/nextid.c
+++ b/servers/slapd/back-ldbm/nextid.c
@@ -1,5 +1,7 @@
 /* id.c - keep track of the next id to be given out */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
diff --git a/servers/slapd/back-ldbm/proto-back-ldbm.h b/servers/slapd/back-ldbm/proto-back-ldbm.h
index 1bc9fdd0e1a686631316c8761af7211a17d1d94d..371e8bf9c22bf553189d58ec81aef9ab482d8a83 100644
--- a/servers/slapd/back-ldbm/proto-back-ldbm.h
+++ b/servers/slapd/back-ldbm/proto-back-ldbm.h
@@ -1,112 +1,117 @@
 #ifndef _PROTO_BACK_LDBM
 #define _PROTO_BACK_LDBM
 
+#include <ldap_cdefs.h>
+
+LDAP_BEGIN_DECL
+
 /*
  * attr.c
  */
 
-void attr_masks( struct ldbminfo *li, char *type, int *indexmask,
- int *syntaxmask );
-void attr_index_config( struct ldbminfo *li, char *fname, int lineno,
- int argc, char **argv, int init );
+void attr_masks LDAP_P(( struct ldbminfo *li, char *type, int *indexmask,
+ int *syntaxmask ));
+void attr_index_config LDAP_P(( struct ldbminfo *li, char *fname, int lineno,
+ int argc, char **argv, int init ));
 
 /*
  * cache.c
  */
 
-void cache_set_state( struct cache *cache, Entry *e, int state );
-void cache_return_entry_r( struct cache *cache, Entry *e );
-void cache_return_entry_w( struct cache *cache, Entry *e );
-int cache_add_entry_lock( struct cache *cache, Entry *e, int state );
-ID cache_find_entry_dn2id( Backend *be, struct cache *cache, char *dn );
-Entry * cache_find_entry_id( struct cache *cache, ID id, int rw );
-int cache_delete_entry( struct cache *cache, Entry *e );
+void cache_set_state LDAP_P(( struct cache *cache, Entry *e, int state ));
+void cache_return_entry_r LDAP_P(( struct cache *cache, Entry *e ));
+void cache_return_entry_w LDAP_P(( struct cache *cache, Entry *e ));
+int cache_add_entry_lock LDAP_P(( struct cache *cache, Entry *e, int state ));
+ID cache_find_entry_dn2id LDAP_P(( Backend *be, struct cache *cache, char *dn ));
+Entry * cache_find_entry_id LDAP_P(( struct cache *cache, ID id, int rw ));
+int cache_delete_entry LDAP_P(( struct cache *cache, Entry *e ));
 
 /*
  * dbcache.c
  */
 
-struct dbcache * ldbm_cache_open( Backend *be, char *name, char *suffix,
- int flags );
-void ldbm_cache_close( Backend *be, struct dbcache *db );
-void ldbm_cache_flush_all( Backend *be );
-Datum ldbm_cache_fetch( struct dbcache *db, Datum key );
-int ldbm_cache_store( struct dbcache *db, Datum key, Datum data, int flags );
-int ldbm_cache_delete( struct dbcache *db, Datum key );
+struct dbcache * ldbm_cache_open LDAP_P(( Backend *be, char *name, char *suffix,
+ int flags ));
+void ldbm_cache_close LDAP_P(( Backend *be, struct dbcache *db ));
+void ldbm_cache_flush_all LDAP_P(( Backend *be ));
+Datum ldbm_cache_fetch LDAP_P(( struct dbcache *db, Datum key ));
+int ldbm_cache_store LDAP_P(( struct dbcache *db, Datum key, Datum data, int flags ));
+int ldbm_cache_delete LDAP_P(( struct dbcache *db, Datum key ));
 
 /*
  * dn2id.c
  */
 
-int dn2id_add( Backend *be, char *dn, ID id );
-ID dn2id( Backend *be, char *dn );
-int dn2id_delete( Backend *be, char *dn );
-/*Entry * dn2entry( Backend *be, char *dn, char **matched );*/
-Entry * dn2entry_r( Backend *be, char *dn, char **matched );
-Entry * dn2entry_w( Backend *be, char *dn, char **matched );
+int dn2id_add LDAP_P(( Backend *be, char *dn, ID id ));
+ID dn2id LDAP_P(( Backend *be, char *dn ));
+int dn2id_delete LDAP_P(( Backend *be, char *dn ));
+/*Entry * dn2entry LDAP_P(( Backend *be, char *dn, char **matched ));*/
+Entry * dn2entry_r LDAP_P(( Backend *be, char *dn, char **matched ));
+Entry * dn2entry_w LDAP_P(( Backend *be, char *dn, char **matched ));
 
 /*
  * filterindex.c
  */
 
-IDList * filter_candidates( Backend *be, Filter *f );
+IDList * filter_candidates LDAP_P(( Backend *be, Filter *f ));
 
 /*
  * id2children.c
  */
 
-int id2children_add( Backend *be, Entry *p, Entry *e );
-int has_children( Backend *be, Entry *p );
+int id2children_add LDAP_P(( Backend *be, Entry *p, Entry *e ));
+int has_children LDAP_P(( Backend *be, Entry *p ));
 
 /*
  * id2entry.c
  */
 
-int id2entry_add( Backend *be, Entry *e );
-int id2entry_delete( Backend *be, Entry *e );
-Entry * id2entry( Backend *be, ID id, int rw ); 
-Entry * id2entry_r( Backend *be, ID id );
-Entry * id2entry_w( Backend *be, ID id );
+int id2entry_add LDAP_P(( Backend *be, Entry *e ));
+int id2entry_delete LDAP_P(( Backend *be, Entry *e ));
+Entry * id2entry LDAP_P(( Backend *be, ID id, int rw )); 
+Entry * id2entry_r LDAP_P(( Backend *be, ID id ));
+Entry * id2entry_w LDAP_P(( Backend *be, ID id ));
 
 /*
  * idl.c
  */
 
-IDList * idl_alloc( int nids );
-IDList * idl_allids( Backend *be );
-void idl_free( IDList *idl );
-IDList * idl_fetch( Backend *be, struct dbcache *db, Datum key );
-int idl_insert_key( Backend *be, struct dbcache *db, Datum key, ID id );
-int idl_insert( IDList **idl, ID id, int maxids );
-IDList * idl_intersection( Backend *be, IDList *a, IDList *b );
-IDList * idl_union( Backend *be, IDList *a, IDList *b );
-IDList * idl_notin( Backend *be, IDList *a, IDList *b );
-ID idl_firstid( IDList *idl );
-ID idl_nextid( IDList *idl, ID id );
+IDList * idl_alloc LDAP_P(( int nids ));
+IDList * idl_allids LDAP_P(( Backend *be ));
+void idl_free LDAP_P(( IDList *idl ));
+IDList * idl_fetch LDAP_P(( Backend *be, struct dbcache *db, Datum key ));
+int idl_insert_key LDAP_P(( Backend *be, struct dbcache *db, Datum key, ID id ));
+int idl_insert LDAP_P(( IDList **idl, ID id, int maxids ));
+IDList * idl_intersection LDAP_P(( Backend *be, IDList *a, IDList *b ));
+IDList * idl_union LDAP_P(( Backend *be, IDList *a, IDList *b ));
+IDList * idl_notin LDAP_P(( Backend *be, IDList *a, IDList *b ));
+ID idl_firstid LDAP_P(( IDList *idl ));
+ID idl_nextid LDAP_P(( IDList *idl, ID id ));
 
 /*
  * index.c
  */
 
-int index_add_entry( Backend *be, Entry *e );
-int index_add_mods( Backend *be, LDAPMod *mods, ID id );
-IDList * index_read( Backend *be, char *type, int indextype, char *val );
-int index_add_values( Backend *be, char *type, struct berval **vals, ID  id );
+int index_add_entry LDAP_P(( Backend *be, Entry *e ));
+int index_add_mods LDAP_P(( Backend *be, LDAPMod *mods, ID id ));
+IDList * index_read LDAP_P(( Backend *be, char *type, int indextype, char *val ));
+int index_add_values LDAP_P(( Backend *be, char *type, struct berval **vals, ID  id ));
 
 /*
  * kerberos.c
  */
 
-#ifdef KERBEROS
-/* krbv4_ldap_auth( Backend *be, struct berval *cred, AUTH_DAT *ad ); */
+#ifdef HAVE_KERBEROS
+/* krbv4_ldap_auth LDAP_P(( Backend *be, struct berval *cred, AUTH_DAT *ad )); */
 #endif
 
 /*
  * nextid.c
  */
 
-ID next_id( Backend *be );
-void next_id_return( Backend *be, ID id );
-ID next_id_get( Backend *be );
+ID next_id LDAP_P(( Backend *be ));
+void next_id_return LDAP_P(( Backend *be, ID id ));
+ID next_id_get LDAP_P(( Backend *be ));
 
+LDAP_END_DECL
 #endif
diff --git a/servers/slapd/back-ldbm/search.c b/servers/slapd/back-ldbm/search.c
index 19dafe4c017680d060d5da3af5f0a0d04d1c0c77..2bf35034bfa4bca8a7b5906031908a4ee643337a 100644
--- a/servers/slapd/back-ldbm/search.c
+++ b/servers/slapd/back-ldbm/search.c
@@ -1,5 +1,7 @@
 /* search.c - ldbm backend search function */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-ldbm/unbind.c b/servers/slapd/back-ldbm/unbind.c
index 9f3421d5ee6ed53a3d6020dee01696d8319312a6..56e3e426a4d3cd655db71615d58690c1705cf8a6 100644
--- a/servers/slapd/back-ldbm/unbind.c
+++ b/servers/slapd/back-ldbm/unbind.c
@@ -1,5 +1,7 @@
 /* unbind.c - handle an ldap unbind operation */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
diff --git a/servers/slapd/back-passwd/config.c b/servers/slapd/back-passwd/config.c
index 5a26ff0fb61e6a401c5fb522ef8ca5d45815f1a7..252717ab7307108b8f636ad979d37e19d1a38c7e 100644
--- a/servers/slapd/back-passwd/config.c
+++ b/servers/slapd/back-passwd/config.c
@@ -1,5 +1,7 @@
 /* config.c - passwd backend configuration file routine */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/time.h>
diff --git a/servers/slapd/back-passwd/search.c b/servers/slapd/back-passwd/search.c
index 11c1d8e7192424712a5acd60e0e9f6592fc0a722..3e9a0f9a8c8dbcc63a19d1ccc398db95068461b5 100644
--- a/servers/slapd/back-passwd/search.c
+++ b/servers/slapd/back-passwd/search.c
@@ -1,5 +1,7 @@
 /* search.c - /etc/passwd backend search function */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/time.h>
diff --git a/servers/slapd/back-shell/abandon.c b/servers/slapd/back-shell/abandon.c
index fc9d6a40b6162512a9c620e67aef97f72a87c083..090ba26a4b1bac8c11df93c3b87dd29c142caede 100644
--- a/servers/slapd/back-shell/abandon.c
+++ b/servers/slapd/back-shell/abandon.c
@@ -1,5 +1,7 @@
 /* abandon.c - shell backend abandon function */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-shell/add.c b/servers/slapd/back-shell/add.c
index 89f054cc9c3d78d76f380d202f26613f595ab448..e9840e9eaa72194f17202752e39471e1e845a316 100644
--- a/servers/slapd/back-shell/add.c
+++ b/servers/slapd/back-shell/add.c
@@ -1,5 +1,7 @@
 /* add.c - shell backend add function */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-shell/bind.c b/servers/slapd/back-shell/bind.c
index dea149e4106fc4779706c3a065000fd7e976a1e5..2d8e5a0a133dd3e910acbf3259fa424798f2029c 100644
--- a/servers/slapd/back-shell/bind.c
+++ b/servers/slapd/back-shell/bind.c
@@ -1,5 +1,7 @@
 /* bind.c - shell backend bind function */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-shell/compare.c b/servers/slapd/back-shell/compare.c
index 48dfbb553be6719c9b9c9e9d858a5f80e2996f3f..642524f04d60b360dec748c3f89e2c1932b322ba 100644
--- a/servers/slapd/back-shell/compare.c
+++ b/servers/slapd/back-shell/compare.c
@@ -1,5 +1,7 @@
 /* compare.c - shell backend compare function */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-shell/config.c b/servers/slapd/back-shell/config.c
index 5d2fa1c19a4299a386eaa4703ddc16486a8b00d0..fc22aac49a3d8ceda0ce7eeb8a48b08c9d5b5126 100644
--- a/servers/slapd/back-shell/config.c
+++ b/servers/slapd/back-shell/config.c
@@ -1,5 +1,7 @@
 /* config.c - shell backend configuration file routine */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-shell/delete.c b/servers/slapd/back-shell/delete.c
index 0dc3439c008ce53ea77a642f8cafa2313585a2f6..e794a23a62486a59e81edccff8c1045d58fa69ae 100644
--- a/servers/slapd/back-shell/delete.c
+++ b/servers/slapd/back-shell/delete.c
@@ -1,5 +1,7 @@
 /* delete.c - shell backend delete function */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-shell/fork.c b/servers/slapd/back-shell/fork.c
index aff0e5bbe446778c06ec249e3312b0d435c077b5..1b25031cbb3c61bc0a352028913afeb58fc0c39f 100644
--- a/servers/slapd/back-shell/fork.c
+++ b/servers/slapd/back-shell/fork.c
@@ -1,5 +1,7 @@
 /* fork.c - fork and exec a process, connecting stdin/out w/pipes */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-shell/init.c b/servers/slapd/back-shell/init.c
index 1df9c4b265d8a6c49674cfc406a10d3e8c1635f9..b4087cb8a88294639d405d9229aa07dfb5a8fbf4 100644
--- a/servers/slapd/back-shell/init.c
+++ b/servers/slapd/back-shell/init.c
@@ -1,5 +1,7 @@
 /* init.c - initialize shell backend */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
diff --git a/servers/slapd/back-shell/modify.c b/servers/slapd/back-shell/modify.c
index d05b79639df7f024d2c95461e996608f6aefa6fe..b61dc8234d958cd506d64ef9587378e629b6b0f3 100644
--- a/servers/slapd/back-shell/modify.c
+++ b/servers/slapd/back-shell/modify.c
@@ -1,5 +1,7 @@
 /* modify.c - shell backend modify function */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-shell/modrdn.c b/servers/slapd/back-shell/modrdn.c
index 9fd793317d19e6de0f0cf1d64722c60f4047512e..a4735164d0b1909a0ca4df0efe8c04af37176e98 100644
--- a/servers/slapd/back-shell/modrdn.c
+++ b/servers/slapd/back-shell/modrdn.c
@@ -1,5 +1,7 @@
 /* modrdn.c - shell backend modrdn function */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-shell/result.c b/servers/slapd/back-shell/result.c
index 675aa1f144ced8a90503c7992ba1ab3c10d18605..f1ac898526c3edb1a817f82eb1f19e3f2234c675 100644
--- a/servers/slapd/back-shell/result.c
+++ b/servers/slapd/back-shell/result.c
@@ -1,5 +1,7 @@
 /* result.c - shell backend result reading function */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-shell/search.c b/servers/slapd/back-shell/search.c
index 749b112a9401238704bb7b9c4fc5606483ef1cc7..cbc3243d2128c8eacad24955f3c233d5a7b857c4 100644
--- a/servers/slapd/back-shell/search.c
+++ b/servers/slapd/back-shell/search.c
@@ -1,5 +1,7 @@
 /* search.c - shell backend search function */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/back-shell/shell.h b/servers/slapd/back-shell/shell.h
index 5ef493c0458ac4e7b4c9304156ed74f629d79341..6228b6408e62c85756d73506a649e14444c48314 100644
--- a/servers/slapd/back-shell/shell.h
+++ b/servers/slapd/back-shell/shell.h
@@ -1,5 +1,12 @@
 /* shell.h - shell backend header file */
 
+#ifndef SLAPD_SHELL_H
+#define SLAPD_SHELL_H
+
+#include <ldap_cdefs.h>
+
+LDAP_BEGIN_DECL
+
 struct shellinfo {
 	char	**si_bind;	/* cmd + args to exec for bind	  */
 	char	**si_unbind;	/* cmd + args to exec for unbind  */
@@ -11,3 +18,7 @@ struct shellinfo {
 	char	**si_delete;	/* cmd + args to exec for delete  */
 	char	**si_abandon;	/* cmd + args to exec for abandon */
 };
+
+LDAP_END_DECL
+
+#endif
diff --git a/servers/slapd/back-shell/unbind.c b/servers/slapd/back-shell/unbind.c
index 649fe96c137703e0759a2992aa6663f11622e19f..df836f3e8f0143a782cf7fde5ebe5d703d977ce8 100644
--- a/servers/slapd/back-shell/unbind.c
+++ b/servers/slapd/back-shell/unbind.c
@@ -1,5 +1,7 @@
 /* unbind.c - shell backend unbind function */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/backend.c b/servers/slapd/backend.c
index 998bfa8c6acc215d0e6a1240cb494210580d55a1..93675af31b8221df1832056bd9485914f64658b4 100644
--- a/servers/slapd/backend.c
+++ b/servers/slapd/backend.c
@@ -1,6 +1,8 @@
 /* backend.c - routines for dealing with back-end databases */
 
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -21,6 +23,7 @@ extern int	ldbm_back_abandon();
 extern int	ldbm_back_config();
 extern int	ldbm_back_init();
 extern int	ldbm_back_close();
+extern int      ldbm_back_group();
 #endif
 
 #ifdef LDAP_PASSWD
@@ -86,6 +89,9 @@ new_backend(
 		be->be_config = ldbm_back_config;
 		be->be_init = ldbm_back_init;
 		be->be_close = ldbm_back_close;
+#ifdef ACLGROUP
+		be->be_group = ldbm_back_group;
+#endif
 		be->be_type = "ldbm";
 		foundit = 1;
 	}
@@ -105,6 +111,9 @@ new_backend(
 		be->be_config = passwd_back_config;
 		be->be_init = NULL;
 		be->be_close = NULL;
+#ifdef ACLGROUP
+		be->be_group = NULL;
+#endif
 		be->be_type = "passwd";
 		foundit = 1;
 	}
@@ -124,6 +133,9 @@ new_backend(
 		be->be_config = shell_back_config;
 		be->be_init = shell_back_init;
 		be->be_close = NULL;
+#ifdef ACLGROUP
+		be->be_group = NULL;
+#endif
 		be->be_type = "shell";
 		foundit = 1;
 	}
@@ -149,7 +161,20 @@ select_backend( char * dn )
 	dnlen = strlen( dn );
 	for ( i = 0; i < nbackends; i++ ) {
 		for ( j = 0; backends[i].be_suffix != NULL &&
-		    backends[i].be_suffix[j] != NULL; j++ ) {
+		    backends[i].be_suffix[j] != NULL; j++ )
+		{
+#ifdef LDAP_ALLOW_NULL_SEARCH_BASE
+			/* Add greg@greg.rim.or.jp
+			 * It's quick hack for cheap client
+			 * Some browser offer a NULL base at ldap_search
+			 */
+			if(dnlen == 0) {
+				Debug( LDAP_DEBUG_TRACE,
+					"select_backend: use default backend\n", 0, 0, 0 );
+				return (&backends[i]);
+			}
+#endif /* LDAP_ALLOW_NULL_SEARCH_BASE */
+
 			len = strlen( backends[i].be_suffix[j] );
 
 			if ( len > dnlen ) {
@@ -231,3 +256,14 @@ be_unbind(
 		}
 	}
 }
+
+#ifdef ACLGROUP
+int 
+be_group(Backend *be, char *bdn, char *edn)
+{
+        if (be->be_group)
+                return(be->be_group(be, bdn, edn));
+        else
+                return(1);
+}
+#endif
diff --git a/servers/slapd/bind.c b/servers/slapd/bind.c
index 562ce5b54ab6c08665074201735a7ab76cc798a2..4fa847853540897df6232299bade1f27978846bf 100644
--- a/servers/slapd/bind.c
+++ b/servers/slapd/bind.c
@@ -12,6 +12,8 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/ch_malloc.c b/servers/slapd/ch_malloc.c
index 43ec4c9adb08ebd63a0eaec3e9967f9bc3252379..9f5cf5607356a9f95b24d693c043a465d67fe8b3 100644
--- a/servers/slapd/ch_malloc.c
+++ b/servers/slapd/ch_malloc.c
@@ -1,5 +1,7 @@
 /* ch_malloc.c - malloc routines that test returns from malloc and friends */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
diff --git a/servers/slapd/charray.c b/servers/slapd/charray.c
index b731cf1e9021fe81d93ee6cc3b79e2dffdcdacfe..71ef9e63451816b70340c260cee310a0d41d4a92 100644
--- a/servers/slapd/charray.c
+++ b/servers/slapd/charray.c
@@ -1,5 +1,7 @@
 /* charray.c - routines for dealing with char * arrays */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -113,6 +115,9 @@ str2charray( char *str, char *brkstr )
 	char	*s;
 	int	i;
 
+	/* protect the input string from strtok */
+	str = strdup( str );
+
 	i = 1;
 	for ( s = str; *s; s++ ) {
 		if ( strchr( brkstr, *s ) != NULL ) {
@@ -128,5 +133,6 @@ str2charray( char *str, char *brkstr )
 	}
 	res[i] = NULL;
 
+	free( str );
 	return( res );
 }
diff --git a/servers/slapd/compare.c b/servers/slapd/compare.c
index 248c8b1ec7f9884cb49845af0d65abf065bf84a7..d836a4336542a1e2714f1330f66cc738744fc684 100644
--- a/servers/slapd/compare.c
+++ b/servers/slapd/compare.c
@@ -10,6 +10,8 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
diff --git a/servers/slapd/config.c b/servers/slapd/config.c
index 66050586ab794ac4f4e258306e58de0e293206ac..55660182c6388f8c88c675129ae22adc0a27f655 100644
--- a/servers/slapd/config.c
+++ b/servers/slapd/config.c
@@ -1,5 +1,7 @@
 /* config.c - configuration file handling routines */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/configinfo.c b/servers/slapd/configinfo.c
index c1719efde8a5d300bfdee1b767756a92e5d9bfef..f6dcfe9d5fa26e304b1e02f4d1232fe5cf5cb717 100644
--- a/servers/slapd/configinfo.c
+++ b/servers/slapd/configinfo.c
@@ -10,6 +10,8 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/connection.c b/servers/slapd/connection.c
index 1b2bd41ed7e58d5af4d588b8f2bcdcc9b3dd4543..bbfdd4344e5cef97d2feee0230c33b908f7136df 100644
--- a/servers/slapd/connection.c
+++ b/servers/slapd/connection.c
@@ -1,4 +1,3 @@
-#define DISABLE_BRIDGE
 #include "portable.h"
 
 #include <stdio.h>
diff --git a/servers/slapd/daemon.c b/servers/slapd/daemon.c
index c15c58b24a4c89aba1e42001fc11b9ace951f251..1dfc8682b16c1793dcce66210ad2922db1a253af 100644
--- a/servers/slapd/daemon.c
+++ b/servers/slapd/daemon.c
@@ -5,7 +5,6 @@
  *	Added locking of new_conn_mutex when traversing the c[] array.
  */
 
-#define DISABLE_BRIDGE /* disable bridge code */
 #include "portable.h"
 
 #include <stdio.h>
diff --git a/servers/slapd/delete.c b/servers/slapd/delete.c
index bbab47806c0d7d24d7181125d39cfc485bdc898f..bab22d924f10210e19f5da8dfcf7e5f8bb915bde 100644
--- a/servers/slapd/delete.c
+++ b/servers/slapd/delete.c
@@ -10,6 +10,8 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/detach.c b/servers/slapd/detach.c
index 7b3fdab57ae97fc181fae0452a6ee4c3cd04b1de..bb28bc96a2d9fd156d1c7322e12d86e31e5b909e 100644
--- a/servers/slapd/detach.c
+++ b/servers/slapd/detach.c
@@ -10,6 +10,8 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/types.h>
 #ifdef SVR4
@@ -39,6 +41,12 @@ detach()
 	nbits = getdtablesize();
 #endif /* USE_SYSCONF */
 
+#ifdef FD_SETSIZE
+	if ( nbits > FD_SETSIZE ) {
+		nbits = FD_SETSIZE;
+	}
+#endif /* FD_SETSIZE */
+
 #ifdef LDAP_DEBUG
 	if ( ldap_debug == 0 ) {
 #endif
diff --git a/servers/slapd/dn.c b/servers/slapd/dn.c
index 226937efa99f521a9c0cf0d6ef98deeb631f7fe7..de10e368c70ba5b7779aad604c92da04bf5d846d 100644
--- a/servers/slapd/dn.c
+++ b/servers/slapd/dn.c
@@ -1,12 +1,13 @@
 /* dn.c - routines for dealing with distinguished names */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <ctype.h>
 #include <string.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#include "portable.h"
 #include "slap.h"
 
 static char	**dn_explode();
@@ -206,7 +207,7 @@ dn_parent(
 		}
 	}
 
-	return( NULL );
+	return( strdup("") );
 }
 
 /*
diff --git a/servers/slapd/entry.c b/servers/slapd/entry.c
index 72ef4b168452dac77cf2d763c1ad4dbe3bfd487b..562cc47b689e05c24d1ae0582c5c33a6c1c0f310 100644
--- a/servers/slapd/entry.c
+++ b/servers/slapd/entry.c
@@ -1,5 +1,7 @@
 /* entry.c - routines for dealing with entries */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
diff --git a/servers/slapd/filter.c b/servers/slapd/filter.c
index 8d990b361d4b42ce5f137437199b1acb78858d97..49f8166f2b04c65d1700107a56fd8f77c8ba89b3 100644
--- a/servers/slapd/filter.c
+++ b/servers/slapd/filter.c
@@ -1,5 +1,7 @@
 /* filter.c - routines for parsing and dealing with filters */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -135,6 +137,7 @@ get_filter( Connection *conn, BerElement *ber, Filter **filt, char **fstr )
 		Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
 		if ( (err = get_filter_list( conn, ber, &f->f_and, &ftmp ))
 		    == 0 ) {
+			if (ftmp == NULL) ftmp = strdup("");
 			*fstr = ch_malloc( 4 + strlen( ftmp ) );
 			sprintf( *fstr, "(&%s)", ftmp );
 			free( ftmp );
@@ -145,6 +148,7 @@ get_filter( Connection *conn, BerElement *ber, Filter **filt, char **fstr )
 		Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
 		if ( (err = get_filter_list( conn, ber, &f->f_or, &ftmp ))
 		    == 0 ) {
+			if (ftmp == NULL) ftmp = strdup("");
 			*fstr = ch_malloc( 4 + strlen( ftmp ) );
 			sprintf( *fstr, "(|%s)", ftmp );
 			free( ftmp );
@@ -155,6 +159,7 @@ get_filter( Connection *conn, BerElement *ber, Filter **filt, char **fstr )
 		Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
 		(void) ber_skip_tag( ber, &len );
 		if ( (err = get_filter( conn, ber, &f->f_not, &ftmp )) == 0 ) {
+			if (ftmp == NULL) ftmp = strdup("");
 			*fstr = ch_malloc( 4 + strlen( ftmp ) );
 			sprintf( *fstr, "(!%s)", ftmp );
 			free( ftmp );
diff --git a/servers/slapd/filterentry.c b/servers/slapd/filterentry.c
index 712ff22e2e1751ede97126547147dc6d3c796776..495fcb2ca624b324b545c592dcc215e0da2d4a20 100644
--- a/servers/slapd/filterentry.c
+++ b/servers/slapd/filterentry.c
@@ -1,25 +1,18 @@
 /* filterentry.c - apply a filter to an entry */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#ifdef sunos5
-#include "regexpr.h"
-#else
-#include "regex.h"
-#endif
+#include <regex.h>
 #include "slap.h"
 
 extern Attribute	*attr_find();
 extern char		*first_word();
 extern char		*next_word();
 extern char		*phonetic();
-extern char		*re_comp();
-
-#ifndef sunos5
-extern pthread_mutex_t	regex_mutex;
-#endif
 
 static int	test_filter_list();
 static int	test_substring_filter();
@@ -223,11 +216,12 @@ test_approx_filter(
 			    w2 = next_word( w2 ) ) {
 				c2 = phonetic( w2 );
 				if ( strcmp( c1, c2 ) == 0 ) {
+					free( c2 );
 					break;
 				}
+				free( c2 );
 			}
 			free( c1 );
-			free( c2 );
 
 			/*
 			 * if we stopped because we ran out of words
@@ -322,6 +316,7 @@ test_substring_filter(
 	char		pat[BUFSIZ];
 	char		buf[BUFSIZ];
 	struct berval	*val;
+	regex_t		re;
 
 	Debug( LDAP_DEBUG_FILTER, "begin test_substring_filter\n", 0, 0, 0 );
 
@@ -389,19 +384,16 @@ test_substring_filter(
 	}
 
 	/* compile the regex */
-#ifdef sunos5
-	if ( (p = compile( pat, NULL, NULL )) == NULL ) {
-		Debug( LDAP_DEBUG_ANY, "compile failed (%s)\n", p, 0, 0 );
+	Debug( LDAP_DEBUG_FILTER, "test_substring_filter: regcomp pat: %s\n",
+		pat, 0, 0 );
+	if ((rc = regcomp(&re, pat, 0))) {
+		char error[512];
+
+		regerror(rc, &re, error, sizeof(error));
+		Debug( LDAP_DEBUG_ANY, "regcomp failed (%s) %s\n",
+			p, error, 0 );
 		return( -1 );
 	}
-#else /* sunos5 */
-	pthread_mutex_lock( &regex_mutex );
-	if ( (p = re_comp( pat )) != 0 ) {
-		Debug( LDAP_DEBUG_ANY, "re_comp failed (%s)\n", p, 0, 0 );
-		pthread_mutex_unlock( &regex_mutex );
-		return( -1 );
-	}
-#endif /* sunos5 */
 
 	/* for each value in the attribute see if regex matches */
 	for ( i = 0; a->a_vals[i] != NULL; i++ ) {
@@ -417,29 +409,18 @@ test_substring_filter(
 		}
 		value_normalize( realval, a->a_syntax );
 
-#ifdef sunos5
-		rc = step( realval, p );
-#else /* sunos5 */
-		rc = re_exec( realval );
-#endif /* sunos5 */
+		rc = !regexec(&re, realval, 0, NULL, 0);
 
 		if ( tmp != NULL ) {
 			free( tmp );
 		}
 		if ( rc == 1 ) {
-#ifdef sunos5
-			free( p );
-#else /* sunos5 */
-			pthread_mutex_unlock( &regex_mutex );
-#endif /* sunos5 */
+			regfree(&re);
 			return( 0 );
 		}
 	}
-#ifdef sunos5
-	free( p );
-#else /* sunos5 */
-	pthread_mutex_unlock( &regex_mutex );
-#endif /* sunos5 */
+
+	regfree(&re);
 
 	Debug( LDAP_DEBUG_FILTER, "end test_substring_filter 1\n", 0, 0, 0 );
 	return( 1 );
diff --git a/servers/slapd/init.c b/servers/slapd/init.c
index 03dd850f0e86c8e92e6fa4371741334114c32e9f..7ba83187584a30403971bd7362ae2e87bf6225f1 100644
--- a/servers/slapd/init.c
+++ b/servers/slapd/init.c
@@ -1,5 +1,7 @@
 /* init.c - initialize various things */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/time.h>
@@ -18,9 +20,6 @@ extern pthread_mutex_t	entry2str_mutex;
 extern pthread_mutex_t	replog_mutex;
 extern pthread_mutex_t	ops_mutex;
 extern pthread_mutex_t	num_sent_mutex;
-#ifndef sunos5
-extern pthread_mutex_t	regex_mutex;
-#endif
 
 init()
 {
@@ -31,7 +30,4 @@ init()
 	pthread_mutex_init( &replog_mutex, pthread_mutexattr_default );
 	pthread_mutex_init( &ops_mutex, pthread_mutexattr_default );
 	pthread_mutex_init( &num_sent_mutex, pthread_mutexattr_default );
-#ifndef sunos5
-	pthread_mutex_init( &regex_mutex, pthread_mutexattr_default );
-#endif
 }
diff --git a/servers/slapd/lock.c b/servers/slapd/lock.c
index 305fb5b91ce0d346275ad6faf836fbd13d8d4b97..2e3cb917fda37569985384d9a0a640f242518961 100644
--- a/servers/slapd/lock.c
+++ b/servers/slapd/lock.c
@@ -1,5 +1,7 @@
 /* lock.c - routines to open and apply an advisory lock to a file */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/time.h>
 #include <sys/types.h>
diff --git a/servers/slapd/main.c b/servers/slapd/main.c
index 01930b970910c9c1ad6cd9a8bf6939af58baed05..258923e61efb1573dd8ad97d576195e92ae3ef1c 100644
--- a/servers/slapd/main.c
+++ b/servers/slapd/main.c
@@ -1,3 +1,5 @@
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -10,16 +12,17 @@
 #include "slap.h"
 #include "ldapconfig.h"
 
-extern void	daemon();
+extern void	slapd_daemon();
 extern int	lber_debug;
 
 extern char Versionstr[];
 
+
 /*
  * read-only global variables or variables only written by the listener
  * thread (after they are initialized) - no need to protect them with a mutex.
  */
-int		ldap_debug;
+int		ldap_debug = 0;
 #ifdef LDAP_DEBUG
 int		ldap_syslog = LDAP_DEBUG_STATS;
 #else
@@ -55,15 +58,12 @@ pthread_mutex_t	num_sent_mutex;
  */
 pthread_mutex_t	entry2str_mutex;
 pthread_mutex_t	replog_mutex;
-#ifndef sunos5
-pthread_mutex_t	regex_mutex;
-#endif
 
 static
 usage( name )
     char	*name;
 {
-	fprintf( stderr, "usage: %s [-d debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]\n", name );
+	fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]\n", name );
 }
 
 main( argc, argv )
@@ -105,19 +105,19 @@ main( argc, argv )
 				    LDAP_DEBUG_CONFIG );
 				printf( "\tLDAP_DEBUG_ACL\t\t%d\n",
 				    LDAP_DEBUG_ACL );
-				printf( "\tLDAP_DEBUG_STATS\t\t%d\n",
+				printf( "\tLDAP_DEBUG_STATS\t%d\n",
 				    LDAP_DEBUG_STATS );
-				printf( "\tLDAP_DEBUG_STATS2\t\t%d\n",
+				printf( "\tLDAP_DEBUG_STATS2\t%d\n",
 				    LDAP_DEBUG_STATS2 );
-				printf( "\tLDAP_DEBUG_SHELL\t\t%d\n",
+				printf( "\tLDAP_DEBUG_SHELL\t%d\n",
 				    LDAP_DEBUG_SHELL );
-				printf( "\tLDAP_DEBUG_PARSE\t\t%d\n",
+				printf( "\tLDAP_DEBUG_PARSE\t%d\n",
 				    LDAP_DEBUG_PARSE );
 				printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
 				    LDAP_DEBUG_ANY );
 				exit( 0 );
 			} else {
-				ldap_debug = atoi( optarg );
+				ldap_debug |= atoi( optarg );
 				lber_debug = (ldap_debug & LDAP_DEBUG_BER);
 			}
 			break;
@@ -184,12 +184,27 @@ main( argc, argv )
 		pthread_attr_init( &attr );
 		pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
 
-		if ( pthread_create( &listener_tid, attr, (void *) daemon,
+#ifndef THREAD_MIT_PTHREADS
+		/* POSIX_THREADS or compatible
+		 * This is a draft 10 or standard pthreads implementation
+		 */
+		if ( pthread_create( &listener_tid, &attr, (void *) slapd_daemon,
+		    (void *) port ) != 0 ) {
+			Debug( LDAP_DEBUG_ANY,
+			    "listener pthread_create failed\n", 0, 0, 0 );
+			exit( 1 );
+		}
+#else	/* !THREAD_MIT_PTHREADS */
+		/*
+		 * This is a draft 4 or earlier pthreads implementation
+		 */
+		if ( pthread_create( &listener_tid, attr, (void *) slapd_daemon,
 		    (void *) port ) != 0 ) {
 			Debug( LDAP_DEBUG_ANY,
 			    "listener pthread_create failed\n", 0, 0, 0 );
 			exit( 1 );
 		}
+#endif	/* !THREAD_MIT_PTHREADS */
 		pthread_attr_destroy( &attr );
 		pthread_join( listener_tid, (void *) &status );
 		pthread_exit( 0 );
diff --git a/servers/slapd/modify.c b/servers/slapd/modify.c
index dd7689cfaf9bc23f031ea5607917c4211d62fe6e..8c0351046049f65a382f3eea426cf746c6054610 100644
--- a/servers/slapd/modify.c
+++ b/servers/slapd/modify.c
@@ -10,6 +10,8 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
diff --git a/servers/slapd/modrdn.c b/servers/slapd/modrdn.c
index 802dda55acb4cf4f20fe02cde7a5b0ce69cd4d9e..75af6e13e832efb40987ecb1e73204019d617203 100644
--- a/servers/slapd/modrdn.c
+++ b/servers/slapd/modrdn.c
@@ -10,6 +10,8 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/monitor.c b/servers/slapd/monitor.c
index a7311989ef4fb086ed8541a5d238e74307f941cc..66c6494e5c11d3ad172226f8e29b09cd9c94cf71 100644
--- a/servers/slapd/monitor.c
+++ b/servers/slapd/monitor.c
@@ -17,6 +17,8 @@
  *	Added locking of currenttime_mutex to protect call(s) to localtime().
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
diff --git a/servers/slapd/operation.c b/servers/slapd/operation.c
index 0c62e219ece0041aede5f999440faa54f87664e2..a960c30695d509ec1901c2a3bcea05fa413e7df2 100644
--- a/servers/slapd/operation.c
+++ b/servers/slapd/operation.c
@@ -1,5 +1,7 @@
 /* operation.c - routines to deal with pending ldap operations */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/phonetic.c b/servers/slapd/phonetic.c
index 959e06e1e332479aeae05f92dcd4d8c2ce8f4f13..996c68160cb75b66b168acae343e7491e0ab30e3 100644
--- a/servers/slapd/phonetic.c
+++ b/servers/slapd/phonetic.c
@@ -1,5 +1,7 @@
 /* phonetic.c - routines to do phonetic matching */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
diff --git a/servers/slapd/proto-slap.h b/servers/slapd/proto-slap.h
index 83e4872d1594aba2b164ec68afde89bd9ee46cc0..3b08579d76e04813c2294fbc80225463607703db 100644
--- a/servers/slapd/proto-slap.h
+++ b/servers/slapd/proto-slap.h
@@ -1,213 +1,215 @@
 #ifndef _PROTO_SLAP
 #define _PROTO_SLAP
 
+#include <ldap_cdefs.h>
+
 /*
  * acl.c
  */
 
-int access_allowed( Backend *be, Connection *conn, Operation *op, Entry *e,
-	char *attr, struct berval *val, char *dn, int  access );
+int access_allowed LDAP_P(( Backend *be, Connection *conn, Operation *op, Entry *e,
+	char *attr, struct berval *val, char *dn, int  access ));
 
-struct acl * acl_get_applicable( Backend *be, Operation *op, Entry *e,
-	char *attr, char *edn, int nmatches, regmatch_t *matches );
-int acl_access_allowed( struct acl *a, Backend *be, Connection *conn, Entry *e,
+struct acl * acl_get_applicable LDAP_P(( Backend *be, Operation *op, Entry *e,
+	char *attr, char *edn, int nmatches, regmatch_t *matches ));
+int acl_access_allowed LDAP_P(( struct acl *a, Backend *be, Connection *conn, Entry *e,
 	struct berval *val, Operation *op, int  access, char *edn,
-	regmatch_t *matches );
+	regmatch_t *matches ));
 
-int acl_check_mods( Backend *be, Connection *conn, Operation *op, Entry *e,
-	LDAPMod *mods );
+int acl_check_mods LDAP_P(( Backend *be, Connection *conn, Operation *op, Entry *e,
+	LDAPMod *mods ));
 
 /*
  * aclparse.c
  */
 
-void parse_acl( Backend *be, char *fname, int lineno, int argc, char **argv );
-char * access2str( int access );
-int str2access( char *str );
+void parse_acl LDAP_P(( Backend *be, char *fname, int lineno, int argc, char **argv ));
+char * access2str LDAP_P(( int access ));
+int str2access LDAP_P(( char *str ));
 
 /*
  * attr.c
  */
 
-void attr_free( Attribute *a );
-char * attr_normalize( char *s );
-int attr_merge_fast( Entry *e, char *type, struct berval **vals, int  nvals,
-	int  naddvals, int  *maxvals, Attribute ***a );
-int attr_merge( Entry *e, char *type, struct berval **vals );
-Attribute * attr_find( Attribute *a, char *type );
-int attr_delete( Attribute **attrs, char *type );
-int attr_syntax( char *type );
-void attr_syntax_config( char *fname, int lineno, int argc, char **argv );
+void attr_free LDAP_P(( Attribute *a ));
+char * attr_normalize LDAP_P(( char *s ));
+int attr_merge_fast LDAP_P(( Entry *e, char *type, struct berval **vals, int  nvals,
+	int  naddvals, int  *maxvals, Attribute ***a ));
+int attr_merge LDAP_P(( Entry *e, char *type, struct berval **vals ));
+Attribute * attr_find LDAP_P(( Attribute *a, char *type ));
+int attr_delete LDAP_P(( Attribute **attrs, char *type ));
+int attr_syntax LDAP_P(( char *type ));
+void attr_syntax_config LDAP_P(( char *fname, int lineno, int argc, char **argv ));
 
 /*
  * ava.c
  */
 
-int get_ava( BerElement *ber, Ava *ava );
-void ava_free( Ava *ava, int freeit );
+int get_ava LDAP_P(( BerElement *ber, Ava *ava ));
+void ava_free LDAP_P(( Ava *ava, int freeit ));
 
 /*
  * backend.c
  */
 
-Backend * new_backend( char *type );
-Backend * select_backend( char * dn );
-int be_issuffix( Backend *be, char *suffix );
-int be_isroot( Backend *be, char *dn );
-int be_isroot_pw( Backend *be, char *dn, struct berval *cred );
-void be_close();
+Backend * new_backend LDAP_P(( char *type ));
+Backend * select_backend LDAP_P(( char * dn ));
+int be_issuffix LDAP_P(( Backend *be, char *suffix ));
+int be_isroot LDAP_P(( Backend *be, char *dn ));
+int be_isroot_pw LDAP_P(( Backend *be, char *dn, struct berval *cred ));
+void be_close LDAP_P(());
 
 /*
  * ch_malloc.c
  */
 
-char * ch_malloc( unsigned long size );
-char * ch_realloc( char *block, unsigned long size );
-char * ch_calloc( unsigned long nelem, unsigned long size );
+char * ch_malloc LDAP_P(( unsigned long size ));
+char * ch_realloc LDAP_P(( char *block, unsigned long size ));
+char * ch_calloc LDAP_P(( unsigned long nelem, unsigned long size ));
 
 /*
  * charray.c
  */
 
-void charray_add( char ***a, char *s );
-void charray_merge( char ***a, char **s );
-void charray_free( char **array );
-int charray_inlist( char **a, char *s );
-char ** charray_dup( char **a );
-char ** str2charray( char *str, char *brkstr );
+void charray_add LDAP_P(( char ***a, char *s ));
+void charray_merge LDAP_P(( char ***a, char **s ));
+void charray_free LDAP_P(( char **array ));
+int charray_inlist LDAP_P(( char **a, char *s ));
+char ** charray_dup LDAP_P(( char **a ));
+char ** str2charray LDAP_P(( char *str, char *brkstr ));
 
 /*
  * config.c
  */
 
-void read_config( char *fname, Backend **bep, FILE *pfp );
+void read_config LDAP_P(( char *fname, Backend **bep, FILE *pfp ));
 
 /*
  * connection.c
  */
 
-void connection_activity( Connection *conn );
+void connection_activity LDAP_P(( Connection *conn ));
 
 /*
  * dn.c
  */
 
-char * dn_normalize( char *dn );
-char * dn_normalize_case( char *dn );
-char * dn_parent( Backend *be, char *dn );
-int dn_issuffix( char *dn, char *suffix );
-int dn_type( char *dn );
-char * dn_upcase( char *dn );
+char * dn_normalize LDAP_P(( char *dn ));
+char * dn_normalize_case LDAP_P(( char *dn ));
+char * dn_parent LDAP_P(( Backend *be, char *dn ));
+int dn_issuffix LDAP_P(( char *dn, char *suffix ));
+int dn_type LDAP_P(( char *dn ));
+char * dn_upcase LDAP_P(( char *dn ));
 
 /*
  * entry.c
  */
 
-Entry * str2entry( char	*s );
-char * entry2str( Entry *e, int *len, int printid );
-void entry_free( Entry *e );
+Entry * str2entry LDAP_P(( char	*s ));
+char * entry2str LDAP_P(( Entry *e, int *len, int printid ));
+void entry_free LDAP_P(( Entry *e ));
 
-int entry_rdwr_lock( Entry *e, int rw );
-int entry_rdwr_rlock( Entry *e );
-int entry_rdwr_wlock( Entry *e );
-int entry_rdwr_unlock( Entry *e, int rw );
-int entry_rdwr_runlock( Entry *e );
-int entry_rdwr_wunlock( Entry *e );
-int entry_rdwr_init( Entry *e );
+int entry_rdwr_lock LDAP_P(( Entry *e, int rw ));
+int entry_rdwr_rlock LDAP_P(( Entry *e ));
+int entry_rdwr_wlock LDAP_P(( Entry *e ));
+int entry_rdwr_unlock LDAP_P(( Entry *e, int rw ));
+int entry_rdwr_runlock LDAP_P(( Entry *e ));
+int entry_rdwr_wunlock LDAP_P(( Entry *e ));
+int entry_rdwr_init LDAP_P(( Entry *e ));
 
 /*
  * filter.c
  */
 
-int get_filter( Connection *conn, BerElement *ber, Filter **filt, char **fstr );
-void filter_free( Filter *f );
-void filter_print( Filter *f );
+int get_filter LDAP_P(( Connection *conn, BerElement *ber, Filter **filt, char **fstr ));
+void filter_free LDAP_P(( Filter *f ));
+void filter_print LDAP_P(( Filter *f ));
 
 /*
  * filterentry.c
  */
 
-int test_filter( Backend *be, Connection *conn, Operation *op, Entry *e,
-	Filter	*f );
+int test_filter LDAP_P(( Backend *be, Connection *conn, Operation *op, Entry *e,
+	Filter	*f ));
 
 /*
  * lock.c
  */
 
-FILE * lock_fopen( char *fname, char *type, FILE **lfp );
-int lock_fclose( FILE *fp, FILE *lfp );
+FILE * lock_fopen LDAP_P(( char *fname, char *type, FILE **lfp ));
+int lock_fclose LDAP_P(( FILE *fp, FILE *lfp ));
 
 /*
  * monitor.c
  */
 
-void monitor_info( Connection *conn, Operation *op );
+void monitor_info LDAP_P(( Connection *conn, Operation *op ));
 
 /*
  * operation.c
  */
 
-void op_free( Operation *op );
-Operation * op_add( Operation **olist, BerElement *ber, unsigned long msgid,
-	unsigned long tag, char *dn, int id, int connid );
-void op_delete( Operation **olist, Operation *op );
+void op_free LDAP_P(( Operation *op ));
+Operation * op_add LDAP_P(( Operation **olist, BerElement *ber, unsigned long msgid,
+	unsigned long tag, char *dn, int id, int connid ));
+void op_delete LDAP_P(( Operation **olist, Operation *op ));
 
 /*
  * phonetic.c
  */
 
-char * first_word( char *s );
-char * next_word( char *s );
-char * word_dup( char *w );
-char * phonetic( char *s );
+char * first_word LDAP_P(( char *s ));
+char * next_word LDAP_P(( char *s ));
+char * word_dup LDAP_P(( char *w ));
+char * phonetic LDAP_P(( char *s ));
 
 /*
  * repl.c
  */
 
-void replog( Backend *be, int optype, char *dn, void *change, int flag );
+void replog LDAP_P(( Backend *be, int optype, char *dn, void *change, int flag ));
 
 /*
  * result.c
  */
 
-void send_ldap_result( Connection *conn, Operation *op, int err, char *matched,
-	char *text );
-void send_ldap_search_result( Connection *conn, Operation *op, int err,
-	char *matched, char *text, int nentries );
-void close_connection( Connection *conn, int opconnid, int opid );
+void send_ldap_result LDAP_P(( Connection *conn, Operation *op, int err, char *matched,
+	char *text ));
+void send_ldap_search_result LDAP_P(( Connection *conn, Operation *op, int err,
+	char *matched, char *text, int nentries ));
+void close_connection LDAP_P(( Connection *conn, int opconnid, int opid ));
 
 /*
  * schema.c
  */
 
-int oc_schema_check( Entry *e );
+int oc_schema_check LDAP_P(( Entry *e ));
 
 /*
  * schemaparse.c
  */
 
-void parse_oc( Backend *be, char *fname, int lineno, int argc, char **argv );
+void parse_oc LDAP_P(( Backend *be, char *fname, int lineno, int argc, char **argv ));
 
 /*
  * str2filter.c
  */
 
-Filter * str2filter( char *str );
+Filter * str2filter LDAP_P(( char *str ));
 
 /*
  * value.c
  */
 
-int value_add_fast( struct berval ***vals, struct berval **addvals, int nvals,
-	int naddvals, int *maxvals );
-int value_add( struct berval ***vals, struct berval **addvals );
-void value_normalize( char *s, int syntax );
-int value_cmp( struct berval *v1, struct berval *v2, int syntax,
-	int normalize );
-int value_ncmp( struct berval *v1, struct berval *v2, int syntax, int len,
-	int normalize );
-int value_find( struct berval **vals, struct berval *v, int syntax,
-	int normalize );
+int value_add_fast LDAP_P(( struct berval ***vals, struct berval **addvals, int nvals,
+	int naddvals, int *maxvals ));
+int value_add LDAP_P(( struct berval ***vals, struct berval **addvals ));
+void value_normalize LDAP_P(( char *s, int syntax ));
+int value_cmp LDAP_P(( struct berval *v1, struct berval *v2, int syntax,
+	int normalize ));
+int value_ncmp LDAP_P(( struct berval *v1, struct berval *v2, int syntax, int len,
+	int normalize ));
+int value_find LDAP_P(( struct berval **vals, struct berval *v, int syntax,
+	int normalize ));
 
 #endif /* _proto_slap */
diff --git a/servers/slapd/repl.c b/servers/slapd/repl.c
index 0a3d7c46318d628e2cd77c1f2fde515b82306643..cd0198ebd754c2af46f35a0921029eeaae760224 100644
--- a/servers/slapd/repl.c
+++ b/servers/slapd/repl.c
@@ -1,5 +1,7 @@
 /* repl.c - log modifications for replication purposes */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/result.c b/servers/slapd/result.c
index 620c7ef13c00542c41ca437a374f80d67ca000eb..185cc0269d511c3440c573538ba6df451050d801 100644
--- a/servers/slapd/result.c
+++ b/servers/slapd/result.c
@@ -1,6 +1,5 @@
 /* result.c - routines to send ldap results, errors, and referrals */
 
-#define DISABLE_BRIDGE
 #include "portable.h"
 
 #include <stdio.h>
diff --git a/servers/slapd/schema.c b/servers/slapd/schema.c
index ba16d9b0f13540041d9b221fcc4db4888f99a794..fc5f0214faeb94c0b14ebfd25697cebce659a1ce 100644
--- a/servers/slapd/schema.c
+++ b/servers/slapd/schema.c
@@ -1,5 +1,7 @@
 /* schema.c - routines to enforce schema definitions */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -76,6 +78,11 @@ oc_check_required( Entry *e, char *ocname )
 		return( 0 );
 	}
 
+	/* check for empty oc_required */
+	if(oc->oc_required == NULL) {
+		return( 0 );
+	}
+
 	/* for each required attribute */
 	for ( i = 0; oc->oc_required[i] != NULL; i++ ) {
 		/* see if it's in the entry */
@@ -111,14 +118,16 @@ oc_check_allowed( char *type, struct berval **ocl )
 		/* if we know about the oc */
 		if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
 			/* does it require the type? */
-			for ( j = 0; oc->oc_required[j] != NULL; j++ ) {
+			for ( j = 0; oc->oc_required != NULL && 
+				oc->oc_required[j] != NULL; j++ ) {
 				if ( strcasecmp( oc->oc_required[j], type )
 				    == 0 ) {
 					return( 0 );
 				}
 			}
 			/* does it allow the type? */
-			for ( j = 0; oc->oc_allowed[j] != NULL; j++ ) {
+			for ( j = 0; oc->oc_allowed != NULL && 
+				oc->oc_allowed[j] != NULL; j++ ) {
 				if ( strcasecmp( oc->oc_allowed[j], type )
 				    == 0 || strcmp( oc->oc_allowed[j], "*" )
 				    == 0 )
diff --git a/servers/slapd/schemaparse.c b/servers/slapd/schemaparse.c
index 180ed71d005caeca8636e3c0336294756ef91b43..70edf66b8bcc422745e16d5afac4baaa2c0aebf0 100644
--- a/servers/slapd/schemaparse.c
+++ b/servers/slapd/schemaparse.c
@@ -1,5 +1,7 @@
 /* schemaparse.c - routines to parse config file objectclass definitions */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/search.c b/servers/slapd/search.c
index fc96d2fd021727bd9f14c04445f3ace096defa32..721c013f84dee4bfef97a620d4e8b04f3e180d10 100644
--- a/servers/slapd/search.c
+++ b/servers/slapd/search.c
@@ -10,6 +10,8 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/shell-backends/passwd-shell.c b/servers/slapd/shell-backends/passwd-shell.c
index 2210651dbf1905fd1a95b58adb3dff8e9be35209..fd354e0fe4b6530df645ffc1c98ba6a1422c91a7 100644
--- a/servers/slapd/shell-backends/passwd-shell.c
+++ b/servers/slapd/shell-backends/passwd-shell.c
@@ -13,6 +13,8 @@
 */
 
 
+#include "portable.h"
+
 #include <sys/types.h>
 #include <stdio.h>
 #include <string.h>
diff --git a/servers/slapd/shell-backends/shellutil.c b/servers/slapd/shell-backends/shellutil.c
index 82eea1665b8d879664c61c833e4e9f89d088b5f1..e1a6d030a480b5830d6292c521e1fc59e1ef7988 100644
--- a/servers/slapd/shell-backends/shellutil.c
+++ b/servers/slapd/shell-backends/shellutil.c
@@ -14,6 +14,8 @@
 */
 
 
+#include "portable.h"
+
 #include <sys/types.h>
 #include <stdio.h>
 #include <string.h>
diff --git a/servers/slapd/shell-backends/shellutil.h b/servers/slapd/shell-backends/shellutil.h
index 7ea0db057cbae98961b67a628fadf0ab8b7b4125..8c52ab5dcf0547bdf7e1897efd73fde92ce59516 100644
--- a/servers/slapd/shell-backends/shellutil.h
+++ b/servers/slapd/shell-backends/shellutil.h
@@ -12,6 +12,12 @@
  is provided ``as is'' without express or implied warranty.
 */
 
+#ifndef SHELLUTIL_H
+#define SHELLUTIL_H
+
+#include <ldap_cdefs.h>
+
+LDAP_BEGIN_DECL
 
 #define MAXLINELEN	512
 
@@ -94,3 +100,6 @@ char *estrdup( char *s );
  */
 extern int	debugflg;
 extern char	*progname;
+
+LDAP_END_DECL
+#endif
diff --git a/servers/slapd/slap.h b/servers/slapd/slap.h
index 43f1eae8d1e7119572f5705bf74b1450031ab058..f5dee02400a728e06155cd0c69bf1fc019e93b76 100644
--- a/servers/slapd/slap.h
+++ b/servers/slapd/slap.h
@@ -3,11 +3,14 @@
 #ifndef _SLDAPD_H_
 #define _SLDAPD_H_
 
-#define LDAP_SYSLOG
+#include "portable.h"
+
+#include <stdlib.h>
 
-#include <syslog.h>
 #include <sys/types.h>
-#include <regex.h>
+#include <ac/syslog.h>
+#include <ac/regex.h>
+
 #undef NDEBUG
 #include <assert.h>
 
@@ -27,6 +30,8 @@
 
 #define MAXREMATCHES 10
 
+LDAP_BEGIN_DECL
+
 /*
  * represents an attribute value assertion (i.e., attr=value)
  */
@@ -134,7 +139,7 @@ struct access {
 	char		*a_dnattr;
 	long		a_access;
 
-#ifdef ACLGROUP
+#ifdef SLAPD_ACLGROUP
     char		*a_group;
 #endif
 
@@ -226,7 +231,7 @@ typedef struct op {
 					/* LDAP_AUTH_*			  */
 	int		o_opid;		/* id of this operation		  */
 	int		o_connid;	/* id of conn initiating this op  */
-#ifdef CLDAP
+#ifdef LDAP_CONNECTIONLESS
 	int		o_cldap;	/* != 0 if this came in via CLDAP */
 	struct sockaddr	o_clientaddr;	/* client address if via CLDAP	  */
 	char		o_searchbase;	/* search base if via CLDAP	  */
@@ -248,7 +253,7 @@ typedef struct conn {
 	char		*c_dn;		/* current DN bound to this conn  */
 	pthread_mutex_t	c_dnmutex;	/* mutex for c_dn field		  */
 	int		c_authtype;	/* auth method used to bind c_dn  */
-#ifdef COMPAT
+#ifdef LDAP_COMPAT
 	int		c_version;	/* for compatibility w/2.0, 3.0	  */
 #endif
 	char		*c_addr;	/* address of client on this conn */
@@ -280,8 +285,8 @@ typedef struct conn {
 #define Statslog( level, fmt, connid, opid, arg1, arg2, arg3 )
 #endif
 
-#ifdef NEEDPROTOS
 #include "proto-slap.h"
-#endif
+
+LDAP_END_DECL
 
 #endif /* _slap_h_ */
diff --git a/servers/slapd/str2filter.c b/servers/slapd/str2filter.c
index aca9674fd846519207cd3e26d05a867f00419c45..46b80f5fe678b6393eec854e80cc98674c8518da 100644
--- a/servers/slapd/str2filter.c
+++ b/servers/slapd/str2filter.c
@@ -1,5 +1,7 @@
 /* str2filter.c - parse an rfc 1588 string filter */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/strdup.c b/servers/slapd/strdup.c
index a16928a2e8ea9211c5ea7c073468ea78636ab38e..ec7398d4a88300cea5dee303d6abe2371b074e7a 100644
--- a/servers/slapd/strdup.c
+++ b/servers/slapd/strdup.c
@@ -1,8 +1,9 @@
+#include "portable.h"
+
 #if defined( ultrix ) || defined( nextstep )
 
 #include <string.h>
 
-
 char *strdup( char *s )
 {
         char    *p;
diff --git a/servers/slapd/tempnam.c b/servers/slapd/tempnam.c
index 20d108c9d2682f5732a24284d6275c55343cc7bb..53c0e502286830444d5d8554d9910bc10a4690ea 100644
--- a/servers/slapd/tempnam.c
+++ b/servers/slapd/tempnam.c
@@ -1,3 +1,5 @@
+#include "portable.h"
+
 #if defined( nextstep )
 
 #include <string.h>
diff --git a/servers/slapd/tools/centipede.c b/servers/slapd/tools/centipede.c
index 9919232b7aa31fd9b5fcc039e5125d8ba13ea7ef..5de9388b5631589e66804367e6c2f03aa1969f00 100644
--- a/servers/slapd/tools/centipede.c
+++ b/servers/slapd/tools/centipede.c
@@ -1,5 +1,7 @@
 /* centipede.c - generate and install indexing information (view w/tabstop=4) */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
@@ -556,6 +558,11 @@ diff_centroids(
 	int		amax, acur, dmax, dcur;
 	char	**vals;
 
+#ifdef LDBM_USE_DB2
+	DBC	*ocursorp;
+	DBC	*ncursorp;
+#endif /* LDBM_USE_DB2 */
+
 	if ( verbose ) {
 		printf( "Generating mods for differential %s centroid...", attr );
 		fflush( stdout );
@@ -600,8 +607,14 @@ diff_centroids(
 
 	olast.dptr = NULL;
 	nlast.dptr = NULL;
+#ifdef LDBM_USE_DB2
+	for ( okey = ldbm_firstkey( oldbm, &ocursorp ),
+			nkey = ldbm_firstkey( nldbm, &ncursorp );
+	      okey.dptr != NULL && nkey.dptr != NULL; )
+#else
 	for ( okey = ldbm_firstkey( oldbm ), nkey = ldbm_firstkey( nldbm );
 	      okey.dptr != NULL && nkey.dptr != NULL; )
+#endif
 	{
 		rc = strcmp( okey.dptr, nkey.dptr );
 
@@ -616,8 +629,13 @@ diff_centroids(
 			}
 			nlast = nkey;
 
+#ifdef LDBM_USE_DB2
+			okey = ldbm_nextkey( oldbm, olast, ocursorp );
+			nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
+#else
 			okey = ldbm_nextkey( oldbm, olast );
 			nkey = ldbm_nextkey( nldbm, nlast );
+#endif
 		} else if ( rc > 0 ) {
 			/* new value is not in old centroid - add it */
 			if ( charray_add_dup( &avals, &acur, &amax, nkey.dptr ) == NULL ) {
@@ -629,7 +647,12 @@ diff_centroids(
 				ldbm_datum_free( nldbm, nlast );
 			}
 			nlast = nkey;
+
+#ifdef LDBM_USE_DB2
+			nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
+#else
 			nkey = ldbm_nextkey( nldbm, nlast );
+#endif
 		} else {
 			/* old value is not in new centroid - delete it */
 			if ( charray_add_dup( &dvals, &dcur, &dmax, okey.dptr ) == NULL ) {
@@ -641,7 +664,12 @@ diff_centroids(
 				ldbm_datum_free( oldbm, olast );
 			}
 			olast = okey;
+
+#ifdef LDBM_USE_DB2
+			okey = ldbm_nextkey( oldbm, olast, ocursorp );
+#else
 			okey = ldbm_nextkey( oldbm, olast );
+#endif
 		}
 	}
 
@@ -651,7 +679,11 @@ diff_centroids(
 			return( NULL );
 		}
 
+#ifdef LDBM_USE_DB2
+		okey = ldbm_nextkey( oldbm, olast, ocursorp );
+#else
 		okey = ldbm_nextkey( oldbm, olast );
+#endif
 		if ( olast.dptr != NULL ) {
 			ldbm_datum_free( oldbm, olast );
 		}
@@ -666,7 +698,11 @@ diff_centroids(
 			return( NULL );
 		}
 
+#ifdef LDBM_USE_DB2
+		nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
+#else
 		nkey = ldbm_nextkey( nldbm, nlast );
+#endif
 		if ( nlast.dptr != NULL ) {
 			ldbm_datum_free( nldbm, nlast );
 		}
@@ -687,8 +723,14 @@ diff_centroids(
 
 	/* generate list of values to add */
 	lastkey.dptr = NULL;
+#ifdef LDBM_USE_DB2
+	for ( key = ldbm_firstkey( nldbm, &ncursorp ); key.dptr != NULL;
+	  key = ldbm_nextkey( nldbm, lastkey, ncursorp ) )
+#else
 	for ( key = ldbm_firstkey( nldbm ); key.dptr != NULL;
-	  key = ldbm_nextkey( nldbm, lastkey ) ) {
+	  key = ldbm_nextkey( nldbm, lastkey ) )
+#endif
+	{
 		/* see if it's in the old one */
 		data = ldbm_fetch( oldbm, key );
 
@@ -712,8 +754,14 @@ diff_centroids(
 
 	/* generate list of values to delete */
 	lastkey.dptr = NULL;
+#ifdef LDBM_USE_DB2
+	for ( key = ldbm_firstkey( oldbm, &ocursorp ); key.dptr != NULL;
+	  key = ldbm_nextkey( oldbm, lastkey, ocursorp ) )
+#else
 	for ( key = ldbm_firstkey( oldbm ); key.dptr != NULL;
-	  key = ldbm_nextkey( oldbm, lastkey ) ) {
+	  key = ldbm_nextkey( oldbm, lastkey ) )
+#endif
+	{
 		/* see if it's in the new one */
 		data = ldbm_fetch( nldbm, key );
 
@@ -773,6 +821,10 @@ full_centroid(
 	char	**vals;
 	int		vcur, vmax;
 
+#ifdef LDBM_USE_DB2
+	DBC *cursorp;
+#endif
+
 	if ( verbose ) {
 		printf( "Generating mods for full %s centroid...", attr );
 		fflush( stdout );
@@ -800,8 +852,14 @@ full_centroid(
 	lastkey.dptr = NULL;
 	vals = NULL;
 	vcur = vmax = 0;
+#ifdef LDBM_USE_DB2
+	for ( key = ldbm_firstkey( ldbm, &cursorp ); key.dptr != NULL;
+	  key = ldbm_nextkey( ldbm, lastkey, cursorp ) )
+#else
 	for ( key = ldbm_firstkey( ldbm ); key.dptr != NULL;
-	  key = ldbm_nextkey( ldbm, lastkey ) ) {
+	  key = ldbm_nextkey( ldbm, lastkey ) )
+#endif
+	{
 		if ( charray_add_dup( &vals, &vcur, &vmax, key.dptr ) == NULL ) {
 			ldap_mods_free( mods, 1 );
 			return( NULL );
diff --git a/servers/slapd/tools/chlog2replog.c b/servers/slapd/tools/chlog2replog.c
index f9f3b3c5d78b2cd8c2814d57e8182ec4c8408601..cd982936c85b2c55564ebb21e68882f862d7cb1a 100644
--- a/servers/slapd/tools/chlog2replog.c
+++ b/servers/slapd/tools/chlog2replog.c
@@ -16,6 +16,8 @@
  * slapd/slurpd locking conventions.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
diff --git a/servers/slapd/tools/edb2ldif.c b/servers/slapd/tools/edb2ldif.c
index d46be1edfd60d694c23ba5a2454fbb80b963fbd8..29bbf1f497087179b06de3206012e92087fb8147 100644
--- a/servers/slapd/tools/edb2ldif.c
+++ b/servers/slapd/tools/edb2ldif.c
@@ -10,6 +10,8 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <dirent.h>
diff --git a/servers/slapd/tools/ldapsyntax.c b/servers/slapd/tools/ldapsyntax.c
index 4fa12c407aefacef435a029e7c03f2c866238efe..69703a33d0e04d0cd6b9dd71ecd7f5ee0419aa0c 100644
--- a/servers/slapd/tools/ldapsyntax.c
+++ b/servers/slapd/tools/ldapsyntax.c
@@ -10,6 +10,8 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
diff --git a/servers/slapd/tools/ldapsyntax.h b/servers/slapd/tools/ldapsyntax.h
index ddcf1f2b3e837c1a6591cdb117bafceeb7a2026d..44a23263caa97cf47fce893eeb9b96d90c532d83 100644
--- a/servers/slapd/tools/ldapsyntax.h
+++ b/servers/slapd/tools/ldapsyntax.h
@@ -10,17 +10,23 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#ifndef LDAPSYNTAX_H
+#define LDAPSYNTAX_H 1
+
+#include <ldap_cdefs.h>
+
+LDAP_BEGIN_DECL
 
 /* XXX: the "master" LINE_WIDTH #define is in ../slap.h */
 #define LINE_WIDTH	76	/* for lines in string rep of an entry */
 
-#ifdef NEEDPROTOS
 /*
  * function prototypes
  */
 
-int init_syntaxes( void );
-int av2ldif( FILE *outfp, AV_Sequence av, DN dn, short syntax,
-    char *attrname, PS str_ps );
+int init_syntaxes LDAP_P(( void ));
+int av2ldif LDAP_P(( FILE *outfp, AV_Sequence av, DN dn, short syntax,
+    char *attrname, PS str_ps ));
 
-#endif /* NEEDPROTOS */
+LDAP_END_DECL
+#endif
diff --git a/servers/slapd/tools/ldbmcat.c b/servers/slapd/tools/ldbmcat.c
index 79465df072ef442648214c7c973a2e1d3d0f87bb..b89a54f489f774d3a7d691a918ec27d5a1ef69b8 100644
--- a/servers/slapd/tools/ldbmcat.c
+++ b/servers/slapd/tools/ldbmcat.c
@@ -1,3 +1,5 @@
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
@@ -24,6 +26,10 @@ main( argc, argv )
 	char		*file, *s;
 	int		printid = 1;
 
+#ifdef LDBM_USE_DB2
+	DBC	*cursorp;
+#endif
+
 	if ( argc < 2 || argc > 3 || ( argc == 3 && strcmp( argv[1], "-n" )
 	    != 0 )) {
 		usage( argv[0] );
@@ -41,8 +47,15 @@ main( argc, argv )
 	}
 
         last.dptr = NULL;
+
+#ifdef LDBM_USE_DB2
+        for ( key = ldbm_firstkey( dbp, &cursorp ); key.dptr != NULL;
+            key = ldbm_nextkey( dbp, last, cursorp ) )
+#else
         for ( key = ldbm_firstkey( dbp ); key.dptr != NULL;
-            key = ldbm_nextkey( dbp, last ) ) {
+            key = ldbm_nextkey( dbp, last ) )
+#endif
+	{
                 if ( last.dptr != NULL )
                         ldbm_datum_free( dbp, last );
                 data = ldbm_fetch( dbp, key );
diff --git a/servers/slapd/tools/ldbmtest.c b/servers/slapd/tools/ldbmtest.c
index b202c7b79fe12376b89fccf8d3b62db113e01418..fd2f1a00597451be7ee541297378d926abf3e380 100644
--- a/servers/slapd/tools/ldbmtest.c
+++ b/servers/slapd/tools/ldbmtest.c
@@ -1,4 +1,3 @@
-#define DISABLE_BRIDGE
 #include "portable.h"
 
 #include <stdio.h>
diff --git a/servers/slapd/tools/ldif.c b/servers/slapd/tools/ldif.c
index dc9f25d584e9d80d46c4ee47bff1c4541c53d554..adcb4b8534d31d9891f00f13f1297a2871de9c29 100644
--- a/servers/slapd/tools/ldif.c
+++ b/servers/slapd/tools/ldif.c
@@ -1,3 +1,5 @@
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <memory.h>
@@ -24,7 +26,7 @@ main( argc, argv )
 {
 	char	buf[BUFSIZ];
 	char	*type, *out;
-	int	binary = 0;
+	int	len, binary = 0;
 
 	if (argc < 2 || argc > 3 ) {
 		usage( argv[0] );
@@ -79,7 +81,9 @@ main( argc, argv )
 	}
 
 	/* not binary:  one value per line... */
-	while ( gets( buf ) != NULL ) {
+	while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
+		if( buf[len=strlen(buf)] == '\n') buf[len] = '\0';
+
 		if (( out = ldif_type_and_value( type, buf, strlen( buf ) ))
 		    == NULL ) {
 		    	perror( "ldif_type_and_value" );
diff --git a/servers/slapd/tools/ldif2id2children.c b/servers/slapd/tools/ldif2id2children.c
index 0bb8e5ec6d0ebd0d8c38d2815024df37f3131bbf..795a26f151d9d978ac1a30f1e604fd7e66f0c94a 100644
--- a/servers/slapd/tools/ldif2id2children.c
+++ b/servers/slapd/tools/ldif2id2children.c
@@ -1,3 +1,5 @@
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/tools/ldif2id2entry.c b/servers/slapd/tools/ldif2id2entry.c
index ec2bf272797d167a761c8d55c874086dc0d49ced..057b08530708bc66f09af5ac2745a0c0cf5432b3 100644
--- a/servers/slapd/tools/ldif2id2entry.c
+++ b/servers/slapd/tools/ldif2id2entry.c
@@ -1,3 +1,5 @@
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slapd/tools/ldif2index.c b/servers/slapd/tools/ldif2index.c
index dd03f3dc7821cec1ff481bd98eabce04a860d85a..2e4ba0c4397696132952db67f7455715838b5de5 100644
--- a/servers/slapd/tools/ldif2index.c
+++ b/servers/slapd/tools/ldif2index.c
@@ -1,7 +1,10 @@
+#include "portable.h"
+
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/string.h>
+#include <ac/socket.h>
+
 #include "../slap.h"
 
 #include "ldapconfig.h"
diff --git a/servers/slapd/tools/ldif2ldbm.c b/servers/slapd/tools/ldif2ldbm.c
index b2caa7b3cc0d87f86bd6cfd8b42ce1df40c3654f..01021b68405fb80d2b656c4a6e328221cd6b4ef8 100644
--- a/servers/slapd/tools/ldif2ldbm.c
+++ b/servers/slapd/tools/ldif2ldbm.c
@@ -1,3 +1,5 @@
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -5,9 +7,8 @@
 #include <sys/param.h>
 #include "../slap.h"
 #include "../back-ldbm/back-ldbm.h"
+#include "ldapconfig.h"
 
-#define DEFAULT_CONFIGFILE	"%ETCDIR%/slapd.conf"
-#define DEFAULT_ETCDIR		"%ETCDIR%"
 #define INDEXCMD		"ldif2index"
 #define ID2ENTRYCMD		"ldif2id2entry"
 #define ID2CHILDRENCMD		"ldif2id2children"
@@ -52,14 +53,14 @@ static int      nkids;
 static void
 usage( char *name )
 {
-	fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-j #jobs] [-n databasenumber] [-e etcdir]\n", name );
+	fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-j #jobs] [-n databasenumber] [-s sbindir]\n", name );
 	exit( 1 );
 }
 
 main( int argc, char **argv )
 {
 	int		i, stop, status;
-	char		*linep, *buf, *etcdir;
+	char		*linep, *buf, *sbindir;
 	char		*args[10];
 	char		buf2[20], buf3[20];
 	char		line[BUFSIZ];
@@ -74,17 +75,18 @@ main( int argc, char **argv )
 	Avlnode		*avltypes = NULL;
 	extern char	*optarg;
 
-	etcdir = DEFAULT_ETCDIR;
-	tailorfile = DEFAULT_CONFIGFILE;
+	sbindir = DEFAULT_SBINDIR;
+	tailorfile = SLAPD_DEFAULT_CONFIGFILE;
 	dbnum = -1;
-	while ( (i = getopt( argc, argv, "d:e:f:i:j:n:" )) != EOF ) {
+	while ( (i = getopt( argc, argv, "d:e:s:f:i:j:n:" )) != EOF ) {
 		switch ( i ) {
 		case 'd':	/* turn on debugging */
 			ldap_debug = atoi( optarg );
 			break;
 
-		case 'e':	/* alternate etcdir (index cmd location) */
-			etcdir = strdup( optarg );
+		case 's':	/* alternate sbindir (index cmd location) */
+		case 'e':	/* accept -e for backwards compatibility */
+			sbindir = strdup( optarg );
 			break;
 
 		case 'f':	/* specify a tailor file */
@@ -135,7 +137,7 @@ main( int argc, char **argv )
 			fprintf( stderr, "No ldbm database found in config file\n" );
 			exit( 1 );
 		}
-	} else if ( dbnum < 0 || dbnum > nbackends ) {
+	} else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
 		fprintf( stderr, "Database number selected via -n is out of range\n" );
 		fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
 		exit( 1 );
@@ -150,14 +152,14 @@ main( int argc, char **argv )
 	 */
 
 	i = 0;
-	sprintf( cmd, "%s/%s", etcdir, ID2ENTRYCMD );
+	sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
 	args[i++] = cmd;
 	args[i++] = "-i";
 	args[i++] = inputfile;
 	args[i++] = "-f";
 	args[i++] = tailorfile;
 	args[i++] = "-n";
-	sprintf( buf2, "%d", dbnum );
+	sprintf( buf2, "%d", dbnum+1 );
 	args[i++] = buf2;
 	if ( ldap_debug ) {
 		sprintf( buf3, "%d", ldap_debug );
@@ -172,14 +174,14 @@ main( int argc, char **argv )
 	 */
 
 	i = 0;
-	sprintf( cmd, "%s/%s", etcdir, ID2CHILDRENCMD );
+	sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
 	args[i++] = cmd;
 	args[i++] = "-i";
 	args[i++] = inputfile;
 	args[i++] = "-f";
 	args[i++] = tailorfile;
 	args[i++] = "-n";
-	sprintf( buf2, "%d", dbnum );
+	sprintf( buf2, "%d", dbnum+1 );
 	args[i++] = buf2;
 	if ( ldap_debug ) {
 		sprintf( buf3, "%d", ldap_debug );
@@ -194,14 +196,14 @@ main( int argc, char **argv )
 	 */
 
 	i = 0;
-	sprintf( cmd, "%s/%s", etcdir, INDEXCMD );
+	sprintf( cmd, "%s/%s", sbindir, INDEXCMD );
 	args[i++] = cmd;
 	args[i++] = "-i";
 	args[i++] = inputfile;
 	args[i++] = "-f";
 	args[i++] = tailorfile;
 	args[i++] = "-n";
-	sprintf( buf2, "%d", dbnum );
+	sprintf( buf2, "%d", dbnum+1 );
 	args[i++] = buf2;
 	if ( ldap_debug ) {
 		sprintf( buf3, "%d", ldap_debug );
diff --git a/servers/slapd/tools/sizecount.c b/servers/slapd/tools/sizecount.c
index 45d82301007b6866f9aedf8e2379b24f53f852b9..a653796ba5ac07754094fa7ebdfabaecde934b15 100644
--- a/servers/slapd/tools/sizecount.c
+++ b/servers/slapd/tools/sizecount.c
@@ -1,3 +1,5 @@
+#include "portable.h"
+
 #include <stdio.h>
 #include <ldbm.h>
 #include <lber.h>
diff --git a/servers/slapd/unbind.c b/servers/slapd/unbind.c
index 500006c34f0a76218346a570a99fb6d992198863..487fbbead5af88c2fd0e214850d8d0be12863c74 100644
--- a/servers/slapd/unbind.c
+++ b/servers/slapd/unbind.c
@@ -13,6 +13,8 @@
  *
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
diff --git a/servers/slapd/value.c b/servers/slapd/value.c
index c9e6fbb5774a1560f88990c63cab8a1e528fed5b..5be4338ed0050f52b6541189c2f15b3898946096 100644
--- a/servers/slapd/value.c
+++ b/servers/slapd/value.c
@@ -1,5 +1,6 @@
 /* value.c - routines for dealing with values */
 
+#include "portable.h"
 #include <stdio.h>
 #include <string.h>
 #include <sys/time.h>
diff --git a/servers/slurpd/admin.c b/servers/slurpd/admin.c
index 6a58b40c4c34f044f7072fc9dea3faf174b47469..c056cd1d8ba68cc6a2619604e2e619552747652e 100644
--- a/servers/slurpd/admin.c
+++ b/servers/slurpd/admin.c
@@ -15,6 +15,7 @@
  * reconfiguration of slurpd.
  */
 
+#include "portable.h"
 
 #include <stdio.h>
 #include <signal.h>
diff --git a/servers/slurpd/args.c b/servers/slurpd/args.c
index b3cc935df8ece477c1cfc25ea772f6124906ba1e..ec4f0b52b707a1c7fe59869469805c98f26295e2 100644
--- a/servers/slurpd/args.c
+++ b/servers/slurpd/args.c
@@ -14,8 +14,11 @@
  * args.c - process command-line arguments, and set appropriate globals.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
+#include <time.h>
 
 #include <lber.h>
 #include <ldap.h>
diff --git a/servers/slurpd/ch_malloc.c b/servers/slurpd/ch_malloc.c
index 298391e4f3c41ebae00739b16d5ca591374349c6..22b83b9f4dbe489d611e2094a4d3f467abc4e3f8 100644
--- a/servers/slurpd/ch_malloc.c
+++ b/servers/slurpd/ch_malloc.c
@@ -14,6 +14,8 @@
  * ch_malloc.c - malloc() and friends, with check for NULL return.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
diff --git a/servers/slurpd/config.c b/servers/slurpd/config.c
index 7a8261e8492b7cfe799ddf915243dc2d62352450..617943cbacee19d82be6715c1961ab218e64b105 100644
--- a/servers/slurpd/config.c
+++ b/servers/slurpd/config.c
@@ -15,6 +15,8 @@
  * config.c - configuration file handling routines
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
diff --git a/servers/slurpd/detach.c b/servers/slurpd/detach.c
index 7b3fdab57ae97fc181fae0452a6ee4c3cd04b1de..bb28bc96a2d9fd156d1c7322e12d86e31e5b909e 100644
--- a/servers/slurpd/detach.c
+++ b/servers/slurpd/detach.c
@@ -10,6 +10,8 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/types.h>
 #ifdef SVR4
@@ -39,6 +41,12 @@ detach()
 	nbits = getdtablesize();
 #endif /* USE_SYSCONF */
 
+#ifdef FD_SETSIZE
+	if ( nbits > FD_SETSIZE ) {
+		nbits = FD_SETSIZE;
+	}
+#endif /* FD_SETSIZE */
+
 #ifdef LDAP_DEBUG
 	if ( ldap_debug == 0 ) {
 #endif
diff --git a/servers/slurpd/fm.c b/servers/slurpd/fm.c
index e517cb665a56353aa177bbabbd6eb7c7bc53ced0..14815549d9b405aee03d4da7b940e8ea0de773b6 100644
--- a/servers/slurpd/fm.c
+++ b/servers/slurpd/fm.c
@@ -14,7 +14,6 @@
  * fm.c - file management routines.
  */
 
-#define DISABLE_BRIDGE
 #include "portable.h"
 
 #include <stdio.h>
diff --git a/servers/slurpd/globals.c b/servers/slurpd/globals.c
index f8630323cbc0ea690d0654271ead0207ae45d836..89403fc490c02a5ddbd734a1670de7c93132581b 100644
--- a/servers/slurpd/globals.c
+++ b/servers/slurpd/globals.c
@@ -14,6 +14,8 @@
  * globals.c - initialization code for global data
  */
 
+#include <portable.h>
+
 #include <stdio.h>
 
 #include "slurp.h"
diff --git a/servers/slurpd/globals.h b/servers/slurpd/globals.h
index 5d976ff8a4dbb3925eccb64e4a79fa654e91c873..1582d1780daae21c8a4d6410420ca03b28758424 100644
--- a/servers/slurpd/globals.h
+++ b/servers/slurpd/globals.h
@@ -10,6 +10,8 @@
  * is provided ``as is'' without express or implied warranty.
  */
 
+#ifndef SLURPD_GLOBALS_H
+#define SLURPD_GLOBALS_H 1
 
 /*
  * globals.h - definition of structure holding global data.
@@ -17,6 +19,8 @@
 
 #include "slurp.h"
 
+LDAP_BEGIN_DECL
+
 typedef struct globals {
     /* Thread ID for file manager thread */
     pthread_t fm_tid;
@@ -50,11 +54,11 @@ typedef struct globals {
     St	*st;
     /* Pointer to replication queue */
     Rq *rq;
-#ifdef KERBEROS
+#ifdef HAVE_KERBEROS
     /* Default name of kerberos srvtab file */
     char *default_srvtab;
-#endif /* KERBEROS */
-#if defined( THREAD_SUNOS4_LWP )
+#endif /* HAVE_KERBEROS */
+#if defined( HAVE_LWP )  && !defined( HAVE_LWP_THR )
     tl_t *tsl_list;
     mon_t tsl_mon;
 #endif /* THREAD_SUNOS4_LWP */
@@ -62,3 +66,7 @@ typedef struct globals {
 
 
 extern Globals *sglob;
+
+LDAP_END_DECL
+
+#endif /* SLURPD_GLOBALS_H */
diff --git a/servers/slurpd/ldap_op.c b/servers/slurpd/ldap_op.c
index 487f7466cc349792efa5817f1a5949e67132d005..90ef27449fd0caa0772c54ae18a928232096d662 100644
--- a/servers/slurpd/ldap_op.c
+++ b/servers/slurpd/ldap_op.c
@@ -14,7 +14,6 @@
  * ldap_op.c - routines to perform LDAP operations
  */
 
-#define DISABLE_BRIDGE
 #include "portable.h"
 
 #include <stdio.h>
diff --git a/servers/slurpd/lock.c b/servers/slurpd/lock.c
index 62aab06784b37f5acd99621aa00da0c789b69a70..6e14c1bfa0f33e82897051aaa505149f1f0495ec 100644
--- a/servers/slurpd/lock.c
+++ b/servers/slurpd/lock.c
@@ -14,6 +14,8 @@
  * lock.c - routines to open and apply an advisory lock to a file
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <sys/time.h>
 #include <sys/types.h>
diff --git a/servers/slurpd/main.c b/servers/slurpd/main.c
index a3b935faa578f384b348171f9122abe6aa06c260..1cd84b06ee20f9e79736f461c334989075fafd28 100644
--- a/servers/slurpd/main.c
+++ b/servers/slurpd/main.c
@@ -15,6 +15,8 @@
  * main.c - main routine for slurpd.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 
 #include "slurp.h"
@@ -121,6 +123,21 @@ main(
      * Start the main file manager thread (in fm.c).
      */
     pthread_attr_init( &attr );
+#ifndef THREAD_MIT_PTHREADS
+    /* POSIX_THREADS or compatible
+     * This is a draft 10 or standard pthreads implementation
+     */
+    if ( pthread_create( &(sglob->fm_tid), &attr, (void *) fm, (void *) NULL )
+	    != 0 ) {
+	Debug( LDAP_DEBUG_ANY, "file manager pthread_create failed\n",
+		0, 0, 0 );
+	exit( 1 );
+
+    }
+#else /* !THREAD_MIT_PTHREADS */
+    /*
+     * This is a draft 4 or earlier pthreads implementation
+     */
     if ( pthread_create( &(sglob->fm_tid), attr, (void *) fm, (void *) NULL )
 	    != 0 ) {
 	Debug( LDAP_DEBUG_ANY, "file manager pthread_create failed\n",
@@ -128,17 +145,26 @@ main(
 	exit( 1 );
 
     }
+#endif /* !THREAD_MIT_PTHREADS */
     pthread_attr_destroy( &attr );
 
     /*
      * Wait for the fm thread to finish.
      */
+#ifdef POSIX_THREADS
+    pthread_join( sglob->fm_tid, (void *) NULL );
+#else
     pthread_join( sglob->fm_tid, (void *) &status );
+#endif
     /*
      * Wait for the replica threads to finish.
      */
     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
+#ifdef POSIX_THREADS
+	pthread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
+#else
 	pthread_join( sglob->replicas[ i ]->ri_tid, (void *) &status );
+#endif
     }
     Debug( LDAP_DEBUG_ANY, "slurpd: terminating normally\n", 0, 0, 0 );
     sglob->slurpd_shutdown = 1;
diff --git a/servers/slurpd/re.c b/servers/slurpd/re.c
index f8ec36d038082ef80e2e8eba6d7dea6894a985a5..64f9225ddb97383f354f52bea90a7f82fd74a671 100644
--- a/servers/slurpd/re.c
+++ b/servers/slurpd/re.c
@@ -18,7 +18,6 @@
  */
 
 
-#define DISABLE_BRIDGE
 #include "portable.h"
 
 #include <stdio.h>
diff --git a/servers/slurpd/reject.c b/servers/slurpd/reject.c
index 6ba2c8e34cd5fde023af225632a09fff94db62b7..95ae7b7e62581ccfc1601df1823d41baa30398c7 100644
--- a/servers/slurpd/reject.c
+++ b/servers/slurpd/reject.c
@@ -17,7 +17,6 @@
  * to a replica LDAP server.
  */
 
-#define DISABLE_BRIDGE
 #include "portable.h"
 
 #include <stdio.h>
diff --git a/servers/slurpd/replica.c b/servers/slurpd/replica.c
index ed25a629077031feed19d6dee387adaea5b66f1b..38da5edaf8d20163853ab0f5d1c44acf09a62851 100644
--- a/servers/slurpd/replica.c
+++ b/servers/slurpd/replica.c
@@ -15,6 +15,7 @@
  * replica.c - code to start up replica threads.
  */
 
+#include "portable.h"
 
 #include <stdio.h>
 
@@ -57,8 +58,26 @@ start_replica_thread(
     pthread_attr_t	attr;
 
     pthread_attr_init( &attr );
+#ifdef NOTDEF
+	/* if main wants to join with us, we shouldn't detach */
     pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
+#endif
 
+#ifndef THREAD_MIT_PTHREADS
+    /* POSIX_THREADS or compatible
+     * This is a draft 10 or standard pthreads implementation
+     */
+    if ( pthread_create( &(ri->ri_tid), &attr, (void *) replicate,
+	    (void *) ri ) != 0 ) {
+	Debug( LDAP_DEBUG_ANY, "replica \"%s:%d\" pthread_create failed\n",
+		ri->ri_hostname, ri->ri_port, 0 );
+	pthread_attr_destroy( &attr );
+	return -1;
+    }
+#else	/* !THREAD_MIT_PTHREADS */
+    /*
+     * This is a draft 4 or earlier pthreads implementation
+     */
     if ( pthread_create( &(ri->ri_tid), attr, (void *) replicate,
 	    (void *) ri ) != 0 ) {
 	Debug( LDAP_DEBUG_ANY, "replica \"%s:%d\" pthread_create failed\n",
@@ -66,6 +85,7 @@ start_replica_thread(
 	pthread_attr_destroy( &attr );
 	return -1;
     }
+#endif	/* !THREAD_MIT_PTHREADS */
     pthread_attr_destroy( &attr );
     return 0;
 }
diff --git a/servers/slurpd/replog.c b/servers/slurpd/replog.c
index eb53bf10539b4f86e612840ce5f95a8499f23aa3..a9ecdb7ecc60ec4b3a64baddede5a1af7ee7babf 100644
--- a/servers/slurpd/replog.c
+++ b/servers/slurpd/replog.c
@@ -15,10 +15,8 @@
  * replog.c - routines which read and write replication log files.
  */
 
-#define DISABLE_BRIDGE
 #include "portable.h"
 
-
 #include <errno.h>
 #include <stdio.h>
 #include <syslog.h>
diff --git a/servers/slurpd/ri.c b/servers/slurpd/ri.c
index cc478d1e6a77386007feb94078d5b432e56bfd87..e11fc30f7bfc86c4cb26225c2ec318c08016401b 100644
--- a/servers/slurpd/ri.c
+++ b/servers/slurpd/ri.c
@@ -17,6 +17,7 @@
  */
 
 
+#include "portable.h"
 
 #include <stdio.h>
 #include <signal.h>
@@ -60,7 +61,11 @@ Ri_process(
     int		rc ;
     char	*errmsg;
 
+#ifdef SIGSTKFLT
+    (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
+#else
     (void) SIGNAL( SIGUSR1, (void *) do_nothing );
+#endif
     (void) SIGNAL( SIGPIPE, SIG_IGN );
     if ( ri == NULL ) {
 	Debug( LDAP_DEBUG_ANY, "Error: Ri_process: ri == NULL!\n", 0, 0, 0 );
@@ -146,7 +151,8 @@ Ri_process(
 
 
 /*
- * Wake a replication thread which may be sleeping.  Send it a SIGUSR1.
+ * Wake a replication thread which may be sleeping.
+ * Send it a SIG(STKFLT|USR1).
  */
 static void
 Ri_wake(
@@ -156,8 +162,13 @@ Ri_wake(
     if ( ri == NULL ) {
 	return;
     }
+#ifdef SIGSTKFLT
+    pthread_kill( ri->ri_tid, SIGSTKFLT );
+    (void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
+#else
     pthread_kill( ri->ri_tid, SIGUSR1 );
     (void) SIGNAL( SIGUSR1, (void *) do_nothing );
+#endif
 }
 
 
diff --git a/servers/slurpd/rq.c b/servers/slurpd/rq.c
index 845a7a7d340138771258c0c368643ecc4bd474d4..260872b7d90cdc9483a53feb8c05a5bc615c8b23 100644
--- a/servers/slurpd/rq.c
+++ b/servers/slurpd/rq.c
@@ -31,7 +31,6 @@
  *
  */
 
-#define DISABLE_BRIDGE
 #include "portable.h"
 
 #include <stdio.h>
diff --git a/servers/slurpd/sanity.c b/servers/slurpd/sanity.c
index 04be7db86af87d728e9f03c83735cd62d3587613..240e479e4a69bf1bcebedb80a07e71990d616492 100644
--- a/servers/slurpd/sanity.c
+++ b/servers/slurpd/sanity.c
@@ -18,6 +18,8 @@
  * feedback to the users.
  */
 
+#include "portable.h"
+
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
diff --git a/servers/slurpd/slurp.h b/servers/slurpd/slurp.h
index 622cd38374d2480b7af0b269a9e8c3eac2d12ad3..d493745d22fdee180bc86fe2ea8a96bff3a96c03 100644
--- a/servers/slurpd/slurp.h
+++ b/servers/slurpd/slurp.h
@@ -17,6 +17,8 @@
 
 #define LDAP_SYSLOG
 
+#include "portable.h"
+
 #include <syslog.h>
 #include <errno.h>
 #include <sys/types.h>
@@ -24,7 +26,6 @@
 #include "lber.h"
 #include "ldap.h"
 #include "lthread.h"
-#include "portable.h"
 #include "ldapconfig.h"
 #include "ldif.h"
 
@@ -135,6 +136,7 @@
 #define	RETRY_SLEEP_TIME		60
 
 
+LDAP_BEGIN_DECL
 
 /*
  * ****************************************************************************
@@ -335,15 +337,11 @@ typedef struct tsl {
 /* 
  * Public functions used to instantiate and initialize queue objects.
  */
-#ifdef NEEDPROTOS
-extern int Ri_init( Ri **ri );
-extern int Rq_init( Rq **rq );
-extern int Re_init( Re **re );
-#else /* NEEDPROTOS */
-extern int Ri_init();
-extern int Rq_init();
-extern int Re_init();
-#endif /* NEEDPROTOS */
+extern int Ri_init LDAP_P(( Ri **ri ));
+extern int Rq_init LDAP_P(( Rq **rq ));
+extern int Re_init LDAP_P(( Re **re ));
+
+LDAP_END_DECL
 
 #endif /* _SLURPD_H_ */
 
diff --git a/servers/slurpd/st.c b/servers/slurpd/st.c
index a274674f35c76da12ae11e91a7481a946a515b5a..64029e2dbbd9c44ca9cc59e26ae7bf7ea47b86c4 100644
--- a/servers/slurpd/st.c
+++ b/servers/slurpd/st.c
@@ -16,7 +16,6 @@
  * writing status information to disk.
  */
 
-#define DISABLE_BRIDGE
 #include "portable.h"
 
 #include <stdio.h>
diff --git a/servers/slurpd/tsleep.c b/servers/slurpd/tsleep.c
index 5dc3ba3deabfbae444527663c6d8c4daa3ab9669..3cb34708e4f76b408e36e73c2e8e6586cc7257e8 100644
--- a/servers/slurpd/tsleep.c
+++ b/servers/slurpd/tsleep.c
@@ -24,6 +24,7 @@
  * is so much simpler...
  */
 
+#include "portable.h"
 #include <stdio.h>
 
 #include "slurp.h"