Add script 'set_suexec_users_nologin.sh'.
Sets the login shell of all suexec users (SuexecUserGroup in active vhost configurations) from '/bin/bash' to '/usr/sbin/nologin'. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
be451b61a6
commit
5da75e1fc0
1 file changed
+394
Executable
+394
@@ -0,0 +1,394 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# ----------
|
||||
# - Set the login shell of all suexec users (SuexecUserGroup directive in
|
||||
# - the active vhost configurations) from '/bin/bash' to '/usr/sbin/nologin'.
|
||||
# -
|
||||
# - suexec does not need a login shell - only UID/GID and the docroot are
|
||||
# - checked. So websites keep working with shell '/usr/sbin/nologin'.
|
||||
# -
|
||||
# - Note:
|
||||
# - FTP accounts are not affected, if pure-ftpd authenticates against
|
||||
# - PureDB (virtual users) only. With UnixAuthentication or PAMAuthentication
|
||||
# - (pam_shells !) FTP logins of these users would fail afterwards.
|
||||
# ----------
|
||||
|
||||
script_name="$(basename $(realpath $0))"
|
||||
script_dir="$(dirname $(realpath $0))"
|
||||
|
||||
log_file="$(mktemp)"
|
||||
|
||||
backup_date=$(date +%Y-%m-%d-%H%M)
|
||||
|
||||
new_shell="/usr/sbin/nologin"
|
||||
old_shell="/bin/bash"
|
||||
|
||||
# - Users with UID less than this are never touched
|
||||
# -
|
||||
min_uid=200
|
||||
|
||||
declare -a suexec_user_arr
|
||||
|
||||
|
||||
# =============
|
||||
# --- Some functions
|
||||
# =============
|
||||
|
||||
usage() {
|
||||
|
||||
[[ -n "$1" ]] && error "$1"
|
||||
|
||||
echo -e "
|
||||
\033[1mUsage:\033[m
|
||||
|
||||
$(basename $0) [-h] [-n] [-d <vhost-directory>]
|
||||
|
||||
\033[1mDescription\033[m
|
||||
|
||||
Sets the login shell of all suexec users found in the vhost
|
||||
configurations from '${old_shell}' to '${new_shell}'.
|
||||
|
||||
Subdirectory 'DELETED' and hidden files (i.e. vim swap files)
|
||||
of the vhost directory are ignored.
|
||||
|
||||
\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.
|
||||
|
||||
\033[1mExample:\033[m
|
||||
|
||||
$(basename $0) -n
|
||||
|
||||
"
|
||||
clean_up 1
|
||||
}
|
||||
|
||||
clean_up() {
|
||||
|
||||
# Perform program exit housekeeping
|
||||
rm -f $log_file
|
||||
blank_line
|
||||
exit $1
|
||||
}
|
||||
|
||||
echononl(){
|
||||
echo X\\c > /tmp/shprompt$$
|
||||
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
|
||||
echo -e -n "$*\\c" 1>&2
|
||||
else
|
||||
echo -e -n "$*" 1>&2
|
||||
fi
|
||||
rm /tmp/shprompt$$
|
||||
}
|
||||
echo_ok() {
|
||||
if $terminal ; then
|
||||
echo -e "\033[75G[ \033[32mok\033[m ]"
|
||||
else
|
||||
echo " [ ok ]"
|
||||
fi
|
||||
}
|
||||
echo_failed(){
|
||||
if $terminal ; then
|
||||
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
|
||||
else
|
||||
echo ' [ failed! ]'
|
||||
fi
|
||||
}
|
||||
echo_skipped() {
|
||||
if $terminal ; then
|
||||
echo -e "\033[75G[ \033[37mskipped\033[m ]"
|
||||
else
|
||||
echo " [ skipped ]"
|
||||
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 "Error: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
warn (){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
|
||||
else
|
||||
echo "Warning: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
info (){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*"
|
||||
else
|
||||
echo "Info: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
blank_line() {
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# ----------
|
||||
# - 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
|
||||
_vhost_base_dir=""
|
||||
while getopts d:hn opt ; do
|
||||
case $opt in
|
||||
d) _vhost_base_dir="$OPTARG" ;;
|
||||
h) usage ;;
|
||||
n) dry_run=true ;;
|
||||
*) usage "Unknown option" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# - Only root is allowed to change login shells
|
||||
# -
|
||||
[[ $(id -u) -eq 0 ]] || fatal "This script must be run as root (i.e. via sudo)."
|
||||
|
||||
[[ -x "$new_shell" ]] || fatal "Shell '${new_shell}' not found."
|
||||
|
||||
|
||||
# ==========
|
||||
# - 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 vhost directory (if not given by option -d)
|
||||
# -
|
||||
if [[ -z "$_vhost_base_dir" ]] ; then
|
||||
|
||||
# - Determin httpd binary
|
||||
# -
|
||||
_httpd_binary="$(which httpd)"
|
||||
if [[ -z "$_httpd_binary" ]] && [[ -x "/usr/local/apache2/bin/httpd" ]]; then
|
||||
_httpd_binary="/usr/local/apache2/bin/httpd"
|
||||
fi
|
||||
|
||||
# - Determin ServerRoot directory
|
||||
# -
|
||||
if [[ -n "$_httpd_binary" ]] ; then
|
||||
apache_base_dir="$($_httpd_binary -t -D DUMP_RUN_CFG 2>/dev/null | grep ServerRoot | awk '{print$2}' | tr -d '"')"
|
||||
fi
|
||||
[[ -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 suexec users from vhost configurations
|
||||
# -
|
||||
# - Ignore subdirectory 'DELETED' and hidden files (vim swap files a.s.o.)
|
||||
# -
|
||||
while IFS='' read -r _user ; do
|
||||
suexec_user_arr+=("$_user")
|
||||
done < <(
|
||||
find "$_vhost_base_dir" -path "${_vhost_base_dir}/DELETED" -prune -o -type f ! -name '.*' -print0 \
|
||||
| xargs -0 grep -hiE '^\s*SuexecUserGroup\s+\S+' 2>/dev/null \
|
||||
| awk '{print $2}' | sort -u
|
||||
)
|
||||
|
||||
[[ ${#suexec_user_arr[@]} -gt 0 ]] || fatal "No suexec users found in '${_vhost_base_dir}'."
|
||||
|
||||
|
||||
echo ""
|
||||
echo -e " \033[32m\033[1mParameter Summary:\033[m"
|
||||
echo ""
|
||||
echo " Apache VHost directory.............: $_vhost_base_dir"
|
||||
echo " Number of suexec users.............: ${#suexec_user_arr[@]}"
|
||||
echo " Change shell.......................: $old_shell --> $new_shell"
|
||||
echo " Dry-run............................: $dry_run"
|
||||
echo ""
|
||||
|
||||
|
||||
# ----------
|
||||
# - Some pre-script tasks ..
|
||||
# ----------
|
||||
|
||||
if ! $dry_run ; then
|
||||
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
echo -e " \033[32m-----\033[m"
|
||||
echo -e " \033[1mDoing some pre-script tasks ..\033[m"
|
||||
echo -e " \033[32m-----\033[m"
|
||||
echo ""
|
||||
fi
|
||||
|
||||
# - Backup /etc/passwd (removed again, if nothing has changed)
|
||||
# -
|
||||
echononl " Backup '/etc/passwd' to \033[1m/etc/passwd.${backup_date}\033[m"
|
||||
cp -a /etc/passwd "/etc/passwd.${backup_date}" > $log_file 2>&1
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo_ok
|
||||
else
|
||||
echo_failed
|
||||
fatal "$(cat $log_file)"
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
# ----------
|
||||
# - Main part of script
|
||||
# ----------
|
||||
|
||||
if $terminal ; then
|
||||
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
|
||||
|
||||
_changed=false
|
||||
for _user in "${suexec_user_arr[@]}" ; do
|
||||
|
||||
echononl " User \033[1m${_user}\033[m"
|
||||
|
||||
# - User exists?
|
||||
# -
|
||||
_passwd_entry="$(getent passwd "$_user")"
|
||||
if [[ -z "$_passwd_entry" ]] ; then
|
||||
echo_skipped
|
||||
echo -e " \033[33mUser does not exist\033[m"
|
||||
continue
|
||||
fi
|
||||
|
||||
_uid="$(echo "$_passwd_entry" | cut -d: -f3)"
|
||||
_shell="$(echo "$_passwd_entry" | cut -d: -f7)"
|
||||
|
||||
# - Already done?
|
||||
# -
|
||||
if [[ "$_shell" = "$new_shell" ]] ; then
|
||||
echo_ok
|
||||
continue
|
||||
fi
|
||||
|
||||
# - Don't touch system accounts
|
||||
# -
|
||||
if [[ $_uid -lt $min_uid ]] ; then
|
||||
echo_skipped
|
||||
echo -e " \033[33mUID $_uid is less than $min_uid\033[m"
|
||||
continue
|
||||
fi
|
||||
|
||||
# - Only change '/bin/bash', leave other (special) shells as they are
|
||||
# -
|
||||
if [[ "$_shell" != "$old_shell" ]] ; then
|
||||
echo_skipped
|
||||
echo -e " \033[33mShell is '${_shell}', not '${old_shell}'\033[m"
|
||||
continue
|
||||
fi
|
||||
|
||||
if $dry_run ; then
|
||||
echo_skipped
|
||||
echo -e " \033[33mDry-run:\033[m would change shell $_shell --> $new_shell"
|
||||
continue
|
||||
fi
|
||||
|
||||
usermod -s "$new_shell" "$_user" > $log_file 2>&1
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo_ok
|
||||
echo -e " Shell changed: $_shell --> $new_shell"
|
||||
_changed=true
|
||||
else
|
||||
echo_failed
|
||||
error "$(cat $log_file)"
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
|
||||
# ----------
|
||||
# - Some post-script tasks ..
|
||||
# ----------
|
||||
|
||||
if ! $dry_run && ! $_changed ; then
|
||||
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
echo ""
|
||||
echo -e " \033[32m-----\033[m"
|
||||
echo -e " \033[1mDoing some post-script tasks ..\033[m"
|
||||
echo -e " \033[32m-----\033[m"
|
||||
echo ""
|
||||
fi
|
||||
|
||||
echo " Nothing has changed"
|
||||
echononl " Remove previously created backup '/etc/passwd.${backup_date}'"
|
||||
rm -f "/etc/passwd.${backup_date}" > $log_file 2>&1
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo_ok
|
||||
else
|
||||
echo_failed
|
||||
error "$(cat $log_file)"
|
||||
fi
|
||||
fi
|
||||
|
||||
clean_up 0
|
||||
Reference in new issue
Block a user