aboutsummaryrefslogtreecommitdiffstats
path: root/community/anytun/anytun.initd
blob: 0767a73802029900775cdb65866c10a43cd79dd7 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/sbin/openrc-run
# Distributed under the terms of the GNU General Public License v3
# Written by Bernhard Tittelbach based on examples from Gentoo, openvpn and anytun debian init.rd script

opts="${opts} reload"
depend() {
	need net
	use dns
	after bootmisc
}

DAEMON=/usr/sbin/anytun
ANYTUNCONFIG=/usr/bin/anytun-config
CONTROLDAEMON=/usr/bin/anytun-controld
NAME=anytun
DESC=anytun
CONFIG_DIR=/etc/anytun
VARCONFIG_DIR=/var/run/anytun-controld
VARRUN_DIR=/var/run/anytun

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

# Include anytun defaults if available
if [ -f /etc/conf.d/anytun ] ; then
	. /etc/conf.d/anytun
fi

test -x $DAEMON || exit 1


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
		test -d $VARRUN_DIR || mkdir -p $VARRUN_DIR
		DAEMONARG=`sed 's/#.*//' < $CONFIG_DIR/$NAME/config | grep -e '\w' | sed  's/^/--/' | tr '\n' ' '`
		start-stop-daemon --start --pidfile $VARRUN_DIR/${NAME}.pid --exec $DAEMON -- --write-pid $VARRUN_DIR/${NAME}.pid $POSTUP \
			$DAEMONOPTS $DAEMONARG || return 1
	else
		eerror "no config found"
		return 1
	fi
	start_configd
	return 0
}
stop_vpn () {
	start-stop-daemon --stop --pidfile $PIDFILE --exec $DAEMON
	rm -f $PIDFILE
	stop_configd
}

start_configd () {
	if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then
		test -d $VARCONFIG_DIR || mkdir -p $VARCONFIG_DIR
		chmod 700 $VARCONFIG_DIR
		rm -f $VARCONFIG_DIR/$NAME 2>/dev/null
		KDPRF=`sed 's/#.*//'  <  $CONFIG_DIR/$NAME/config | grep -e 'kd-prf' | sed  's/^/ --/' | xargs echo`
		for CLIENTPATH in $CONFIG_DIR/$NAME/conf.d/* ; do
			DAEMONARG=`sed 's/#.*//'  <  $CLIENTPATH | grep -e '\w' | sed  's/^/ --/' | xargs echo`
			$ANYTUNCONFIG $DAEMONARG $CIPHER $AUTHALGO $KDPRF >> $VARCONFIG_DIR/$NAME
		done
		CONTROLHOST=`sed 's/#.*//'  <  $CONFIG_DIR/$NAME/config | grep -e 'control-host' | sed  's/^/ --/'i | xargs echo`
		start-stop-daemon --start --pidfile $VARCONFIG_DIR/${NAME}.pid --exec $CONTROLDAEMON \
			-- -f $VARCONFIG_DIR/$NAME $DAEMONOPTS $CONTROLHOST --write-pid $VARCONFIG_DIR/${NAME}.pid
	fi
}
stop_configd () {
	if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then
		start-stop-daemon --stop --pidfile $VARCONFIG_DIR/${NAME}.pid --exec $CONTROLDAEMON
		rm -f $VARCONFIG_DIR/${NAME}.pid
	fi 
}

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 ${VARRUN_DIR}/*.pid ; do
			NAME=`basename $PIDFILE .pid`
			ebegin "Stopping ${DESC} VPN: ${NAME}"
			stop_vpn
			eend $?
		done
	else
		if test -e ${VARRUN_DIR}/${VPN}.pid ; then
			PIDFILE=${VARRUN_DIR}/${VPN}.pid
			NAME=`basename $PIDFILE .pid`
			ebegin "Stopping ${DESC} VPN: ${NAME}"
			stop_vpn
			eend $?
		else
			eerror " failure: No such tunnel is running: $VPN"
		fi
	fi
}

reload () {
	if test -z "$VPN" ; then
		for PIDFILE in ${VARRUN_DIR}/*.pid ; do
			NAME=`basename $PIDFILE .pid`
			if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then
				ebegin "Reloading ${DESC} VPN: ${NAME}"
				stop_vpn
				start_vpn
				eend $?
			else
				ebegin "Reloading ${DESC} VPN: ${NAME}"
				stop_configd
				start_configd
				eend $?
			fi
		done
	else
		if test -e ${VARRUN_DIR}/${VPN}.pid ; then
			PIDFILE=${VARRUN_DIR}/${VPN}.pid
			NAME=`basename $PIDFILE .pid`
			if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then
				ebegin "Reloading ${DESC} VPN: ${NAME}"
				stop_vpn
				start_vpn
				eend $?
			else
				ebegin "Reloading ${DESC} VPN: ${NAME}"
				stop_configd
				start_configd
				eend $?
			fi
		else
			eerror "failure: No such tunnel is running: $VPN"
		fi
	fi
}