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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
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.
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)"
33 #Detect the user using such display
34 local user=$(who | grep '('$display')' | awk '{print $1}')
36 #Detect the id of the user
37 local uid=$(id -u $user)
39 sudo -u $user DISPLAY=$display DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus notify-send "$@"
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"
48 # Check if clamav is installed
51 if [ ! -f /var/lib/clamav/daily.cld -o ! -x /usr/bin/clamscan -o ! -d /var/log/clamav ]
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"
59 # Check if the excludes file exists
62 echo "$DATE: File $SCANEXC does not exist." >> $ERRORLOG
64 notify-send-user -i /usr/share/pixmaps/clamtk.png -u critical "clamav: File $SCANEXC not found"
68 # Read and set up the path and file excludes
69 . $SCANEXC >/dev/null 2>&1
71 # Create the scan log file if it doesn't exist
74 # Compress the previous scan log
75 TIMESTAMP=`date +"%Y-%m-%d_%T"`
76 gzip -9 -c $SCANLOG > ${SCANLOG}-${TIMESTAMP}.gz
78 touch -t 000001010000 $SCANLOG
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
85 # Clear the daa for the previous scan
90 ionice -c 3 nice /usr/bin/clamscan --file-list=$SCANFIL --log=$SCANLOG --infected --copy=$SCANQNT >/dev/null 2>&1
92 echo "Finish time: $DATE" >> $SCANLOG
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" ]
100 notify-send-user -i /usr/share/pixmaps/clamtk.png -u critical "clamav: $INFECTED_FILE_COUNT infected files found" "see $SCANLOG for details"