898c29c6138edf3d1d0c1847d364fb25562c4dee
[openafs.git] / src / cf / krb5.m4
1 dnl Find the compiler and linker flags for Kerberos v5.
2 dnl
3 dnl Finds the compiler and linker flags for linking with Kerberos v5
4 dnl libraries.  Provides the --with-krb5, --with-krb5-include, and
5 dnl --with-krb5-lib configure options to specify non-standard paths to the
6 dnl Kerberos libraries.  Uses krb5-config where available unless reduced
7 dnl dependencies is requested.
8 dnl
9 dnl Provides the macro RRA_LIB_KRB5 and sets the substitution variables
10 dnl KRB5_CPPFLAGS, KRB5_LDFLAGS, and KRB5_LIBS.  Also provides
11 dnl RRA_LIB_KRB5_SWITCH to set CPPFLAGS, LDFLAGS, and LIBS to include the
12 dnl Kerberos libraries, saving the current values first, and
13 dnl RRA_LIB_KRB5_RESTORE to restore those settings to before the last
14 dnl RRA_LIB_KRB5_SWITCH.
15 dnl
16 dnl If KRB5_CPPFLAGS, KRB5_LDFLAGS, or KRB5_LIBS are set before calling these
17 dnl macros, their values will be added to whatever the macros discover.
18 dnl
19 dnl Provides the RRA_LIB_KRB5_OPTIONAL macro, which should be used if Kerberos
20 dnl support is optional.  This macro will still always set the substitution
21 dnl variables, but they'll be empty unless --with-krb5 is given.  Also,
22 dnl HAVE_KERBEROS will be defined if --with-krb5 is given and
23 dnl $rra_use_kerberos will be set to "true".
24 dnl
25 dnl Sets the Automake conditional KRB5_USES_COM_ERR saying whether we use
26 dnl com_err, since if we're also linking with AFS libraries, we may have to
27 dnl change library ordering in that case.
28 dnl
29 dnl Depends on RRA_ENABLE_REDUCED_DEPENDS and RRA_SET_LDFLAGS.
30 dnl
31 dnl Also provides RRA_FUNC_KRB5_GET_INIT_CREDS_OPT_FREE_ARGS, which checks
32 dnl whether krb5_get_init_creds_opt_free takes one argument or two.  Defines
33 dnl HAVE_KRB5_GET_INIT_CREDS_OPT_FREE_2_ARGS if it takes two arguments.
34 dnl
35 dnl Written by Russ Allbery <rra@stanford.edu>
36 dnl Copyright 2005, 2006, 2007, 2008, 2009, 2010
37 dnl     Board of Trustees, Leland Stanford Jr. University
38 dnl
39 dnl See LICENSE for licensing terms.
40
41 dnl Save the current CPPFLAGS, LDFLAGS, and LIBS settings and switch to
42 dnl versions that include the Kerberos v5 flags.  Used as a wrapper, with
43 dnl RRA_LIB_KRB5_RESTORE, around tests.
44 AC_DEFUN([RRA_LIB_KRB5_SWITCH],
45 [rra_krb5_save_CPPFLAGS="$CPPFLAGS"
46  rra_krb5_save_LDFLAGS="$LDFLAGS"
47  rra_krb5_save_LIBS="$LIBS"
48  CPPFLAGS="$KRB5_CPPFLAGS $CPPFLAGS"
49  LDFLAGS="$KRB5_LDFLAGS $LDFLAGS"
50  LIBS="$KRB5_LIBS $LIBS"])
51
52 dnl Restore CPPFLAGS, LDFLAGS, and LIBS to their previous values (before
53 dnl RRA_LIB_KRB5_SWITCH was called).
54 AC_DEFUN([RRA_LIB_KRB5_RESTORE],
55 [CPPFLAGS="$rra_krb5_save_CPPFLAGS"
56  LDFLAGS="$rra_krb5_save_LDFLAGS"
57  LIBS="$rra_krb5_save_LIBS"])
58
59 dnl Set KRB5_CPPFLAGS and KRB5_LDFLAGS based on rra_krb5_root,
60 dnl rra_krb5_libdir, and rra_krb5_includedir.
61 AC_DEFUN([_RRA_LIB_KRB5_PATHS],
62 [AS_IF([test x"$rra_krb5_libdir" != x],
63     [KRB5_LDFLAGS="-L$rra_krb5_libdir"],
64     [AS_IF([test x"$rra_krb5_root" != x],
65         [RRA_SET_LDFLAGS([KRB5_LDFLAGS], [$rra_krb5_root])])])
66  AS_IF([test x"$rra_krb5_includedir" != x],
67     [KRB5_CPPFLAGS="-I$rra_krb5_includedir"],
68     [AS_IF([test x"$rra_krb5_root" != x],
69         [AS_IF([test x"$rra_krb5_root" != x/usr],
70             [KRB5_CPPFLAGS="-I${rra_krb5_root}/include"])])])])
71
72 dnl Does the appropriate library checks for reduced-dependency Kerberos v5
73 dnl linkage.  The single argument, if true, says to fail if Kerberos could not
74 dnl be found.
75 AC_DEFUN([_RRA_LIB_KRB5_REDUCED],
76 [RRA_LIB_KRB5_SWITCH
77  AC_CHECK_LIB([krb5], [krb5_init_context], [KRB5_LIBS="-lkrb5"],
78      [AS_IF([test x"$1" = xtrue],
79          [AC_MSG_ERROR([cannot find usable Kerberos v5 library])])])
80  LIBS="$KRB5_LIBS $LIBS"
81  AC_CHECK_FUNCS([krb5_get_error_message],
82      [AC_CHECK_FUNCS([krb5_free_error_message])],
83      [AC_CHECK_FUNCS([krb5_get_error_string], ,
84          [AC_CHECK_FUNCS([krb5_get_err_txt], ,
85              [AC_CHECK_LIB([ksvc], [krb5_svc_get_msg],
86                  [KRB5_LIBS="$KRB5_LIBS -lksvc"
87                   AC_DEFINE([HAVE_KRB5_SVC_GET_MSG], [1])
88                   AC_CHECK_HEADERS([ibm_svc/krb5_svc.h])],
89                  [AC_CHECK_LIB([com_err], [com_err],
90                      [KRB5_LIBS="$KRB5_LIBS -lcom_err"],
91                      [AC_MSG_ERROR([cannot find usable com_err library])])
92                   AC_CHECK_HEADERS([et/com_err.h])])])])])
93  RRA_LIB_KRB5_RESTORE])
94
95 dnl Does the appropriate library checks for Kerberos v5 linkage when we don't
96 dnl have krb5-config or reduced dependencies.  The single argument, if true,
97 dnl says to fail if Kerberos could not be found.
98 AC_DEFUN([_RRA_LIB_KRB5_MANUAL],
99 [RRA_LIB_KRB5_SWITCH
100  rra_krb5_extra=
101  LIBS=
102  AC_SEARCH_LIBS([res_search], [resolv], ,
103     [AC_SEARCH_LIBS([__res_search], [resolv])])
104  AC_SEARCH_LIBS([gethostbyname], [nsl])
105  AC_SEARCH_LIBS([socket], [socket], ,
106     [AC_CHECK_LIB([nsl], [socket], [LIBS="-lnsl -lsocket $LIBS"], ,
107         [-lsocket])])
108  AC_SEARCH_LIBS([crypt], [crypt])
109  AC_SEARCH_LIBS([rk_simple_execve], [roken])
110  rra_krb5_extra="$LIBS"
111  LIBS="$rra_krb5_save_LIBS"
112  AC_CHECK_LIB([krb5], [krb5_init_context],
113     [KRB5_LIBS="-lkrb5 -lasn1 -lcom_err -lcrypto $rra_krb5_extra"],
114     [AC_CHECK_LIB([krb5support], [krb5int_getspecific],
115         [rra_krb5_extra="-lkrb5support $rra_krb5_extra"],
116         [AC_CHECK_LIB([pthreads], [pthread_setspecific],
117             [rra_krb5_pthread="-lpthreads"],
118             [AC_CHECK_LIB([pthread], [pthread_setspecific],
119                 [rra_krb5_pthread="-lpthread"])])
120          AC_CHECK_LIB([krb5support], [krb5int_setspecific],
121             [rra_krb5_extra="-lkrb5support $rra_krb5_extra $rra_krb5_pthread"],
122             , [$rra_krb5_pthread])])
123      AC_CHECK_LIB([com_err], [error_message],
124         [rra_krb5_extra="-lcom_err $rra_krb5_extra"])
125      AC_CHECK_LIB([ksvc], [krb5_svc_get_msg],
126         [rra_krb5_extra="-lksvc $rra_krb5_extra"])
127      AC_CHECK_LIB([k5crypto], [krb5int_hash_md5],
128         [rra_krb5_extra="-lk5crypto $rra_krb5_extra"])
129      AC_CHECK_LIB([k5profile], [profile_get_values],
130         [rra_krb5_extra="-lk5profile $rra_krb5_extra"])
131      AC_CHECK_LIB([krb5], [krb5_cc_default],
132         [KRB5_LIBS="-lkrb5 $rra_krb5_extra"],
133         [AS_IF([test x"$1" = xtrue],
134             [AC_MSG_ERROR([cannot find usable Kerberos v5 library])])],
135         [$rra_krb5_extra])],
136     [-lasn1 -lcom_err -lcrypto $rra_krb5_extra])
137  LIBS="$KRB5_LIBS $LIBS"
138  AC_CHECK_FUNCS([krb5_get_error_message],
139      [AC_CHECK_FUNCS([krb5_free_error_message])],
140      [AC_CHECK_FUNCS([krb5_get_error_string], ,
141          [AC_CHECK_FUNCS([krb5_get_err_txt], ,
142              [AC_CHECK_FUNCS([error_message], ,
143              [AC_CHECK_FUNCS([krb5_svc_get_msg],
144                  [AC_CHECK_HEADERS([ibm_svc/krb5_svc.h])],
145                  [AC_CHECK_HEADERS([et/com_err.h])])])])])])
146  RRA_LIB_KRB5_RESTORE])
147
148 dnl Sanity-check the results of krb5-config and be sure we can really link a
149 dnl Kerberos program.  If that fails, clear KRB5_CPPFLAGS and KRB5_LIBS so
150 dnl that we know we don't have usable flags and fall back on the manual
151 dnl check.
152 AC_DEFUN([_RRA_LIB_KRB5_CHECK],
153 [RRA_LIB_KRB5_SWITCH
154  AC_CHECK_FUNC([krb5_init_context],
155     [RRA_LIB_KRB5_RESTORE],
156     [RRA_LIB_KRB5_RESTORE
157      KRB5_CPPFLAGS=
158      KRB5_LIBS=
159      _RRA_LIB_KRB5_PATHS
160      _RRA_LIB_KRB5_MANUAL([$1])])])
161
162 dnl The core of the library checking, shared between RRA_LIB_KRB5 and
163 dnl RRA_LIB_KRB5_OPTIONAL.  The single argument, if "true", says to fail if
164 dnl Kerberos could not be found.
165 AC_DEFUN([_RRA_LIB_KRB5_INTERNAL],
166 [AC_REQUIRE([RRA_ENABLE_REDUCED_DEPENDS])
167  AS_IF([test x"$rra_reduced_depends" = xtrue],
168     [_RRA_LIB_KRB5_PATHS
169      _RRA_LIB_KRB5_REDUCED([$1])],
170     [AC_ARG_VAR([KRB5_CONFIG], [Path to krb5-config])
171      AS_IF([test x"$rra_krb5_root" != x && test -z "$KRB5_CONFIG"],
172          [AS_IF([test -x "${rra_krb5_root}/bin/krb5-config"],
173              [KRB5_CONFIG="${rra_krb5_root}/bin/krb5-config"])],
174          [AC_PATH_PROG([KRB5_CONFIG], [krb5-config], ,
175              [${PATH}:/usr/kerberos/bin])])
176      AS_IF([test x"$KRB5_CONFIG" != x && test -x "$KRB5_CONFIG"],
177          [AC_CACHE_CHECK([for krb5 support in krb5-config],
178              [rra_cv_lib_krb5_config],
179              [AS_IF(["$KRB5_CONFIG" 2>&1 | grep krb5 >/dev/null 2>&1],
180                  [rra_cv_lib_krb5_config=yes],
181                  [rra_cv_lib_krb5_config=no])])
182           AS_IF([test x"$rra_cv_lib_krb5_config" = xyes],
183               [KRB5_CPPFLAGS=`"$KRB5_CONFIG" --cflags krb5 2>/dev/null`
184                KRB5_LIBS=`"$KRB5_CONFIG" --libs krb5 2>/dev/null`],
185               [KRB5_CPPFLAGS=`"$KRB5_CONFIG" --cflags 2>/dev/null`
186                KRB5_LIBS=`"$KRB5_CONFIG" --libs 2>/dev/null`])
187           KRB5_CPPFLAGS=`echo "$KRB5_CPPFLAGS" | sed 's%-I/usr/include ?%%'`
188           _RRA_LIB_KRB5_CHECK([$1])
189           RRA_LIB_KRB5_SWITCH
190           AC_CHECK_FUNCS([krb5_get_error_message],
191               [AC_CHECK_FUNCS([krb5_free_error_message])],
192               [AC_CHECK_FUNCS([krb5_get_error_string], ,
193                   [AC_CHECK_FUNCS([krb5_get_err_txt], ,
194                       [AC_CHECK_FUNCS([krb5_svc_get_msg],
195                           [AC_CHECK_HEADERS([ibm_svc/krb5_svc.h])],
196                           [AC_CHECK_HEADERS([et/com_err.h])])])])])
197           RRA_LIB_KRB5_RESTORE],
198          [_RRA_LIB_KRB5_PATHS
199           _RRA_LIB_KRB5_MANUAL([$1])])])
200  rra_krb5_uses_com_err=false
201  case "$LIBS" in
202  *-lcom_err*)
203      rra_krb5_uses_com_err=true
204      ;;
205  esac
206  AM_CONDITIONAL([KRB5_USES_COM_ERR], [test x"$rra_krb5_uses_com_err" = xtrue])])
207
208 dnl The main macro for packages with mandatory Kerberos support.
209 AC_DEFUN([RRA_LIB_KRB5],
210 [rra_krb5_root=
211  rra_krb5_libdir=
212  rra_krb5_includedir=
213  AC_SUBST([KRB5_CPPFLAGS])
214  AC_SUBST([KRB5_LDFLAGS])
215  AC_SUBST([KRB5_LIBS])
216
217  AC_ARG_WITH([krb5],
218     [AS_HELP_STRING([--with-krb5=DIR],
219         [Location of Kerberos v5 headers and libraries])],
220     [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
221         [rra_krb5_root="$withval"])])
222  AC_ARG_WITH([krb5-include],
223     [AS_HELP_STRING([--with-krb5-include=DIR],
224         [Location of Kerberos v5 headers])],
225     [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
226         [rra_krb5_includedir="$withval"])])
227  AC_ARG_WITH([krb5-lib],
228     [AS_HELP_STRING([--with-krb5-lib=DIR],
229         [Location of Kerberos v5 libraries])],
230     [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
231         [rra_krb5_libdir="$withval"])])
232  _RRA_LIB_KRB5_INTERNAL([true])])
233
234 dnl The main macro for packages with optional Kerberos support.
235 AC_DEFUN([RRA_LIB_KRB5_OPTIONAL],
236 [rra_krb5_root=
237  rra_krb5_libdir=
238  rra_krb5_includedir=
239  rra_use_kerberos=
240  AC_SUBST([KRB5_CPPFLAGS])
241  AC_SUBST([KRB5_LDFLAGS])
242  AC_SUBST([KRB5_LIBS])
243
244  AC_ARG_WITH([krb5],
245     [AS_HELP_STRING([--with-krb5@<:@=DIR@:>@],
246         [Location of Kerberos v5 headers and libraries])],
247     [AS_IF([test x"$withval" = xno],
248         [rra_use_kerberos=false],
249         [AS_IF([test x"$withval" != xyes], [rra_krb5_root="$withval"])
250          rra_use_kerberos=true])])
251  AC_ARG_WITH([krb5-include],
252     [AS_HELP_STRING([--with-krb5-include=DIR],
253         [Location of Kerberos v5 headers])],
254     [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
255         [rra_krb5_includedir="$withval"])])
256  AC_ARG_WITH([krb5-lib],
257     [AS_HELP_STRING([--with-krb5-lib=DIR],
258         [Location of Kerberos v5 libraries])],
259     [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
260         [rra_krb5_libdir="$withval"])])
261
262  AS_IF([test x"$rra_use_kerberos" != xfalse],
263      [AS_IF([test x"$rra_use_kerberos" = xtrue],
264          [_RRA_LIB_KRB5_INTERNAL([true])],
265          [_RRA_LIB_KRB5_INTERNAL([false])])])
266  AS_IF([test x"$KRB5_LIBS" != x],
267     [AC_DEFINE([HAVE_KERBEROS], 1, [Define to enable Kerberos features.])])])
268
269 dnl Check whether krb5_get_init_creds_opt_free takes one argument or two.
270 dnl Early Heimdal used to take a single argument.  Defines
271 dnl HAVE_KRB5_GET_INIT_CREDS_OPT_FREE_2_ARGS if it takes two arguments.
272 dnl
273 dnl Should be called with RRA_LIB_KRB5_SWITCH active.
274 AC_DEFUN([RRA_FUNC_KRB5_GET_INIT_CREDS_OPT_FREE_ARGS],
275 [AC_CACHE_CHECK([if krb5_get_init_creds_opt_free takes two arguments],
276     [rra_cv_func_krb5_get_init_creds_opt_free_args],
277     [AC_TRY_COMPILE([#include <krb5.h>],
278         [krb5_get_init_creds_opt *opts; krb5_context c;
279          krb5_get_init_creds_opt_free(c, opts);],
280         [rra_cv_func_krb5_get_init_creds_opt_free_args=yes],
281         [rra_cv_func_krb5_get_init_creds_opt_free_args=no])])
282  AS_IF([test $rra_cv_func_krb5_get_init_creds_opt_free_args = yes],
283     [AC_DEFINE([HAVE_KRB5_GET_INIT_CREDS_OPT_FREE_2_ARGS], 1,
284         [Define if krb5_get_init_creds_opt_free takes two arguments.])])])