Chrooted SFTP access also for suEXEC (mod_fcgid) sites

create_vhost.sh:
- suEXEC (mod_fcgid) sites get the same chrooted SFTP setup as static
  suEXEC sites: web base directory root:root 755, DocumentRoot owned by
  suEXEC user/group, SFTP group membership, sshd and chroot path checks.
- SFTP group: use $SFTP_CHROOT_GROUP if it exists, otherwise 'sftpusers'
  (if exists). If neither exists, skip that step (group is no longer
  created).

fix_sftp_site_permissions.sh:
- Also handle active '*.suexec' vhosts. PHP files (*.php, *.phtml,
  *.php5) are not touched there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrisandClaude Opus 5.5 committed 2026-11-16 16:31:58 +01:00
1 parent 76f0fe1a7a
commit fec60403d1
3 files changed
+117 -47

No files matched your search

+6 -2
View File
@@ -73,8 +73,12 @@
# -
#CREATE_SYMLINK_WEB_BASE_DIR=
# - Group for chrooted SFTP users (vhost type "static site with suEXEC
# - (chrooted SFTP access)").
# - Group for chrooted SFTP users (vhost types "suEXEC (using mod_fcgid)"
# - and "static site with suEXEC (chrooted SFTP access)").
# -
# - The suEXEC user is added to this group, if it exists. Otherwise it is
# - added to group 'sftpusers' (if exists). If neither group exists, the
# - user is not added to any SFTP group. (The group is not created.)
# -
# - sshd must restrict members of this group, i.e. at the end of
# - /etc/ssh/sshd_config:
+62 -31
View File
@@ -309,10 +309,15 @@ VHOST_DIR=""
WEBSITES_ROOT_DIR=""
CREATE_SYMLINK_WEB_BASE_DIR=false
# - Static site with suEXEC and chrooted SFTP access:
# - suEXEC sites (suEXEC using mod_fcgid and static site with suEXEC)
# - with chrooted SFTP access:
# - sshd has to restrict members of that group to (chrooted) sftp
# -
# - If this group does not exist, group 'sftpusers' is used (if exists).
# - If neither exists, the suEXEC user is not added to any SFTP group.
# -
SFTP_CHROOT_GROUP="sftp-chroot"
SFTP_CHROOT_GROUP_FALLBACK="sftpusers"
# ---
@@ -487,7 +492,7 @@ if [ "$_type" != "PHP-FPM" -a "$_type" != "FCGID" -a "$_type" != "MOD_PHP" -a "$
echo "[1] PHP-FPM (FastCGI Process Manager)"
echo "[2] mod_fcgid (Apache FastCGI implementation)"
echo "[3] mod_php (Apache PHP Module)"
echo "[4] suEXEC (using mod_fcgid)"
echo "[4] suEXEC (using mod_fcgid, chrooted SFTP access)"
echo "[5] static site (No PHP engine enabled)"
echo "[6] only redirect"
echo "[7] parke domain/site"
@@ -574,6 +579,26 @@ else
echo "Configure site \"${site_url}\" - \"$_type_msg\""
fi
# - All suEXEC sites ([4] suEXEC using mod_fcgid and [8] static site with suEXEC)
# - get chrooted SFTP access: the suEXEC user is the (chrooted) SFTP user.
# -
if $_suEXEC ; then
_sftp_chroot=true
fi
# - Determine the SFTP group the suEXEC user will be added to:
# - $SFTP_CHROOT_GROUP, if it exists, otherwise $SFTP_CHROOT_GROUP_FALLBACK
# - ('sftpusers'), if it exists. If neither exists, no SFTP group is used.
# -
_sftp_group=""
if $_sftp_chroot ; then
if [[ -n "$SFTP_CHROOT_GROUP" ]] && getent group "$SFTP_CHROOT_GROUP" > /dev/null ; then
_sftp_group="$SFTP_CHROOT_GROUP"
elif [[ -n "$SFTP_CHROOT_GROUP_FALLBACK" ]] && getent group "$SFTP_CHROOT_GROUP_FALLBACK" > /dev/null ; then
_sftp_group="$SFTP_CHROOT_GROUP_FALLBACK"
fi
fi
if $_base_webserver_info_needed ; then
@@ -2079,7 +2104,11 @@ if $_print_summary ; then
fi
if $_sftp_chroot ; then
echo " SFTP chroot directory (home)...: $_web_base_dir"
echo " SFTP chroot group (sshd).......: $SFTP_CHROOT_GROUP"
if [[ -n "$_sftp_group" ]]; then
echo " SFTP chroot group (sshd).......: $_sftp_group"
else
echo -e " SFTP chroot group (sshd).......: \033[33mnone\033[m (neither \"$SFTP_CHROOT_GROUP\" nor \"$SFTP_CHROOT_GROUP_FALLBACK\" exists)"
fi
fi
echo ""
@@ -2447,32 +2476,24 @@ if $_suEXEC ; then
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
if [[ -n "$_sftp_group" ]]; then
echononl "\tAdd user \"$suEXEC_user\" to group \"$_sftp_group\" (chrooted SFTP users).."
if id -nG $suEXEC_user | tr ' ' '\n' | grep -q -x "$_sftp_group" ; then
echo_skipped
else
echo_failed
error "Cannot create Group \"$SFTP_CHROOT_GROUP\"."
fatal
usermod -a -G $_sftp_group $suEXEC_user
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
error "Cannot add user \"$suEXEC_user\" to group \"$_sftp_group\"."
fatal
fi
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
echononl "\tAdd user \"$suEXEC_user\" to a SFTP group (\"$SFTP_CHROOT_GROUP\" / \"$SFTP_CHROOT_GROUP_FALLBACK\").."
echo_skipped
warn "Neither group \"$SFTP_CHROOT_GROUP\" nor group \"$SFTP_CHROOT_GROUP_FALLBACK\" exists.\n\t User \"$suEXEC_user\" was NOT added to a SFTP group."
fi
# - Webserver user must be able to read all files of the SFTP user
@@ -2581,10 +2602,16 @@ else
fi
echononl "\tChange owner of Web Base directory \"$_web_base_dir\".."
if $_sftp_chroot ; then
if $_suEXEC ; then
# - suEXEC sites (suEXEC using mod_fcgid and static site with suEXEC):
# -
# - The web base directory is the home directory of the suEXEC user and
# - serves as SFTP chroot directory.
# -
# - 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.
# - The subdirectories (DocumentRoot, conf, ..) are owned by the suEXEC user.
# -
# - Note: also done, if the directory already exists - otherwise sshd
# - refuses the (chrooted) login.
@@ -2620,9 +2647,7 @@ if $_sftp_chroot ; then
elif $web_base_directory_already_exists ; then
echo_skipped
else
if $_suEXEC ; then
chown -R ${suEXEC_user}:$suEXEC_group $_web_base_dir
elif [[ "$_type" = "PHP-FPM" ]]; then
if [[ "$_type" = "PHP-FPM" ]]; then
chown -R ${unix_socket_owner}:${unix_socket_group} $_web_base_dir
else
chown -R ${web_user}:${web_group} $_web_base_dir
@@ -3702,7 +3727,7 @@ if $_sftp_chroot && $_sshd_chroot_missing ; then
\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 \033[1mMatch Group ${_sftp_group:-$SFTP_CHROOT_GROUP}
\t ChrootDirectory %h
\t ForceCommand internal-sftp -u 0027
\t AllowTcpForwarding no
@@ -3718,6 +3743,12 @@ if $_sftp_chroot && $_sshd_chroot_missing ; then
\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."
if [[ -z "$_sftp_group" ]]; then
warn "User \"$suEXEC_user\" is not member of a SFTP group. Create group and add user:
\t groupadd $SFTP_CHROOT_GROUP
\t usermod -a -G $SFTP_CHROOT_GROUP $suEXEC_user"
fi
fi
if $_sftp_chroot && ! grep -q -r 'fix_sftp_site_permissions' /etc/cron.d /etc/crontab /var/spool/cron 2> /dev/null ; then
+49 -14
View File
@@ -1,9 +1,13 @@
#!/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).
# - Repair permissions of suEXEC websites with chrooted SFTP access
# - (see create_vhost.sh):
# -
# - - vhost type "static site with suEXEC (chrooted SFTP access)",
# - vhost configuration files '*.static-suexec'
# - - vhost type "suEXEC (using mod_fcgid)",
# - vhost configuration files '*.suexec'
# -
# - 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
@@ -16,6 +20,11 @@
# - files : owned by suEXEC user --> group <suexec group>, g+r
# - directories: owned by suEXEC user --> group <suexec group>, u+rx,g+rx
# -
# - '*.suexec' sites: PHP files ('*.php', '*.phtml', '*.php5') are NOT
# - touched. They are executed as the suEXEC user (mod_fcgid), apache doesn't
# - need to read them. So restrictive permissions (i.e. 600 for config files
# - with passwords) stay.
# -
# - 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
@@ -36,7 +45,11 @@ script_dir="$(dirname $(realpath $0))"
log_file="$(mktemp)"
lock_file="/run/lock/${script_name%.sh}.lock"
vhost_extension="static-suexec"
# - vhost configuration extensions to handle
# -
# - Note: '*.static-suexec' does NOT match '*.suexec' (no dot before 'suexec').
# -
vhost_extensions="static-suexec suexec"
declare -a site_conf_arr
@@ -57,8 +70,14 @@ usage() {
\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.
all active vhosts of type
'*.static-suexec' (static site with suEXEC, chrooted SFTP access)
'*.suexec' (suEXEC using mod_fcgid, chrooted SFTP access)
so apache can deliver all files of the suEXEC user. On '*.suexec' sites
PHP files ('*.php', '*.phtml', '*.php5') are not touched (executed as the
suEXEC user).
Only files/directories owned by the suEXEC user are touched. Changes are
done as the suEXEC user, not as root.
@@ -290,16 +309,20 @@ fi
[[ -d "$_vhost_base_dir" ]] || fatal "VHost directory '${_vhost_base_dir}' not found."
# - Collect active vhost configurations of type '*.static-suexec'
# - Collect active vhost configurations of type '*.static-suexec' / '*.suexec'
# -
# - Active means: a symlink '<site>.conf' (as created by create_vhost.sh)
# - points to '<site>.conf.static-suexec'.
# - points to '<site>.conf.static-suexec' or '<site>.conf.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
_match=false
for _ext in $vhost_extensions ; do
[[ "$_target" = *.${_ext} ]] && _match=true && break
done
$_match || continue
[[ -f "$_target" ]] || continue
containsElement=false
for _conf in "${site_conf_arr[@]}" ; do
@@ -313,7 +336,7 @@ done < <(
# - 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."
info "No active '*.static-suexec' / '*.suexec' vhosts found in '${_vhost_base_dir}' - nothing to do."
clean_up 0
fi
@@ -324,7 +347,7 @@ if $terminal ; then
echo ""
echo " Apache VHost directory.............: $_vhost_base_dir"
echo " Webserver user.....................: ${web_user:-unknown}"
echo " Number of sites (*.${vhost_extension}).: ${#site_conf_arr[@]}"
echo " Number of sites (*.[static-]suexec): ${#site_conf_arr[@]}"
echo " Dry-run............................: $dry_run"
echo " Verbose............................: $verbose"
echo ""
@@ -346,7 +369,19 @@ declare -i _total_errors=0
for _conf in "${site_conf_arr[@]}" ; do
_site="$(basename "$_conf")"
_site="${_site%.conf.${vhost_extension}}"
# - Type of site. Check 'static-suexec' first ('*.suexec' doesn't match
# - it anyway, but be explicit).
# -
# - '*.suexec' sites: don't touch PHP files (see head of script)
# -
if [[ "$_site" = *.conf.static-suexec ]] ; then
_site="${_site%.conf.static-suexec}"
_exclude_args=()
else
_site="${_site%.conf.suexec}"
_exclude_args=( ! -name '*.php' ! -name '*.phtml' ! -name '*.php5' )
fi
echononl " Site \033[1m${_site}\033[m"
@@ -419,7 +454,7 @@ for _conf in "${site_conf_arr[@]}" ; do
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 \
runuser -u "$_user" -- find "$_doc_root" -xdev -user "$_user" -type f "${_exclude_args[@]}" \
\( ! -group "$_group" -o ! -perm -g=r \) -print \
> "$_list_files" 2>> "$log_file" || _failed=true
else
@@ -427,7 +462,7 @@ for _conf in "${site_conf_arr[@]}" ; do
\( ! -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 \
runuser -u "$_user" -- find "$_doc_root" -xdev -user "$_user" -type f "${_exclude_args[@]}" \
\( ! -group "$_group" -o ! -perm -g=r \) -print \
-exec chgrp "$_group" {} + -exec chmod g+r {} + \
> "$_list_files" 2>> "$log_file" || _failed=true