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 ""
|
2022-07-18 19:09:40 -07:00
|
|
|
echo "usage: $thisfilename [-d <domain>] [-u <username>] [-o <mode>] [-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."
|
2022-05-02 16:19:00 -07:00
|
|
|
echo " -o <mode> Type of query. 'virtualhosts' (default), 'redirects', 'aliases'."
|
|
|
|
echo " -c CVS - Output in cvs format, instead of tabbed table."
|
|
|
|
}
|
|
|
|
|
|
|
|
vhost:getoptions "$@"
|
|
|
|
|
|
|
|
# create newline var
|
|
|
|
NL=$'\n'
|
|
|
|
|
|
|
|
# check for option (mode or type of query)
|
|
|
|
if [[ -z $option ]]; then
|
|
|
|
option='virtualhosts'
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $option = 'virtualhosts' ]]; then
|
|
|
|
|
|
|
|
if [[ -n $domain ]]; then
|
|
|
|
if [[ -d /srv/www/$domain ]]; then
|
|
|
|
virtualhostArray=($domain)
|
|
|
|
else
|
|
|
|
echo "ERROR: $domain not found"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
vhost::set-virtualhostArray
|
|
|
|
fi
|
|
|
|
|
2024-09-18 12:35:55 -07:00
|
|
|
output="virtualhost username config php status"
|
2022-05-02 16:19:00 -07:00
|
|
|
|
|
|
|
for v in "${virtualhostArray[@]}"
|
|
|
|
do
|
2022-07-18 19:09:40 -07:00
|
|
|
owner=$(stat -c '%U' /srv/www/$v)
|
2024-09-18 12:35:55 -07:00
|
|
|
fpm=$(head -n 1 /etc/apache2/sites-available/$v.conf |grep ^Use|cut -d ' ' -f 5)
|
2022-05-02 16:19:00 -07:00
|
|
|
if [[ -f /etc/apache2/sites-available/$v.conf ]]; then
|
|
|
|
macro="Custom"
|
|
|
|
if head -n 1 /etc/apache2/sites-available/$v.conf |grep --quiet ^Use; then
|
|
|
|
macro=$(head -n 1 /etc/apache2/sites-available/$v.conf |grep ^Use|cut -d ' ' -f 2)
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
macro="None"
|
|
|
|
fi
|
|
|
|
if [[ -f /etc/apache2/sites-enabled/$v.conf ]]; then
|
|
|
|
status="Enabled"
|
|
|
|
else
|
|
|
|
status="Disabled"
|
|
|
|
fi
|
2022-07-18 19:09:40 -07:00
|
|
|
if [[ -n $username ]]; then
|
|
|
|
if [[ $username = $owner ]]; then
|
2024-09-18 12:35:55 -07:00
|
|
|
output="$output${NL}$v $owner $macro $fpm $status"
|
2022-07-18 19:09:40 -07:00
|
|
|
fi
|
|
|
|
else
|
2024-09-18 12:35:55 -07:00
|
|
|
output="$output${NL}$v $owner $macro $fpm $status"
|
2022-07-18 19:09:40 -07:00
|
|
|
fi
|
2022-05-02 16:19:00 -07:00
|
|
|
done
|
2024-09-18 12:35:55 -07:00
|
|
|
if [[ $output != "virtualhost username config php status" ]]; then
|
2022-07-18 19:09:40 -07:00
|
|
|
if [[ $cvs ]]; then
|
|
|
|
echo "$output" | tr " " ","
|
|
|
|
else
|
|
|
|
echo "$output" | column -t
|
|
|
|
fi
|
2022-05-02 16:19:00 -07:00
|
|
|
fi
|
|
|
|
elif [[ $option = 'redirects' ]]; then
|
|
|
|
|
|
|
|
# working dir for redirect configs
|
|
|
|
cd /etc/apache2/sites-available
|
|
|
|
|
|
|
|
if [[ -n $domain ]]; then
|
|
|
|
if [[ -f $domain.conf ]]; then
|
|
|
|
if head -n 1 $domain.conf |grep --quiet '^Use Redirect'; then
|
|
|
|
vhostConfigs=("$domain.conf")
|
|
|
|
else
|
|
|
|
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
|
|
|
|
vhostConfigs=(`ls -1`)
|
|
|
|
fi
|
|
|
|
|
2022-07-18 19:09:40 -07:00
|
|
|
output="virtualhost config redirect status"
|
2022-05-02 16:19:00 -07:00
|
|
|
|
|
|
|
for c in "${vhostConfigs[@]}"
|
|
|
|
do
|
|
|
|
if head -n 1 /etc/apache2/sites-available/$c |grep --quiet '^Use Redirect'; then
|
|
|
|
domain=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use Redirect'|cut -d ' ' -f 3)
|
|
|
|
macro=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use Redirect'|cut -d ' ' -f 2)
|
|
|
|
redirect=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use Redirect'|cut -d ' ' -f 4)
|
|
|
|
if [[ -f /etc/apache2/sites-enabled/$c ]]; then
|
|
|
|
status="Enabled"
|
|
|
|
else
|
|
|
|
status="Disabled"
|
|
|
|
fi
|
|
|
|
output="$output${NL}$domain $macro $redirect $status"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ $cvs ]]; then
|
|
|
|
echo "$output" | tr " " ","
|
|
|
|
else
|
|
|
|
echo "$output" | column -t
|
|
|
|
fi
|
|
|
|
|
|
|
|
elif [[ $option = 'aliases' ]]; then
|
|
|
|
|
|
|
|
# working dir for aliases configs
|
|
|
|
cd /etc/apache2/sites-available
|
|
|
|
|
|
|
|
if [[ -n $domain ]]; then
|
|
|
|
if [[ -f $domain.conf ]]; then
|
|
|
|
if head -n 1 $domain.conf |grep --quiet '^Use VHostAlias'; then
|
|
|
|
vhostConfigs=("$domain.conf")
|
|
|
|
else
|
|
|
|
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
|
|
|
|
vhostConfigs=(`ls -1`)
|
|
|
|
fi
|
|
|
|
|
2022-07-18 19:09:40 -07:00
|
|
|
output="virtualhost username macro alias status"
|
2022-05-02 16:19:00 -07:00
|
|
|
|
|
|
|
for c in "${vhostConfigs[@]}"
|
|
|
|
do
|
|
|
|
if head -n 1 /etc/apache2/sites-available/$c |grep --quiet '^Use VHostAlias'; then
|
|
|
|
macro=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use VHostAlias'|cut -d ' ' -f 2)
|
|
|
|
alias=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use VHostAlias'|cut -d ' ' -f 3)
|
2022-07-18 19:09:40 -07:00
|
|
|
owner=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use VHostAlias'|cut -d ' ' -f 4)
|
2022-05-02 16:19:00 -07:00
|
|
|
virtualhost=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use VHostAlias'|cut -d ' ' -f 5)
|
|
|
|
if [[ -f /etc/apache2/sites-enabled/$c ]]; then
|
|
|
|
status="Enabled"
|
|
|
|
else
|
|
|
|
status="Disabled"
|
|
|
|
fi
|
2022-07-18 19:09:40 -07:00
|
|
|
output="$output${NL}$alias $owner $macro $virtualhost $status"
|
2022-05-02 16:19:00 -07:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ $cvs ]]; then
|
|
|
|
echo "$output" | tr " " ","
|
|
|
|
else
|
|
|
|
echo "$output" | column -t
|
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
echo "ERROR: Invalid mode '$option'"
|
|
|
|
exit 1
|
|
|
|
fi
|