a14dd2f4aa69164e64049e110d23ebd5b2009972
[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([krb5_svc_get_msg],
143                  [AC_CHECK_HEADERS([ibm_svc/krb5_svc.h])],
144                  [AC_CHECK_HEADERS([et/com_err.h])])])])])
145  RRA_LIB_KRB5_RESTORE])
146
147 dnl Sanity-check the results of krb5-config and be sure we can really link a
148 dnl Kerberos program.  If that fails, clear KRB5_CPPFLAGS and KRB5_LIBS so
149 dnl that we know we don't have usable flags and fall back on the manual
150 dnl check.
151 AC_DEFUN([_RRA_LIB_KRB5_CHECK],
152 [RRA_LIB_KRB5_SWITCH
153  AC_CHECK_FUNC([krb5_init_context],
154     [RRA_LIB_KRB5_RESTORE],
155     [RRA_LIB_KRB5_RESTORE
156      KRB5_CPPFLAGS=
157      KRB5_LIBS=
158      _RRA_LIB_KRB5_PATHS
159      _RRA_LIB_KRB5_MANUAL([$1])])])
160
161 dnl The core of the library checking, shared between RRA_LIB_KRB5 and
162 dnl RRA_LIB_KRB5_OPTIONAL.  The single argument, if "true", says to fail if
163 dnl Kerberos could not be found.
164 AC_DEFUN([_RRA_LIB_KRB5_INTERNAL],
165 [AC_REQUIRE([RRA_ENABLE_REDUCED_DEPENDS])
166  AS_IF([test x"$rra_reduced_depends" = xtrue],
167     [_RRA_LIB_KRB5_PATHS
168      _RRA_LIB_KRB5_REDUCED([$1])],
169     [AC_ARG_VAR([KRB5_CONFIG], [Path to krb5-config])
170      AS_IF([test x"$rra_krb5_root" != x && test -z "$KRB5_CONFIG"],
171          [AS_IF([test -x "${rra_krb5_root}/bin/krb5-config"],
172              [KRB5_CONFIG="${rra_krb5_root}/bin/krb5-config"])],
173          [AC_PATH_PROG([KRB5_CONFIG], [krb5-config], ,
174              [${PATH}:/usr/kerberos/bin])])
175      AS_IF([test x"$KRB5_CONFIG" != x && test -x "$KRB5_CONFIG"],
176          [AC_CACHE_CHECK([for krb5 support in krb5-config],
177              [rra_cv_lib_krb5_config],
178              [AS_IF(["$KRB5_CONFIG" 2>&1 | grep krb5 >/dev/null 2>&1],
179                  [rra_cv_lib_krb5_config=yes],
180                  [rra_cv_lib_krb5_config=no])])
181           AS_IF([test x"$rra_cv_lib_krb5_config" = xyes],
182               [KRB5_CPPFLAGS=`"$KRB5_CONFIG" --cflags krb5 2>/dev/null`
183                KRB5_LIBS=`"$KRB5_CONFIG" --libs krb5 2>/dev/null`],
184               [KRB5_CPPFLAGS=`"$KRB5_CONFIG" --cflags 2>/dev/null`
185                KRB5_LIBS=`"$KRB5_CONFIG" --libs 2>/dev/null`])
186           KRB5_CPPFLAGS=`echo "$KRB5_CPPFLAGS" | sed 's%-I/usr/include ?%%'`
187           _RRA_LIB_KRB5_CHECK([$1])
188           RRA_LIB_KRB5_SWITCH
189           AC_CHECK_FUNCS([krb5_get_error_message],
190               [AC_CHECK_FUNCS([krb5_free_error_message])],
191               [AC_CHECK_FUNCS([krb5_get_error_string], ,
192                   [AC_CHECK_FUNCS([krb5_get_err_txt], ,
193                       [AC_CHECK_FUNCS([krb5_svc_get_msg],
194                           [AC_CHECK_HEADERS([ibm_svc/krb5_svc.h])],
195                           [AC_CHECK_HEADERS([et/com_err.h])])])])])
196           RRA_LIB_KRB5_RESTORE],
197          [_RRA_LIB_KRB5_PATHS
198           _RRA_LIB_KRB5_MANUAL([$1])])])
199  rra_krb5_uses_com_err=false
200  case "$LIBS" in
201  *-lcom_err*)
202      rra_krb5_uses_com_err=true
203      ;;
204  esac
205  AM_CONDITIONAL([KRB5_USES_COM_ERR], [test x"$rra_krb5_uses_com_err" = xtrue])])
206
207 dnl The main macro for packages with mandatory Kerberos support.
208 AC_DEFUN([RRA_LIB_KRB5],
209 [rra_krb5_root=
210  rra_krb5_libdir=
211  rra_krb5_includedir=
212  AC_SUBST([KRB5_CPPFLAGS])
213  AC_SUBST([KRB5_LDFLAGS])
214  AC_SUBST([KRB5_LIBS])
215
216  AC_ARG_WITH([krb5],
217     [AS_HELP_STRING([--with-krb5=DIR],
218         [Location of Kerberos v5 headers and libraries])],
219     [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
220         [rra_krb5_root="$withval"])])
221  AC_ARG_WITH([krb5-include],
222     [AS_HELP_STRING([--with-krb5-include=DIR],
223         [Location of Kerberos v5 headers])],
224     [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
225         [rra_krb5_includedir="$withval"])])
226  AC_ARG_WITH([krb5-lib],
227     [AS_HELP_STRING([--with-krb5-lib=DIR],
228         [Location of Kerberos v5 libraries])],
229     [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
230         [rra_krb5_libdir="$withval"])])
231  _RRA_LIB_KRB5_INTERNAL([true])])
232
233 dnl The main macro for packages with optional Kerberos support.
234 AC_DEFUN([RRA_LIB_KRB5_OPTIONAL],
235 [rra_krb5_root=
236  rra_krb5_libdir=
237  rra_krb5_includedir=
238  rra_use_kerberos=
239  AC_SUBST([KRB5_CPPFLAGS])
240  AC_SUBST([KRB5_LDFLAGS])
241  AC_SUBST([KRB5_LIBS])
242
243  AC_ARG_WITH([krb5],
244     [AS_HELP_STRING([--with-krb5@<:@=DIR@:>@],
245         [Location of Kerberos v5 headers and libraries])],
246     [AS_IF([test x"$withval" = xno],
247         [rra_use_kerberos=false],
248         [AS_IF([test x"$withval" != xyes], [rra_krb5_root="$withval"])
249          rra_use_kerberos=true])])
250  AC_ARG_WITH([krb5-include],
251     [AS_HELP_STRING([--with-krb5-include=DIR],
252         [Location of Kerberos v5 headers])],
253     [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
254         [rra_krb5_includedir="$withval"])])
255  AC_ARG_WITH([krb5-lib],
256     [AS_HELP_STRING([--with-krb5-lib=DIR],
257         [Location of Kerberos v5 libraries])],
258     [AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
259         [rra_krb5_libdir="$withval"])])
260
261  AS_IF([test x"$rra_use_kerberos" != xfalse],
262      [AS_IF([test x"$rra_use_kerberos" = xtrue],
263          [_RRA_LIB_KRB5_INTERNAL([true])],
264          [_RRA_LIB_KRB5_INTERNAL([false])])])
265  AS_IF([test x"$KRB5_LIBS" != x],
266     [AC_DEFINE([HAVE_KERBEROS], 1, [Define to enable Kerberos features.])])])
267
268 dnl Check whether krb5_get_init_creds_opt_free takes one argument or two.
269 dnl Early Heimdal used to take a single argument.  Defines
270 dnl HAVE_KRB5_GET_INIT_CREDS_OPT_FREE_2_ARGS if it takes two arguments.
271 dnl
272 dnl Should be called with RRA_LIB_KRB5_SWITCH active.
273 AC_DEFUN([RRA_FUNC_KRB5_GET_INIT_CREDS_OPT_FREE_ARGS],
274 [AC_CACHE_CHECK([if krb5_get_init_creds_opt_free takes two arguments],
275     [rra_cv_func_krb5_get_init_creds_opt_free_args],
276     [AC_TRY_COMPILE([#include <krb5.h>],
277         [krb5_get_init_creds_opt *opts; krb5_context c;
278          krb5_get_init_creds_opt_free(c, opts);],
279         [rra_cv_func_krb5_get_init_creds_opt_free_args=yes],
280         [rra_cv_func_krb5_get_init_creds_opt_free_args=no])])
281  AS_IF([test $rra_cv_func_krb5_get_init_creds_opt_free_args = yes],
282     [AC_DEFINE([HAVE_KRB5_GET_INIT_CREDS_OPT_FREE_2_ARGS], 1,
283         [Define if krb5_get_init_creds_opt_free takes two arguments.])])])