#!/usr/bin/env bash playbook_add() { local tinc="$(config "app.tinc")" local tincd="$(config "app.tincd")" local dir="$(config "fs.etcdir")/tinc/tyilnet" local name="$(tr "." "_" <<< "${BASHTARD_PLATFORM[fqdn]}")" local ipv4="$(config "vpn.ipv4")" if [[ -z "$ipv4" ]] then emerg "$BASHTARD_PLAYBOOK" "No IPv4 address set for ${BASHTARD_PLATFORM[fqdn]}" return 2 fi case "${BASHTARD_PLATFORM[key]}" in freebsd) iptool=ifconfig ;; *) iptool=ip esac info "$BASHTARD_PLAYBOOK" "Installing tinc" pkg install "tinc" info "$BASHTARD_PLAYBOOK" "Creating tinc configuration at $dir" mkdir -pv -- \ "$dir" \ "$dir/hosts" file_template tinc.conf \ "name=$name" \ > "$dir/tinc.conf" file_template "tinc-up-$iptool" \ "ip4=$(config "vpn.ipv4")" \ > "$dir/tinc-up" file_template "tinc-down-$iptool" \ "ip4=$(config "vpn.ipv4")" \ > "$dir/tinc-down" file_template "host" \ "ip4=$(config "vpn.ipv4")" \ > "$dir/hosts/$name" chmod +x \ "$dir/tinc-up" \ "$dir/tinc-down" info "$BASHTARD_PLAYBOOK" "Generating private keys" case "$($tincd --version | awk '{ print $3 }' | head -n1)" in 1.0*) $tincd -n tyilnet -K4096 ;; 1.1*|*) $tinc -n tyilnet generate-rsa-keys 4096 $tinc -n tyilnet generate-ed25519-keys ;; esac info "$BASHTARD_PLAYBOOK" "Adding new host to Bashtard configs" cp -v -- \ "$dir/hosts/$name" \ "$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/share/hosts/$name" playbook_sync info "$BASHTARD_PLAYBOOK" "Enabling VPN service" case "${BASHTARD_PLATFORM[key]}" in freebsd) if ! grep -Fq 'tincd_cfg="tyilnet"' "/etc/rc.conf.d/tincd" then printf 'tincd_cfg="%s"\n' "tyilnet" >> "/etc/rc.conf.d/tincd" fi ;; linux-gentoo) if ! grep -Fq "NETWORK: tyilnet" /etc/conf.d/tinc.networks then printf "NETWORK: %s\n" "tyilnet" >> /etc/conf.d/tinc.networks fi ;; esac case "${BASHTARD_PLATFORM[init]}" in systemd) systemctl enable --now tinc@tyilnet.service ;; *) svc enable "tinc" svc start "tinc" ;; esac } playbook_sync() { local dir="$(config "fs.etcdir")/tinc/tyilnet" local name="$(tr "." "_" <<< "${BASHTARD_PLATFORM[fqdn]}")" local host info "$BASHTARD_PLAYBOOK" "Regenerating tinc hosts" rm -fr -- "$dir/hosts" mkdir -p -- "$dir/hosts" for path in "$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/share/hosts"/* do host="$(basename "$path")" notice "$BASHTARD_PLAYBOOK" "Updating host $host" file_template "hosts/$host" \ > "$dir/hosts/$host" done [[ "$BASHTARD_COMMAND" == "add" ]] && return case "${BASHTARD_PLATFORM[init]}" in systemd) systemctl reload tinc@tyilnet.service ;; *) svc reload "tinc" ;; esac } playbook_del() { case "${BASHTARD_PLATFORM[init]}" in systemd) systemctl disable --now tinc@tyilnet.service ;; *) svc stop "tinc" svc disable "tinc" ;; esac pkg uninstall "tinc" rm -frv -- "$(config "fs.etcdir")/tinc/tyilnet" }