]> git.armaanb.net Git - opendoas.git/blob - configure
Change the way insults are enabled
[opendoas.git] / configure
1 #!/bin/sh
2
3 die() {
4         printf "$1\n" >&2
5         exit 1
6 }
7
8 usage() {
9         cat <<EOF
10 usage: configure [options]
11
12   --prefix=PREFIX        installation prefix [/usr]
13   --exec-prefix=EPREFIX  installation prefix for executable files [PREFIX]
14   --bindir=DIR           user executables [PREFIX/bin]
15   --datadir=DIR          architecture-independent data files [PREFIX/share]
16   --mandir=DIR           manual pages [DATADIR/man]
17   --sysconfdir=DIR       directory for configuration files [/etc]
18
19   --build=build-alias    a cpu-vendor-opsys for the system where the application will be built
20   --host=host-alias      a cpu-vendor-opsys for the system where the application will run
21   --target=target-alias  the machine that CC will produce code for
22
23   --enable-debug         enable debugging
24   --enable-static        prepare for static build
25
26   --without-pam          disable pam support
27   --without-shadow       disable shadow support
28
29   --with-timestamp       enable timestamp support
30
31   --without-insults      disable insults
32
33   --uid-max=NUM          set UID_MAX (default 65535)
34   --gid-max=NUM          set GID_MAX (default 65535)
35
36   --help, -h             display this help and exit
37 EOF
38         exit 0
39 }
40
41 # defaults
42 WITHOUT_TIMESTAMP=yes
43 UID_MAX=65535
44 GID_MAX=65535
45
46 for x; do
47         opt=${x%%=*}
48         var=${x#*=}
49         case "$opt" in
50         --prefix) PREFIX=$var ;;
51         --exec-prefix) EPREFIX=$var ;;
52         --bindir) BINDIR=$var ;;
53         --datadir) SHAREDIR=$var ;;
54         --mandir) MANDIR=$var ;;
55         --sysconfdir) SYSCONFDIR=$var ;;
56         --build) BUILD=$var ;;
57         --host) HOST=$var ;;
58         --target) TARGET=$var ;;
59         --enable-debug) DEBUG=yes ;;
60         --enable-static) BUILD_STATIC=yes ;;
61         --with-pam) WITHOUT_PAM=; WITHOUT_SHADOW=yes ;;
62         --with-shadow) WITHOUT_SHADOW=; WITHOUT_PAM=yes ;;
63         --without-pam) WITHOUT_PAM=yes ;;
64         --without-shadow) WITHOUT_SHADOW=yes ;;
65         --without-insults) WITHOUT_INSULTS=yes ;;
66         --with-timestamp) WITHOUT_TIMESTAMP= ;;
67         --without-timestamp) WITHOUT_TIMESTAMP=yes ;;
68         --uid-max) UID_MAX=$var ;;
69         --gid-max) UID_MAX=$var ;;
70         --help|-h) usage ;;
71         *) die "Error: unknown option $opt" ;;
72         esac
73 done
74
75 CONFIG_MK=config.mk
76 CONFIG_H=config.h
77 rm -f "$CONFIG_MK" "$CONFIG_H"
78
79 cat <<! >$CONFIG_H
80 #ifndef CONFIG_H
81 #define CONFIG_H
82
83 !
84
85 if [ -z "$BUILD" ]; then
86         BUILD="$(uname -m)-unknown-$(uname -s | tr '[:upper:]' '[:lower:]')"
87 fi
88 if [ -z "$HOST" ]; then
89         [ -z "$TARGET" ] && TARGET=$BUILD
90         HOST=$TARGET
91 fi
92 if [ -z "$TARGET" ]; then
93         [ -z "$HOST" ] && HOST=$BUILD
94         TARGET=$HOST
95 fi
96
97 if [ -z "$OS" ]; then
98         # Derive OS from cpu-manufacturer-os-kernel
99         CPU=${TARGET%%-*}
100         REST=${TARGET#*-}
101         MANU=${REST%%-*}
102         REST=${REST#*-}
103         OS=${REST%%-*}
104         REST=${REST#*-}
105         KERNEL=${REST%%-*}
106 fi
107
108 OS_CFLAGS="-D__${OS}__"
109
110 case "$OS" in
111         linux)
112                 printf 'Setting UID_MAX\t\t\t\t%d.\n' "$UID_MAX" >&2
113                 printf '#define UID_MAX %s\n' "$UID_MAX" >>$CONFIG_H
114                 printf 'Setting GID_MAX\t\t\t\t%d.\n' "$GID_MAX" >&2
115                 printf '#define GID_MAX %s\n' "$GID_MAX" >>$CONFIG_H
116                 OS_CFLAGS="$OS_CFLAGS -D_DEFAULT_SOURCE -D_GNU_SOURCE"
117                 ;;
118         netbsd)
119                 OS_CFLAGS="$OS_CFLAGS -D_OPENBSD_SOURCE"
120                 printf 'LDLIBS +=       -lutil\n' >>$CONFIG_MK
121                 : ${BINGRP:=wheel}
122                 ;;
123         freebsd)
124                 printf 'LDLIBS +=       -lutil\n' >>$CONFIG_MK
125                 : ${BINGRP:=wheel}
126                 ;;
127         darwin)
128                 : ${BINGRP:=wheel}
129                 ;;
130 esac
131
132 : ${PREFIX:=/usr/local}
133 : ${EPREFIX:=${PREFIX}}
134 : ${BINDIR:=${PREFIX}/bin}
135 : ${SHAREDIR:=${PREFIX}/share}
136 : ${MANDIR:=${SHAREDIR}/man}
137 : ${SYSCONFDIR:=/etc}
138 : ${BINMODE:=4755}
139 : ${BINOWN:=root}
140 : ${BINGRP:=root}
141
142 cat <<EOF >>$CONFIG_MK
143 PREFIX   ?=     ${PREFIX}
144 EPREFIX  ?=     ${EPREFIX}
145 BINDIR   ?=     ${BINDIR}
146 SHAREDIR ?=     ${SHAREDIR}
147 MANDIR   ?=     ${MANDIR}
148 SYSCONFDIR?=    ${SYSCONFDIR}
149 BINMODE  ?=     ${BINMODE}
150 BINOWN  ?=      ${BINOWN}
151 BINGRP  ?=      ${BINGRP}
152 EOF
153
154 [ -n "$OS_CFLAGS" ] && \
155         printf 'OS_CFLAGS   +=  %s\n' "$OS_CFLAGS" >>$CONFIG_MK
156
157 [ -n "$DEBUG" ] && \
158         printf 'CFLAGS   +=     -O0 -g\n' >>$CONFIG_MK
159
160 [ -n "$BUILD_STATIC" ] && \
161         printf 'CFLAGS   +=     -static\n' >>$CONFIG_MK
162
163 # Add CPPFLAGS/CFLAGS/LDFLAGS/LDLIBS to CC for testing features
164 XCC="${CC:=cc} $CFLAGS $OS_CFLAGS $CPPFLAGS $LDFLAGS $LDLIBS"
165 # Make sure to disable --as-needed for CC tests.
166
167 case "$OS" in
168         darwin) ;;
169         *) XCC="$XCC -Wl,--no-as-needed" ;;
170 esac
171
172 check_func() {
173         func="$1"; src="$2"; shift 2
174         printf 'Checking for %-14s\t\t' "$func ..." >&2
175         printf '%s\n' "$src" >"_$func.c"
176         $XCC "_$func.c" -o "_$func" 2>/dev/null
177         ret=$?
178         rm -f "_$func.c" "_$func"
179         upperfunc="$(printf '%s\n' "$func" | tr '[[:lower:]]' '[[:upper:]]')"
180         if [ $ret -eq 0 ]; then
181                 printf 'yes.\n' >&2
182                 printf '#define HAVE_%s\n' "$upperfunc" >>$CONFIG_H
183                 return 0
184         else
185                 printf '/* #define HAVE_%s */\n' "$upperfunc" >>$CONFIG_H
186                 printf 'no.\n' >&2
187                 return 1
188         fi
189 }
190
191 authmethod() {
192         #
193         # Check for pam_appl.h.
194         #
195         src='
196 #include <security/pam_appl.h>
197 int main(void) {
198         return 0;
199 }'
200         [ -z "$WITHOUT_PAM" ] && check_func "pam_appl_h" "$src" && {
201                 printf 'SRCS     +=     pam.c\n' >>$CONFIG_MK
202                 printf 'LDLIBS +=       -lpam\n' >>$CONFIG_MK
203                 printf '#define USE_PAM\n' >>$CONFIG_H
204                 printf 'pam\n'
205                 return 0
206         }
207
208         #
209         # Check for shadow.h.
210         #
211         src='
212 #include <shadow.h>
213 int main(void) {
214         return 0;
215 }'
216         [ -z "$WITHOUT_SHADOW" ] && check_func "shadow_h" "$src" && {
217                 printf 'SRCS     +=     shadow.c\n' >>$CONFIG_MK
218                 printf 'LDLIBS +=       -lcrypt\n' >>$CONFIG_MK
219                 printf '#define USE_SHADOW\n' >>$CONFIG_H
220                 printf 'shadow\n'
221                 return 0
222         }
223
224         return 1
225 }
226
227 definsults() {
228         printf 'SRCS    += insults.c\n' >>$CONFIG_MK
229         [ -z "$WITHOUT_INSULTS" ] && {
230                 printf '#define DOAS_INSULTS\n' >>$CONFIG_H
231         }
232         return 0
233 }
234
235
236 persistmethod() {
237         [ -z "$WITHOUT_TIMESTAMP" ] && {
238                 printf '#define USE_TIMESTAMP\n' >>$CONFIG_H
239                 printf 'SRCS    += timestamp.c\n' >>$CONFIG_MK
240                 printf 'timestamp\n'
241                 return 0
242         }
243         return 1
244 }
245
246 #
247 # Check for explicit_bzero().
248 #
249 src='
250 #include <string.h>
251 int main(void) {
252         explicit_bzero(NULL, 0);
253         return 0;
254 }'
255 check_func "explicit_bzero" "$src" || {
256         printf 'SRCS += libopenbsd/explicit_bzero.c\n' >>$CONFIG_MK
257 }
258
259 #
260 # Check for strlcat().
261 #
262 src='
263 #include <string.h>
264 int main(void) {
265         const char s1[] = "foo";
266         char s2[10];
267         strlcat(s2, s1, sizeof(s2));
268         return 0;
269 }'
270 check_func "strlcat" "$src" || {
271         printf 'SRCS += libopenbsd/strlcat.c\n' >>$CONFIG_MK
272 }
273
274 #
275 # Check for strlcpy().
276 #
277 src='
278 #include <string.h>
279 int main(void) {
280         const char s1[] = "foo";
281         char s2[10];
282         strlcpy(s2, s1, sizeof(s2));
283         return 0;
284 }'
285 check_func "strlcpy" "$src" || {
286         printf 'SRCS += libopenbsd/strlcpy.c\n' >>$CONFIG_MK
287 }
288
289 #
290 # Check for errc().
291 #
292 src='
293 #include <err.h>
294 int main(void) {
295         errc(0, 0, "");
296         return 0;
297 }'
298 check_func "errc" "$src" || {
299         printf 'SRCS += libopenbsd/errc.c\n' >>$CONFIG_MK
300 }
301
302 #
303 # Check for verrc().
304 #
305 src='
306 #include <stddef.h>
307 #include <err.h>
308 int main(void) {
309         verrc(0, 0, "x", NULL);
310         return 0;
311 }'
312 check_func "verrc" "$src" || {
313         printf 'SRCS += libopenbsd/verrc.c\n' >>$CONFIG_MK
314 }
315
316 #
317 # Check for setprogname().
318 #
319 src='
320 #include <stdlib.h>
321 int main(void) {
322         setprogname("");
323         return 0;
324 }'
325 check_func "setprogname" "$src" || {
326         printf 'SRCS += libopenbsd/progname.c\n' >>$CONFIG_MK
327 }
328
329 #
330 # Check for readpassphrase().
331 #
332 src='
333 #include <readpassphrase.h>
334 int main(void) {
335         char buf[12];
336         readpassphrase("", buf, sizeof(buf), 0);
337         return 0;
338 }'
339 check_func "readpassphrase" "$src" || {
340         printf 'SRCS += libopenbsd/readpassphrase.c\n' >>$CONFIG_MK
341 }
342
343 #
344 # Check for strtonum().
345 #
346 src='
347 #include <stdlib.h>
348 int main(void) {
349         const char *errstr;
350         strtonum("", 1, 64, &errstr);
351         return 0;
352 }'
353 check_func "strtonum" "$src" || {
354         printf 'SRCS += libopenbsd/strtonum.c\n' >>$CONFIG_MK
355 }
356
357 #
358 # Check for reallocarray().
359 #
360 src='
361 #include <stdlib.h>
362 int main(void) {
363         reallocarray(NULL, 0, 0);
364         return 0;
365 }'
366 check_func "reallocarray" "$src" || {
367         printf 'SRCS += libopenbsd/reallocarray.c\n' >>$CONFIG_MK
368 }
369
370 #
371 # Check for execvpe().
372 #
373 src='
374 #include <unistd.h>
375 int main(void) {
376         const char *p = { "", NULL };
377         execvpe("", p, p);
378         return 0;
379 }'
380 check_func "execvpe" "$src" || {
381         printf 'SRCS += libopenbsd/execvpe.c\n' >>$CONFIG_MK
382 }
383
384 #
385 # Check for setresuid().
386 #
387 src='
388 #include <unistd.h>
389 int main(void) {
390         setresuid(0, 0, 0);
391         return 0;
392 }'
393 check_func "setresuid" "$src"
394 have_setresuid=$?
395
396 #
397 # Check for setresgid().
398 #
399 src='
400 #include <unistd.h>
401 int main(void) {
402         setresgid(0, 0, 0);
403         return 0;
404 }'
405 check_func "setresgid" "$src"
406 have_setresgid=$?
407
408 if [ $have_setresuid -eq 1 -o $have_setresgid -eq 1 ]; then
409         printf 'SRCS += libopenbsd/bsd-setres_id.c\n' >>$CONFIG_MK
410 fi
411
412 #
413 # Check for setreuid().
414 #
415 src='
416 #include <unistd.h>
417 int main(void) {
418         setreuid(0, 0);
419         return 0;
420 }'
421 check_func "setreuid" "$src"
422
423
424 #
425 # Check for setregid().
426 #
427 src='
428 #include <unistd.h>
429 int main(void) {
430         setregid(0, 0);
431         return 0;
432 }'
433 check_func "setregid" "$src"
434
435 #
436 # Check for closefrom().
437 #
438 src='
439 #include <unistd.h>
440 int main(void) {
441         closefrom(0);
442         return 0;
443 }'
444 check_func "closefrom" "$src" || {
445         printf 'SRCS += libopenbsd/closefrom.c\n' >>$CONFIG_MK
446 }
447
448 #
449 # Check for sysconf().
450 #
451 src='
452 #include <unistd.h>
453 int main(void) {
454         (void)sysconf(0);
455         return 0;
456 }'
457 check_func "sysconf" "$src"
458
459 #
460 # Check for dirfd().
461 #
462 src='
463 #include <dirent.h>
464 int main(void) {
465         (void)dirfd(0);
466         return 0;
467 }'
468 check_func "dirfd" "$src"
469
470 #
471 # Check for fcntl.h.
472 #
473 src='
474 #include <fcntl.h>
475 int main(void) {
476         return 0;
477 }'
478 check_func "fcntl_h" "$src"
479
480 #
481 # Check for F_CLOSEM.
482 #
483 src='
484 #include <fcntl.h>
485 #ifndef F_CLOSEM
486 #error no F_CLOSEM
487 #endif
488 int main(void) {
489         return 0;
490 }'
491 check_func "F_CLOSEM" "$src"
492
493 #
494 # Check for dirent.h.
495 #
496 src='
497 #include <dirent.h>
498 int main(void) {
499         return 0;
500 }'
501 check_func "dirent_h" "$src"
502
503 #
504 # Check for sys/ndir.h.
505 #
506 src='
507 #include <sys/ndir.h>
508 int main(void) {
509         return 0;
510 }'
511 check_func "sys_ndir_h" "$src"
512
513 #
514 # Check for sys/dir.h.
515 #
516 src='
517 #include <sys/dir.h>
518 int main(void) {
519         return 0;
520 }'
521 check_func "sys_dir_h" "$src"
522
523 #
524 # Check for ndir.h.
525 #
526 src='
527 #include <ndir.h>
528 int main(void) {
529         return 0;
530 }'
531 check_func "ndir_h" "$src"
532
533 #
534 # Check for login_cap.h.
535 #
536 src='
537 #include <sys/types.h>
538 #include <login_cap.h>
539 int main(void) {
540         return 0;
541 }'
542 check_func "login_cap_h" "$src"
543
544 #
545 #
546 #
547 src='
548 #include <stdlib.h>
549 int main(void){return 0;}
550 __attribute__((__unused__)) static void foo(void){return;}
551 '
552 check_func "__attribute__" "$src" || {
553         printf 'OS_CFLAGS       +=      -DNO_ATTRIBUTE_ON_RETURN_TYPE=1\n' >>$CONFIG_MK
554 }
555
556 auth=$(authmethod)
557 if [ $? -eq 0 ]; then
558         printf 'Using auth method\t\t\t%s.\n' "$auth" >&2
559 else
560         printf 'Error auth method\t\t\n' >&2
561         exit 1
562 fi
563
564 insults=$(definsults)
565
566 persist=$(persistmethod)
567 if [ $? -eq 0 ]; then
568         printf 'Using persist method\t\t\t%s.\n' "$persist" >&2
569 else
570         printf 'Using persist method\t\t\tnone.\n' >&2
571 fi
572
573 printf '#define DOAS_CONF "%s/doas.conf"\n' "${SYSCONFDIR}" >>$CONFIG_H
574
575 printf '\n#endif /* CONFIG_H */\n' >>$CONFIG_H