You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.7 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=95
  3. STOP=10
  4. PIDFILE=/var/run/privoxy.pid
  5. CFGFILE=/var/etc/privoxy.conf
  6. CFGTEMP=/var/etc/privoxy.conf.tmp
  7. _uci2conf() {
  8. # redefined callback for options when calling config_load
  9. config_cb() {
  10. if [ ."$2" != ."privoxy" ]; then
  11. option_cb() { return 0; }
  12. else
  13. option_cb()
  14. {
  15. # $1 name of variable
  16. # $2 value
  17. local __OPT="$1"
  18. local __VAL="$2"
  19. case $__OPT in
  20. confdir|templdir|temporary_directory|logdir|logfile)
  21. # needs to be handled separately because we need to set permissions
  22. # AND needs to be defined first because of a BUG inside privoxy
  23. # require directories to be defined first inside config
  24. ;;
  25. debug_*)
  26. [ $__VAL -eq 0 ] && return # not set ignore
  27. echo -e "debug\t$(echo $__OPT | sed -e 's#debug_##g')" >> $CFGTEMP ;;
  28. *)
  29. # detect list options (LENGTH) and ignore
  30. echo $__OPT | grep -i "_LENGTH" >/dev/null 2>&1 && return
  31. # detect list options (ITEM) and ignore
  32. echo $__OPT | grep -i "_ITEM" >/dev/null 2>&1 && __OPT=$(echo $__OPT | sed -e "s#_ITEM.*##g")
  33. # uci only accept "_" but we need "-"
  34. local __OPT=$(echo $__OPT | sed -e "s#_#-#g")
  35. # write to config
  36. echo -e "$__OPT\t$__VAL" >> $CFGTEMP
  37. ;;
  38. esac
  39. }
  40. list_cb()
  41. {
  42. option_cb "$@"
  43. }
  44. fi
  45. }
  46. # temporary config file
  47. # privoxy need read access
  48. mkdir -m0755 -p /var/etc
  49. echo "" > $CFGTEMP
  50. chmod 644 $CFGTEMP
  51. chgrp privoxy $CFGTEMP
  52. echo '### AUTO-GENERATED CONFIGURATION' >> $CFGTEMP
  53. echo '### USED BY PRIVOXY' >> $CFGTEMP
  54. echo '### DO NOT EDIT' >> $CFGTEMP
  55. echo '### SEE /etc/config/privoxy INSTEAD' >> $CFGTEMP
  56. echo '' >> $CFGTEMP
  57. # logdir and logfile
  58. # privoxy needs read/write access
  59. _LOGDIR=$(uci -q get privoxy.privoxy.logdir) || _LOGDIR="/var/log"
  60. _LOGFILE=$(uci -q get privoxy.privoxy.logfile) || _LOGFILE="privoxy.log"
  61. mkdir -m0755 -p $_LOGDIR
  62. touch $_LOGDIR/$_LOGFILE
  63. chmod 664 $_LOGDIR/$_LOGFILE
  64. chown privoxy:privoxy $_LOGDIR/$_LOGFILE
  65. echo -e "logdir\t$_LOGDIR" >> $CFGTEMP
  66. echo -e "logfile\t$_LOGFILE" >> $CFGTEMP
  67. # confdir
  68. # privoxy needs read access (possibly write access)
  69. _CONFDIR=$(uci -q get privoxy.privoxy.confdir) || _CONFDIR="/etc/privoxy"
  70. chmod 755 $_CONFDIR
  71. chmod 664 $_CONFDIR/*
  72. chgrp privoxy $_CONFDIR $_CONFDIR/*
  73. echo -e "confdir\t$_CONFDIR" >> $CFGTEMP
  74. # templdir
  75. # privoxy need read access
  76. _TEMPLDIR=$(uci -q get privoxy.privoxy.templdir) # no default needed
  77. if [ -z "$_TEMPLDIR" ]; then
  78. chmod 755 $_CONFDIR/templates
  79. chmod 644 $_CONFDIR/templates/*
  80. chgrp privoxy $_CONFDIR/templates $_CONFDIR/templates/*
  81. else
  82. chmod 755 $_TEMPLDIR
  83. chmod 644 $_TEMPLDIR/*
  84. chgrp privoxy $_TEMPLDIR $_TEMPLDIR/*
  85. echo -e "templdir\t$_TEMPLDIR" >> $CFGTEMP
  86. fi
  87. # temporary-directory
  88. # privoxy needs read/write access
  89. _TMP_DIR=$(uci -q get privoxy.privoxy.temporary_directory) # no default needed
  90. if [ -n "$_TMP_DIR" ]; then
  91. mkdir -m0750 -p $_TMP_DIR
  92. chown privoxy:privoxy $_TMP_DIR
  93. echo -e "temporary-directory\t$_TMP_DIR" >> $CFGTEMP
  94. fi
  95. config_load "privoxy" # calling above option_cb() and write the rest into $CFGTEMP
  96. # move temp to final privoxy readable configuration
  97. mv -f $CFGTEMP $CFGFILE
  98. return 0
  99. }
  100. boot() {
  101. # wait a given time (default 10 seconds) before startup
  102. # to wait for interfaces to come up / not using hotplug events during boot
  103. _start() {
  104. [ $1 -gt 0 ] && {
  105. logger -p daemon.info -t "privoxy[]" "Scheduled startup in $1 seconds"
  106. sleep $1
  107. }
  108. start
  109. }
  110. local _DELAY
  111. _DELAY=$(uci_get "privoxy" "system" "boot_delay" "10")
  112. _start $_DELAY &
  113. return 0
  114. }
  115. shutdown() {
  116. rm -f /tmp/privoxy.hotplug
  117. stop
  118. }
  119. start() {
  120. # if already running do nothing
  121. local _PID=$(cat $PIDFILE 2>/dev/null)
  122. kill -1 $_PID 2>/dev/null && return 0
  123. _uci2conf
  124. /usr/sbin/privoxy --pidfile $PIDFILE --user privoxy.privoxy $CFGFILE
  125. touch /tmp/privoxy.hotplug
  126. # verify startup
  127. _PID=$(cat $PIDFILE 2>/dev/null)
  128. kill -1 $_PID 2>/dev/null
  129. local _ERR=$?
  130. [ $_ERR -eq 0 ] \
  131. && logger -p daemon.notice -t "privoxy[$_PID]" "Started successfully"\
  132. || logger -p daemon.warn -t "privoxy[]" "Failed to start"
  133. return $_ERR
  134. }
  135. reload() {
  136. # reload is also used by luci-app-privoxy
  137. local _PID=$(cat $PIDFILE 2>/dev/null)
  138. kill -1 $_PID 2>/dev/null
  139. if [ $? -eq 0 ]; then
  140. # only restart if already running
  141. restart
  142. else
  143. # only start if enabled
  144. enabled && start
  145. fi
  146. return 0
  147. }
  148. stop() {
  149. local _PID=$(cat $PIDFILE 2>/dev/null)
  150. kill -15 $_PID 2>/dev/null
  151. sleep 1 # give time to shutdown
  152. local _tmp=$(pgrep privoxy | tr "\n" " ")
  153. if [ -z "$_tmp" ]; then
  154. logger -p daemon.notice -t "privoxy[$_PID]" "Shutdown successfully"
  155. else
  156. kill -9 $_tmp # Normally never come here
  157. logger -p daemon.warn -t "privoxy[$_tmp]" "Shutdown forced by KILL"
  158. fi
  159. return 0
  160. }