vhost-stack/bin/vhost-get.sh

339 lines
10 KiB
Bash
Executable File

#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# 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)
# load include file
source $(dirname $0)/vhost.sh
help()
{
thisfilename=$(basename -- "$0")
echo "Get VirtualHost info."
echo ""
echo "usage: $thisfilename [-d <domain>] [-u <username>] [-m <macro>] [-o <option>] [-c] [-h]"
echo ""
echo " -h Print this help."
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."
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."
echo " -c CVS - Output in cvs format, instead of tabbed table."
}
vhost:getoptions "$@"
# create newline var
NL=$'\n'
# check for macro
if [[ -z $macro ]]; then
macro='VHost'
fi
if [[ $macro = 'VHost' ]]; then
if [[ -n $domain ]]; then
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
else
echo "ERROR: $domain is not configured on this server."
exit 1
fi
else
# get array of all VHostHTTP(S) configs
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VHostHTTP([S]?) ")
fi
output="domain username macro php status"
for config in "${configs[@]}"
do
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
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"
fi
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 = '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"
else
status="Disabled"
fi
output="$output${NL}$domain $macro $redirect $status"
done
if [[ $cvs ]]; then
echo "$output" | tr " " ","
else
echo "$output" | column -t
fi
elif [[ $macro = 'VHostAlias' ]]; then
if [[ -n $domain ]]; then
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"
exit 1
fi
else
echo "ERROR: $domain is not configured on this server."
exit 1
fi
else
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
fi
output="domain username macro php alias status"
for config in "${configs[@]}"
do
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"
fi
else
output="$output${NL}$alias $owner $macro $fpm $domain $status"
fi
done
if [[ $cvs ]]; then
echo "$output" | tr " " ","
else
echo "$output" | column -t
fi
elif [[ $macro = 'VHostSubdomain' ]]; then
if [[ -n $domain ]]; then
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"
exit 1
fi
else
echo "ERROR: $domain is not configured on this server."
exit 1
fi
else
# get array of all VHostSubdomainHTTP(S) configs
readarray -t configs < <(head -n 1 /etc/apache2/sites-available/*.conf|grep --extended-regexp "^Use VHostSubdomainHTTP([S]?) ")
fi
output="domain username macro php status"
for config in "${configs[@]}"
do
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"
fi
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"
fi
output="$output${NL}$domain $macro $status"
done
if [[ $cvs ]]; then
echo "$output" | tr " " ","
else
echo "$output" | column -t
fi
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
else
echo "ERROR: Invalid macro '$macro'"
exit 1
fi