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.

175 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. fi
  41. }
  42. # temporary config file
  43. # privoxy need read access
  44. mkdir -m0755 -p /var/etc
  45. echo "" > $CFGTEMP
  46. chmod 644 $CFGTEMP
  47. chgrp privoxy $CFGTEMP
  48. echo '### AUTO-GENERATED CONFIGURATION' >> $CFGTEMP
  49. echo '### USED BY PRIVOXY' >> $CFGTEMP
  50. echo '### DO NOT EDIT' >> $CFGTEMP
  51. echo '### SEE /etc/config/privoxy INSTEAD' >> $CFGTEMP
  52. echo '' >> $CFGTEMP
  53. # logdir and logfile
  54. # privoxy needs read/write access
  55. _LOGDIR=$(uci -q get privoxy.privoxy.logdir) || _LOGDIR="/var/log"
  56. _LOGFILE=$(uci -q get privoxy.privoxy.logfile) || _LOGFILE="privoxy.log"
  57. mkdir -m0755 -p $_LOGDIR
  58. touch $_LOGDIR/$_LOGFILE
  59. chmod 664 $_LOGDIR/$_LOGFILE
  60. chown privoxy:privoxy $_LOGDIR/$_LOGFILE
  61. echo -e "logdir\t$_LOGDIR" >> $CFGTEMP
  62. echo -e "logfile\t$_LOGFILE" >> $CFGTEMP
  63. # confdir
  64. # privoxy needs read access (possibly write access)
  65. _CONFDIR=$(uci -q get privoxy.privoxy.confdir) || _CONFDIR="/etc/privoxy"
  66. chmod 755 $_CONFDIR
  67. chmod 664 $_CONFDIR/*
  68. chgrp privoxy $_CONFDIR $_CONFDIR/*
  69. echo -e "confdir\t$_CONFDIR" >> $CFGTEMP
  70. # templdir
  71. # privoxy need read access
  72. _TEMPLDIR=$(uci -q get privoxy.privoxy.templdir) # no default needed
  73. if [ -z "$_TEMPLDIR" ]; then
  74. chmod 755 $_CONFDIR/templates
  75. chmod 644 $_CONFDIR/templates/*
  76. chgrp privoxy $_CONFDIR/templates $_CONFDIR/templates/*
  77. else
  78. chmod 755 $_TEMPLDIR
  79. chmod 644 $_TEMPLDIR/*
  80. chgrp privoxy $_TEMPLDIR $_TEMPLDIR/*
  81. echo -e "templdir\t$_TEMPLDIR" >> $CFGTEMP
  82. fi
  83. # temporary-directory
  84. # privoxy needs read/write access
  85. _TMP_DIR=$(uci -q get privoxy.privoxy.temporary_directory) # no default needed
  86. if [ -n "$_TMP_DIR" ]; then
  87. mkdir -m0750 -p $_TMP_DIR
  88. chown privoxy:privoxy $_TMP_DIR
  89. echo -e "temporary-directory\t$_TMP_DIR" >> $CFGTEMP
  90. fi
  91. config_load "privoxy" # calling above option_cb() and write the rest into $CFGTEMP
  92. # move temp to final privoxy readable configuration
  93. mv -f $CFGTEMP $CFGFILE
  94. return 0
  95. }
  96. boot() {
  97. # wait a given time (default 10 seconds) before startup
  98. # to wait for interfaces to come up / not using hotplug events during boot
  99. _start() {
  100. [ $1 -gt 0 ] && {
  101. logger -p daemon.info -t "privoxy[]" "Scheduled startup in $1 seconds"
  102. sleep $1
  103. }
  104. start
  105. }
  106. local _DELAY
  107. _DELAY=$(uci_get "privoxy" "system" "boot_delay" "10")
  108. _start $_DELAY &
  109. return 0
  110. }
  111. shutdown() {
  112. rm -f /tmp/privoxy.hotplug
  113. stop
  114. }
  115. start() {
  116. # if already running do nothing
  117. local _PID=$(cat $PIDFILE 2>/dev/null)
  118. kill -1 $_PID 2>/dev/null && return 0
  119. _uci2conf
  120. /usr/sbin/privoxy --pidfile $PIDFILE --user privoxy.privoxy $CFGFILE
  121. touch /tmp/privoxy.hotplug
  122. # verify startup
  123. _PID=$(cat $PIDFILE 2>/dev/null)
  124. kill -1 $_PID 2>/dev/null
  125. local _ERR=$?
  126. [ $_ERR -eq 0 ] \
  127. && logger -p daemon.notice -t "privoxy[$_PID]" "Started successfully"\
  128. || logger -p daemon.warn -t "privoxy[]" "Failed to start"
  129. return $_ERR
  130. }
  131. reload() {
  132. # reload is also used by luci-app-privoxy
  133. local _PID=$(cat $PIDFILE 2>/dev/null)
  134. kill -1 $_PID 2>/dev/null
  135. if [ $? -eq 0 ]; then
  136. # only restart if already running
  137. restart
  138. else
  139. # only start if enabled
  140. enabled && start
  141. fi
  142. return 0
  143. }
  144. stop() {
  145. local _PID=$(cat $PIDFILE 2>/dev/null)
  146. kill -15 $_PID 2>/dev/null
  147. sleep 1 # give time to shutdown
  148. local _tmp=$(pgrep privoxy | tr "\n" " ")
  149. if [ -z "$_tmp" ]; then
  150. logger -p daemon.notice -t "privoxy[$_PID]" "Shutdown successfully"
  151. else
  152. kill -9 $_tmp # Normally never come here
  153. logger -p daemon.warn -t "privoxy[$_tmp]" "Shutdown forced by KILL"
  154. fi
  155. return 0
  156. }