2 # linesync.sh, Copyright (c) 2002 Arjen Wolfs
3 # 20020604, sengaia@undernet.org
5 # The code contained is in this file is licenced under the terms
6 # and conditions as specified in the GNU General Public License.
8 # linesync.sh - centralized ircd.conf updates.
9 # The purpose of this little shell script is to allow a section of an ircd.conf to be
10 # updated from a central location. Hence it is intended to facilitate the automated
11 # distribution of k, K, Q, and U lines; or any other .conf lines you may wish to keep
12 # synchronized accross all servers on a network.
14 # This script will download a file called linesync from a specified web server (see
15 # below for configuration), and calculate an md5sum from it. It will then download
16 # a file called linesync.sum from a configurable number of other web servers and
17 # compare the contents of these files against the checksum calculated. If any of the
18 # downloaded checksums mismatch, the program will abort. This provides security to
19 # the centralized update mechanism - in order for it to be compromised, multiple
20 # web servers would have to compromised.
22 # If all checksums match, the script inspects the .conf lines contained within the
23 # downloaded file. If any .conf lines are found that are not specifically allowed,
24 # the program will abort. This will prevent malicious/dangerous .conf lines (such as
25 # O: or C: lines) from being inserted into ircd.conf.
27 # If all the checks mentioned above are passed, the script checks ircd.conf for a section
28 # that begins with "# BEGIN LINESYNC", and ends with "# END LINESYNC". The section contained
29 # between these two comments is the section maintained by this program. If these lines are not
30 # found in the ircd.conf, they will be appended to it.
31 # Next, the script will build a new ircd.conf by removing all lines present between the two
32 # commented lines mentioned above, and replace them with the contents of the file downloaded.
34 # Once this has been completed, ircd.conf is backed up and replaced with the newly built version,
35 # and ircd will be rehashed.
37 # Configuration: This script requires two parameters - the full path to your ircd.conf, and the
38 # full path to your ircd.pid. It will look for a configuration file called linesync.conf in the
39 # same directory as ircd.conf. See the included sample linesync.conf for information on how to
40 # set it up. Obviously, you will need to have web server(s) to use for the distribution of your
41 # .conf update and checksums. This script requires the presence of wget and md5sum, and various
42 # other programs that should be present by default on any Unix system.
44 # This program should be run from crontab, i.e something like:
45 # 0 0 * * * /home/irc/bin/linesync.sh /home/irc/lib/ircd.conf /home/irc/lib/ircd.pid
47 # This program has been tested on and works on FreeBSD, Solaris, and Linux.
48 # md5sum is included in GNU textutils.
51 # Arjen Wolfs (sengaia@undernet.org), June 9 2002.
54 # This checks for the presence of an executable file in $PATH
56 if [ ! -x "`which $1 2>&1`" ]; then
57 echo "You don't seem to have $1. Sorry."
62 # This checks for the presence of any file
64 if [ ! -f "$1" ]; then
65 echo "There doesn't appear to be a $1. Sorry."
70 # Try to find programs we will need
71 locate_program wget && locate_program egrep
79 # try to find an appropriate md5 program
80 # BSD md5 capability courtesy of spale
81 md5_cmd=`which md5sum`
82 if [ -z "$md5_cmd" ]; then
84 if [ -z "$md5_cmd" ]; then
85 echo "No MD5 capable programs found (I looked for md5sum and md5)."
92 if [ -z "$awk_cmd" ]; then
94 is_gawk=`echo | awk --version | head -1 | egrep '^GNU.+$'`
95 if [ -z "$is_gawk" ]; then
96 echo "Your version of awk is not GNU awk. Sorry."
102 # Check for required command line parameters
103 if [ -z "$1" -o -z "$2" ]; then
104 echo "Usage: $0 <conf_path> <pid_path>"
105 echo " <conf_path> Full path to ircd.conf (/home/irc/lib/ircd.conf)"
106 echo " <pid_path> Full path to ircd.pid (/home/irc/lib/ircd.pid)"
110 # check and set up stuff
114 dpath=`dirname $cpath`
115 lpath="$dpath/linesync.conf"
117 save_dir=$PWD; cd $dpath
118 tpath=$PWD; cd $save_dir
119 tmp_path="$dpath/tmp"
120 mkdir $tmp_path > /dev/null 2>&1
122 # load and check configuration
124 if [ -z "$LINE_SERVER" -o -z "$LINE_CHECK" -o -z "$ALLOWED_LINES" ]; then
125 echo "Please setup $lpath correctly."
129 # Not all versions of date support %s, work around it
130 TS=`date +%Y%m%d%H%M%S`
131 TMPFILE="$tmp_path/linesync.$TS"
132 LSFILE="$LINE_SERVER""linesync"
133 # Attempt to download our .conf update
134 wget --cache=off --quiet --output-document=$TMPFILE $LSFILE > /dev/null 2>&1
135 if [ ! -s "$TMPFILE" ]; then
136 echo "Unable to retrieve $LSFILE. Sorry."
137 rm $TMPFILE > /dev/null 2>&1
141 # Check wether the file contains any disallowed .conf lines
142 bad_lines=`egrep '^[^'$ALLOWED_LINES'#]+' $TMPFILE`
143 if [ ! -z "$bad_lines" ]; then
144 echo "The file downloaded in $TMPFILE contains the following disallowed line(s):"
149 # check our ircd.conf
150 ircd_setup=`egrep '^# (BEGIN|END) LINESYNC$' $cpath|wc -l`
151 if [ $ircd_setup != 2 ]; then
152 cp $cpath $cpath.orig
153 echo "Performing initial merge on $cpath, original file saved as $cpath.orig."
155 echo "# Do NOT remove the following line, linesync.sh depends on it!" >> $cpath
156 echo "# BEGIN LINESYNC" >> $cpath
157 echo "# END LINESYNC" >> $cpath
158 echo "# Do not remove the previous line, linesync.sh depends on it!" >> $cpath
160 # Do an initial merge to remove duplicates
161 inpath="$tmp_path/linesync.tmp.$TS"
164 if (!loaded_template) {
165 command="cat " tempfile; tlines=0;
166 while ((command | getline avar) > 0) { template[tlines]=avar; tlines++ }
171 for (i=0; i<tlines; i++) {
172 if (tolower($0)==tolower(template[i])) { dup_line++; break }
174 if (!dup_line) print $0
175 } ' tempfile=$TMPFILE < $cpath > $inpath
181 CKSUM=`$md5_cmd $TMPFILE|cut -d' ' -f1`
183 check_file="$tmp_path/linesync.sum.$TS"
184 for ck_server in $LINE_CHECK; do
185 sumfile="$ck_server""linesync.sum"
186 wget --cache=off --quiet --output-document=$check_file $sumfile > /dev/null 2>&1
187 if [ ! -s "$check_file" ]; then
188 echo "Unable to retrieve checksum from $sumfile"
191 if [ "$CKSUM" != "`cat $check_file`" ]; then
192 echo "Checksum retrieved from $sumfile does not match!"
197 # It all checks out, proceed...
199 # Replace the marked block in ircd.conf with the new version
202 $0=="# BEGIN LINESYNC" { chop++; print; next }
203 $0=="# END LINESYNC" {
204 command="cat " syncfile
205 while ((command | getline avar) > 0) { print avar }
209 { if (!chop) print $0 }
210 ' syncfile=$TMPFILE < $inpath > $tmp_path/linesync.new.$TS
212 # Back up the current ircd.conf and replace it with the new one
213 cp $cpath $dpath/ircd.conf.bk
214 cp $tmp_path/linesync.new.$TS $cpath
216 # Rehash ircd (without caring wether or not it succeeds)
217 kill -HUP `cat $ppath 2>/dev/null` > /dev/null 2>&1
220 rm -rf $tmp_path > /dev/null 2>&1