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
c49229e
%_file_context_file %{_sysconfdir}/selinux/${SELINUXTYPE}/contexts/files/file_contexts
c49229e
%_file_context_file_pre %{_localstatedir}/lib/rpm-state/file_contexts.pre
c49229e
29c9d82
%_file_custom_defined_booleans %{_sharedstatedir}/selinux/${_policytype}/rpmbooleans.custom
29c9d82
%_file_custom_defined_booleans_tmp %{_sharedstatedir}/selinux/${_policytype}/rpmbooleans.custom.tmp
29c9d82
37cf7d7
# %selinux_modules_install [-s <policytype>] [-p <modulepriority>] module [module]...
37cf7d7
%selinux_modules_install("s:p:") \
c49229e
. /etc/selinux/config \
c49229e
_policytype=%{-s*} \
c49229e
if [ -z "${_policytype}" ]; then \
c49229e
  _policytype="targeted" \
c49229e
fi \
37cf7d7
%{_sbindir}/semodule -n -s ${_policytype} -X %{!-p:200}%{-p*} -i %* \
c49229e
if %{_sbindir}/selinuxenabled && [ "${SELINUXTYPE}" = "${_policytype}" ]; then \
c49229e
  %{_sbindir}/load_policy \
c49229e
fi \
c49229e
%{nil}
c49229e
37cf7d7
# %selinux_modules_uninstall [-s <policytype>] [-p <modulepriority>] module [module]...
37cf7d7
%selinux_modules_uninstall("s:p:") \
c49229e
. /etc/selinux/config \
c49229e
_policytype=%{-s*} \
c49229e
if [ -z "${_policytype}" ]; then \
c49229e
  _policytype="targeted" \
c49229e
fi \
c49229e
if [ $1 -eq 0 ]; then \
37cf7d7
  %{_sbindir}/semodule -n -X %{!-p:200}%{-p*} -r %* &> /dev/null || : \
c49229e
  if %{_sbindir}/selinuxenabled && [ "${SELINUXTYPE}" = "${_policytype}" ]; then \
c49229e
    %{_sbindir}/load_policy \
c49229e
  fi \
c49229e
fi \
c49229e
%{nil}
c49229e
c49229e
# %selinux_relabel_pre [-s <policytype>]
c49229e
%selinux_relabel_pre("s:") \
c49229e
. /etc/selinux/config \
c49229e
_policytype=%{-s*} \
c49229e
if [ -z "${_policytype}" ]; then \
c49229e
  _policytype="targeted" \
c49229e
fi \
c49229e
if %{_sbindir}/selinuxenabled && [ "${SELINUXTYPE}" = "${_policytype}" ]; then \
c49229e
  [ -f %{_file_context_file_pre} ] || cp -f %{_file_context_file} %{_file_context_file_pre} \
c49229e
fi \
c49229e
%{nil}
c49229e
c49229e
c49229e
# %selinux_relabel_post [-s <policytype>]
c49229e
%selinux_relabel_post("s:") \
c49229e
. /etc/selinux/config \
c49229e
_policytype=%{-s*} \
c49229e
if [ -z "${_policytype}" ]; then \
c49229e
  _policytype="targeted" \
c49229e
fi \
c49229e
if /usr/sbin/selinuxenabled && [ "${SELINUXTYPE}" = "${_policytype}" ]; then \
c49229e
   if [ -f %{_file_context_file_pre} ]; then \
c49229e
     %{_sbindir}/fixfiles -C %{_file_context_file_pre} restore \
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:") \
6fa7bc6
. /etc/selinux/config \
6fa7bc6
_policytype=%{-s*} \
6fa7bc6
if [ -z "${_policytype}" ]; then \
6fa7bc6
  _policytype="targeted" \
6fa7bc6
fi \
6fa7bc6
LOCAL_MODIFICATIONS=$(semanage boolean -E) \
6fa7bc6
if [ ! -f %_file_custom_defined_booleans ]; then \
29c9d82
    echo "# This file is managed by macros.selinux-policy. Do not edit it manually" > %_file_custom_defined_booleans \
6fa7bc6
fi \
6fa7bc6
semanage_import='' \
6fa7bc6
for boolean in %*; do \
6fa7bc6
    boolean_name=${boolean%=*} \
6fa7bc6
    boolean_value=${boolean#*=} \
6fa7bc6
    boolean_local_string=$(grep "$boolean_name\$" <<<$LOCAL_MODIFICATIONS) \
6fa7bc6
    if [ -n "$boolean_local_string" ]; then \
6fa7bc6
        semanage_import="${semanage_import}\\nboolean -m -$boolean_value $boolean_name" \
6fa7bc6
        boolean_customized_string=$(grep "$boolean_name\$" %_file_custom_defined_booleans | tail -n 1) \
6fa7bc6
        if [ -n "$boolean_customized_string" ]; then \
6fa7bc6
            /bin/echo $boolean_customized_string >> %_file_custom_defined_booleans \
6fa7bc6
        else \
6fa7bc6
            /bin/echo $boolean_local_string >> %_file_custom_defined_booleans \
6fa7bc6
        fi \
6fa7bc6
    else \
6fa7bc6
        semanage_import="${semanage_import}\\nboolean -m -$boolean_value $boolean_name" \
6fa7bc6
        boolean_default_value=$(semanage boolean -l | grep "^$boolean_name " | sed 's/[^(]*([^,]*, *\\(on\\|off\\).*/\\1/') \
6fa7bc6
        /bin/echo "boolean -m --$boolean_default_value $boolean_name" >> %_file_custom_defined_booleans \
6fa7bc6
    fi \
6fa7bc6
done; \
6fa7bc6
if /usr/sbin/selinuxenabled && [ "${SELINUXTYPE}" = "${_policytype}" ]; then \
6fa7bc6
    /bin/echo -e "$semanage_import" | %{_sbindir}/semanage import -S "${_policytype}" \
6fa7bc6
else \
6fa7bc6
    echo -e "$semanage_import" | %{_sbindir}/semanage import -S "${_policytype} -N" \
6fa7bc6
fi \
6fa7bc6
%{nil}
6fa7bc6
6fa7bc6
# %selinux_unset_booleans [-s <policytype>] boolean [boolean]...
6fa7bc6
%selinux_unset_booleans("s:") \
6fa7bc6
. /etc/selinux/config \
6fa7bc6
_policytype=%{-s*} \
6fa7bc6
if [ -z "${_policytype}" ]; then \
6fa7bc6
  _policytype="targeted" \
6fa7bc6
fi \
6fa7bc6
semanage_import='' \
6fa7bc6
for boolean in %*; do \
6fa7bc6
    boolean_name=${boolean%=*} \
6fa7bc6
    boolean_customized_string=$(grep "$boolean_name\$" %_file_custom_defined_booleans | tail -n 1) \
6fa7bc6
    if [ -n "$boolean_customized_string" ]; then \
6fa7bc6
        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 \
6fa7bc6
        if ! grep -q "$boolean_name\$" %_file_custom_defined_booleans; then \
6fa7bc6
            semanage_import="${semanage_import}\\n${boolean_customized_string}" \
6fa7bc6
        fi \
6fa7bc6
    fi \
6fa7bc6
done; \
6fa7bc6
if /usr/sbin/selinuxenabled && [ "${SELINUXTYPE}" = "${_policytype}" ]; then \
6fa7bc6
    /bin/echo -e "$semanage_import" | %{_sbindir}/semanage import -S "${_policytype}" \
6fa7bc6
else \
6fa7bc6
    echo -e "$semanage_import" | %{_sbindir}/semanage import -S "${_policytype} -N" \
6fa7bc6
fi \
6fa7bc6
%{nil}