Blob Blame History Raw
#! /bin/bash

# Copyright (C) 2004,2012 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 and/or 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# Usage:  clamav-notify-servers

CFGFILE=/etc/sysconfig/clamav-servers
OP=nc
NC=nc
TIMEOUT_TERM=30
TIMEOUT_KILL=60

K_OPT=1
LANG=C timeout --help | grep -q -- '--kill-after' || unset K_OPT

get_sockets() {
    (
	cd /etc/clamd.d
	for i in *.conf; do
	    test -f "$i" || continue
	    test -r "$i" || continue

	    base=${i%%.conf}
	    S=/var/run/clamd.$base/clamd.sock
	    test -S "$S" || continue
	    test -w "$S" || continue

	    echo "$S"
	done
    )
}

op_nc() {
    cd /var/run
    for i in ${CLAMD_SOCKETS}; do
	case $i in
	  (/*)
		test -S "$i" || { 
		  echo "socket '$i' does not exist" >&2
		  continue
		}
		CMD=( $NC -U $i )
		;;

	  (ip4:*)
		addr=${i##ip4:}
		CMD=( $NC -4 ${addr%%:*} ${addr##*:} )
		;;

	  (ip6:*)
		addr=${i##ip6:}
		CMD=( $NC -6 ${addr%%:*} ${addr##*:} )
		;;
	  (*)
		echo "unsupported socket name '$i'" >&2
		continue
	esac

	resp=`printf 'zRELOAD\0' | timeout ${K_OPT:+-k $TIMEOUT_KILL} $TIMEOUT_TERM "${CMD[@]}"`
	case $resp in
	  (RELOADING)
		;;
	  (*)
		echo "clamd server '$i' gave '$resp' response" >&2
		let ++fail
		;;
	esac
    done
    cd - &>/dev/null
}


CLAMD_SOCKETS=`get_sockets`
f=$CFGFILE
test ! -e "$f" || . "$f"

fail=0
op_$OP "$@"

exit $fail