stunnel5.init 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #! /bin/sh -e
  2. ### BEGIN INIT INFO
  3. # Provides: stunnel
  4. # Required-Start: $local_fs $remote_fs
  5. # Required-Stop: $local_fs $remote_fs
  6. # Should-Start: $syslog
  7. # Should-Stop: $syslog
  8. # Default-Start: 2 3 4 5
  9. # Default-Stop: 0 1 6
  10. # Short-Description: Start or stop stunnel 4.x (TLS tunnel for network daemons)
  11. # Description: Starts or stops all configured TLS network tunnels. Each *.conf file in
  12. # /usr/local/etc/stunnel/ will spawn a separate stunnel process. The list of files
  13. # can be overridden in /usr/local/etc/default/stunnel, and that same file can be used
  14. # to completely disable *all* tunnels.
  15. ### END INIT INFO
  16. . /lib/lsb/init-functions
  17. DEFAULTPIDFILE="/var/run/stunnel.pid"
  18. DAEMON=/usr/local/lamvpn/stunnel5
  19. NAME=stunnel5
  20. DESC="Stunnel5 Service"
  21. OPTIONS=""
  22. get_opt() {
  23. sed -e "s;^[[:space:]]*;;" -e "s;[[:space:]]*$;;" \
  24. -e "s;[[:space:]]*=[[:space:]]*;=;" "$1" |
  25. grep -i "^$2=" | sed -e "s;^[^=]*=;;"
  26. }
  27. get_pidfile() {
  28. local file=$1
  29. if [ -f $file ]; then
  30. CHROOT=`get_opt $file chroot`
  31. PIDFILE=`get_opt $file pid`
  32. if [ "$PIDFILE" = "" ]; then
  33. PIDFILE=$DEFAULTPIDFILE
  34. fi
  35. echo "$CHROOT/$PIDFILE"
  36. fi
  37. }
  38. startdaemons() {
  39. local res file args pidfile warn status
  40. if ! [ -d /var/run/stunnel ]; then
  41. rm -rf /var/run/stunnel
  42. install -d -o stunnel -g stunnel /var/run/stunnel
  43. fi
  44. if [ -n "$RLIMITS" ]; then
  45. ulimit $RLIMITS
  46. fi
  47. res=0
  48. for file in $FILES; do
  49. if [ -f $file ]; then
  50. echo -n " $file: "
  51. args="$file $OPTIONS"
  52. pidfile=`get_pidfile $file`
  53. if egrep -qe '^pid[[:space:]]*=' "$file"; then
  54. warn=''
  55. else
  56. warn=' (no pid=pidfile specified!)'
  57. fi
  58. status=0
  59. start_daemon -p "$pidfile" "$DAEMON" $args || status=$?
  60. if [ "$status" -eq 0 ]; then
  61. echo -n "started$warn"
  62. else
  63. echo "failed$warn"
  64. echo "You should check that you have specified the pid= in you configuration file"
  65. res=1
  66. fi
  67. fi
  68. done;
  69. echo ''
  70. return "$res"
  71. }
  72. killdaemons()
  73. {
  74. local sig file pidfile status
  75. sig=$1
  76. res=0
  77. for file in $FILES; do
  78. echo -n " $file: "
  79. pidfile=`get_pidfile $file`
  80. if [ ! -e "$pidfile" ]; then
  81. echo -n "no pid file"
  82. else
  83. status=0
  84. killproc -p "$pidfile" "$DAEMON" ${sig:+"$sig"} || status=$?
  85. if [ "$status" -eq 0 ]; then
  86. echo -n 'stopped'
  87. else
  88. echo -n 'failed'
  89. res=1
  90. fi
  91. fi
  92. done
  93. echo ''
  94. return "$res"
  95. }
  96. querydaemons()
  97. {
  98. local res file pidfile status
  99. res=0
  100. for file in $FILES; do
  101. echo -n " $file: "
  102. pidfile=`get_pidfile "$file"`
  103. if [ ! -e "$pidfile" ]; then
  104. echo -n 'no pid file'
  105. res=1
  106. else
  107. status=0
  108. pidofproc -p "$pidfile" "$DAEMON" >/dev/null || status="$?"
  109. if [ "$status" = 0 ]; then
  110. echo -n 'running'
  111. elif [ "$status" = 4 ]; then
  112. echo "cannot access the pid file $pidfile"
  113. res=1
  114. else
  115. echo -n 'stopped'
  116. res=1
  117. fi
  118. fi
  119. done
  120. echo ''
  121. exit "$res"
  122. }
  123. restartrunningdaemons()
  124. {
  125. local res file pidfile status args
  126. res=0
  127. for file in $FILES; do
  128. echo -n " $file: "
  129. pidfile=`get_pidfile "$file"`
  130. if [ ! -e "$pidfile" ]; then
  131. echo -n 'no pid file'
  132. else
  133. status=0
  134. pidofproc -p "$pidfile" "$DAEMON" >/dev/null || status="$?"
  135. if [ "$status" = 0 ]; then
  136. echo -n 'stopping'
  137. killproc -p "$pidfile" "$DAEMON" "$sig" || status="$?"
  138. if [ "$status" -eq 0 ]; then
  139. echo -n ' starting'
  140. args="$file $OPTIONS"
  141. start_daemon -p "$pidfile" "$DAEMON" $args || status="$?"
  142. if [ "$status" -eq 0 ]; then
  143. echo -n ' started'
  144. else
  145. echo ' failed'
  146. res=1
  147. fi
  148. else
  149. echo -n ' failed'
  150. res=1
  151. fi
  152. elif [ "$status" = 4 ]; then
  153. echo "cannot access the pid file $pidfile"
  154. else
  155. echo -n 'stopped'
  156. fi
  157. fi
  158. done
  159. echo ''
  160. exit "$res"
  161. }
  162. if [ "x$OPTIONS" != "x" ]; then
  163. OPTIONS="-- $OPTIONS"
  164. fi
  165. # If the user want to manage a single tunnel, the conf file's name
  166. # is in $2. Otherwise, respect /usr/local/etc/default/stunnel4 setting.
  167. # If no setting there, use /usr/local/etc/stunnel/*.conf.
  168. if [ -n "${2:-}" ]; then
  169. if [ -e "/etc/stunnel5/stunnel5.conf" ]; then
  170. FILES="/etc/stunnel5/stunnel5.conf"
  171. fi
  172. else
  173. if [ -z "$FILES" ]; then
  174. FILES="/etc/stunnel5/*.conf"
  175. fi
  176. fi
  177. [ -x $DAEMON ] || exit 0
  178. set -e
  179. res=0
  180. case "$1" in
  181. start)
  182. echo -n "Starting $DESC:"
  183. startdaemons
  184. res=$?
  185. ;;
  186. stop)
  187. echo -n "Stopping $DESC:"
  188. killdaemons
  189. res=$?
  190. ;;
  191. reopen-logs)
  192. echo -n "Reopening log files $DESC:"
  193. killdaemons USR1
  194. res=$?
  195. ;;
  196. force-reload|reload)
  197. echo -n "Reloading configuration $DESC:"
  198. killdaemons HUP
  199. res=$?
  200. ;;
  201. restart)
  202. echo -n "Restarting $DESC:"
  203. killdaemons && startdaemons
  204. res=$?
  205. ;;
  206. try-restart)
  207. echo -n "Restarting $DESC if running:"
  208. restartrunningdaemons
  209. res=$?
  210. ;;
  211. status)
  212. echo -n "$DESC status:"
  213. querydaemons
  214. res=$?
  215. ;;
  216. *)
  217. N=/etc/init.d/$NAME
  218. echo "Usage: $N {start|stop|status|reload|reopen-logs|restart|try-restart} [<stunnel instance>]" >&2
  219. res=1
  220. ;;
  221. esac
  222. exit "$res"