summaryrefslogtreecommitdiff
path: root/conf/dist/kolabsrv.in
blob: ba648374bb172f8d141278d97e17b2bb31e8e4bb (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!@BASH@

PRGNAME=$(basename $0)

# check if everything is correct
function check() {
	if [ -x "$RCDIR/$SERVICE" ]; then
		case $PARAM in
			start|START)     PARAM="start" && rcrun;;
			stop|STOP)       PARAM="stop" && rcrun;;
			restart|RESTART) PARAM="restart" && rcrun;;
			reload|RELOAD)   PARAM="reload" && rcrun;;
			status|STATUS)   PARAM="status" && rcrun;;
			*) echo "ERROR: Parameter $PARAM not supported!"
		esac
	else
		echo "ERROR $RCDIR/$SERVICE NOT found - abort" >&2
	fi	
}

# starts/stops/restarts... the service
function rcrun() {
	$RCDIR/$SERVICE $PARAM
	retc=$?
	if [ $retc -ne 0 ] && [ $retc -ne 3 ]; then
		( echo "ERROR: $RCDIR/$SERVICE $PARAM failed"
		  echo "Run: $PRGNAME rc all stop"
		  echo "to stop all services"
		) >&2
		exit 1
	fi
}	

# map distribution specific rc script names
function getServiceName() {
	sname=$1
	# Perhaps better use:
	# lsb_release -i | cut -f2
	case $system in
	redhat|fedora)
		case $sname in
			apache) SERVICE="httpd" ;;
			openldap) SERVICE="ldap" ;;
			sasl) SERVICE="saslauthd" ;;
			imapd) SERVICE="cyrus-imapd" ;;
			amavisd) SERVICE="amavisd" ;;
			clamav) SERVICE="clamd.amavisd" ;;
			*) SERVICE=$sname
		esac
		;;

	opensuse)
		case $sname in
			apache) SERVICE="apache2" ;;
			openldap) SERVICE="ldap" ;;
			sasl) SERVICE="saslauthd" ;;
			imapd) SERVICE="cyrus" ;;
			amavisd) SERVICE="amavis" ;;
			clamav) SERVICE="clamd" ;;
			*) SERVICE=$sname
		esac
		;;
	debian)
		case $sname in
			apache) SERVICE="apache2" ;;
			openldap) SERVICE="slapd" ;;
			sasl) SERVICE="saslauthd" ;;
			imapd) SERVICE="kolab-cyrus" ;;
			amavisd) SERVICE="amavis" ;;
			clamav) SERVICE="clamav-daemon" ;;
			spamd) SERVICE="spamassassin" ;;
			freshclam) SERVICE="clamav-freshclam" ;;
			*) SERVICE=$sname
		esac
		;;
	mandriva)
		case $sname in
			apache) SERVICE="httpd" ;;
			openldap) SERVICE="ldap" ;;
			sasl) SERVICE="saslauthd" ;;
			imapd) SERVICE="cyrus-imapd" ;;
			amavisd) SERVICE="amavisd" ;;
			clamav) SERVICE="clamd" ;;
			spamd) SERVICE="spamd" ;;
			freshclam) SERVICE="freshclam" ;;
			*) SERVICE=$sname
		esac
		;;
	esac
}	

# Copied from the script /etc/init.d/vboxadd from Virtualbox
# That script has more nice code, for e.g. displaying the result of
# executed commands.
if [ -f /etc/arch-release ]; then
	system=arch
elif [ -f /etc/redhat-release ]; then
	system=redhat
elif [ -f /etc/fedora-release ]; then
	system=fedora
elif [ -f /etc/SuSE-release ]; then
	system=opensuse
elif [ -f /etc/gentoo-release ]; then
	system=gentoo
elif [ -e /etc/debian_version ]; then
	system=debian
elif [ -f /etc/mandriva-release ]; then
	system=mandriva
elif [ -f /etc/lfs-release -a -d /etc/rc.d/init.d ]; then
	system=lfs
else
	system=other
fi

# Some distributions run / control the services clamav and freshclam from
# amavisd.  The services can be unset in @sysconfdir@/kolab/profile.sh,
# when unset, they are not called obviously. 
CLAMAV=clamav
FRESHCLAM=freshclam

[[ -f /etc/kolab/profile.sh ]] && . /etc/kolab/profile.sh

# variables
RC=$1
SERVICE=$2
PARAM=$3

# Use the service name as defined in the openpkg environment
# Kolab installations on native distributions, should adapt service names
# with the function getServiceName
#
# The order in which the services get started in openSUSE is as follows:
# S08ldap
# S08saslauthd
# S08clamd
# S10amavis
# S10cyrus
# S11postfix
# S12apache2
# S13kolabd
# S13freshclam
# 
# In earlier days, spamd was part of the services as well.  But as
# spamassassin is used as library for amavisd, it is not needed to run it
# as a seperate service.  More information about this topic can be found
# at https://issues.kolab.org/issue3953
SERVICES="openldap sasl $CLAMAV amavisd imapd postfix apache"
SERVICES="$SERVICES kolabd $FRESHCLAM"

case $RC in 
rc)
	# okay
	;;
*list)
	# Provide an alphanumerical list of services.
	for S in $SERVICES; do
		getServiceName $S
		DIST_SERVICES="$DIST_SERVICES $SERVICE"
	done
	case $RC in
	alphalist)
		echo $DIST_SERVICES | tr " " "\n" | sort | tr "\n" " "
		echo
		;;
	*)
		echo $DIST_SERVICES
		;;
	esac
	exit
	;;
status)
	for S in $SERVICES; do
		getServiceName $S
		case $S in
		amavisd|clamav|freshclam|spamd)
			DEFENSIVE_SERVICES="$DEFENSIVE_SERVICES $SERVICE"
			;;
		*)
			GROUPWARE_SERVICES="$GROUPWARE_SERVICES $SERVICE"
			;;
		esac
	done

	echo "Groupware services:"
	echo "-------------------"
	chkconfig --list $(tr " " "\n" <<< $GROUPWARE_SERVICES | sort)
	echo "All the above listed groupware services must be active."
	echo

	echo "Defensive services:"
	echo "-------------------"
	chkconfig --list $(tr " " "\n" <<< $DEFENSIVE_SERVICES | sort)

	if [[ "$ENABLE_VIRUS_SCAN" == "no" ]]; then
		echo "SPAM detection and virus scanning are disabled."
		echo "Hence the above listed defensive services do not need to be active."
	else
		echo "SPAM detection and virus scanning are enabled."
		echo "The above listed defensive services must be active."
	fi
	exit
	;;
*)
	SERVICES=$(echo $SERVICES | tr -s " " | sed 's, ,|,'g)

	( [[ -n "$RC" ]] && echo "ERROR: $RC not supported"
	  echo "Use: $PRGNAME rc [all|$SERVICES] [stop|start|restart|status]"
	  echo
	  echo "Obtain a list of all kolab related processes;"
	  echo "- in execution order:"
	  echo "  $PRGNAME list"
	  echo
	  echo "- in alphabetical order:"
	  echo "  $PRGNAME alphalist"
	  echo
	  echo "Display the state of the all kolab related processes:"
	  echo "  $PRGNAME status"
	) >&2

	exit 1
	;;
esac

if [[ "$SERVICE" != "all" ]]; then

	SERVICE_FOUND=no

	for S in $SERVICES; do
		if [[ $S == "$SERVICE" ]]; then
			SERVICE_FOUND=yes
			SERVICES=$SERVICE
			break
		fi
	done

	if [[ $SERVICE_FOUND == "no" ]]; then
		( echo "ERROR: unknown service: $SERVICE"
		  echo "Use: $PRGNAME rc [all|$SERVICES] [stop|start|restart|status]"
		) >&2

		exit 1
	fi
else
	if [[ "$PARAM" == "stop" ]]; then
		SERVICES=$(tr " " "\n" <<< "$SERVICES" | tac)
	fi
fi

RCDIR=@kolab_rcdir@

for SERVICE in $SERVICES; do
	# Only start the virus scanning dependent services in case
	# virus scannning has been enabled on the kolab web admin interface.
	# An service is always stopped independent of the virus service	
	# scanning setting.
	if [[ "$PARAM" == "start" ]] || [[ "$PARAM" == "restart" ]]; then
		case $SERVICE in
		amavisd|clamav|freshclam|spamd)
			if [[ "$ENABLE_VIRUS_SCAN" == "no" ]]; then
				RUN_SERVICE=no
			else
				RUN_SERVICE=yes
			fi
			;;
		*)
			RUN_SERVICE=yes
			;;
		esac
	else
		RUN_SERVICE=yes
	fi

	[[ $RUN_SERVICE == "yes" ]] && {
		getServiceName $SERVICE
		check $SERVICE
	}
done