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