diff --git a/servers/slapd/acl.c b/servers/slapd/acl.c
index 10985533190df937542169770f0443e30b080c11..cbd734612d3d93b3cc897b041560e219c0029bf9 100644
--- a/servers/slapd/acl.c
+++ b/servers/slapd/acl.c
@@ -3,12 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <regex.h>
+
+#include <ac/regex.h>
+#include <ac/socket.h>
+#include <ac/string.h>
 
 #include "slap.h"
 
@@ -362,7 +360,7 @@ acl_access_allowed(
 
 			return( (b->a_access & ~ACL_SELF) >= access );
 		}
-#ifdef ACLGROUP
+#ifdef SLAPD_ACLGROUPS
 		if ( b->a_group != NULL && op->o_dn != NULL ) {
 			char buf[512];
 
@@ -386,7 +384,7 @@ acl_access_allowed(
 				return( (b->a_access & ~ACL_SELF) >= access );
 			}
 		}
-#endif /* ACLGROUP */
+#endif /* SLAPD_ACLGROUPS */
 	}
 
 	if ( odn ) free( odn );
diff --git a/servers/slapd/aclparse.c b/servers/slapd/aclparse.c
index a8ba9510896255886cec94d2c9eacfa87fe80ed6..4e92136f928f9e4bc6c6e92fbec7273557e26fc6 100644
--- a/servers/slapd/aclparse.c
+++ b/servers/slapd/aclparse.c
@@ -3,16 +3,14 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <unistd.h>
-#include <regex.h>
+
+#include <ac/ctype.h>
+#include <ac/regex.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/unistd.h>
 
 #include "slap.h"
-#include "portable.h"
 
 extern Filter		*str2filter();
 extern struct acl	*global_acl;
@@ -210,11 +208,11 @@ parse_acl(
 			} else if ( strcasecmp( left, "dnattr" ) == 0 ) {
 				b->a_dnattr = strdup( right );
 
-#ifdef ACLGROUP
+#ifdef SLAPD_ACLGROUPS
 			} else if ( strcasecmp( left, "group" ) == 0 ) {
 				regtest(fname, lineno, right);
 				b->a_group = dn_upcase(strdup( right ));
-#endif /* ACLGROUP */
+#endif /* SLAPD_ACLGROUPS */
 			} else if ( strcasecmp( left, "domain" ) == 0 ) {
 				char	*s;
 				regtest(fname, lineno, right);
diff --git a/servers/slapd/add.c b/servers/slapd/add.c
index 7d31bc8744aec3e5795ba34e1eab5099bdea59da..110e3d365dd94715ee392feab4fab3d9d8241899 100644
--- a/servers/slapd/add.c
+++ b/servers/slapd/add.c
@@ -13,10 +13,11 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/string.h>
+#include <ac/time.h>
+#include <ac/socket.h>
+
 #include "slap.h"
 
 extern Backend	*select_backend();
diff --git a/servers/slapd/attr.c b/servers/slapd/attr.c
index 5842813b96b4b03246c8298c72a995ddee72a667..1aa239239588c557989755251035ce2f66bd9626 100644
--- a/servers/slapd/attr.c
+++ b/servers/slapd/attr.c
@@ -3,15 +3,22 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <ctype.h>
+
+#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+#endif
+
+#include <ac/ctype.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+
+#ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h>
+#endif
+
 #include <sys/stat.h>
-#include "portable.h"
+
 #include "slap.h"
 
 extern char	**charray_dup();
diff --git a/servers/slapd/ava.c b/servers/slapd/ava.c
index 12ef18cbf4f9acfd27c6e2165a39aafc68acb47b..1e10339cb2c98614de6d2b51b7f6ad665661721e 100644
--- a/servers/slapd/ava.c
+++ b/servers/slapd/ava.c
@@ -3,9 +3,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"
 
 int
diff --git a/servers/slapd/back-ldbm/add.c b/servers/slapd/back-ldbm/add.c
index 37b6a4063536263b53437637d94cee47acd196b3..eebcf4a2574cb096875f68b42a3464732ce8e254 100644
--- a/servers/slapd/back-ldbm/add.c
+++ b/servers/slapd/back-ldbm/add.c
@@ -3,9 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 #include "back-ldbm.h"
 #include "proto-back-ldbm.h"
diff --git a/servers/slapd/back-ldbm/attr.c b/servers/slapd/back-ldbm/attr.c
index ca59ba309090332fe4f99337e7012b597551d998..ea9694857fc2897f069c1480224884666c674829 100644
--- a/servers/slapd/back-ldbm/attr.c
+++ b/servers/slapd/back-ldbm/attr.c
@@ -3,9 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 #include "back-ldbm.h"
 
diff --git a/servers/slapd/back-ldbm/back-ldbm.h b/servers/slapd/back-ldbm/back-ldbm.h
index 3d656f4dd91f1cc8f865d3f28e2a910f98031de0..dda1ff8452aa6bb37d6b18bb17c49bc6e09add26 100644
--- a/servers/slapd/back-ldbm/back-ldbm.h
+++ b/servers/slapd/back-ldbm/back-ldbm.h
@@ -9,7 +9,7 @@ LDAP_BEGIN_DECL
 
 #define DEFAULT_CACHE_SIZE	1000
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 #	define DEFAULT_DBCACHE_SIZE (100 * DEFAULT_DB_PAGE_SIZE)
 #else
 #	define DEFAULT_DBCACHE_SIZE 100000
diff --git a/servers/slapd/back-ldbm/bind.c b/servers/slapd/back-ldbm/bind.c
index 90aa228f089b44e7cd90c3eef85b63def75caac1..f27872456e984b3131d370ed4a80ed43b210266b 100644
--- a/servers/slapd/back-ldbm/bind.c
+++ b/servers/slapd/back-ldbm/bind.c
@@ -3,45 +3,33 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/krb.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/unistd.h>
+
 #include "slap.h"
 #include "back-ldbm.h"
 #include "proto-back-ldbm.h"
-#ifdef KERBEROS
-#ifdef KERBEROS_V
-#include <kerberosIV/krb.h>
-#else
-#include <krb.h>
-#endif /* KERBEROS_V */
-#endif /* KERBEROS */
-
-#ifdef LDAP_CRYPT
-/* change for crypted passwords -- lukeh */
-#ifdef __NeXT__
-extern char *crypt (char *key, char *salt);
-#else
-#include <unistd.h>
-#endif
-#endif /* LDAP_CRYPT */
 
-#ifdef LDAP_SHA1
+#ifdef SLAPD_SHA1
 #include <lutil_sha1.h>
-#endif /* LDAP_SHA1 */
-#ifdef LDAP_MD5
+#endif /* SLAPD_SHA1 */
+
+#ifdef SLAPD_MD5
 #include <lutil_md5.h>
-#endif /* LDAP_MD5 */
+#endif /* SLAPD_MD5 */
 
 #include <lutil.h>
 
 extern Attribute	*attr_find();
 
-#ifdef KERBEROS
+#ifdef HAVE_KERBEROS
 extern int	krbv4_ldap_auth();
 #endif
 
-#ifdef LDAP_CRYPT
+#ifdef SLAPD_CRYPT
 pthread_mutex_t crypt_mutex;
 
 static int
@@ -65,7 +53,7 @@ crypted_value_find(
 					return ( 0 );
 				}
 				pthread_mutex_unlock( &crypt_mutex );
-#ifdef LDAP_MD5
+#ifdef SLAPD_MD5
 		} else if ( syntax != SYNTAX_BIN && strncasecmp( "{MD5}",
 			vals[i]->bv_val, (sizeof("{MD5}") - 1 ) ) == 0 ) {
 				ldap_MD5_CTX MD5context;
@@ -87,8 +75,8 @@ crypted_value_find(
 				if (strcmp(userpassword, base64digest) == 0) {
 					return ( 0 );
 				}
-#endif /* LDAP_MD5 */
-#ifdef LDAP_SHA1
+#endif /* SLAPD_MD5 */
+#ifdef SLAPD_SHA1
 		} else if ( syntax != SYNTAX_BIN && strncasecmp( "{SHA}",
 			vals[i]->bv_val, (sizeof("{SHA}") - 1 ) ) == 0 ) {
 				ldap_SHA1_CTX SHA1context;
@@ -110,7 +98,7 @@ crypted_value_find(
 				if (strcmp(userpassword, base64digest) == 0) {
 					return ( 0 );
 				}
-#endif /* LDAP_SHA1 */
+#endif /* SLAPD_SHA1 */
 		} else {
                 if ( value_cmp( vals[i], v, syntax, normalize ) == 0 ) {
                         return( 0 );
@@ -120,7 +108,7 @@ crypted_value_find(
 
 	return( 1 );
 }
-#endif /* LDAP_CRYPT */
+#endif /* SLAPD_CRYPT */
 
 int
 ldbm_back_bind(
@@ -137,7 +125,7 @@ ldbm_back_bind(
 	Attribute	*a;
 	int		rc;
 	char		*matched = NULL;
-#ifdef KERBEROS
+#ifdef HAVE_KERBEROS
 	char		krbname[MAX_K_NAME_SZ + 1];
 	AUTH_DAT	ad;
 #endif
@@ -195,7 +183,7 @@ ldbm_back_bind(
 			goto return_results;
 		}
 
-#ifdef LDAP_CRYPT
+#ifdef SLAPD_CRYPT
 		if ( crypted_value_find( a->a_vals, cred, a->a_syntax, 0, cred ) != 0 )
 #else
 		if ( value_find( a->a_vals, cred, a->a_syntax, 0 ) != 0 )
@@ -214,7 +202,7 @@ ldbm_back_bind(
 		rc = 0;
 		break;
 
-#ifdef KERBEROS
+#ifdef HAVE_KERBEROS
 	case LDAP_AUTH_KRBV41:
 		if ( krbv4_ldap_auth( be, cred, &ad ) != LDAP_SUCCESS ) {
 			send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,
diff --git a/servers/slapd/back-ldbm/cache.c b/servers/slapd/back-ldbm/cache.c
index 4c5e7c32038f51eb2562273d28a1ef01fc2b0dd4..940a226b842404c47cf1d064af21182392832b8e 100644
--- a/servers/slapd/back-ldbm/cache.c
+++ b/servers/slapd/back-ldbm/cache.c
@@ -3,9 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
 #include "slap.h"
+
 #include "back-ldbm.h"
 
 static int	cache_delete_entry_internal();
diff --git a/servers/slapd/back-ldbm/close.c b/servers/slapd/back-ldbm/close.c
index 66ef3e860640eff4d9842497f0c8e9bff84a5d6b..f300d9f92d94e8234392b4645987383d2ba7ce2a 100644
--- a/servers/slapd/back-ldbm/close.c
+++ b/servers/slapd/back-ldbm/close.c
@@ -3,8 +3,9 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+
 #include "slap.h"
 #include "back-ldbm.h"
 
diff --git a/servers/slapd/back-ldbm/compare.c b/servers/slapd/back-ldbm/compare.c
index 714923e1774e09eb71d613c9d2adb217be5e1213..e98f83dd94e3823838619ca869277bd496bf15c2 100644
--- a/servers/slapd/back-ldbm/compare.c
+++ b/servers/slapd/back-ldbm/compare.c
@@ -3,9 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 #include "back-ldbm.h"
 #include "proto-back-ldbm.h"
diff --git a/servers/slapd/back-ldbm/config.c b/servers/slapd/back-ldbm/config.c
index 5cbfd6e924413c85c11f5013b3bef518130e1c6c..0e087dc0b18badec3009ca2f54fe9fbedd4c68b5 100644
--- a/servers/slapd/back-ldbm/config.c
+++ b/servers/slapd/back-ldbm/config.c
@@ -3,9 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 #include "back-ldbm.h"
 
diff --git a/servers/slapd/back-ldbm/dbcache.c b/servers/slapd/back-ldbm/dbcache.c
index bef299be86f319f87dd56793d26c3f2c17051d5b..ff09593f930dd593c68797b12e8dd1da03c9b585 100644
--- a/servers/slapd/back-ldbm/dbcache.c
+++ b/servers/slapd/back-ldbm/dbcache.c
@@ -3,14 +3,17 @@
 #include "portable.h"
 
 #include <stdio.h>
+
+#include <ac/errno.h>
+#include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
 
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/param.h>
 #include <sys/stat.h>
-#include <errno.h>
+
+#ifdef HAVE_SYS_PARAM_H
+#include <sys/param.h>
+#endif
 
 #include "slap.h"
 #include "back-ldbm.h"
@@ -180,7 +183,7 @@ ldbm_cache_fetch(
 )
 {
 	Datum	data;
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &data, 0, sizeof( data ) );
 #endif
 
diff --git a/servers/slapd/back-ldbm/delete.c b/servers/slapd/back-ldbm/delete.c
index ee5fbabca6ba1db807c60f7a26541c5ceeb79a68..a7997e0f3f641bd4f2b913bf90f9700d90b03bef 100644
--- a/servers/slapd/back-ldbm/delete.c
+++ b/servers/slapd/back-ldbm/delete.c
@@ -3,9 +3,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 "back-ldbm.h"
 #include "proto-back-ldbm.h"
diff --git a/servers/slapd/back-ldbm/dn2id.c b/servers/slapd/back-ldbm/dn2id.c
index 89a5ea13eac76794c64eda97415471a6da033a65..ff56ba616be5f11aa0d251524ef3939a0cbc26f1 100644
--- a/servers/slapd/back-ldbm/dn2id.c
+++ b/servers/slapd/back-ldbm/dn2id.c
@@ -3,9 +3,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 "back-ldbm.h"
 #include "proto-back-ldbm.h"
@@ -26,7 +27,7 @@ dn2id_add(
 	Datum		key, data;
 	struct ldbminfo *li = (struct ldbminfo *) be->be_private;
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &key, 0, sizeof( key ) );
 	memset( &data, 0, sizeof( data ) );
 #endif
@@ -71,7 +72,7 @@ dn2id(
 	ID		id;
 	Datum		key, data;
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &key, 0, sizeof( key ) );
 	memset( &data, 0, sizeof( data ) );
 #endif
@@ -128,7 +129,7 @@ dn2id_delete(
 	Datum		key;
 	int		rc;
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &key, 0, sizeof( key ) );
 #endif
 
diff --git a/servers/slapd/back-ldbm/filterindex.c b/servers/slapd/back-ldbm/filterindex.c
index b65b841e2440a7353790b9081fe723eac337bca2..ffb9292b4245d08b69eabd8f97226ecafb4d28de 100644
--- a/servers/slapd/back-ldbm/filterindex.c
+++ b/servers/slapd/back-ldbm/filterindex.c
@@ -3,9 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 #include "back-ldbm.h"
 
diff --git a/servers/slapd/back-ldbm/group.c b/servers/slapd/back-ldbm/group.c
index e3e3436ceb9e9f864e07f97e22cfda78d5b6b5f1..16106d890468570a8179b12bb16226f69b217f7a 100644
--- a/servers/slapd/back-ldbm/group.c
+++ b/servers/slapd/back-ldbm/group.c
@@ -3,9 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 #include "back-ldbm.h"
 #include "proto-back-ldbm.h"
@@ -13,7 +14,7 @@
 extern Attribute        *attr_find();
 
 
-#ifdef ACLGROUP
+#ifdef SLAPD_ACLGROUPS
 /* return 0 IFF edn is a value in uniqueMember attribute
  * of entry with bdn AND that entry has an objectClass
  * value of groupOfUniqueNames
diff --git a/servers/slapd/back-ldbm/id2children.c b/servers/slapd/back-ldbm/id2children.c
index 2906c5b2cb97b91a4666dec5946cc9cb37aaf8d3..b25a14c54d1871823a0f81e54219bea3039bf9e2 100644
--- a/servers/slapd/back-ldbm/id2children.c
+++ b/servers/slapd/back-ldbm/id2children.c
@@ -3,8 +3,9 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+
 #include "slap.h"
 #include "back-ldbm.h"
 
@@ -25,7 +26,7 @@ id2children_add(
 	IDList		*idl;
 	char		buf[20];
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &key, 0, sizeof( key ) );
 	memset( &data, 0, sizeof( data ) );
 #endif
@@ -70,7 +71,7 @@ has_children(
 	IDList		*idl;
 	char		buf[20];
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &key, 0, sizeof( key ) );
 #endif
 
diff --git a/servers/slapd/back-ldbm/id2entry.c b/servers/slapd/back-ldbm/id2entry.c
index 140555c0c32cabc9b44245fd09f47e2e5702743e..5e7582daf767180a2a305c2659378884fe294ab7 100644
--- a/servers/slapd/back-ldbm/id2entry.c
+++ b/servers/slapd/back-ldbm/id2entry.c
@@ -3,8 +3,9 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+
 #include "slap.h"
 #include "back-ldbm.h"
 
@@ -23,7 +24,7 @@ id2entry_add( Backend *be, Entry *e )
 	Datum		key, data;
 	int		len, rc, flags;
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &key, 0, sizeof( key ) );
 	memset( &data, 0, sizeof( data ) );
 #endif
@@ -75,7 +76,7 @@ id2entry_delete( Backend *be, Entry *e )
 	/* XXX - check for writer lock - should also check no reader pending */
 	assert(pthread_rdwr_wchk_np(&e->e_rdwr));
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &key, 0, sizeof( key ) );
 #endif
 
@@ -116,7 +117,7 @@ id2entry( Backend *be, ID id, int rw )
 	Datum		key, data;
 	Entry		*e;
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &key, 0, sizeof( key ) );
 	memset( &data, 0, sizeof( data ) );
 #endif
diff --git a/servers/slapd/back-ldbm/idl.c b/servers/slapd/back-ldbm/idl.c
index d68c7b834bc28d947822e7217f0c0bedd5314105..88f089116f040a9a09941481d05a891d7d9aad2f 100644
--- a/servers/slapd/back-ldbm/idl.c
+++ b/servers/slapd/back-ldbm/idl.c
@@ -3,12 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
+
 #include <ac/string.h>
+#include <ac/socket.h>
 
-#include <sys/types.h>
-#ifdef CLDAP
-#include <sys/socket.h>
-#endif
 #include "slap.h"
 #include "ldapconfig.h"
 #include "back-ldbm.h"
@@ -63,7 +61,7 @@ idl_fetch_one(
 	char	*kstr;
 	int	i, nids;
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &k2, 0, sizeof( k2 ) );
 	memset( &data, 0, sizeof( data ) );
 #endif
@@ -90,7 +88,7 @@ idl_fetch(
 	char	*kstr;
 	int	i, nids;
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &k2, 0, sizeof( k2 ) );
 	memset( &data, 0, sizeof( data ) );
 #endif
@@ -184,7 +182,7 @@ idl_store(
 	Datum	data;
 	struct ldbminfo *li = (struct ldbminfo *) be->be_private;
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &data, 0, sizeof( data ) );
 #endif
 
@@ -306,7 +304,7 @@ idl_insert_key(
 	char	*kstr;
 	Datum	k2;
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &k2, 0, sizeof( k2 ) );
 #endif
 
diff --git a/servers/slapd/back-ldbm/index.c b/servers/slapd/back-ldbm/index.c
index 1e58ab8c91b29bf80fbf68b35f3595d18015eb24..0581fc1a1ed14b966e219e05682a64f3e01ff65f 100644
--- a/servers/slapd/back-ldbm/index.c
+++ b/servers/slapd/back-ldbm/index.c
@@ -3,9 +3,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 "back-ldbm.h"
 
@@ -107,7 +108,7 @@ index_read(
 	char		*realval, *tmpval;
 	char		buf[BUFSIZ];
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &key, 0, sizeof( key ) );
 #endif
 
@@ -181,7 +182,7 @@ add_value(
 	char	*realval, *tmpval, *s;
 	char	buf[BUFSIZ];
 
-#ifdef LDBM_USE_DB2
+#ifdef HAVE_BERKELEY_DB2
 	memset( &key, 0, sizeof( key ) );
 #endif
 
diff --git a/servers/slapd/back-ldbm/init.c b/servers/slapd/back-ldbm/init.c
index eaf886baf847c6828dfec76715c6702cf5154d30..65fda4378518a767f0bda90906cb6dde32604bf7 100644
--- a/servers/slapd/back-ldbm/init.c
+++ b/servers/slapd/back-ldbm/init.c
@@ -3,9 +3,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 "back-ldbm.h"
 
@@ -17,9 +18,9 @@ ldbm_back_init(
 	char		*argv[ 4 ];
 	int		i;
 
-#ifdef LDAP_CRYPT
+#ifdef SLAPD_CRYPT
 	extern pthread_mutex_t crypt_mutex;
-#endif /* LDAP_CRYPT */
+#endif /* SLAPD_CRYPT */
 
 	/* allocate backend-specific stuff */
 	li = (struct ldbminfo *) ch_calloc( 1, sizeof(struct ldbminfo) );
@@ -65,9 +66,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
+#ifdef SLAPD_CRYPT
 	pthread_mutex_init( &crypt_mutex, pthread_mutexattr_default );
-#endif /* LDAP_CRYPT */
+#endif /* SLAPD_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 c369c2bc1394b30d7426650c98bbc9c040b6c3ef..14ab368db56fd0dcb6bb5547b39f483a9429b4e2 100644
--- a/servers/slapd/back-ldbm/kerberos.c
+++ b/servers/slapd/back-ldbm/kerberos.c
@@ -2,20 +2,17 @@
 
 #include "portable.h"
 
+#ifdef HAVE_KERBEROS
+
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/krb.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 #include "back-ldbm.h"
 
-#ifdef KERBEROS
-#ifdef KERBEROS_V
-#include <kerberosIV/krb.h>
-#else
-#include <krb.h>
-#endif /* KERBEROS_V */
-
 #define LDAP_KRB_PRINCIPAL	"ldapserver"
 
 extern char		*ldap_srvtab;
diff --git a/servers/slapd/back-ldbm/modify.c b/servers/slapd/back-ldbm/modify.c
index 455dd8c175f3d087a50b82a6439c43940efaa9fe..666547f1c99c583a791c85fec09cdde66163c3da 100644
--- a/servers/slapd/back-ldbm/modify.c
+++ b/servers/slapd/back-ldbm/modify.c
@@ -3,9 +3,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 "back-ldbm.h"
 #include "proto-back-ldbm.h"
diff --git a/servers/slapd/back-ldbm/modrdn.c b/servers/slapd/back-ldbm/modrdn.c
index 62a2acd18bf626c694c0a2a6c47fa161865a7824..155538304523b763b8a8d239bce0ea467b44d348 100644
--- a/servers/slapd/back-ldbm/modrdn.c
+++ b/servers/slapd/back-ldbm/modrdn.c
@@ -3,9 +3,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 "back-ldbm.h"
 #include "proto-back-ldbm.h"
diff --git a/servers/slapd/back-ldbm/nextid.c b/servers/slapd/back-ldbm/nextid.c
index b680890a79ef7e71ec190608971d7e265b7a7c73..b16100a3f011df13bd84f4254b83046563b16808 100644
--- a/servers/slapd/back-ldbm/nextid.c
+++ b/servers/slapd/back-ldbm/nextid.c
@@ -3,9 +3,13 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+
+#ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h>
+#endif
+
 #include "slap.h"
 #include "back-ldbm.h"
 
diff --git a/servers/slapd/back-ldbm/search.c b/servers/slapd/back-ldbm/search.c
index 2bf35034bfa4bca8a7b5906031908a4ee643337a..705d6b47d67f607c790c120cc1aac9f5eb42b20f 100644
--- a/servers/slapd/back-ldbm/search.c
+++ b/servers/slapd/back-ldbm/search.c
@@ -3,9 +3,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 "back-ldbm.h"
 #include "proto-back-ldbm.h"
diff --git a/servers/slapd/back-ldbm/unbind.c b/servers/slapd/back-ldbm/unbind.c
index 56e3e426a4d3cd655db71615d58690c1705cf8a6..6330b4db20d94845af6ee5e9b9b43a04dbeb59ed 100644
--- a/servers/slapd/back-ldbm/unbind.c
+++ b/servers/slapd/back-ldbm/unbind.c
@@ -3,8 +3,9 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+
 #include "slap.h"
 
 int
diff --git a/servers/slapd/back-passwd/config.c b/servers/slapd/back-passwd/config.c
index 252717ab7307108b8f636ad979d37e19d1a38c7e..839af28cab70685b9baed13512de900d70c92ca8 100644
--- a/servers/slapd/back-passwd/config.c
+++ b/servers/slapd/back-passwd/config.c
@@ -3,11 +3,11 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include "portable.h"
+
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+
 #include "slap.h"
 
 passwd_back_config(
diff --git a/servers/slapd/back-passwd/search.c b/servers/slapd/back-passwd/search.c
index 3e9a0f9a8c8dbcc63a19d1ccc398db95068461b5..7c32656ec2d39b75496070f38e3db95fa6385637 100644
--- a/servers/slapd/back-passwd/search.c
+++ b/servers/slapd/back-passwd/search.c
@@ -3,12 +3,13 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+
 #include <pwd.h>
-#include "portable.h"
+
 #include "slap.h"
 
 extern time_t		currenttime;
diff --git a/servers/slapd/back-shell/abandon.c b/servers/slapd/back-shell/abandon.c
index 090ba26a4b1bac8c11df93c3b87dd29c142caede..36e81963a9826f061f113461cc642b79accd9307 100644
--- a/servers/slapd/back-shell/abandon.c
+++ b/servers/slapd/back-shell/abandon.c
@@ -3,10 +3,11 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
 #include <signal.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 #include "shell.h"
 
diff --git a/servers/slapd/back-shell/add.c b/servers/slapd/back-shell/add.c
index e9840e9eaa72194f17202752e39471e1e845a316..c798e8c6c1404e4d81e3ee016df85121b4c98d2a 100644
--- a/servers/slapd/back-shell/add.c
+++ b/servers/slapd/back-shell/add.c
@@ -3,9 +3,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 "shell.h"
 
diff --git a/servers/slapd/back-shell/bind.c b/servers/slapd/back-shell/bind.c
index 2d8e5a0a133dd3e910acbf3259fa424798f2029c..33bcd9df97e41ea8f6b36d67c461ce10bf9f33f2 100644
--- a/servers/slapd/back-shell/bind.c
+++ b/servers/slapd/back-shell/bind.c
@@ -3,9 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 #include "shell.h"
 
diff --git a/servers/slapd/back-shell/compare.c b/servers/slapd/back-shell/compare.c
index 642524f04d60b360dec748c3f89e2c1932b322ba..ed93c411d9af32cfc4697b0b23f6c08d8996c0cc 100644
--- a/servers/slapd/back-shell/compare.c
+++ b/servers/slapd/back-shell/compare.c
@@ -3,9 +3,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 "shell.h"
 
diff --git a/servers/slapd/back-shell/config.c b/servers/slapd/back-shell/config.c
index fc22aac49a3d8ceda0ce7eeb8a48b08c9d5b5126..10aee26a2c795c6e3accb906e796dd0410ddae51 100644
--- a/servers/slapd/back-shell/config.c
+++ b/servers/slapd/back-shell/config.c
@@ -3,9 +3,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 "shell.h"
 
diff --git a/servers/slapd/back-shell/delete.c b/servers/slapd/back-shell/delete.c
index e794a23a62486a59e81edccff8c1045d58fa69ae..75760ed2711e6af5cad115d5df03035487f3211b 100644
--- a/servers/slapd/back-shell/delete.c
+++ b/servers/slapd/back-shell/delete.c
@@ -3,9 +3,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 "shell.h"
 
diff --git a/servers/slapd/back-shell/fork.c b/servers/slapd/back-shell/fork.c
index 1b25031cbb3c61bc0a352028913afeb58fc0c39f..14eaca43490680b602ac2e461b4abe24dfe33e5f 100644
--- a/servers/slapd/back-shell/fork.c
+++ b/servers/slapd/back-shell/fork.c
@@ -3,9 +3,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"
 
 forkandexec(
diff --git a/servers/slapd/back-shell/init.c b/servers/slapd/back-shell/init.c
index b4087cb8a88294639d405d9229aa07dfb5a8fbf4..0865887123467d1df69e0aadfef3f7004bf7eace 100644
--- a/servers/slapd/back-shell/init.c
+++ b/servers/slapd/back-shell/init.c
@@ -3,8 +3,9 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+
 #include "slap.h"
 #include "shell.h"
 
diff --git a/servers/slapd/back-shell/modify.c b/servers/slapd/back-shell/modify.c
index b61dc8234d958cd506d64ef9587378e629b6b0f3..ce2c7bfd07129070e4d90af4596841259744b33f 100644
--- a/servers/slapd/back-shell/modify.c
+++ b/servers/slapd/back-shell/modify.c
@@ -3,9 +3,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 "shell.h"
 
diff --git a/servers/slapd/back-shell/modrdn.c b/servers/slapd/back-shell/modrdn.c
index a4735164d0b1909a0ca4df0efe8c04af37176e98..f3f00217ad95bfe5a553f5d988f3dfb498d6ebb3 100644
--- a/servers/slapd/back-shell/modrdn.c
+++ b/servers/slapd/back-shell/modrdn.c
@@ -3,9 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 #include "shell.h"
 
diff --git a/servers/slapd/back-shell/result.c b/servers/slapd/back-shell/result.c
index f1ac898526c3edb1a817f82eb1f19e3f2234c675..3d18e23baacde91489e3e86d71a94be2a5bf33c3 100644
--- a/servers/slapd/back-shell/result.c
+++ b/servers/slapd/back-shell/result.c
@@ -3,9 +3,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 "shell.h"
 
diff --git a/servers/slapd/back-shell/search.c b/servers/slapd/back-shell/search.c
index cbc3243d2128c8eacad24955f3c233d5a7b857c4..969215883bfb008066cd107e2913bd120143e33e 100644
--- a/servers/slapd/back-shell/search.c
+++ b/servers/slapd/back-shell/search.c
@@ -3,9 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 #include "shell.h"
 
diff --git a/servers/slapd/back-shell/unbind.c b/servers/slapd/back-shell/unbind.c
index df836f3e8f0143a782cf7fde5ebe5d703d977ce8..93c6889468632f7381578ff7e6853c5d1920f1d2 100644
--- a/servers/slapd/back-shell/unbind.c
+++ b/servers/slapd/back-shell/unbind.c
@@ -3,9 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 #include "shell.h"
 
diff --git a/servers/slapd/backend.c b/servers/slapd/backend.c
index 93675af31b8221df1832056bd9485914f64658b4..d40093b087593a54797955ee19cca23dc3cde83d 100644
--- a/servers/slapd/backend.c
+++ b/servers/slapd/backend.c
@@ -4,13 +4,15 @@
 #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 <sys/stat.h>
+
 #include "slap.h"
 
-#ifdef LDAP_LDBM
+#ifdef SLAPD_LDBM
 extern int	ldbm_back_bind();
 extern int	ldbm_back_unbind();
 extern int	ldbm_back_search();
@@ -26,12 +28,12 @@ extern int	ldbm_back_close();
 extern int      ldbm_back_group();
 #endif
 
-#ifdef LDAP_PASSWD
+#ifdef SLAPD_PASSWD
 extern int	passwd_back_search();
 extern int	passwd_back_config();
 #endif
 
-#ifdef LDAP_SHELL
+#ifdef SLAPD_SHELL
 extern int	shell_back_bind();
 extern int	shell_back_unbind();
 extern int	shell_back_search();
@@ -75,7 +77,7 @@ new_backend(
 	be->be_timelimit = deftime;
 	foundit = 0;
 
-#ifdef LDAP_LDBM
+#ifdef SLAPD_LDBM
 	if ( strcasecmp( type, "ldbm" ) == 0 ) {
 		be->be_bind = ldbm_back_bind;
 		be->be_unbind = ldbm_back_unbind;
@@ -89,7 +91,7 @@ new_backend(
 		be->be_config = ldbm_back_config;
 		be->be_init = ldbm_back_init;
 		be->be_close = ldbm_back_close;
-#ifdef ACLGROUP
+#ifdef SLAPD_ACLGROUPS
 		be->be_group = ldbm_back_group;
 #endif
 		be->be_type = "ldbm";
@@ -97,7 +99,7 @@ new_backend(
 	}
 #endif
 
-#ifdef LDAP_PASSWD
+#ifdef SLAPD_PASSWD
 	if ( strcasecmp( type, "passwd" ) == 0 ) {
 		be->be_bind = NULL;
 		be->be_unbind = NULL;
@@ -111,7 +113,7 @@ new_backend(
 		be->be_config = passwd_back_config;
 		be->be_init = NULL;
 		be->be_close = NULL;
-#ifdef ACLGROUP
+#ifdef SLAPD_ACLGROUPS
 		be->be_group = NULL;
 #endif
 		be->be_type = "passwd";
@@ -119,7 +121,7 @@ new_backend(
 	}
 #endif
 
-#ifdef LDAP_SHELL
+#ifdef SLAPD_SHELL
 	if ( strcasecmp( type, "shell" ) == 0 ) {
 		be->be_bind = shell_back_bind;
 		be->be_unbind = shell_back_unbind;
@@ -133,7 +135,7 @@ new_backend(
 		be->be_config = shell_back_config;
 		be->be_init = shell_back_init;
 		be->be_close = NULL;
-#ifdef ACLGROUP
+#ifdef SLAPD_ACLGROUPS
 		be->be_group = NULL;
 #endif
 		be->be_type = "shell";
@@ -257,7 +259,7 @@ be_unbind(
 	}
 }
 
-#ifdef ACLGROUP
+#ifdef SLAPD_ACLGROUPS
 int 
 be_group(Backend *be, char *bdn, char *edn)
 {
diff --git a/servers/slapd/bind.c b/servers/slapd/bind.c
index 4fa847853540897df6232299bade1f27978846bf..12a913bd15f0003b61603b2a75c91afd30381b97 100644
--- a/servers/slapd/bind.c
+++ b/servers/slapd/bind.c
@@ -15,9 +15,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"
 
 extern Backend	*select_backend();
@@ -52,7 +53,7 @@ do_bind(
 	 *	}
 	 */
 
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	/*
 	 * in version 3.0 there is an extra SEQUENCE tag after the
 	 * BindRequest SEQUENCE tag.
@@ -81,13 +82,13 @@ do_bind(
 		    "decoding error" );
 		return;
 	}
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	if ( conn->c_version == 30 ) {
 		switch ( method ) {
 		case LDAP_AUTH_SIMPLE_30:
 			method = LDAP_AUTH_SIMPLE;
 			break;
-#ifdef KERBEROS
+#ifdef HAVE_KERBEROS
 		case LDAP_AUTH_KRBV41_30:
 			method = LDAP_AUTH_KRBV41;
 			break;
diff --git a/servers/slapd/ch_malloc.c b/servers/slapd/ch_malloc.c
index 9f5cf5607356a9f95b24d693c043a465d67fe8b3..9ce0ed80ec8936c9ab3d80c701954616f59242c6 100644
--- a/servers/slapd/ch_malloc.c
+++ b/servers/slapd/ch_malloc.c
@@ -3,8 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/string.h>
+#include <ac/socket.h>
+
 #include "slap.h"
 
 char *
diff --git a/servers/slapd/charray.c b/servers/slapd/charray.c
index 71ef9e63451816b70340c260cee310a0d41d4a92..7edb80c8480d34ce3eecffcb9ec5fdbd73cc799c 100644
--- a/servers/slapd/charray.c
+++ b/servers/slapd/charray.c
@@ -3,9 +3,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"
 
 void
diff --git a/servers/slapd/compare.c b/servers/slapd/compare.c
index d836a4336542a1e2714f1330f66cc738744fc684..d31f2655a41f8e7bf557590dd4ae7ad3e916bc50 100644
--- a/servers/slapd/compare.c
+++ b/servers/slapd/compare.c
@@ -13,8 +13,9 @@
 #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/config.c b/servers/slapd/config.c
index 55660182c6388f8c88c675129ae22adc0a27f655..66e856796274133c3c80dd96caccd55838ae7dd7 100644
--- a/servers/slapd/config.c
+++ b/servers/slapd/config.c
@@ -3,9 +3,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/configinfo.c b/servers/slapd/configinfo.c
index f6dcfe9d5fa26e304b1e02f4d1232fe5cf5cb717..744d611ad80c5c57eebe378f542459699bd97999 100644
--- a/servers/slapd/configinfo.c
+++ b/servers/slapd/configinfo.c
@@ -13,9 +13,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/connection.c b/servers/slapd/connection.c
index bbfdd4344e5cef97d2feee0230c33b908f7136df..661877ffb41a3944d0fbb5d2ab1958b65be9812f 100644
--- a/servers/slapd/connection.c
+++ b/servers/slapd/connection.c
@@ -1,12 +1,13 @@
 #include "portable.h"
 
 #include <stdio.h>
+
+#include <ac/errno.h>
+#include <ac/signal.h>
+#include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <errno.h>
-#include <signal.h>
+
 #include "slap.h"
 
 extern Operation	*op_add();
@@ -18,11 +19,6 @@ extern long		ops_completed;
 extern pthread_mutex_t	ops_mutex;
 extern pthread_t	listener_tid;
 
-#ifdef DECL_SYS_ERRLIST
-extern int		sys_nerr;
-extern char		*sys_errlist[];
-#endif
-
 struct co_arg {
 	Connection	*co_conn;
 	Operation	*co_op;
@@ -52,7 +48,7 @@ connection_operation( struct co_arg *arg )
 		do_bind( arg->co_conn, arg->co_op );
 		break;
 
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	case LDAP_REQ_UNBIND_30:
 #endif
 	case LDAP_REQ_UNBIND:
@@ -63,7 +59,7 @@ connection_operation( struct co_arg *arg )
 		do_add( arg->co_conn, arg->co_op );
 		break;
 
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	case LDAP_REQ_DELETE_30:
 #endif
 	case LDAP_REQ_DELETE:
@@ -86,7 +82,7 @@ connection_operation( struct co_arg *arg )
 		do_search( arg->co_conn, arg->co_op );
 		break;
 
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	case LDAP_REQ_ABANDON_30:
 #endif
 	case LDAP_REQ_ABANDON:
@@ -177,7 +173,7 @@ connection_activity(
 		return;
 	}
 
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	if ( conn->c_version == 30 ) {
 		(void) ber_skip_tag( ber, &len );
 	}
@@ -205,7 +201,7 @@ connection_activity(
 
 	pthread_attr_init( &attr );
 	pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
-#ifndef THREAD_MIT_PTHREADS
+#if !defined(HAVE_PTHREADS_D4) && !defined(HAVE_DCE)
 	/* POSIX_THREADS or compatible
 	 * This is a draft 10 or standard pthreads implementation
 	 */
@@ -217,7 +213,7 @@ connection_activity(
 		active_threads++;
 		pthread_mutex_unlock( &active_threads_mutex );
 	}
-#else	/* !THREAD_MIT_PTHREAD */
+#else	/* pthread draft4  */
 	/*
 	 * This is a draft 4 or earlier pthreads implementation
 	 */
@@ -229,6 +225,6 @@ connection_activity(
 		active_threads++;
 		pthread_mutex_unlock( &active_threads_mutex );
 	}
-#endif	/* !THREAD_MIT_PTHREAD */
+#endif	/* pthread draft4 */
 	pthread_attr_destroy( &attr );
 }
diff --git a/servers/slapd/daemon.c b/servers/slapd/daemon.c
index 1dfc8682b16c1793dcce66210ad2922db1a253af..d95c0a059eee167c1f99c64c5b87da158e81352d 100644
--- a/servers/slapd/daemon.c
+++ b/servers/slapd/daemon.c
@@ -8,43 +8,33 @@
 #include "portable.h"
 
 #include <stdio.h>
+
+#include <ac/ctype.h>
+#include <ac/errno.h>
+#include <ac/signal.h>
+#include <ac/socket.h>
 #include <ac/string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <errno.h>
 #include <ac/time.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <signal.h>
-#ifdef _AIX
-#include <sys/select.h>
-#endif
+#include <ac/unistd.h>
+
 #include "slap.h"
 #include "ldapconfig.h"
-#ifdef NEED_FILIO
+
+#ifdef HAVE_SYS_FILIO_H
 #include <sys/filio.h>
-#else /* NEED_FILIO */
+#elif HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
-#endif /* NEED_FILIO */
-#ifdef USE_SYSCONF
-#include <unistd.h>
-#endif /* USE_SYSCONF */
+#endif
 
-#ifdef TCP_WRAPPERS
+#ifdef HAVE_TCPD
 #include <tcpd.h>
 
 int allow_severity = LOG_INFO;
 int deny_severity = LOG_NOTICE;
-#endif /* TCP_WRAPPERS */
+#endif /* TCP Wrappers */
 
 extern Operation	*op_add();
 
-#ifdef DECL_SYS_ERRLIST
-extern int		sys_nerr;
-extern char		*sys_errlist[];
-#endif
-
 extern time_t		currenttime;
 extern pthread_mutex_t	currenttime_mutex;
 extern int		active_threads;
@@ -79,14 +69,14 @@ slapd_daemon(
 	FILE			*fp;
 	int			on = 1;
 
-#ifdef USE_SYSCONF
+#ifdef HAVE_SYSCONF
 	dtblsize = sysconf( _SC_OPEN_MAX );
-#else /* USE_SYSCONF */
+#elif HAVE_GETDTABLESIZE
 	dtblsize = getdtablesize();
-#endif /* USE_SYSCONF */
-	/*
-	 * Add greg@greg.rim.or.jp
-	 */
+#else
+	dtblsize = FD_SETSIZE
+#endif
+
 #ifdef FD_SETSIZE
 	if(dtblsize > FD_SETSIZE) {
 		dtblsize = FD_SETSIZE;
@@ -151,14 +141,14 @@ slapd_daemon(
 	}
 
 	(void) SIGNAL( SIGPIPE, SIG_IGN );
-#ifdef linux
+#ifdef HAVE_LINUX_THREADS
 	/*
 	 * LinuxThreads are implemented using SIGUSR1/USR2,
 	 * so we'll use SIGSTKFLT and SIGUNUSED
 	 */
 	(void) SIGNAL( SIGSTKFLT, (void *) do_nothing );
 	(void) SIGNAL( SIGUNUSED, (void *) set_shutdown );
-#else /* !linux */
+#else  /* !linux */
 	(void) SIGNAL( SIGUSR1, (void *) do_nothing );
 	(void) SIGNAL( SIGUSR2, (void *) set_shutdown );
 #endif /* !linux */
@@ -284,7 +274,7 @@ slapd_daemon(
 				char *s;
 				client_addr = inet_ntoa( from.sin_addr );
 
-#if defined(REVERSE_LOOKUP) || defined(TCP_WRAPPERS)
+#if defined(SLAPD_RLOOKUPS) || defined(HAVE_TCPD)
 				hp = gethostbyaddr( (char *)
 				    &(from.sin_addr.s_addr),
 				    sizeof(from.sin_addr.s_addr), AF_INET );
@@ -309,7 +299,7 @@ slapd_daemon(
 				client_addr = NULL;
 			}
 
-#ifdef TCP_WRAPPERS
+#ifdef HAVE_TCPD
 			if(!hosts_ctl("slapd", client_name, client_addr,
 				STRING_UNKNOWN))
 			{
@@ -325,7 +315,7 @@ slapd_daemon(
 				pthread_mutex_unlock( &new_conn_mutex );
 				continue;
 			}
-#endif /* TCP_WRAPPERS */
+#endif /* HAVE_TCPD */
 
 			Statslog( LDAP_DEBUG_STATS,
 			    "conn=%d fd=%d connection from %s (%s) accepted.\n",
@@ -424,7 +414,7 @@ set_shutdown()
 {
 	Debug( LDAP_DEBUG_ANY, "slapd got shutdown signal\n", 0, 0, 0 );
 	slapd_shutdown = 1;
-#ifdef linux
+#ifdef HAVE_LINUX_THREADS
 	/*
 	 * LinuxThreads are implemented using SIGUSR1/USR2,
 	 * so we'll use SIGSTKFLT and SIGUNUSED
@@ -444,7 +434,7 @@ static void
 do_nothing()
 {
 	Debug( LDAP_DEBUG_TRACE, "slapd got do_nothing signal\n", 0, 0, 0 );
-#ifdef linux
+#ifdef HAVE_LINUX_THREADS
 	/*
 	 * LinuxThreads are implemented using SIGUSR1/USR2,
 	 * so we'll use SIGSTKFLT and SIGUNUSED
diff --git a/servers/slapd/delete.c b/servers/slapd/delete.c
index bab22d924f10210e19f5da8dfcf7e5f8bb915bde..eecf64090f750df6341620e71d36a78695069379 100644
--- a/servers/slapd/delete.c
+++ b/servers/slapd/delete.c
@@ -13,9 +13,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"
 
 extern Backend	*select_backend();
diff --git a/servers/slapd/detach.c b/servers/slapd/detach.c
index bb28bc96a2d9fd156d1c7322e12d86e31e5b909e..dd6db49d69a9ec9db074422dced7e974463cf45f 100644
--- a/servers/slapd/detach.c
+++ b/servers/slapd/detach.c
@@ -13,20 +13,14 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <sys/types.h>
-#ifdef SVR4
+
+#include <ac/signal.h>
+#include <ac/unistd.h>
+
 #include <sys/stat.h>
-#endif /* svr4 */
 #include <fcntl.h>
 #include <sys/file.h>
 #include <sys/ioctl.h>
-#include <signal.h>
-#include "portable.h"
-
-#ifdef USE_SYSCONF
-#include <unistd.h>
-#endif /* USE_SYSCONF */
-
 
 detach()
 {
@@ -35,11 +29,13 @@ detach()
 	extern int	ldap_debug;
 #endif
 
-#ifdef USE_SYSCONF
+#ifdef HAVE_SYSCONF
 	nbits = sysconf( _SC_OPEN_MAX );
-#else /* USE_SYSCONF */
+#elif HAVE_GETDTABLESIZE
 	nbits = getdtablesize();
-#endif /* USE_SYSCONF */
+#else
+	nbits = FD_SETSIZE
+#endif 
 
 #ifdef FD_SETSIZE
 	if ( nbits > FD_SETSIZE ) {
@@ -51,7 +47,7 @@ detach()
 	if ( ldap_debug == 0 ) {
 #endif
 		for ( i = 0; i < 5; i++ ) {
-#if defined( sunos5 ) && defined( THREAD_SUNOS5_LWP )
+#if defined( HAVE_LWP_THR )
 			switch ( fork1() ) {
 #else
 			switch ( fork() ) {
@@ -88,14 +84,14 @@ detach()
 			(void) dup2( sd, 2 );
 		close( sd );
 
-#ifdef USE_SETSID
+#ifdef HAVE_SETSID
 		setsid();
-#else /* USE_SETSID */
+#else /* HAVE_SETSID */
 		if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
 			(void) ioctl( sd, TIOCNOTTY, NULL );
 			(void) close( sd );
 		}
-#endif /* USE_SETSID */
+#endif /* HAVE_SETSID */
 #ifdef LDAP_DEBUG
 	} 
 #endif
diff --git a/servers/slapd/dn.c b/servers/slapd/dn.c
index de10e368c70ba5b7779aad604c92da04bf5d846d..d67aca70acb54293f011788fed664b829ca7d9f8 100644
--- a/servers/slapd/dn.c
+++ b/servers/slapd/dn.c
@@ -3,11 +3,12 @@
 #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 <ac/ctype.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+
 #include "slap.h"
 
 static char	**dn_explode();
diff --git a/servers/slapd/entry.c b/servers/slapd/entry.c
index 562cc47b689e05c24d1ae0582c5c33a6c1c0f310..c0474e20238eded3a92d3bce734e248856d8e924 100644
--- a/servers/slapd/entry.c
+++ b/servers/slapd/entry.c
@@ -3,10 +3,11 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/ctype.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 
 void	entry_free();
diff --git a/servers/slapd/filter.c b/servers/slapd/filter.c
index 49f8166f2b04c65d1700107a56fd8f77c8ba89b3..be8a8ea2425d0b5013aaccd8665987555a8c6793 100644
--- a/servers/slapd/filter.c
+++ b/servers/slapd/filter.c
@@ -3,9 +3,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 
 static int	get_filter_list();
@@ -56,7 +57,7 @@ get_filter( Connection *conn, BerElement *ber, Filter **filt, char **fstr )
 	err = 0;
 	*fstr = NULL;
 	f->f_choice = ber_peek_tag( ber, &len );
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	if ( conn->c_version == 30 ) {
 		switch ( f->f_choice ) {
 		case LDAP_FILTER_EQUALITY:
@@ -194,7 +195,7 @@ get_filter_list( Connection *conn, BerElement *ber, Filter **f, char **fstr )
 
 	Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
 
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	if ( conn->c_version == 30 ) {
 		(void) ber_skip_tag( ber, &len );
 	}
@@ -235,7 +236,7 @@ get_substring_filter(
 
 	Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
 
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	if ( conn->c_version == 30 ) {
 		(void) ber_skip_tag( ber, &len );
 	}
@@ -253,7 +254,7 @@ get_substring_filter(
 	sprintf( *fstr, "(%s=", f->f_sub_type );
 	for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
 	    tag = ber_next_element( ber, &len, last ) ) {
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 		if ( conn->c_version == 30 ) {
 			rc = ber_scanf( ber, "{a}", &val );
 		} else
@@ -271,7 +272,7 @@ get_substring_filter(
 		value_normalize( val, syntax );
 
 		switch ( tag ) {
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 		case LDAP_SUBSTRING_INITIAL_30:
 #endif
 		case LDAP_SUBSTRING_INITIAL:
@@ -285,7 +286,7 @@ get_substring_filter(
 			strcat( *fstr, val );
 			break;
 
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 		case LDAP_SUBSTRING_ANY_30:
 #endif
 		case LDAP_SUBSTRING_ANY:
@@ -297,7 +298,7 @@ get_substring_filter(
 			strcat( *fstr, val );
 			break;
 
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 		case LDAP_SUBSTRING_FINAL_30:
 #endif
 		case LDAP_SUBSTRING_FINAL:
diff --git a/servers/slapd/filterentry.c b/servers/slapd/filterentry.c
index 495fcb2ca624b324b545c592dcc215e0da2d4a20..ac0a943d02e4d7b58d5ed811a8ef0c569e157c28 100644
--- a/servers/slapd/filterentry.c
+++ b/servers/slapd/filterentry.c
@@ -3,10 +3,11 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <regex.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/regex.h>
+
 #include "slap.h"
 
 extern Attribute	*attr_find();
diff --git a/servers/slapd/init.c b/servers/slapd/init.c
index 7ba83187584a30403971bd7362ae2e87bf6225f1..81384199e3148351faaf7f2315b7064ad2650abe 100644
--- a/servers/slapd/init.c
+++ b/servers/slapd/init.c
@@ -3,13 +3,11 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+
 #include "portable.h"
 #include "slap.h"
 
diff --git a/servers/slapd/lock.c b/servers/slapd/lock.c
index 2e3cb917fda37569985384d9a0a640f242518961..debc102e2ef056c70ad900572fecd435b49a0f43 100644
--- a/servers/slapd/lock.c
+++ b/servers/slapd/lock.c
@@ -3,15 +3,13 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include "portable.h"
-#ifdef USE_LOCKF
-#include <unistd.h>
-#endif
+
+#include <ac/socket.h>
+#include <ac/time.h>
+#include <ac/unistd.h>
+
 #include <sys/file.h>
 #include <sys/param.h>
-#include <sys/socket.h>
 #include "slap.h"
 
 FILE *
diff --git a/servers/slapd/main.c b/servers/slapd/main.c
index 258923e61efb1573dd8ad97d576195e92ae3ef1c..59fb391b1fdb411f5a8d3fa4dc5ed756a4a3a1cf 100644
--- a/servers/slapd/main.c
+++ b/servers/slapd/main.c
@@ -1,14 +1,11 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#include "portable.h"
+
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+
 #include "slap.h"
 #include "ldapconfig.h"
 
@@ -184,7 +181,7 @@ main( argc, argv )
 		pthread_attr_init( &attr );
 		pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
 
-#ifndef THREAD_MIT_PTHREADS
+#if !defined(HAVE_PTHREADS_D4) && !defined(HAVE_DCE)
 		/* POSIX_THREADS or compatible
 		 * This is a draft 10 or standard pthreads implementation
 		 */
@@ -194,7 +191,7 @@ main( argc, argv )
 			    "listener pthread_create failed\n", 0, 0, 0 );
 			exit( 1 );
 		}
-#else	/* !THREAD_MIT_PTHREADS */
+#else	/* draft4 */
 		/*
 		 * This is a draft 4 or earlier pthreads implementation
 		 */
@@ -204,7 +201,7 @@ main( argc, argv )
 			    "listener pthread_create failed\n", 0, 0, 0 );
 			exit( 1 );
 		}
-#endif	/* !THREAD_MIT_PTHREADS */
+#endif	/* !draft4 */
 		pthread_attr_destroy( &attr );
 		pthread_join( listener_tid, (void *) &status );
 		pthread_exit( 0 );
@@ -236,7 +233,7 @@ main( argc, argv )
 #endif
 		flen = sizeof(from);
 		if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) {
-#ifdef REVERSE_LOOKUP
+#ifdef SLAPD_RLOOKUPS
 			hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr),
 			    sizeof(from.sin_addr.s_addr), AF_INET );
 #else
diff --git a/servers/slapd/modify.c b/servers/slapd/modify.c
index 2820a3ed19d2ccf59c35f9ecaf088c12939d58ed..1406b985b52210f56510ff98b06ca35686c14d8c 100644
--- a/servers/slapd/modify.c
+++ b/servers/slapd/modify.c
@@ -13,11 +13,11 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+
 #include "slap.h"
 
 extern Backend	*select_backend();
diff --git a/servers/slapd/modrdn.c b/servers/slapd/modrdn.c
index 75af6e13e832efb40987ecb1e73204019d617203..4928727817c1ed4f9b29fbd11eadc836893fc17c 100644
--- a/servers/slapd/modrdn.c
+++ b/servers/slapd/modrdn.c
@@ -13,9 +13,10 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+
 #include "slap.h"
 
 extern Backend	*select_backend();
diff --git a/servers/slapd/monitor.c b/servers/slapd/monitor.c
index c43b61ac46bc6b3e77a856d5e82f4b8bf63aadbb..05940823ca2207987fc83339e045decc42c0a274 100644
--- a/servers/slapd/monitor.c
+++ b/servers/slapd/monitor.c
@@ -20,10 +20,11 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+
 #include "slap.h"
 #include "ldapconfig.h"
 
@@ -194,7 +195,7 @@ monitor_info( Connection *conn, Operation *op )
 	val.bv_len = strlen( buf );
 	attr_merge( e, "nbackends", vals );
 
-#ifdef THREAD_SUNOS5_LWP
+#ifdef HAVE_LWP_THR
 	sprintf( buf, "%d", thr_getconcurrency() );
 	val.bv_val = buf;
 	val.bv_len = strlen( buf );
diff --git a/servers/slapd/operation.c b/servers/slapd/operation.c
index a960c30695d509ec1901c2a3bcea05fa413e7df2..b08d67929840f6e0a4b5f326664aef1d23b04529 100644
--- a/servers/slapd/operation.c
+++ b/servers/slapd/operation.c
@@ -3,9 +3,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"
 
 extern time_t		currenttime;
diff --git a/servers/slapd/phonetic.c b/servers/slapd/phonetic.c
index 996c68160cb75b66b168acae343e7491e0ab30e3..485e3718a758a0a728048d767b59d4ed51cfac96 100644
--- a/servers/slapd/phonetic.c
+++ b/servers/slapd/phonetic.c
@@ -3,15 +3,15 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include "portable.h"
+
+#include <ac/ctype.h>
+#include <ac/string.h>
+#include <ac/socket.h>
+#include <ac/time.h>
+
 #include "slap.h"
 
-#if !defined(METAPHONE) && !defined(SOUNDEX)
+#if !defined(METAPHONE) && !defined(SLAPD_PHONETIC)
 #define METAPHONE
 #endif
 
@@ -79,7 +79,7 @@ word_dup( char *w )
 #define MAXPHONEMELEN	4
 #endif
 
-#if defined(SOUNDEX)
+#if defined(SLAPD_PHONETIC)
 
 /* lifted from isode-8.0 */
 char *
@@ -430,4 +430,4 @@ phonetic( char *Word )
 }
 
 #endif /* metaphone */
-#endif /* soundex */
+#endif /* SLAPD_PHONETIC */
diff --git a/servers/slapd/repl.c b/servers/slapd/repl.c
index cd0198ebd754c2af46f35a0921029eeaae760224..4450be586654bea2683ca1ae67143c6e6ae12490 100644
--- a/servers/slapd/repl.c
+++ b/servers/slapd/repl.c
@@ -3,10 +3,12 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
+
+#include <ac/string.h>
+#include <ac/socket.h>
+
 #include <sys/file.h>
-#include <sys/socket.h>
+
 #include "slap.h"
 
 extern pthread_mutex_t	replog_mutex;
diff --git a/servers/slapd/result.c b/servers/slapd/result.c
index 185cc0269d511c3440c573538ba6df451050d801..07bf54540f831e380dab0ab541c9777089b5e285 100644
--- a/servers/slapd/result.c
+++ b/servers/slapd/result.c
@@ -3,19 +3,14 @@
 #include "portable.h"
 
 #include <stdio.h>
+
+#include <ac/errno.h>
+#include <ac/signal.h>
+#include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <errno.h>
-#include <signal.h>
-#include "slap.h"
 
-#ifdef DECL_SYS_ERRLIST
-extern int		sys_nerr;
-extern char		*sys_errlist[];
-#endif
+#include "slap.h"
 
 extern int		active_threads;
 extern pthread_mutex_t	active_threads_mutex;
@@ -63,7 +58,7 @@ send_ldap_result2(
 		break;
 	}
 
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
 	    == NULLBER ) {
 #else
@@ -73,13 +68,13 @@ send_ldap_result2(
 		return;
 	}
 
-#ifdef CLDAP
+#ifdef LDAP_CONNECTIONLESS
 	if ( op->o_cldap ) {
 		rc = ber_printf( ber, "{is{t{ess}}}", op->o_msgid, "", tag,
 		    err, matched ? matched : "", text ? text : "" );
 	} else
 #endif
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	if ( conn->c_version == 30 ) {
 		rc = ber_printf( ber, "{it{{ess}}}", op->o_msgid, tag, err,
 		    matched ? matched : "", text ? text : "" );
@@ -124,7 +119,7 @@ send_ldap_result2(
 		active_threads--;
 		conn->c_writewaiter = 1;
 
-#ifdef linux
+#ifdef HAVE_LINUX_THREADS
 		pthread_kill( listener_tid, SIGSTKFLT );
 #else /* !linux */
 		pthread_kill( listener_tid, SIGUSR1 );
@@ -159,7 +154,7 @@ send_ldap_result(
     char	*text
 )
 {
-#ifdef CLDAP
+#ifdef LDAP_CONNECTIONLESS
 	if ( op->o_cldap ) {
 		SAFEMEMCPY( (char *)conn->c_sb.sb_useaddr, &op->o_clientaddr,
 		    sizeof( struct sockaddr ));
@@ -213,7 +208,7 @@ send_search_entry(
 
 	edn = dn_normalize_case( strdup( e->e_dn ) );
 
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	if ( (ber = ber_alloc_t( conn->c_version == 30 ? 0 : LBER_USE_DER ))
 		== NULLBER )
 #else
@@ -226,7 +221,7 @@ send_search_entry(
 		goto error_return;
 	}
 
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	if ( conn->c_version == 30 ) {
 		rc = ber_printf( ber, "{it{{s{", op->o_msgid,
 		    LDAP_RES_SEARCH_ENTRY, e->e_dn );
@@ -315,7 +310,7 @@ send_search_entry(
 
 	free(edn);
 
-#ifdef COMPAT30
+#ifdef LDAP_COMPAT30
 	if ( conn->c_version == 30 ) {
 		rc = ber_printf( ber, "}}}}" );
 	} else
diff --git a/servers/slapd/schema.c b/servers/slapd/schema.c
index fc5f0214faeb94c0b14ebfd25697cebce659a1ce..a324a60d1d8ca26a85fa06d025953bc21d763ef5 100644
--- a/servers/slapd/schema.c
+++ b/servers/slapd/schema.c
@@ -3,9 +3,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"
 
 extern Attribute	*attr_find();
diff --git a/servers/slapd/schemaparse.c b/servers/slapd/schemaparse.c
index 70edf66b8bcc422745e16d5afac4baaa2c0aebf0..8f72136abbc2711209d36b66d69ab713c32d4fcc 100644
--- a/servers/slapd/schemaparse.c
+++ b/servers/slapd/schemaparse.c
@@ -3,9 +3,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"
 
 extern char		**str2charray();
diff --git a/servers/slapd/search.c b/servers/slapd/search.c
index 721c013f84dee4bfef97a620d4e8b04f3e180d10..a1c34b76dac8192bd7c5487012fbfac9d7e7b3d8 100644
--- a/servers/slapd/search.c
+++ b/servers/slapd/search.c
@@ -13,9 +13,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/shell-backends/Makefile.in b/servers/slapd/shell-backends/Makefile.in
index aab59ca8fcfdaa47867aaeacd111d5ff045ec6f2..8789dede37e4fd09b9ff5422f1641d827eedc0d4 100644
--- a/servers/slapd/shell-backends/Makefile.in
+++ b/servers/slapd/shell-backends/Makefile.in
@@ -7,6 +7,11 @@ OBJS = passwd-shell.o shellutil.o
 LDAP_INCDIR= ../../../include       
 LDAP_LIBDIR= ../../../libraries
 
+BUILD_OPT = "--enable-shell"
+BUILD_SRV = @BUILD_SHELL@
+
+all-local: $(PROGRAMS)
+
 passwd-shell:	pwd-version.o
 	$(CC) $(LDFLAGS) -o $@ $(OBJS) pwd-version.o $(LIBS)
 
diff --git a/servers/slapd/shell-backends/passwd-shell.c b/servers/slapd/shell-backends/passwd-shell.c
index fd354e0fe4b6530df645ffc1c98ba6a1422c91a7..2d7df71c74b31030c164bb4155478b76a6b141b4 100644
--- a/servers/slapd/shell-backends/passwd-shell.c
+++ b/servers/slapd/shell-backends/passwd-shell.c
@@ -15,27 +15,21 @@
 
 #include "portable.h"
 
-#include <sys/types.h>
 #include <stdio.h>
-#include <string.h>
 #include <stdlib.h>
 #include <pwd.h>
-#include <varargs.h>
+
+#include <ac/string.h>
+
 #include <lber.h>
 #include <ldap.h>
+
 #include "shellutil.h"
 #include "passwd-shell.h"
 
 
-#ifdef LDAP_DEBUG
-void debug_printf();
-#else /* LDAP_DEBUG */
-#define debug_printf()
-#endif /* LDAP_DEBUG */
-
-
-static void pwdfile_search( struct ldop *op, FILE *ofp );
-static struct ldentry *pw2entry( struct ldop *op, struct passwd *pw );
+static void pwdfile_search LDAP_P(( struct ldop *op, FILE *ofp ));
+static struct ldentry *pw2entry LDAP_P(( struct ldop *op, struct passwd *pw ));
 
 static char	tmpbuf[ MAXLINELEN * 2 ];
 
diff --git a/servers/slapd/shell-backends/shellutil.c b/servers/slapd/shell-backends/shellutil.c
index e1a6d030a480b5830d6292c521e1fc59e1ef7988..4f7b4690433b21c92f4518ed1ddaeb245754204c 100644
--- a/servers/slapd/shell-backends/shellutil.c
+++ b/servers/slapd/shell-backends/shellutil.c
@@ -16,12 +16,19 @@
 
 #include "portable.h"
 
-#include <sys/types.h>
 #include <stdio.h>
-#include <string.h>
+
+#ifdef STDC_HEADERS
 #include <stdlib.h>
-#include <pwd.h>
+#include <stdarg.h>
+#else
 #include <varargs.h>
+#endif
+
+#include <pwd.h>
+
+#include <ac/string.h>
+
 #include <lber.h>
 #include <ldap.h>
 #include "shellutil.h"
@@ -316,19 +323,30 @@ ecalloc( unsigned nelem, unsigned elsize )
 
 /* VARARGS */
 void
-debug_printf( va_alist /* char *fmt, args... */ )
+debug_printf
+#ifdef STDC_HEADERS
+	( char *fmt, ... )
+#else
+	( va_alist )
     va_dcl
+#endif
 {
-    char	*fmt;
     va_list	ap;
-
-    if ( debugflg ) {
-	va_start( ap );
-	fmt = va_arg( ap, char * );
-	fprintf( stderr, "%s: ", progname );
-	vfprintf( stderr, fmt, ap );
-	va_end( ap );
-    }
+#ifndef STDC_HEADERS
+    char	*fmt;
+#endif
+
+	if ( debugflg ) {
+#ifdef STDC_HEADERS
+		va_start( ap, fmt );
+#else
+		va_start( ap );
+		fmt = va_arg( ap, char * );
+#endif
+		fprintf( stderr, "%s: ", progname );
+		vfprintf( stderr, fmt, ap );
+		va_end( ap );
+	}
 }
 
 
diff --git a/servers/slapd/shell-backends/shellutil.h b/servers/slapd/shell-backends/shellutil.h
index 8c52ab5dcf0547bdf7e1897efd73fde92ce59516..58775b2f99f5d99eca24284ad4e868fb94680b20 100644
--- a/servers/slapd/shell-backends/shellutil.h
+++ b/servers/slapd/shell-backends/shellutil.h
@@ -75,7 +75,7 @@ struct ldentry {
 
 
 #ifdef LDAP_DEBUG
-void debug_printf();
+void debug_printf LDAP_P((char *, ...));
 #else /* LDAP_DEBUG */
 #define debug_printf()
 #endif /* LDAP_DEBUG */
diff --git a/servers/slapd/slap.h b/servers/slapd/slap.h
index f5dee02400a728e06155cd0c69bf1fc019e93b76..fad750b28d03330690ee2066009cdece6dca46d7 100644
--- a/servers/slapd/slap.h
+++ b/servers/slapd/slap.h
@@ -139,7 +139,7 @@ struct access {
 	char		*a_dnattr;
 	long		a_access;
 
-#ifdef SLAPD_ACLGROUP
+#ifdef SLAPD_ACLGROUPS
     char		*a_group;
 #endif
 
@@ -211,7 +211,7 @@ typedef struct backend {
 	IFP	be_init;	/* backend init routine			   */
 	IFP	be_close;	/* backend close routine		   */
 
-#ifdef ACLGROUP
+#ifdef SLAPD_ACLGROUPS
 	IFP	be_group;	/* backend group member test               */
 #endif
 } Backend;
diff --git a/servers/slapd/str2filter.c b/servers/slapd/str2filter.c
index 46b80f5fe678b6393eec854e80cc98674c8518da..5aea1d55b3b5bf248ab8d8e39077f0dbd110ffda 100644
--- a/servers/slapd/str2filter.c
+++ b/servers/slapd/str2filter.c
@@ -3,9 +3,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"
 
 static char	*find_matching_paren();
diff --git a/servers/slapd/strdup.c b/servers/slapd/strdup.c
index ec7398d4a88300cea5dee303d6abe2371b074e7a..d9c24e69cb8232af19e7037bfc551b1d62453afc 100644
--- a/servers/slapd/strdup.c
+++ b/servers/slapd/strdup.c
@@ -1,8 +1,8 @@
 #include "portable.h"
 
-#if defined( ultrix ) || defined( nextstep )
+#ifndef HAVE_STRDUP
 
-#include <string.h>
+#include <ac/string.h>
 
 char *strdup( char *s )
 {
@@ -16,4 +16,4 @@ char *strdup( char *s )
         return( p );
 }
 
-#endif /* ultrix || nextstep */
+#endif /* !strdup */
diff --git a/servers/slapd/tempnam.c b/servers/slapd/tempnam.c
index 53c0e502286830444d5d8554d9910bc10a4690ea..9aa21870b874f073dcf6b84067db9c69b4a17444 100644
--- a/servers/slapd/tempnam.c
+++ b/servers/slapd/tempnam.c
@@ -1,10 +1,8 @@
 #include "portable.h"
 
-#if defined( nextstep )
+#ifndef HAVE_TEMPNAME
 
-#include <string.h>
-
-char *tempnam( char *dir, char *pfx );
+#include <ac/string.h>
 
 char *tempnam( char *dir, char *pfx )
 {
diff --git a/servers/slapd/unbind.c b/servers/slapd/unbind.c
index 487fbbead5af88c2fd0e214850d8d0be12863c74..1b0d820e68aa11be91e7e98fab34fdfc2503277c 100644
--- a/servers/slapd/unbind.c
+++ b/servers/slapd/unbind.c
@@ -16,8 +16,9 @@
 #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/value.c b/servers/slapd/value.c
index 5be4338ed0050f52b6541189c2f15b3898946096..f5ee5f47772798de588772427fce2c35da80247a 100644
--- a/servers/slapd/value.c
+++ b/servers/slapd/value.c
@@ -1,13 +1,16 @@
 /* value.c - routines for dealing with values */
 
 #include "portable.h"
+
 #include <stdio.h>
-#include <string.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/ctype.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+
 #include <sys/stat.h>
-#include "portable.h"
+
 #include "slap.h"
 
 int
@@ -100,7 +103,7 @@ value_normalize(
 	*d = '\0';
 }
 
-#define MIN( a, b )	(a < b ? a : b )
+#define LDAP_MIN( a, b )	((a) < (b) ? (a) : (b) )
 
 int
 value_cmp(
@@ -134,7 +137,7 @@ value_cmp(
 		break;
 
 	case SYNTAX_BIN:
-		rc = memcmp( v1->bv_val, v2->bv_val, MIN( v1->bv_len,
+		rc = memcmp( v1->bv_val, v2->bv_val, LDAP_MIN( v1->bv_len,
 		    v2->bv_len ) );
 		break;
 	}