apache-require/inventory.sh
2020-10-02 15:15:54 +02:00

69 lines
1.9 KiB
Bash
Executable file

#!/bin/sh
# TODO Find .htacces from DocumentRoot
# TODO find piece of apache conf in /etc
# TODO Handle /sur/share/scripts/vhost...
# TODO total by host: vhost, htacces, conf, cgi
set -e
apache_dir=/etc/apache2
apache_conf=$apache_dir/apache2.conf
tmp_dir=/tmp/apache-require
confs=$tmp_dir/confs
files_with_directives=$tmp_dir/files_with_directives
mkdir -p "$tmp_dir"
module_loaded() {
apache2ctl -D DUMP_MODULES | grep -q access_compat_module
}
# Get all config files included the
get_confs() {
# Initialize le the list of configuration files with the default conf
test ! -e "$confs" && printf "%s\\n" $apache_conf > "$confs"
cd "$apache_dir"
# TODO: Refactor this mess
confs_size=0
while [ "$confs_size" -lt "$(stat -c %s "$confs")" ]; do
confs_size=$(stat -c %s "$confs")
for conf_file in $(cat "$confs"); do
# XXX: Expand the filenames
for glob in $(awk '/^[[:space:]]*Include/ {print $2}' "$conf_file"); do
realpath $glob >> "$confs"
done
done
sort "$confs" | uniq > "$confs"_tmp && mv "$confs"_tmp "$confs"
done
cd - 1>/dev/null
}
count_directives() {
directives="Allow Order Deny Satisfy"
for directive in $directives; do
export "$directive"="$(grep -Ec "^[[:blank:]]*$directive\\s" "$1")"
done
# shellcheck disable=SC2154
if [ "$Allow" -ne 0 ] || [ "$Order" -ne 0 ] || \
[ "$Deny" -ne 0 ] || [ "$Satisfy" -ne 0 ]; then
printf "%s %d %d %d %d\\n" "$1" "$Allow" "$Order" "$Deny" "$Satisfy"
fi
}
display_results() {
mv -b "$files_with_directives" "$files_with_directives".bak || true
printf 'File\tAllow\tOrder\tDeny\tSatsify\n' >&2
# TODO make shellcheck happy
for file in $(cat $confs); do
count_directives "$file" | tee -a "$files_with_directives"
done
}
get_confs
display_results