xref: /freebsd/tests/sys/net/if_ovpn/utils.subr (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1067acae2SKristof Provost##
2*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
3067acae2SKristof Provost#
4067acae2SKristof Provost# Copyright (c) 2022 Rubicon Communications, LLC ("Netgate")
5067acae2SKristof Provost#
6067acae2SKristof Provost# Redistribution and use in source and binary forms, with or without
7067acae2SKristof Provost# modification, are permitted provided that the following conditions
8067acae2SKristof Provost# are met:
9067acae2SKristof Provost# 1. Redistributions of source code must retain the above copyright
10067acae2SKristof Provost#    notice, this list of conditions and the following disclaimer.
11067acae2SKristof Provost# 2. Redistributions in binary form must reproduce the above copyright
12067acae2SKristof Provost#    notice, this list of conditions and the following disclaimer in the
13067acae2SKristof Provost#    documentation and/or other materials provided with the distribution.
14067acae2SKristof Provost#
15067acae2SKristof Provost# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16067acae2SKristof Provost# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17067acae2SKristof Provost# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18067acae2SKristof Provost# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19067acae2SKristof Provost# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20067acae2SKristof Provost# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21067acae2SKristof Provost# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22067acae2SKristof Provost# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23067acae2SKristof Provost# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24067acae2SKristof Provost# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25067acae2SKristof Provost# SUCH DAMAGE.
26067acae2SKristof Provost
27067acae2SKristof Provost. $(atf_get_srcdir)/../../common/vnet.subr
28067acae2SKristof Provost
29067acae2SKristof Provostovpn_init()
30067acae2SKristof Provost{
31067acae2SKristof Provost	vnet_init
32067acae2SKristof Provost
33067acae2SKristof Provost	if ! kldstat -q -m if_ovpn; then
34067acae2SKristof Provost		atf_skip "This test requires if_ovpn"
35067acae2SKristof Provost	fi
36067acae2SKristof Provost
37067acae2SKristof Provost	has_dco=$(openvpn --version 2>&1 | grep '\[DCO\]')
38067acae2SKristof Provost	if [ -z "$has_dco" ]; then
39067acae2SKristof Provost		atf_skip "openvpn binary does not support DCO"
40067acae2SKristof Provost	fi
41067acae2SKristof Provost}
42067acae2SKristof Provost
43067acae2SKristof Provostovpn_cleanup()
44067acae2SKristof Provost{
45067acae2SKristof Provost	for jail in `cat ovpn_jails.lst | sort -u`
46067acae2SKristof Provost	do
47067acae2SKristof Provost		cat ovpn_${jail}.log| sed s/^/\[${jail}\]\ /
48067acae2SKristof Provost	done
49067acae2SKristof Provost
50067acae2SKristof Provost	vnet_cleanup
51067acae2SKristof Provost}
52067acae2SKristof Provost
53067acae2SKristof Provostovpn_start()
54067acae2SKristof Provost{
55067acae2SKristof Provost	jail=$1
56067acae2SKristof Provost	cfg=$2
57067acae2SKristof Provost
58067acae2SKristof Provost	echo ${jail} >> ovpn_jails.lst
59067acae2SKristof Provost
60067acae2SKristof Provost	dir=$(pwd)
61067acae2SKristof Provost
62067acae2SKristof Provost	echo "Start" >> ovpn_${jail}.log
63067acae2SKristof Provost	echo "=====" >> ovpn_${jail}.log
64067acae2SKristof Provost
65067acae2SKristof Provost	echo "$cfg" > ovpn_${jail}.ovpn
66067acae2SKristof Provost
67067acae2SKristof Provost	echo "Jail $jail:"
68067acae2SKristof Provost	echo "==========="
69067acae2SKristof Provost	cat ovpn_${jail}.ovpn
70067acae2SKristof Provost
71067acae2SKristof Provost	jexec $jail sh -c "cd ${dir} &&
72067acae2SKristof Provost	    openvpn --config ovpn_${jail}.ovpn >> ovpn_${jail}.log &"
73067acae2SKristof Provost}
74