#!/usr/bin/bash
#
##
# Linux Malware Detect v2.0.1
#             (C) 2002-2026, R-fx Networks <proj@rfxn.com>
#             (C) 2026, Ryan MacDonald <ryan@rfxn.com>
# This program may be freely redistributed under the terms of the GNU GPL v2
##
#
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
inspath='/usr/lib/maldet'
intcnf="$inspath/internals/internals.conf"

# prevent overlapping cron runs — use flock command form so the lock fd is
# CLOEXEC and never inherited by backgrounded maldet -b scans
LOCKFILE="$inspath/tmp/.cron.lock"
if command -v flock >/dev/null 2>&1 && [ -z "$_CRON_FLOCK" ]; then
	export _CRON_FLOCK=1
	flock -n "$LOCKFILE" "$0" "$@"
	rc=$?
	# flock -n exits 1 on lock contention; inner errors use exit 2
	if [ "$rc" -eq 1 ]; then
		exit 0
	fi
	exit "$rc"
fi

# Inner-script errors use exit 2 to distinguish from flock lock contention
# (exit 1). Do NOT use 'exit 1' for errors below the flock block.
if [ -f "$intcnf" ]; then
	. $intcnf
else
	echo "\$intcnf not found."
	exit 2
fi
if [ -f "$cnf" ]; then
	. $cnf
	if [ -f "$compatcnf" ]; then
	        . $compatcnf
	fi
else
	echo "could not find \$cnf, fatal error, bye."
	exit 2
fi

if [ -f "/etc/sysconfig/maldet" ]; then
	. /etc/sysconfig/maldet
elif [ -f "/etc/default/maldet" ]; then
	. /etc/default/maldet
fi

if [ -f "$cron_custom_conf" ]; then
	. $cron_custom_conf
fi

if [ -z "$scan_days" ]; then
	scan_days=1
fi

if [ -z "$cron_prune_days" ]; then
	cron_prune_days=21
fi

if [ "$find" ]; then
	# prune quarantine/session/tmp data older than $cron_prune_days days
	tmpdirs="$tmpdir $varlibpath/sess $varlibpath/quarantine $varlibpath/pub"
	for dir in $tmpdirs; do
	 if [ -d "$dir" ]; then
	  $find "$dir" -type f -mtime +${cron_prune_days} -delete 2>/dev/null
	 fi
	done
fi

if [ "$autoupdate_version" == "1" ] || [ "$autoupdate_signatures" == "1" ]; then
	# sleep for random 1-999s interval to better distribute upstream load
	sleep $(echo $RANDOM | cut -c1-3) >> /dev/null 2>&1
fi

if [ "$autoupdate_version" == "1" ]; then
	$inspath/maldet -d 1 2>&1 | tail -20 >> "$maldet_log"
	if [ "${PIPESTATUS[0]}" -ne 0 ]; then
		echo "$(date) cron.daily: maldet -d (version update) failed" >> "$maldet_log"
	fi
fi

if [ "$autoupdate_signatures" == "1" ]; then
	$inspath/maldet -u 1 2>&1 | tail -20 >> "$maldet_log"
	if [ "${PIPESTATUS[0]}" -ne 0 ]; then
		echo "$(date) cron.daily: maldet -u (signature update) failed" >> "$maldet_log"
	fi
fi

# Maintenance: rotate oversized history files, clean stale scan metadata,
# compress/archive old sessions
$inspath/maldet --maintenance >> /dev/null 2>&1  # safe: maintenance output logged internally

# Hook scan digest — fire if hook.hits.log has content since last sweep
if [ "${cron_digest_hook:-1}" == "1" ] && [ -f "$sessdir/hook.hits.log" ] && [ -s "$sessdir/hook.hits.log" ]; then
	echo "$(date) cron.daily: hook digest: checking for new detections" >> "$maldet_log"
	$inspath/maldet --digest >> /dev/null 2>&1
fi

# if we're running inotify monitoring, send daily hit summary
if [ "$(ps -A --user root -o "cmd" | grep -E maldetect | grep -E inotifywait)" ]; then
        $inspath/maldet --monitor-report >> /dev/null 2>&1
elif [ "$cron_daily_scan" == "1" ]; then
	if [ -d "/home/virtual" ] && [ -d "/usr/lib/opcenter" ]; then
		# ensim
	        $inspath/maldet -b -r /home/virtual/?/fst/var/www/html/,/home/virtual/?/fst/home/?/public_html/ $scan_days >> /dev/null 2>&1
	elif [ -d "/etc/psa" ] && [ -d "/var/lib/psa" ]; then
		# psa
		$inspath/maldet -b -r /var/www/vhosts/?/ $scan_days >> /dev/null 2>&1
        elif [ -d "/usr/local/directadmin" ]; then
                # DirectAdmin
                $inspath/maldet -b -r /home?/?/domains/?/public_html/,/var/www/html/?/ $scan_days >> /dev/null 2>&1
	elif [ -d "/var/www/clients" ]; then
		# ISPConfig
                $inspath/maldet -b -r /var/www/clients/?/web?/web,/var/www/clients/?/web?/subdomains,/var/www $scan_days >> /dev/null 2>&1
	elif [ -d "/etc/webmin/virtual-server" ]; then
		# Virtualmin
                $inspath/maldet -b -r /home/?/public_html/,/home/?/domains/?/public_html/ $scan_days >> /dev/null 2>&1
	elif [ -d "/usr/local/ispmgr" ] || [ -d "/usr/local/mgr5" ]; then
		# ISPmanager
		$inspath/maldet -b -r /var/www/?/data/,/home/?/data/ $scan_days >> /dev/null 2>&1
	elif [ -d "/var/customers/webs" ]; then
		# froxlor
		$inspath/maldet -b -r /var/customers/webs/ $scan_days >> /dev/null 2>&1
	elif [ -d "/opt/webdir" ] && [ -d "/etc/nginx/bx" ]; then
		# Bitrix
		$inspath/maldet -b -r /home/bitrix/www/,/home/bitrix/ext_www/?/ $scan_days >> /dev/null 2>&1
        elif [ -d "/usr/local/vesta" ] || [ -d "/usr/local/hestia" ]; then
                # VestaCP or HestiaCP
                $inspath/maldet -b -r /home/?/web/?/public_html/,/home/?/web/?/public_shtml/,/home/?/tmp/,/home/?/web/?/private/ $scan_days >> /dev/null 2>&1
        elif [ -d "/usr/share/dtc" ]; then
                # DTC
                if [ -f /var/lib/dtc/saved_install_config ]; then
                    . /var/lib/dtc/saved_install_config
                fi
                $inspath/maldet -b -r ${conf_hosting_path:-/var/www/sites}/?/?/subdomains/?/html/ $scan_days >> /dev/null 2>&1
	elif [ -d "/usr/local/cpanel" ] || [ -d "/var/cpanel" ]; then
		# cPanel: parse /etc/userdatadomains for all document roots
		# format: domain: user==owner==type==parent==/docroot==ip:port==
		cpanel_paths="/home?/?/public_html/"
		if [ -f /etc/userdatadomains ]; then
			cpanel_extra=$(awk -F'==' '{gsub(/^[[:space:]]+|[[:space:]]+$/, "", $5); if ($5 != "" && $5 != "/") print $5}' /etc/userdatadomains | sort -u | while IFS= read -r docroot; do
				# skip paths already covered by the public_html glob
				case "$docroot" in
					/home*/public_html|/home*/public_html/*) continue ;;
				esac
				[ -d "$docroot" ] && printf '%s/,' "$docroot"
			done)
			cpanel_extra="${cpanel_extra%,}"
			if [ -n "$cpanel_extra" ]; then
				cpanel_paths="${cpanel_paths},${cpanel_extra}"
			fi
		fi
		$inspath/maldet -b -r ${cpanel_paths},/var/www/,/usr/local/apache/htdocs/ $scan_days >> /dev/null 2>&1
	else
		# interworx and other standard home/user/public_html setups
	        $inspath/maldet -b -r /home?/?/public_html/,/var/www/,/usr/local/apache/htdocs/ $scan_days >> /dev/null 2>&1
	fi
fi

if [ -f "$cron_custom_exec" ]; then
	. $cron_custom_exec
fi
