apache-require/inventory.sh
2020-10-02 16:30:03 +02:00

80 lines
2.1 KiB
Bash
Executable file

#!/bin/sh
# TODO Find .htaccess from DocumentRoot
# TODO find piece of apache conf in /etc
# TODO Handle /sur/share/scripts/vhost...
# TODO total by host: vhost, htaccess, conf, cgi
set -e
apache_dir=/etc/apache2
apache_conf=$apache_dir/apache2.conf
tmp_dir=/tmp/apache-require
confs=$tmp_dir/confs
confs_vhost=$tmp_dir/confs_vhost
confs_system=$tmp_dir/confs_system
confs_htaccess=$tmp_dir/confs_htaccess
result=$tmp_dir/result
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
}
get_htaccess() {
xargs -P 0 \
awk 'sub("^[[:space:]]*DocumentRoot[[:space:]]*", "")' \
< "$confs" \
| sort -u \
| xargs -P 0 -I _ find _ -name .htaccess > "$confs_htaccess"
}
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 "$result" "$result".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 "$result"
done
}
get_confs
display_results