#! /bin/bash
# This module sets policykit default permissions
# Copyright (C) 2008 SUSE Linux Products GmbH, Nuernberg, Germany.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Based on SuSEconfig.permissions
#
# Author: Ludwig Nussel <lnussel@suse.de> 2008
#

shopt -s nullglob

. /etc/sysconfig/security || exit 1

if [ -z "$POLKIT_DEFAULT_PRIVS" ]; then
	case "$PERMISSION_SECURITY" in
		*secure*|*paranoid*) POLKIT_DEFAULT_PRIVS="restrictive" ;;
		*) POLKIT_DEFAULT_PRIVS="standard" ;;
	esac
fi

mode=""
case "$CHECK_POLKIT_PRIVS" in
    set|"") mode="-set" ;;
    warn) ;;
    no) exit 0 ;;
    *) echo "invalid value '$CHECK_POLKIT_PRIVS' for \$CHECK_POLKIT_PRIVS, using 'set'" >&2; mode="-set" ;;
esac

# collect files that contain permission specifications
# we need to use a useful order
#
# 1. common privileges file (we have none yet)
files=""
FIXED_PROFILES_DIR=/usr/etc/polkit-default-privs/profiles
CUSTOM_PROFILES_DIR=/etc/polkit-default-privs

# 2. easy, standard, restrictive as those are defined by SUSE
have_local=
for level in $POLKIT_DEFAULT_PRIVS; do
    profile=$FIXED_PROFILES_DIR/$level
    case "$level" in
	easy|standard|restrictive)
	    if [ -e $profile ]; then
		files="$files $profile"
	    fi
	;;
	local)
	    # we always add the local profile to the list of profiles, this
	    # only serves as an ordering helper, i.e. if a specific order is
	    # configured, then maintain that. Otherwise append "local" to the
	    # end below.
	    have_local=1
	    ;;
	*)
	    echo "invalid value '$level' in POLKIT_DEFAULT_PRIVS in /etc/sysconfig/security, ignoring" 1>&2
	;;
    esac
done

if [ -z "$have_local" ]; then
	POLKIT_DEFAULT_PRIVS="$POLKIT_DEFAULT_PRIVS local"
fi

# 3. central privileges files with user defined level incl 'local'
for level in $POLKIT_DEFAULT_PRIVS; do 
    case "$level" in
	easy|standard|restrictive) continue ;;
    esac
    profile=$CUSTOM_PROFILES_DIR/$level
    if [ -e $profile ]; then
	files="$files $profile"
    fi
done

chkstat-polkit $mode $files
