#!/bin/bash
## kola:
##   exclusive: false
##   description: Verify that there are no files and directories
##     with 'g+w' or 'o+w' permission in /etc, except the known lists.

set -xeuo pipefail

# shellcheck disable=SC1091
. "$KOLA_EXT_DATA/commonlib.sh"

# List of known files and directories with group write permission (FCOS & EL)
list_known=()
# List of known files and directories with group write permission (EL only)
list_known_el=()

if ! is_fcos; then
    list_known+=("${list_known_el[@]}")
fi

unknown=""
while IFS= read -r -d '' e; do
    found="false"
    for k in "${list_known[@]}"; do
        if [[ "${k}" == "${e}" ]]; then
            found="true"
            break
        fi
    done
    if [[ "${found}" == "false" ]]; then
        unknown+=" ${e}"
    fi
done< <(find /usr /etc -type f -perm /022 -print0 -o -type d -perm /022 -print0)

if [[ -n "${unknown}" ]]; then
    find /usr /etc -type f -perm /022 -print0 -o -type d -perm /022 -print0 | xargs -0 ls -al
    find /usr /etc -type f -perm /022 -print0 -o -type d -perm /022 -print0 | xargs -0 rpm -qf
    fatal "found files or directories with 'g+w' or 'o+w' permission"
fi
ok "no files with 'g+w' or 'o+w' permission found in /etc"
