507 lines
14 KiB
Bash
Executable File
507 lines
14 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# ----------
|
|
# - Repair permissions of static websites with chrooted SFTP access
|
|
# - (vhost type "static site with suEXEC (chrooted SFTP access)", vhost
|
|
# - configuration files '*.static-suexec', see create_vhost.sh).
|
|
# -
|
|
# - The webserver user is member of the suEXEC group of each such site. So
|
|
# - apache can deliver every file of the suEXEC user, as long as the file
|
|
# - has the suEXEC group and is group readable (directories: group readable
|
|
# - and executable).
|
|
# -
|
|
# - SFTP clients may break that (explicit chmod, 'preserve permissions' on
|
|
# - upload). This script repairs it:
|
|
# -
|
|
# - files : owned by suEXEC user --> group <suexec group>, g+r
|
|
# - directories: owned by suEXEC user --> group <suexec group>, u+rx,g+rx
|
|
# -
|
|
# - Security note:
|
|
# - All changes inside the DocumentRoot are done AS THE SUEXEC USER (runuser),
|
|
# - never as root. So a user cannot trick this script (i.e. by replacing a
|
|
# - file with a symlink to /etc/shadow) into changing files outside of his
|
|
# - own ones.
|
|
# -
|
|
# - Intended to run via cron, i.e. /etc/cron.d/fix_sftp_site_permissions:
|
|
# -
|
|
# - */10 * * * * root /path/to/fix_sftp_site_permissions.sh
|
|
# -
|
|
# - Without terminal, output is only produced if something was changed or
|
|
# - something went wrong (no cron mails otherwise).
|
|
# ----------
|
|
|
|
script_name="$(basename $(realpath $0))"
|
|
script_dir="$(dirname $(realpath $0))"
|
|
|
|
log_file="$(mktemp)"
|
|
lock_file="/run/lock/${script_name%.sh}.lock"
|
|
|
|
vhost_extension="static-suexec"
|
|
|
|
declare -a site_conf_arr
|
|
|
|
|
|
# =============
|
|
# --- Some functions
|
|
# =============
|
|
|
|
usage() {
|
|
|
|
[[ -n "$1" ]] && error "$1"
|
|
|
|
echo -e "
|
|
\033[1mUsage:\033[m
|
|
|
|
$(basename $0) [-h] [-n] [-v] [-d <vhost-directory>]
|
|
|
|
\033[1mDescription\033[m
|
|
|
|
Repairs group ownership and group permissions inside the DocumentRoot of
|
|
all active vhosts of type '*.${vhost_extension}' (static site with suEXEC,
|
|
chrooted SFTP access), so apache can deliver all files of the suEXEC user.
|
|
|
|
Only files/directories owned by the suEXEC user are touched. Changes are
|
|
done as the suEXEC user, not as root.
|
|
|
|
Without terminal (cron), output is only produced, if something was changed
|
|
or went wrong.
|
|
|
|
\033[1mOptions\033[m
|
|
|
|
-d <vhost-directory>
|
|
Directory containing the vhost configurations.
|
|
Default: <ServerRoot>/conf/vhosts
|
|
|
|
-h
|
|
Prints this help.
|
|
|
|
-n
|
|
Dry-run: only print what would be changed.
|
|
|
|
-v
|
|
Verbose: print every changed file/directory.
|
|
|
|
\033[1mExample:\033[m
|
|
|
|
$(basename $0) -n -v
|
|
|
|
Cronjob (/etc/cron.d/fix_sftp_site_permissions):
|
|
|
|
*/10 * * * * root $(realpath $0)
|
|
|
|
"
|
|
clean_up 1
|
|
}
|
|
|
|
clean_up() {
|
|
|
|
# Perform program exit housekeeping
|
|
rm -f $log_file
|
|
blank_line
|
|
exit $1
|
|
}
|
|
|
|
echononl(){
|
|
if $terminal ; then
|
|
echo -e -n "$*" 1>&2
|
|
fi
|
|
}
|
|
echo_ok() {
|
|
if $terminal ; then
|
|
echo -e "\033[75G[ \033[32mok\033[m ]"
|
|
fi
|
|
}
|
|
echo_failed(){
|
|
if $terminal ; then
|
|
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
|
|
fi
|
|
}
|
|
echo_skipped() {
|
|
if $terminal ; then
|
|
echo -e "\033[75G[ \033[37mskipped\033[m ]"
|
|
fi
|
|
}
|
|
fatal (){
|
|
echo ""
|
|
echo ""
|
|
if $terminal ; then
|
|
echo -e "\t[ \033[31m\033[1mFatal\033[m ]: \033[37m\033[1m$*\033[m"
|
|
echo ""
|
|
echo -e "\t\033[31m\033[1m Script will be interrupted..\033[m\033[m"
|
|
else
|
|
echo "fatal: $*"
|
|
echo "Script will be interrupted.."
|
|
fi
|
|
clean_up 1
|
|
}
|
|
|
|
error(){
|
|
echo ""
|
|
if $terminal ; then
|
|
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
|
|
else
|
|
echo -e "Error: $*"
|
|
fi
|
|
echo ""
|
|
}
|
|
|
|
warn (){
|
|
echo ""
|
|
if $terminal ; then
|
|
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
|
|
else
|
|
echo -e "Warning: $*"
|
|
fi
|
|
echo ""
|
|
}
|
|
|
|
info (){
|
|
if $terminal ; then
|
|
echo ""
|
|
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
|
|
echo ""
|
|
fi
|
|
}
|
|
|
|
blank_line() {
|
|
if $terminal ; then
|
|
echo ""
|
|
fi
|
|
}
|
|
|
|
# - Message for a site: in terminal mode right below the site line, without
|
|
# - terminal prefixed with the site name (cron mail).
|
|
# -
|
|
site_msg() {
|
|
if $terminal ; then
|
|
echo -e " $*"
|
|
else
|
|
# - no color codes in cron mails
|
|
echo -e "[${_site}] $*" | sed 's/\x1b\[[0-9;]*m//g'
|
|
fi
|
|
}
|
|
|
|
# - Print list of changed objects (verbose mode)
|
|
# -
|
|
print_list() {
|
|
local _line
|
|
while IFS='' read -r _line ; do
|
|
[[ -z "$_line" ]] && continue
|
|
site_msg " $_line"
|
|
done < "$1"
|
|
}
|
|
|
|
|
|
# ----------
|
|
# - Jobhandling
|
|
# ----------
|
|
|
|
# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM
|
|
# -
|
|
trap clean_up SIGHUP SIGINT SIGTERM
|
|
|
|
|
|
# ----------
|
|
# - Some checks ..
|
|
# ----------
|
|
|
|
# - Running in a terminal?
|
|
# -
|
|
if [[ -t 1 ]] ; then
|
|
terminal=true
|
|
else
|
|
terminal=false
|
|
fi
|
|
|
|
# - Read commandline options
|
|
# -
|
|
dry_run=false
|
|
verbose=false
|
|
_vhost_base_dir=""
|
|
while getopts d:hnv opt ; do
|
|
case $opt in
|
|
d) _vhost_base_dir="$OPTARG" ;;
|
|
h) usage ;;
|
|
n) dry_run=true ;;
|
|
v) verbose=true ;;
|
|
*) usage "Unknown option" ;;
|
|
esac
|
|
done
|
|
|
|
# - Only root can switch to the suEXEC users
|
|
# -
|
|
[[ $(id -u) -eq 0 ]] || fatal "This script must be run as root (i.e. via sudo)."
|
|
|
|
command -v runuser > /dev/null 2>&1 || fatal "Command 'runuser' (util-linux) not found."
|
|
|
|
# - find runs as the suEXEC user, which can't access i.e. '/root'
|
|
# -
|
|
cd / || fatal "Cannot change into directory '/'."
|
|
|
|
# - Don't run twice at the same time (cron)
|
|
# -
|
|
exec 9> "$lock_file" || fatal "Cannot open lock file '${lock_file}'."
|
|
if ! flock -n 9 ; then
|
|
$terminal && warn "Another instance of '${script_name}' is running - exiting."
|
|
rm -f $log_file
|
|
exit 0
|
|
fi
|
|
|
|
|
|
# ==========
|
|
# - Begin Main Script
|
|
# ==========
|
|
|
|
# ----------
|
|
# - Headline
|
|
# ----------
|
|
|
|
if $terminal ; then
|
|
echo ""
|
|
echo -e "\033[1m----------\033[m"
|
|
echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m"
|
|
echo -e "\033[1m----------\033[m"
|
|
fi
|
|
|
|
|
|
# - Determin httpd binary
|
|
# -
|
|
_httpd_binary="$(which httpd 2> /dev/null)"
|
|
if [[ -z "$_httpd_binary" ]] && [[ -x "/usr/local/apache2/bin/httpd" ]]; then
|
|
_httpd_binary="/usr/local/apache2/bin/httpd"
|
|
fi
|
|
|
|
if [[ -n "$_httpd_binary" ]] ; then
|
|
_run_cfg="$($_httpd_binary -t -D DUMP_RUN_CFG 2> /dev/null)"
|
|
fi
|
|
|
|
# - Determin webserver user (only used to check group membership)
|
|
# -
|
|
web_user="$(echo "$_run_cfg" | grep -i -E "^User" | awk '{print$2}' | cut -d '=' -f2 | tr -d '"')"
|
|
|
|
# - Determin vhost directory (if not given by option -d)
|
|
# -
|
|
if [[ -z "$_vhost_base_dir" ]] ; then
|
|
apache_base_dir="$(echo "$_run_cfg" | grep ServerRoot | awk '{print$2}' | tr -d '"')"
|
|
[[ -z "$apache_base_dir" ]] && apache_base_dir="/usr/local/apache2"
|
|
_vhost_base_dir="${apache_base_dir}/conf/vhosts"
|
|
fi
|
|
|
|
[[ -d "$_vhost_base_dir" ]] || fatal "VHost directory '${_vhost_base_dir}' not found."
|
|
|
|
|
|
# - Collect active vhost configurations of type '*.static-suexec'
|
|
# -
|
|
# - Active means: a symlink '<site>.conf' (as created by create_vhost.sh)
|
|
# - points to '<site>.conf.static-suexec'.
|
|
# -
|
|
# - Ignore subdirectory 'DELETED' and hidden files.
|
|
# -
|
|
while IFS='' read -r -d '' _link ; do
|
|
_target="$(realpath -e "$_link" 2> /dev/null)" || continue
|
|
[[ "$_target" = *.${vhost_extension} ]] || continue
|
|
[[ -f "$_target" ]] || continue
|
|
containsElement=false
|
|
for _conf in "${site_conf_arr[@]}" ; do
|
|
[[ "$_conf" = "$_target" ]] && containsElement=true && break
|
|
done
|
|
$containsElement || site_conf_arr+=("$_target")
|
|
done < <(
|
|
find "$_vhost_base_dir" -path "${_vhost_base_dir}/DELETED" -prune -o -type l ! -name '.*' -print0
|
|
)
|
|
|
|
# - No such sites is not an error - just nothing to do
|
|
# -
|
|
if [[ ${#site_conf_arr[@]} -eq 0 ]] ; then
|
|
info "No active '*.${vhost_extension}' vhosts found in '${_vhost_base_dir}' - nothing to do."
|
|
clean_up 0
|
|
fi
|
|
|
|
|
|
if $terminal ; then
|
|
echo ""
|
|
echo -e " \033[32m\033[1mParameter Summary:\033[m"
|
|
echo ""
|
|
echo " Apache VHost directory.............: $_vhost_base_dir"
|
|
echo " Webserver user.....................: ${web_user:-unknown}"
|
|
echo " Number of sites (*.${vhost_extension}).: ${#site_conf_arr[@]}"
|
|
echo " Dry-run............................: $dry_run"
|
|
echo " Verbose............................: $verbose"
|
|
echo ""
|
|
echo ""
|
|
echo -e " \033[32m-----\033[m"
|
|
echo -e " \033[1mMain part of script ..\033[m"
|
|
echo -e " \033[32m-----\033[m"
|
|
echo ""
|
|
fi
|
|
|
|
|
|
_list_dirs="$(mktemp)"
|
|
_list_files="$(mktemp)"
|
|
_list_foreign="$(mktemp)"
|
|
|
|
declare -i _total_changed=0
|
|
declare -i _total_errors=0
|
|
|
|
for _conf in "${site_conf_arr[@]}" ; do
|
|
|
|
_site="$(basename "$_conf")"
|
|
_site="${_site%.conf.${vhost_extension}}"
|
|
|
|
echononl " Site \033[1m${_site}\033[m"
|
|
|
|
# - Read DocumentRoot and SuexecUserGroup from vhost configuration
|
|
# -
|
|
_doc_root="$(grep -m 1 -i -E '^\s*DocumentRoot\s+' "$_conf" | awk '{print$2}' | tr -d '"' | sed 's#/*$##')"
|
|
_suexec_line="$(grep -m 1 -i -E '^\s*SuexecUserGroup\s+' "$_conf")"
|
|
_user="$(echo "$_suexec_line" | awk '{print$2}' | tr -d '"')"
|
|
_group="$(echo "$_suexec_line" | awk '{print$3}' | tr -d '"')"
|
|
|
|
if [[ -z "$_doc_root" ]] || [[ -z "$_user" ]] || [[ -z "$_group" ]] ; then
|
|
echo_failed
|
|
site_msg "No DocumentRoot or no SuexecUserGroup found in '${_conf}'"
|
|
(( _total_errors++ ))
|
|
continue
|
|
fi
|
|
|
|
# - Some sanity checks before changing anything
|
|
# -
|
|
if ! getent passwd "$_user" > /dev/null ; then
|
|
echo_failed
|
|
site_msg "suEXEC user '${_user}' does not exist"
|
|
(( _total_errors++ ))
|
|
continue
|
|
fi
|
|
if ! getent group "$_group" > /dev/null ; then
|
|
echo_failed
|
|
site_msg "suEXEC group '${_group}' does not exist"
|
|
(( _total_errors++ ))
|
|
continue
|
|
fi
|
|
if [[ $(id -u "$_user") -eq 0 ]] ; then
|
|
echo_failed
|
|
site_msg "suEXEC user '${_user}' is root - refusing"
|
|
(( _total_errors++ ))
|
|
continue
|
|
fi
|
|
if [[ -L "$_doc_root" ]] || [[ ! -d "$_doc_root" ]] ; then
|
|
echo_failed
|
|
site_msg "DocumentRoot '${_doc_root}' does not exist or is a symlink"
|
|
(( _total_errors++ ))
|
|
continue
|
|
fi
|
|
if [[ "$(stat -c '%U' "$_doc_root")" != "$_user" ]] ; then
|
|
echo_failed
|
|
site_msg "DocumentRoot '${_doc_root}' is not owned by suEXEC user '${_user}'"
|
|
(( _total_errors++ ))
|
|
continue
|
|
fi
|
|
|
|
_msgs=()
|
|
|
|
# - Webserver user must be member of the suEXEC group
|
|
# -
|
|
if [[ -n "$web_user" ]] && ! id -nG "$web_user" 2> /dev/null | tr ' ' '\n' | grep -q -x "$_group" ; then
|
|
_msgs+=("\033[33mWarning:\033[m webserver user '${web_user}' is NOT member of group '${_group}'"
|
|
" Fix: usermod -a -G ${_group} ${web_user} && apachectl graceful")
|
|
fi
|
|
|
|
# - Find (and fix) wrong group/permissions - as the suEXEC user
|
|
# -
|
|
# - Directories: '-exec ... \;' runs BEFORE find descends into the
|
|
# - directory, so even directories with mode 000 will be handled.
|
|
# -
|
|
> "$_list_dirs"
|
|
> "$_list_files"
|
|
_failed=false
|
|
|
|
if $dry_run ; then
|
|
runuser -u "$_user" -- find "$_doc_root" -xdev -user "$_user" -type d \
|
|
\( ! -group "$_group" -o ! -perm -g=rx -o ! -perm -u=rx \) -print \
|
|
> "$_list_dirs" 2> "$log_file" || _failed=true
|
|
runuser -u "$_user" -- find "$_doc_root" -xdev -user "$_user" -type f \
|
|
\( ! -group "$_group" -o ! -perm -g=r \) -print \
|
|
> "$_list_files" 2>> "$log_file" || _failed=true
|
|
else
|
|
runuser -u "$_user" -- find "$_doc_root" -xdev -user "$_user" -type d \
|
|
\( ! -group "$_group" -o ! -perm -g=rx -o ! -perm -u=rx \) -print \
|
|
-exec chgrp "$_group" {} \; -exec chmod u+rx,g+rx {} \; \
|
|
> "$_list_dirs" 2> "$log_file" || _failed=true
|
|
runuser -u "$_user" -- find "$_doc_root" -xdev -user "$_user" -type f \
|
|
\( ! -group "$_group" -o ! -perm -g=r \) -print \
|
|
-exec chgrp "$_group" {} + -exec chmod g+r {} + \
|
|
> "$_list_files" 2>> "$log_file" || _failed=true
|
|
fi
|
|
|
|
declare -i _num_dirs=$(grep -c . "$_list_dirs")
|
|
declare -i _num_files=$(grep -c . "$_list_files")
|
|
|
|
# - Objects not owned by the suEXEC user (not touched, only reported)
|
|
# -
|
|
find "$_doc_root" -xdev ! -user "$_user" ! -type l -print > "$_list_foreign" 2> /dev/null
|
|
declare -i _num_foreign=$(grep -c . "$_list_foreign")
|
|
|
|
if $_failed ; then
|
|
echo_failed
|
|
(( _total_errors++ ))
|
|
elif [[ $_num_dirs -gt 0 ]] || [[ $_num_files -gt 0 ]] || [[ ${#_msgs[@]} -gt 0 ]] ; then
|
|
$dry_run && echo_skipped || echo_ok
|
|
else
|
|
echo_ok
|
|
fi
|
|
|
|
if [[ $_num_dirs -gt 0 ]] || [[ $_num_files -gt 0 ]] ; then
|
|
if $dry_run ; then
|
|
site_msg "\033[33mDry-run:\033[m would fix ${_num_dirs} directories, ${_num_files} files (user: ${_user}, group: ${_group})"
|
|
else
|
|
site_msg "Fixed ${_num_dirs} directories, ${_num_files} files (user: ${_user}, group: ${_group})"
|
|
_total_changed+=$(( _num_dirs + _num_files ))
|
|
fi
|
|
if $verbose ; then
|
|
print_list "$_list_dirs"
|
|
print_list "$_list_files"
|
|
fi
|
|
fi
|
|
|
|
for _msg in "${_msgs[@]}" ; do
|
|
site_msg "$_msg"
|
|
done
|
|
|
|
if $_failed ; then
|
|
while IFS='' read -r _line ; do
|
|
site_msg "\033[31mError:\033[m $_line"
|
|
done < <(sort -u "$log_file")
|
|
fi
|
|
|
|
# - Only in terminal mode / verbose: files not owned by the suEXEC user
|
|
# - would produce a cron mail on every run otherwise.
|
|
# -
|
|
if [[ $_num_foreign -gt 0 ]] && ( $terminal || $verbose ) ; then
|
|
site_msg "\033[33mNote:\033[m ${_num_foreign} objects in DocumentRoot are NOT owned by '${_user}' (not touched)"
|
|
$verbose && print_list "$_list_foreign"
|
|
fi
|
|
|
|
done
|
|
|
|
rm -f "$_list_dirs" "$_list_files" "$_list_foreign"
|
|
|
|
|
|
# ----------
|
|
# - Some post-script tasks ..
|
|
# ----------
|
|
|
|
if [[ $_total_errors -gt 0 ]] ; then
|
|
if $terminal ; then
|
|
error "${_total_errors} site(s) failed."
|
|
else
|
|
echo "Error: ${_total_errors} site(s) failed."
|
|
fi
|
|
clean_up 1
|
|
fi
|
|
|
|
if ! $dry_run && [[ $_total_changed -eq 0 ]] ; then
|
|
info "Nothing has changed."
|
|
fi
|
|
|
|
clean_up 0
|