fork 0k-charms
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.

266 lines
7.3 KiB

  1. #!/bin/sh -e
  2. ### BEGIN INIT INFO
  3. # Provides: openvpn
  4. # Required-Start: $network $remote_fs $syslog
  5. # Required-Stop: $network $remote_fs $syslog
  6. # Should-Start: network-manager
  7. # Should-Stop: network-manager
  8. # X-Start-Before: $x-display-manager gdm kdm xdm wdm ldm sdm nodm
  9. # X-Interactive: true
  10. # Default-Start: 2 3 4 5
  11. # Default-Stop: 0 1 6
  12. # Short-Description: Openvpn VPN service
  13. ### END INIT INFO
  14. # Original version by Robert Leslie
  15. # <rob@mars.org>, edited by iwj and cs
  16. # Modified for openvpn by Alberto Gonzalez Iniesta <agi@inittab.org>
  17. # Modified for restarting / starting / stopping single tunnels by Richard Mueller <mueller@teamix.net>
  18. . /lib/lsb/init-functions
  19. test $DEBIAN_SCRIPT_DEBUG && set -v -x
  20. DAEMON=/usr/sbin/openvpn
  21. DESC="virtual private network daemon"
  22. CONFIG_DIR=/etc/openvpn
  23. test -x $DAEMON || exit 0
  24. test -d $CONFIG_DIR || exit 0
  25. # Source defaults file; edit that file to configure this script.
  26. AUTOSTART="all"
  27. STATUSREFRESH=10
  28. if test -e /etc/default/openvpn ; then
  29. . /etc/default/openvpn
  30. fi
  31. start_vpn () {
  32. if grep -q '^[ ]*daemon' $CONFIG_DIR/$NAME.conf ; then
  33. # daemon already given in config file
  34. DAEMONARG=
  35. else
  36. # need to daemonize
  37. DAEMONARG="--daemon ovpn-$NAME"
  38. fi
  39. if grep -q '^[ ]*status ' $CONFIG_DIR/$NAME.conf ; then
  40. # status file already given in config file
  41. STATUSARG=""
  42. elif test $STATUSREFRESH -eq 0 ; then
  43. # default status file disabled in /etc/default/openvpn
  44. STATUSARG=""
  45. else
  46. # prepare default status file
  47. STATUSARG="--status /var/run/openvpn/$NAME.status $STATUSREFRESH"
  48. fi
  49. log_progress_msg "$NAME"
  50. STATUS=0
  51. mkdir -p /var/run/openvpn
  52. mkdir -p /var/log/openvpn
  53. start-stop-daemon --start --quiet --oknodo \
  54. --pidfile /var/run/openvpn.$NAME.pid \
  55. --exec $DAEMON -- $OPTARGS --writepid /var/run/openvpn.$NAME.pid \
  56. $DAEMONARG $STATUSARG --cd $CONFIG_DIR \
  57. --config $CONFIG_DIR/$NAME.conf \
  58. --log-append /var/log/openvpn/$NAME.log || STATUS=1
  59. }
  60. stop_vpn () {
  61. kill `cat $PIDFILE` || true
  62. rm -f $PIDFILE
  63. rm -f /var/run/openvpn/$NAME.status 2> /dev/null
  64. }
  65. case "$1" in
  66. start)
  67. log_daemon_msg "Starting $DESC"
  68. # autostart VPNs
  69. if test -z "$2" ; then
  70. # check if automatic startup is disabled by AUTOSTART=none
  71. if test "x$AUTOSTART" = "xnone" -o -z "$AUTOSTART" ; then
  72. log_warning_msg " Autostart disabled."
  73. exit 0
  74. fi
  75. if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
  76. # all VPNs shall be started automatically
  77. for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do
  78. NAME=${CONFIG%%.conf}
  79. start_vpn
  80. done
  81. else
  82. # start only specified VPNs
  83. for NAME in $AUTOSTART ; do
  84. if test -e $CONFIG_DIR/$NAME.conf ; then
  85. start_vpn
  86. else
  87. log_failure_msg "No such VPN: $NAME"
  88. STATUS=1
  89. fi
  90. done
  91. fi
  92. #start VPNs from command line
  93. else
  94. while shift ; do
  95. [ -z "$1" ] && break
  96. if test -e $CONFIG_DIR/$1.conf ; then
  97. NAME=$1
  98. start_vpn
  99. else
  100. log_failure_msg " No such VPN: $1"
  101. STATUS=1
  102. fi
  103. done
  104. fi
  105. log_end_msg ${STATUS:-0}
  106. ;;
  107. stop)
  108. log_daemon_msg "Stopping $DESC"
  109. if test -z "$2" ; then
  110. for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
  111. NAME=`echo $PIDFILE | cut -c18-`
  112. NAME=${NAME%%.pid}
  113. stop_vpn
  114. log_progress_msg "$NAME"
  115. done
  116. else
  117. while shift ; do
  118. [ -z "$1" ] && break
  119. if test -e /var/run/openvpn.$1.pid ; then
  120. PIDFILE=`ls /var/run/openvpn.$1.pid 2> /dev/null`
  121. NAME=`echo $PIDFILE | cut -c18-`
  122. NAME=${NAME%%.pid}
  123. stop_vpn
  124. log_progress_msg "$NAME"
  125. else
  126. log_failure_msg " (failure: No such VPN is running: $1)"
  127. fi
  128. done
  129. fi
  130. log_end_msg 0
  131. ;;
  132. # Only 'reload' running VPNs. New ones will only start with 'start' or 'restart'.
  133. reload|force-reload)
  134. log_daemon_msg "Reloading $DESC"
  135. for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
  136. NAME=`echo $PIDFILE | cut -c18-`
  137. NAME=${NAME%%.pid}
  138. # If openvpn if running under a different user than root we'll need to restart
  139. if egrep '^[[:blank:]]*user[[:blank:]]' $CONFIG_DIR/$NAME.conf > /dev/null 2>&1 ; then
  140. stop_vpn
  141. sleep 1
  142. start_vpn
  143. log_progress_msg "(restarted)"
  144. else
  145. kill -HUP `cat $PIDFILE` || true
  146. log_progress_msg "$NAME"
  147. fi
  148. done
  149. log_end_msg 0
  150. ;;
  151. # Only 'soft-restart' running VPNs. New ones will only start with 'start' or 'restart'.
  152. soft-restart)
  153. log_daemon_msg "$DESC sending SIGUSR1"
  154. for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
  155. NAME=`echo $PIDFILE | cut -c18-`
  156. NAME=${NAME%%.pid}
  157. kill -USR1 `cat $PIDFILE` || true
  158. log_progress_msg "$NAME"
  159. done
  160. log_end_msg 0
  161. ;;
  162. restart)
  163. shift
  164. $0 stop ${@}
  165. sleep 1
  166. $0 start ${@}
  167. ;;
  168. cond-restart)
  169. log_daemon_msg "Restarting $DESC."
  170. for PIDFILE in `ls /var/run/openvpn.*.pid 2> /dev/null`; do
  171. NAME=`echo $PIDFILE | cut -c18-`
  172. NAME=${NAME%%.pid}
  173. stop_vpn
  174. sleep 1
  175. start_vpn
  176. done
  177. log_end_msg 0
  178. ;;
  179. status)
  180. GLOBAL_STATUS=0
  181. if test -z "$2" ; then
  182. # We want status for all defined VPNs.
  183. # Returns success if all autostarted VPNs are defined and running
  184. if test "x$AUTOSTART" = "xnone" ; then
  185. # Consider it a failure if AUTOSTART=none
  186. log_warning_msg "No VPN autostarted"
  187. GLOBAL_STATUS=1
  188. else
  189. if ! test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
  190. # Consider it a failure if one of the autostarted VPN is not defined
  191. for VPN in $AUTOSTART ; do
  192. if ! test -f $CONFIG_DIR/$VPN.conf ; then
  193. log_warning_msg "VPN '$VPN' is in AUTOSTART but is not defined"
  194. GLOBAL_STATUS=1
  195. fi
  196. done
  197. fi
  198. fi
  199. for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do
  200. NAME=${CONFIG%%.conf}
  201. # Is it an autostarted VPN ?
  202. if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
  203. AUTOVPN=1
  204. else
  205. if test "x$AUTOSTART" = "xnone" ; then
  206. AUTOVPN=0
  207. else
  208. AUTOVPN=0
  209. for VPN in $AUTOSTART; do
  210. if test "x$VPN" = "x$NAME" ; then
  211. AUTOVPN=1
  212. fi
  213. done
  214. fi
  215. fi
  216. if test "x$AUTOVPN" = "x1" ; then
  217. # If it is autostarted, then it contributes to global status
  218. status_of_proc -p /var/run/openvpn.${NAME}.pid openvpn "VPN '${NAME}'" || GLOBAL_STATUS=1
  219. else
  220. status_of_proc -p /var/run/openvpn.${NAME}.pid openvpn "VPN '${NAME}' (non autostarted)" || true
  221. fi
  222. done
  223. else
  224. # We just want status for specified VPNs.
  225. # Returns success if all specified VPNs are defined and running
  226. while shift ; do
  227. [ -z "$1" ] && break
  228. NAME=$1
  229. if test -e $CONFIG_DIR/$NAME.conf ; then
  230. # Config exists
  231. status_of_proc -p /var/run/openvpn.${NAME}.pid openvpn "VPN '${NAME}'" || GLOBAL_STATUS=1
  232. else
  233. # Config does not exist
  234. log_warning_msg "VPN '$NAME': missing $CONFIG_DIR/$NAME.conf file !"
  235. GLOBAL_STATUS=1
  236. fi
  237. done
  238. fi
  239. exit $GLOBAL_STATUS
  240. ;;
  241. *)
  242. echo "Usage: $0 {start|stop|reload|restart|force-reload|cond-restart|soft-restart|status}" >&2
  243. exit 1
  244. ;;
  245. esac
  246. exit 0
  247. # vim:set ai sts=2 sw=2 tw=0: