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