#!/bin/sh scriptname=$0 DAEMON=${scriptname##*/rc.} . /etc/init.d/modlibrc mypidof() { ps -w | sed -n "/\/m[o]d\/etc\/${1}.conf/ s/^[ ]*\([0-9]*\)[ ].*/\1/ p" } start() { echo -n "Starting $DAEMON ... " if [ -n "$(mypidof $DAEMON)" ]; then echo "already started." return 1; fi if [ ! -e "/tmp/flash/openvpn/static.key" ]; then echo -n "creating static.key ... " openvpn --genkey --secret /tmp/flash/openvpn/static.key /usr/bin/modsave flash >/dev/null fi if [ -r "/tmp/flash/openvpn/own_${DAEMON}.conf" ]; then cp /tmp/flash/openvpn/own_${DAEMON}.conf /mod/etc/${DAEMON}.conf else 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 fi cp -f /etc/resolv.conf /tmp/openvpn/etc cp -f /etc/TZ /tmp/openvpn/etc $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() { echo -n "Stopping ${DAEMON} ... " PID=$(mypidof $DAEMON) if [ -z "$PID" ]; then echo "not running." return 1 fi kill $PID > /dev/null 2>&1 exitval=$? if [ "$exitval" -eq 0 ]; then echo 'done.' else echo 'failed.' exit $exitval fi rm -f /mod/etc/${DAEMON}.conf rm -f /var/run/${DAEMON}.pid } if [ ! -e /var/tmp/.openvpnfirstrun ]; then touch /var/tmp/.openvpnfirstrun /mod/etc/default.openvpn/openvpn_dynamic_conf fi [ -x /mod/sbin/$DAEMON ] || ln -s /usr/sbin/openvpn /mod/sbin/$DAEMON case $1 in ""|load) if [ "$DAEMON" = "openvpn" ]; then modreg cgi 'openvpn' 'OpenVPN' modreg daemon openvpn modreg extra openvpn '$(lang de:"Verbundene Clients" en:"Connected clients")' 2 "openvpn_clients" modreg file openvpn 'box_crt' 'Box Cert' 0 "box_crt" modreg file openvpn 'box_key' 'Private Key' 0 "box_key" modreg file openvpn 'ca_crt' 'CA Cert' 0 "ca_crt" modreg file openvpn 'crl_pem' 'CRL' 0 "crl_pem" modreg file openvpn 'dh_pem' 'DH Param' 0 "dh_pem" modreg file openvpn 'static_key' 'Static Key' 0 "static_key" modlib_addgroup openvpn modlib_adduser openvpn -G openvpn -s /bin/false -H -D -g "OpenVPN account" mkdir -p /tmp/openvpn/etc mkdir -p /tmp/flash/openvpn 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 DAEMON="$d" start fi as=${as#*#} done DAEMON="$d_BU" ;; unload) stop if [ "$DAEMON" = "openvpn" ]; then modunreg file openvpn modunreg daemon openvpn modunreg cgi openvpn modunreg extra openvpn fi ;; start) start ;; stop) stop ;; restart) stop sleep 1 start ;; reload) if [ -r "/var/run/${DAEMON}.pid" ]; then kill -HUP $(cat /var/run/${DAEMON}.pid) fi ;; status) if [ -z "$(mypidof $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