diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
new file mode 100644
index 4c18c13..7f35faf
*** a/src/backend/libpq/auth.c
--- b/src/backend/libpq/auth.c
*************** static Port *pam_port_cludge;	/* Workaro
*** 94,101 ****
   */
  #ifdef USE_LDAP
  #ifndef WIN32
- /* We use a deprecated function to keep the codepath the same as win32. */
- #define LDAP_DEPRECATED 1
  #include <ldap.h>
  #else
  #include <winldap.h>
--- 94,99 ----
*************** CheckLDAPAuth(Port *port)
*** 2207,2218 ****
  				attributes[0],
  				port->user_name);
  
! 		r = ldap_search_s(ldap,
  						  port->hba->ldapbasedn,
  						  port->hba->ldapscope,
  						  filter,
  						  attributes,
  						  0,
  						  &search_message);
  
  		if (r != LDAP_SUCCESS)
--- 2205,2220 ----
  				attributes[0],
  				port->user_name);
  
! 		r = ldap_search_ext_s(ldap,
  						  port->hba->ldapbasedn,
  						  port->hba->ldapscope,
  						  filter,
  						  attributes,
  						  0,
+ 						  NULL,
+ 						  NULL,
+ 						  NULL,
+ 						  0,
  						  &search_message);
  
  		if (r != LDAP_SUCCESS)
diff --git a/src/include/port.h b/src/include/port.h
new file mode 100644
index 99d3a9b..85c2c77
*** a/src/include/port.h
--- b/src/include/port.h
*************** extern int	pg_get_encoding_from_locale(c
*** 456,461 ****
--- 456,475 ----
  extern char *inet_net_ntop(int af, const void *src, int bits,
  			  char *dst, size_t size);
  
+ /* port/ldap.c */
+ #if defined(USE_LDAP) && defined(HAVE_LIBLDAP)
+ #include <ldap.h>
+ 
+ extern LDAP *pg_ldap_init(const char *hostname, int portno);
+ extern int pg_ldap_simple_bind_s(LDAP *ld, const char *dn, const char *passwd);
+ 
+ /* define macros for the API functions missing in OpenLDAP */
+ #define ldap_init(hostname, portno) pg_ldap_init(hostname, portno)
+ #define ldap_simple_bind_s(ld, dn, passwd) pg_ldap_simple_bind_s(ld, dn, passwd)
+ #define ldap_unbind(ld) ldap_unbind_ext(ld, NULL, NULL)
+ #define ldap_unbind_s(ld) ldap_unbind_ext_s(ld, NULL, NULL)
+ #endif
+ 
  /* port/pgcheckdir.c */
  extern int	pg_check_dir(const char *dir);
  
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
new file mode 100644
index b83010b..faeb2d6
*** a/src/interfaces/libpq/fe-connect.c
--- b/src/interfaces/libpq/fe-connect.c
***************
*** 63,70 ****
  #ifdef WIN32
  #include <winldap.h>
  #else
- /* OpenLDAP deprecates RFC 1823, but we want standard conformance */
- #define LDAP_DEPRECATED 1
  #include <ldap.h>
  typedef struct timeval LDAP_TIMEVAL;
  #endif
--- 63,68 ----
*************** ldapServiceLookup(const char *purl, PQco
*** 3557,3563 ****
  
  	/* search */
  	res = NULL;
! 	if ((rc = ldap_search_st(ld, dn, scope, filter, attrs, 0, &time, &res))
  		!= LDAP_SUCCESS)
  	{
  		if (res != NULL)
--- 3555,3561 ----
  
  	/* search */
  	res = NULL;
! 	if ((rc = ldap_search_ext_s(ld, dn, scope, filter, attrs, 0, NULL, NULL, &time, 0, &res))
  		!= LDAP_SUCCESS)
  	{
  		if (res != NULL)
diff --git a/src/port/Makefile b/src/port/Makefile
new file mode 100644
index a3db615..328c2ab
*** a/src/port/Makefile
--- b/src/port/Makefile
*************** override CPPFLAGS := -I$(top_builddir)/s
*** 31,37 ****
  LIBS += $(PTHREAD_LIBS)
  
  OBJS = $(LIBOBJS) chklocale.o dirmod.o erand48.o exec.o fls.o inet_net_ntop.o \
! 	noblock.o path.o pgcheckdir.o pg_crc.o pgmkdirp.o pgsleep.o \
  	pgstrcasecmp.o qsort.o qsort_arg.o quotes.o sprompt.o tar.o thread.o
  
  # foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
--- 31,37 ----
  LIBS += $(PTHREAD_LIBS)
  
  OBJS = $(LIBOBJS) chklocale.o dirmod.o erand48.o exec.o fls.o inet_net_ntop.o \
! 	ldap.o noblock.o path.o pgcheckdir.o pg_crc.o pgmkdirp.o pgsleep.o \
  	pgstrcasecmp.o qsort.o qsort_arg.o quotes.o sprompt.o tar.o thread.o
  
  # foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
diff --git a/src/port/ldap.c b/src/port/ldap.c
new file mode 100644
index ...7803bce
*** a/src/port/ldap.c
--- b/src/port/ldap.c
***************
*** 0 ****
--- 1,92 ----
+ /*-------------------------------------------------------------------------
+  *
+  * ldap.c
+  *		implement old LDAP API functions on top of OpenLDAP compliant with
+  *		http://tools.ietf.org/html/draft-ietf-ldapext-ldap-c-api
+  *
+  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
+  * Portions Copyright (c) 1994, Regents of the University of California
+  *
+  * IDENTIFICATION
+  *		src/port/ldap.c
+  *
+  *-------------------------------------------------------------------------
+  */
+ 
+ #include "c.h"
+ 
+ #ifndef FRONTEND
+ #include "utils/palloc.h"
+ #endif
+ 
+ #if defined(USE_LDAP) && defined(HAVE_LIBLDAP)
+ /* OpenLDAP has deprecated ldap_init */
+ LDAP *pg_ldap_init(const char *hostname, int portno)
+ {
+ 	LDAP   *ld = NULL;
+ 	int		rc;
+ 	char   *url;
+ 
+ #ifdef FRONTEND
+ 	url = malloc(strlen(hostname) + 15);
+ 	if (!url)
+ 		return NULL;
+ #else
+ 	url = palloc(strlen(hostname) + 15);
+ #endif
+ 
+ 	sprintf(url, "ldap://%s:%d/", hostname, portno);
+ 	rc = ldap_initialize(&ld, url);
+ 
+ #ifdef FRONTEND
+ 	free(url);
+ #else
+ 	pfree(url);
+ #endif
+ 
+ 	if (rc == LDAP_SUCCESS)
+ 		return ld;
+ 	else
+ 		return NULL;
+ }
+ 
+ /* OpenLDAP has deprecated ldap_simple_bind_s */
+ int pg_ldap_simple_bind_s(LDAP *ld, const char *dn, const char *passwd)
+ {
+ 	struct berval cred;
+ 	int		rc;
+ 	char   *pwd = NULL;
+ 
+ 	if (passwd)
+ 	{
+ #ifdef FRONTEND
+ 		pwd = strdup(passwd);
+ 		if (!pwd)
+ 			return LDAP_NO_MEMORY;
+ #else
+ 		pwd = pstrdup(passwd);
+ #endif
+ 	}
+ 
+ 	cred.bv_val = pwd;
+ 	cred.bv_len = pwd ? strlen(pwd) : 0;
+ 	rc = ldap_sasl_bind_s(ld,
+ 						  dn,
+ 						  LDAP_SASL_SIMPLE,
+ 						  &cred,
+ 						  NULL,
+ 						  NULL,
+ 						  NULL);
+ 
+ 	if (passwd)
+ 	{
+ #ifdef FRONTEND
+ 		free(pwd);
+ #else
+ 		pfree(pwd);
+ #endif
+ 	}
+ 
+ 	return rc;
+ }
+ #endif
