2022-05-02 16:19:00 -07:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# vhost-stack
|
|
|
|
# https://git.stack-source.com/msb/vhost-stack
|
2022-08-22 13:22:16 -07:00
|
|
|
# Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2022-05-02 16:19:00 -07:00
|
|
|
|
|
|
|
# load include file
|
|
|
|
source $(dirname $0)/vhost.sh
|
|
|
|
|
|
|
|
help()
|
|
|
|
{
|
|
|
|
thisfilename=$(basename -- "$0")
|
|
|
|
echo "Get VirtualHost info."
|
|
|
|
echo ""
|
2025-02-04 09:48:29 -08:00
|
|
|
echo "usage: $thisfilename [-d <domain>] [-u <username>] [-m <macro>] [-o <option>] [-c] [-h]"
|
2022-05-02 16:19:00 -07:00
|
|
|
echo ""
|
|
|
|
echo " -h Print this help."
|
2022-07-18 19:09:40 -07:00
|
|
|
echo " -d <domain> Domain name to get info for. By default all domains are returned."
|
|
|
|
echo " -u <username> Only return domains owned by specified username."
|
2025-02-04 09:48:29 -08:00
|
|
|
echo " -m <macro> Abbreviated name of Apache macro to query."
|
|
|
|
echo " e.g. VHost, Redirect, VHostAlias, VHostSubdomain, VMail, Mailman3."
|
|
|
|
echo " Optional, defaults to VHost (all regular VirtualHosts)."
|
|
|
|
echo " Leave off the HTTP/HTTPS part, both are included in results."
|
|
|
|
echo " -o <option> Alias or Redirect URL to query."
|
|
|
|
echo " For Aliases & Redirects '-d <domain>' is the alias/redirect domain,"
|
|
|
|
echo " and '-o <option>' is the existing VirtualHost to alias/redirect to."
|
2022-05-02 16:19:00 -07:00
|
|
|
echo " -c CVS - Output in cvs format, instead of tabbed table."
|
|
|
|
}
|
|
|
|
|
|
|
|
vhost:getoptions "$@"
|
|
|
|
|
|
|
|
# create newline var
|
|
|
|
NL=$'\n'
|
|
|
|
|
2025-02-04 09:48:29 -08:00
|
|
|
# check for macro
|
|
|
|
if [[ -z $macro ]]; then
|
|
|
|
macro='VHost'
|
2022-05-02 16:19:00 -07:00
|
|
|
fi
|
|
|
|
|
2025-02-04 09:48:29 -08:00
|
|
|
if [[ $macro = 'VHost' ]]; then
|
2022-05-02 16:19:00 -07:00
|
|
|
|
|
|
|
if [[ -n $domain ]]; then
|
2025-02-04 09:48:29 -08:00
|
|
|
if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
|
|
|
|
# get single VHostHTTP(S) config for specified domain
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/$domain.conf|grep --extended-regexp "^Use VHostHTTP([S]?) ")
|
|
|
|
configcount=${#configs[@]}
|
|
|
|
if [ "$configcount" -eq 0 ]; then
|
|
|
|
echo "ERROR: $domain is not configured as a VHost"
|
|
|
|
exit 1
|
|
|
|
fi
|
2022-05-02 16:19:00 -07:00
|
|
|
else
|
2025-02-04 09:48:29 -08:00
|
|
|
echo "ERROR: $domain is not configured on this server."
|
2022-05-02 16:19:00 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
2025-02-04 09:48:29 -08:00
|
|
|
# get array of all VHostHTTP(S) configs
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VHostHTTP([S]?) ")
|
2022-05-02 16:19:00 -07:00
|
|
|
fi
|
|
|
|
|
2025-02-04 09:48:29 -08:00
|
|
|
output="domain username macro php status"
|
2022-05-02 16:19:00 -07:00
|
|
|
|
2025-02-04 09:48:29 -08:00
|
|
|
for config in "${configs[@]}"
|
2022-05-02 16:19:00 -07:00
|
|
|
do
|
2025-02-04 09:48:29 -08:00
|
|
|
IFS=' ' read -r -a parts <<< "$config"
|
|
|
|
macro=${parts[1]}
|
|
|
|
domain=${parts[2]}
|
|
|
|
owner=${parts[3]}
|
|
|
|
fpm=${parts[4]}
|
|
|
|
if [[ -f /etc/apache2/sites-enabled/$domain.conf ]]; then
|
2022-05-02 16:19:00 -07:00
|
|
|
status="Enabled"
|
|
|
|
else
|
|
|
|
status="Disabled"
|
|
|
|
fi
|
2025-02-04 09:48:29 -08:00
|
|
|
# filter by username, if it was specified
|
2022-07-18 19:09:40 -07:00
|
|
|
if [[ -n $username ]]; then
|
|
|
|
if [[ $username = $owner ]]; then
|
2025-02-04 09:48:29 -08:00
|
|
|
output="$output${NL}$domain $owner $macro $fpm $status"
|
2022-07-18 19:09:40 -07:00
|
|
|
fi
|
|
|
|
else
|
2025-02-04 09:48:29 -08:00
|
|
|
output="$output${NL}$domain $owner $macro $fpm $status"
|
2022-07-18 19:09:40 -07:00
|
|
|
fi
|
2022-05-02 16:19:00 -07:00
|
|
|
done
|
2025-02-04 09:48:29 -08:00
|
|
|
|
|
|
|
if [[ $cvs ]]; then
|
|
|
|
echo "$output" | tr " " ","
|
|
|
|
else
|
|
|
|
echo "$output" | column -t
|
|
|
|
fi
|
|
|
|
|
|
|
|
elif [[ $macro = 'Redirect' ]]; then
|
|
|
|
|
|
|
|
if [[ -n $domain ]]; then
|
|
|
|
if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
|
|
|
|
# get single RedirectHTTP(S) config for specified domain
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/$domain.conf|grep --extended-regexp "^Use RedirectHTTP([S]?) $domain")
|
|
|
|
configcount=${#configs[@]}
|
|
|
|
if [ "$configcount" -eq 0 ]; then
|
|
|
|
echo "ERROR: $domain is not configured as a Redirect"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "ERROR: $domain is not configured on this server."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [[ -n $option ]]; then
|
|
|
|
# get array of RedirectHTTP(S) configs for specified Redirect URL
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use RedirectHTTP([S]?) .* $option$")
|
|
|
|
else
|
|
|
|
# get array of all RedirectHTTP(S) configs
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use RedirectHTTP([S]?) ")
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
output="domain macro redirect status"
|
|
|
|
|
|
|
|
for config in "${configs[@]}"
|
|
|
|
do
|
|
|
|
IFS=' ' read -r -a parts <<< "$config"
|
|
|
|
macro=${parts[1]}
|
|
|
|
domain=${parts[2]}
|
|
|
|
redirect=${parts[3]}
|
|
|
|
if [[ -f /etc/apache2/sites-enabled/$domain.conf ]]; then
|
|
|
|
status="Enabled"
|
2022-07-18 19:09:40 -07:00
|
|
|
else
|
2025-02-04 09:48:29 -08:00
|
|
|
status="Disabled"
|
2022-07-18 19:09:40 -07:00
|
|
|
fi
|
2025-02-04 09:48:29 -08:00
|
|
|
output="$output${NL}$domain $macro $redirect $status"
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ $cvs ]]; then
|
|
|
|
echo "$output" | tr " " ","
|
|
|
|
else
|
|
|
|
echo "$output" | column -t
|
2022-05-02 16:19:00 -07:00
|
|
|
fi
|
|
|
|
|
2025-02-04 09:48:29 -08:00
|
|
|
elif [[ $macro = 'VHostAlias' ]]; then
|
2022-05-02 16:19:00 -07:00
|
|
|
|
|
|
|
if [[ -n $domain ]]; then
|
2025-02-04 09:48:29 -08:00
|
|
|
if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
|
|
|
|
# get single VHostAliasHTTP(S) config for specified domain
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/$domain.conf|grep --extended-regexp "^Use VHostAliasHTTP([S]?) ")
|
|
|
|
configcount=${#configs[@]}
|
|
|
|
if [ "$configcount" -eq 0 ]; then
|
|
|
|
echo "ERROR: $domain is not configured as a Alias"
|
2022-05-02 16:19:00 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "ERROR: $domain is not configured on this server."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
2025-02-04 09:48:29 -08:00
|
|
|
if [[ -n $option ]]; then
|
|
|
|
# get array of VHostAliasHTTP(S) configs for specified aliased domain
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VHostAliasHTTP([S]?) $option ")
|
|
|
|
else
|
|
|
|
# get array of all VHostAliasHTTP(S) configs
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VHostAliasHTTP([S]?) ")
|
|
|
|
fi
|
2022-05-02 16:19:00 -07:00
|
|
|
fi
|
|
|
|
|
2025-02-04 09:48:29 -08:00
|
|
|
output="domain username macro php alias status"
|
2022-05-02 16:19:00 -07:00
|
|
|
|
2025-02-04 09:48:29 -08:00
|
|
|
for config in "${configs[@]}"
|
2022-05-02 16:19:00 -07:00
|
|
|
do
|
2025-02-04 09:48:29 -08:00
|
|
|
IFS=' ' read -r -a parts <<< "$config"
|
|
|
|
macro=${parts[1]}
|
|
|
|
domain=${parts[2]}
|
|
|
|
owner=${parts[3]}
|
|
|
|
fpm=${parts[4]}
|
|
|
|
alias=${parts[5]}
|
|
|
|
if [[ -f /etc/apache2/sites-enabled/$domain.conf ]]; then
|
|
|
|
status="Enabled"
|
|
|
|
else
|
|
|
|
status="Disabled"
|
|
|
|
fi
|
|
|
|
# filter by username, if it was specified
|
|
|
|
if [[ -n $username ]]; then
|
|
|
|
if [[ $username = $owner ]]; then
|
|
|
|
output="$output${NL}$alias $owner $macro $fpm $domain $status"
|
2022-05-02 16:19:00 -07:00
|
|
|
fi
|
2025-02-04 09:48:29 -08:00
|
|
|
else
|
|
|
|
output="$output${NL}$alias $owner $macro $fpm $domain $status"
|
2022-05-02 16:19:00 -07:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ $cvs ]]; then
|
|
|
|
echo "$output" | tr " " ","
|
|
|
|
else
|
|
|
|
echo "$output" | column -t
|
|
|
|
fi
|
|
|
|
|
2025-02-04 09:48:29 -08:00
|
|
|
elif [[ $macro = 'VHostSubdomain' ]]; then
|
2022-05-02 16:19:00 -07:00
|
|
|
|
|
|
|
if [[ -n $domain ]]; then
|
2025-02-04 09:48:29 -08:00
|
|
|
if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
|
|
|
|
# get single VHostSubdomainHTTP(S) config for specified domain
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/$domain.conf|grep --extended-regexp "^Use VHostSubdomainHTTP([S]?) ")
|
|
|
|
configcount=${#configs[@]}
|
|
|
|
if [ "$configcount" -eq 0 ]; then
|
|
|
|
echo "ERROR: $domain is not configured as a VHostSubdomain"
|
2022-05-02 16:19:00 -07:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "ERROR: $domain is not configured on this server."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
2025-02-04 09:48:29 -08:00
|
|
|
# get array of all VHostSubdomainHTTP(S) configs
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VHostSubdomainHTTP([S]?) ")
|
2022-05-02 16:19:00 -07:00
|
|
|
fi
|
|
|
|
|
2025-02-04 09:48:29 -08:00
|
|
|
output="domain username macro php status"
|
2022-05-02 16:19:00 -07:00
|
|
|
|
2025-02-04 09:48:29 -08:00
|
|
|
for config in "${configs[@]}"
|
2022-05-02 16:19:00 -07:00
|
|
|
do
|
2025-02-04 09:48:29 -08:00
|
|
|
IFS=' ' read -r -a parts <<< "$config"
|
|
|
|
macro=${parts[1]}
|
|
|
|
domain=${parts[5]}.${parts[2]}
|
|
|
|
owner=${parts[3]}
|
|
|
|
fpm=${parts[4]}
|
|
|
|
if [[ -f /etc/apache2/sites-enabled/$domain.conf ]]; then
|
|
|
|
status="Enabled"
|
|
|
|
else
|
|
|
|
status="Disabled"
|
|
|
|
fi
|
|
|
|
# filter by username, if it was specified
|
|
|
|
if [[ -n $username ]]; then
|
|
|
|
if [[ $username = $owner ]]; then
|
|
|
|
output="$output${NL}$domain $owner $macro $fpm $status"
|
2022-05-02 16:19:00 -07:00
|
|
|
fi
|
2025-02-04 09:48:29 -08:00
|
|
|
else
|
|
|
|
output="$output${NL}$domain $owner $macro $fpm $status"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ $cvs ]]; then
|
|
|
|
echo "$output" | tr " " ","
|
|
|
|
else
|
|
|
|
echo "$output" | column -t
|
|
|
|
fi
|
|
|
|
|
|
|
|
elif [[ $macro = 'VMail' ]]; then
|
|
|
|
|
|
|
|
if [[ -n $domain ]]; then
|
|
|
|
if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
|
|
|
|
# get single VMailHTTP(S) config for specified domain
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/$domain.conf|grep --extended-regexp "^Use VMailHTTP([S]?) ")
|
|
|
|
configcount=${#configs[@]}
|
|
|
|
if [ "$configcount" -eq 0 ]; then
|
|
|
|
echo "ERROR: $domain is not configured for VMailHTTP"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "ERROR: $domain is not configured on this server."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [[ -n $option ]]; then
|
|
|
|
# get array of VMailHTTP(S) configs for specified aliased domain
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VMailHTTP([S]?) $option ")
|
|
|
|
else
|
|
|
|
# get array of all VMailHTTP(S) configs
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VMailHTTP([S]?) ")
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
output="domain macro status"
|
|
|
|
|
|
|
|
for config in "${configs[@]}"
|
|
|
|
do
|
|
|
|
IFS=' ' read -r -a parts <<< "$config"
|
|
|
|
macro=${parts[1]}
|
|
|
|
domain=${parts[2]}
|
|
|
|
if [[ -f /etc/apache2/sites-enabled/$domain.conf ]]; then
|
|
|
|
status="Enabled"
|
|
|
|
else
|
|
|
|
status="Disabled"
|
2022-05-02 16:19:00 -07:00
|
|
|
fi
|
2025-02-04 09:48:29 -08:00
|
|
|
output="$output${NL}$domain $macro $status"
|
2022-05-02 16:19:00 -07:00
|
|
|
done
|
|
|
|
|
|
|
|
if [[ $cvs ]]; then
|
|
|
|
echo "$output" | tr " " ","
|
|
|
|
else
|
|
|
|
echo "$output" | column -t
|
|
|
|
fi
|
|
|
|
|
2025-02-04 09:48:29 -08:00
|
|
|
elif [[ $macro = 'Mailman3' ]]; then
|
|
|
|
|
|
|
|
if [[ -n $domain ]]; then
|
|
|
|
if [[ -f /etc/apache2/sites-available/$domain.conf ]]; then
|
|
|
|
# get single Mailman3HTTP(S) config for specified domain
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/$domain.conf|grep --extended-regexp "^Use Mailman3HTTP([S]?) ")
|
|
|
|
configcount=${#configs[@]}
|
|
|
|
if [ "$configcount" -eq 0 ]; then
|
|
|
|
echo "ERROR: $domain is not configured for Mailman3"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "ERROR: $domain is not configured on this server."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [[ -n $option ]]; then
|
|
|
|
# get array of Mailman3HTTP(S) configs for specified aliased domain
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use Mailman3HTTP([S]?) $option ")
|
|
|
|
else
|
|
|
|
# get array of all VMailHTTP(S) configs
|
|
|
|
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use Mailman3HTTP([S]?) ")
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
output="domain macro status"
|
|
|
|
|
|
|
|
for config in "${configs[@]}"
|
|
|
|
do
|
|
|
|
IFS=' ' read -r -a parts <<< "$config"
|
|
|
|
macro=${parts[1]}
|
|
|
|
domain=${parts[2]}
|
|
|
|
if [[ -f /etc/apache2/sites-enabled/$domain.conf ]]; then
|
|
|
|
status="Enabled"
|
|
|
|
else
|
|
|
|
status="Disabled"
|
|
|
|
fi
|
|
|
|
output="$output${NL}$domain $macro $status"
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ $cvs ]]; then
|
|
|
|
echo "$output" | tr " " ","
|
|
|
|
else
|
|
|
|
echo "$output" | column -t
|
|
|
|
fi
|
2022-05-02 16:19:00 -07:00
|
|
|
else
|
2025-02-04 09:48:29 -08:00
|
|
|
echo "ERROR: Invalid macro '$macro'"
|
2022-05-02 16:19:00 -07:00
|
|
|
exit 1
|
|
|
|
fi
|