2 # linesync.sh, Copyright (c) 2002 Arjen Wolfs
3 # 20020604, sengaia@undernet.org
4 # 20050417, daniel@undernet.org - modified for u2.10.12
6 # The code contained is in this file is licenced under the terms
7 # and conditions as specified in the GNU General Public License.
9 # linesync.sh - centralized ircd.conf updates.
10 # The purpose of this little shell script is to allow a section of an ircd.conf to be
11 # updated from a central location. Hence it is intended to facilitate the automated
12 # distribution of Kill, Jupe, Quarantine and Uworld lines; or any other .conf lines you
13 # may wish to keep synchronized accross all servers on a network.
15 # This script will download a file called linesync from a specified web server (see
16 # below for configuration), and calculate an md5sum from it. It will then download
17 # a file called linesync.sum from a configurable number of other web servers and
18 # compare the contents of these files against the checksum calculated. If any of the
19 # downloaded checksums mismatch, the program will abort. This provides security to
20 # the centralized update mechanism - in order for it to be compromised, multiple
21 # web servers would have to compromised.
23 # If all checksums match, the script inspects the .conf lines contained within the
24 # downloaded file. If any .conf lines are found that are not specifically allowed,
25 # the program will abort. This will prevent malicious/dangerous .conf lines (such as
26 # Operator or Connect lines) from being inserted into ircd.conf.
28 # If all the checks mentioned above are passed, the script checks ircd.conf for a section
29 # that begins with "# BEGIN LINESYNC", and ends with "# END LINESYNC". The section contained
30 # between these two comments is the section maintained by this program. If these lines are not
31 # found in the ircd.conf, they will be appended to it.
32 # Next, the script will build a new ircd.conf by removing all lines present between the two
33 # commented lines mentioned above, and replace them with the contents of the file downloaded.
35 # Once this has been completed, ircd.conf is backed up and replaced with the newly built version,
36 # and ircd will be rehashed.
38 # Configuration: This script requires two parameters - the full path to your ircd.conf, and the
39 # full path to your ircd.pid. It will look for a configuration file called linesync.conf in the
40 # same directory as ircd.conf. See the included sample linesync.conf for information on how to
41 # set it up. Obviously, you will need to have web server(s) to use for the distribution of your
42 # .conf update and checksums. This script requires the presence of wget and md5sum, and various
43 # other programs that should be present by default on any Unix system.
45 # This program should be run from crontab, i.e something like:
46 # 0 0 * * * /home/irc/bin/linesync.sh /home/irc/lib/ircd.conf /home/irc/lib/ircd.pid
48 # This program has been tested on and works on FreeBSD, Solaris, and Linux.
49 # md5sum is included in GNU textutils.
52 # Arjen Wolfs (sengaia@undernet.org), June 9 2002.
55 # This checks for the presence of an executable file in $PATH
57 if [ ! -x "`which $1 2>&1`" ]; then
58 echo "You don't seem to have $1. Sorry."
63 # This checks for the presence of any file
65 if [ ! -f "$1" ]; then
66 echo "There doesn't appear to be a $1. Sorry."
71 # Try to find programs we will need
72 locate_program wget && locate_program egrep && locate_program diff
80 # try to find an appropriate md5 program
81 # BSD md5 capability courtesy of spale
82 md5_cmd=`which md5sum`
83 if [ -z "$md5_cmd" ]; then
85 if [ -z "$md5_cmd" ]; then
86 echo "No MD5 capable programs found (I looked for md5sum and md5)."
93 if [ -z "$awk_cmd" ]; then
95 is_gawk=`echo | awk --version | head -1 | egrep '^GNU.+$'`
96 if [ -z "$is_gawk" ]; then
97 echo "Your version of awk is not GNU awk. Sorry."
103 # Check for required command line parameters
104 if [ -z "$1" -o -z "$2" ]; then
105 echo "Usage: $0 <conf_path> <pid_path>"
106 echo " <conf_path> Full path to ircd.conf (/home/irc/lib/ircd.conf)"
107 echo " <pid_path> Full path to ircd.pid (/home/irc/lib/ircd.pid)"
111 # check and set up stuff
116 dpath=`dirname $cpath`
117 lpath="$dpath/linesync.conf"
119 save_dir=$PWD; cd $dpath
120 tpath=$PWD; cd $save_dir
121 tmp_path="$dpath/tmp"
122 mkdir $tmp_path > /dev/null 2>&1
124 # load and check configuration
126 if [ -z "$LINE_SERVER" -o -z "$LINE_CHECK" -o -z "$ALLOWED_LINES" ]; then
127 echo "Please setup $lpath correctly."
131 # Not all versions of date support %s, work around it
132 TS=`date +%Y%m%d%H%M%S`
133 TMPFILE="$tmp_path/linesync.$TS"
134 LSFILE="$LINE_SERVER""linesync"
135 # Attempt to download our .conf update
136 wget --cache=off --quiet --output-document=$TMPFILE $LSFILE > /dev/null 2>&1
137 if [ ! -s "$TMPFILE" ]; then
138 echo "Unable to retrieve $LSFILE. Sorry."
139 rm $TMPFILE > /dev/null 2>&1
143 # Check wether the file contains any disallowed .conf lines
144 bad_lines=`egrep '^[^'$ALLOWED_LINES'|#]+' $TMPFILE`
145 if [ ! -z "$bad_lines" ]; then
146 echo "The file downloaded in $TMPFILE contains the following disallowed line(s):"
151 # check our ircd.conf
152 ircd_setup=`egrep '^# (BEGIN|END) LINESYNC$' $cpath|wc -l`
153 if [ $ircd_setup != 2 ]; then
154 cp $cpath $cpath.orig
155 echo "Performing initial merge on $cpath, original file saved as $cpath.orig."
157 echo "# Do NOT remove the following line, linesync.sh depends on it!" >> $cpath
158 echo "# BEGIN LINESYNC" >> $cpath
159 echo "# END LINESYNC" >> $cpath
160 echo "# Do not remove the previous line, linesync.sh depends on it!" >> $cpath
162 # Do an initial merge to remove duplicates
163 inpath="$tmp_path/linesync.tmp.$TS"
166 if (!loaded_template) {
167 command="cat " tempfile; tlines=0;
168 while ((command | getline avar) > 0) { template[tlines]=avar; tlines++ }
173 for (i=0; i<tlines; i++) {
174 if (tolower($0)==tolower(template[i])) { dup_line++; break }
176 if (!dup_line) print $0
177 } ' tempfile=$TMPFILE < $cpath > $inpath
183 CKSUM=`$md5_cmd $TMPFILE|cut -d' ' -f1`
185 check_file="$tmp_path/linesync.sum.$TS"
186 for ck_server in $LINE_CHECK; do
187 sumfile="$ck_server""linesync.sum"
188 wget --cache=off --quiet --output-document=$check_file $sumfile > /dev/null 2>&1
189 if [ ! -s "$check_file" ]; then
190 echo "Unable to retrieve checksum from $sumfile"
193 if [ "$CKSUM" != "`cat $check_file`" ]; then
194 echo "Checksum retrieved from $sumfile does not match!"
199 # It all checks out, proceed...
201 # Replace the marked block in ircd.conf with the new version
204 $0=="# BEGIN LINESYNC" { chop++; print; next }
205 $0=="# END LINESYNC" {
206 command="cat " syncfile
207 while ((command | getline avar) > 0) { print avar }
211 { if (!chop) print $0 }
212 ' syncfile=$TMPFILE < $inpath > $tmp_path/linesync.new.$TS
214 # run a diff between current and new confs to see if we updated anything
215 # no point sending the ircd a -HUP if this is not needed, especially on a
216 # busy network, such as Undernet.
217 diff=`$diff_cmd $cpath $tmp_path/linesync.new.$TS`
218 if [ ! -z "$diff" ]; then
219 # Changes were detected
221 # Back up the current ircd.conf and replace it with the new one
222 cp $cpath $dpath/ircd.conf.bk
223 cp $tmp_path/linesync.new.$TS $cpath
225 # Rehash ircd (without caring wether or not it succeeds)
226 kill -HUP `cat $ppath 2>/dev/null` > /dev/null 2>&1
230 rm -rf $tmp_path > /dev/null 2>&1