apache-require/inventory.sh

69 lines
1.9 KiB
Bash
Raw Normal View History

2020-09-29 11:08:10 +02:00
#!/bin/sh
2020-10-02 14:46:40 +02:00
# 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
2020-09-24 17:21:30 +02:00
set -e
apache_dir=/etc/apache2
apache_conf=$apache_dir/apache2.conf
2020-09-29 11:40:55 +02:00
tmp_dir=/tmp/apache-require
2020-10-02 14:53:35 +02:00
confs=$tmp_dir/confs
files_with_directives=$tmp_dir/files_with_directives
2020-09-29 11:40:55 +02:00
mkdir -p "$tmp_dir"
2020-09-24 17:21:30 +02:00
module_loaded() {
apache2ctl -D DUMP_MODULES | grep -q access_compat_module
}
# Get all config files included the
2020-10-02 14:53:35 +02:00
get_confs() {
2020-09-24 17:21:30 +02:00
# Initialize le the list of configuration files with the default conf
2020-10-02 14:53:35 +02:00
test ! -e "$confs" && printf "%s\\n" $apache_conf > "$confs"
2020-09-24 17:21:30 +02:00
2020-09-29 11:08:10 +02:00
cd "$apache_dir"
# TODO: Refactor this mess
2020-10-02 14:53:35 +02:00
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
2020-09-24 17:21:30 +02:00
# XXX: Expand the filenames
for glob in $(awk '/^[[:space:]]*Include/ {print $2}' "$conf_file"); do
2020-10-02 14:53:35 +02:00
realpath $glob >> "$confs"
2020-09-24 17:21:30 +02:00
done
done
2020-10-02 14:53:35 +02:00
sort "$confs" | uniq > "$confs"_tmp && mv "$confs"_tmp "$confs"
2020-09-24 17:21:30 +02:00
done
2020-09-29 11:08:10 +02:00
cd - 1>/dev/null
2020-09-24 17:21:30 +02:00
}
count_directives() {
directives="Allow Order Deny Satisfy"
for directive in $directives; do
export "$directive"="$(grep -Ec "^[[:blank:]]*$directive\\s" "$1")"
done
2020-09-24 17:21:30 +02:00
# 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
2020-09-24 17:21:30 +02:00
}
2020-09-29 11:08:10 +02:00
display_results() {
2020-09-29 11:43:05 +02:00
mv -b "$files_with_directives" "$files_with_directives".bak || true
printf 'File\tAllow\tOrder\tDeny\tSatsify\n' >&2
# TODO make shellcheck happy
2020-10-02 14:53:35 +02:00
for file in $(cat $confs); do
count_directives "$file" | tee -a "$files_with_directives"
2020-09-29 11:08:10 +02:00
done
}
2020-09-24 17:21:30 +02:00
2020-10-02 14:53:35 +02:00
get_confs
2020-09-29 11:08:10 +02:00
display_results