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.

169 lines
4.5 KiB

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