Files

124 lines
4.3 KiB
Bash
Raw Permalink Normal View History

2023-04-05 07:53:20 -07:00
#!/bin/bash
alias_block=''
# ---- DOCROOT_SUBDIR (optional, local development, environment=DEV only: Laravel / CodeIgniter 4 /
# Symfony style apps whose web root is public_html/public). Unset or empty (the
# default, and what every production site uses) renders EXACTLY the same bytes
# as before this variable existed. Validated with the same rules as WHP's
# panel/internal/vhost/docrootsub validator: letters, digits, '.', '_', '-';
# at most 4 segments, 64 bytes per segment, 128 bytes total; no segment starts
# with '.' or '-'; no '.', '..' or empty segment; the name cgi-bin (and the
# other reserved names) is refused; no leading/trailing slash.
docroot_valid() {
local LC_ALL=C s=$1 g seg
[ -z "$s" ] && return 0
# Whole-string charset first: `read` below only sees the first line, so a
# newline (config injection) must be refused before the per-segment checks.
[[ $s =~ ^[A-Za-z0-9._/-]+$ ]] || return 1
[ ${#s} -le 128 ] || return 1
[[ $s != /* && $s != */ ]] || return 1
local -a segs
IFS=/ read -r -a segs <<< "$s"
[ ${#segs[@]} -le 4 ] || return 1
for seg in "${segs[@]}"; do
[ -n "$seg" ] || return 1
[ ${#seg} -le 64 ] || return 1
[[ $seg =~ ^[A-Za-z0-9._-]+$ ]] || return 1
[[ $seg != .* && $seg != -* ]] || return 1
g=${seg,,}
case "$g" in .git|.well-known|.whp|.site-builder|.svn|.hg|.bzr|.env|.htaccess|cgi-bin) return 1 ;; esac
done
return 0
}
# Defence in depth: only DEV mode (local-dev.sh) honours DOCROOT_SUBDIR. A
# production container never moves its web root from this variable, even if a
# site owner managed to put it in the container environment: WHP sets web root
# folders through its own gated path.
if [ -n "${DOCROOT_SUBDIR:-}" ] && [ "${environment:-}" != "DEV" ]; then
echo "create-vhost: ignoring DOCROOT_SUBDIR (only honoured when environment=DEV)" >&2
DOCROOT_SUBDIR=''
fi
docroot="/home/$user/public_html"
parent_block=''
if [ -n "${DOCROOT_SUBDIR:-}" ]; then
if ! docroot_valid "$DOCROOT_SUBDIR"; then
echo "FATAL: invalid DOCROOT_SUBDIR '$DOCROOT_SUBDIR': use up to 4 folders of ASCII letters, digits, '.', '_' and '-' (e.g. public or web/public); no segment may start with '.' or '-', and '.', '..' and cgi-bin are not allowed." >&2
exit 1
fi
docroot="/home/$user/public_html/$DOCROOT_SUBDIR"
# Same shape as WHP's shared-tier render: public_html itself stops being a web
# root (no symlinks followed, public_html/.htaccess never read, nothing served).
parent_block="<Directory \"/home/$user/public_html\">
# DOCROOT_SUBDIR is set: the web root is $docroot.
# public_html itself is not served, no symlink is followed into the web
# root, and public_html/.htaccess is never read.
Options None
AllowOverride None
Require all denied
</Directory>
"
fi
# ---- HTTP -> HTTPS redirect keeps a non-standard HTTPS port (DEV only, set by
# local-dev.sh from -s). Without it a `-s 8443` mapping is redirected to
# https://host/ (port 443) and breaks. Unset, non-DEV or 443 = unchanged bytes.
https_port_part=''
if [ "${environment:-}" = "DEV" ] && [[ "${PUBLIC_HTTPS_PORT:-}" =~ ^[0-9]{1,5}$ ]] && [ "$PUBLIC_HTTPS_PORT" != "443" ]; then
https_port_part=":$PUBLIC_HTTPS_PORT"
fi
#Create Server Alias Block
if [ ! -z $serveralias ]; then
for alias in $(echo $serveralias | tr ',' ' ')
do
alias_block=$alias_block"ServerAlias $alias
"
done
fi
2023-04-05 07:53:20 -07:00
cat <<EOF > /etc/httpd/conf.d/$domain.conf
<Directory "/home/$user">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
${parent_block}<Directory "$docroot">
2023-04-05 07:53:20 -07:00
Options All MultiViews
AllowOverride All
Require all granted
</Directory>
<VirtualHost _default_:*>
ServerName "$domain"
$alias_block
DocumentRoot "$docroot"
2023-04-05 07:53:20 -07:00
RewriteEngine on
RewriteCond %{SERVER_NAME} =$domain
RewriteRule ^ https://%{SERVER_NAME}${https_port_part}%{REQUEST_URI} [END,NE,R=permanent]
2023-04-05 07:53:20 -07:00
</VirtualHost>
Listen 443 https
SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog
SSLSessionCache shmcb:/run/httpd/sslcache(512000)
SSLSessionCacheTimeout 300
SSLCryptoDevice builtin
2023-04-05 07:53:20 -07:00
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerName "$domain"
$alias_block
DocumentRoot "$docroot"
2023-04-05 07:53:20 -07:00
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
</VirtualHost>
</IfModule>
EOF