summaryrefslogtreecommitdiff
path: root/gentoo-linux/net-misc/uanytun/files/uanytun-0.3.1.init
blob: 9f82a6d145473e4796e65b4cf8d0de63a6fa6041 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/sbin/runscript
# Distributed under the terms of the GNU General Public License v3
# Written by Bernhard Tittelbach based on examples from Gentoo, openvpn and uanytun debian initrd script

depend() {
	need net
	use dns
	after bootmisc
}

DAEMON=/usr/sbin/uanytun
CONFIG_DIR=/etc/uanytun
NAME=uanytun
DESC=uanytun
VARRUN_PREFIX=/var/run/uanytun

VPN=${SVCNAME#*.}
[ "$VPN" = "$SVCNAME" ] && VPN=""

test -x $DAEMON || exit 1

# Include uanytun defaults if available
if [ -f /etc/default/uanytun ] ; then
	. /etc/default/uanytun
fi

start_vpn () {
	if [ -f $CONFIG_DIR/$NAME/config ] ; then
		POSTUP=''
		test -f  $CONFIG_DIR/$NAME/post-up.sh && POSTUP="-x $CONFIG_DIR/$NAME/post-up.sh"
		CHROOTDIR=`grep '^chroot' < $CONFIG_DIR/$NAME/config | sed 's/chroot\s*//'`
		if [ -n "$CHROOTDIR" ] ; then
			test -d $CHROOTDIR || mkdir -p $CHROOTDIR
		fi
		DAEMONARG=`sed 's/#.*//' < $CONFIG_DIR/$NAME/config | grep -e '\w' | sed  's/^/--/' | tr '\n' ' '`
		start-stop-daemon --start --pidfile $VARRUN_PREFIX.${NAME}.pid --exec \
			$DAEMON -- --write-pid $VARRUN_PREFIX.${NAME}.pid $POSTUP $DAEMONARG || return 1 
	else
		eerror "no config found"
		return 1
	fi
	return 0
}
stop_vpn () {
	start-stop-daemon --stop --pidfile $PIDFILE --exec $DAEMON --oknodo
	rm -f $PIDFILE
}

start () {
	if test -z "$VPN" ; then
		if [ -f $CONFIG_DIR/autostart ] ; then
			for NAME in `sed 's/#.*//'  <  $CONFIG_DIR/autostart | grep -e '\w'`; do
				ebegin "Starting ${DESC} VPN: ${NAME}"
				start_vpn
				eend $?
			done
		else
			eerror "no config found"
			return 1
		fi
	else
		NAME="$VPN"
		ebegin "Starting ${DESC} VPN: ${NAME}"
		start_vpn
		eend $?
	fi
}

stop () {
	if test -z "$VPN" ; then
		for PIDFILE in `ls $VARRUN_PREFIX.*.pid 2> /dev/null`; do
			NAME=`echo $PIDFILE | cut -c18-`
			NAME=${NAME%%.pid}
			ebegin "Stopping ${DESC} VPN: ${NAME}"
			stop_vpn
			eend $?
		done
	else
		if test -e $VARRUN_PREFIX.${VPN}.pid ; then
			PIDFILE=`ls $VARRUN_PREFIX.${VPN}.pid 2> /dev/null`
			NAME=`echo $PIDFILE | cut -c18-`
			NAME=${NAME%%.pid}
			ebegin "Stopping ${DESC} VPN: ${NAME}"
			stop_vpn
			eend $?
		else
			eerror " failure: No such tunnel is running: $VPN"
		fi
	fi
}