#!/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" } config(){ if [ -r "/tmp/flash/openvpn/own_${DAEMON}.conf" ]; then cp /tmp/flash/openvpn/own_${DAEMON}.conf /mod/etc/${DAEMON}.conf else modlib_config ${DAEMON} /mod/etc/${DAEMON}.conf fi } start() { cp -f /etc/resolv.conf /tmp/openvpn/etc cp -f /etc/TZ /tmp/openvpn/etc mknod -m 444 /tmp/openvpn/dev/urandom c 1 9 2>/dev/null for file in $(echo ${OPENVPN_FILES2CP#*#} | tr ';#' '\n\n'); do if [ ! -r $file ]; then # not a full path? Try to find a binary fullpath=$(which $file); [ ! x = x$fullpath ] && file=$fullpath fi if [ -r $file ]; then mkdir -p /tmp/openvpn${file%/*} cp -a $file /tmp/openvpn${file%/*} else echo "Copying files to chroot. File $file not found. Skipped" fi done trap 'echo -e "You may get some more hints by starting ${DAEMON} w/o \"--daemon\":\n\t$DAEMON --config /mod/etc/${DAEMON}.conf"' EXIT modlib_startdaemon ${DAEMON} --config /mod/etc/${DAEMON}.conf --writepid /var/run/${DAEMON}.pid --daemon trap - EXIT # 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 /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_add_user_and_group openvpn mkdir -p /tmp/openvpn/etc mkdir -p /tmp/openvpn/dev mkdir -p /tmp/flash/openvpn 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 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" DAEMON_LONG_NAME=$DAEMON start fi as=${as#*#} done DAEMON="$d_BU" DAEMON_LONG_NAME=$DAEMON ;; unload) stop if [ "$DAEMON" = "openvpn" ]; then for d in $(echo "${OPENVPN_CONFIG_NAMES#*DEFAULT#}" | sed 's/#/ openvpn_/g' ); do DAEMON="$d" DAEMON_LONG_NAME=$DAEMON stop done modunreg file openvpn modunreg daemon openvpn modunreg cgi openvpn modunreg extra openvpn fi ;; start) modlib_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