summaryrefslogtreecommitdiff
path: root/security/tailscale/files/tailscaled.in
blob: f2b418733d4f2272203d56aaa9e70a223b03144d (plain) (blame)
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
#!/bin/sh

# $FreeBSD$
#
# PROVIDE: tailscaled
# REQUIRE: NETWORKING
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# tailscaled_enable (bool):	Set it to YES to enable tailscaled.
#				Default is "NO".
# tailscaled_port (number):	Set the port to listen on for incoming VPN packets.
#				Default is "41641".
# tailscaled_syslog_output_enable (bool):	Set to enable syslog output.
#						Default is "NO". See daemon(8).
# tailscaled_syslog_output_priority (str):	Set syslog priority if syslog enabled.
#						Default is "info". See daemon(8).
# tailscaled_syslog_output_facility (str):	Set syslog facility if syslog enabled.
#						Default is "daemon". See daemon(8).

. /etc/rc.subr

name=tailscaled
rcvar=tailscaled_enable

load_rc_config $name

: ${tailscaled_enable:="NO"}
: ${tailscaled_port:="41641"}

DAEMON=$(/usr/sbin/daemon 2>&1 | grep -q syslog ; echo $?)
if [ ${DAEMON} -eq 0 ]; then
        : ${tailscaled_syslog_output_enable:="NO"}
        : ${tailscaled_syslog_output_priority:="info"}
        : ${tailscaled_syslog_output_facility:="daemon"}
        if checkyesno tailscaled_syslog_output_enable; then
                tailscaled_syslog_output_flags="-t ${name} -T ${name}"

                if [ -n "${tailscaled_syslog_output_priority}" ]; then
                        tailscaled_syslog_output_flags="${tailscaled_syslog_output_flags} -s ${tailscaled_syslog_output_priority}"
                fi

                if [ -n "${tailscaled_syslog_output_facility}" ]; then
                        tailscaled_syslog_output_flags="${tailscaled_syslog_output_flags} -l ${tailscaled_syslog_output_facility}"
                fi
        fi
else
        tailscaled_syslog_output_enable="NO"
        tailscaled_syslog_output_flags=""
fi

pidfile=/var/run/${name}.pid
procname="%%PREFIX%%/bin/${name}"

# XXX: Can we have multiple interfaces?
tailscale_tap_dev="tailscale0"

start_cmd="${name}_start"
stop_postcmd="${name}_poststop"

tailscaled_start()
{
	env CACHE_DIRECTORY=/var/db/tailscale /usr/sbin/daemon -f ${tailscaled_syslog_output_flags} -p ${pidfile} ${procname} --port ${tailscaled_port}
}

tailscaled_poststop()
{
	/sbin/ifconfig ${tailscale_tap_dev} >/dev/null 2>&1 && (
		logger -s -t tailscaled "Destroying tailscale0 adapter"
		/sbin/ifconfig ${tailscale_tap_dev} destroy || logger -s -t tailscaled "Failed to destroy ${tailscale_tap_dev} adapter"
	)
}

run_rc_command "$1"