#!/bin/sh export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/mod/sbin:/mod/bin:/mod/usr/sbin:/mod/usr/bin export LD_LIBRARY_PATH=/mod/lib:/mod/usr/lib scriptname=$0; DAEMON=${scriptname##*/rc.} if [ ! -e /var/tmp/.openvpnfirstrun ]; then touch /var/tmp/.openvpnfirstrun /mod/etc/default.openvpn/openvpn_dynamic_conf fi if [ ! -x /mod/sbin/$DAEMON ]; then ln -s /usr/sbin/openvpn /mod/sbin/$DAEMON fi case "$1" in ""|load|start|restart) if [ ! -r "/mod/etc/conf/${DAEMON}.cfg" ]; then echo "Error[$DAEMON]: not configured" 1>&2 exit 1 fi . /mod/etc/conf/${DAEMON}.cfg ;; esac start() { if [ ! -e "/tmp/flash/static.key" ]; then echo "Creating static.key" openvpn --genkey --secret /tmp/flash/static.key /usr/bin/modsave flash fi if [ -x "/tmp/flash/${DAEMON}_conf" ]; then /tmp/flash/${DAEMON}_conf /mod/etc/${DAEMON}.conf else /mod/etc/default.${DAEMON}/${DAEMON}_conf /mod/etc/${DAEMON}.conf fi if [ -r "/tmp/flash/${DAEMON}.extra" ]; then cat /tmp/flash/${DAEMON}.extra >> /mod/etc/${DAEMON}.conf fi echo -n "Starting $DAEMON ... " $DAEMON --config /mod/etc/${DAEMON}.conf --writepid /var/run/${DAEMON}.pid exitval=$? if [ "$exitval" -eq 0 ]; then echo 'done.' else echo 'failed.' exit $exitval fi # if we have brctl, then try to add tap to "lan" if requestet (ignore errors) if [ $(which brctl) ] && TAP=`grep "#Helperline" /mod/etc/${DAEMON}.conf | grep -o tap[0-9] `; then brctl addif lan $TAP 2> /dev/null fi } stop() { if [ ! -z "$(pidof $DAEMON)" ];then echo -n "Stopping ${DAEMON} ... " killall $DAEMON > /dev/null 2>&1 exitval=$? if [ "$exitval" -eq 0 ]; then echo 'done.' else echo 'failed.' exit $exitval fi fi rm -f /mod/etc/${DAEMON}.conf rm -f /var/run/${DAEMON}.pid } case "$1" in ""|load) if [ "$DAEMON" = "openvpn" ]; then modreg cgi 'openvpn' 'OpenVPN' deffile='/mod/etc/default.openvpn/box_crt.def' [ -r "/tmp/flash/box_crt.def" ] && deffile='/tmp/flash/box_crt.def' modreg file 'box_crt' 'OpenVPN: Box Cert' 0 "$deffile" deffile='/mod/etc/default.openvpn/box_key.def' [ -r "/tmp/flash/box_key.def" ] && deffile='/tmp/flash/box_key.def' modreg file 'box_key' 'OpenVPN: Private Key' 0 "$deffile" deffile='/mod/etc/default.openvpn/ca_crt.def' [ -r "/tmp/flash/ca_crt.def" ] && deffile='/tmp/flash/ca_crt.def' modreg file 'ca_crt' 'OpenVPN: CA Cert' 0 "$deffile" deffile='/mod/etc/default.openvpn/crl_pem.def' [ -r "/tmp/flash/crl_pem.def" ] && deffile='/tmp/flash/crl_pem.def' modreg file 'crl_pem' 'OpenVPN: CRL' 0 "$deffile" deffile='/mod/etc/default.openvpn/dh_pem.def' [ -r "/tmp/flash/dh_pem.def" ] && deffile='/tmp/flash/dh_pem.def' modreg file 'dh_pem' 'OpenVPN: DH Param' 0 "$deffile" deffile='/mod/etc/default.openvpn/static_key.def' [ -r "/tmp/flash/static_key.def" ] && deffile='/tmp/flash/static_key.def' modreg file 'static_key' 'OpenVPN: Static Key' 0 "$deffile" fi d_BU="$DAEMON" as="${OPENVPN_AUTOSTART#*#}" ; for d in `echo "openvpn${OPENVPN_CONFIG_NAMES#*DEFAULT#}" | sed 's/#/ openvpn_/g' `; do if [ "${as%%#*}" != "yes" ]; then echo "$d is disabled" 1>&2 else if [ -e "/var/run/$d.pid" ]; then echo "$d already started." else DAEMON="$d" start fi fi as=${as#*#}; done DAEMON="$d_BU" ;; unload) stop if [ "$DAEMON" = "openvpn" ]; then modunreg file 'box_crt' modunreg file 'box_key' modunreg file 'ca_crt' modunreg file 'crl_pem' modunreg file 'dh_pem' modunreg file 'static_key' modunreg cgi 'openvpn' fi ;; start) start ;; stop) stop ;; restart) stop sleep 1 start ;; reload) if [ -r "/var/run/openvpn.pid" ]; then kill -HUP `cat /var/run/openvpn.pid` fi ;; status) if [ -z "$(pidof "$DAEMON")" ]; then echo 'stopped' else echo 'running' fi ;; *) echo "Usage: $0 [load|unload|start|stop|restart|reload|status]" 1>&2 exit 1 ;; esac exit 0