From e26de67770db049a6ec053b2b63ae12c8a65a473 Mon Sep 17 00:00:00 2001
From: Bart Hartgers <bart@openldap.org>
Date: Thu, 25 Feb 1999 13:07:27 +0000
Subject: [PATCH] Patch to support to determine the number of args of the
 gethostby{name,addr}_r functions. Contributed by Juan Carlos Gomes IT#68
 (apparently).

Also includes support for detecting ssleay in configure.in.
---
 acconfig.h                   |  6 +++
 build/openldap.m4            | 71 ++++++++++++++++++++++++++++++++++++
 configure.in                 | 48 +++++++++++++++++++++++-
 libraries/libldap/util-int.c | 36 ++++++++++++++++--
 4 files changed, 157 insertions(+), 4 deletions(-)

diff --git a/acconfig.h b/acconfig.h
index 3904300286..2b9417817e 100644
--- a/acconfig.h
+++ b/acconfig.h
@@ -45,6 +45,12 @@
 /* define this to the number of arguments ctime_r() expects */
 #undef CTIME_R_NARGS
 
+/* define this to the number of arguments gethostbyaddr_r expects */
+#undef GETHOSTBYADDR_R_NARGS
+
+/* define this to the number of arguments gethostbyname_r expects */
+#undef GETHOSTBYNAME_R_NARGS
+
 /* define this if sys_errlist is not defined in stdio.h or errno.h */
 #undef DECL_SYS_ERRLIST
 
diff --git a/build/openldap.m4 b/build/openldap.m4
index 85e77c7266..21171f9abc 100644
--- a/build/openldap.m4
+++ b/build/openldap.m4
@@ -443,4 +443,75 @@ AC_DEFUN(OL_FUNC_CTIME_R_NARGS,
     AC_DEFINE_UNQUOTED(CTIME_R_NARGS, $ol_cv_func_ctime_r_nargs)
   fi
 ])dnl
+dnl ====================================================================
+dnl check no of arguments for gethostbyname_r
+AC_DEFUN(OL_FUNC_GETHOSTBYNAME_R_NARGS,
+ [AC_CACHE_CHECK(number of arguments of gethostbyname_r, ol_cv_func_gethostbyname_r_nargs,
+   [AC_TRY_COMPILE([#include <sys/types.h>
+		    #include <sys/socket.h>
+		    #include <netinet/in.h>
+		    #include <netdb.h>
+		    #define BUFSIZE (sizeof(struct hostent)+10)],
+		   [struct hostent hent; char buffer[BUFSIZE];
+		    int bufsize=BUFSIZE;int h_errno;
+                    (void)gethostbyname_r( "segovia.cs.purdue.edu", &hent, buffer, bufsize, &h_errno);
+		    return 0;],
+		ol_cv_func_gethostbyname_r_nargs=5, ol_cv_func_gethostbyname_r_nargs=0)
+		if test $ol_cv_func_gethostbyname_r_nargs = 0 ; then
+ 			AC_TRY_COMPILE([#include <sys/types.h>
+		    #include <sys/socket.h>
+		    #include <netinet/in.h>
+		    #include <netdb.h>
+		    #define BUFSIZE (sizeof(struct hostent)+10)],
+		   [struct hostent hent;struct hostent *rhent;
+		    char buffer[BUFSIZE];
+		    int bufsize=BUFSIZE;int h_errno;
+                     (void)gethostbyname_r( "segovia.cs.purdue.edu", &hent, buffer, bufsize, &rhent, &h_errno);
+		    return 0;],
+		   ol_cv_func_gethostbyname_r_nargs=6, ol_cv_func_gethostbyname_r_nargs=0)
+		fi
+	])
+  if test $ol_cv_func_gethostbyname_r_nargs -gt 1 ; then
+    AC_DEFINE_UNQUOTED(GETHOSTBYNAME_R_NARGS, $ol_cv_func_gethostbyname_r_nargs)
+  fi
+])dnl
+dnl check no of arguments for gethostbyaddr_r
+AC_DEFUN(OL_FUNC_GETHOSTBYADDR_R_NARGS,
+ [AC_CACHE_CHECK(number of arguments of gethostbyaddr_r, ol_cv_func_gethostbyaddr_r_nargs,
+   [AC_TRY_COMPILE([#include <sys/types.h>
+		    #include <sys/socket.h>
+		    #include <netinet/in.h>
+		    #include <netdb.h>
+		    #define BUFSIZE (sizeof(struct hostent)+10)],
+		   [struct hostent hent; char buffer[BUFSIZE]; 
+		    struct in_addr add={0x70707070};
+		    size_t alen=sizeof(struct in_addr);
+		    int bufsize=BUFSIZE;int h_errno;
+                    (void)gethostbyaddr_r( (void *)&(add.s_addr),
+				alen, AF_INET, &hent, buffer, bufsize, &h_errno);
+		    return 0;],
+		    ol_cv_func_gethostbyaddr_r_nargs=7,
+		    ol_cv_func_gethostbyaddr_r_nargs=0)
+		if test $ol_cv_func_gethostbyaddr_r_nargs = 0 ; then
+			AC_TRY_COMPILE([#include <sys/types.h>
+		    #include <sys/socket.h>
+		    #include <netinet/in.h>
+		    #include <netdb.h>
+		    #define BUFSIZE (sizeof(struct hostent)+10)],
+		   [struct hostent hent; struct hostent *rhent; char buffer[BUFSIZE]; 
+		    struct in_addr add={0x70707070};
+		    size_t alen=sizeof(struct in_addr);
+		    int bufsize=BUFSIZE;int h_errno;
+                    (void)gethostbyaddr_r( (void *)&(add.s_addr),
+				alen, AF_INET, &hent, buffer, bufsize, 
+				&rhent, &h_errno);
+		    return 0;],
+		    ol_cv_func_gethostbyaddr_r_nargs=8,
+		    ol_cv_func_gethostbyaddr_r_nargs=0)
+		fi
+	])
+  if test $ol_cv_func_gethostbyaddr_r_nargs -gt 1 ; then
+    AC_DEFINE_UNQUOTED(GETHOSTBYADDR_R_NARGS, $ol_cv_func_gethostbyaddr_r_nargs)
+  fi
+])dnl
 
diff --git a/configure.in b/configure.in
index 44904c3f40..0e1bc7649f 100644
--- a/configure.in
+++ b/configure.in
@@ -49,7 +49,6 @@ 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
 
-
 dnl General "with" options
 OL_ARG_ENABLE(dmalloc,[  --enable-dmalloc	enable debug malloc support], no)dnl
 
@@ -59,6 +58,8 @@ OL_ARG_WITH(threads,[  --with-threads	use threads],
 	auto, [auto posix mach lwp yes no manual] )
 OL_ARG_WITH(yielding_select,[  --with-yielding-select	with implicitly yielding select],
 	auto, [auto yes no manual] )
+OL_ARG_WITH(tls,[  --with-tls  use tls/ssl],
+	auto, [auto ssleay openssl yes no] )
 
 dnl Server options
 
@@ -261,6 +262,7 @@ SLAPD_PERL_CPPFLAGS=
 
 KRB_LIBS=
 TERMCAP_LIBS=
+TLS_LIBS=
 
 dnl ----------------------------------------------------------------
 dnl Checks for programs
@@ -452,6 +454,35 @@ if test $ol_link_kerberos = yes ; then
 	AC_DEFINE(HAVE_KERBEROS, 1, [define if you have Kerberos])
 fi
 
+dnl
+dnl Check for SSL/TLS
+dnl
+ol_link_tls=no
+if test $ol_with_tls = auto -o $ol_with_tls = ssleay \
+	-o $ol_with_tls = openssl ; then
+	
+	AC_CHECK_HEADERS(ssl.h)
+	
+	if test $ac_cv_header_ssl_h = yes ; then
+		AC_CHECK_LIB( ssl, SSLeay_add_ssl_algorithms, 
+			[have_ssleay=yes], [have_ssleay=no], [-lcrypto])
+			
+		if test $have_ssleay = yes ; then
+			ol_with_tls=found
+			ol_link_tls=yes
+
+			AC_DEFINE( HAVE_SSLEAY, 1, 
+				[define if you have SSLeay] )
+
+			TLS_LIBS="-lssl -lcrypto"
+		fi
+	fi
+fi
+
+if test $ol_link_tls = yes ; then
+	AC_DEFINE( HAVE_TLS, 1, [define if you have TLS] )
+fi	
+
 ol_link_threads=no
 if test $ol_with_threads = auto -o $ol_with_threads = yes \
 	-o $ol_with_threads = posix ; then
@@ -1516,8 +1547,22 @@ else
 	ol_cv_func_ctime_r=0
 fi
 
+if test "$ac_cv_func_gethostbyname_r" = yes ; then
+ 	OL_FUNC_GETHOSTBYNAME_R_NARGS
+else
+ 	ol_cv_func_gethostbyname_r=0
+fi
+ 
+if test "$ac_cv_func_gethostbyaddr_r" = yes ; then
+ 	OL_FUNC_GETHOSTBYADDR_R_NARGS
+else
+ 	ol_cv_func_gethostbyaddr_r=0
+fi
+
 if test "$ac_cv_func_ctime_r" = yes \
 	-a "$ol_cv_func_ctime_r_nargs" -ge 2 -a "$ol_cv_func_ctime_r_nargs" -le 3 \
+ 	-a "$ol_cv_func_gethostbyname_r_nargs" -ge 5 -a "$ol_cv_func_gethostbyname_r_nargs" -le 6 \
+ 	-a "$ol_cv_func_gethostbyaddr_r_nargs" -ge 5 -a "$ol_cv_func_gethostbyaddr_r_nargs" -le 6 \
 	-a "$ac_cv_func_gethostbyaddr_r" = yes \
 	-a "$ac_cv_func_gethostbyname_r" = yes \
 	; then
@@ -1650,6 +1695,7 @@ AC_SUBST(SLAPD_PERL_LDFLAGS)
 
 AC_SUBST(KRB_LIBS)
 AC_SUBST(TERMCAP_LIBS)
+AC_SUBST(TLS_LIBS)
 
 dnl ----------------------------------------------------------------
 dnl final output
diff --git a/libraries/libldap/util-int.c b/libraries/libldap/util-int.c
index f40082c64d..2316716da6 100644
--- a/libraries/libldap/util-int.c
+++ b/libraries/libldap/util-int.c
@@ -4,8 +4,8 @@
  */
 /*
  * util-int.c	Various functions to replace missing threadsafe ones.
- *				  Without the real *_r funcs, things will work, but won't be
- *				  threadsafe. 
+ *				  Without the real *_r funcs, things will
+ *				  work, but might not be threadsafe. 
  * 
  * Written by Bart Hartgers.
  *
@@ -19,6 +19,7 @@
  * in file LICENSE in the top-level directory of the distribution.
  */ 
 
+
 #include "portable.h"
 
 #include <stdlib.h>
@@ -90,6 +91,11 @@ int ldap_pvt_gethostbyname_a(
 	int *herrno_ptr )
 {
 #if defined( HAVE_GETHOSTBYNAME_R )
+
+# if (GETHOSTBYNAME_R_NARGS > 6) || (GETHOSTBYNAME_R_NARGS < 5)
+    Ouch! gethostbyname_r() must have either 5 or 6 args
+#endif
+
 # define NEED_SAFE_REALLOC 1   
 	int r=-1;
 	int buflen=BUFSTART;
@@ -97,8 +103,19 @@ int ldap_pvt_gethostbyname_a(
 	for(;buflen<BUFMAX;) {
 		if (safe_realloc( buf, buflen )==NULL)
 			return r;
+
+#if (GETHOSTBYNAME_R_NARGS < 6)
+		r = ((*result=gethostbyname_r( name, resbuf, *buf,\
+					       buflen, herrno_ptr ))== NULL) ?\
+		    -1 : 0;
+#else
 		r = gethostbyname_r( name, resbuf, *buf,
 			buflen, result, herrno_ptr );
+#endif
+
+		Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",\
+		       name, r, 0 );
+
 #ifdef NETDB_INTERNAL
 		if ((r<0) &&
 			(*herrno_ptr==NETDB_INTERNAL) &&
@@ -157,6 +174,11 @@ int ldap_pvt_gethostbyaddr_a(
 	int *herrno_ptr )
 {
 #if defined( HAVE_GETHOSTBYADDR_R )
+
+#if (GETHOSTBYADDR_R_NARGS > 8) || (GETHOSTBYADDR_R_NARGS < 7)
+    Ouch! gethostbyaddr_r() must have either 7 or 8 args
+#endif
+
 # undef NEED_SAFE_REALLOC
 # define NEED_SAFE_REALLOC   
 	int r=-1;
@@ -165,9 +187,17 @@ int ldap_pvt_gethostbyaddr_a(
 	for(;buflen<BUFMAX;) {
 		if (safe_realloc( buf, buflen )==NULL)
 			return r;
+#if (GETHOSTBYADDR_R_NARGS < 8)
+		r = ((*result=gethostbyaddr_r( addr, len, type,
+					       resbuf, *buf, buflen, 
+					       herrno_ptr )) == NULL) ?\
+		    -1 : 0;
+#else
 		r = gethostbyaddr_r( addr, len, type,
 			resbuf, *buf, buflen, 
 			result, herrno_ptr );
+#endif
+
 #ifdef NETDB_INTERNAL
 		if ((r<0) &&
 			(*herrno_ptr==NETDB_INTERNAL) &&
@@ -289,7 +319,7 @@ static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
 	
 	for( n_alias=total_alias_len=0, p=src->h_aliases; (*p) ; p++ ) {
 		total_alias_len += strlen( *p ) + 1;
-		n_alias++;
+		n_alias++; 
 	}
 
 	for( n_addr=0, p=src->h_addr_list; (*p) ; p++ ) {
-- 
GitLab