1# serial 5   -*- Autoconf -*-
2# Copyright (C) 2006-2007, 2009-2021 Free Software Foundation, Inc.
3# This file is free software; the Free Software Foundation
4# gives unlimited permission to copy and/or distribute it,
5# with or without modifications, as long as this notice is preserved.
6
7# From Jim Meyering
8# Provide <selinux/selinux.h>, if necessary.
9# If it is already present, provide wrapper functions to guard against
10# misbehavior from getfilecon, lgetfilecon, and fgetfilecon.
11
12AC_DEFUN([gl_HEADERS_SELINUX_SELINUX_H],
13[
14  AC_REQUIRE([gl_LIBSELINUX])
15  if test "$with_selinux" != no; then
16    AC_CHECK_HEADERS([selinux/selinux.h])
17
18    if test "$ac_cv_header_selinux_selinux_h" = yes; then
19      # We do have <selinux/selinux.h>, so do compile getfilecon.c
20      # and arrange to use its wrappers.
21      gl_CHECK_NEXT_HEADERS([selinux/selinux.h])
22      AC_DEFINE([getfilecon], [rpl_getfilecon],
23                [Always use our getfilecon wrapper.])
24      AC_DEFINE([lgetfilecon], [rpl_lgetfilecon],
25                [Always use our lgetfilecon wrapper.])
26      AC_DEFINE([fgetfilecon], [rpl_fgetfilecon],
27                [Always use our fgetfilecon wrapper.])
28    fi
29
30    case "$ac_cv_search_setfilecon:$ac_cv_header_selinux_selinux_h" in
31      no:*) # already warned
32        ;;
33      *:no)
34        AC_MSG_WARN([libselinux was found but selinux/selinux.h is missing.])
35        AC_MSG_WARN([AC_PACKAGE_NAME will be compiled without SELinux support.])
36    esac
37  else
38    # Do as if <selinux/selinux.h> does not exist, even if
39    # AC_CHECK_HEADERS_ONCE has already determined that it exists.
40    AC_DEFINE([HAVE_SELINUX_SELINUX_H], [0])
41  fi
42])
43
44AC_DEFUN([gl_LIBSELINUX],
45[
46  AC_REQUIRE([AC_CANONICAL_HOST])
47  AC_REQUIRE([AC_CANONICAL_BUILD])
48
49  AC_ARG_WITH([selinux],
50    AS_HELP_STRING([--without-selinux], [do not use SELinux, even on systems with SELinux]),
51    [], [with_selinux=maybe])
52
53  LIB_SELINUX=
54  if test "$with_selinux" != no; then
55    gl_save_LIBS=$LIBS
56    AC_SEARCH_LIBS([setfilecon], [selinux],
57                   [test "$ac_cv_search_setfilecon" = "none required" ||
58                    LIB_SELINUX=$ac_cv_search_setfilecon])
59    LIBS=$gl_save_LIBS
60  fi
61  AC_SUBST([LIB_SELINUX])
62
63  # Warn if SELinux is found but libselinux is absent;
64  if test "$ac_cv_search_setfilecon" = no; then
65    if test "$host" = "$build" && test -d /selinux; then
66      AC_MSG_WARN([This system supports SELinux but libselinux is missing.])
67      AC_MSG_WARN([AC_PACKAGE_NAME will be compiled without SELinux support.])
68    fi
69    with_selinux=no
70  fi
71])
72