791e847b79e5c1cb982eafc5fccc86b1a2d4a103
[infra/tools.git] / clamav / etc / cron.daily / clamav
1 #!/bin/bash
2
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20
21 #
22 # This script runs a virus scan on a Linux client using clamav. It is stored in
23 # /etc/cron.daily so that it does a scan daily. Once an initial scan is
24 # performed, the script only scans changed files. Files and directories can be
25 # excluded by updating the etc/clamav/clamscan_excludes.conf file.
26 #
27
28 # Use notify-send to put a message on the user's display
29 function notify-send-user() {
30     #Detect the name of the display in use
31     local display=":$(ls /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)"
32
33     #Detect the user using such display
34     local user=$(who | grep '('$display')' | awk '{print $1}')
35
36     #Detect the id of the user
37     local uid=$(id -u $user)
38
39     sudo -u $user DISPLAY=$display DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus notify-send "$@"
40 }
41
42 # Set the various configuration variables for the script
43 SCANLOG="/var/log/clamav/scan.log"
44 ERRORLOG="/var/log/clamav/error.log"
45 SCANEXC="/etc/clamav/clamscan_excludes.conf"
46 SCANQNT="/var/.quatrantine"
47
48 # Check if clamav is installed
49 DATE=`date -u`
50
51 if [ ! -f /var/lib/clamav/daily.cvd -o ! -x /usr/bin/clamscan -o ! -d /var/log/clamav ]
52 then
53   echo "$DATE: clamav is not installed or is incorrectly installed." >> $ERRORLOG
54   notify-send-user -i /usr/share/pixmaps/clamtk.png -u critical  "clamav: software not installed correctly"
55   chmod a+r $ERRORLOG
56   exit 1
57 fi
58
59 # Check if the excludes file exists
60 if [ ! -f $SCANEXC ]
61 then
62   echo "$DATE: File $SCANEXC does not exist." >> $ERRORLOG
63   chmod a+r $ERRORLOG
64   notify-send-user -i /usr/share/pixmaps/clamtk.png -u critical  "clamav: File $SCANEXC not found"
65   exit 2
66 fi
67
68 # Read and set up the path and file excludes
69 . $SCANEXC >/dev/null 2>&1
70
71 # Create the scan log file if it doesn't exist
72 if [ -f $SCANLOG ]
73 then
74   # Compress the previous scan log
75   TIMESTAMP=`date +"%Y-%m-%d_%T"`
76   gzip -9 -c $SCANLOG > ${SCANLOG}-${TIMESTAMP}.gz
77 else
78   touch -t 000001010000 $SCANLOG
79 fi
80
81 # Find the files that should be scanned in this scan
82 SCANFIL=`mktemp "/tmp/clamscan_files.XXXXXX"`
83 ionice -c 3 nice find / "${FIND_PRUNE_FILTER_ARRAY[@]}" -newer ${SCANLOG} -type f "${FIND_FILE_FILTER_ARRAY[@]}" >> $SCANFIL
84
85 # Clear the daa for the previous scan
86 rm $SCANLOG
87
88 # Run the scan
89 mkdir -p $SCANQNT
90 ionice -c 3 nice /usr/bin/clamscan --file-list=$SCANFIL --log=$SCANLOG --infected --copy=$SCANQNT >/dev/null 2>&1
91
92 echo "Finish time: $DATE" >> $SCANLOG
93
94 chmod a+r $SCANLOG
95
96 # Check if any viruses were found
97 INFECTED_FILE_COUNT=`grep '^Infected files: ' /var/log/clamav/scan.log | sed 's/^Infected files: //'`
98 if [ "$INFECTED_FILE_COUNT" -gt "0" ]
99 then
100   notify-send-user -i /usr/share/pixmaps/clamtk.png -u critical  "clamav: $INFECTED_FILE_COUNT infected files found" "see $SCANLOG for details"
101   exit 3
102 fi
103
104 rm -f $SCANFIL
105
106 exit 0