3 ## virtman.sh - Adds virtual hosts to Apache configuration file
4 ## Copyright 2006 by Michal Nazarewicz <mina86@mina86.com>
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
19 ## This is part of Tiny Applications Collection
20 ## -> http://tinyapps.sourceforge.net/
32 PUBLIC_HTML
=public_html
43 err
() { __FMT
="$1"; shift; printf "%s: $__FMT\n" "${0##*/}" "$@" >&2; }
49 if [ X
"$1" = X--help
] ||
[ $# -eq 0 ]; then
51 usage: virtman.sh [ -c httpd.conf ] [ -b ip:port ] [ -h host ] [ -v prefix ]
52 [ -V servername ] [ -d public_html ] [ -D docroot ]
53 [ -- ] user [ user ... ]
55 -c http.conf path to httpd.conf [auto]
56 -b ip:port an IP and port to listen to [$IP:$PORT]
57 -h host hostname to suffix user name [$HOST]
58 -v prefix prefix added to host name [same as user]
59 -V servername full server name (overwrites -h and -v)
60 -d public_html name of user's document root [$PUBLIC_HTML]
61 -D docroot full path to document root (overwrites -d)
62 -l logdir name of logdir in user's directory [$LOGDIR]
63 -L logdir full path to log directory (overwrites -l)
64 user username to add virutal host for. May be used several
65 times if none of -v, -V, -D and -L were given.
74 while [ X
"${1#-}" != X
"$1" ] && [ X
"$1" != X--
]; do
76 ## Split to arg and option
77 if [ -z "${1#-?}" ]; then
79 err
'argument missing for %s' "$1"
94 (c
) HTTPD_CONF
="$ARG";;
97 (V
) SERVERNAME
="$ARG";;
98 (d
) PUBLIC_HTML
="$ARG";;
101 (L
) FULL_LOGDIR
="$ARG";;
105 if [ -n "$I" ]; then IP
="$I"; fi
106 if [ X
"$P" != X
"$ARG" ]; then PORT
="$P"; fi
109 err
'invalid option -%s' "$OPT"
114 if [ X
"$1" = X--
]; then shift; fi
118 ## Validate number of users
120 if [ $# -eq 0 ]; then
121 err
'user name required'
124 if [ $# -gt 1 ] && [ -n "$SERVERNAME$DOCROOT$FULL_LOGDIR$PREFIX" ]; then
125 err
'several user names cannot be used with -v, -V, -D or -L'
133 if [ -z "$HTTPD_CONF" ] ||
[ X
"$HTTPD_CONF" = Xauto
]; then
135 for DIR
in /etc
/apache
/etc
/apache2
/etc \
136 /usr
/apache
/etc
/usr
/apache
2/etc \
137 /usr
/local
/apache
/etc
/usr
/local
/apache
2/etc
; do
138 if [ -f "$DIR/httpd.conf" ]; then
139 HTTPD_CONF
="$DIR/httpd.conf"
140 err
'found httpd.conf in "%s"' "$HTTPD_CONF"
144 if [ -z "$HTTPD_CONF" ]; then
145 printf 'httpd.conf not found'
157 echo "Adding virtual host for $USR"
159 if [ -n "$SERVERNAME" ]
160 then SN
="$SERVERNAME"
161 elif [ -z "$PREFIX" ]
163 else SN
="$PREFIX.$HOST"
168 elif HM
="$(grep ^mina86: /etc/passwd |cut -d: -f6)"
169 then DR
="$HM/$PUBLIC_HTML"
170 else DR
="/home/$USR/$PUBLIC_HTML"
173 if [ -n "$FULL_LOGDIR" ]
174 then LD
="$FULL_LOGDIR"
175 elif HM
="$(grep ^mina86: /etc/passwd |cut -d: -f6)"
176 then LD
="$HM/$LOGDIR";
177 else LD
="/home/$USR/$LOGDIR"
183 cat <<EOF >>"$HTTPD_CONF"
188 ServerAlias "www.$SN"
190 ServerAdmin "$USR@$HOST"
191 ErrorLog "$LD/$LS-error_log"
192 CustomLog "$LD/$LS-access_log" common