- (dtucker) [configure.ac] Bug #1181: Explicitly test to see if OpenSSL
   (0.9.8a and presumably newer) requires -ldl to successfully link.
diff --git a/ChangeLog b/ChangeLog
index dc43a05..62adc61 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,8 @@
    afterward.  Removes the need to mangle $LIBS later to remove -lpam and -ldl.
  - (dtucker) [configure.ac] Relocate --with-pam parts in preparation for
    fixing bug #1181.  No changes yet.
+ - (dtucker) [configure.ac] Bug #1181: Explicitly test to see if OpenSSL
+   (0.9.8a and presumably newer) requires -ldl to successfully link.
 
 20060819
  - (djm) OpenBSD CVS Sync
@@ -5281,4 +5283,4 @@
    - (djm) Trim deprecated options from INSTALL. Mention UsePAM
    - (djm) Fix quote handling in sftp; Patch from admorten AT umich.edu
 
-$Id: ChangeLog,v 1.4501 2006/08/20 10:03:50 dtucker Exp $
+$Id: ChangeLog,v 1.4502 2006/08/20 10:17:53 dtucker Exp $
diff --git a/configure.ac b/configure.ac
index a3eec95..b2939ee 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,4 +1,4 @@
-# $Id: configure.ac,v 1.353 2006/08/20 10:03:50 dtucker Exp $
+# $Id: configure.ac,v 1.354 2006/08/20 10:17:53 dtucker Exp $
 #
 # Copyright (c) 1999-2004 Damien Miller
 #
@@ -15,7 +15,7 @@
 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
 AC_INIT(OpenSSH, Portable, openssh-unix-dev@mindrot.org)
-AC_REVISION($Revision: 1.353 $)
+AC_REVISION($Revision: 1.354 $)
 AC_CONFIG_SRCDIR([ssh.c])
 
 AC_CONFIG_HEADER(config.h)
@@ -1825,6 +1825,36 @@
 	]
 )
 
+AC_MSG_CHECKING([if programs using OpenSSL functions will link])
+AC_LINK_IFELSE(
+	[AC_LANG_SOURCE([[
+#include <openssl/evp.h>
+int main(void) { SSLeay_add_all_algorithms(); }
+	]])],
+	[
+		AC_MSG_RESULT(yes)
+	],
+	[
+		AC_MSG_RESULT(no)
+		saved_LIBS="$LIBS"
+		LIBS="$LIBS -ldl"
+		AC_MSG_CHECKING([if programs using OpenSSL need -ldl])
+		AC_LINK_IFELSE(
+			[AC_LANG_SOURCE([[
+#include <openssl/evp.h>
+int main(void) { SSLeay_add_all_algorithms(); }
+			]])],
+			[
+				AC_MSG_RESULT(yes)
+			],
+			[
+				AC_MSG_RESULT(no)
+				LIBS="$saved_LIBS"
+			]
+		)
+	]
+)
+
 AC_ARG_WITH(ssl-engine,
 	[  --with-ssl-engine       Enable OpenSSL (hardware) ENGINE support ],
 	[ if test "x$withval" != "xno" ; then
@@ -1926,12 +1956,19 @@
 
 			PAM_MSG="yes"
 
+			LIBPAM="-lpam"
 			AC_DEFINE(USE_PAM, 1,
 				[Define if you want to enable PAM support])
+
 			if test $ac_cv_lib_dl_dlopen = yes; then
-				LIBPAM="-lpam -ldl"
-			else
-				LIBPAM="-lpam"
+				case "$LIBS" in
+				*-ldl*)
+					# libdl already in LIBS
+					;;
+				*)
+					LIBPAM="-$LIBPAM -ldl"
+					;;
+				esac
 			fi
 			AC_SUBST(LIBPAM)
 		fi