Lukas Vrabec 37cf7d
# Copyright (C) 2017 Red Hat, Inc. All rights reserved.
Lukas Vrabec 37cf7d
#
Lukas Vrabec 37cf7d
#   Author: Petr Lautrbach <plautrba@redhat.com>
Lukas Vrabec 37cf7d
#   Author: Lukáš Vrabec <lvrabec@redhat.com>
Petr Lautrbach c49229
#
Petr Lautrbach c49229
# This program is free software; you can redistribute it and/or
Petr Lautrbach c49229
# modify it under the terms of the GNU General Public License
Petr Lautrbach c49229
# as published by the Free Software Foundation; either version 2
Petr Lautrbach c49229
# of the License, or (at your option) any later version.
Petr Lautrbach c49229
#
Petr Lautrbach c49229
# This program is distributed in the hope that it will be useful,
Petr Lautrbach c49229
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Petr Lautrbach c49229
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Petr Lautrbach c49229
# GNU General Public License for more details.
Petr Lautrbach c49229
#
Petr Lautrbach c49229
# You should have received a copy of the GNU General Public License
Petr Lautrbach c49229
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Petr Lautrbach c49229
Petr Lautrbach c49229
# RPM macros for packages installing SELinux modules
Petr Lautrbach c49229
Petr Lautrbach c49229
%_selinux_policy_version SELINUXPOLICYVERSION
Petr Lautrbach c49229
Lukas Vrabec 5c9722
%_selinux_store_path SELINUXSTOREPATH
Lukas Vrabec 146094
%_selinux_store_policy_path %{_selinux_store_path}/${_policytype}
Lukas Vrabec 5c9722
Petr Lautrbach c49229
%_file_context_file %{_sysconfdir}/selinux/${SELINUXTYPE}/contexts/files/file_contexts
Petr Lautrbach c49229
%_file_context_file_pre %{_localstatedir}/lib/rpm-state/file_contexts.pre
Petr Lautrbach c49229
Lukas Vrabec 146094
%_file_custom_defined_booleans %{_selinux_store_policy_path}/rpmbooleans.custom
Lukas Vrabec 146094
%_file_custom_defined_booleans_tmp %{_selinux_store_policy_path}/rpmbooleans.custom.tmp
Lukas Vrabec 29c9d8
Lukas Vrabec 723bc0
# %selinux_requires
Lukas Vrabec 723bc0
%selinux_requires \
Lukas Vrabec 723bc0
Requires: selinux-policy >= %{_selinux_policy_version} \
Lukas Vrabec 723bc0
BuildRequires: git \
Lukas Vrabec 723bc0
BuildRequires: pkgconfig(systemd) \
Lukas Vrabec 723bc0
BuildRequires: selinux-policy \
Lukas Vrabec 723bc0
BuildRequires: selinux-policy-devel \
Lukas Vrabec 723bc0
Requires(post): selinux-policy-base >= %{_selinux_policy_version} \
Lukas Vrabec 723bc0
Requires(post): libselinux-utils \
Lukas Vrabec 723bc0
Requires(post): policycoreutils \
Lukas Vrabec 146094
%if 0%{?fedora} || 0%{?rhel} > 7\
Lukas Vrabec 723bc0
Requires(post): policycoreutils-python-utils \
Lukas Vrabec 723bc0
%else \
Lukas Vrabec 723bc0
Requires(post): policycoreutils-python \
Lukas Vrabec 723bc0
%endif \
Lukas Vrabec 723bc0
%{nil}
Lukas Vrabec 723bc0
Lukas Vrabec 37cf7d
# %selinux_modules_install [-s <policytype>] [-p <modulepriority>] module [module]...
Lukas Vrabec 37cf7d
%selinux_modules_install("s:p:") \
Petr Lautrbach c49229
. /etc/selinux/config \
Petr Lautrbach c49229
_policytype=%{-s*} \
Petr Lautrbach c49229
if [ -z "${_policytype}" ]; then \
Petr Lautrbach c49229
  _policytype="targeted" \
Petr Lautrbach c49229
fi \
Lukas Vrabec 146094
if [ "${SELINUXTYPE}" = "${_policytype}" ]; then \
Lukas Vrabec 146094
  %{_sbindir}/semodule -n -s ${_policytype} -X %{!-p:200}%{-p*} -i %* \
Lukas Vrabec 146094
  %{_sbindir}/selinuxenabled && %{_sbindir}/load_policy \
Petr Lautrbach c49229
fi \
Petr Lautrbach c49229
%{nil}
Petr Lautrbach c49229
Lukas Vrabec 37cf7d
# %selinux_modules_uninstall [-s <policytype>] [-p <modulepriority>] module [module]...
Lukas Vrabec 37cf7d
%selinux_modules_uninstall("s:p:") \
Petr Lautrbach c49229
. /etc/selinux/config \
Petr Lautrbach c49229
_policytype=%{-s*} \
Petr Lautrbach c49229
if [ -z "${_policytype}" ]; then \
Petr Lautrbach c49229
  _policytype="targeted" \
Petr Lautrbach c49229
fi \
Petr Lautrbach c49229
if [ $1 -eq 0 ]; then \
Lukas Vrabec 146094
  if [ "${SELINUXTYPE}" = "${_policytype}" ]; then \
Lukas Vrabec 146094
    %{_sbindir}/semodule -n -X %{!-p:200}%{-p*} -s ${_policytype} -r %* &> /dev/null || : \
Lukas Vrabec 146094
    %{_sbindir}/selinuxenabled && %{_sbindir}/load_policy \
Petr Lautrbach c49229
  fi \
Petr Lautrbach c49229
fi \
Petr Lautrbach c49229
%{nil}
Petr Lautrbach c49229
Petr Lautrbach c49229
# %selinux_relabel_pre [-s <policytype>]
Petr Lautrbach c49229
%selinux_relabel_pre("s:") \
Petr Lautrbach c49229
. /etc/selinux/config \
Petr Lautrbach c49229
_policytype=%{-s*} \
Petr Lautrbach c49229
if [ -z "${_policytype}" ]; then \
Petr Lautrbach c49229
  _policytype="targeted" \
Petr Lautrbach c49229
fi \
Petr Lautrbach c49229
if %{_sbindir}/selinuxenabled && [ "${SELINUXTYPE}" = "${_policytype}" ]; then \
Petr Lautrbach c49229
  [ -f %{_file_context_file_pre} ] || cp -f %{_file_context_file} %{_file_context_file_pre} \
Petr Lautrbach c49229
fi \
Petr Lautrbach c49229
%{nil}
Petr Lautrbach c49229
Petr Lautrbach c49229
Petr Lautrbach c49229
# %selinux_relabel_post [-s <policytype>]
Petr Lautrbach c49229
%selinux_relabel_post("s:") \
Petr Lautrbach c49229
. /etc/selinux/config \
Petr Lautrbach c49229
_policytype=%{-s*} \
Petr Lautrbach c49229
if [ -z "${_policytype}" ]; then \
Petr Lautrbach c49229
  _policytype="targeted" \
Petr Lautrbach c49229
fi \
Lukas Vrabec 21c53d
if %{_sbindir}/selinuxenabled && [ "${SELINUXTYPE}" = "${_policytype}" ]; then \
Petr Lautrbach c49229
   if [ -f %{_file_context_file_pre} ]; then \
Lukas Vrabec 5c9722
     %{_sbindir}/fixfiles -C %{_file_context_file_pre} restore &> /dev/null \
Petr Lautrbach c49229
     rm -f %{_file_context_file_pre} \
Petr Lautrbach c49229
   fi \
Petr Lautrbach c49229
fi \
Petr Lautrbach c49229
%{nil}
Lukas Vrabec 6fa7bc
Lukas Vrabec 6fa7bc
# %selinux_set_booleans [-s <policytype>] boolean [boolean]...
Lukas Vrabec 6fa7bc
%selinux_set_booleans("s:") \
Lukas Vrabec 6fa7bc
. /etc/selinux/config \
Lukas Vrabec 6fa7bc
_policytype=%{-s*} \
Lukas Vrabec 6fa7bc
if [ -z "${_policytype}" ]; then \
Lukas Vrabec 6fa7bc
  _policytype="targeted" \
Lukas Vrabec 6fa7bc
fi \
Lukas Vrabec 146094
if [ -d "%{_selinux_store_policy_path}" ]; then \
Lukas Vrabec 146094
  LOCAL_MODIFICATIONS=$(%{_sbindir}/semanage boolean -E) \
Lukas Vrabec 146094
  if [ ! -f %_file_custom_defined_booleans ]; then \
Lukas Vrabec 146094
      /bin/echo "# This file is managed by macros.selinux-policy. Do not edit it manually" > %_file_custom_defined_booleans \
Lukas Vrabec 146094
  fi \
Lukas Vrabec 146094
  semanage_import='' \
Lukas Vrabec 146094
  for boolean in %*; do \
Lukas Vrabec 146094
      boolean_name=${boolean%=*} \
Lukas Vrabec 146094
      boolean_value=${boolean#*=} \
Lukas Vrabec 146094
      boolean_local_string=$(grep "$boolean_name\$" <<<$LOCAL_MODIFICATIONS) \
Lukas Vrabec 146094
      if [ -n "$boolean_local_string" ]; then \
Lukas Vrabec 146094
          semanage_import="${semanage_import}\\nboolean -m -$boolean_value $boolean_name" \
Lukas Vrabec 146094
          boolean_customized_string=$(grep "$boolean_name\$" %_file_custom_defined_booleans | tail -n 1) \
Lukas Vrabec 146094
          if [ -n "$boolean_customized_string" ]; then \
Lukas Vrabec 146094
              /bin/echo $boolean_customized_string >> %_file_custom_defined_booleans \
Lukas Vrabec 146094
          else \
Lukas Vrabec 146094
              /bin/echo $boolean_local_string >> %_file_custom_defined_booleans \
Lukas Vrabec 146094
          fi \
Lukas Vrabec 146094
      else \
Lukas Vrabec 146094
          semanage_import="${semanage_import}\\nboolean -m -$boolean_value $boolean_name" \
Lukas Vrabec 146094
          boolean_default_value=$(LC_ALL=C %{_sbindir}/semanage boolean -l | grep "^$boolean_name " | sed 's/[^(]*([^,]*, *\\(on\\|off\\).*/\\1/') \
Lukas Vrabec 146094
          /bin/echo "boolean -m --$boolean_default_value $boolean_name" >> %_file_custom_defined_booleans \
Lukas Vrabec 146094
      fi \
Lukas Vrabec 146094
  done; \
Lukas Vrabec 146094
  if %{_sbindir}/selinuxenabled && [ "${SELINUXTYPE}" = "${_policytype}" ]; then \
Lukas Vrabec 146094
      /bin/echo -e "$semanage_import" | %{_sbindir}/semanage import -S "${_policytype}" \
Lukas Vrabec 146094
  elif test -d /usr/share/selinux/"${_policytype}"/base.lst; then \
Lukas Vrabec 146094
      /bin/echo -e "$semanage_import" | %{_sbindir}/semanage import -S "${_policytype}" -N \
Lukas Vrabec 146094
  fi \
Lukas Vrabec 6fa7bc
fi \
Lukas Vrabec 6fa7bc
%{nil}
Lukas Vrabec 6fa7bc
Lukas Vrabec 6fa7bc
# %selinux_unset_booleans [-s <policytype>] boolean [boolean]...
Lukas Vrabec 6fa7bc
%selinux_unset_booleans("s:") \
Lukas Vrabec 6fa7bc
. /etc/selinux/config \
Lukas Vrabec 6fa7bc
_policytype=%{-s*} \
Lukas Vrabec 6fa7bc
if [ -z "${_policytype}" ]; then \
Lukas Vrabec 6fa7bc
  _policytype="targeted" \
Lukas Vrabec 6fa7bc
fi \
Lukas Vrabec 146094
if [ -d "%{_selinux_store_policy_path}" ]; then \
Lukas Vrabec 146094
  semanage_import='' \
Lukas Vrabec 146094
  for boolean in %*; do \
Lukas Vrabec 146094
      boolean_name=${boolean%=*} \
Lukas Vrabec 146094
      boolean_customized_string=$(grep "$boolean_name\$" %_file_custom_defined_booleans | tail -n 1) \
Lukas Vrabec 146094
      if [ -n "$boolean_customized_string" ]; then \
Lukas Vrabec 146094
          awk "/$boolean_customized_string/ && !f{f=1; next} 1" %_file_custom_defined_booleans > %_file_custom_defined_booleans_tmp && mv %_file_custom_defined_booleans_tmp %_file_custom_defined_booleans \
Lukas Vrabec 146094
          if ! grep -q "$boolean_name\$" %_file_custom_defined_booleans; then \
Lukas Vrabec 146094
              semanage_import="${semanage_import}\\n${boolean_customized_string}" \
Lukas Vrabec 146094
          fi \
Lukas Vrabec 146094
      fi \
Lukas Vrabec 146094
  done; \
Lukas Vrabec 146094
  if %{_sbindir}/selinuxenabled && [ "${SELINUXTYPE}" = "${_policytype}" ]; then \
Lukas Vrabec 146094
      /bin/echo -e "$semanage_import" | %{_sbindir}/semanage import -S "${_policytype}" \
Lukas Vrabec 146094
  elif test -d /usr/share/selinux/"${_policytype}"/base.lst; then \
Lukas Vrabec 146094
      /bin/echo -e "$semanage_import" | %{_sbindir}/semanage import -S "${_policytype}" -N \
Lukas Vrabec 146094
  fi \
Lukas Vrabec 6fa7bc
fi \
Lukas Vrabec 6fa7bc
%{nil}