aboutsummaryrefslogtreecommitdiff
path: root/net/privoxy/files/privoxy.init
blob: ae493c2bd6326d57773b0ef8b1dfff3549910910 (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
#!/bin/sh /etc/rc.common

START=95
STOP=10

PIDFILE=/var/run/privoxy.pid
CFGFILE=/var/etc/privoxy.conf
CFGTEMP=/var/etc/privoxy.conf.tmp

_uci2conf() {
	# redefined callback for options when calling config_load
	config_cb() {
		if [ ."$2" != ."privoxy" ]; then
			option_cb() { return 0; }
		else
			option_cb()
			{
				# $1	name of variable
				# $2	value
				local __OPT="$1"
				local __VAL="$2"
				case $__OPT in
					confdir|templdir|temporary_directory|logdir|logfile)
						# needs to be handled separately because we need to set permissions
						# AND needs to be defined first because of a BUG inside privoxy
						# require directories to be defined first inside config 
						;;
					debug_*)
						[ $__VAL -eq 0 ] && return	# not set ignore
						echo -e "debug\t$(echo $__OPT | sed -e 's#debug_##g')" >> $CFGTEMP ;;
					*)
						# detect list options (LENGTH) and ignore
						echo $__OPT | grep -i "_LENGTH" >/dev/null 2>&1 && return
						# detect list options (ITEM) and ignore
						echo $__OPT | grep -i "_ITEM" >/dev/null 2>&1 && __OPT=$(echo $__OPT | sed -e "s#_ITEM.*##g")
						# uci only accept "_" but we need "-"
						local __OPT=$(echo $__OPT | sed -e "s#_#-#g")
						# write to config
						echo -e "$__OPT\t$__VAL" >> $CFGTEMP
						;;
				esac
			}

			list_cb()
			{
				option_cb "$@"
			}
		fi
	}

	# temporary config file
	# privoxy need read access
	mkdir -m0755 -p /var/etc
	echo "" > $CFGTEMP
	chmod 644 $CFGTEMP
	chgrp privoxy $CFGTEMP

	echo '### AUTO-GENERATED CONFIGURATION'	   >> $CFGTEMP
	echo '### USED BY PRIVOXY'		   >> $CFGTEMP
	echo '### DO NOT EDIT'			   >> $CFGTEMP
	echo '### SEE /etc/config/privoxy INSTEAD' >> $CFGTEMP
	echo ''					   >> $CFGTEMP

	# logdir and logfile
	# privoxy needs read/write access
	_LOGDIR=$(uci -q get privoxy.privoxy.logdir)   || _LOGDIR="/var/log"
	_LOGFILE=$(uci -q get privoxy.privoxy.logfile) || _LOGFILE="privoxy.log"
	mkdir -m0755 -p $_LOGDIR
	touch $_LOGDIR/$_LOGFILE
	chmod 664 $_LOGDIR/$_LOGFILE
	chown privoxy:privoxy $_LOGDIR/$_LOGFILE
	echo -e "logdir\t$_LOGDIR" >> $CFGTEMP
	echo -e "logfile\t$_LOGFILE" >> $CFGTEMP

	# confdir
	# privoxy needs read access (possibly write access)
	_CONFDIR=$(uci -q get privoxy.privoxy.confdir) || _CONFDIR="/etc/privoxy"
	chmod 755 $_CONFDIR
	chmod 664 $_CONFDIR/*
	chgrp privoxy $_CONFDIR $_CONFDIR/*
	echo -e "confdir\t$_CONFDIR" >> $CFGTEMP

	# templdir
	# privoxy need read access
	_TEMPLDIR=$(uci -q get privoxy.privoxy.templdir)	# no default needed
	if [ -z "$_TEMPLDIR" ]; then
		chmod 755 $_CONFDIR/templates
		chmod 644 $_CONFDIR/templates/*
		chgrp privoxy $_CONFDIR/templates $_CONFDIR/templates/*
	else
		chmod 755 $_TEMPLDIR
		chmod 644 $_TEMPLDIR/*
		chgrp privoxy $_TEMPLDIR $_TEMPLDIR/*
		echo -e "templdir\t$_TEMPLDIR" >> $CFGTEMP
	fi

	# temporary-directory
	# privoxy needs read/write access
	_TMP_DIR=$(uci -q get privoxy.privoxy.temporary_directory)	# no default needed
	if [ -n "$_TMP_DIR" ]; then
		mkdir -m0750 -p $_TMP_DIR
		chown privoxy:privoxy $_TMP_DIR
		echo -e "temporary-directory\t$_TMP_DIR" >> $CFGTEMP
	fi

	config_load "privoxy"	# calling above option_cb() and write the rest into $CFGTEMP

	# move temp to final privoxy readable configuration
	mv -f $CFGTEMP $CFGFILE
	return 0
}

boot() {
	# wait a given time (default 10 seconds) before startup
	# to wait for interfaces to come up / not using hotplug events during boot
	_start() {
		[ $1 -gt 0 ] && {
			logger -p daemon.info -t "privoxy[]" "Scheduled startup in $1 seconds"
			sleep $1
		}
		start
	}

	local _DELAY
	_DELAY=$(uci_get "privoxy" "system" "boot_delay" "10")
	_start $_DELAY &
	return 0
}

shutdown() {
	rm -f /tmp/privoxy.hotplug
	stop
}

start() {
	# if already running do nothing
	local _PID=$(cat $PIDFILE 2>/dev/null)
	kill -1 $_PID 2>/dev/null && return 0

	_uci2conf
	/usr/sbin/privoxy --pidfile $PIDFILE --user privoxy.privoxy $CFGFILE
	touch /tmp/privoxy.hotplug

	# verify startup
	_PID=$(cat $PIDFILE 2>/dev/null)
	kill -1 $_PID 2>/dev/null
	local _ERR=$?
	[ $_ERR -eq 0 ] \
		&& logger -p daemon.notice -t "privoxy[$_PID]" "Started successfully"\
		|| logger -p daemon.warn -t "privoxy[]" "Failed to start"
	return $_ERR
}

reload() {
	# reload is also used by luci-app-privoxy
	local _PID=$(cat $PIDFILE 2>/dev/null)
	kill -1 $_PID 2>/dev/null
	if [ $? -eq 0 ]; then
		# only restart if already running
		restart
	else
		# only start if enabled
		enabled && start
	fi
	return 0
}

stop() {
	local _PID=$(cat $PIDFILE 2>/dev/null)
	kill -15 $_PID 2>/dev/null
	sleep 1			# give time to shutdown
	local _tmp=$(pgrep /usr/sbin/privoxy | tr "\n" " ")
	if [ -z "$_tmp" ]; then
		logger -p daemon.notice -t "privoxy[$_PID]" "Shutdown successfully"
	else
		kill -9 $_tmp	# Normally never come here
		logger -p daemon.warn -t "privoxy[$_tmp]" "Shutdown forced by KILL"
	fi
	return 0
}