aboutsummaryrefslogtreecommitdiffstats
path: root/debian/fetchmail.init
blob: a6fbcb0efcf7c96acbe84da63ab88ff0ab320c83 (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
#!/bin/sh
#
# Fetchmail init script
# Latest change: Do Aug 30 16:04:39 CEST 2012
#
### BEGIN INIT INFO
# Provides:          fetchmail
# Required-Start:    $network $local_fs $remote_fs $syslog
# Required-Stop:     $remote_fs
# Should-Start:      $mail-transport-agent postfix exim4 $named
# Should-Stop:       $mail-transport-agent postfix exim4
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: init-Script for system wide fetchmail daemon
# Description: Enable service provided by fetchmail in daemon mode
### END INIT INFO
#
# A fetchmailrc file containg hosts and passwords for all local users should be
# placed in /etc/fetchmailrc.  Remember to make the /etc/fetchmailrc mode 600
# to avoid disclosing the users' passwords.
#
# This script will NOT start or stop fetchmail if the /etc/fetchmailrc file
# does not exist or if START_DAEMON in /etc/default/fetchmail is set to no.

set -e

# Defaults
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/fetchmail
USER=fetchmail
OPTIONS=""

CONFFILE="/etc/fetchmailrc"
PIDFILE="/var/run/fetchmail/fetchmail.pid"
UIDL="/var/lib/fetchmail/.fetchmail-UIDL-cache"
START_DAEMON="no"

. /lib/lsb/init-functions

if [ -r /etc/default/fetchmail ]; then
    . /etc/default/fetchmail
fi

OPTIONS="$OPTIONS -f $CONFFILE --pidfile $PIDFILE"

if [ ! "x$START_DAEMON" = "xyes" -a ! "$1" = "status" ]; then
	log_action_msg "Not starting fetchmail daemon, disabled via /etc/default/fetchmail"
	exit 0
fi

if [ ! -e $CONFFILE ]; then
    log_failure_msg "$CONFFILE not found."
    log_failure_msg "can not start fetchmail daemon... consider disabling the script"
    exit 6
fi


test -f $DAEMON || exit 0

if [ "$1" = "start" ]; then
    if [ ! -r $CONFFILE ] ; then
        log_failure_msg "$CONFFILE found but not readable."
        exit 0
    fi
fi


# sanity checks (saves on MY sanity :-P )
if ! id $USER >/dev/null 2>&1; then
	if [ "$USER" = "fetchmail" ]; then
		# The fetchmail user might have been removed when the fetchmail-common
		# package is purged. We have to re-add it here so the system-wide
		# daemon will run.

		adduser --system --ingroup nogroup --home /var/lib/fetchmail \
			--shell /bin/sh --disabled-password fetchmail >/dev/null 2>&1 || true
		# work around possible adduser bug, see #119366
		[ -d /var/lib/fetchmail ] || mkdir -p /var/lib/fetchmail
		chmod 700 /var/lib/fetchmail
		chown -h -R fetchmail:nogroup /var/lib/fetchmail
		[ -x /sbin/restorecon ] && /sbin/restorecon /var/lib/fetchmail
	else
		log_failure_msg "$0: $USER user does not exist!"
		exit 1
	fi
fi

# add daemon option if user hasn't already specified it
if ! grep -qs '^[[:space:]]*set[[:space:]]\+daemon[[:space:]]' "$CONFFILE"; then
	# Make sure user did not use -d on /etc/default/fetchmail
	if ! grep -qs -e '^[[:space:]]*OPTIONS=.*-d[[:space:]]*[[:digit:]]\+' "/etc/default/fetchmail"; then
		OPTIONS="$OPTIONS -d 300"
	fi
fi

# add syslog option unless user specified "set no syslog".
if ! grep -qs '^[[:space:]]*set[[:space:]]\+no[[:space:]]\+syslog' "$CONFFILE"; then
	OPTIONS="$OPTIONS --syslog"
fi

# support for ephemeral /var/run
if [ "${PIDFILE%/*}" = "/var/run/fetchmail" ] && [ ! -d ${PIDFILE%/*} ] && [ "$1" != "status" ]; then
	mkdir /var/run/fetchmail
	chown -h $USER:nogroup /var/run/fetchmail
	chmod 700 /var/run/fetchmail
	[ -x /sbin/restorecon ] && /sbin/restorecon /var/run/fetchmail
fi

# sanity check
#if [ ! -d ${PIDFILE%/*} ]; then
#	log_failure_msg "$0: directory ${PIDFILE%/*} does not exist!"
#	exit 1
#fi

# If the user is going to use a UIDL cache, try to find a better place for the
# UIDL cache than root's homedir. Also create $UIDL if it doesn't exist,
# because the daemon won't have the permission.
# THIS IS NOT NEEDED SINCE 6.3.x
#if ! grep -qs idfile "$CONFFILE" && [ -d /var/lib/fetchmail ]; then 
#	OPTIONS="$OPTIONS -i $UIDL"
#	touch $UIDL
#	chown -h $USER $UIDL
#	chmod 0600 $UIDL
#fi

# Makes sure certain files/directories have the proper owner
if [ -f $CONFFILE -a "`stat -c '%U %a' $CONFFILE 2>/dev/null`" != "$USER 600" ]; then
	chown -h $USER $CONFFILE
	chmod -f 0600 $CONFFILE
fi

case "$1" in
	start)
		if test -e $PIDFILE ; then
			pid=`cat $PIDFILE | sed -e 's/\s.*//'|head -n1`
			PIDDIR=/proc/$pid
		    if [ -d ${PIDDIR} -a  "$(readlink -f ${PIDDIR}/exe)" = "${DAEMON}" ]; then
				log_failure_msg "fetchmail already started; not starting."
				exit 0
			else
				log_warning_msg "Removing stale PID file $PIDFILE."
				rm -f $PIDFILE
			fi
		fi
	        log_begin_msg "Starting mail retriever agent:" "fetchmail"
		if start-stop-daemon -S -o -q -x $DAEMON -u $USER -c $USER -- $OPTIONS; then
			log_end_msg 0
		else
			log_end_msg 1
			exit 1
		fi
		;;
	status)
		status_of_proc $DAEMON fetchmail -p $PIDFILE
		;;
	stop)
		if ! test -e $PIDFILE ; then
			log_failure_msg "Pidfile not found! Is fetchmail running?"
			exit 0
		fi
	        log_begin_msg "Stopping mail retriever agent:" "fetchmail"
		if start-stop-daemon -K -o -q -p $PIDFILE -x $DAEMON -u $USER; then
			log_end_msg 0
		else
			log_end_msg 1
			exit 1
		fi
		;;
	force-reload|restart)
	        log_begin_msg "Restarting mail retriever agent:" "fetchmail"
		if ! start-stop-daemon -K -o -q -p $PIDFILE -x $DAEMON -u $USER; then
			log_end_msg 1
			exit 1
		fi
		sleep 1
		if start-stop-daemon -S -q -x $DAEMON -u $USER -c $USER -- $OPTIONS; then
			log_end_msg 0
		else
			log_end_msg 1
			exit 1
		fi
		;;
	try-restart)
		if test -e $PIDFILE ; then
			pid=`cat $PIDFILE | sed -e 's/\s.*//'|head -n1`
			PIDDIR=/proc/$pid
			if [ -d ${PIDDIR} -a  "$(readlink -f ${PIDDIR}/exe)" = "${DAEMON}" ]; then
				$0 restart
				exit 0
			fi
		fi
		test -f /etc/rc`/sbin/runlevel | cut -d' ' -f2`.d/S*fetchmail* && $0 start
		;;
	awaken)
	        log_begin_msg "Awakening mail retriever agent:" "fetchmail"
		if [ -s $PIDFILE ]; then
			start-stop-daemon -K -s 10 -q -p $PIDFILE -x $DAEMON
			log_end_msg 0
			exit 0
		else
			log_end_msg 1
			exit 1
		fi
		;;
	debug-run)
		echo "$0: Initiating debug run of system-wide fetchmail service..." 1>&2
		echo "$0: script will be run in debug mode, all output to forced to" 1>&2
		echo "$0: stdout. This is not enough to debug failures that only" 1>&2
		echo "$0: happen in daemon mode." 1>&2
		echo "$0: You might want to direct output to a file, and tail -f it." 1>&2
		if [ "$2" = "strace" ]; then
			echo "$0: (running debug mode under strace. See strace(1) for options)" 1>&2
			echo "$0: WARNING: strace output may contain security-sensitive info, such as" 1>&2
			echo "$0: passwords; please clobber them before sending the strace file to a" 1>&2
			echo "$0: public bug tracking system, such as Debian's." 1>&2
		fi
		echo "$0: Stopping the service..." 1>&2
		"$0" stop
		echo "$0: exit status of service stop was: $?"
		echo "$0: RUNUSER is $USER"
		echo "$0: OPTIONS would be $OPTIONS"
		echo "$0: Starting service in nodetach mode, hit ^C (SIGINT/intr) to finish run..." 1>&2
		if [ "$2" = "strace" ] ; then
			shift
			shift
			[ $# -ne 0 ] && echo "$0: (strace options are: -tt $@)" 1>&2
			su -s /bin/sh -c "/usr/bin/strace -tt $* $DAEMON $OPTIONS --nosyslog --nodetach -v -v" $USER <&- 2>&1
		else
			su -s /bin/sh -c "$DAEMON $OPTIONS --nosyslog --nodetach -v -v" $USER <&- 2>&1
		fi
		echo "$0: End of service run. Exit status was: $?"
		exit 0
		;;
	*)
		log_warning_msg "Usage: /etc/init.d/fetchmail {start|stop|restart|force-reload|awaken|debug-run}"
		log_warning_msg "  start - starts system-wide fetchmail service"
		log_warning_msg "  stop  - stops system-wide fetchmail service"
		log_warning_msg "  restart, force-reload - starts a new system-wide fetchmail service"
		log_warning_msg "  awaken - tell system-wide fetchmail to start a poll cycle immediately"
		log_warning_msg "  debug-run [strace [strace options...]] - start a debug run of the"
		log_warning_msg "    system-wide fetchmail service, optionally running it under strace"
		exit 1
		;;
esac

exit 0

# vim:ts=4:sw=4: