#!/bin/bash # # vhost-stack # https://git.stack-source.com/msb/vhost-stack # Copyright (c) 2022 Matthew Saunders Brown # 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 ] [-u ] [-o ] [-c] [-h]" echo "" echo " -h Print this help." echo " -d Domain name to get info for. By default all domains are returned." echo " -u Only return domains owned by specified username." echo " -o 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 output="virtualhost username config status" for v in "${virtualhostArray[@]}" do owner=$(stat -c '%U' /srv/www/$v) 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 if [[ -n $username ]]; then if [[ $username = $owner ]]; then output="$output${NL}$v $owner $macro $status" fi else output="$output${NL}$v $owner $macro $status" fi done if [[ $output != "virtualhost username config status" ]]; then if [[ $cvs ]]; then echo "$output" | tr " " "," else echo "$output" | column -t fi 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 output="virtualhost config redirect status" 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 output="virtualhost username macro alias status" 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) owner=$(head -n 1 /etc/apache2/sites-available/$c |grep '^Use VHostAlias'|cut -d ' ' -f 4) 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 output="$output${NL}$alias $owner $macro $virtualhost $status" fi done if [[ $cvs ]]; then echo "$output" | tr " " "," else echo "$output" | column -t fi else echo "ERROR: Invalid mode '$option'" exit 1 fi