summaryrefslogtreecommitdiff
path: root/playbook.bash
blob: 057fb711840bd100103d6d5f20bbb392629b78c1 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env bash

# shellcheck disable=SC2034

BASHTARD_PLAYBOOK_VARS[$BASHTARD_PLAYBOOK.ipv4]="required"
BASHTARD_PLAYBOOK_VARS[$BASHTARD_PLAYBOOK.name]="required"

playbook_add()
{
	local data
	local etc
	local host
	local ipv4
	local name
	local tinc
	local tincd

	data="$(playbook_path "data")"
	etc="$(config "fs.etcdir")/tinc/tyilnet"
	host="$(tr "." "_" <<< "${BASHTARD_PLATFORM[fqdn]}")"
	ipv4="$(config "$BASHTARD_PLAYBOOK.ipv4")"
	name="$(config "$BASHTARD_PLAYBOOK.name")"
	tinc="$(config "app.tinc")"
	tincd="$(config "app.tincd")"

	case "${BASHTARD_PLATFORM[key]}" in
		freebsd) iptool=ifconfig ;;
		*)       iptool=ip
	esac

	info "$BASHTARD_PLAYBOOK/add" "Installing tinc"
	pkg install "tinc"

	info "$BASHTARD_PLAYBOOK/add" "Creating tinc configuration at $etc"
	mkdir -pv -- \
		"$etc" \
		"$etc/hosts"

	file_template tinc.conf \
		"name=$host" \
		> "$etc/tinc.conf"

	file_template "tinc-up-$iptool" \
		"ip4=$ipv4" \
		> "$etc/tinc-up"

	file_template "tinc-down-$iptool" \
		"ip4=$ipv4" \
		> "$etc/tinc-down"

	file_template "host" \
		"ip4=$ipv4" \
		> "$etc/hosts/$host"

	chmod +x \
		"$etc/tinc-up" \
		"$etc/tinc-down"

	info "$BASHTARD_PLAYBOOK/add" "Generating private keys"

	case "$($tincd --version | awk '{ print $3 }' | head -n1)" in
		1.0*)
			$tincd -n "$name" -K4096
			;;
		1.1*|*)
			$tinc -n "$name" generate-rsa-keys 4096
			$tinc -n "$name" generate-ed25519-keys
			;;
	esac

	info "$BASHTARD_PLAYBOOK/add" "Adding new host to Bashtard configs"

	mkdir -pv -- "$data/hosts"
	cp -v -- \
		"$etc/hosts/$host" \
		"$data/hosts/$host"

	playbook_sync

	info "$BASHTARD_PLAYBOOK" "Enabling VPN service"

	case "${BASHTARD_PLATFORM[key]}" in
		freebsd)
			if ! grep -Fq 'tincd_cfg="'"$name"'"' "/etc/rc.conf.d/tincd"
			then
				printf 'tincd_cfg="%s"\n' "$name" >> "/etc/rc.conf.d/tincd"
			fi
			;;
		linux-gentoo|linux-alpine_linux)
			if ! grep -Fq "NETWORK: $name" /etc/conf.d/tinc.networks
			then
				printf "NETWORK: %s\n" "$name" >> /etc/conf.d/tinc.networks
			fi
			;;
	esac

	case "${BASHTARD_PLATFORM[init]}" in
		systemd)
			systemctl enable --now "tinc@$name.service"
			;;
		*)
			svc enable "tinc"
			svc start "tinc"
			;;
	esac
}

playbook_sync()
{
	local data
	local etc
	local hash
	local host
	local name

	data="$(playbook_path "data")"
	etc="$(config "fs.etcdir")/tinc/$(config "$BASHTARD_PLAYBOOK.name")"
	hash="$(dir_hash "$etc/hosts")"
	host="$(tr "." "_" <<< "${BASHTARD_PLATFORM[fqdn]}")"
	name="$(config "$BASHTARD_PLAYBOOK.name")"

	info "$BASHTARD_PLAYBOOK" "Regenerating tinc hosts"
	rm -fr -- "$etc/hosts"
	mkdir -p -- "$etc/hosts"

	for path in "$data/hosts"/*
	do
		file="$(basename "$path")"

		notice "$BASHTARD_PLAYBOOK" "Updating host $file"
		cp -v -- "$data/hosts/$file" "$etc/hosts/$file"
	done

	[[ "$BASHTARD_COMMAND" == "add" ]] && return
	[[ "$hash" == "$(dir_hash "$etc/hosts")" ]] && return

	info "$BASHTARD_PLAYBOOK" "Reloading service"

	case "${BASHTARD_PLATFORM[init]}" in
		systemd)
			systemctl reload "tinc@$name.service"
			;;
		*)
			svc reload "tinc"
			;;
	esac
}

playbook_del()
{
	local etc
	local name

	etc="$(config "fs.etcdir")"
	name="$(config "$BASHTARD_PLAYBOOK.name")"

	case "${BASHTARD_PLATFORM[init]}" in
		systemd)
			systemctl disable --now "tinc@$name.service"
			;;
		*)
			svc stop "tinc"
			svc disable "tinc"
			;;
	esac

	pkg uninstall "tinc"

	rm -frv -- "$etc/tinc/$name"
}