From 7223fecf9f14c308992c09e4890388f09b59829d Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Thu, 7 Mar 2019 09:27:24 +1300
Subject: [PATCH 2/2] Use the same libldap variant in the frontend and backend.

Previously, we always used libldap in the backend, but either
libldap or libldab_r in the frontend depending on the build option
--enable-thread-safety.   Since that's the default, in practice the
backend and frontend code effectively use a different version of
libldap for most users.  This change avoids the risk of crashing in
the library exit code when using LDAP and postgres_fdw (or other
code that loads libpq into the backend).

Make the backend use whichever library the frontend is using.

Remove the redundant configure test that warned about the known
problem when mixing libraries.

Discussion: https://postgr.es/m/CAMVYW_6Ee-cYdpZ%2B%2BVLnab_-3hmcorOA_kmBtsnw2R%2Bn0nVxMw%40mail.gmail.com
---
 config/programs.m4            | 28 ------------------
 configure                     | 53 +++--------------------------------
 configure.in                  | 12 +++-----
 src/Makefile.global.in        |  5 ++--
 src/Makefile.shlib            |  2 +-
 src/backend/Makefile          |  2 +-
 src/interfaces/libpq/Makefile |  4 +--
 7 files changed, 14 insertions(+), 92 deletions(-)

diff --git a/config/programs.m4 b/config/programs.m4
index 5a1859fd13..a2341ba524 100644
--- a/config/programs.m4
+++ b/config/programs.m4
@@ -131,34 +131,6 @@ AC_SUBST(FLEXFLAGS)
 
 
 
-# PGAC_LDAP_SAFE
-# --------------
-# PostgreSQL sometimes loads libldap_r and plain libldap into the same
-# process.  Check for OpenLDAP versions known not to tolerate doing so; assume
-# non-OpenLDAP implementations are safe.  The dblink test suite exercises the
-# hazardous interaction directly.
-
-AC_DEFUN([PGAC_LDAP_SAFE],
-[AC_CACHE_CHECK([for compatible LDAP implementation], [pgac_cv_ldap_safe],
-[AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
-[#include <ldap.h>
-#if !defined(LDAP_VENDOR_VERSION) || \
-     (defined(LDAP_API_FEATURE_X_OPENLDAP) && \
-      LDAP_VENDOR_VERSION >= 20424 && LDAP_VENDOR_VERSION <= 20431)
-choke me
-#endif], [])],
-[pgac_cv_ldap_safe=yes],
-[pgac_cv_ldap_safe=no])])
-
-if test "$pgac_cv_ldap_safe" != yes; then
-  AC_MSG_WARN([
-*** With OpenLDAP versions 2.4.24 through 2.4.31, inclusive, each backend
-*** process that loads libpq (via WAL receiver, dblink, or postgres_fdw) and
-*** also uses LDAP will crash on exit.])
-fi])
-
-
-
 # PGAC_CHECK_READLINE
 # -------------------
 # Check for the readline library and dependent libraries, either
diff --git a/configure b/configure
index d2a0033328..424932129a 100755
--- a/configure
+++ b/configure
@@ -652,8 +652,7 @@ CFLAGS_SSE42
 have_win32_dbghelp
 LIBOBJS
 UUID_LIBS
-LDAP_LIBS_BE
-LDAP_LIBS_FE
+LDAP_LIBS
 PTHREAD_CFLAGS
 PTHREAD_LIBS
 PTHREAD_CC
@@ -12375,7 +12374,6 @@ else
   as_fn_error $? "library 'ldap' is required for LDAP" "$LINENO" 5
 fi
 
-    LDAP_LIBS_BE="-lldap $EXTRA_LDAP_LIBS"
     if test "$enable_thread_safety" = yes; then
       # on some platforms ldap_r fails to link without PTHREAD_LIBS
       { $as_echo "$as_me:${as_lineno-$LINENO}: checking for ldap_simple_bind in -lldap_r" >&5
@@ -12425,9 +12423,9 @@ else
   as_fn_error $? "library 'ldap_r' is required for LDAP" "$LINENO" 5
 fi
 
-      LDAP_LIBS_FE="-lldap_r $EXTRA_LDAP_LIBS"
+      LDAP_LIBS="-lldap_r $EXTRA_LDAP_LIBS"
     else
-      LDAP_LIBS_FE="-lldap $EXTRA_LDAP_LIBS"
+      LDAP_LIBS="-lldap $EXTRA_LDAP_LIBS"
     fi
     for ac_func in ldap_initialize
 do :
@@ -12488,14 +12486,12 @@ else
   as_fn_error $? "library 'wldap32' is required for LDAP" "$LINENO" 5
 fi
 
-    LDAP_LIBS_FE="-lwldap32"
-    LDAP_LIBS_BE="-lwldap32"
+    LDAP_LIBS="-lwldap32"
   fi
   LIBS="$_LIBS"
 fi
 
 
-
 # for contrib/sepgsql
 if test "$with_selinux" = yes; then
   { $as_echo "$as_me:${as_lineno-$LINENO}: checking for security_compute_create_name in -lselinux" >&5
@@ -13168,47 +13164,6 @@ fi
 
 done
 
-     { $as_echo "$as_me:${as_lineno-$LINENO}: checking for compatible LDAP implementation" >&5
-$as_echo_n "checking for compatible LDAP implementation... " >&6; }
-if ${pgac_cv_ldap_safe+:} false; then :
-  $as_echo_n "(cached) " >&6
-else
-  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h.  */
-#include <ldap.h>
-#if !defined(LDAP_VENDOR_VERSION) || \
-     (defined(LDAP_API_FEATURE_X_OPENLDAP) && \
-      LDAP_VENDOR_VERSION >= 20424 && LDAP_VENDOR_VERSION <= 20431)
-choke me
-#endif
-int
-main ()
-{
-
-  ;
-  return 0;
-}
-_ACEOF
-if ac_fn_c_try_compile "$LINENO"; then :
-  pgac_cv_ldap_safe=yes
-else
-  pgac_cv_ldap_safe=no
-fi
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_ldap_safe" >&5
-$as_echo "$pgac_cv_ldap_safe" >&6; }
-
-if test "$pgac_cv_ldap_safe" != yes; then
-  { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING:
-*** With OpenLDAP versions 2.4.24 through 2.4.31, inclusive, each backend
-*** process that loads libpq (via WAL receiver, dblink, or postgres_fdw) and
-*** also uses LDAP will crash on exit." >&5
-$as_echo "$as_me: WARNING:
-*** With OpenLDAP versions 2.4.24 through 2.4.31, inclusive, each backend
-*** process that loads libpq (via WAL receiver, dblink, or postgres_fdw) and
-*** also uses LDAP will crash on exit." >&2;}
-fi
   else
      for ac_header in winldap.h
 do :
diff --git a/configure.in b/configure.in
index 8ed0c81cc3..867017b291 100644
--- a/configure.in
+++ b/configure.in
@@ -1238,26 +1238,23 @@ if test "$with_ldap" = yes ; then
     AC_CHECK_LIB(ldap, ldap_bind, [],
 		 [AC_MSG_ERROR([library 'ldap' is required for LDAP])],
 		 [$EXTRA_LDAP_LIBS])
-    LDAP_LIBS_BE="-lldap $EXTRA_LDAP_LIBS"
     if test "$enable_thread_safety" = yes; then
       # on some platforms ldap_r fails to link without PTHREAD_LIBS
       AC_CHECK_LIB(ldap_r, ldap_simple_bind, [],
 		   [AC_MSG_ERROR([library 'ldap_r' is required for LDAP])],
 		   [$PTHREAD_CFLAGS $PTHREAD_LIBS $EXTRA_LDAP_LIBS])
-      LDAP_LIBS_FE="-lldap_r $EXTRA_LDAP_LIBS"
+      LDAP_LIBS="-lldap_r $EXTRA_LDAP_LIBS"
     else
-      LDAP_LIBS_FE="-lldap $EXTRA_LDAP_LIBS"
+      LDAP_LIBS="-lldap $EXTRA_LDAP_LIBS"
     fi
     AC_CHECK_FUNCS([ldap_initialize])
   else
     AC_CHECK_LIB(wldap32, ldap_bind, [], [AC_MSG_ERROR([library 'wldap32' is required for LDAP])])
-    LDAP_LIBS_FE="-lwldap32"
-    LDAP_LIBS_BE="-lwldap32"
+    LDAP_LIBS="-lwldap32"
   fi
   LIBS="$_LIBS"
 fi
-AC_SUBST(LDAP_LIBS_FE)
-AC_SUBST(LDAP_LIBS_BE)
+AC_SUBST(LDAP_LIBS)
 
 # for contrib/sepgsql
 if test "$with_selinux" = yes; then
@@ -1423,7 +1420,6 @@ if test "$with_ldap" = yes ; then
   if test "$PORTNAME" != "win32"; then
      AC_CHECK_HEADERS(ldap.h, [],
                       [AC_MSG_ERROR([header file <ldap.h> is required for LDAP])])
-     PGAC_LDAP_SAFE
   else
      AC_CHECK_HEADERS(winldap.h, [],
                       [AC_MSG_ERROR([header file <winldap.h> is required for LDAP])],
diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index c118f64040..6fe5afa890 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -284,8 +284,7 @@ AR = @AR@
 DLLTOOL = @DLLTOOL@
 DLLWRAP = @DLLWRAP@
 LIBS = @LIBS@
-LDAP_LIBS_FE = @LDAP_LIBS_FE@
-LDAP_LIBS_BE = @LDAP_LIBS_BE@
+LDAP_LIBS = @LDAP_LIBS@
 UUID_LIBS = @UUID_LIBS@
 UUID_EXTRA_OBJS = @UUID_EXTRA_OBJS@
 LLVM_LIBS=@LLVM_LIBS@
@@ -570,7 +569,7 @@ endif
 
 # Cygwin seems to need ldap libraries to be mentioned here, too
 ifeq ($(PORTNAME),cygwin)
-libpq_pgport += $(LDAP_LIBS_FE)
+libpq_pgport += $(LDAP_LIBS)
 endif
 
 
diff --git a/src/Makefile.shlib b/src/Makefile.shlib
index 373d73caef..8765818920 100644
--- a/src/Makefile.shlib
+++ b/src/Makefile.shlib
@@ -359,7 +359,7 @@ ifeq ($(PORTNAME), cygwin)
 # Cygwin case
 
 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
-	$(CC) $(CFLAGS)  -shared -o $@ -Wl,--out-implib=$(stlib) $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) $(LDAP_LIBS_BE)
+	$(CC) $(CFLAGS)  -shared -o $@ -Wl,--out-implib=$(stlib) $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) $(LDAP_LIBS)
 
 # see notes in src/backend/parser/Makefile  about use of this type of rule
 $(stlib): $(shlib)
diff --git a/src/backend/Makefile b/src/backend/Makefile
index 478a96db9b..41d644b588 100644
--- a/src/backend/Makefile
+++ b/src/backend/Makefile
@@ -42,7 +42,7 @@ OBJS = $(SUBDIROBJS) $(LOCALOBJS) $(top_builddir)/src/port/libpgport_srv.a \
 
 # We put libpgport and libpgcommon into OBJS, so remove it from LIBS; also add
 # libldap and ICU
-LIBS := $(filter-out -lpgport -lpgcommon, $(LIBS)) $(LDAP_LIBS_BE) $(ICU_LIBS)
+LIBS := $(filter-out -lpgport -lpgcommon, $(LIBS)) $(LDAP_LIBS) $(ICU_LIBS)
 
 # The backend doesn't need everything that's in LIBS, however
 LIBS := $(filter-out -lz -lreadline -ledit -ltermcap -lncurses -lcurses, $(LIBS))
diff --git a/src/interfaces/libpq/Makefile b/src/interfaces/libpq/Makefile
index 025542dfe9..ef997d7f34 100644
--- a/src/interfaces/libpq/Makefile
+++ b/src/interfaces/libpq/Makefile
@@ -61,9 +61,9 @@ endif
 # that are built correctly for use in a shlib.
 SHLIB_LINK_INTERNAL = -lpgcommon_shlib -lpgport_shlib
 ifneq ($(PORTNAME), win32)
-SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lgssapi_krb5 -lgss -lgssapi -lssl -lsocket -lnsl -lresolv -lintl -lm, $(LIBS)) $(LDAP_LIBS_FE) $(PTHREAD_LIBS)
+SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lgssapi_krb5 -lgss -lgssapi -lssl -lsocket -lnsl -lresolv -lintl -lm, $(LIBS)) $(LDAP_LIBS) $(PTHREAD_LIBS)
 else
-SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lgssapi32 -lssl -lsocket -lnsl -lresolv -lintl -lm $(PTHREAD_LIBS), $(LIBS)) $(LDAP_LIBS_FE)
+SHLIB_LINK += $(filter -lcrypt -ldes -lcom_err -lcrypto -lk5crypto -lkrb5 -lgssapi32 -lssl -lsocket -lnsl -lresolv -lintl -lm $(PTHREAD_LIBS), $(LIBS)) $(LDAP_LIBS)
 endif
 ifeq ($(PORTNAME), win32)
 SHLIB_LINK += -lshell32 -lws2_32 -lsecur32 $(filter -leay32 -lssleay32 -lcomerr32 -lkrb5_32, $(LIBS))
-- 
2.21.0

