Add support for static sites with chrooted SFTP access in create_vhost.sh and new permission repair script

This commit is contained in:
chris committed 2026-11-16 14:41:47 +01:00
1 parent 6f231e4a0a
commit 76f0fe1a7a
3 files changed
+804 -58

No files matched your search

+279 -58
View File
@@ -48,7 +48,9 @@ cat<<EOF
created.
-t <type>
One of "PHP-FPM", "FCGID", "MOD_PHP" or "STATIC".
One of "PHP-FPM", "FCGID", "MOD_PHP", "STATIC" or "STATIC-SFTP".
"STATIC-SFTP" creates a static site with suEXEC user, who can
access the website's base directory via chrooted SFTP.
If not give, the site url will be requested.
-s
@@ -285,6 +287,9 @@ _tcp_port=9000
__CUSTOM_IP_LOG=ip_requests.log
_suEXEC=false
set_suEXEC_password=false
_sftp_chroot=false
_sshd_chroot_missing=false
_auto=""
site_url=""
_type=""
@@ -304,6 +309,11 @@ VHOST_DIR=""
WEBSITES_ROOT_DIR=""
CREATE_SYMLINK_WEB_BASE_DIR=false
# - Static site with suEXEC and chrooted SFTP access:
# - sshd has to restrict members of that group to (chrooted) sftp
# -
SFTP_CHROOT_GROUP="sftp-chroot"
# ---
# - Determin timeout value
@@ -385,8 +395,12 @@ while getopts aChl:n:p:sS:t:u:V:v: opt ; do
_connection_msg="Unix Socket"
fi
;;
t) if [ "$OPTARG" = "PHP-FPM" -o "$OPTARG" = "FCGID" -o "$OPTARG" = "MOD_PHP" ]; then
t) if [ "$OPTARG" = "PHP-FPM" -o "$OPTARG" = "FCGID" -o "$OPTARG" = "MOD_PHP" -o "$OPTARG" = "STATIC" ]; then
_type=$OPTARG
elif [ "$OPTARG" = "STATIC-SFTP" ]; then
_type="STATIC"
_suEXEC=true
_sftp_chroot=true
fi
;;
u) site_url=$OPTARG ;;
@@ -477,6 +491,7 @@ if [ "$_type" != "PHP-FPM" -a "$_type" != "FCGID" -a "$_type" != "MOD_PHP" -a "$
echo "[5] static site (No PHP engine enabled)"
echo "[6] only redirect"
echo "[7] parke domain/site"
echo "[8] static site with suEXEC (chrooted SFTP access, No PHP engine enabled)"
echo ""
echononl "Your choice: "
while [ "$_type" != "PHP-FPM" -a "$_type" != "FCGID" -a "$_type" != "MOD_PHP" -a "$_type" != "STATIC" -a "$_type" != "REDIRECT" -a "$_type" != "PARKED" ];do
@@ -511,8 +526,14 @@ if [ "$_type" != "PHP-FPM" -a "$_type" != "FCGID" -a "$_type" != "MOD_PHP" -a "$
_type_msg="parke domai/site"
_new_extension=parked
;;
*) echo ""
echo -e "\t\033[1;33mFalsche Eingabe ! [ 1 = PHP-FPM ; 2 = mod_fcgid , 3 = mod_php, 4 =suEXEC, 5 = static, 6 = redirect, 7 = parke]\033[m"
8) _type="STATIC"
_suEXEC=true
_sftp_chroot=true
_type_msg="static site with suEXEC (chrooted SFTP access)"
_new_extension=static-suexec
;;
*) echo ""
echo -e "\t\033[1;33mFalsche Eingabe ! [ 1 = PHP-FPM ; 2 = mod_fcgid , 3 = mod_php, 4 =suEXEC, 5 = static, 6 = redirect, 7 = parke, 8 = static/SFTP]\033[m"
echo ""
echononl "Your choice: "
;;
@@ -535,8 +556,13 @@ else
_type_msg="mod_php (Apache PHP Module)"
_new_extension=mod_php
elif [ "$_type" = "STATIC" ]; then
_type_msg="static site (No PHP engine enabled)"
_new_extension=static
if $_sftp_chroot ; then
_type_msg="static site with suEXEC (chrooted SFTP access)"
_new_extension=static-suexec
else
_type_msg="static site (No PHP engine enabled)"
_new_extension=static
fi
elif [ "$_type" = "REDIRECT" ]; then
_type_msg="only redirect to another site"
_new_extension=redirect
@@ -1671,61 +1697,71 @@ if $_https && [[ -z "$_https_only" ]]; then
fi
if [ "$_type" = "FCGID" ]; then
if $_suEXEC ; then
echo ""
echo ""
echo -e "\033[33m--\033[m"
echo""
#echo -e "Use a common (existing) FCGID-configuration?"
#echo ""
#echo -e "\033[1;33m--\033[m"
#echo ""
#echo ""
if $_sftp_chroot ; then
echo "The suEXEC user is also the (chrooted) SFTP user for this site."
echo -e "Its home directory (and SFTP chroot) will be \"\033[1m$_web_base_dir\033[m\"."
echo ""
fi
echononl "suEXEC User: "
read suEXEC_user
while [ "X$suEXEC_user" = "X" ]; do
echononl "suEXEC User is mandatory. Try again: "
read suEXEC_user
done
echononl "suEXEC Group [$suEXEC_user]: "
read suEXEC_group
if [ "X$suEXEC_group" = "X" ]; then
suEXEC_group=$suEXEC_user
fi
getent shadow $suEXEC_user > /dev/null
if [ "$?" != "0" ]; then
_suEXEC_passwd_1="X"
_suEXEC_passwd_2="Y"
while [ "$_suEXEC_passwd_1" != "$_suEXEC_passwd_2" ]; do
echononl "Password for User \"$suEXEC_user\": "
read -s _suEXEC_passwd_1
echo
if [ "X$_suEXEC_passwd_1" = "X" ]; then
echo -e "\n\t\033[33m\033[1mPassword is mandatory!\033[m\n"
continue
fi
echononl "Repeat password for User \"$suEXEC_user\": "
read -s _suEXEC_passwd_2
echo
if [ "X$_suEXEC_passwd_2" = "X" ]; then
echo -e "\n\t\033[33m\033[1mPasswordretry is mandatory!\033[m\n"
continue
fi
if [ "$_suEXEC_passwd_1" != "$_suEXEC_passwd_2" ]; then
echo -e "\n\t\033[33m\033[1mSorry, passwords do not match\033[m\n"
else
suEXEC_password=$_suEXEC_passwd_1
fi
done
set_suEXEC_password=true
else
suEXEC_password=false
set_suEXEC_password=false
fi
fi # if $_suEXEC
if [ "$_type" = "FCGID" ]; then
OK=""
if $_suEXEC ; then
echononl "suEXEC User: "
read suEXEC_user
while [ "X$suEXEC_user" = "X" ]; do
echononl "suEXEC User is mandatory. Try again: "
read suEXEC_user
done
echononl "suEXEC Group [$suEXEC_user]: "
read suEXEC_group
if [ "X$suEXEC_group" = "X" ]; then
suEXEC_group=$suEXEC_user
fi
getent shadow $suEXEC_user > /dev/null
if [ "$?" != "0" ]; then
_suEXEC_passwd_1="X"
_suEXEC_passwd_2="Y"
while [ "$_suEXEC_passwd_1" != "$_suEXEC_passwd_2" ]; do
echononl "Password for User \"$suEXEC_user\": "
read -s _suEXEC_passwd_1
echo
if [ "X$_suEXEC_passwd_1" = "X" ]; then
echo -e "\n\t\033[33m\033[1mPassword is mandatory!\033[m\n"
continue
fi
echononl "Repeat password for User \"$suEXEC_user\": "
read -s _suEXEC_passwd_2
echo
if [ "X$_suEXEC_passwd_2" = "X" ]; then
echo -e "\n\t\033[33m\033[1mPasswordretry is mandatory!\033[m\n"
continue
fi
if [ "$_suEXEC_passwd_1" != "$_suEXEC_passwd_2" ]; then
echo -e "\n\t\033[33m\033[1mSorry, passwords do not match\033[m\n"
else
suEXEC_password=$_suEXEC_passwd_1
fi
done
set_suEXEC_password=true
else
suEXEC_password=false
fi
_COMMON_FCGI_CONFIG=false
else
echo ""
echo ""
echo -e "\033[33m--\033[m"
echo""
if [ -n "$_COMMON_FCGID_CONFIG_PATH" -a -d "$_COMMON_FCGID_CONFIG_PATH" ] ; then
if [ "$_auto" = "auto" -o "$_auto" = "full-auto" ]; then
_COMMON_FCGI_CONFIG=true
@@ -2041,6 +2077,10 @@ if $_print_summary ; then
echo " suEXEC User....................: $suEXEC_user"
echo " suEXEC Group...................: $suEXEC_group"
fi
if $_sftp_chroot ; then
echo " SFTP chroot directory (home)...: $_web_base_dir"
echo " SFTP chroot group (sshd).......: $SFTP_CHROOT_GROUP"
fi
echo ""
echo "Apache Installationsverzeichnis...: $apache_base_dir ($_apache_base_dir_realpath)"
@@ -2377,7 +2417,14 @@ if $_suEXEC ; then
# - and docroot), so use '/usr/sbin/nologin'
# -
echononl "\tAdding User \"$suEXEC_user\".."
useradd -g $suEXEC_group -d $_web_base_dir -s /usr/sbin/nologin -m $suEXEC_user
if $_sftp_chroot ; then
# - SFTP chroot: home directory (= website's base directory) must be
# - owned by root, so don't let useradd create it ('-M').
# -
useradd -g $suEXEC_group -d $_web_base_dir -s /usr/sbin/nologin -M $suEXEC_user
else
useradd -g $suEXEC_group -d $_web_base_dir -s /usr/sbin/nologin -m $suEXEC_user
fi
if [ "$?" = "0" ]; then
echo_ok
else
@@ -2397,6 +2444,83 @@ if $_suEXEC ; then
else
echo_skipped
fi
if $_sftp_chroot ; then
echononl "\tAdding Group \"$SFTP_CHROOT_GROUP\" (chrooted SFTP users).."
if getent group $SFTP_CHROOT_GROUP > /dev/null ; then
echo_skipped
else
groupadd $SFTP_CHROOT_GROUP
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
error "Cannot create Group \"$SFTP_CHROOT_GROUP\"."
fatal
fi
fi
echononl "\tAdd user \"$suEXEC_user\" to group \"$SFTP_CHROOT_GROUP\".."
if id -nG $suEXEC_user | tr ' ' '\n' | grep -q -x "$SFTP_CHROOT_GROUP" ; then
echo_skipped
else
usermod -a -G $SFTP_CHROOT_GROUP $suEXEC_user
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
error "Cannot add user \"$suEXEC_user\" to group \"$SFTP_CHROOT_GROUP\"."
fatal
fi
fi
# - Webserver user must be able to read all files of the SFTP user
# -
# - Note: Apache children call initgroups() at startup, so the (graceful)
# - restart at the end of this script activates the new membership.
# -
echononl "\tAdd webserver user \"$web_user\" to group \"$suEXEC_group\".."
if id -nG $web_user | tr ' ' '\n' | grep -q -x "$suEXEC_group" ; then
echo_skipped
else
usermod -a -G $suEXEC_group $web_user
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
error "Cannot add user \"$web_user\" to group \"$suEXEC_group\"."
fatal
fi
fi
_suEXEC_home="$(getent passwd $suEXEC_user | cut -d ':' -f 6)"
if [[ "$(realpath -m "$_suEXEC_home")" != "$(realpath -m "$_web_base_dir")" ]]; then
warn "Home directory of user \"$suEXEC_user\" is \"$_suEXEC_home\", not \"$_web_base_dir\".\n\n\t With 'ChrootDirectory %h' SFTP access will NOT be chrooted into this site.\n\t Change it (if appropriate) with:\n\n\t usermod -d $_web_base_dir $suEXEC_user"
fi
# - Check effective sshd configuration for that user
# -
echononl "\tCheck sshd: chrooted SFTP for user \"$suEXEC_user\".."
_sshd_binary="$(command -v sshd 2> /dev/null)"
[[ -z "$_sshd_binary" ]] && [[ -x /usr/sbin/sshd ]] && _sshd_binary=/usr/sbin/sshd
if [[ -z "$_sshd_binary" ]]; then
echo_skipped
warn "sshd binary not found - cannot check sshd configuration."
_sshd_chroot_missing=true
else
_sshd_conf_eff="$($_sshd_binary -T -C user=${suEXEC_user},host=localhost,addr=127.0.0.1 2> $log_file)"
_sshd_chroot="$(echo "$_sshd_conf_eff" | awk 'tolower($1) == "chrootdirectory" {print $2}')"
_sshd_force_cmd="$(echo "$_sshd_conf_eff" | awk 'tolower($1) == "forcecommand" {$1=""; print}')"
if [[ -n "$_sshd_chroot" ]] && [[ "$_sshd_chroot" != "none" ]] \
&& [[ "$_sshd_force_cmd" =~ internal-sftp ]] ; then
echo_ok
else
echo_failed
_sshd_chroot_missing=true
fi
fi
fi
fi
@@ -2457,7 +2581,43 @@ else
fi
echononl "\tChange owner of Web Base directory \"$_web_base_dir\".."
if $web_base_directory_already_exists ; then
if $_sftp_chroot ; then
# - SFTP chroot (sshd): The chroot directory and all its parent directories
# - must be owned by root and must not be writable by group or others.
# -
# - Note: also done, if the directory already exists - otherwise sshd
# - refuses the (chrooted) login.
# -
chown root:root $_web_base_dir && chmod 755 $_web_base_dir
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
error "Cannot change owner/permissions of web base directory \"$_web_base_dir\""
fatal
fi
echononl "\tCheck SFTP chroot path \"$_web_base_dir\".."
_chroot_path="$(realpath $_web_base_dir)"
_chroot_path_failed=""
while [[ -n "$_chroot_path" ]]; do
_owner_uid="$(stat -c '%u' "$_chroot_path")"
_perms="$(stat -c '%a' "$_chroot_path")"
if [[ "$_owner_uid" != "0" ]] || (( 8#$_perms & 8#022 )) ; then
_chroot_path_failed="${_chroot_path_failed}\n\t $(stat -c '%A %U:%G %n' "$_chroot_path")"
fi
[[ "$_chroot_path" = "/" ]] && break
_chroot_path="$(dirname "$_chroot_path")"
done
if [[ -z "$_chroot_path_failed" ]]; then
echo_ok
else
echo_failed
warn "SFTP chroot will fail! The following directories are not owned by root\n\t or are writable by group/others:\n${_chroot_path_failed}"
fi
elif $web_base_directory_already_exists ; then
echo_skipped
else
if $_suEXEC ; then
@@ -2515,7 +2675,17 @@ echononl "\tChange owner of \"DocumentRoot\" directory.."
if $web_base_directory_already_exists ; then
echo_skipped
else
if $_suEXEC ; then
if $_sftp_chroot ; then
# - Owned by the SFTP user and its (suEXEC) group. The webserver user is
# - member of that group, so apache can read all files of the user.
# -
# - Note: Using the user's own group (instead of the webserver group) is
# - robust - files created by the user always get that group, even
# - if the setgid bit gets lost (chmod by the user), and the user
# - can chgrp them back.
# -
chown -R ${suEXEC_user}:${suEXEC_group} $_doc_root && chmod 2750 $_doc_root
elif $_suEXEC ; then
chown -R ${suEXEC_user}:$suEXEC_group $_doc_root
elif [[ "$_type" = "PHP-FPM" ]]; then
chown -R ${unix_socket_owner}:${unix_socket_group} $_doc_root
@@ -2556,6 +2726,20 @@ else
echo_skipped
fi
# - SFTP chroot: let the SFTP user read (but not write) the logfiles of the site.
# - (Apache opens the logfiles as root.)
# -
if $_sftp_chroot && [[ "$(dirname "$(realpath -m $_log_dir)")" = "$(realpath -m $_web_base_dir)" ]]; then
echononl "\tChange owner/permissions of Logfile directory (root:${suEXEC_group} 750).."
chown root:${suEXEC_group} $_log_dir && chmod 750 $_log_dir
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
error "Cannot change owner/permissions of directory \"$_log_dir\""
fi
fi
if [ "$_type" = "FCGID" -a "$_COMMON_FCGI_CONFIG" = "false" ]; then
@@ -3432,8 +3616,13 @@ EOF
if [ "$?" = "0" ];then
echo_ok
echononl "\tChange ownerchip (${web_user}:${web_group}) of \"index.html\".."
chown ${web_user}:$web_group ${_doc_root}/index.html
if $_sftp_chroot ; then
echononl "\tChange ownerchip (${suEXEC_user}:${suEXEC_group}) of \"index.html\".."
chown ${suEXEC_user}:$suEXEC_group ${_doc_root}/index.html && chmod 640 ${_doc_root}/index.html
else
echononl "\tChange ownerchip (${web_user}:${web_group}) of \"index.html\".."
chown ${web_user}:$web_group ${_doc_root}/index.html
fi
if [ "$?" = "0" ];then
echo_ok
else
@@ -3505,7 +3694,39 @@ if $_syntax_ok ; then
error "Graceful restart of Apache Webserver failed!"
fi
fi
if $_sftp_chroot && $_sshd_chroot_missing ; then
warn "sshd is NOT configured for chrooted SFTP of user \"$suEXEC_user\"!
\t Add the following block at the END of /etc/ssh/sshd_config
\t (Match blocks in files included at the top of sshd_config, i.e.
\t /etc/ssh/sshd_config.d/*.conf, may affect the following settings):
\t \033[1mMatch Group ${SFTP_CHROOT_GROUP}
\t ChrootDirectory %h
\t ForceCommand internal-sftp -u 0027
\t AllowTcpForwarding no
\t AllowAgentForwarding no
\t X11Forwarding no
\t PermitTunnel no
\t PermitTTY no
\t #PasswordAuthentication yes\033[m
\t Check the configuration and reload sshd:
\t sshd -t && systemctl reload ssh
\t sshd -T -C user=${suEXEC_user},host=localhost,addr=127.0.0.1 | grep -i -E '^(chrootdirectory|forcecommand)'
\t Note: 'AllowUsers'/'AllowGroups' (if used) must permit the user."
fi
if $_sftp_chroot && ! grep -q -r 'fix_sftp_site_permissions' /etc/cron.d /etc/crontab /var/spool/cron 2> /dev/null ; then
info "SFTP clients may set permissions apache cannot read (i.e. 600). To repair
\t them automatically, install a cronjob, i.e. /etc/cron.d/fix_sftp_site_permissions:
\t */10 * * * * root ${script_base_dir}/fix_sftp_site_permissions.sh"
fi
rm -f $log_file
echo