1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2011 OpenWrt.org
3 # Copyright (C) 2011 Linus Lüssing
4 # Based on Jo-Philipp Wich's OpenVPN init script
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
13 EXTRA_COMMANDS="up down"
22 *_*_*_*) v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
23 *_*_*) v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
24 *_*) v=${v%%_*}-${v#*_} ;;
31 local p; local v; local s="$1"; local f="$2"; shift; shift
33 config_get_bool v "$s" "$p"
34 [ "$v" == 1 ] && echo "$p = yes" >> "$f"
35 [ "$v" == 0 ] && echo "$p = no" >> "$f"
40 local p; local v; local s="$1"; shift
42 config_get v "$s" "$p"
45 [ -n "$v" ] && append_param "$p" && ARGS="$ARGS=$v"
51 append_conf_params() {
52 local p; local v; local s="$1"; local f="$2"; shift; shift
54 config_get v "$s" "$p"
57 # Look up OpenWRT interface names
58 [ "$p" = "BindToInterface" ] && {
59 local ifname=$(uci -P /var/state get network.$v.ifname 2>&-)
60 [ -n "$ifname" ] && v="$ifname"
63 [ -n "$v" ] && echo "$p = $v" >> "$f"
70 config_get_bool enabled "$1" 'enabled' 0
80 section_enabled "$n" || return 1
82 if [ "$#" = "2" ]; then
83 [ "$2" != "$n" ] && return 1
87 section_enabled "$s" || {
88 [ -f "$TMP_TINC/$n/hosts/$s" ] && rm "$TMP_TINC/$n/hosts/$s"
92 [ ! -f "/etc/tinc/$n/hosts/$s" ] && {
93 echo -n "tinc: Warning, public key for $s for network $n "
94 echo -n "missing in /etc/tinc/$n/hosts/$s, "
95 echo "skipping configuration of $s"
100 append_conf_bools "$s" "$TMP_TINC/$n/hosts/$s" \
101 ClampMSS IndirectData PMTUDiscovery TCPOnly
104 append_conf_params "$s" "$TMP_TINC/$n/hosts/$s" \
105 Address Cipher Compression Digest MACLength PMTU \
106 Port PublicKey PublicKeyFile Subnet
109 check_gen_own_key() {
110 local s="$1"; local n; local k
112 config_get n "$s" Name
113 config_get_bool k "$s" generate_keys 0
114 [ "$k" == 0 ] && return 0
116 ([ -z "$n" ] || [ -f "$TMP_TINC/$s/hosts/$n" ] || [ -f "$TMP_TINC/$s/rsa_key.priv" ]) && \
118 [ ! -d "$TMP_TINC/$s/hosts" ] && mkdir -p "$TMP_TINC/$s/hosts"
120 config_get k "$s" key_size
122 $BIN -c "$TMP_TINC/$s" --generate-keys </dev/null
124 $BIN -c "$TMP_TINC/$s" "--generate-keys=$k" </dev/null
127 [ ! -d "/etc/tinc/$s/hosts" ] && mkdir -p "/etc/tinc/$s/hosts"
128 cp "$TMP_TINC/$s/rsa_key.priv" "/etc/tinc/$s/"
129 [ -n "$n" ] && cp "$TMP_TINC/$s/hosts/$n" "/etc/tinc/$s/hosts/"
136 section_enabled "$s" || return 1
138 [ -d "$TMP_TINC/$s" ] && rm -rf "$TMP_TINC/$s/"
139 mkdir -p "$TMP_TINC/$s"
140 [ -d "/etc/tinc/$s" ] && cp -r "/etc/tinc/$s" "$TMP_TINC/"
143 append_conf_bools "$s" "$TMP_TINC/$s/tinc.conf" \
144 DecrementTTL DirectOnly Hostnames IffOneQueue \
145 LocalDiscovery PriorityInheritance StrictSubnets TunnelServer \
146 ClampMSS IndirectData PMTUDiscovery TCPOnly
149 append_conf_params "$s" "$TMP_TINC/$s/tinc.conf" \
150 AddressFamily BindToAddress ConnectTo BindToInterface \
151 Broadcast Device DeviceType Forwarding \
152 GraphDumpFile Interface KeyExpire MACExpire \
153 MaxTimeout Mode Name PingInterval PingTimeout \
154 PrivateKey PrivateKeyFile ProcessPriority ReplayWindow \
155 UDPRcvBuf UDPSndBuf \
156 Address Cipher Compression Digest MACLength PMTU \
157 Port PublicKey PublicKeyFile Subnet
159 check_gen_own_key "$s" && return 0
165 section_enabled "$s" || return 1
170 append_params "$s" logfile debug
172 SERVICE_PID_FILE="/var/run/tinc.$s.pid"
173 service_start $BIN -c "$TMP_TINC/$s" -n $s $ARGS --pidfile="$SERVICE_PID_FILE"
179 section_enabled "$s" || return 1
181 SERVICE_PID_FILE="/var/run/tinc.$s.pid"
184 rm -rf "$TMP_TINC/$s/"
190 section_enabled "$s" || return 1
192 SERVICE_PID_FILE="/var/run/tinc.$s.pid"
199 config_foreach prepare_net 'tinc-net'
200 config_foreach prepare_host 'tinc-host'
202 config_foreach start_instance 'tinc-net'
207 config_foreach stop_instance 'tinc-net'
212 config_foreach reload_instance 'tinc-net'
219 for instance in "$@"; do
220 config_get exists "$instance" 'TYPE'
221 if [ "$exists" == "tinc-net" ]; then
222 prepare_net "$instance"
223 config_foreach prepare_host 'tinc-host' "$instance"
224 start_instance "$instance"
233 for instance in "$@"; do
234 config_get exists "$instance" 'TYPE'
235 if [ "$exists" == "tinc-net" ]; then
236 stop_instance "$instance"