#!/usr/bin/env bash # Configure the apparmor.d package # Copyright (C) 2021 Alexandre Pujol # SPDX-License-Identifier: GPL-2.0-only readonly ROOT=.build declare -a REMOVE_LIST _die() { printf '%s\n' "$*" >&2 && exit 1; } _warning() { printf 'Warning: %s\n' "$*" >&2; } _init() { rm -rf "${ROOT:?}" && rsync -a --exclude=.git . "$ROOT"; } # Remove files or directories in the package remove_files() { msg="Remove unneeded profiles/resources:" for path in "${REMOVE_LIST[@]}"; do [[ ! -e "${ROOT:?}/$path" ]] && continue msg+=$'\n'" $(stat -c '%A %u:%g' "${ROOT:?}/$path") $path" done echo "$msg" for path in "${REMOVE_LIST[@]}"; do rm -rf "${ROOT:?}/$path" done } # Set the distribution, flavor & groups configure() { echo "Set the configuration for $DISTRIBUTION." if [[ "$DISTRIBUTION" == archlinux ]]; then REMOVE_LIST+=( apparmor.d/abstractions/apt-common apparmor.d/groups/apt apparmor.d/groups/cron ) elif [[ "$DISTRIBUTION" == debian ]]; then REMOVE_LIST+=( apparmor.d/groups/pacman root/usr/share/libalpm/hooks/apparmor.hook ) fi } # Initialise the apparmor.d with the selected configuration. initialise() { _init remove_files echo "Ignore profiles in profiles.ignore." while read -r profile; do [[ "$profile" =~ ^\# ]] && continue if [[ "$profile" == */ ]]; then find "$ROOT/apparmor.d" -iname "${profile////}" -type d -exec rm -r {} \; else find "$ROOT/apparmor.d" -iname "$profile" -type f -exec rm {} \; fi done