1# $Id: configure.ac,v 1.302 2016/11/24 04:14:01 manu Exp $
2#                                               -*- Autoconf -*-
3# Process this file with autoconf to produce a configure script.
4
5AC_PREREQ(2.57)
6AC_INIT(milter-greylist, 4.6.4, manu@netbsd.org)
7AC_CONFIG_SRCDIR([milter-greylist.c])
8AC_CONFIG_HEADER([config.h])
9
10# Checks for programs.
11AC_PROG_CC
12AC_PROG_LEX
13AC_PROG_YACC
14AC_PROG_INSTALL
15AC_CHECK_PROGS(MKDEP, mkdep)
16AC_CHECK_PROGS(RM, rm)
17AC_CHECK_PROGS(MV, mv)
18AC_CHECK_PROGS(TEST, test)
19AC_CHECK_PROGS(SED, sed)
20AC_CHECK_PROGS(TRUE, true)
21AC_CHECK_PROGS(TOUCH, touch)
22
23
24# Try to configure for override of packaging metadata
25_FIX_PACKAGE_NAME=""
26_FIX_PACKAGE_VERSION=""
27_FIX_PACKAGE_STRING=""
28_FIX_PACKAGE_URL=""
29_FIX_PACKAGE_BUGREPORT=""
30
31AC_ARG_WITH(package-name,
32	[  --with-package-name=STRING  Redefine packaging metadata (name)],
33	[if test x"$withval" = x; then
34	    echo "ERROR: No PACKAGE_NAME provided, using default" >&2
35	else
36	    PACKAGE_NAME="$withval"
37	    PACKAGE_STRING="$PACKAGE_NAME $PACKAGE_VERSION"
38	    _FIX_PACKAGE_NAME=y
39            _FIX_PACKAGE_STRING=y
40	fi])
41
42AC_ARG_WITH(package-version,
43	[  --with-package-version=STRING  Redefine packaging metadata (version)],
44	[if test x"$withval" = x; then
45	    echo "ERROR: No PACKAGE_VERSION provided, using default" >&2
46	else
47	    PACKAGE_VERSION="$withval"
48	    PACKAGE_STRING="$PACKAGE_NAME $PACKAGE_VERSION"
49            _FIX_PACKAGE_VERSION=y
50            _FIX_PACKAGE_STRING=y
51	fi])
52
53PACKAGE_VERSION_UPSTREAM="$PACKAGE_VERSION"
54AC_ARG_WITH(package-version-suffix,
55	[  --with-package-version-suffix=STRING  Vendor-specific suffix to version or release],
56	[if test x"$withval" = x; then
57	    echo "ERROR: No PACKAGE_VERSION_SUFFIX provided, not appending" >&2
58	    PACKAGE_VERSION_SUFFIX=""
59	else
60	    PACKAGE_VERSION_SUFFIX="$withval"
61	    PACKAGE_VERSION="$PACKAGE_VERSION_UPSTREAM$PACKAGE_VERSION_SUFFIX"
62	    PACKAGE_STRING="$PACKAGE_NAME $PACKAGE_VERSION"
63            _FIX_PACKAGE_VERSION=y
64            _FIX_PACKAGE_STRING=y
65	fi])
66
67AC_ARG_WITH(package-string,
68	[  --with-package-string=STRING  Redefine packaging metadata (name and version+suffix)],
69	[if test x"$withval" = x; then
70	    echo "ERROR: No PACKAGE_STRING provided, using default" >&2
71	else
72	    PACKAGE_STRING="$withval"
73            _FIX_PACKAGE_STRING=y
74	fi])
75
76AC_ARG_WITH(package-bugreport,
77	[  --with-package-bugreport=STRING  Redefine packaging metadata (email)],
78	[if test x"$withval" = x; then
79	    echo "ERROR: No PACKAGE_BUGREPORT provided, using default" >&2
80	else
81	    PACKAGE_BUGREPORT="$withval"
82            _FIX_PACKAGE_BUGREPORT=y
83	fi])
84
85AC_ARG_WITH(package-url,
86	[  --with-package-url=STRING  Redefine packaging metadata (distro URL)],
87	[if test x"$withval" = x; then
88	    echo "ERROR: No PACKAGE_URL provided, using default" >&2
89	else
90	    PACKAGE_URL="$withval"
91            _FIX_PACKAGE_URL=y
92	fi])
93
94m4_define([__AC_UNDEFINE],[_AC_DEFINE([#ifdef $1
95 #undef $1
96#endif])])
97
98if test x"$_FIX_PACKAGE_NAME" = xy; then
99    __AC_UNDEFINE([PACKAGE_NAME])
100    AC_DEFINE_UNQUOTED([PACKAGE_NAME], ["$PACKAGE_NAME"], [Packaging metadata: distro source code name])
101fi
102
103if test x"$_FIX_PACKAGE_VERSION" = xy; then
104    __AC_UNDEFINE([PACKAGE_VERSION])
105    AC_DEFINE_UNQUOTED([PACKAGE_VERSION], ["$PACKAGE_VERSION"], [Packaging metadata: distro source code version])
106fi
107if test x"$_FIX_PACKAGE_STRING" = xy; then
108    __AC_UNDEFINE([PACKAGE_STRING])
109    AC_DEFINE_UNQUOTED([PACKAGE_STRING], ["$PACKAGE_STRING"], [Packaging metadata: distro source code name+version])
110fi
111if test x"$_FIX_PACKAGE_URL" = xy; then
112    __AC_UNDEFINE([PACKAGE_URL])
113    AC_DEFINE_UNQUOTED([PACKAGE_URL], ["$PACKAGE_URL"], [Packaging metadata: distro contact])
114fi
115if test x"$_FIX_PACKAGE_BUGREPORT" = xy; then
116    __AC_UNDEFINE([PACKAGE_BUGREPORT])
117    AC_DEFINE_UNQUOTED([PACKAGE_BUGREPORT], ["$PACKAGE_BUGREPORT"], [Packaging metadata: distro contact])
118fi
119
120AC_DEFINE_UNQUOTED([PACKAGE_VERSION_UPSTREAM], ["$PACKAGE_VERSION_UPSTREAM"], [Packaging metadata: upstream source code version])
121AC_DEFINE_UNQUOTED([PACKAGE_VERSION_SUFFIX], ["$PACKAGE_VERSION_SUFFIX"], [Packaging metadata: Vendor-specific suffix to version or release])
122
123
124
125# Check that the compiler won't bug us for -Wall
126AC_MSG_CHECKING([if compiler accepts -Wall])
127SAVEDCFLAGS=$CFLAGS
128CFLAGS=$CFLAGS" -Wall"
129wall="no"
130AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])],
131    [wall="yes"; SAVEDCFLAGS=$CFLAGS; efl=$efl" -Wall"])
132AC_MSG_RESULT([$wall])
133CFLAGS=$SAVEDCFLAGS
134
135# Check that the compiler won't bug us for -Werror
136AC_MSG_CHECKING([if compiler accepts -Werror])
137SAVEDCFLAGS=$CFLAGS
138CFLAGS=$CFLAGS" -Werror"
139werror="no"
140AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])],
141    [Werror="yes"; efl=$efl" -Werror"])
142AC_MSG_RESULT([$werror])
143CFLAGS=$SAVEDCFLAGS
144
145NORPSAVEDLDFLAGS=$LDFLAGS
146
147# Any linker will probably accept -L
148rpath="-L"
149
150# Check if the linker accepts -R
151AC_MSG_CHECKING([if ld accepts -R])
152SAVEDLDFLAGS=$LDFLAGS
153LDFLAGS=$LDFLAGS" -Wl,-R=/"
154AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])],
155    [rpath="-R"; ldrpath=yes], [ldrpath=no])
156LDFLAGS=$SAVEDLDFLAGS
157AC_MSG_RESULT([$ldrpath])
158
159# Check if the linker accepts --rpath
160AC_MSG_CHECKING([if ld accepts --rpath])
161SAVEDLDFLAGS=$LDFLAGS
162LDFLAGS=$LDFLAGS" -Wl,--rpath=/"
163AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])],
164    [rpath="--rpath="; ldrpath=yes], [ldrpath=no])
165LDFLAGS=$SAVEDLDFLAGS
166AC_MSG_RESULT([$ldrpath])
167
168# Path to required packages
169AC_ARG_WITH(db,
170        [  --with-db=DIR   Find Berkeley DB in DIR],
171        [CFLAGS=$CFLAGS" -I$withval/include"
172            LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"])
173AC_ARG_WITH(libmilter,
174	[  --with-libmilter=DIR   Find libmilter in DIR],
175	[CFLAGS=$CFLAGS" -I$withval/include"
176	    LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"])
177AC_ARG_WITH(libpthread,
178	[  --with-libpthread=DIR  Find libpthread in DIR],
179	[CFLAGS=$CFLAGS" -I$withval/include"
180	    LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"])
181
182# Which BIND resolver to use?..
183use_libbind=no
184SAVEDLDFLAGS=$LDFLAGS
185SAVEDCFLAGS=$CFLAGS
186SAVEDLIBS=$LIBS
187AC_ARG_WITH(libbind,
188	[  --with-libbind=DIR	Find libbind in DIR],
189	[CFLAGS=$CFLAGS" -I$withval/include/bind -I$withval/include"
190	    LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"
191	    AC_CHECK_LIB(bind, bind9_getaddresses, [
192		SAVEDLDFLAGS=$LDFLAGS
193		SAVEDCFLAGS=$CFLAGS
194		SAVEDLIBS="-lbind $LIBS"
195		use_libbind=yes
196	    ], [AC_CHECK_LIB(bind9, bind9_getaddresses, [
197                SAVEDLDFLAGS=$LDFLAGS
198                SAVEDCFLAGS=$CFLAGS
199                SAVEDLIBS="-lbind9 $LIBS"
200                use_libbind=yes
201	    ], [echo "neither -lbind nor -lbind9 worked. check config.log for details"
202		exit 1;])
203	 ])
204])
205LDFLAGS=$SAVEDLDFLAGS
206CFLAGS=$SAVEDCFLAGS
207LIBS=$SAVEDLIBS
208
209# OpenSSL, may be needed by itself and for curl, ldap, dkim/opendkim, ...
210dir_openssl=""
211LDFLAGS_openssl=""
212LIBS_openssl_ssl=""
213LIBS_openssl_crypto=""
214LIBS_openssl_ssh2=""
215SAVEDLDFLAGS=$LDFLAGS
216SAVEDCFLAGS=$CFLAGS
217SAVEDLIBS=$LIBS
218AC_ARG_WITH(openssl,
219	[  --with-openssl(=DIR)  Find OpenSSL in DIR],
220	[if test x"$withval" = xyes; then withval=""; fi
221	 if test -z "$withval"; then
222            for _DIR in "/usr/local/ssl" "/usr/local" "/usr" "/usr/sfw" "/opt/sfw" "/"; do
223                if test -z "$withval" -a -e "$_DIR/lib/libssl.so"; then
224                    withval="$_DIR";
225                    echo "autodetected OpenSSL library directory: $_DIR"
226                fi
227            done
228	fi
229	if test -z "$withval"; then
230	    echo "Error: path for OpenSSL libraries not provided and not autodetected!"
231	    exit 1
232        fi
233	LDFLAGS_openssl="-L$withval/lib -Wl,$rpath$withval/lib"
234	SAVEDLDFLAGS=$LDFLAGS" $LDFLAGS_openssl"
235        SAVEDCFLAGS=$CFLAGS" -I$withval/include"
236	dir_openssl="$withval"
237	LIBS_openssl_ssl="-lssl"
238	LIBS_openssl_crypto="-lcrypto"
239	SAVEDLIBS="$LIBS_openssl_ssl $LIBS_openssl_crypto $LIBS"
240	AC_CHECK_LIB(c -lssh2, main, [
241	    LIBS_openssl_ssh2="-lssh2 -lz"
242	    echo "autodetected libssh2: $LIBS_openssl_ssh2"
243	    SAVEDLIBS="$LIBS_openssl_ssh2 $SAVEDLIBS"],[])
244	])
245LDFLAGS=$SAVEDLDFLAGS
246CFLAGS=$SAVEDCFLAGS
247LIBS=$SAVEDLIBS
248
249SAVEDLDFLAGS=$LDFLAGS
250SAVEDCFLAGS=$CFLAGS
251SAVEDLIBS=$LIBS
252AC_ARG_WITH(openssl-static,
253	[  --with-openssl-static(=DIR)  Find OpenSSL in DIR and link statically],
254	[if test x"$withval" = xyes; then withval=""; fi
255	if test -z "$withval"; then
256            for _DIR in "/usr/local/ssl" "/usr/local" "/usr" "/usr/sfw" "/opt/sfw" "/"; do
257                if test -z "$withval" -a -e "$_DIR/lib/libssl.a"; then
258                    withval="$_DIR";
259                    echo "autodetected OpenSSL-static library directory: $_DIR"
260                fi
261            done
262	fi
263	if test -z "$withval"; then
264	    echo "Error: path for OpenSSL static libraries not provided and not autodetected!"
265	    exit 1
266        fi
267	LDFLAGS_openssl=""
268	SAVEDLDFLAGS=$LDFLAGS" $LDFLAGS_openssl"
269	SAVEDCFLAGS=$CFLAGS" -I$withval/include"
270	dir_openssl="$withval"
271	LIBS_openssl_ssl="$withval/lib/libssl.a"
272	LIBS_openssl_crypto="$withval/lib/libcrypto.a"
273	SAVEDLIBS="$LIBS_openssl_ssl $LIBS_openssl_crypto $LIBS"
274	_lib_ssh2=""
275	for _DIR in "/usr/local/ssl" "/usr/local" "/usr" "/usr/sfw" "/opt/sfw" "/"; do
276	    if test -z "$_lib_ssh2" -a -e "$_DIR/lib/libssh2.a"; then
277		_lib_ssh2="$_DIR/lib/libssh2.a"
278	    fi
279	done
280	if test -z "$_lib_ssh2"; then
281	    _lib_ssh2="-lssh2"
282	fi
283	AC_CHECK_LIB(c $_lib_ssh2, main, [
284	    LIBS_openssl_ssh2="$_lib_ssh2 -lz"
285	    echo "autodetected libssh2: $LIBS_openssl_ssh2"
286	    SAVEDLIBS="$LIBS_openssl_ssh2 $SAVEDLIBS"],[])
287	unset _lib_ssh2
288	])
289LDFLAGS=$SAVEDLDFLAGS
290CFLAGS=$SAVEDCFLAGS
291LIBS=$SAVEDLIBS
292
293# OpenLDAP may be used instead of curl to access multiple
294# (failover) LDAP catalog servers
295dir_openldap=""
296LDFLAGS_openldap=""
297LIBS_openldap=""
298SAVEDLDFLAGS=$LDFLAGS
299SAVEDCFLAGS=$CFLAGS
300SAVEDLIBS=$LIBS
301AC_ARG_WITH(openldap,
302	[  --with-openldap(=DIR)  Find OpenLDAP in DIR],
303	[if test x"$withval" = xyes; then withval=""; fi
304        if test -z "$withval"; then
305            for _DIR in "/usr/local" "/usr" "/usr/sfw" "/opt/sfw" "/"; do
306                if test -z "$withval" -a -e "$_DIR/lib/libldap_r-2.4.so" || \
307		   test -z "$withval" -a -e "$_DIR/lib/libldap_r.so"; then
308                    withval="$_DIR";
309                    echo "autodetected OpenLDAP library directory: $_DIR"
310                fi
311            done
312        fi
313	if test -z "$withval"; then
314	    echo "Error: path for OpenLDAP libraries not provided and not autodetected!"
315	    exit 1
316        fi
317	    SAVEDCFLAGS=$CFLAGS" -I$withval/include -DUSE_LDAP -DLDAP_DEPRECATED"
318	    LDFLAGS_openldap="-L$withval/lib -Wl,$rpath$withval/lib"
319	    SAVEDLDFLAGS=$LDFLAGS" $LDFLAGS_openldap"
320	    dir_openldap="$withval"
321	    if test -d "$dir_openldap/include/openldap"; then
322		### Promote this include path
323		SAVEDCFLAGS="-I$dir_openldap/include/openldap $SAVEDCFLAGS"
324	    fi
325            ### Pick variants of more important libs that may be needed by LDAP
326            _ldap_LIBS="-lldap_r-2.4 -llber-2.4"
327            AC_CHECK_LIB(c $_ldap_LIBS, ldap_search, [
328               SAVEDLIBS="$_ldap_LIBS $LIBS"
329	       LIBS_openldap="$_ldap_LIBS"
330               ],[_ldap_LIBS="-lldap_r -llber"
331                       AC_CHECK_LIB([c $_ldap_LIBS], ldap_search, [
332                       SAVEDLIBS="$_ldap_LIBS $LIBS"
333		       LIBS_openldap="$_ldap_LIBS"
334		       ],[echo "Error: no OpenLDAP library names located!"; exit 1;])
335	       ])
336	])
337LDFLAGS=$SAVEDLDFLAGS
338CFLAGS=$SAVEDCFLAGS
339LIBS=$SAVEDLIBS
340
341# Try to configure for openldap-static
342SAVEDLDFLAGS=$LDFLAGS
343SAVEDCFLAGS=$CFLAGS
344SAVEDLIBS=$LIBS
345AC_ARG_WITH(openldap-static,
346       [  --with-openldap-static(=DIR)  Find OpenLDAP in DIR and link statically],
347       [if test x"$withval" = xyes; then withval=""; fi
348	if test -z "$withval"; then
349	    for _DIR in "/usr/local" "/usr" "/usr/sfw" "/opt/sfw" "/"; do
350		if test -z "$withval" -a -e "$_DIR/lib/libldap_r-2.4.a" || \
351		   test -z "$withval" -a -e "$_DIR/lib/libldap_r.a"; then
352		    withval="$_DIR";
353		    echo "autodetected OpenLDAP library directory: $_DIR"
354		fi
355	    done
356	fi
357	if test -z "$withval"; then
358	    echo "Error: path for OpenLDAP static libraries not provided and not autodetected!"
359	    exit 1
360        fi
361	   dir_openldap="$withval"
362	   LDFLAGS_openldap=""
363	   SAVEDLDFLAGS=$LDFLAGS" $LDFLAGS_openldap"
364	   SAVEDCFLAGS=$CFLAGS" -I$withval/include -DUSE_LDAP -DLDAP_DEPRECATED"
365	   SAVEDLIBS="$LIBS"
366	   if test -d "$dir_openldap/include/openldap"; then
367		### Promote this include path
368		SAVEDCFLAGS="-I$dir_openldap/include/openldap $SAVEDCFLAGS"
369	   fi
370	   ### Lower-priority variants first; in case of conflicts use the later one
371	   if test -e "$withval/lib/libldap_r.a"; then
372		LIBS_openldap="$withval/lib/libldap_r.a $withval/lib/liblber.a"
373	   fi
374	   if test -e "$withval/lib/libldap_r-2.4.a"; then
375		LIBS_openldap="$withval/lib/libldap_r-2.4.a $withval/lib/liblber-2.4.a"
376	   fi
377	   if test -z "$LIBS_openldap"; then
378		echo "Error: searched for OpenLDAP static libraries in dir '$withval/lib', not found"
379		exit 1
380	   fi
381	   LIBS_openldap_add=""
382	   ### More generic libs
383	   for L in nsl socket gss dl resolv; do
384		AC_CHECK_LIB(c -l$L, main, [LIBS_openldap_add="-l$L $LIBS_openldap_add"])
385	   done
386	   ### Pick variants of more important libs that may be needed by LDAP
387	   _ldap_LIBS="$LIBS_openldap"
388           AC_CHECK_LIB(c $_ldap_LIBS, ldap_search, [
389               SAVEDLIBS="$_ldap_LIBS $LIBS"
390               ],[_ldap_LIBS="$LIBS_openldap $LIBS_openldap_add -lsasl"
391		       AC_CHECK_LIB([c $_ldap_LIBS], ldap_search, [
392                       SAVEDLIBS="$_ldap_LIBS $LIBS"
393               ],[_ldap_LIBS="$LIBS_openldap $LIBS_openldap_add -lsasl2"
394		       AC_CHECK_LIB([c $_ldap_LIBS], ldap_search, [
395                       SAVEDLIBS="$_ldap_LIBS $LIBS"
396               ],[_ldap_LIBS="$LIBS_openldap $LIBS_openldap_add -lsasl $LDFLAGS_openssl $LIBS_openssl_crypto $LIBS_openssl_ssl"
397		       AC_CHECK_LIB([c $_ldap_LIBS], ldap_search, [
398                       SAVEDLIBS="$_ldap_LIBS $LIBS"
399               ],[_ldap_LIBS="$LIBS_openldap $LIBS_openldap_add -lsasl2 $LDFLAGS_openssl $LIBS_openssl_crypto $LIBS_openssl_ssl"
400		       AC_CHECK_LIB([c $_ldap_LIBS], ldap_search, [
401                       SAVEDLIBS="$_ldap_LIBS $LIBS"
402        	   ], [echo "OpenLDAP static libraries libldap_r.a and/or liblber.a, or their shared";
403		       echo "  dependencies (SASL, OpenSSL, GSS) were not found, check config.log for details";
404            	       echo "Remove --with-openldap-static to build without LDAP support, or";
405		       echo "  try --with-openldap for the shared library, or";
406		       echo "  try --with-curl for an alternate solution";
407            	       exit 1;])
408	       ])
409	       ])
410	       ])
411       ])
412])
413LDFLAGS=$SAVEDLDFLAGS
414CFLAGS=$SAVEDCFLAGS
415LIBS=$SAVEDLIBS
416
417use_libcurl=no
418AC_ARG_WITH(libcurl,
419	[  --with-libcurl=DIR  Find libcurl in DIR],
420	[CFLAGS=$CFLAGS" -I$withval/include -DUSE_CURL"
421	    LIBS="-lcurl $LIBS"
422	    LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"
423	    use_libcurl=yes])
424
425# Try to configure for libcurl-static
426SAVEDLDFLAGS=$LDFLAGS
427SAVEDCFLAGS=$CFLAGS
428SAVEDLIBS=$LIBS
429AC_ARG_WITH(libcurl-static,
430       [  --with-libcurl-static=DIR  Find libcurl in DIR and link statically],
431       [SAVEDLDFLAGS=$LDFLAGS" -L$withval/lib"
432	   SAVEDCFLAGS=$CFLAGS" -I$withval/include -DUSE_CURL"
433	   _lib_idn=""
434	   AC_CHECK_LIB(idn, main, [echo "autodetected libidn, will use with libcurl"
435	       _lib_idn="-lidn"
436	       SAVEDLIBS=" $_lib_idn $LIBS"])
437	   LIBS=$SAVEDLIBS
438           AC_CHECK_LIB(curl $_lib_idn, Curl_connect, [
439               SAVEDLDFLAGS=$LDFLAGS
440               SAVEDCFLAGS=$CFLAGS
441               SAVEDLIBS="$withval/lib/libcurl.a $LIBS"
442	       use_libcurl=yes
443               ],[AC_CHECK_LIB([curl $_lib_idn $LIBS_openssl_ssh2],  Curl_connect, [
444                       SAVEDLDFLAGS=$LDFLAGS
445                       SAVEDCFLAGS=$CFLAGS
446                       SAVEDLIBS="$withval/lib/libcurl.a $LIBS_openssl_ssh2 $LIBS"
447		       use_libcurl=yes
448               ], [echo "static library libcurl.a and/or libssh2.a not found, check config.log for details";
449                   echo "Remove --with-libcurl-static to build without CURL support ";
450		   echo "  or try --with-libcurl for the shared library";
451                   exit 1;])
452       ])
453])
454LDFLAGS=$SAVEDLDFLAGS
455CFLAGS=$SAVEDCFLAGS
456LIBS=$SAVEDLIBS
457
458AC_ARG_WITH(libGeoIP,
459	[  --with-libGeoIP=DIR  Find libGeoIP in DIR],
460	[CFLAGS=$CFLAGS" -I$withval/include -DUSE_GEOIP"
461	    LIBS="-lGeoIP $LIBS"
462	    USE_GEOIP="true"
463	    LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"])
464AC_ARG_WITH(libmaxminddb,
465	[  --with-libmaxminddb=DIR  Find GeoIP2 libmaxminddb in DIR],
466	[if test -z "$USE_GEOIP"; then
467		CFLAGS=$CFLAGS" -I$withval/include -DUSE_GEOIP2"
468		LIBS="-lmaxminddb $LIBS"
469		LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"
470	else
471		echo "--with-libGeoIP and -with-libmaxminddb are mutually exclusive";
472		exit 1;
473	fi])
474AC_ARG_WITH(libdmalloc,
475	[  --with-libdmalloc=DIR  Find libdmalloc in DIR],
476	[CFLAGS=$CFLAGS" -I$withval/include -DUSE_DMALLOC"
477	    LIBS="-ldmalloc $LIBS"
478	    LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"])
479
480# Checks for header files.
481AC_HEADER_STDC
482AC_CHECK_HEADERS([arpa/inet.h fcntl.h netinet/in.h stdlib.h string.h strings.h sys/socket.h sys/time.h syslog.h unistd.h sys/param.h netdb.h getopt.h sys/cdefs.h arpa/nameser.h stdbool.h sys/queue.h])
483
484# Checks for typedefs, structures, and compiler characteristics.
485AC_TYPE_PID_T
486AC_TYPE_SIZE_T
487AC_HEADER_TIME
488
489# Do we have res_state type? Tru64 UNIX doesn't have...
490AC_CHECK_TYPES([res_state],[],[],[
491		#include <sys/types.h>
492		#include <netinet/in.h>
493		#include <arpa/nameser.h>
494		#include <resolv.h>
495])
496
497# Do we need -lpthread with -lbind?
498if test $use_libbind = yes ; then
499	AC_MSG_CHECKING([if -lbind needs -lpthread to link])
500	lbind_needs_lpthread=no
501	SAVEDLIBS=$LIBS
502	AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])],
503	    [],
504	    [
505		LIBS="-lpthread $LIBS"
506		AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])],
507		    [lbind_needs_lpthread=yes]);
508	    ]);
509	if test $lbind_needs_lpthread = yes ; then
510		SAVEDLIBS=$LIBS
511	fi
512	AC_MSG_RESULT($lbind_needs_lpthread)
513	LIBS=$SAVEDLIBS
514fi
515
516# Do we need -lcrypto with -lcurl?
517if test $use_libcurl = yes ; then
518	AC_MSG_CHECKING([if -lcurl needs -lcrypto to link and run])
519	lcurl_needs_lcrypto=no
520	SAVEDLIBS=$LIBS
521	SAVEDLDFLAGS=$LDFLAGS
522	AC_RUN_IFELSE([AC_LANG_PROGRAM([],[])],
523	    [],
524	    [LDFLAGS="$LDFLAGS $LDFLAGS_openssl"
525		LIBS="$LIBS_openssl_crypto $LIBS"
526		AC_RUN_IFELSE([AC_LANG_PROGRAM([],[])],
527		    [lcurl_needs_lcrypto=yes
528		    SAVEDLIBS=$LIBS],
529			[echo "Required libcrypto not found for libcurl.";
530			 echo "  Use --with-openssl=DIR to provide the OpenSSL installation root.";
531                	 exit 1;])
532	    ])
533	if test $lcurl_needs_lcrypto = yes ; then
534		SAVEDLIBS=$LIBS
535		SAVEDLDFLAGS=$LDFLAGS
536	fi
537	AC_MSG_RESULT($lcurl_needs_lcrypto)
538	LIBS=$SAVEDLIBS
539	LDFLAGS=$SAVEDLDFLAGS
540fi
541
542# Checks for library functions.
543AC_FUNC_FORK
544AC_FUNC_STRFTIME
545AC_FUNC_MEMCMP
546AC_FUNC_SELECT_ARGTYPES
547AC_FUNC_SETVBUF_REVERSED
548AC_FUNC_STAT
549AC_CHECK_FUNCS([bzero gettimeofday malloc inet_ntoa strerror select socket initgroups strlcat vsyslog strcasestr getaddrinfo getnameinfo])
550
551
552# Check for libpthread. On FreeBSD, the libc_r does the job.
553# On digitalUNIX, libpthreads (with a trailing s) is the library to use
554# FIXME: FreeBSD 4.x wants -pthread
555AC_CHECK_LIB([pthread], [pthread_create],
556    [LIBS="$LIBS -lpthread"],
557    [AC_CHECK_LIB([c_r], [pthread_create],
558        [LIBS="$LIBS -lc_r"],
559	[AC_CHECK_LIB([pthreads -lpthread], [pthread_create],
560            [LIBS="$LIBS -lpthreads -lpthread"],
561                [echo "Required libpthread not found. Use --with-libpthread";
562                 exit 1;])
563	])
564     ])
565
566# Try to configure for libspf2
567SAVEDLDFLAGS=$LDFLAGS
568SAVEDCFLAGS=$CFLAGS
569AC_ARG_WITH(libspf2,
570       [  --with-libspf2=DIR  Find libspf2 in DIR],
571       [LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"
572           CFLAGS=$CFLAGS" -I$withval/include"
573           AC_CHECK_LIB(spf2, SPF_server_new, [
574               SAVEDLDFLAGS=$LDFLAGS
575               SAVEDCFLAGS=$CFLAGS
576               LIBS=$LIBS" -lspf2"
577               AC_DEFINE([HAVE_SPF2], [], [we use libspf2])
578               ],[AC_CHECK_LIB([spf2 -lintl],  SPF_server_new, [
579                       SAVEDLDFLAGS=$LDFLAGS
580                       SAVEDCFLAGS=$CFLAGS
581                       LIBS=$LIBS" -lspf2 -lintl"
582                       AC_DEFINE([HAVE_SPF2], [], [we use libspf2])
583               ], [echo "libspf2 not found, check config.log for details"
584                   echo "Remove --with-libspf2 to build without SPF support"
585                   exit 1;])
586       ])
587])
588LDFLAGS=$SAVEDLDFLAGS
589CFLAGS=$SAVEDCFLAGS
590
591# Try to configure for libspf2-static
592SAVEDLDFLAGS=$LDFLAGS
593SAVEDCFLAGS=$CFLAGS
594AC_ARG_WITH(libspf2-static,
595       [  --with-libspf2-static=DIR  Find libspf2 in DIR and link statically],
596       [LDFLAGS=$LDFLAGS" -L$withval/lib"
597	   CFLAGS=$CFLAGS" -I$withval/include"
598           AC_CHECK_LIB(spf2, SPF_server_new, [
599               SAVEDLDFLAGS=$LDFLAGS
600               SAVEDCFLAGS=$CFLAGS
601               LIBS=$LIBS" $withval/lib/libspf2.a"
602               AC_DEFINE([HAVE_SPF2], [], [we use libspf2])
603               ],[AC_CHECK_LIB([spf2 -lintl],  SPF_server_new, [
604                       SAVEDLDFLAGS=$LDFLAGS
605                       SAVEDCFLAGS=$CFLAGS
606                       LIBS=$LIBS" $withval/lib/libspf2.a -lintl"
607                       AC_DEFINE([HAVE_SPF2], [], [we use libspf2])
608               ], [echo "static library libspf2.a not found, check config.log for details"
609                   echo "Remove --with-libspf2-static to build without SPF support "
610		   echo "  or try --with-libspf2 for the shared library"
611                   exit 1;])
612       ])
613])
614LDFLAGS=$SAVEDLDFLAGS
615CFLAGS=$SAVEDCFLAGS
616
617# Try to configure for libspf2 1.0
618SAVEDLDFLAGS=$LDFLAGS
619SAVEDCFLAGS=$CFLAGS
620AC_ARG_WITH(libspf2_10,
621       [  --with-libspf2_10=DIR  Find libspf2 1.0 in DIR],
622       [LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"
623           CFLAGS=$CFLAGS" -I$withval/include"
624           AC_CHECK_LIB(spf2, SPF_create_config, [
625               SAVEDLDFLAGS=$LDFLAGS
626               SAVEDCFLAGS=$CFLAGS
627               LIBS=$LIBS" -lspf2"
628               AC_DEFINE([HAVE_SPF2_10], [], [we use libspf2 1.0])
629               ],[AC_CHECK_LIB([spf2 -lintl],  SPF_create_config, [
630                       SAVEDLDFLAGS=$LDFLAGS
631                       SAVEDCFLAGS=$CFLAGS
632                       LIBS=$LIBS" -lspf2 -lintl"
633                       AC_DEFINE([HAVE_SPF2_10], [], [we use libspf2 1.0])
634               ], [echo "libspf2 1.0 not found, check config.log for details"
635                   echo "Remove --with-libspf2_10 to build without SPF support"
636                   exit 1;])
637       ])
638])
639LDFLAGS=$SAVEDLDFLAGS
640CFLAGS=$SAVEDCFLAGS
641
642# Try to configure for libspf_alt
643SAVEDLDFLAGS=$LDFLAGS
644SAVEDCFLAGS=$CFLAGS
645AC_ARG_WITH(libspf_alt,
646	[  --with-libspf_alt=DIR  Find libspf_alt in DIR],
647	[LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"
648	    CFLAGS=$CFLAGS" -I$withval/include"
649	    AC_CHECK_LIB(spf_alt, SPF_create_config, [
650		SAVEDLDFLAGS=$LDFLAGS
651		SAVEDCFLAGS=$CFLAGS
652		LIBS=$LIBS" -lspf_alt"
653		AC_DEFINE([HAVE_SPF_ALT], [], [we use libspf_alt])
654		],[AC_CHECK_LIB([spf_alt -lintl],  SPF_create_config, [
655			SAVEDLDFLAGS=$LDFLAGS
656			SAVEDCFLAGS=$CFLAGS
657			LIBS=$LIBS" -lspf_alt -lintl"
658			AC_DEFINE([HAVE_SPF_ALT], [], [we use libspf_alt])
659		], [echo "libspf_alt not found, check config.log for details"
660		    echo "Remove --with-libspf_alt to build without SPF support"
661		    exit 1;])
662	])
663])
664LDFLAGS=$SAVEDLDFLAGS
665CFLAGS=$SAVEDCFLAGS
666
667# Try to configure for libspf
668SAVEDLDFLAGS=$LDFLAGS
669SAVEDCFLAGS=$CFLAGS
670AC_ARG_WITH(libspf,
671	[  --with-libspf=DIR  Find libspf in DIR],
672	[LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"
673	    CFLAGS=$CFLAGS" -I$withval/include"
674	    AC_CHECK_LIB(spf, SPF_init, [
675		SAVEDLDFLAGS=$LDFLAGS
676		SAVEDCFLAGS=$CFLAGS
677		LIBS=$LIBS" -lspf"
678		AC_DEFINE([HAVE_SPF], [], [we use libspf])
679		], [echo "libspf not found, check config.log for details"
680		    echo "Remove --with-libspf to build without SPF support"
681		    exit 1;])
682])
683LDFLAGS=$SAVEDLDFLAGS
684CFLAGS=$SAVEDCFLAGS
685
686# Try to configure for libdkim
687SAVEDLDFLAGS=$LDFLAGS
688SAVEDCFLAGS=$CFLAGS
689AC_ARG_WITH(libdkim,
690	[  --with-libdkim=DIR  Find Sendmail's libdkim in DIR],
691	[LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"
692	    CFLAGS=$CFLAGS" -I$withval/include"
693	    AC_CHECK_LIB([dkim -lcrypto], dkim_init, [
694		SAVEDLDFLAGS=$LDFLAGS
695		SAVEDCFLAGS=$CFLAGS
696		LIBS=$LIBS" -ldkim -lcrypto"
697		AC_DEFINE([USE_DKIM], [], [we use libdkim])
698		], [echo "libdkim not found, check config.log for details"
699		    echo "Remove --with-dkim to build without DKIM support"
700		    exit 1;])
701])
702LDFLAGS=$SAVEDLDFLAGS
703CFLAGS=$SAVEDCFLAGS
704
705# Try to configure for libopendkim
706SAVEDLDFLAGS=$LDFLAGS
707SAVEDCFLAGS=$CFLAGS
708AC_ARG_WITH(libopendkim,
709	[  --with-libopendkim=DIR  Find The Trusted Domain Project's libopendkim in DIR],
710	[LDFLAGS=$LDFLAGS" -L$withval/lib -Wl,$rpath$withval/lib"
711	    dkim_include=""
712	    for dir in include include/dkim include/opendkim ; do
713	        test -f $withval/$dir/dkim.h && dkim_include=$dir
714	    done
715	    CFLAGS=$CFLAGS" -I$withval/$dkim_include"
716	    AC_CHECK_LIB([opendkim -lcrypto], dkim_init, [
717		SAVEDLDFLAGS=$LDFLAGS
718		SAVEDCFLAGS=$CFLAGS
719		LIBS=$LIBS" -lopendkim -lcrypto"
720		AC_DEFINE([USE_DKIM], [], [we use libopendkim])
721		], [echo "libopendkim not found, check config.log for details"
722		    echo "Remove --with-opendkim to build without DKIM support"
723		    exit 1;])
724])
725LDFLAGS=$SAVEDLDFLAGS
726CFLAGS=$SAVEDCFLAGS
727
728# NOTE: Sun CCS make insists on a space "-j 1" instead of "-j1" ok for gmake.
729AC_ARG_ENABLE(parallel-make,
730	[  --disable-parallel-make   use if your make does not support -j],
731	[if test "x${enable_parallel_make}" = "xyes" ; then
732		AC_SUBST(MAKE_J1, "-j 1")
733	 else
734		AC_SUBST(MAKE_J1, "")
735	 fi
736	],
737	[AC_SUBST(MAKE_J1, "-j 1")])
738
739AC_ARG_ENABLE(rpath,
740	[  --disable-rpath	do not hardcode runtime library paths],
741	[if test x$enableval = xno; then
742		LDFLAGS=$NORPSAVEDLDFLAGS
743	fi])
744
745# Check for socket functions linking. On Solaris, -lsocket -lnsl is required
746AC_CHECK_LIB([socket], [socket], [LIBS="$LIBS -lsocket"],
747	[AC_CHECK_LIB([socket -lnsl], [socket],
748		[LIBS="$LIBS -lsocket -lnsl"], [])
749	])
750
751# Check for inet_aton (libmilter uses it). On solaris, -lresolv is required
752AC_CHECK_LIB([resolv], [inet_aton], [LIBS="$LIBS -lresolv"], [])
753
754# Check for inet_pton (libmilter uses it). On solaris, -lnsl is required
755AC_CHECK_LIB([nsl], [inet_pton], [LIBS="$LIBS -lnsl"], [])
756
757# Check if we have res_ninit()
758AC_MSG_CHECKING([for res_ninit])
759AC_LINK_IFELSE([AC_LANG_PROGRAM([
760		#include <sys/types.h>
761		#include <netinet/in.h>
762		#include <arpa/nameser.h>
763		#include <resolv.h>
764	],[
765		struct __res_state res;
766		res_ninit(&res);
767	])], [AC_DEFINE([HAVE_RES_NINIT], [1],
768	    [we have res_ninit]) res_ninit=yes],
769	    [res_ninit=no])
770AC_MSG_RESULT([$res_ninit])
771
772# Check if we have res_ndestroy()
773AC_MSG_CHECKING([for res_ndestroy])
774AC_LINK_IFELSE([AC_LANG_PROGRAM([
775		#include <sys/types.h>
776		#include <netinet/in.h>
777		#include <arpa/nameser.h>
778		#include <resolv.h>
779	],[
780		struct __res_state res;
781		res_ndestroy(&res);
782	])], [AC_DEFINE([HAVE_RES_NDESTROY], [1],
783	    [we have res_ndestroy]) res_ndestroy=yes],
784	    [res_ndestroy=no])
785AC_MSG_RESULT([$res_ndestroy])
786
787# Check if we have res_nupdate()
788AC_MSG_CHECKING([for res_nupdate])
789AC_LINK_IFELSE([AC_LANG_PROGRAM([
790		#include <sys/types.h>
791		#include <netinet/in.h>
792		#include <arpa/nameser.h>
793		#include <resolv.h>
794		#include <res_update.h>
795	],[
796		struct __res_state res;
797		ns_updrec updrec;
798		res_nupdate(&res, &updrec, NULL);
799	])], [AC_DEFINE([HAVE_RES_NUPDATE], [1], [we have res_nupdate])
800	      AC_DEFINE([USE_NSUPDATE], [1], [use nsupdate])
801	      res_nupdate=yes],
802	     [res_nupdate=no])
803AC_MSG_RESULT([$res_nupdate])
804
805# Check for getaddrinfo and getnameinfo
806AC_DEFUN([IPv6_CHECK_FUNC], [
807AH_TEMPLATE(AS_TR_CPP(HAVE_$1), [Define to 1 if you have the `]$1[' function.])
808AC_CHECK_FUNC($1, [dnl
809  ac_cv_lib_socket_$1=no
810  ac_cv_lib_inet6_$1=no
811], [dnl
812  AC_CHECK_LIB(socket, $1, [dnl
813    LIBS="$LIBS -lsocket"
814    ac_cv_lib_inet6_$1=no
815  ], [dnl
816    AC_MSG_CHECKING([whether your system has IPv6 directory])
817    AC_CACHE_VAL(ipv6_cv_dir, [dnl
818      for ipv6_cv_dir in /usr/local/v6 /usr/inet6 no; do
819	if test $ipv6_cv_dir = no -o -d $ipv6_cv_dir; then
820	  break
821	fi
822      done])dnl
823    AC_MSG_RESULT($ipv6_cv_dir)
824    if test $ipv6_cv_dir = no; then
825      ac_cv_lib_inet6_$1=no
826    else
827      if test x$ipv6_libinet6 = x; then
828	ipv6_libinet6=no
829	SAVELDFLAGS="$LDFLAGS"
830	LDFLAGS="$LDFLAGS -L$ipv6_cv_dir/lib"
831      fi
832      AC_CHECK_LIB(inet6, $1, [dnl
833	if test $ipv6_libinet6 = no; then
834	  ipv6_libinet6=yes
835	  LIBS="$LIBS -linet6"
836	fi],)dnl
837      if test $ipv6_libinet6 = no; then
838	LDFLAGS="$SAVELDFLAGS"
839      fi
840    fi])dnl
841])dnl
842ipv6_cv_$1=no
843if test $ac_cv_func_$1 = yes -o $ac_cv_lib_socket_$1 = yes \
844     -o $ac_cv_lib_inet6_$1 = yes
845then
846  ipv6_cv_$1=yes
847fi
848if test $ipv6_cv_$1 = no; then
849  if test $1 = getaddrinfo; then
850    for ipv6_cv_pfx in o n; do
851      AC_EGREP_HEADER(${ipv6_cv_pfx}$1, netdb.h,
852		      [AC_CHECK_FUNC(${ipv6_cv_pfx}$1)])
853      if eval test X\$ac_cv_func_${ipv6_cv_pfx}$1 = Xyes; then
854        ipv6_cv_$1=yes
855        break
856      fi
857    done
858  fi
859fi
860if test $ipv6_cv_$1 = yes; then
861  AC_DEFINE_UNQUOTED(AS_TR_CPP(HAVE_$1))
862  ifelse([$2], , :, [$2])
863else
864  ifelse([$3], , :, [$3])
865fi])
866
867IPv6_CHECK_FUNC(getaddrinfo)
868IPv6_CHECK_FUNC(getnameinfo)
869
870# Check if <time.h> defines timeradd (and timersub)
871AC_MSG_CHECKING([if <time.h> defines timeradd])
872AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
873		#if HAVE_TIME_H
874		#include <time.h>
875		#endif
876		#ifdef HAVE_SYS_TIME_H
877		#include <sys/time.h>
878		#endif
879	],[
880		#ifndef timeradd
881		error timeradd undefined
882		#endif
883	])], [timeradd=yes],
884	[AC_DEFINE([HAVE_MISSING_TIMERADD],
885	    [], [<time.h> defines timeradd]) timeradd=no])
886AC_MSG_RESULT([$timeradd])
887
888# Check if -D_REENTRANT is needed for localtime_r, gmtime_r, strtok_r
889SAVEDCFLAGS=$CFLAGS
890CFLAGS=$CFLAGS$efl
891AC_MSG_CHECKING([if -D_REENTRANT is needed to use localtime_r and strtok_r])
892AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
893               #include <time.h>
894               #include <string.h>
895               #include <stdio.h>
896       ],[[
897
898               (void)localtime_r(NULL, NULL);
899	       (void)strtok_r(NULL, NULL, NULL);
900       ]])], [dreentrant=no],
901               [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
902                               #define _REENTRANT
903                               #include <time.h>
904                               #include <string.h>
905                               #include <stdio.h>
906                       ],[[
907                                (void)localtime_r(NULL, NULL);
908	       			(void)strtok_r(NULL, NULL, NULL);
909                       ]])],
910                       [SAVEDCFLAGS=$SAVEDCFLAGS" -D_REENTRANT";
911                        dreentrant=yes], [dreentrant=no])
912       ])
913AC_MSG_RESULT([$dreentrant])
914CFLAGS=$SAVEDCFLAGS
915
916# Check if -D__EXTENSIONS__ is needed for snprintf, getopt, seteuid...
917SAVEDCFLAGS=$CFLAGS
918CFLAGS=$CFLAGS$efl
919AC_MSG_CHECKING([if -D__EXTENSIONS__ is needed to use snprintf])
920AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
921		#include <stdio.h>
922	],[[
923		char dst[16 + 1];
924
925		snprintf(dst, 16, "test");
926	]])], [dextensions=no],
927		[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
928				#define __EXTENSIONS__
929				#include <stdio.h>
930			],[[
931				char dst[16 + 1];
932
933				snprintf(dst, 16, "test");
934			]])],
935			[SAVEDCFLAGS=$SAVEDCFLAGS" -D__EXTENSIONS__";
936			 dextensions=yes], [dextensions=no])
937	])
938AC_MSG_RESULT([$dextensions])
939CFLAGS=$SAVEDCFLAGS
940
941# Solaris 256 streams limitation workaround
942AC_ARG_ENABLE(stdio-hack,
943        [  --enable-stdio-hack   Use workaround for Solaris stdio limitation],
944        [CFLAGS=$CFLAGS" -DUSE_FD_POOL"])
945
946# Check for DRAC
947AC_ARG_ENABLE(drac,
948	[  --enable-drac	Enable DRAC support],
949	[if test x$enableval = xyes; then CFLAGS=$CFLAGS" -DUSE_DRAC"; fi])
950AC_ARG_ENABLE(p0f,
951	[  --enable-p0f		Enable p0f support, v2],
952	[if test x$enableval = xyes; then CFLAGS=$CFLAGS" -DUSE_P0F"; fi])
953AC_ARG_ENABLE(p0f3,
954	[  --enable-p0f3	Enable p0f support, v3.00 to v3.05],
955	[if test x$enableval = xyes; then
956	 CFLAGS=$CFLAGS" -DUSE_P0F -DHAVE_P0F3"; fi])
957AC_ARG_ENABLE(p0f306,
958	[  --enable-p0f306	Enable p0f support, v3.06 and up],
959	[if test x$enableval = xyes; then
960	 CFLAGS=$CFLAGS" -DUSE_P0F -DHAVE_P0F3 -DHAVE_P0F306"; fi])
961AC_ARG_WITH(p0f-src,
962	[  --with-p0f-src=path  p0f sources path ],
963	[CFLAGS=$CFLAGS" -I$withval -DP0F_QUERY_FROM_P0F_DIST"])
964
965# Check for SpamAssassin
966AC_ARG_ENABLE(spamassassin,
967	[  --enable-spamassassin	Enable SpamAssassin support],
968	[if test x$enableval = xyes; then CFLAGS=$CFLAGS" -DUSE_SPAMD"; fi])
969
970# Check for libmilter. For sendmail-8.12.1, -lsm is required too.
971# This uses a gross hack on the second AC_CHECK_LIB first argument, but using
972# [-lsm] in the optionnal 5th argument does not seems to help at all.
973AC_CHECK_LIB(milter, smfi_register,
974    [LIBS="$LIBS -lmilter"],
975    [AC_CHECK_LIB([milter -lsm], smfi_register,
976        [LIBS="$LIBS -lmilter -lsm"],
977        [AC_CHECK_LIB([milter -lsmutil], smfi_register,
978            [LIBS="$LIBS -lmilter -lsmutil"],
979                [echo "Required libmilter not found. Use --with-libmilter";
980                exit 1;])
981        ])
982])
983
984AC_CHECK_MEMBER([struct smfiDesc.xxfi_data],
985                [CFLAGS=$CFLAGS" -DHAVE_DATA_CALLBACK"],
986                [],
987                [#include <libmilter/mfapi.h>])
988
989# Check if -lpthreads is needed after -lmilter
990AC_MSG_CHECKING([if -lpthread is needed after -lmilter])
991SAVEDLIBS=$LIBS
992AC_LINK_IFELSE([AC_LANG_PROGRAM([
993		#include <pthread.h>
994		#include <libmilter/mfapi.h>
995	],[
996		pthread_self();
997		smfi_main();
998	])], [pthend=no], [LIBS=$LIBS" -lpthread";
999		AC_LINK_IFELSE([AC_LANG_PROGRAM([
1000			#include <pthread.h>
1001			#include <libmilter/mfapi.h>
1002		],[
1003			pthread_self();
1004			smfi_main();
1005		])], [pthend=yes; SAVEDLIBS=$LIBS], [pthend=no])
1006	])
1007
1008LIBS=$SAVEDLIBS
1009AC_MSG_RESULT([$pthend])
1010
1011# Solaris 8 has no <sys/queue.h> at all - headers checked above
1012# Some Linux flavors have an outdated <sys/queue.h>
1013AC_MSG_CHECKING([if <sys/queue.h> is outdated])
1014oldqueue=no
1015AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
1016		#include <sys/queue.h>
1017	],[
1018		#ifndef TAILQ_FIRST
1019		error Your <sys/queue.h> is outdated
1020		#endif
1021		#ifndef SIMPLEQ_REMOVE
1022		error Your <sys/queue.h> is outdated
1023		#endif
1024	])], [oldqueue=no],
1025	[AC_DEFINE([HAVE_OLD_QUEUE_H], [], [old <sys/queue.h>]) oldqueue=yes])
1026AC_MSG_RESULT([$oldqueue])
1027
1028# Darwin is unable to define two __RCSID in the same file
1029AC_MSG_CHECKING([if __RCSID can be used twice in the same source])
1030AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
1031		#include <sys/cdefs.h>
1032		#ifdef __RCSID
1033		__RCSID("Id1");
1034		__RCSID("Id2");
1035		#endif
1036	],[
1037		/* nothing */
1038	])], [rcsidtwice=yes],
1039	[AC_DEFINE([HAVE_BROKEN_RCSID], [],
1040	    [__RCSID cannot be used twice]) rcsidtwice=no])
1041AC_MSG_RESULT([$rcsidtwice])
1042
1043# Digital UNIX's native compiler require -pthread when using libpthread
1044# It also require -D_XOPEN_SOURCE=500 for various reasons I have not
1045# been abe to isolate. Let's define both there.
1046AC_MSG_CHECKING([whether <pthreads.h> wants -pthread])
1047SAVEDCFLAGS=$CFLAGS
1048AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>],[])],
1049	[pthreadfl=no],
1050	[CFLAGS=$CFLAGS" -pthread -D_XOPEN_SOURCE=500";
1051	 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>],[])],
1052		[pthreadfl=yes; SAVEDCFLAGS=$CFLAGS], [pthreadfl=no])
1053	])
1054CFLAGS=$SAVEDCFLAGS
1055AC_MSG_RESULT([$pthreadfl])
1056
1057# Linux requires -D_XOPEN_SOURCE=500 to build with libpthread and -Wall
1058AC_MSG_CHECKING([whether you need -D_XOPEN_SOURCE to use libpthread])
1059xopen=no
1060AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
1061		#include <sys/types.h>
1062		#include <pthread.h>
1063	],[
1064		pthread_rwlock_t lock;
1065	])], [xopen=no], [CFLAGS="$CFLAGS -D_XOPEN_SOURCE=500"; xopen=yes])
1066AC_MSG_RESULT([$xopen])
1067
1068# Tru64 Unix needs -D_OSF_SOURCE for IPV6 support
1069SAVEDCFLAGS=$CFLAGS
1070CFLAGS=$CFLAGS" -D_XOPEN_SOURCE=500 -D_BSD_SOURCE"
1071AC_MSG_CHECKING([if -D_OSF_SOURCE is needed for IPV6 support])
1072AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
1073		#include <sys/types.h>
1074		#include <sys/socket.h>
1075		#include <netinet/in.h>
1076	],[[
1077		#ifdef AF_INET6
1078		  struct in6_addr s;
1079		#endif
1080	]])], [dosfsource=no],
1081		[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
1082				#define _OSF_SOURCE
1083				#include <sys/socket.h>
1084				#include <netinet/in.h>
1085			],[[
1086				#ifdef AF_INET6
1087				  struct in6_addr s;
1088				#endif
1089			]])],
1090			[SAVEDCFLAGS=$SAVEDCFLAGS" -D_OSF_SOURCE";
1091			 dosfsource=yes], [dosfsource=no])
1092	])
1093AC_MSG_RESULT([$dosfsource])
1094CFLAGS=$SAVEDCFLAGS
1095
1096# Linux does not have a sa_len field in sockaddr
1097AC_CHECK_MEMBER([struct sockaddr.sa_len],
1098	[AC_DEFINE([HAVE_SA_LEN], [], [sa_len field in struct sockaddr])],, [
1099		#include <sys/types.h>
1100		#include <sys/socket.h>
1101	])
1102
1103# Pre-RFC2553 system does not have a sin6_scope_id field in sockaddr_in6
1104AC_CHECK_MEMBER([struct sockaddr_in6.sin6_scope_id],
1105	[AC_DEFINE([HAVE_SIN6_SCOPE_ID], [1],
1106	    [sin6_scope_id field in struct sockaddr_in6])],, [
1107		#include <sys/types.h>
1108		#include <sys/socket.h>
1109		#include <netinet/in.h>
1110	])
1111
1112# Darwin seems to have no definition for socklen_t beyond _BSD_SOCKLEN_T_
1113AC_MSG_CHECKING([if socklen_t is missing])
1114AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
1115		#include <sys/types.h>
1116		#include <sys/socket.h>
1117	],[
1118		socklen_t slen;
1119	])], [socklen=no],
1120	[AC_DEFINE([HAVE_MISSING_SOCKLEN_T], [],
1121	    [missing socklen_t definition]) socklen=yes])
1122AC_MSG_RESULT([$socklen])
1123
1124# GNU pth-2.0.0 has a bug with rwlock.
1125AC_MSG_CHECKING([for broken rwlock in libpthread])
1126brokenpth=yes
1127AC_RUN_IFELSE([AC_LANG_PROGRAM([
1128		#if STDC_HEADERS
1129		# include <stdlib.h>
1130		#else
1131		# ifdef __cplusplus
1132		extern "C" void exit (int);
1133		# else
1134		void exit (int);
1135		# endif
1136		#endif
1137
1138		#if HAVE_SYS_TIME_H
1139		# include <sys/time.h>
1140		#else
1141		struct timeval {
1142			long tv_sec;
1143			long tv_usec;
1144		};
1145		#endif
1146
1147		#if HAVE_SYS_TYPES_H
1148		# include <sys/types.h>
1149		#else
1150		#endif
1151
1152		#if HAVE_UNISTD_H
1153		# include <unistd.h>
1154		#else
1155		# ifdef __cplusplus
1156		extern "C" unsigned int exit (unsigned int);
1157		extern "C" int
1158		    select(int, void *, void *, void *, struct timeval *);
1159		# else
1160		unsigned int exit (unsigned int);
1161		int select(int, void *, void *, void *, struct timeval *);
1162		# endif
1163		#endif
1164
1165		#include <pthread.h>
1166		pthread_rwlock_t lock;
1167
1168		#ifdef __cplusplus
1169		extern "C" void testfunc (void *);
1170		extern "C" void yield(void);
1171		#else
1172		void testfunc();
1173		void yeild(void);
1174		#endif
1175
1176		/*
1177		 * On some Linux flavors, using sleep(3) instead
1178		 * of select causes an infinite hang.
1179		 */
1180		void yield (void)
1181		{
1182			struct timeval tv;
1183
1184			tv.tv_sec = 1;
1185			tv.tv_usec = 0;
1186			if (select(0, NULL, NULL, NULL, &tv) != 0)
1187				exit(EXIT_FAILURE);
1188			return;
1189		}
1190
1191		#ifdef __cplusplus
1192		void testfunc (void *dontcare)
1193		#else
1194		void testfunc (dontcare) void *dontcare;
1195		#endif
1196		{
1197			if (pthread_rwlock_rdlock(&lock) != 0)
1198				exit(EXIT_FAILURE);
1199			yield();
1200			if (pthread_rwlock_unlock(&lock) != 0)
1201				exit(EXIT_FAILURE);
1202			return;
1203		}
1204	],[
1205		pthread_t tid;
1206
1207		if (pthread_rwlock_init(&lock, NULL) != 0)
1208			exit(EXIT_FAILURE);
1209		if (pthread_create(&tid, NULL, (void *)testfunc, NULL) != 0)
1210			exit(EXIT_FAILURE);
1211		if (pthread_rwlock_rdlock(&lock) != 0)
1212			exit(EXIT_FAILURE);
1213		yield();
1214		if (pthread_rwlock_unlock(&lock) != 0)
1215			exit(EXIT_FAILURE);
1216		if (pthread_join(tid, NULL) != 0)
1217			exit(EXIT_FAILURE);
1218		return 0;
1219	])], [brokenpth=no],
1220	AC_DEFINE([HAVE_BROKEN_RWLOCK], [], [broken rwlock in libpthread]))
1221AC_MSG_RESULT([$brokenpth])
1222
1223# Check is DNS resolver is re-entrant
1224AC_ARG_WITH(thread-safe-resolver,
1225	[  --with-thread-safe-resolver   Resolver is thread-safe],
1226	[rdns=yes],[
1227		SAVEDCFLAGS=$CFLAGS
1228		CFLAGS=$CFLAGS$efl
1229		AC_MSG_CHECKING([if DNS resolver is re-entrant])
1230		AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
1231				#define _BSD_SOURCE
1232
1233				#include <sys/types.h>
1234				#include <netinet/in.h>
1235				#include <arpa/inet.h>
1236				#include <arpa/nameser.h>
1237				#include <resolv.h>
1238			],[
1239				struct __res_state res;
1240
1241				res_ninit(&res);
1242			])], [rdns=yes], [rdns=no])
1243		AC_MSG_RESULT([$rdns])
1244		CFLAGS=$SAVEDCFLAGS
1245	])
1246
1247# Check for DNSRBL
1248AC_ARG_ENABLE(dnsrbl,
1249	[  --enable-dnsrbl	Enable DNSRBL support],
1250	[if test x$rdns = xyes; then
1251		SAVEDCFLAGS=$CFLAGS" -DUSE_DNSRBL"
1252		# For Debian
1253		CFLAGS=
1254		AC_TRY_LINK([#include <arpa/inet.h>
1255		             #include <arpa/nameser.h>
1256			], [ int result = ns_initparse(NULL, 0, NULL); ],
1257			[],[ AC_CHECK_LIB([resolv], [ns_initparse],
1258			    [LIBS="$LIBS -lresolv"], [
1259				if test -r ${libdir}/libresolv.a; then
1260					LIBS="$LIBS ${libdir}/libresolv.a"
1261				else
1262					AC_MSG_WARN([ns_initparse not found])
1263				fi
1264			    ])
1265		])
1266		CFLAGS=$SAVEDCFLAGS
1267	 else
1268		echo "--enable-dnsrbl used but DNS resolver is not thread safe";
1269		echo "Try installing BIND9 and using --with-libbind, or if you";
1270		echo "know what you are doing, use --with-thread-safe-resolver";
1271		exit 1;
1272	 fi
1273	])
1274
1275# Check for MX
1276AC_ARG_ENABLE(mx,
1277        [  --enable-mx      Enable MX support],
1278        [if test x$rdns = xyes; then
1279                SAVEDCFLAGS=$CFLAGS" -DUSE_MX"
1280                # For Debian
1281                CFLAGS=
1282                AC_TRY_LINK([#include <stdio.h>
1283                             #include <arpa/nameser.h>
1284                        ], [ int result = ns_initparse(NULL, 0, NULL); ],
1285                        [],[ AC_CHECK_LIB([resolv], [ns_initparse],
1286                            [LIBS="$LIBS -lresolv"], [
1287                                if test -r ${libdir}/libresolv.a; then
1288                                        LIBS="$LIBS ${libdir}/libresolv.a"
1289                                else
1290                                        AC_MSG_WARN([ns_initparse not found])
1291                                fi
1292                            ])
1293                ])
1294                CFLAGS=$SAVEDCFLAGS
1295         else
1296                echo "--enable-mx defined, but DNS resolver is not thread safe";
1297                echo "Try installing BIND9 and using --with-libbind, or if you";
1298                echo "know what you are doing, use --with-thread-safe-resolver";
1299                exit 1;
1300         fi
1301        ])
1302
1303# We enable some features for Postfix
1304AC_ARG_ENABLE(postfix,
1305	[  --enable-postfix	Enable Postfix support],
1306	[if test x$enableval = xyes; then
1307		CFLAGS=$CFLAGS" -DUSE_POSTFIX"
1308	 fi
1309	])
1310
1311# Record the build environnement for milter-greylist -r
1312AC_DEFINE_UNQUOTED([BUILD_ENV], ["`logname`@`uname -n`:`pwd` `date`"],
1313    [Build environnement])
1314
1315# Options
1316AC_ARG_WITH(user,
1317	[  --with-user=USERNAME   The user that will run milter-greylist ],
1318	[AC_SUBST(USER, $withval)], [AC_SUBST(USER, root)])
1319
1320CONFFILE="\${SYSCONFDIR}/mail/greylist.conf"
1321AC_ARG_WITH(conffile,
1322	[  --with-conffile=PATH Location of config file],
1323	[CONFFILE="$withval"])
1324AC_SUBST(CONFFILE)
1325CFLAGS=$CFLAGS" -DCONFFILE=\\\"\${CONFFILE}\\\""
1326
1327DUMPFILE="\${LOCALSTATEDIR}/milter-greylist/greylist.db"
1328AC_ARG_WITH(dumpfile,
1329	[  --with-dumpfile=PATH   Location of dump file],
1330	[DUMPFILE="$withval"])
1331AC_SUBST(DUMPFILE)
1332CFLAGS=$CFLAGS" -DDUMPFILE=\\\"\${DUMPFILE}\\\""
1333
1334AC_ARG_WITH(delay,
1335	[  --with-delay=VALUE     Delay before accepting mail],
1336	[CFLAGS=$CFLAGS" -DGLDELAY=$withval"])
1337AC_ARG_WITH(timeout,
1338	[  --with-timeout=VALUE   Timeout before we forget about a send],
1339	[CFLAGS=$CFLAGS" -DTIMEOUT=$withval"])
1340AC_ARG_WITH(autowhite,
1341	[  --with-autowhite=VALUE How long auto-whitelisting lasts],
1342	[CFLAGS=$CFLAGS" -DAUTOWHITE_DELAY=$withval"])
1343AC_ARG_WITH([drac-db],
1344	[  --with-drac-db=PATH   Where is the DRAC database ],
1345	[CFLAGS=$CFLAGS" -DDRACDB=\\\"$withval\\\""])
1346
1347AC_CONFIG_FILES([Makefile milter-greylist.spec])
1348AC_OUTPUT
1349