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