home *** CD-ROM | disk | FTP | other *** search
/ ftp.ac-grenoble.fr / 2015.02.ftp.ac-grenoble.fr.tar / ftp.ac-grenoble.fr / pub / slis / updates_rsync / monitor_v2 < prev    next >
Text File  |  2006-04-06  |  10KB  |  276 lines

  1. #!/bin/bash
  2. # $Id: monitor_v2,v 1.2 2005/04/20 08:27:40 bzizou Exp $ #
  3.  
  4. # This script is part of the SLIS Project initiated by the CARMI-Internet
  5. # (Acad├⌐mie de Grenoble - France 38).
  6. # Ce script fait partie du projet SLIS d├⌐marr├⌐ par le CARMI-Internet
  7. # (Acad├⌐mie de Grenoble - France 38).
  8. #
  9. # SLIS : Serveur de communications Linux pour l'Internet Scolaire.
  10. # Copyright (C) 1998-2005 Bruno Bzeznik
  11. #
  12. #    This program is free software; you can redistribute it and/or modify
  13. #    it under the terms of the GNU General Public License as published by
  14. #    the Free Software Foundation; either version 2 of the License, or
  15. #    (at your option) any later version.
  16. #
  17. #    This program is distributed in the hope that it will be useful,
  18. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. #    GNU General Public License for more details.
  21. #    You should have received a copy of the GNU General Public License
  22. #    along with this program (For example ./COPYING);
  23. #    if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  24. #    Cambridge, MA 02139, USA.
  25. #
  26. # Please send all comments and bug reports by electronic mail to:
  27. #   Bruno Bzeznik <Bruno@ac-grenoble.fr>
  28. # or to <slis@ac-grenoble.fr>
  29. #
  30. # Envoyez vos suggestions et reports de bugs par e-mail ├â#   Bruno Bzeznik <Bruno@ac-grenoble.fr>
  31. # ou ├á <slis@ac-grenoble.fr>
  32. #
  33.  
  34.  
  35. # Load the SLIS lib and variables
  36. if [ -f /usr/local/lib/slis-sys.inc.bash ]
  37. then
  38.   . /usr/local/lib/slis-sys.inc.bash
  39.   load_config
  40. else
  41.   . /home/hadmin/slis.conf
  42. fi
  43. if [ "$SLIM_HOST" = "" ]
  44. then
  45.   logger -t "monitor[$$]" "ERROR: variable SLIM_HOST not found. Aborting"
  46.   echo "ERROR: variable SLIM_HOST not found. Aborting"
  47.   exit 1
  48. fi
  49. VERSION=`cat /etc/version_slis_num`
  50.  
  51. # Get the hostname
  52. export `grep HOSTNAME /etc/sysconfig/network`
  53. HOST=`echo $HOSTNAME|sed "s/\.$DOMAIN//"`
  54.  
  55.  
  56. # Slim host variables
  57. if [ "$SLIM_HOST" = "" ] 
  58. then
  59.   logger -t "monitor[$$]" "ERROR: SLIM_HOST variable not defined"
  60.   rm -f /var/lock/monitor
  61.   exit 1
  62. fi
  63. if [ "$SLIM_MONITOR_PATH" = "" ]
  64. then
  65.   SLIM_MONITOR_PATH="slim/scripts/monitor/get"
  66.   logger -t "monitor[$$]" "Warning: SLIM_MONITOR_PATH variable not defined, using slim/scripts/monitor/get"
  67. fi
  68.  
  69. # Locking
  70. # Updates can be freezed because of rsync or psql waiting infinetely
  71. # for the end of a dead request. So, there's a locking system a bit particular.
  72.  
  73. MAX_AGE=36000   # This is the maximum number of seconds you think
  74.                 # a monitor must leave before suspecting it to be
  75.                 # freezed.
  76.  
  77. RETRY_TIME=360  # This is the max number of seconds you wait after
  78.                 # having "refreshed" a freezed monitor for starting
  79.                 # the new one.
  80.  
  81. TEST_TIMEOUT=180 # This is the maximum time (s) to wait for the test
  82.                  # connection that is done before everything
  83.  
  84. # Locking
  85. LOCK_OUTPUT=`lockfile -1 -r 3 -l $MAX_AGE -s 0 /var/lock/monitor 2>&1`; LOCK_RC=$?
  86.  
  87. # If unable to lock (recent lock file found)
  88. if [ "$LOCK_RC" != "0" ]
  89. then
  90.   logger -t "monitor[$$]" "Error: Unable to lock. Is another monitor running?"
  91.   echo "Error: Unable to lock. Is another monitor active?" | mail "$SLISMASTER"
  92.   exit 1
  93. fi
  94.  
  95. # If a very old lock file is found
  96. if [ "`echo $LOCK_OUTPUT | grep -i "forcing lock"`" != "" ]
  97. then
  98.   logger -t "monitor[$$]" "Warning: forcing lock and killing probably sleeping monitor"
  99.  
  100.   # Perhaps rsync or psql is freezed
  101.   killall rsync 2> /dev/null
  102.   killall psql 2> /dev/null
  103.   killall wget 2> /dev/null
  104.  
  105.   # Try to lock another time while waiting for the previous monitor to finish
  106.   lockfile -5 -r $RETRY_TIME /var/lock/monitor ; LOCK_RC=$?
  107.   if [ "$LOCK_RC" != "0" ]
  108.   then
  109.     logger -t "monitor[$$]" "Error: Second try for locking failed"
  110.     echo "Error in monitor: Second try for locking failed" | mail "$SLISMASTER"
  111.     exit 1
  112.   fi
  113. fi
  114.  
  115. # Test the connection
  116. #   OBSOLETED BY SIGNATURE VERIFICATION, BUT KEPT HERE FOR MEMORY
  117. #logger -t "monitor[$$]" "Downloading test files"
  118. #mkdir -p /tmp/monitoring
  119. #cd /tmp/monitoring
  120. #rsync rsync://$SLIM_HOST/$RSYNC_MODULE/TESTFILE* . &
  121. #RSYNC_PID=$!
  122. #RUNNING=0
  123. #declare -i c=0
  124. #while [ "$RUNNING" = "0" ]
  125. #do
  126. #  sleep 1
  127. #  let c=$c+1
  128. #  if [ $c  -ge $TEST_TIMEOUT ]
  129. #  then
  130. #    logger -t "monitor[$$]" "ERROR: TESTFILE* files could not be downloaded. Aborting monitoring."
  131. #    killall rsync
  132. #    /bin/rm -f TESTFILE*
  133. #    rm -rf /var/lock/monitor
  134. #    exit 2
  135. #  fi
  136. #  ps $RSYNC_PID >/dev/null;
  137. #  RUNNING=$?
  138. #done
  139. #logger -t "monitor[$$]" "Verifying checksums"
  140. #/usr/bin/md5sum -c TESTFILE.md5 >/dev/null 2>&1;
  141. #if [ "$?" = "0" ]
  142. #then
  143. #  logger -t "monitor[$$]" "Connection test OK, starting monitoring..."
  144. #  /bin/rm -f TESTFILE*
  145. #else
  146. #  logger -t "monitor[$$]" "ERROR: connection test failed (incorrect md5sum). Aborting monitoring."
  147. #  /bin/rm -f TESTFILE*
  148. #  rm -rf /var/lock/monitor
  149. #  exit 2
  150. #fi
  151.  
  152.  
  153. mkdir -p /tmp/monitoring
  154. cd /tmp/monitoring
  155. SECURE=0
  156.  
  157. # Download the public key of the slim host if not already installed
  158. mkdir -p /usr/local/share/ssl
  159. if [ ! \( -f /usr/local/share/ssl/slimpubkey.pem \) ]
  160. then
  161.   logger -t "monitor[$$]" "Server's public key not found. Getting it..."
  162.   wget -q -O /tmp/monitoring/slimcert.pem https://$SLIM_HOST/$SLIM_MONITOR_PATH/slimcert.pem
  163.   if [ "$?" != "0" ]
  164.   then
  165.     logger -t "monitor[$$]" "WARNING: Could not download the public key from https://$SLIM_HOST/$SLIM_MONITOR_PATH/slimcert.pem. Trying to download it from rsync."
  166.     rsync rsync://$SLIM_HOST/$RSYNC_MODULE/slimcert.pem . 2>/dev/null >/dev/null
  167.     if [ "$?" != "0" ]
  168.     then
  169.       logger -t "monitor[$$]" "ERROR downloading rsync://$SLIM_HOST/$RSYNC_MODULE/slimcert.pem"
  170.       logger -t "monitor[$$]" "ERROR: Could not download the server's certificate!"
  171.       SECURE=0
  172.     else
  173.       logger -t "monitor[$$]" "WARNING: Trusting the public key obtained from the rsync server."
  174.       /usr/bin/openssl x509 -pubkey -in slimcert.pem > /usr/local/share/ssl/slimpubkey.pem
  175.       mv slimcert.pem /usr/local/share/ssl/
  176.       SECURE=1
  177.     fi
  178.   else
  179.     logger -t "monitor[$$]" "WARNING: Trusting the public key obtained from the https slim server."
  180.     /usr/bin/openssl x509 -pubkey -in slimcert.pem > /usr/local/share/ssl/slimpubkey.pem
  181.     mv slimcert.pem /usr/local/share/ssl/
  182.     SECURE=1
  183.   fi
  184. else
  185.   logger -t "monitor[$$]" "SLIM public key found. We're in secure mode."
  186.   SECURE=1
  187. fi
  188.  
  189.  
  190. # Get the monitor script from SLIM and revert to the rsync script if SLIM is not found
  191. wget -q -O /tmp/monitoring/monitor_rawfile https://$SLIM_HOST/$SLIM_MONITOR_PATH/monitor_getscript.php?slis_name=$HOST
  192. if [ $? != 0 ]
  193. then
  194.   logger -t "monitor[$$]" "ERROR getting https://$SLIM_HOST/$SLIM_MONITOR_PATH/monitor_getscript.php?slis_name=$HOST"
  195.   logger -t "monitor[$$]" "Reverting to the old rsync monitoring script."
  196.   cd /tmp/monitoring
  197.   rsync rsync://$SLIM_HOST/$RSYNC_MODULE/monitor . 2>/dev/null >/dev/null
  198.   if [ "$?" != "0" ]
  199.   then
  200.     logger -t "monitor[$$]" "ERROR getting rsync://$SLIM_HOST/$RSYNC_MODULE/monitor. Aborting."
  201.     rm -rf /var/lock/monitor
  202.     exit 3
  203.   else
  204.     if [ "$SECURE" = "1" ]
  205.     then
  206.       rsync rsync://$SLIM_HOST/$RSYNC_MODULE/monitor.sighash . 2>/dev/null >/dev/null
  207.       if [ "$?" != "0" ]
  208.       then
  209.         logger -t "monitor[$$]" "ERROR: Signature rsync://$SLIM_HOST/$RSYNC_MODULE/monitor.sighash could not be downloaded. Aborting."
  210.         rm -rf /var/lock/monitor
  211.         exit 4
  212.       else
  213.         VERIFY=`/usr/bin/openssl md5 -verify /usr/local/share/ssl/slimpubkey.pem -signature monitor.sighash < monitor`
  214.         if [ "$VERIFY" != "Verified OK" ]
  215.         then
  216.           logger -t "monitor[$$]" "ERROR: INVALID SIGNATURE!"
  217.           logger -t "monitor[$$]" "ERROR: We can not trust the server, monitoring aborted."
  218.           rm -rf /var/lock/monitor
  219.           exit 5
  220.         fi
  221.       fi
  222.     else
  223.       logger -t "monitor[$$]" "Warning: will start the rsync monitor script in NON SECURE MODE (old method)"
  224.     fi
  225.     logger -t "monitor[$$]" "Warning: Starting the rsync monitoring script (not SLIM)"
  226.     /bin/bash ./monitor
  227.     rm -rf /var/lock/monitor
  228.     exit 0
  229.   fi
  230. fi
  231.  
  232. # extract sign
  233. cat /tmp/monitoring/monitor_rawfile | grep \#SIGN: | cut -d: -f2 > /tmp/monitoring/monitor.sig.base64
  234.  
  235. # extract timestamp
  236. TIMESTAMP=`cat /tmp/monitoring/monitor_rawfile | grep \#TIMESTAMP: | cut -d: -f2`
  237.  
  238. # suppress sign from page
  239. awk '{if (NR > 2) print}' /tmp/monitoring/monitor_rawfile  > /tmp/monitoring/monitor_script
  240.  
  241. # Decode the signature
  242. perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)' < /tmp/monitoring/monitor.sig.base64 > /tmp/monitoring/monitor.sig
  243. #openssl base64 -d -a -in /tmp/monitoring/monitor.sig.base64 > /tmp/monitoring/monitor.sig
  244.  
  245. # Verification of the signature
  246. if [ -s /tmp/monitoring/monitor.sig.base64 ]
  247. then
  248.   VERIFY=`/usr/bin/openssl sha1 -verify /usr/local/share/ssl/slimpubkey.pem -signature /tmp/monitoring/monitor.sig  < /tmp/monitoring/monitor_script `
  249.   if [ "$VERIFY" != "Verified OK" ]
  250.   then
  251.     logger -t "monitor[$$]" "ERROR: INVALID SIGNATURE!"
  252.     logger -t "monitor[$$]" "ERROR: We can not trust the server, monitoring aborted."
  253.     rm -f /var/lock/monitor
  254.     exit 3
  255.   else
  256.     export TIMESTAMP
  257.  
  258.     # Start the monitoring script
  259.     logger -t "monitor[$$]" "Starting the monitoring script..."
  260.     /bin/bash /tmp/monitoring/monitor_script
  261.     if [ $? != 0 ]
  262.     then
  263.       wget -q -O /tmp/hop https://$SLIM_HOST/$SLIM_MONITOR_PATH/monitor_ackscript.php?slis_name=$HOST\×tamp=$TIMESTAMP\&status=KO
  264.       logger -t "monitor[$$]" "WARNING: Monitoring script ended with error(s)"
  265.     else
  266.       wget -q -O /tmp/hop https://$SLIM_HOST/$SLIM_MONITOR_PATH/monitor_ackscript.php?slis_name=$HOST\×tamp=$TIMESTAMP\&status=OK
  267.       logger -t "monitor[$$]" "Monitoring script ended successfully."
  268.     fi
  269.   fi
  270. else
  271.   logger -t "monitor[$$]" "ERROR: Signature could not be extracted! Aborting."
  272. fi
  273.  
  274. # Remove the lock
  275. rm -f /var/lock/monitor
  276.