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.

150 lines
4.1 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. # confdir
  48. # privoxy needs read access (possibly write access)
  49. _CONFDIR=$(uci -q get privoxy.privoxy.confdir) || _CONFDIR="/etc/privoxy"
  50. chmod 755 $_CONFDIR
  51. chmod 664 $_CONFDIR/*
  52. chgrp privoxy $_CONFDIR $_CONFDIR/*
  53. echo -e "confdir\t$_CONFDIR" >> $CFGTEMP
  54. # templdir
  55. # privoxy need read access
  56. _TEMPLDIR=$(uci -q get privoxy.privoxy.templdir) # no default needed
  57. if [ -z "$_TEMPLDIR" ]; then
  58. chmod 755 $_CONFDIR/templates
  59. chmod 644 $_CONFDIR/templates/*
  60. chgrp privoxy $_CONFDIR/templates $_CONFDIR/templates/*
  61. else
  62. chmod 755 $_TEMPLDIR
  63. chmod 644 $_TEMPLDIR/*
  64. chgrp privoxy $_TEMPLDIR $_TEMPLDIR/*
  65. echo -e "templdir\t$_TEMPLDIR" >> $CFGTEMP
  66. fi
  67. # logdir and logfile
  68. # privoxy needs read/write access
  69. _LOGDIR=$(uci -q get privoxy.privoxy.logdir) || _LOGDIR="/var/log"
  70. _LOGFILE=$(uci -q get privoxy.privoxy.logfile) || _LOGFILE="privoxy.log"
  71. mkdir -m0755 -p $_LOGDIR
  72. touch $_LOGDIR/$_LOGFILE
  73. chmod 664 $_LOGDIR/$_LOGFILE
  74. chown privoxy:privoxy $_LOGDIR/$_LOGFILE
  75. echo -e "logdir\t$_LOGDIR" >> $CFGTEMP
  76. echo -e "logfile\t$_LOGFILE" >> $CFGTEMP
  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. return 0 # will be started by "iface" hotplug events
  92. }
  93. start() {
  94. # if already running do nothing
  95. local _PID=$(cat $PIDFILE 2>/dev/null)
  96. kill -1 $_PID 2>/dev/null && return 0
  97. _uci2conf
  98. /usr/sbin/privoxy --pidfile $PIDFILE --user privoxy.privoxy $CFGFILE
  99. # verify startup
  100. _PID=$(cat $PIDFILE 2>/dev/null)
  101. kill -1 $_PID 2>/dev/null
  102. local _ERR=$?
  103. [ $_ERR -eq 0 ] \
  104. && logger -p daemon.notice -t "privoxy[$_PID]" "Started successfully"\
  105. || logger -p daemon.warn -t "privoxy[-----]" "Failed to start"
  106. return $_ERR
  107. }
  108. reload() {
  109. # reload is also used by luci-app-privoxy
  110. local _PID=$(cat $PIDFILE 2>/dev/null)
  111. kill -1 $_PID 2>/dev/null
  112. if [ $? -eq 0 ]; then
  113. # only restart if already running
  114. restart
  115. else
  116. # only start if enabled
  117. enabled && start
  118. fi
  119. return 0
  120. }
  121. stop() {
  122. local _PID=$(cat $PIDFILE 2>/dev/null)
  123. kill -15 $_PID 2>/dev/null
  124. sleep 1 # give time to shutdown
  125. local _tmp=$(pgrep privoxy | tr "\n" " ")
  126. if [ -z "$_tmp" ]; then
  127. logger -p daemon.notice -t "privoxy[$_PID]" "Shutdown successfully"
  128. else
  129. kill -9 $_tmp # Normally never come here
  130. logger -p daemon.warn -t "privoxy[-----]" "Shutdown forced by KILL"
  131. fi
  132. return 0
  133. }