shared-macros.mk: drop support for 32 bit mysql
[oi-userland.git] / components / sysutils / clamav / files / freshclam.sh
blobac90d1d182c56762c545b15ccaff1a0947a562ba
1 #!/bin/sh
3 ### Startup for COSclamav package
4 ### (C) 2008-2016 by Jim Klimov, COS&HT
5 ### $Id: freshclam.sh,v 1.6 2016/02/22 20:40:00 jim Exp $
7 ### Use to update ClamAV virus signatures from cron:
8 ### 0,15,30,45 * * * * [ -x /usr/bin/freshclam.sh ] && /usr/bin/freshclam.sh
10 CLAMD_CONFFILE=/etc/clamav/clamd.conf
11 FRESHCLAM_CONFFILE=/etc/clamav/freshclam.conf
12 FRESHCLAM_BINFILE="/usr/bin/freshclam"
14 ### Clumsy syntax due to portable shells:
15 FRESHCLAMSH_BINFILE_DIR="`dirname "$0"`" && \
16 FRESHCLAMSH_BINFILE_DIR="`cd "$FRESHCLAMSH_BINFILE_DIR" && pwd`" && \
17 FRESHCLAMSH_BINFILE="$FRESHCLAMSH_BINFILE_DIR/`basename "$0"`" && \
18 [ x"$FRESHCLAMSH_BINFILE" != x ] && [ -x "$FRESHCLAMSH_BINFILE" ] || \
19 FRESHCLAMSH_BINFILE="/usr/bin/freshclam.sh"
21 STATUS_LINES=100
23 PATH=/usr/bin:/bin:/usr/sbin:/bin:$PATH
24 export PATH
26 LD_LIBRARY_PATH=\
27 /usr/lib:/lib:/usr/ssl/lib:\
28 /usr/sfw/lib:/opt/sfw/lib:\
29 /usr/gnu/lib:/opt/gnu/lib:\
30 $LD_LIBRARY_PATH
31 export LD_LIBRARY_PATH
33 if [ ! -s "$CLAMD_CONFFILE" -o ! -s "$FRESHCLAM_CONFFILE" ]; then
34 # echo "ClamAV-FreshCLAM: No config file!" >&2
35 exit 0
38 adderror() {
39 BUF_ERROR="$BUF_ERROR
40 $1"
41 [ x"$RES" = x0 ] && RES="$2"
42 [ "$RES_MAX" -lt "$2" ] && RES_MAX="$2"
45 do_delcron() {
46 ( which crontab >/dev/null 2>&1 ) || \
47 { echo "ERROR: crontab program not found" >&2; return 1; }
48 RES=0
50 # Below we depend on parsing some outputs
51 LC_ALL=C
52 LANG=C
53 export LC_ALL LANG
55 OUT="`crontab -l 2>&1`" || RES=$?
56 [ $RES != 0 ] && \
57 if echo "$OUT" | grep "can't open" >/dev/null && \
58 [ "`echo "$OUT" | wc -l`" -eq 1 ] \
59 ; then OUT="" ; RES=0; else return $RES; fi
60 echo "$OUT" | egrep -v "bin/freshclam|$FRESHCLAMSH_BINFILE|^$" > "/tmp/saved-crontab.$$"
61 if [ -s "/tmp/saved-crontab.$$" ]; then
62 crontab "/tmp/saved-crontab.$$" || RES=$?
63 else
64 crontab -r || RES=$?
66 rm -f "/tmp/saved-crontab.$$"
67 [ $RES != 0 ] && echo "ERROR: could not delete freshclam from crontab" >&2
68 return $RES
71 do_addcron() {
72 [ x"$1" = x ] && return 1 # Whole cron-schedule spec
73 do_delcron || return $? # Also checks for valid crontab program in PATH
75 # Below we depend on parsing some outputs
76 LC_ALL=C
77 LANG=C
78 export LC_ALL LANG
80 if [ "$1" = SMF ]; then
81 [ -n "$SMF_FMRI" ] && \
82 SCHED="`/usr/bin/svcprop -p freshclam/cron-schedule "$SMF_FMRI"`" \
83 && [ -n "$SCHED" ] || return $?
84 else
85 SCHED="$1"
87 # SMF escapes our schedule... and shell too...
88 SCHED="`echo "$SCHED" | sed 's,\\\\ , ,g'`"
89 OUT="`crontab -l 2>/dev/null`" || OUT=""
90 echo "$OUT
91 $SCHED [ -x '$FRESHCLAMSH_BINFILE' ] && '$FRESHCLAMSH_BINFILE'" > "/tmp/saved-crontab.$$"
92 RES=0
93 crontab "/tmp/saved-crontab.$$" || RES=$?
94 rm -f "/tmp/saved-crontab.$$"
95 [ $RES != 0 ] && echo "ERROR: could not add freshclam to crontab" >&2
96 return $RES
99 case "$1" in
100 --smf-enable) # Initial refresh and a cronjob (with schedule in $2)
101 [ -x "$FRESHCLAM_BINFILE" ] && "$FRESHCLAM_BINFILE"
102 do_addcron "SMF"
104 --add-cron) # For CLI user and SMF integration
105 [ x"$2" = x -o $# != 2 ] && \
106 echo "ERROR: Option $1 requires a cron-schedule 5-token argument like '0 * * * *'" >&2 && \
107 exit 1
108 do_addcron "$2"
109 exit $?
111 --del-cron) # For CLI user and SMF integration
112 do_delcron
113 exit $?
115 status) ### Use "status nowarn" to not return errors for warnings only.
116 ### Use "status nowarnobs" to not return errors for warnings
117 ### and obsolete databases only.
118 [ x"$2" != x ] && [ "$2" -gt 10 ] && STATUS_LINES="$2"
119 LOGFILE="`egrep '^[^#]*UpdateLogFile ' $FRESHCLAM_CONFFILE | awk '{print $NF }'`"
120 if [ x"$LOGFILE" = x -o ! -f "$LOGFILE" ]; then
121 echo "ClamAV-FreshCLAM: No log file ($LOGFILE)!" >&2
122 exit 1
125 BUF_ERROR=""
126 BUF_START=""
127 RES=0
128 RES_MAX=0
129 tail -"$STATUS_LINES" "$LOGFILE" | ( while read LINE; do
130 case "$LINE" in
131 ClamAV\ update\ process\ started\ at*)
132 BUF_ERROR=""
133 RES=0
134 RES_MAX=0
135 BUF_START="$LINE"
137 *connect\ timing\ out*|*Can?t\ connect\ to*)
138 adderror "$LINE" 10 ;;
139 *node\ name\ or\ service\ name\ not\ known*)
140 adderror "$LINE" 11 ;;
141 Update\ failed*)
142 adderror "$LINE" 12 ;;
143 *Your\ ClamAV\ installation\ is\ OUTDATED*)
144 adderror "$LINE" 3 ;;
145 WARNING*)
146 adderror "$LINE" 2 ;;
147 esac
148 done
149 if [ x"$RES" != x0 ]; then
150 [ x"$BUF_START" != x ] && echo "$BUF_START" >&2
151 echo "$BUF_ERROR" >&2
152 if [ x"$2" = xnowarn -a "$RES_MAX" -le 2 ]; then
153 echo "Status: WARN"
154 RES=0
155 else
156 if [ x"$2" = xnowarnobs -a "$RES_MAX" -le 3 ]; then
157 echo "Status: WARN"
158 RES=0
159 else
160 echo "Status: FAIL"
163 else
164 echo "Status: OK"
166 exit $RES )
168 *) # Wrapper for the program
169 [ -x "$FRESHCLAM_BINFILE" ] && "$FRESHCLAM_BINFILE" "$@"
171 esac