summaryrefslogtreecommitdiff
blob: 9f102ed4ba25e93032423688d5b190a8c41f7c67 (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
#!/sbin/runscript
# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/net-misc/tor/files/tor.initd-r5,v 1.3 2011/08/30 13:57:42 blueness Exp $

opts="${opts} checkconfig checkvarrun reload"
PIDDIR="/var/run/tor"
PIDFILE="${PIDDIR}/tor.pid"
CONFFILE="/etc/tor/torrc"

depend() {
        need net
}

checkvarrun()
{
	# Check if /var/run/tor exists, create it if it doesn't 
	[ ! -d /var/run/tor ] && mkdir -p ${PIDDIR}
	[ ! -d /var/run/tor ] && return 1

	# The directory definitly exists now, but does it belong to tor:tor?
	local dir=$(find ${PIDDIR} -maxdepth 0 -a -user tor -a -group tor)
	[ -z "$dir" ] && chown tor:tor ${PIDDIR}
	dir=$(find ${PIDDIR} -maxdepth 0 -a -user tor -a -group tor)
	[ -z "$dir" ] && return 1

	# And does it have the right perms?
	dir=$(find ${PIDDIR} -maxdepth 0 -perm 0755)
	[ -z "$dir" ] && chmod 0755 ${PIDDIR}
	dir=$(find ${PIDDIR} -maxdepth 0 -perm 0755)
	[ -z "$dir" ] && return 1

	# If any of the above failed, we would not be here
	return 0
}

checkconfig() {
        # first check that it exists
        if [ ! -f ${CONFFILE} ] ; then
                eerror "You need to setup ${CONFFILE} first"
                eerror "Example is in ${CONFFILE}.sample"
                return 1
	fi

	# now verify whether the configuration is valid
	/usr/bin/tor --verify-config -f ${CONFFILE} > /dev/null 2>&1
	if [ $? -eq 0 ] ; then
		einfo "Tor configuration (${CONFFILE}) is valid."
		return 0
	else
		eerror "Tor configuration (${CONFFILE}) not valid."
		/usr/bin/tor --verify-config -f ${CONFFILE}
		return 1
	fi
}

start() {
	checkconfig || return 1
	checkvarrun || return 1
        ebegin "Starting Tor"
        HOME=/var/lib/tor
	start-stop-daemon --start --pidfile "${PIDFILE}" --quiet --exec /usr/bin/tor  -- -f "${CONFFILE}" --runasdaemon 1 --PidFile "${PIDFILE}" > /dev/null 2>&1
        eend $?
}

stop() {
        ebegin "Stopping Tor"
	start-stop-daemon --stop --pidfile "${PIDFILE}" --exec /usr/bin/tor -- --PidFile "${PIDFILE}"
        eend $?
}

reload() {
	if [ ! -f ${PIDFILE} ]; then
		eerror "${SVCNAME} isn't running"
		return 1
	fi
	checkconfig || return 1
	ebegin "Reloading Tor configuration"
	start-stop-daemon --signal HUP --pidfile ${PIDFILE}
	eend $?
}