xref: /freebsd/tests/sys/net/if_ovpn/utils.subr (revision 067acae2f3a459537001b21c28ce7311da85a1e3)
1*067acae2SKristof Provost##
2*067acae2SKristof Provost# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*067acae2SKristof Provost#
4*067acae2SKristof Provost# Copyright (c) 2022 Rubicon Communications, LLC ("Netgate")
5*067acae2SKristof Provost#
6*067acae2SKristof Provost# Redistribution and use in source and binary forms, with or without
7*067acae2SKristof Provost# modification, are permitted provided that the following conditions
8*067acae2SKristof Provost# are met:
9*067acae2SKristof Provost# 1. Redistributions of source code must retain the above copyright
10*067acae2SKristof Provost#    notice, this list of conditions and the following disclaimer.
11*067acae2SKristof Provost# 2. Redistributions in binary form must reproduce the above copyright
12*067acae2SKristof Provost#    notice, this list of conditions and the following disclaimer in the
13*067acae2SKristof Provost#    documentation and/or other materials provided with the distribution.
14*067acae2SKristof Provost#
15*067acae2SKristof Provost# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*067acae2SKristof Provost# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*067acae2SKristof Provost# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*067acae2SKristof Provost# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*067acae2SKristof Provost# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*067acae2SKristof Provost# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*067acae2SKristof Provost# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*067acae2SKristof Provost# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*067acae2SKristof Provost# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*067acae2SKristof Provost# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*067acae2SKristof Provost# SUCH DAMAGE.
26*067acae2SKristof Provost
27*067acae2SKristof Provost. $(atf_get_srcdir)/../../common/vnet.subr
28*067acae2SKristof Provost
29*067acae2SKristof Provostovpn_init()
30*067acae2SKristof Provost{
31*067acae2SKristof Provost	vnet_init
32*067acae2SKristof Provost
33*067acae2SKristof Provost	if ! kldstat -q -m if_ovpn; then
34*067acae2SKristof Provost		atf_skip "This test requires if_ovpn"
35*067acae2SKristof Provost	fi
36*067acae2SKristof Provost
37*067acae2SKristof Provost	has_dco=$(openvpn --version 2>&1 | grep '\[DCO\]')
38*067acae2SKristof Provost	if [ -z "$has_dco" ]; then
39*067acae2SKristof Provost		atf_skip "openvpn binary does not support DCO"
40*067acae2SKristof Provost	fi
41*067acae2SKristof Provost}
42*067acae2SKristof Provost
43*067acae2SKristof Provostovpn_cleanup()
44*067acae2SKristof Provost{
45*067acae2SKristof Provost	for jail in `cat ovpn_jails.lst | sort -u`
46*067acae2SKristof Provost	do
47*067acae2SKristof Provost		cat ovpn_${jail}.log| sed s/^/\[${jail}\]\ /
48*067acae2SKristof Provost	done
49*067acae2SKristof Provost
50*067acae2SKristof Provost	vnet_cleanup
51*067acae2SKristof Provost}
52*067acae2SKristof Provost
53*067acae2SKristof Provostovpn_start()
54*067acae2SKristof Provost{
55*067acae2SKristof Provost	jail=$1
56*067acae2SKristof Provost	cfg=$2
57*067acae2SKristof Provost
58*067acae2SKristof Provost	echo ${jail} >> ovpn_jails.lst
59*067acae2SKristof Provost
60*067acae2SKristof Provost	dir=$(pwd)
61*067acae2SKristof Provost
62*067acae2SKristof Provost	echo "Start" >> ovpn_${jail}.log
63*067acae2SKristof Provost	echo "=====" >> ovpn_${jail}.log
64*067acae2SKristof Provost
65*067acae2SKristof Provost	echo "$cfg" > ovpn_${jail}.ovpn
66*067acae2SKristof Provost
67*067acae2SKristof Provost	echo "Jail $jail:"
68*067acae2SKristof Provost	echo "==========="
69*067acae2SKristof Provost	cat ovpn_${jail}.ovpn
70*067acae2SKristof Provost
71*067acae2SKristof Provost	jexec $jail sh -c "cd ${dir} &&
72*067acae2SKristof Provost	    openvpn --config ovpn_${jail}.ovpn >> ovpn_${jail}.log &"
73*067acae2SKristof Provost}
74