1#!/bin/sh
2
3# PROVIDE: tailscaled
4# REQUIRE: NETWORKING
5# KEYWORD: shutdown
6#
7# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
8# to enable this service:
9#
10# tailscaled_enable (bool):	Set it to YES to enable tailscaled.
11#				Default is "NO".
12# tailscaled_state_dir (str):	Set the path to use for the state directory.
13# 				Default is "/var/db/tailscale"
14# tailscaled_port (number):	Set the port to listen on for incoming VPN packets.
15#				Default is "41641".
16# tailscaled_syslog_output_enable (bool):	Set to enable syslog output.
17#						Default is "NO". See daemon(8).
18# tailscaled_syslog_output_priority (str):	Set syslog priority if syslog enabled.
19#						Default is "info". See daemon(8).
20# tailscaled_syslog_output_facility (str):	Set syslog facility if syslog enabled.
21#						Default is "daemon". See daemon(8).
22# tailscaled_exitnode_enable (bool):	Set it to YES to announce tailscaled as
23#                                       an exit node. Default is "NO".
24# tailscaled_up_args (str):		Additional arguments to pass to tailscale up
25#                                       Default is "" (empty string).
26# tailscaled_tun_dev (str):	Set the name of the tun interface tailscaled creates.
27#				Default is "tailscale0"
28
29. /etc/rc.subr
30
31name=tailscaled
32rcvar=tailscaled_enable
33
34load_rc_config $name
35
36: ${tailscaled_enable:="NO"}
37: ${tailscaled_state_dir:="/var/db/tailscale"}
38: ${tailscaled_port:="41641"}
39: ${tailscaled_exitnode_enable:="NO"}
40: ${tailscaled_up_args:=""}
41: ${tailscaled_tun_dev:="tailscale0"}
42
43DAEMON=$(/usr/sbin/daemon 2>&1 | grep -q syslog ; echo $?)
44if [ ${DAEMON} -eq 0 ]; then
45        : ${tailscaled_syslog_output_enable:="NO"}
46        : ${tailscaled_syslog_output_priority:="info"}
47        : ${tailscaled_syslog_output_facility:="daemon"}
48        if checkyesno tailscaled_syslog_output_enable; then
49                tailscaled_syslog_output_flags="-t ${name} -T ${name}"
50
51                if [ -n "${tailscaled_syslog_output_priority}" ]; then
52                        tailscaled_syslog_output_flags="${tailscaled_syslog_output_flags} -s ${tailscaled_syslog_output_priority}"
53                fi
54
55                if [ -n "${tailscaled_syslog_output_facility}" ]; then
56                        tailscaled_syslog_output_flags="${tailscaled_syslog_output_flags} -l ${tailscaled_syslog_output_facility}"
57                fi
58        fi
59else
60        tailscaled_syslog_output_enable="NO"
61        tailscaled_syslog_output_flags=""
62fi
63
64pidfile=/var/run/${name}.pid
65procname="%%PREFIX%%/bin/${name}"
66ctlname="%%PREFIX%%/bin/tailscale"
67
68start_cmd="${name}_start"
69start_postcmd="${name}_poststart"
70stop_postcmd="${name}_poststop"
71
72tailscaled_start()
73{
74	# Check for orphaned tailscale network interface
75	# And if it exists, then destroy it
76	/sbin/ifconfig ${tailscaled_tun_dev} >/dev/null 2>&1 && (
77		/sbin/ifconfig ${tailscaled_tun_dev} | fgrep -qw PID ||
78		/sbin/ifconfig ${tailscaled_tun_dev} destroy
79	)
80
81	/usr/sbin/daemon -f ${tailscaled_syslog_output_flags} -p ${pidfile} ${procname} -port ${tailscaled_port} -tun ${tailscaled_tun_dev} -statedir ${tailscaled_state_dir}
82}
83
84tailscaled_poststart()
85{
86        if checkyesno tailscaled_exitnode_enable; then
87		logger -s -t tailscale "Enabling Exit node mode"
88		tailscaled_up_args=" --advertise-exit-node ${tailscaled_up_args}"
89        fi
90        if [ -n "${tailscaled_up_args}" ]; then
91                ${ctlname} up ${tailscaled_up_args}
92        fi
93}
94
95tailscaled_poststop()
96{
97	/sbin/ifconfig ${tailscaled_tun_dev} >/dev/null 2>&1 && (
98		logger -s -t tailscaled "Destroying ${tailscaled_tun_dev} adapter"
99		/sbin/ifconfig ${tailscaled_tun_dev} destroy || logger -s -t tailscaled "Failed to destroy ${tailscaled_tun_dev} adapter"
100	)
101}
102
103run_rc_command "$1"
104