#!/usr/bin/env bash # Configure the apparmor.d package # Copyright (C) 2021 Alexandre Pujol # SPDX-License-Identifier: GPL-2.0-only readonly ROOT=.build _die() { printf 'Error: %s\n' "$*" >&2 && exit 1; } _warning() { printf ' Warning: %s\n' "$*" >&2; } # Displace files in the package sources # $@ List of files to displace _displace_files() { for path in "$@"; do mv "${ROOT:?}/$path" "${ROOT:?}/$path.apparmor.d" done } # Initialize a new clean apparmor.d build directory initialize() { rm -rf "${ROOT:?}" && rsync -a --exclude=.git . "$ROOT" } # Ignore profiles in profiles.ignore ignore() { 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 "$ROOT/apparmor.d/tunables/run" sed -i -e '/capability bpf/d' -e '/capability perfmon/d' \ "$ROOT/apparmor.d/groups/virt/libvirtd" fi ;; *) _die "$DISTRIBUTION is not a supported distribution." ;; esac } # Synchronise all profile in a new apparmor.d directory. synchronise() { echo "Synchronise all profiles." mv "${ROOT:?}/apparmor.d/groups/"*/* "${ROOT:?}/apparmor.d/" rm -rf "${ROOT:?}/apparmor.d/groups/" for dir in profiles-a-f profiles-g-l profiles-m-r profiles-s-z; do mv "${ROOT:?}/apparmor.d/$dir/"* "${ROOT:?}/apparmor.d/" rm -rf "${ROOT:?}/apparmor.d/$dir" done } # Set flags on some profile setflags() { echo "Set apparmor flags from profiles.flags" while read -r profile; do IFS=' ' read -r -a manifest <<< "$profile" profile="${manifest[0]}" flags="${manifest[1]}" [[ "$profile" =~ ^\# || -z "$profile" ]] && continue path="${ROOT:?}/apparmor.d/$profile" if [[ ! -f "$path" ]]; then _warning "Profile $profile not found" continue fi # If flags is set, overwrite profile flag if [[ -n "$flags" ]]; then # Remove all flags definition, then set manifest' flags sed -e "s/flags=(.*)//" \ -e "s/ {$/ flags=(${flags//,/ }) {/" \ -i "$path" fi done