annotate files/fwireguard.in @ 458:09c782570d89

Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 11 Jun 2024 03:15:54 +0200
parents
children 345f1270e41e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
458
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1 #!/bin/sh
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
2
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
3 # PROVIDE: fwireguard
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
4 # REQUIRE: NETWORKING
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
5 # KEYWORD: shutdown
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
6
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
7 # @(#)%%SIMPLEVERSIONTAG%%
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
8
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
9 #
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
10 # fwireguard_enable (bool): Set to "YES" to enable wireguard (default: "NO")
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
11 # fwireguard_wait (str): wait (sleep) this time before calling post-start
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
12 # when configuring an interface (default: 2s)
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
13 #
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
14
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
15 . /etc/rc.subr
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
16
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
17 name=fwireguard
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
18 desc="Wireguard startup helper"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
19 rcvar=fwireguard_enable
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
20 extra_commands="reload status"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
21
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
22 start_cmd="${name}_start"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
23 stop_cmd="${name}_stop"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
24 reload_cmd="${name}_reload"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
25 status_cmd="${name}_status"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
26
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
27
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
28 load_rc_config $name
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
29
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
30
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
31 : ${fwireguard_enable:="NO"}
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
32 : ${fwireguard_wait="2s"}
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
33
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
34
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
35 fwireguard_start()
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
36 {
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
37 local _d _f _if
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
38 _d="%%PREFIX%%/etc/fwireguard"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
39 if [ ! -d "${_d}" ]; then mkdir "${_d}"; fi
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
40 for _if in `/sbin/ifconfig -g wg`; do
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
41
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
42 _f="${_d}/${_if}.key"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
43 if [ ! -f "${_f}" ]; then
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
44 echo "Generating secret key for ${_if} in ${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
45 (umask 0077; /usr/bin/wg genkey > "${_f}")
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
46 fi
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
47
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
48 _f="${_d}/${_if}.pub"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
49 if [ ! -f "${_f}" ]; then
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
50 echo "Generating public key for ${_if} in ${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
51 /usr/bin/wg pubkey < ${_d}/${_if}.key > "${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
52 fi
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
53
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
54 _f="${_d}/${_if}.conf"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
55 if [ ! -f "${_f}" ]; then
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
56 echo "Generating minimal config for ${_if} in ${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
57 umask 0077
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
58 echo "[Interface]" > "${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
59 /usr/bin/printf 'PrivateKey\t\t= ' >> "${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
60 /bin/cat "${_d}/${_if}.key" >> "${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
61 echo -e "#ListenPort\t\t= 51820" >> "${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
62 echo -e "#FwMark\t\t\t= 0x12345678\n" >> "${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
63 echo "#[Peer]" >> "${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
64 echo -e "#PublicKey\t\t= BlAbLABlA/EtCeTcEtc=" >> "${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
65 echo -e "#AllowedIPs\t\t= 10.X.X.1/32, 10.X.X.2/32" >> "${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
66 echo -e "#PresharedKey\t\t= BlAbLABlA/EtCeTcEtc=" >> "${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
67 echo -e "#Endpoint\t\t= [2001:db8::1]:51820" >> "${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
68 echo -e "#PersistentKeepalive\t= 30" >> "${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
69 fi
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
70
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
71 /sbin/ifconfig "${_if}" destroy
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
72 /sbin/ifconfig "${_if}" create # will take ifconfig_wgX="inet values" from /etc/rc.conf
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
73 /usr/bin/wg setconf "${_if}" "${_f}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
74 if [ -x "${_d}/${_if}.post-start" ]; then
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
75 if [ -n "${fwireguard_wait}" ]; then
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
76 /bin/sleep "${fwireguard_wait}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
77 fi
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
78 "${_d}/${_if}.post-start"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
79 fi
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
80 # /usr/bin/wg syncconf ${_if} ${_f}
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
81 done
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
82 }
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
83
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
84
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
85 fwireguard_stop()
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
86 {
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
87 local _d _if
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
88 _d="%%PREFIX%%/etc/fwireguard"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
89 for _if in `/sbin/ifconfig -g wg`; do
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
90 if [ -x "${_d}/${_if}.pre-stop" ]; then
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
91 "${_d}/${_if}.pre-stop"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
92 fi
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
93 /sbin/ifconfig "${_if}" down
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
94 done
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
95 }
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
96
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
97
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
98 fwireguard_reload()
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
99 {
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
100 fwireguard_start
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
101 }
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
102
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
103
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
104 fwireguard_status()
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
105 {
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
106 local _if
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
107 for _if in `/sbin/ifconfig -g wg`; do
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
108 /usr/bin/wg show "${_if}"
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
109 done
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
110 }
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
111
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
112
09c782570d89 Add a rc.d script "fwireguard" to help with automatic setup of Wireguard interfaces
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
113 run_rc_command "$1"