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