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