diff --git a/acconfig.h b/acconfig.h
index 1fdeb67cb3037a22b3a6a8ce6d5347f63719b35e..7c535afd245c29293f1defa2323454b1b8d12057 100644
--- a/acconfig.h
+++ b/acconfig.h
@@ -108,9 +108,6 @@
 /* define this for LDAP process title support */
 #undef LDAP_PROCTITLE
 
-/* define this for LDAP referrals support */
-#undef LDAP_REFERRALS
-
 /* define this for LDAP User Interface support */
 #undef LDAP_LIBUI
 
@@ -168,5 +165,11 @@
 /* define this to use SLAPD shell backend */
 #undef SLAPD_SHELL
 
+/* These are defined in ldap_features.h */
+/*
+	LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
+	LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
+*/
+
 
 /* Leave that blank line there!!  Autoheader needs it. */
diff --git a/clients/tools/ldapdelete.c b/clients/tools/ldapdelete.c
index d4784b245b0fca61b20949bf78aaa21718c15b42..5349065d042f6f1557c71addd2246b0473be6382 100644
--- a/clients/tools/ldapdelete.c
+++ b/clients/tools/ldapdelete.c
@@ -108,10 +108,11 @@ main( argc, argv )
 	exit( 1 );
     }
 
-#if LDAP_VERSION > LDAP_VERSION2
-	/* this seems prudent */
-	ldap_set_option( LDAP_OPT_DEREF, LDAP_DEREF_NEVER);
-#endif
+	{
+		/* this seems prudent */
+		int deref = LDAP_DEREF_NEVER;
+		ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
+	}
 
     if ( !kerberos ) {
 	authmethod = LDAP_AUTH_SIMPLE;
diff --git a/clients/tools/ldapsearch.c b/clients/tools/ldapsearch.c
index ba586cea9c0f59afd56b36885c90929b5a3baf34..399088a0d2dc75e509840e33d03845df618b65ca 100644
--- a/clients/tools/ldapsearch.c
+++ b/clients/tools/ldapsearch.c
@@ -35,9 +35,9 @@ char	*s;
     fprintf( stderr, "    -A\t\tretrieve attribute names only (no values)\n" );
     fprintf( stderr, "    -B\t\tdo not suppress printing of non-ASCII values\n" );
     fprintf( stderr, "    -L\t\tprint entries in LDIF format (-B is implied)\n" );
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
     fprintf( stderr, "    -R\t\tdo not automatically follow referrals\n" );
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
     fprintf( stderr, "    -d level\tset LDAP debugging level to `level'\n" );
     fprintf( stderr, "    -F sep\tprint `sep' instead of `=' between attribute names and values\n" );
     fprintf( stderr, "    -S attr\tsort the results by attribute `attr'\n" );
diff --git a/configure b/configure
index 427f73df7e8de858caa8c20380d0e0bd8ebab15f..6b9cfe80211907ecb580b92208401516633687cc 100755
--- a/configure
+++ b/configure
@@ -21,9 +21,9 @@ ac_help="$ac_help
 ac_help="$ac_help
   --enable-cache	enable caching (yes)"
 ac_help="$ac_help
-  --enable-dns		enable dns support (no)"
+  --enable--dns		enable V2 DNS  extension (no)"
 ac_help="$ac_help
-  --enable-referrals	enable referrals (yes)"
+  --enable-referrals	enable V2 Referrals extension (yes)"
 ac_help="$ac_help
   --enable-cldap	enable connectionless ldap (no)"
 ac_help="$ac_help
@@ -6774,7 +6774,7 @@ EOF
 fi
 if test $ol_enable_dns != no ; then
 	cat >> confdefs.h <<\EOF
-#define LDAP_DNS 1
+#define LDAP_API_FEATURE_X_OPENLDAP_V2_DNS LDAP_API_VENDOR_VERSION
 EOF
 
 fi
@@ -6786,7 +6786,7 @@ EOF
 fi
 if test $ol_enable_referrals != no ; then
 	cat >> confdefs.h <<\EOF
-#define LDAP_REFERRALS 1
+#define LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS LDAP_API_VENDOR_VERSION
 EOF
 
 fi
@@ -6889,7 +6889,6 @@ fi
 
 
 
-
 
 
 trap '' 1 2 15
@@ -7028,7 +7027,7 @@ servers/slapd/shell-backends/Makefile:build/top.mk:servers/slapd/shell-backends/
 servers/slapd/tools/Makefile:build/top.mk:servers/slapd/tools/Makefile.in \
 servers/slurpd/Makefile:build/top.mk:servers/slurpd/Makefile.in:build/srv.mk \
 tests/Makefile:build/top.mk:tests/Makefile.in \
- include/portable.h" | sed "s/:[^ ]*//g"` conftest*; exit 1' 1 2 15
+ include/portable.h include/ldap_features.h" | sed "s/:[^ ]*//g"` conftest*; exit 1' 1 2 15
 EOF
 cat >> $CONFIG_STATUS <<EOF
 
@@ -7242,7 +7241,7 @@ ac_eD='%g'
 if test "${CONFIG_HEADERS+set}" != set; then
 EOF
 cat >> $CONFIG_STATUS <<EOF
-  CONFIG_HEADERS="include/portable.h"
+  CONFIG_HEADERS="include/portable.h include/ldap_features.h"
 EOF
 cat >> $CONFIG_STATUS <<\EOF
 fi
diff --git a/configure.in b/configure.in
index 3e7d77524f7b5fc3642c07f830aa4000033b584d..d12389bba58c0c50363c2671e8b5067fd5b57f97 100644
--- a/configure.in
+++ b/configure.in
@@ -15,7 +15,7 @@ dnl Do not use AutoConf 2.12; it produces a configuration script
 dnl that causes an "internal 2K buffer" error on HPUX when run
 dnl with /bin/sh.  Autoconf 2.10 seems to be okay.
 AC_CONFIG_AUX_DIR(build)
-AC_CONFIG_HEADER(include/portable.h)dnl
+AC_CONFIG_HEADER(include/portable.h include/ldap_features.h)dnl
 dnl
 dnl Start Args
 AC_MSG_CHECKING(configure arguments)
@@ -27,8 +27,8 @@ dnl OL_ARG_ENABLE(syslog,[  --enable-syslog	enable syslog support], auto)dnl
 OL_ARG_ENABLE(proctitle,[  --enable-proctitle	enable proctitle support], yes)dnl
 OL_ARG_ENABLE(libui,[  --enable-libui	enable library user interface], yes)dnl
 OL_ARG_ENABLE(cache,[  --enable-cache	enable caching], yes)dnl
-OL_ARG_ENABLE(dns,[  --enable-dns		enable dns support], no)dnl
-OL_ARG_ENABLE(referrals,[  --enable-referrals	enable referrals], yes)dnl
+OL_ARG_ENABLE(dns,[  --enable--dns		enable V2 DNS  extension], no)dnl
+OL_ARG_ENABLE(referrals,[  --enable-referrals	enable V2 Referrals extension], yes)dnl
 OL_ARG_ENABLE(cldap,[  --enable-cldap	enable connectionless ldap], no)dnl
 OL_ARG_ENABLE(x_compile,[  --enable-x-compile	enable cross compiling],
 	no, [yes no])dnl
@@ -953,13 +953,13 @@ if test $ol_enable_cache = no ; then
 	AC_DEFINE(LDAP_NOCACHE,1)
 fi
 if test $ol_enable_dns != no ; then
-	AC_DEFINE(LDAP_DNS,1)
+	AC_DEFINE(LDAP_API_FEATURE_X_OPENLDAP_V2_DNS,LDAP_API_VENDOR_VERSION)
 fi
 if test $ol_enable_proctitle != no ; then
 	AC_DEFINE(LDAP_PROCTITLE,1)
 fi
 if test $ol_enable_referrals != no ; then
-	AC_DEFINE(LDAP_REFERRALS,1)
+	AC_DEFINE(LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS,LDAP_API_VENDOR_VERSION)
 fi
 if test $ol_enable_cldap != no ; then
 	AC_DEFINE(LDAP_CONNECTIONLESS,1)
@@ -1036,12 +1036,6 @@ dnl ----------------------------------------------------------------
 dnl final output
 dnl
 
-dnl AC_OUTPUT( \
-dnl contrib/Makefile:build/top.mk:contrib/Makefile.in:build/dir.mk \
-dnl contrib/saucer/Makefile:build/top.mk:contrib/saucer/Makefile.in:build/rules.mk \
-dnl contrib/whois++/Makefile:build/top.mk:contrib/whois++/Makefile.in:build/rules.mk \
-dnl [date > stamp-h])
-
 AC_OUTPUT( \
 Makefile:build/top.mk:Makefile.in:build/dir.mk \
 doc/Makefile:build/top.mk:doc/Makefile.in:build/dir.mk \
diff --git a/include/Makefile.in b/include/Makefile.in
index 2fc4e8b936af9fdcd5902e68a09527d503491472..f3cb2013c31c79eae8ff54f5d72f29ef42f80d42 100644
--- a/include/Makefile.in
+++ b/include/Makefile.in
@@ -10,6 +10,7 @@ install-local: FORCE
 		$(INSTALL) $(INSTALLFLAGS) -m 644 ldap.h $(includedir)
 		$(INSTALL) $(INSTALLFLAGS) -m 644 lber.h $(includedir)
 		$(INSTALL) $(INSTALLFLAGS) -m 644 ldap_cdefs.h $(includedir)
+		$(INSTALL) $(INSTALLFLAGS) -m 644 ldap_features.h $(includedir)
 		$(INSTALL) $(INSTALLFLAGS) -m 644 disptmpl.h $(includedir)
 		$(INSTALL) $(INSTALLFLAGS) -m 644 srchpref.h $(includedir)
 
diff --git a/include/ldap.h b/include/ldap.h
index c2501db475727614f87b6622ce9653516522cf16..a5c32e33180eebd5fb3ec47e17c14b7ef92c44f7 100644
--- a/include/ldap.h
+++ b/include/ldap.h
@@ -37,6 +37,9 @@ LDAP_BEGIN_DECL
 /* We'll eventually release as 200 */
 #define LDAP_VENDOR_VERSION	190
 
+/* include LDAP_API_FEATURE defines */
+#include <ldap_features.h>
+
 #define LDAP_COMPAT20
 #define LDAP_COMPAT30
 #if defined(LDAP_COMPAT20) || defined(LDAP_COMPAT30)
@@ -306,7 +309,7 @@ typedef struct ldapmsg LDAPMessage;
 #define NULLMSG	((LDAPMessage *) NULL)
 
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 /*
  * structure for tracking LDAP server host, ports, DNs, etc.
  */
@@ -359,7 +362,7 @@ typedef struct ldapreq {
 	struct ldapreq	*lr_prev;	/* previous request */
 	struct ldapreq	*lr_next;	/* next request */
 } LDAPRequest;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
 /*
diff --git a/include/ldap_features.h.in b/include/ldap_features.h.in
new file mode 100644
index 0000000000000000000000000000000000000000..105aaeb495675ffd0737d2e0171f7cc976506c4e
--- /dev/null
+++ b/include/ldap_features.h.in
@@ -0,0 +1,13 @@
+/* 
+ * LDAP Features
+ */
+#ifndef _LDAP_FEATURES_H
+#define _LDAP_FEATURES_H 1
+
+/* LDAP v2 DNS */
+#undef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
+
+/* LDAP v2 Referrals */
+#undef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
+
+#endif /* LDAP_FEATURES */
diff --git a/include/portable.h.in b/include/portable.h.in
index b23b58cca4c5634ebf61582b530b557955b86f1d..6d5c210f1d20fbe79521a97119f21fd21aac39b3 100644
--- a/include/portable.h.in
+++ b/include/portable.h.in
@@ -199,18 +199,12 @@ is provided ``as is'' without express or implied warranty.
 /* define this to add debugging code */
 #undef LDAP_DEBUG
 
-/* define this for LDAP DNS support */
-#undef LDAP_DNS
-
 /* define this to remove -lldap cache support */
 #undef LDAP_NOCACHE
 
 /* define this for LDAP process title support */
 #undef LDAP_PROCTITLE
 
-/* define this for LDAP referrals support */
-#undef LDAP_REFERRALS
-
 /* define this for LDAP User Interface support */
 #undef LDAP_LIBUI
 
@@ -256,6 +250,12 @@ is provided ``as is'' without express or implied warranty.
 /* define this to use SLAPD shell backend */
 #undef SLAPD_SHELL
 
+/* These are defined in ldap_features.h */
+/*
+ LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
+ LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
+*/
+
 /* Define if you have the bcopy function.  */
 #undef HAVE_BCOPY
 
diff --git a/libraries/libldap/abandon.c b/libraries/libldap/abandon.c
index 21639d3305c59bae40351f390eda0ac3e30a71ea..ebc6bca5dbc1bd22cb644a9e307a6e4fe782afad 100644
--- a/libraries/libldap/abandon.c
+++ b/libraries/libldap/abandon.c
@@ -47,9 +47,9 @@ do_abandon( LDAP *ld, int origid, int msgid )
 	BerElement	*ber;
 	int		i, err, sendabandon;
 	Sockbuf		*sb;
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	LDAPRequest	*lr;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 	/*
 	 * An abandon request looks like this:
@@ -61,7 +61,7 @@ do_abandon( LDAP *ld, int origid, int msgid )
 
 	sendabandon = 1;
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	/* find the request that we are abandoning */
 	for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
 		if ( lr->lr_msgid == msgid ) {	/* this message */
@@ -83,7 +83,7 @@ do_abandon( LDAP *ld, int origid, int msgid )
 			sendabandon = 0;
 		}
 	}
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 	if ( ldap_msgdelete( ld, msgid ) == 0 ) {
 		ld->ld_errno = LDAP_SUCCESS;
@@ -115,15 +115,15 @@ do_abandon( LDAP *ld, int origid, int msgid )
 				ber_free( ber, 1 );
 			} else {
 				/* send the message */
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 				if ( lr != NULL ) {
 					sb = lr->lr_conn->lconn_sb;
 				} else {
 					sb = &ld->ld_sb;
 				}
-#else /* LDAP_REFERRALS */
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 				sb = &ld->ld_sb;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 				if ( ber_flush( sb, ber, 1 ) != 0 ) {
 					ld->ld_errno = LDAP_SERVER_DOWN;
 					err = -1;
@@ -134,7 +134,7 @@ do_abandon( LDAP *ld, int origid, int msgid )
 		}
 	}
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	if ( lr != NULL ) {
 		if ( sendabandon ) {
 			ldap_free_connection( ld, lr->lr_conn, 0, 1 );
@@ -143,7 +143,7 @@ do_abandon( LDAP *ld, int origid, int msgid )
 			ldap_free_request( ld, lr );
 		}
 	}
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
 	if ( ld->ld_abandoned == NULL ) {
diff --git a/libraries/libldap/bind.c b/libraries/libldap/bind.c
index 1668a3e73a24895509f6e4ffad8e33e7330acf59..0c3676d39d4f76092dfcb1e5902be34c6542342b 100644
--- a/libraries/libldap/bind.c
+++ b/libraries/libldap/bind.c
@@ -113,11 +113,11 @@ ldap_bind_s( LDAP *ld, char *dn, char *passwd, int authmethod )
 }
 
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 void
 ldap_set_rebind_proc( LDAP *ld, int (*rebindproc)( LDAP *ld, char **dnp,
 	char **passwdp, int *authmethodp, int freeit ))
 {
 	ld->ld_rebindproc = rebindproc;
 }
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
diff --git a/libraries/libldap/cldap.c b/libraries/libldap/cldap.c
index bf77d44ba5ea487d1c131c48ce92b1eabc8e4406..5ce38dee7d7a88656ed3ed0ad0f7670947a90a1a 100644
--- a/libraries/libldap/cldap.c
+++ b/libraries/libldap/cldap.c
@@ -151,9 +151,9 @@ cldap_open( char *host, int port )
     }
 
     if ( ld->ld_sb.sb_addrs == NULL
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	    || ( ld->ld_defconn = ldap_new_connection( ld, NULL, 1,0,0 )) == NULL
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 	    ) {
 	free( ld );
 	return( NULL );
diff --git a/libraries/libldap/getdxbyname.c b/libraries/libldap/getdxbyname.c
index 645b1048181814e6949d8ea341a80c6b736cbad7..c9ec19f9acf4d59d2519a1d0016ba9a03d4a4088 100644
--- a/libraries/libldap/getdxbyname.c
+++ b/libraries/libldap/getdxbyname.c
@@ -7,7 +7,7 @@
 
 #include "portable.h"
 
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -195,4 +195,4 @@ decode_answer( unsigned char *answer, int len )
     return( dxs );
 }
 
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
diff --git a/libraries/libldap/kbind.c b/libraries/libldap/kbind.c
index 90367171a9a74c70cc0b6d9518b18f906b9904a2..13cb38eff78644c091418b189f05defcac3e4638 100644
--- a/libraries/libldap/kbind.c
+++ b/libraries/libldap/kbind.c
@@ -260,11 +260,11 @@ ldap_get_kerberosv4_credentials( LDAP *ld, char *who, char *service, int *len )
 		return( NULL );
 	}
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	krbinstance = ld->ld_defconn->lconn_krbinstance;
-#else /* LDAP_REFERRALS */
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 	krbinstance = ld->ld_host;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 	if ( (err = krb_mk_req( &ktxt, service, krbinstance, realm, 0 ))
 	    != KSUCCESS ) {
diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h
index 89402feec154ae6d878a6a0b44ac2473505d29c3..166ecd5563a6ac899464b1f6c1f529f38f47ba15 100644
--- a/libraries/libldap/ldap-int.h
+++ b/libraries/libldap/ldap-int.h
@@ -19,16 +19,16 @@ LDAP_BEGIN_DECL
 #define LDAP_URL_URLCOLON	"URL:"
 #define LDAP_URL_URLCOLON_LEN	4
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 #define LDAP_REF_STR		"Referral:\n"
 #define LDAP_REF_STR_LEN	10
 #define LDAP_LDAP_REF_STR	LDAP_URL_PREFIX
 #define LDAP_LDAP_REF_STR_LEN	LDAP_URL_PREFIX_LEN
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 #define LDAP_DX_REF_STR		"dx://"
 #define LDAP_DX_REF_STR_LEN	5
-#endif /* LDAP_DNS */
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 #define LDAP_BOOL_REFERRALS		0
 #define LDAP_BOOL_RESTART		1
@@ -104,11 +104,11 @@ struct ldap {
 	int		ld_msgid;
 
 	/* do not mess with these */
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	LDAPRequest	*ld_requests;	/* list of outstanding requests */
-#else /* LDAP_REFERRALS */
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 	LDAPMessage	*ld_requests;	/* list of outstanding requests */
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 	LDAPMessage	*ld_responses;	/* list of outstanding responses */
 	int		*ld_abandoned;	/* array of abandoned requests */
 	char		ld_attrbuffer[LDAP_MAX_ATTR_LEN];
@@ -121,14 +121,14 @@ struct ldap {
 	BERTranslateProc ld_lber_encode_translate_proc;
 	BERTranslateProc ld_lber_decode_translate_proc;
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	LDAPConn	*ld_defconn;	/* default connection */
 	LDAPConn	*ld_conns;	/* list of server connections */
 	void		*ld_selectinfo;	/* platform specifics for select */
 	int		(*ld_rebindproc)( struct ldap *ld, char **dnp,
 				char **passwdp, int *authmethodp, int freeit );
 				/* routine to get info needed for re-bind */
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 };
 
 
@@ -176,7 +176,7 @@ void ldap_close_connection( Sockbuf *sb );
 char *ldap_host_connected_to( Sockbuf *sb );
 #endif /* HAVE_KERBEROS */
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 int do_ldap_select( LDAP *ld, struct timeval *timeout );
 void *ldap_new_select_info( void );
 void ldap_free_select_info( void *sip );
@@ -185,7 +185,7 @@ void ldap_mark_select_read( LDAP *ld, Sockbuf *sb );
 void ldap_mark_select_clear( LDAP *ld, Sockbuf *sb );
 int ldap_is_read_ready( LDAP *ld, Sockbuf *sb );
 int ldap_is_write_ready( LDAP *ld, Sockbuf *sb );
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
 /*
@@ -196,7 +196,7 @@ int ldap_send_initial_request( LDAP *ld, unsigned long msgtype,
 BerElement *ldap_alloc_ber_with_options( LDAP *ld );
 void ldap_set_ber_options( LDAP *ld, BerElement *ber );
 
-#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
+#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
 int ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid,
 	LDAPRequest *parentreq, LDAPServer *srvlist, LDAPConn *lc,
 	int bind );
@@ -207,12 +207,12 @@ void ldap_free_request( LDAP *ld, LDAPRequest *lr );
 void ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind );
 void ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all );
 void ldap_dump_requests_and_responses( LDAP *ld );
-#endif /* LDAP_REFERRALS || LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 int ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp );
 int ldap_append_referral( LDAP *ld, char **referralsp, char *s );
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
 /*
@@ -228,12 +228,12 @@ BerElement *ldap_build_search_req( LDAP *ld, char *base, int scope,
 int ldap_ld_free( LDAP *ld, int close );
 int ldap_send_unbind( LDAP *ld, Sockbuf *sb );
 
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 /*
  * in getdxbyname.c
  */
 char **ldap_getdxbyname( char *domain );
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
 /*
diff --git a/libraries/libldap/open.c b/libraries/libldap/open.c
index 0cbb8736071e88a02bdc97dac1eef75d7eaeea31..fa383284b4159caed78ee2110212af88361ceed9 100644
--- a/libraries/libldap/open.c
+++ b/libraries/libldap/open.c
@@ -43,9 +43,9 @@ LDAP *
 ldap_open( char *host, int port )
 {
 	LDAP		*ld;
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	LDAPServer	*srv;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 	Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
 
@@ -53,7 +53,7 @@ ldap_open( char *host, int port )
 		return( NULL );
 	}
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
 	    NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
 	    strdup( ld->ld_defhost )) == NULL )) {
@@ -70,13 +70,13 @@ ldap_open( char *host, int port )
 	}
 	++ld->ld_defconn->lconn_refcnt;	/* so it never gets closed/freed */
 
-#else /* LDAP_REFERRALS */
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 	if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
 	    ld->ld_defport, &ld->ld_host, 0 ) < 0 ) {
 		ldap_ld_free( ld, 0 );
 		return( NULL );
 	}
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 	Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
 		( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
@@ -146,7 +146,7 @@ ldap_init( char *defhost, int defport )
 		return( NULL );
 	}
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
 		free( (char*)ld );
 	    WSACleanup( );
@@ -161,9 +161,9 @@ ldap_init( char *defhost, int defport )
 
 	if ( defhost != NULL &&
 	    ( ld->ld_defhost = strdup( defhost )) == NULL ) {
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 		ldap_free_select_info( ld->ld_selectinfo );
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 		free( (char*)ld );
 	    WSACleanup( );
 		return( NULL );
@@ -175,9 +175,9 @@ ldap_init( char *defhost, int defport )
 	ld->ld_lberoptions = LBER_USE_DER;
 	ld->ld_options.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
 	ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
diff --git a/libraries/libldap/os-ip.c b/libraries/libldap/os-ip.c
index 0288968941a4f43b3be7c2ecfbd805c1b59ffbb0..4642144b20d46cab062319fa5b65d6ac35d99026 100644
--- a/libraries/libldap/os-ip.c
+++ b/libraries/libldap/os-ip.c
@@ -35,7 +35,7 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
  * if host == NULL, connect using address
  * "address" and "port" must be in network byte order
  * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
- * async is only used ifdef LDAP_REFERRALS (non-0 means don't wait for connect)
+ * async is only used ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS (non-0 means don't wait for connect)
  * XXX async is not used yet!
  */
 {
@@ -44,9 +44,9 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
 	struct sockaddr_in	sin;
 	struct hostent		*hp = NULL;
 #ifdef notyet
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	int			status;	/* for ioctl call */
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 #endif /* notyet */
 
 	Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
@@ -72,13 +72,13 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
 			return( -1 );
 		}
 #ifdef notyet
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 		status = 1;
 		if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
 			Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
 			    s, 0, 0 );
 		}
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 #endif /* notyet */
 		(void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
 		sin.sin_family = AF_INET;
@@ -97,7 +97,7 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
 			errno = WSAGetLastError();
 #endif
 #ifdef notyet
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 #ifdef EAGAIN
 			if ( errno == EINPROGRESS || errno == EAGAIN ) {
 #else /* EAGAIN */
@@ -108,7 +108,7 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
 				rc = -2;
 				break;
 			}
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 #endif /* notyet */
 
 #ifdef LDAP_DEBUG		
@@ -127,13 +127,13 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
 
 	if ( connected ) {
 #ifdef notyet
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 		status = 0;
 		if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
 			Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
 			    s, 0, 0 );
 		}
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 #endif /* notyet */
 
 		Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
@@ -183,7 +183,7 @@ ldap_host_connected_to( Sockbuf *sb )
 #endif /* HAVE_KERBEROS */
 
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 /* for UNIX */
 struct selectinfo {
 	fd_set	si_readfds;
@@ -304,4 +304,4 @@ do_ldap_select( LDAP *ld, struct timeval *timeout )
 	return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
 	    NULL, timeout ));
 }
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
diff --git a/libraries/libldap/request.c b/libraries/libldap/request.c
index 7de337c59eece5331a9c0aa9463a6d17b9a31da5..8c33f20d7c8c6d91eb7fc5e124acf31b90715278 100644
--- a/libraries/libldap/request.c
+++ b/libraries/libldap/request.c
@@ -22,21 +22,21 @@ static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of
 
 #include "ldap-int.h"
 
-#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
+#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
 static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPServer *srv, int any ));
 static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
 static void free_servers LDAP_P(( LDAPServer *srvlist ));
-#endif /* LDAP_REFERRALS || LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
 
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 static LDAPServer *dn2servers LDAP_P(( LDAP *ld, char *dn ));
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 static BerElement *re_encode_request LDAP_P(( LDAP *ld, BerElement *origber,
     int msgid, char **dnp ));
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
 BerElement *
@@ -74,13 +74,13 @@ int
 ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
 	BerElement *ber )
 {
-#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
+#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
 	LDAPServer	*servers;
-#endif /* LDAP_REFERRALS || LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
 	Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
 
-#if !defined( LDAP_REFERRALS ) && !defined( LDAP_DNS )
+#if !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) && !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
 	if ( ber_flush( &ld->ld_sb, ber, 1 ) != 0 ) {
 		ld->ld_errno = LDAP_SERVER_DOWN;
 		return( -1 );
@@ -88,9 +88,9 @@ ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
 
 	ld->ld_errno = LDAP_SUCCESS;
 	return( ld->ld_msgid );
-#else /* !LDAP_REFERRALS && !LDAP_DNS */
+#else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 	if (( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_DNS ) == LDAP_OPT_ON )
 		&& ldap_is_dns_dn( dn ) )
 	{
@@ -114,24 +114,24 @@ ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
 		}
 #endif /* LDAP_DEBUG */
 	} else {
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 		/*
 		 * use of DNS is turned off or this is an X.500 DN...
 		 * use our default connection
 		 */
 		servers = NULL;
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 	}	
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
 	return( ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, servers,
 	    NULL, 0 ));
-#endif /* !LDAP_REFERRALS && !LDAP_DNS */
+#endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 }
 
 
 
-#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
+#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
 int
 ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
 	*parentreq, LDAPServer *srvlist, LDAPConn *lc, int bind )
@@ -569,10 +569,10 @@ free_servers( LDAPServer *srvlist )
 	srvlist = nextsrv;
     }
 }
-#endif /* LDAP_REFERRALS || LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 /*
  * XXX merging of errors in this routine needs to be improved
  */
@@ -580,9 +580,9 @@ int
 ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
 {
 	int		rc, count, len, newdn;
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 	int		ldapref;
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 	char		*p, *ports, *ref, *tmpref, *refdn, *unfollowed;
 	LDAPRequest	*origreq;
 	LDAPServer	*srv;
@@ -630,9 +630,9 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
 
 	/* parse out & follow referrals */
 	for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 		ldapref = 0;
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
 		if (( p = strchr( ref, '\n' )) != NULL ) {
 			*p++ = '\0';
@@ -645,17 +645,17 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
 		    LDAP_LDAP_REF_STR, LDAP_LDAP_REF_STR_LEN ) == 0 ) {
 			Debug( LDAP_DEBUG_TRACE,
 			    "chasing LDAP referral: <%s>\n", ref, 0, 0 );
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 			ldapref = 1;
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 			tmpref = ref + LDAP_LDAP_REF_STR_LEN;
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 		} else if ( len > LDAP_DX_REF_STR_LEN && strncasecmp( ref,
 		    LDAP_DX_REF_STR, LDAP_DX_REF_STR_LEN ) == 0 ) {
 			Debug( LDAP_DEBUG_TRACE,
 			    "chasing DX referral: <%s>\n", ref, 0, 0 );
 			tmpref = ref + LDAP_DX_REF_STR_LEN;
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 		} else {
 			Debug( LDAP_DEBUG_TRACE,
 			    "ignoring unknown referral <%s>\n", ref, 0, 0 );
@@ -677,9 +677,9 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
 			return( -1 );
 		}
 
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 		if ( ldapref ) {
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 			if (( srv = (LDAPServer *)calloc( 1,
 			    sizeof( LDAPServer ))) == NULL ) {
 				ber_free( ber, 1 );
@@ -700,11 +700,11 @@ ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
 			} else {
 				srv->lsrv_port = LDAP_PORT;
 			}
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 		} else {
 			srv = dn2servers( ld, tmpref );
 		}
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
 		if ( srv != NULL && ldap_send_server_request( ld, ber, ld->ld_msgid,
 		    lr, srv, NULL, 1 ) >= 0 ) {
@@ -856,10 +856,10 @@ ldap_find_request_by_msgid( LDAP *ld, int msgid )
 
 	return( lr );
 }
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 static LDAPServer *
 dn2servers( LDAP *ld, char *dn )	/* dn can also be a domain.... */
 {
@@ -940,4 +940,4 @@ dn2servers( LDAP *ld, char *dn )	/* dn can also be a domain.... */
 
 	return( srvlist );
 }
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
diff --git a/libraries/libldap/result.c b/libraries/libldap/result.c
index 182a13ccd7d4b19c9048861ad79fe7d8dbf40143..9cc47942c9ccd0bb7c38c986503d2e6855634877 100644
--- a/libraries/libldap/result.c
+++ b/libraries/libldap/result.c
@@ -27,16 +27,16 @@ static int ldap_abandoned LDAP_P(( LDAP *ld, int msgid ));
 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, int msgid ));
 static int wait4msg LDAP_P(( LDAP *ld, int msgid, int all, struct timeval *timeout,
 	LDAPMessage **result ));
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 static int read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb, LDAPConn *lc,
 	LDAPMessage **result ));
 static unsigned long build_result_ber LDAP_P(( LDAP *ld, BerElement *ber, LDAPRequest *lr ));
 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
-#else /* LDAP_REFERRALS */
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 static int read1msg LDAP_P(( LDAP *ld, int msgid, int all, Sockbuf *sb,
 	LDAPMessage **result ));
-#endif /* LDAP_REFERRALS */
-#if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_REFERRALS )
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
+#if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS )
 static int ldap_select1 LDAP_P(( LDAP *ld, struct timeval *timeout ));
 #endif
 
@@ -139,9 +139,9 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
 	struct timeval	tv, *tvp;
 	time_t		start_time = 0;
 	time_t		tmp_time;
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	LDAPConn	*lc, *nextlc;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 #ifdef LDAP_DEBUG
 	if ( timeout == NULL ) {
@@ -163,7 +163,7 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
 		    
 	rc = -2;
 	while ( rc == -2 ) {
-#ifndef LDAP_REFERRALS
+#ifndef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 		/* hack attack */
 		if ( ld->ld_sb.sb_ber.ber_ptr >= ld->ld_sb.sb_ber.ber_end ) {
 			rc = ldap_select1( ld, tvp );
@@ -183,7 +183,7 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
 		} else {
 			rc = read1msg( ld, msgid, all, &ld->ld_sb, result );
 		}
-#else /* !LDAP_REFERRALS */
+#else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 #ifdef LDAP_DEBUG
 		if ( ldap_debug & LDAP_DEBUG_TRACE ) {
 			ldap_dump_connection( ld, ld->ld_conns, 1 );
@@ -238,7 +238,7 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
 				}
 			}
 		}
-#endif /* !LDAP_REFERRALS */
+#endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 		if ( rc == -2 && tvp != NULL ) {
 			tmp_time = time( NULL );
@@ -260,9 +260,9 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
 
 static int
 read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
     LDAPConn *lc,
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
     LDAPMessage **result )
 {
 	BerElement	ber;
@@ -270,12 +270,12 @@ read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
 	long		id;
 	unsigned long	tag, len;
 	int		foundit = 0;
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	LDAPRequest	*lr;
 	BerElement	tmpber;
 	int		rc, refer_cnt, hadref, simple_request;
 	unsigned long	lderr;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 	Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
 
@@ -302,7 +302,7 @@ read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
 		return( -2 );	/* continue looking */
 	}
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
 		Debug( LDAP_DEBUG_ANY,
 		    "no request for response with msgid %ld (tossing)\n",
@@ -314,7 +314,7 @@ read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
 	    ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" : "result", id,
 	    lr->lr_origid );
 	id = lr->lr_origid;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 	/* the message type */
 	if ( (tag = ber_peek_tag( &ber, &len )) == LBER_ERROR ) {
@@ -322,7 +322,7 @@ read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
 		return( -1 );
 	}
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	refer_cnt = 0;
 	hadref = simple_request = 0;
 	rc = -2;	/* default is to keep looking (no response found) */
@@ -424,7 +424,7 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
 		return( rc );
 	}
 
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 	/* make a new ldap message */
 	if ( (new = (LDAPMessage *) calloc( 1, sizeof(LDAPMessage) ))
 	    == NULL ) {
@@ -516,7 +516,7 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
 }
 
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 static unsigned long
 build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
 {
@@ -581,11 +581,11 @@ merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
 	    parentr->lr_res_error : "", parentr->lr_res_matched ?
 	    parentr->lr_res_matched : "" );
 }
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
 
-#if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_REFERRALS )
+#if defined( LDAP_CONNECTIONLESS ) || !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS )
 
 static int
 ldap_select1( LDAP *ld, struct timeval *timeout )
@@ -614,7 +614,7 @@ ldap_select1( LDAP *ld, struct timeval *timeout )
 	return( select( tblsize, &readfds, 0, 0, timeout ) );
 }
 
-#endif /* !LDAP_REFERRALS */
+#endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
 int
diff --git a/libraries/libldap/test.c b/libraries/libldap/test.c
index 43c02ca8fe0b36922ea5e05cff341f80e0bac4d0..1d840e4b0f69f97a258e32f6bd9c0a41dc0ee9b4 100644
--- a/libraries/libldap/test.c
+++ b/libraries/libldap/test.c
@@ -214,7 +214,7 @@ get_modlist( char *prompt1, char *prompt2, char *prompt3 )
 }
 
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 int
 bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
 	int freeit )
@@ -249,7 +249,7 @@ bind_prompt( LDAP *ld, char **dnp, char **passwdp, int *authmethodp,
 
 	return( LDAP_SUCCESS );
 }
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 
 int
@@ -532,11 +532,11 @@ main( int argc, char **argv )
 			if ( cldapflg )
 				cldap_close( ld );
 #endif /* LDAP_CONNECTIONLESS */
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 			if ( !cldapflg )
-#else /* LDAP_REFERRALS */
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 			if ( !cldapflg && bound )
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 				ldap_unbind( ld );
 			exit( 0 );
 			break;
@@ -771,15 +771,15 @@ main( int argc, char **argv )
 			}
 #endif /* STR_TRANSLATION */
 
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 			getline( line, sizeof(line), stdin,
 				"Use DN & DNS to determine where to send requests (0=no, 1=yes)?" );
 			if ( atoi( line ) != 0 ) {
 				LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_DNS);
 			}
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 			getline( line, sizeof(line), stdin,
 				"Recognize and chase referrals (0=no, 1=yes)?" );
 			if ( atoi( line ) != 0 ) {
@@ -790,7 +790,7 @@ main( int argc, char **argv )
 					ldap_set_rebind_proc( ld, bind_prompt );
 				}
 			}
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 			break;
 
 		case 'O':	/* set cache options */
diff --git a/libraries/libldap/unbind.c b/libraries/libldap/unbind.c
index 7de336d504bdf839064e49dfe89d4fc439bd0614..f46c45adc829ceb1ba959c8f9bf74338fc099899 100644
--- a/libraries/libldap/unbind.c
+++ b/libraries/libldap/unbind.c
@@ -35,12 +35,12 @@ ldap_ld_free( LDAP *ld, int close )
 {
 	LDAPMessage	*lm, *next;
 	int		err = LDAP_SUCCESS;
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	LDAPRequest	*lr, *nextlr;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 	if ( ld->ld_sb.sb_naddr == 0 ) {
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 		/* free LDAP structure and outstanding requests/responses */
 		for ( lr = ld->ld_requests; lr != NULL; lr = nextlr ) {
 			nextlr = lr->lr_next;
@@ -51,12 +51,12 @@ ldap_ld_free( LDAP *ld, int close )
 		while ( ld->ld_conns != NULL ) {
 			ldap_free_connection( ld, ld->ld_conns, 1, close );
 		}
-#else /* LDAP_REFERRALS */
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 		if ( close ) {
 			err = ldap_send_unbind( ld, &ld->ld_sb );
 			ldap_close_connection( &ld->ld_sb );
 		}
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 	} else {
 		int	i;
 
@@ -86,17 +86,17 @@ ldap_ld_free( LDAP *ld, int close )
 		free( ld->ld_ufnprefix );
 	if ( ld->ld_filtd != NULL )
 		ldap_getfilter_free( ld->ld_filtd );
-#ifndef LDAP_REFERRALS
+#ifndef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	if ( ld->ld_sb.sb_ber.ber_buf != NULL )
 		free( ld->ld_sb.sb_ber.ber_buf );
-#endif /* !LDAP_REFERRALS */
+#endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 	if ( ld->ld_abandoned != NULL )
 		free( ld->ld_abandoned );
 
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	if ( ld->ld_selectinfo != NULL )
 		ldap_free_select_info( ld->ld_selectinfo );
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 	if ( ld->ld_defhost != NULL )
 		free( ld->ld_defhost );
diff --git a/libraries/libldap/url.c b/libraries/libldap/url.c
index 2f2cf942bc419e9cc2df41d7f5fc26d97fa91fb4..b97139a12d6d399ac8c450dd0281e88795250463 100644
--- a/libraries/libldap/url.c
+++ b/libraries/libldap/url.c
@@ -243,9 +243,9 @@ ldap_url_search( LDAP *ld, char *url, int attrsonly )
 	int		err;
 	LDAPURLDesc	*ludp;
 	BerElement	*ber;
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 	LDAPServer	*srv = NULL;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
 	if ( ldap_url_parse( url, &ludp ) != 0 ) {
 		ld->ld_errno = LDAP_PARAM_ERROR;
@@ -260,7 +260,7 @@ ldap_url_search( LDAP *ld, char *url, int attrsonly )
 	err = 0;
 
 	if ( ludp->lud_host != NULL || ludp->lud_port != 0 ) {
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 		if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer )))
 		    == NULL || ( srv->lsrv_host = strdup( ludp->lud_host ==
 		    NULL ? ld->ld_defhost : ludp->lud_host )) == NULL ) {
@@ -276,22 +276,22 @@ ldap_url_search( LDAP *ld, char *url, int attrsonly )
 				 srv->lsrv_port = ludp->lud_port;
 			}
 		}
-#else /* LDAP_REFERRALS */
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 		ld->ld_errno = LDAP_LOCAL_ERROR;
 		err = -1;
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 	}
 
 	if ( err != 0 ) {
 		ber_free( ber, 1 );
 	} else {
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 		err = ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, srv,
 		    NULL, 1 );
-#else /* LDAP_REFERRALS */
+#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 		err = ldap_send_initial_request( ld, LDAP_REQ_SEARCH,
 		    ludp->lud_dn, ber );
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 	}
 
 	ldap_free_urldesc( ludp );