1#!/bin/sh
2#
3# Copyright (c) 2010, 2011, The Trusted Domain Project.  All rights reserved.
4#
5#
6### BEGIN INIT INFO
7# Provides:          opendkim
8# Required-Start:    $syslog $time $local_fs $remote_fs $named $network
9# Required-Stop:     $syslog $time $local_fs $remote_fs $named
10# Default-Start:     2 3 4 5
11# Default-Stop:      S 0 1 6
12# Short-Description: OpenDKIM Milter
13# Description:       The OpenDKIM milter for signing and verifying email
14#                    messages using the DomainKeys Identified Mail protocol
15### END INIT INFO
16#
17# chkconfig: 345 20 80
18# description: OpenDKIM milter for signing and verifying email
19# processname: opendkim
20#
21# This script should run successfully on any LSB-compliant system. It will
22# attempt to fall back to RedHatisms if LSB isn't available, or to
23# commonly-available utilities if it's not even RedHat.
24
25NAME=opendkim
26PATH=/bin:/usr/bin:/sbin:/usr/sbin
27DAEMON=/usr/sbin/$NAME
28PIDFILE=/var/run/$NAME/$NAME.pid
29CONFIG=/etc/$NAME.conf
30USER=opendkim
31
32# Implement our own status function per LSB specs. This will be used on
33# non-RedHat systems.
34od_status() {
35	pid=$(od_getpid)
36	if [ $? -ne 0 ]; then
37		if [ -f $PIDFILE ]; then
38			echo "$NAME dead but pid file exists"
39			return 1
40		elif [ -d /var/lock/subsys -a -f /var/lock/subsys/$NAME ]; then
41			echo "$NAME dead but subsys locked"
42			return 2
43		else
44			echo "$NAME is stopped"
45			return 3
46		fi
47	fi
48	echo "$NAME (pid $pid) is running..."
49	return 0
50}
51
52od_getpid() {
53	if [ -n "$1" -a "$1" = "-P" ]; then
54		shift
55		PIDFILE=$1
56		shift
57	else
58		PIDFILE=$(grep -i '^pidfile' $CONFIG | head -n 1 | awk '{print $2}')
59	fi
60	if [ ! -f "$PIDFILE" ]; then
61		return 1
62	fi
63	PID=$(cat "$PIDFILE")
64	if [ -n "$(which pgrep)" ]; then
65		for p in $(pgrep -f $DAEMON); do
66			if [ "$PID" = "$p" ]; then
67				echo $p
68				return 0
69			fi
70		done
71	elif [ -x "/bin/pidof" ]; then
72		for p in $(/bin/pidof -o %PPID $DAEMON); do
73			if [ "$PID" = "$p" ]; then
74				echo $p
75				return 0
76			fi
77		done
78	fi
79	return 1
80}
81
82od_killproc() {
83	[ -z "$1" ] && return 1
84	if [ -n "$2" ]; then
85		signal=$2
86	else
87		signal="TERM"
88	fi
89	if $(od_getpid); then
90		pkill -"$signal" -f $1
91	fi
92}
93
94# Check for helper functions
95if [ -f /lib/lsb/init-functions ]; then
96	# Use LSB functions, if available
97	. /lib/lsb/init-functions
98	alias od_killproc=killproc
99	alias od_daemon=start_daemon
100elif [ -f /etc/init.d/functions ]; then
101	# Use RedHat init functions if LSB isn't available
102	. /etc/init.d/functions
103	alias od_daemon=daemon
104	alias log_success_msg=success
105	alias log_warning_msg=passed
106	alias log_failure_msg=failure
107	alias od_killproc=killproc
108	alias od_status=status
109elif [ -f /etc/rc.d/init.d/functions ]; then
110	# Legacy RedHat init location
111	. /etc/rc.d/init.d/functions
112	alias od_daemon=daemon
113	alias log_success_msg=success
114	alias log_warning_msg=passed
115	alias log_failure_msg=failure
116	alias od_killproc=killproc
117	alias od_status=status
118else
119	# If all else fails, use generic commands
120	alias od_daemon=''
121	alias log_success_msg=echo
122	alias log_warning_msg=echo
123	alias log_failure_msg=echo
124fi
125
126if [ ! -x "$DAEMON" ]; then
127	exit 5
128fi
129
130if [ ! -f "$CONFIG" ]; then
131	log_failure_msg "$CONFIG not found"
132	exit 6
133fi
134
135[ -r /etc/default/$NAME ] && . /etc/default/$NAME
136
137ARGS="-u $USER $ARGS"
138
139if [ -n "$SOCKET" ]; then
140	ARGS="$ARGS -p $SOCKET"
141fi
142
143od_start() {
144	echo -n "Starting OpenDKIM Milter: "
145	od_daemon $DAEMON -x $CONFIG $ARGS
146	if [ $? -eq 0 ]; then
147		log_success_msg $NAME
148		if [ -d /var/lock/subsys ]; then
149			touch /var/lock/subsys/$NAME
150		fi
151	else
152		log_failure_msg $NAME
153	fi
154	echo
155}
156
157od_stop() {
158	echo -n "Stopping OpenDKIM Milter: "
159	CPIDFILE=""
160	if [ -f $CONFIG ]; then
161		CPIDFILE=$(grep -i '^pidfile' $CONFIG | head -n 1 | awk '{print $2}')
162	fi
163	if [ x"$CPIDFILE" != x"" ]; then
164		PIDFILE=$CPIDFILE
165	fi
166
167	od_killproc -p $CPIDFILE $DAEMON
168	if [ $? -eq 0 ]; then
169		log_success_msg $NAME
170		if [ -d /var/lock/subsys ]; then
171			rm -f /var/lock/subsys/$NAME
172		fi
173	else
174		log_failure_msg $NAME
175	fi
176	echo
177}
178
179od_reload() {
180	echo -n "Reloading OpenDKIM Milter configuration: "
181	od_killproc $DAEMON USR1
182	if [ $? -eq 0 ]; then
183		log_success_msg $NAME
184	else
185		log_failure_msg $NAME
186	fi
187	echo
188}
189
190case "$1" in
191  start)
192  	od_start
193	;;
194
195  stop)
196  	od_stop
197	;;
198
199  restart|force-reload)
200  	od_stop
201	od_start
202	;;
203
204  reload)
205  	od_reload
206	;;
207
208  status)
209	od_status $NAME
210	;;
211esac
212