apache-require/inventory.sh

64 lines
1.7 KiB
Bash
Raw Normal View History

2020-09-29 11:08:10 +02:00
#!/bin/sh
# TODO Find .htacces from ServerHome
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-09-24 17:21:30 +02:00
conf_files=$tmp_dir/conf_files
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
get_conf_files() {
# Initialize le the list of configuration files with the default conf
2020-09-29 11:40:55 +02:00
test ! -e "$conf_files" && printf "%s\\n" $apache_conf > "$conf_files"
2020-09-24 17:21:30 +02:00
2020-09-29 11:08:10 +02:00
cd "$apache_dir"
# TODO: Refactor this mess
2020-09-24 17:21:30 +02:00
conf_files_size=0
while [ "$conf_files_size" -lt "$(stat -c %s "$conf_files")" ]; do
conf_files_size=$(stat -c %s "$conf_files")
for conf_file in $(cat "$conf_files"); do
# XXX: Expand the filenames
for glob in $(awk '/^[[:space:]]*Include/ {print $2}' "$conf_file"); do
realpath $glob >> "$conf_files"
done
done
sort "$conf_files" | uniq > "$conf_files"_tmp && mv "$conf_files"_tmp "$conf_files"
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:12:57 +02:00
printf 'File\tAllow\tOrder\tDeny\tSatsify\n'
# TODO make shellcheck happy
2020-09-29 11:08:10 +02:00
for file in $(cat $conf_files); 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-09-29 11:08:10 +02:00
get_conf_files
display_results