#!/sbin/runscript VPNDIR="/etc/openvpn" depend() { need net } checktundevice() { if [[ -h /dev/net/tun && -c /dev/misc/net/tun ]]; then ebegin "Detected broken /dev/net/tun symlink, fixing..." rm -f /dev/net/tun ln -s /dev/misc/net/tun /dev/net/tun eend $? fi } start() { checktundevice || return 1 local vpn vpnd onestarted=false vpns for vpn in $(cd "${VPNDIR}"; ls 2>/dev/null); do [[ -d "${VPNDIR}/${vpn}" ]] && vpns="${vpns} ${vpn}" done if [[ -z ${vpns} ]]; then eerror "No VPNs have been configured in /etc/openvpn" eerror "Ensure that you create a directory there with the name of your" eerror "VPN and that it contains a configuration file called local.conf" return 1 fi for vpn in ${vpns}; do vpnd="${VPNDIR}/${vpn}" if [[ ! -e "${vpnd}/local.conf" ]]; then ewarn "Expected ${vpnd} to be a directory containing a local.conf" continue fi if [[ -s "${vpnd}/onlinecheck" ]]; then if ping -c 3 $(head -n 1 "${vpnd}/onlinecheck" ) &>/dev/null ; then einfo "Connection ${VPN} already online. Skipping..." continue fi fi local pidfile="/var/run/openvpn-${vpn}.pid" ebegin "Starting openvpn for ${vpn}" start-stop-daemon --start --exec /usr/sbin/openvpn \ --pidfile "${pidfile}" \ -- --config "${vpnd}/local.conf" --writepid "${pidfile}" \ --daemon --cd "${vpnd}" eend $? "Check your logs to see why startup failed" && onestarted=true done ${onestarted} } stop() { local vpn pidfile allstopped=true for pidfile in $(cd /var/run; ls openvpn-*.pid 2>/dev/null); do vpn="${pidfile##openvpn-}" vpn="${vpn%%.pid}" ebegin "Stopping openvpn for ${vpn}" start-stop-daemon --stop -exec /usr/sbin/openvpn \ --pidfile "/var/run/${pidfile}" eend $? || allstopped=false done ${allstopped} } # vim: ts=4