ax_nagios_get_ssl.m4 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. # ===========================================================================
  2. # SYNOPSIS
  3. #
  4. # AX_NAGIOS_GET_SSL
  5. #
  6. # DESCRIPTION
  7. #
  8. # This macro finds the openssl binary, the header files directory and
  9. # the library files directory. It will also search for the gnutls
  10. # compatibility library/headers and the nss compatibility library/headers.
  11. #
  12. # LICENSE
  13. #
  14. # Copyright (c) 2016 Nagios Core Development Team
  15. #
  16. # This program is free software; you can redistribute it and/or modify it
  17. # under the terms of the GNU General Public License as published by the
  18. # Free Software Foundation; either version 2 of the License, or (at your
  19. # option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful, but
  22. # WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  24. # Public License for more details.
  25. #
  26. # You should have received a copy of the GNU General Public License along
  27. # with this program. If not, see <http://www.gnu.org/licenses/>.
  28. #
  29. # As a special exception, the respective Autoconf Macro's copyright owner
  30. # gives unlimited permission to copy, distribute and modify the configure
  31. # scripts that are the output of Autoconf when processing the Macro. You
  32. # need not follow the terms of the GNU General Public License when using
  33. # or distributing such scripts, even though portions of the text of the
  34. # Macro appear in them. The GNU General Public License (GPL) does govern
  35. # all other use of the material that constitutes the Autoconf Macro.
  36. #
  37. # This special exception to the GPL applies to versions of the Autoconf
  38. # Macro released by the Autoconf Archive. When you make and distribute a
  39. # modified version of the Autoconf Macro, you may extend this special
  40. # exception to the GPL to apply to your modified version as well.
  41. # ===========================================================================
  42. AC_DEFUN([AX_NAGIOS_GET_SSL],
  43. [
  44. # -------------------------------
  45. # SSL library and include paths
  46. # -------------------------------
  47. SSL_TYPE=openssl
  48. try_pkg_config=1
  49. ssl_dir=
  50. ssl_inc_dir=
  51. ssl_lib_dir=
  52. SSL_INC_PREFIX=
  53. SSL_HDR=
  54. SSL_LIB_DIR=
  55. AC_SUBST(HAVE_SSL)
  56. AC_SUBST(SSL_TYPE)
  57. AC_SUBST(SSL_HDR)
  58. AC_SUBST(SSL_INC_PREFIX)
  59. AC_SUBST(SSL_LIB_DIR)
  60. AC_SUBST(SSL_DH_HEADER)
  61. AC_SUBST(SSL_OBJS)
  62. dnl Makefile for generating DH parameters, pre 3.0 and post 3.0
  63. SSL_DH_HEADER_MAKE_OLD="../include/dh.h:
  64. \$(SSLBIN) dhparam -C 2048 | awk '/^-----/ {exit} {print}' > \@S|@@"
  65. SSL_DH_HEADER_MAKE_NEW="../include/dh.h: generate_dh_params
  66. ./generate_dh_params > \@S|@@
  67. generate_dh_params: \$(srcdir)/generate_dh_params.c
  68. \$(CC) \$(CFLAGS) -o \@S|@@ \$(srcdir)/generate_dh_params.c \$(LDFLAGS)"
  69. AC_ARG_WITH([ssl],
  70. AS_HELP_STRING([--with-ssl=DIR],[sets location of the SSL installation]),
  71. [ssl_dir=$withval])
  72. AC_ARG_WITH([ssl-inc],
  73. AS_HELP_STRING([--with-ssl-inc=DIR],
  74. [sets location of the SSL include files]),
  75. [ssl_inc_dir=$withval])
  76. AC_ARG_WITH([ssl-lib],
  77. AS_HELP_STRING([--with-ssl-lib=DIR],[sets location of the SSL libraries]),
  78. [ssl_lib_dir=$withval])
  79. AC_ARG_ENABLE([auto_dh],
  80. AS_HELP_STRING([--disable-auto-dh],[disables using builtin DH parameters (if available) and generates custom parameters]),
  81. auto_dh=no,
  82. auto_dh=yes)
  83. if test x$ssl_inc_dir != x -o x$ssl_lib_dir != x; then
  84. try_pkg_config=0
  85. fi
  86. AC_ARG_WITH([kerberos-inc],
  87. AS_HELP_STRING([--with-kerberos-inc=DIR],
  88. [sets location of the Kerberos include files]),
  89. [kerberos_inc_dir=$withval])
  90. if test x$SSL_TYPE = xyes; then
  91. SSL_TYPE=openssl
  92. fi
  93. dflt_hdrs="$ssl_inc_dir $ssl_dir $ssl_inc_dir/include $ssl_dir/include \
  94. /usr/include /usr /usr/local /usr/pkg /usr/sfw /usr/sfw/include \
  95. /usr/local/opt/{BBB} /usr/include/{BBB} /usr/local/include/{BBB} \
  96. /usr/local/{AAA} /usr/local/{BBB} /usr/lib/{AAA} /usr/lib/{BBB} \
  97. /usr/{AAA} /usr/freeware/lib/{BBB} /opt/{BBB}"
  98. dflt_libs="$ssl_lib_dir {ssldir} {ssldir}/lib {ssldir}/lib64 /usr/lib64 \
  99. /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/i386-linux-gnu \
  100. /usr/local/lib /usr/lib/{AAA} /usr/{AAA}/lib /usr/{BBB}/lib \
  101. /usr/pkg/lib /usr/freeware/lib/{BBB} /usr/sfw/lib /opt/freeware/lib \
  102. /opt/{BBB}/lib/hpux64 /opt/{BBB}/lib/pa20_64 /opt/{BBB}/lib/hpux32 \
  103. /opt/{BBB}/lib /opt/{BBB}";
  104. AS_CASE([$SSL_TYPE],
  105. [no], [SSL_TYPE=NONE],
  106. [yes|openssl],
  107. [ssl_hdr_dirs=`echo "$dflt_hdrs" | sed -e 's/{AAA}/ssl/g' | sed -e 's/{BBB}/openssl/g'`
  108. ssl_lib_dirs=`echo "$dflt_libs" | sed -e 's/{AAA}/ssl/g' | sed -e 's/{BBB}/openssl/g'`
  109. SSL_INC_PREFIX=openssl
  110. SSL_HDR=ssl.h
  111. ssl_lib=libssl],
  112. [*], echo >&6; AC_MSG_ERROR(['--with-ssl-type=$SSL_TYPE' is invalid])
  113. )
  114. dnl Check for SSL support
  115. if test x$SSL_TYPE != xNONE; then
  116. found_ssl=no
  117. dnl RedHat 8.0 and 9.0 include openssl compiled with kerberos,
  118. dnl so we must include header file
  119. dnl Must come before openssl checks for Redhat EL 3
  120. AC_MSG_CHECKING([for Kerberos include files])
  121. found_kerberos=no
  122. for dir in $kerberos_inc_dir /usr/kerberos/include /usr/include/krb5 \
  123. /usr/include; do
  124. kerbdir="$dir"
  125. if test -f "$dir/krb5.h"; then
  126. found_kerberos=yes
  127. CFLAGS="$CFLAGS -I$kerbdir"
  128. AC_DEFINE([HAVE_KRB5_H],[1],[Have the krb5.h header file])
  129. break
  130. fi
  131. done
  132. if test x_$found_kerberos != x_yes; then
  133. AC_MSG_WARN([could not find include files])
  134. else
  135. AC_MSG_RESULT([found Kerberos include files in $kerbdir])
  136. fi
  137. dnl First, try using pkg_config
  138. if test $try_pkg_config -ne 0 ; then
  139. AC_CHECK_TOOL([PKG_CONFIG], [pkg-config])
  140. fi
  141. if test x"$PKG_CONFIG" != x -a $try_pkg_config -ne 0 ; then
  142. cflags=`$PKG_CONFIG $SSL_TYPE --cflags-only-I 2>/dev/null`
  143. if test $? -eq 0; then
  144. CFLAGS="$CFLAGS $cflags"
  145. LDFLAGS="$LDFLAGS `$PKG_CONFIG $SSL_TYPE --libs-only-L 2>/dev/null`"
  146. LIBS="$LIBS `$PKG_CONFIG $SSL_TYPE --libs-only-l 2>/dev/null`"
  147. found_ssl=yes
  148. fi
  149. fi
  150. ax_nagios_run_ssl_save_LIBS=$LIBS
  151. if test "x_$found_ssl" != "x_yes"; then
  152. LIBS="$LIBS -l`echo $ssl_lib | sed -e 's/^lib//'` -lcrypto";
  153. fi
  154. dnl Next try just compiling with default settings (unless inc/lib were specified)
  155. if test "x_$found_ssl" != "x_yes" && test "x$ssl_inc_dir" == "x" && test "x$ssl_lib_dir" == "x"; then
  156. _AX_NAGIOS_RUN_SSL([found_ssl=yes])
  157. fi
  158. if test x_$found_ssl != x_yes; then
  159. dnl Find the SSL Headers
  160. AC_MSG_CHECKING([for SSL headers])
  161. for dir in $ssl_hdr_dirs; do
  162. if test "$dir" = "/include"; then
  163. continue
  164. fi
  165. ssldir="$dir"
  166. sslincdir="$dir"
  167. if test -f "$dir/$SSL_INC_PREFIX/$SSL_HDR"; then
  168. found_ssl=yes
  169. CFLAGS="$CFLAGS -I$dir"
  170. ssldir="$dir/.."
  171. break
  172. fi
  173. if test -f "$dir/include/$SSL_INC_PREFIX/$SSL_HDR"; then
  174. found_ssl=yes
  175. CFLAGS="$CFLAGS -I$dir/include"
  176. sslincdir="$dir/include"
  177. break
  178. fi
  179. if test -f "$dir/include/$SSL_HDR"; then
  180. found_ssl=yes
  181. if test "$SSL_HDR" != compat.h ; then
  182. SSL_INC_PREFIX=""
  183. fi
  184. CFLAGS="$CFLAGS -I$dir/include"
  185. sslincdir="$dir/include"
  186. break
  187. fi
  188. if test -f "$dir/$SSL_HDR"; then
  189. found_ssl=yes
  190. SSL_INC_PREFIX=""
  191. CFLAGS="$CFLAGS -I$dir"
  192. break
  193. fi
  194. done
  195. if test x_$found_ssl != x_yes; then
  196. AC_MSG_ERROR([Cannot find ssl headers])
  197. else
  198. AX_NORMALIZE_PATH([sslincdir])
  199. AC_MSG_RESULT([found in $sslincdir])
  200. dnl Now try and find SSL libraries
  201. AX_CHECK_LINK_FLAG([-Wl,-rpath,/], [RPATH=yes], [RPATH=no])
  202. AC_MSG_CHECKING([for SSL libraries])
  203. found_ssl=no
  204. ssl_lib_dirs=`echo "$ssl_lib_dirs" | sed -e "s|{ssldir}|$ssldir|g"`
  205. if test "`uname -s`" = "Darwin" ; then
  206. soext="dylib"
  207. elif test "`uname -s`" = "HP-UX" ; then
  208. if test x$arch = "xia64"; then
  209. soext="so"
  210. else
  211. soext="sl"
  212. fi
  213. elif test "`uname -s`" = "AIX" ; then
  214. soext="a"
  215. else
  216. soext="so a"
  217. fi
  218. for dir in $ssl_lib_dirs; do
  219. for ext in $soext; do
  220. if test -f "$dir/$ssl_lib.$ext"; then
  221. found_ssl=yes
  222. SSL_LIB_DIR="$dir"
  223. break
  224. fi
  225. done
  226. if test x_$found_ssl == x_yes; then
  227. break
  228. fi
  229. done
  230. if test x_$found_ssl != x_yes; then
  231. AC_MSG_ERROR([Cannot find ssl libraries])
  232. else
  233. AX_NORMALIZE_PATH([SSL_LIB_DIR])
  234. AC_MSG_RESULT([found in $SSL_LIB_DIR])
  235. LDFLAGS="$LDFLAGS -L$SSL_LIB_DIR";
  236. if test x$RPATH == xyes ; then
  237. # Do we need to add rpath?
  238. AC_MSG_CHECKING([checking if rpath is required...])
  239. _AX_NAGIOS_RUN_SSL(
  240. [AC_MSG_RESULT([no])],
  241. [AC_MSG_RESULT([yes])
  242. LDFLAGS="$LDFLAGS -Wl,-rpath,$SSL_LIB_DIR"],
  243. [AC_MSG_RESULT([no])]
  244. )
  245. fi
  246. fi
  247. fi
  248. fi
  249. if test x$found_ssl = xyes ; then
  250. if test -n "$SSL_INC_PREFIX" ; then
  251. SSL_INC_PREFIX="${SSL_INC_PREFIX}/"
  252. fi
  253. dnl try to compile and link to see if SSL is set up properly
  254. AC_MSG_CHECKING([whether compiling and linking against SSL works])
  255. AC_LINK_IFELSE(
  256. [AC_LANG_PROGRAM([#include <${SSL_INC_PREFIX}${SSL_HDR}>], [SSL_new(NULL)])],
  257. [
  258. AC_MSG_RESULT([yes])
  259. SSL_OBJS="nrpe-ssl.o"
  260. AC_DEFINE([HAVE_SSL], [1], [Have SSL support])
  261. $1
  262. ], [
  263. AC_MSG_ERROR([no])
  264. $2
  265. ])
  266. fi
  267. dnl Detection finished. Reset LIBS if we did not succeed
  268. if test "x_$found_ssl" != "x_yes"; then
  269. LIBS=$ax_nagios_run_ssl_save_LIBS
  270. fi
  271. if test x$found_ssl = xyes -a x$need_dh = xyes; then
  272. AC_LINK_IFELSE([dnl
  273. AC_LANG_PROGRAM(
  274. [
  275. #include <stdio.h>
  276. #include <${SSL_INC_PREFIX}${SSL_HDR}>
  277. ],
  278. [
  279. #ifdef OPENSSL_VERSION_MAJOR
  280. printf("%i %i", OPENSSL_VERSION_MAJOR, OPENSSL_VERSION_MINOR);
  281. #else
  282. printf("%i %i", (int)((OPENSSL_VERSION_NUMBER >> 28) & 0x0f), (int)((OPENSSL_VERSION_NUMBER >> 20) & 0xff));
  283. #endif
  284. ])],
  285. [
  286. nagios_ssl_version=$(./conftest$EXEEXT)
  287. SSL_MAJOR=$(echo $nagios_ssl_version | cut -d' ' -f1)
  288. SSL_MINOR=$(echo $nagios_ssl_version | cut -d' ' -f2)
  289. ],
  290. AC_MSG_ERROR([Failed to detect OpenSSL version!]))
  291. if test x$auto_dh = xyes -a $SSL_MAJOR -lt 1 -o \( $SSL_MAJOR -eq 1 -a $SSL_MINOR -lt 1 \); then
  292. dnl auto_dh not available before v1.1.0
  293. auto_dh=no
  294. fi
  295. if test x$auto_dh = xyes; then
  296. AC_DEFINE([AUTO_SSL_DH], [1], [Define to 1 to auto configure SSL DH parameters])
  297. fi
  298. dnl Do the Makefile rules here to maintain compatibility and not rely on GNU Make
  299. if test x$need_dh = xyes ; then
  300. if test x$auto_dh = xno ; then
  301. if test $SSL_MAJOR -lt 3 ; then
  302. dnl Find the openssl program
  303. dnl Only need openssl binary if we are not using auto or using version less than 3.0
  304. AC_PATH_PROG([sslbin],[openssl],[value-if-not-found],[${ssldir}/sbin${PATH_SEPARATOR}${ssldir}/bin${PATH_SEPARATOR}${PATH}])
  305. AC_SUBST([SSL_DH_HEADER_MAKE], [${SSL_DH_HEADER_MAKE_OLD}])
  306. else
  307. AC_SUBST([SSL_DH_HEADER_MAKE], [${SSL_DH_HEADER_MAKE_NEW}])
  308. fi
  309. AC_SUBST([SSL_DH_HEADER],[../include/dh.h])
  310. fi
  311. AC_DEFINE([USE_SSL_DH], [1], [Define to 1 to use SSL DH])
  312. fi
  313. fi
  314. fi
  315. ])
  316. # _AX_NAGIOS_RUN_SSL([action-if-true], [action-if-false], [action-if-cross-compiling])
  317. AC_DEFUN([_AX_NAGIOS_RUN_SSL], [
  318. tmp_prefix=""
  319. if test -n "$SSL_INC_PREFIX" ; then
  320. tmp_prefix="${SSL_INC_PREFIX}/"
  321. fi
  322. AC_RUN_IFELSE(
  323. [AC_LANG_PROGRAM([
  324. #include <${tmp_prefix}opensslv.h>
  325. #include <${tmp_prefix}crypto.h>
  326. ],[
  327. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  328. return OpenSSL_version_num() == OPENSSL_VERSION_NUMBER ? EXIT_SUCCESS : EXIT_FAILURE;
  329. #else
  330. return SSLeay() == OPENSSL_VERSION_NUMBER ? EXIT_SUCCESS : EXIT_FAILURE;
  331. #endif
  332. ])],
  333. [$1],
  334. [$2],
  335. [$3]
  336. )
  337. ])