opendoas

A portable version of the OpenBSD `doas` command
git clone https://pi.duncano.de/git/opendoas.git

configure (8705B)


      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   --pamdir=DIR           PAM directory [SYSCONFDIR/pam.d]
     19 
     20   --build=build-alias    a cpu-vendor-opsys for the system where the application will be built
     21   --host=host-alias      a cpu-vendor-opsys for the system where the application will run
     22   --target=target-alias  the machine that CC will produce code for
     23 
     24   --enable-debug         enable debugging
     25   --enable-static        prepare for static build
     26 
     27   --without-pam          disable pam support
     28   --without-pam          disable shadow support
     29 
     30   --help, -h             display this help and exit
     31 EOF
     32 	exit 0
     33 }
     34 
     35 for x; do
     36 	opt=${x%%=*}
     37 	var=${x#*=}
     38 	case "$opt" in
     39 	--prefix) PREFIX=$var ;;
     40 	--exec-prefix) EPREFIX=$var ;;
     41 	--bindir) BINDIR=$var ;;
     42 	--datadir) SHAREDIR=$var ;;
     43 	--mandir) MANDIR=$var ;;
     44 	--sysconfdir) SYSCONFDIR=$var ;;
     45 	--pamdir) PAMDIR=$var ;;
     46 	--build) BUILD=$var ;;
     47 	--host) HOST=$var ;;
     48 	--target) TARGET=$var ;;
     49 	--enable-debug) DEBUG=yes ;;
     50 	--enable-static) BUILD_STATIC=yes ;;
     51 	--with-pam) WITHOUT_PAM=; WITHOUT_SHADOW=yes ;;
     52 	--with-shadow) WITHOUT_SHADOW=; WITHOUT_PAM=yes ;;
     53 	--without-pam) WITHOUT_PAM=yes ;;
     54 	--without-shadow) WITHOUT_SHADOW=yes ;;
     55 	--help|-h) usage ;;
     56 	*) die "Error: unknown option $opt" ;;
     57 	esac
     58 done
     59 
     60 CONFIG_MK=config.mk
     61 rm -f "$CONFIG_MK"
     62 
     63 # : ${VERSION:="$(git describe --dirty --tags --long --always)"}
     64 : ${VERSION:="6.0"}
     65 
     66 cat <<EOF >>$CONFIG_MK
     67 PREFIX   ?=	${PREFIX:="/usr"}
     68 EPREFIX  ?=	${EPREFIX:="${PREFIX}"}
     69 BINDIR   ?=	${BINDIR:="${PREFIX}/bin"}
     70 SHAREDIR ?=	${SHAREDIR:="${PREFIX}/share"}
     71 MANDIR   ?=	${MANDIR:="${SHAREDIR}/man"}
     72 SYSCONFDIR?=	${SYSCONFDIR:="/etc"}
     73 PAMDIR   ?=	${PAMDIR:="${SYSCONFDIR}/pam.d"}
     74 CFLAGS   +=	-DVERSION="\"${VERSION}\""
     75 EOF
     76 
     77 if [ -z "$BUILD" ]; then
     78 	BUILD="$(uname -m)-unknown-$(uname -s | tr '[:upper:]' '[:lower:]')"
     79 fi
     80 if [ -z "$HOST" ]; then
     81 	[ -z "$TARGET" ] && TARGET=$BUILD
     82 	HOST=$TARGET
     83 fi
     84 if [ -z "$TARGET" ]; then
     85 	[ -z "$HOST" ] && HOST=$BUILD
     86 	TARGET=$HOST
     87 fi
     88 
     89 if [ -z "$OS" ]; then
     90 	# Derive OS from cpu-manufacturer-os-kernel
     91 	CPU=${TARGET%%-*}
     92 	REST=${TARGET#*-}
     93 	MANU=${REST%%-*}
     94 	REST=${REST#*-}
     95 	OS=${REST%%-*}
     96 	REST=${REST#*-}
     97 	KERNEL=${REST%%-*}
     98 fi
     99 
    100 OS_CFLAGS="-D__${OS}__"
    101 
    102 case "$OS" in
    103 	linux)
    104 		OS_CFLAGS="$OS_CFLAGS -D_DEFAULT_SOURCE -D_GNU_SOURCE -DUID_MAX=60000 -DGID_MAX=60000"
    105 		printf 'CURDIR   :=	.\n' >>$CONFIG_MK
    106 		[ -z "$WITHOUT_PAM" ] && \
    107 			printf 'PAM_DOAS  =	pam.d__doas__linux\n' >>$CONFIG_MK
    108 		;;
    109 esac
    110 
    111 [ -n "$OS_CFLAGS" ] && \
    112 	printf 'CFLAGS   +=	%s\n' "$OS_CFLAGS" >>$CONFIG_MK
    113 
    114 [ -n "$DEBUG" ] && \
    115 	printf 'CFLAGS   +=	-O0 -g\n' >>$CONFIG_MK
    116 
    117 [ -n "$BUILD_STATIC" ] && \
    118 	printf 'CFLAGS   +=	-static\n' >>$CONFIG_MK
    119 
    120 # Add CPPFLAGS/CFLAGS/LDFLAGS to CC for testing features
    121 XCC="${CC:=cc} $CFLAGS $OS_CFLAGS $CPPFLAGS $LDFLAGS"
    122 # Make sure to disable --as-needed for CC tests.
    123 XCC="$XCC -Wl,--no-as-needed"
    124 
    125 check_func() {
    126 	func="$1"; src="$2"; shift 2
    127 	printf 'Checking for %-14s\t\t' "$func ..." >&2
    128 	printf '%s\n' "$src" >"_$func.c"
    129 	$XCC "_$func.c" -o "_$func" 2>/dev/null
    130 	ret=$?
    131 	rm -f "_$func.c" "_$func"
    132 	if [ $ret -eq 0 ]; then
    133 		printf 'yes.\n' >&2
    134 		upperfunc="$(printf '%s\n' "$func" | tr '[[:lower:]]' '[[:upper:]]')"
    135 		printf 'CFLAGS   +=	-DHAVE_%s\n' "$upperfunc" >>$CONFIG_MK
    136 		return 0
    137 	else
    138 		printf 'no.\n' >&2
    139 		return 1
    140 	fi
    141 }
    142 
    143 authmethod() {
    144 	#
    145 	# Check for bsd_auth.h.
    146 	#
    147 	src='
    148 #include <bsd_auth.h>
    149 int main(void) {
    150 	return 0;
    151 }'
    152 	check_func "bsd_auth_h" "$src" && {
    153 		have_bsd_auth_h=1
    154 		printf 'bsd\n'
    155 		return 0
    156 	}
    157 
    158 	#
    159 	# Check for pam_appl.h.
    160 	#
    161 	src='
    162 #include <security/pam_appl.h>
    163 int main(void) {
    164 	return 0;
    165 }'
    166 	[ -z "$WITHOUT_PAM" ] && check_func "pam_appl_h" "$src" && {
    167 		printf 'SRCS     +=	pam.c\n' >>$CONFIG_MK
    168 		printf 'LDFLAGS  +=	-lpam\n' >>$CONFIG_MK
    169 		printf 'pam\n'
    170 		return 0
    171 	}
    172 
    173 	#
    174 	# Check for shadow.h.
    175 	#
    176 	src='
    177 #include <shadow.h>
    178 int main(void) {
    179 	return 0;
    180 }'
    181 	[ -z "$WITHOUT_SHADOW" ] && check_func "shadow_h" "$src" && {
    182 		printf 'LDFLAGS  +=	-lcrypt\n' >>$CONFIG_MK
    183 		printf 'shadow\n'
    184 		return 0
    185 	}
    186 
    187 	return 1
    188 }
    189 
    190 #
    191 # Check for explicit_bzero().
    192 #
    193 src='
    194 #include <string.h>
    195 int main(void) {
    196 	explicit_bzero(NULL, 0);
    197 	return 0;
    198 }'
    199 check_func "explicit_bzero" "$src" || {
    200 	printf 'OPENBSD  +=	explicit_bzero.o\n' >>$CONFIG_MK
    201 }
    202 
    203 #
    204 # Check for strlcat().
    205 #
    206 src='
    207 #include <string.h>
    208 int main(void) {
    209 	const char s1[] = "foo";
    210 	char s2[10];
    211 	strlcat(s2, s1, sizeof(s2));
    212 	return 0;
    213 }'
    214 check_func "strlcat" "$src" || {
    215 	printf 'OPENBSD  +=	strlcat.o\n' >>$CONFIG_MK
    216 }
    217 
    218 #
    219 # Check for strlcpy().
    220 #
    221 src='
    222 #include <string.h>
    223 int main(void) {
    224 	const char s1[] = "foo";
    225 	char s2[10];
    226 	strlcpy(s2, s1, sizeof(s2));
    227 	return 0;
    228 }'
    229 check_func "strlcpy" "$src" || {
    230 	printf 'OPENBSD  +=	strlcpy.o\n' >>$CONFIG_MK
    231 }
    232 
    233 #
    234 # Check for errc().
    235 #
    236 src='
    237 #include <err.h>
    238 int main(void) {
    239 	errc(0, 0, "");
    240 	return 0;
    241 }'
    242 check_func "errc" "$src" || {
    243 	printf 'OPENBSD  +=	errc.o\n' >>$CONFIG_MK
    244 }
    245 
    246 #
    247 # Check for verrc().
    248 #
    249 src='
    250 #include <err.h>
    251 int main(void) {
    252 	verrc(0, 0, "");
    253 	return 0;
    254 }'
    255 check_func "verrc" "$src" || {
    256 	printf 'OPENBSD  +=	verrc.o\n' >>$CONFIG_MK
    257 }
    258 
    259 #
    260 # Check for setprogname().
    261 #
    262 src='
    263 #include <stdlib.h>
    264 int main(void) {
    265 	setprogname("");
    266 	return 0;
    267 }'
    268 check_func "setprogname" "$src" || {
    269 	printf 'OPENBSD  +=	progname.o\n' >>$CONFIG_MK
    270 }
    271 
    272 #
    273 # Check for readpassphrase().
    274 #
    275 src='
    276 #include <readpassphrase.h>
    277 int main(void) {
    278 	char buf[12];
    279 	readpassphrase("", buf, sizeof(buf), 0);
    280 	return 0;
    281 }'
    282 check_func "readpassphrase" "$src" || {
    283 	printf 'OPENBSD  +=	readpassphrase.o\n' >>$CONFIG_MK
    284 }
    285 
    286 #
    287 # Check for strtonum().
    288 #
    289 src='
    290 #include <stdlib.h>
    291 int main(void) {
    292 	const char *errstr;
    293 	strtonum("", 1, 64, &errstr);
    294 	return 0;
    295 }'
    296 check_func "strtonum" "$src" || {
    297 	printf 'OPENBSD  +=	strtonum.o\n' >>$CONFIG_MK
    298 }
    299 
    300 #
    301 # Check for reallocarray().
    302 #
    303 src='
    304 #include <stdlib.h>
    305 int main(void) {
    306 	reallocarray(NULL, 0, 0);
    307 	return 0;
    308 }'
    309 check_func "reallocarray" "$src" || {
    310 	printf 'OPENBSD  +=	reallocarray.o\n' >>$CONFIG_MK
    311 }
    312 
    313 #
    314 # Check for execvpe().
    315 #
    316 src='
    317 #include <unistd.h>
    318 int main(void) {
    319 	const char *p = { "", NULL };
    320 	execvpe("", p, p);
    321 	return 0;
    322 }'
    323 check_func "execvpe" "$src" || {
    324 	printf 'OPENBSD  +=	execvpe.o\n' >>$CONFIG_MK
    325 }
    326 
    327 #
    328 # Check for setresuid().
    329 #
    330 src='
    331 #include <unistd.h>
    332 int main(void) {
    333 	setresuid(0, 0, 0);
    334 	return 0;
    335 }'
    336 check_func "setresuid" "$src" || {
    337 	printf 'OPENBSD  +=	setresuid.o\n' >>$CONFIG_MK
    338 }
    339 
    340 #
    341 # Check for pledge().
    342 #
    343 src='
    344 #include <unistd.h>
    345 int main(void) {
    346 	pledge("", NULL);
    347 	return 0;
    348 }'
    349 check_func "pledge" "$src" || {
    350 	printf 'OPENBSD  +=	pledge-noop.o\n' >>$CONFIG_MK
    351 }
    352 
    353 #
    354 # Check for closefrom().
    355 #
    356 src='
    357 #include <unistd.h>
    358 int main(void) {
    359 	closefrom(0);
    360 	return 0;
    361 }'
    362 check_func "closefrom" "$src" || {
    363 	printf 'OPENBSD  +=	closefrom.o\n' >>$CONFIG_MK
    364 }
    365 
    366 #
    367 # Check for sysconf().
    368 #
    369 src='
    370 #include <unistd.h>
    371 int main(void) {
    372 	(void)sysconf(0);
    373 	return 0;
    374 }'
    375 check_func "sysconf" "$src"
    376 
    377 #
    378 # Check for /proc/$PID.
    379 #
    380 printf 'Checking for %-14s\t\t' "/proc/\$PID ..." >&2
    381 if test -d /proc/$$; then
    382 	printf 'yes.\n' >&2
    383 	printf 'CFLAGS   +=	-DHAVE_%s\n' "PROC_PID" >>$CONFIG_MK
    384 else
    385 	printf 'no.\n' >&2
    386 fi
    387 
    388 #
    389 # Check for dirfd().
    390 #
    391 src='
    392 #include <dirent.h>
    393 int main(void) {
    394 	(void)dirfd(0);
    395 	return 0;
    396 }'
    397 check_func "dirfd" "$src"
    398 
    399 #
    400 # Check for fcntl.h.
    401 #
    402 src='
    403 #include <fcntl.h>
    404 int main(void) {
    405 	return 0;
    406 }'
    407 check_func "fcntl_h" "$src"
    408 
    409 #
    410 # Check for F_CLOSEM.
    411 #
    412 src='
    413 #include <fcntl.h>
    414 #ifndef F_CLOSEM
    415 #error no F_CLOSEM
    416 #endif
    417 int main(void) {
    418 	return 0;
    419 }'
    420 check_func "F_CLOSEM" "$src"
    421 
    422 #
    423 # Check for dirent.h.
    424 #
    425 src='
    426 #include <dirent.h>
    427 int main(void) {
    428 	return 0;
    429 }'
    430 check_func "dirent_h" "$src"
    431 
    432 #
    433 # Check for sys/ndir.h.
    434 #
    435 src='
    436 #include <sys/ndir.h>
    437 int main(void) {
    438 	return 0;
    439 }'
    440 check_func "sys_ndir_h" "$src"
    441 
    442 #
    443 # Check for sys/dir.h.
    444 #
    445 src='
    446 #include <sys/dir.h>
    447 int main(void) {
    448 	return 0;
    449 }'
    450 check_func "sys_dir_h" "$src"
    451 
    452 #
    453 # Check for ndir.h.
    454 #
    455 src='
    456 #include <ndir.h>
    457 int main(void) {
    458 	return 0;
    459 }'
    460 check_func "ndir_h" "$src"
    461 
    462 #
    463 #
    464 #
    465 src='
    466 #include <stdlib.h>
    467 int main(void){return 0;}
    468 __attribute__((__unused__)) static void foo(void){return;}
    469 '
    470 check_func "__attribute__" "$src" || {
    471 	printf 'CFLAGS	+=	-DNO_ATTRIBUTE_ON_RETURN_TYPE=1\n' >>$CONFIG_MK
    472 }
    473 
    474 auth=$(authmethod)
    475 if [ $? -eq 0 ]; then
    476 	printf 'Using auth method\t\t\t%s.\n' "$auth" >&2
    477 else
    478 	printf 'Error auth method\t\t\n' >&2
    479 	exit 1
    480 fi