summaryrefslogtreecommitdiff
blob: 0e60680d206a05feca0170d00d5c1dafe7c5cceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/sbin/runscript
# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/app-antivirus/clamav/files/clamd.initd,v 1.1 2012/08/07 18:09:58 eras Exp $

extra_commands="logfix"

depend() {
	use net
	provide antivirus
}

get_config() {
	clamconf | \
	sed 's/["=]//g' | \
	awk "{
	 if(\$0==\"Config file: $1.conf\")S=1
	 if(S==1&&\$0==\"\")exit
	 if(S==1&&\$1~\"^$2\$\"){
		print \$2!=\"disabled\"?\$2:\"$3\";
		exit
	 }
	}"
}

start() {
	local clamd_socket=$(get_config clamd LocalSocket /tmp/clamd)
	local clamd_socket_dir=`dirname ${clamd_socket}`
	logfix

	if [ "${START_CLAMD}" = "yes" ]; then
		if [ ! -e "${clamd_socket_dir}" ]; then
			mkdir ${clamd_socket_dir}
			chown $(get_config clamd User clamav) ${clamd_socket_dir}
		fi
		if [ -S "${clamd_socket}" ]; then
			rm -f ${clamd_socket}
		fi
		ebegin "Starting clamd"
		start-stop-daemon --start --quiet \
			--nicelevel ${CLAMD_NICELEVEL:-0} \
			--exec /usr/sbin/clamd
		eend $? "Failed to start clamd"
	fi

	if [ "${START_FRESHCLAM}" = "yes" ]; then
		ebegin "Starting freshclam"
		start-stop-daemon --start --quiet \
			--nicelevel ${FRESHCLAM_NICELEVEL:-0} \
			--exec /usr/bin/freshclam -- -d
		retcode=$?
		if [ ${retcode} = 1 ]; then
			eend 0
			einfo "Virus databases are already up to date."
		else
			eend ${retcode} "Failed to start freshclam"
		fi
	fi

	if [ "${START_MILTER}" = "yes" ]; then
		if [ -z "${MILTER_CONF_FILE}" ]; then
			MILTER_CONF_FILE="/etc/clamav-milter.conf"
		fi

		ebegin "Starting clamav-milter"
		start-stop-daemon --start --quiet \
			--nicelevel ${MILTER_NICELEVEL:-0} \
			--exec /usr/sbin/clamav-milter -- -c ${MILTER_CONF_FILE}
		eend $? "Failed to start clamav-milter"
	fi
}

stop() {
	if [ "${START_CLAMD}" = "yes" ]; then
		ebegin "Stopping clamd"
		start-stop-daemon --stop --quiet --name clamd
		eend $? "Failed to stop clamd"
	fi
	if [ "${START_FRESHCLAM}" = "yes" ]; then
		ebegin "Stopping freshclam"
		start-stop-daemon --stop --quiet --name freshclam
		eend $? "Failed to stop freshclam"
	fi
	if [ "${START_MILTER}" = "yes" ]; then
		ebegin "Stopping clamav-milter"
		start-stop-daemon --stop --quiet --name clamav-milter
		eend $? "Failed to stop clamav-milter"
	fi
}

logfix() {
	if [ "${START_CLAMD}" = "yes" ]; then
		# fix clamd log permissions
		# (might be clobbered by logrotate or something)
		local logfile=$(get_config clamd LogFile)
		local clamav_user=$(get_config clamd User)
		if [ -n "${logfile}" ] && [ -n "${clamav_user}" ]; then
			if [ ! -f "${logfile}" ]; then
				touch ${logfile}
			fi
			chown ${clamav_user} ${logfile}
			chmod 640 ${logfile}
		fi
	fi

	if [ "${START_FRESHCLAM}" = "yes" ]; then
		# fix freshclam log permissions
		# (might be clobbered by logrotate or something)
		local logfile=$(get_config freshclam UpdateLogFile)
		local freshclam_user=$(get_config freshclam DatabaseOwner clamav)
		if [ -n "${logfile}" -a -n "${clamav_user}" ]; then
			if [ ! -f "${logfile}" ]; then
				touch ${logfile}
			fi	
			chown ${freshclam_user} ${logfile}
			chmod 640 ${logfile}
		fi
	fi
}