[tpwd] Fix segfault when exactly one argument given
[tinyapps.git] / virtman.sh
blobbdfd93f991c904544aff0aeb54e4f82575d14524
1 #!/bin/sh
2 ##
3 ## virtman.sh - Adds virtual hosts to Apache configuration file
4 ## Copyright 2006 by Michal Nazarewicz <mina86@mina86.com>
5 ##
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/
25 ## Defaults
27 set -e
29 HOST="$(hostname -f)"
30 IP='*'
31 PORT=80
32 PUBLIC_HTML=public_html
33 HTTPD_CONF=auto
34 LOGDIR=log
36 ## Zero args
37 PREFIX=
38 SERVERNAME=
39 DOCROOT=
40 FULL_LOGDIR=
42 ## Helper
43 err () { __FMT="$1"; shift; printf "%s: $__FMT\n" "${0##*/}" "$@" >&2; }
47 ## Usage
49 if [ X"$1" = X--help ] || [ $# -eq 0 ]; then
50 cat <<EOF
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.
66 EOF
67 exit
72 ## Parse args
74 while [ X"${1#-}" != X"$1" ] && [ X"$1" != X-- ]; do
76 ## Split to arg and option
77 if [ -z "${1#-?}" ]; then
78 if [ $# -eq 1 ]; then
79 err 'argument missing for %s' "$1"
80 exit 1
82 OPT="${1#-}"
83 ARG="$2"
84 shift 2
85 else
86 ARG="${1#??}"
87 OPT="${1%$ARG}"
88 OPT="${OPT#-}"
89 shift
92 ## Check option
93 case "$OPT" in
94 (c) HTTPD_CONF="$ARG";;
95 (h) HOST="$ARG";;
96 (v) PREFIX="$ARG";;
97 (V) SERVERNAME="$ARG";;
98 (d) PUBLIC_HTML="$ARG";;
99 (D) DOCROOT="$ARG";;
100 (l) LOGDIR="$ARG";;
101 (L) FULL_LOGDIR="$ARG";;
103 I="${ARG%:*}"
104 P="${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"
110 exit 1
112 esac
113 done
114 if [ X"$1" = X-- ]; then shift; fi
118 ## Validate number of users
120 if [ $# -eq 0 ]; then
121 err 'user name required'
122 exit 2
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'
126 exit 2
131 ## Find httpd.conf
133 if [ -z "$HTTPD_CONF" ] || [ X"$HTTPD_CONF" = Xauto ]; then
134 HTTPD_CONF=
135 for DIR in /etc/apache /etc/apache2 /etc \
136 /usr/apache/etc /usr/apache2/etc \
137 /usr/local/apache/etc /usr/local/apache2/etc; do
138 if [ -f "$DIR/httpd.conf" ]; then
139 HTTPD_CONF="$DIR/httpd.conf"
140 err 'found httpd.conf in "%s"' "$HTTPD_CONF"
141 break
143 done
144 if [ -z "$HTTPD_CONF" ]; then
145 printf 'httpd.conf not found'
146 exit 3
152 ## Add
154 IP="$IP:$PORT"
156 for USR; do
157 echo "Adding virtual host for $USR"
159 if [ -n "$SERVERNAME" ]
160 then SN="$SERVERNAME"
161 elif [ -z "$PREFIX" ]
162 then SN="$USR.$HOST"
163 else SN="$PREFIX.$HOST"
166 if [ -n "$DOCROOT" ]
167 then DR="$DOCROOT"
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"
180 LS="${SN%$HOST}"
181 LS="${LS%.}"
183 cat <<EOF >>"$HTTPD_CONF"
185 ## Added by VirtMan
186 <VirtualHost $IP>
187 ServerName "$SN"
188 ServerAlias "www.$SN"
189 DocumentRoot "$DR"
190 ServerAdmin "$USR@$HOST"
191 ErrorLog "$LD/$LS-error_log"
192 CustomLog "$LD/$LS-access_log" common
193 </VirtualHost>
196 done