You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
5.6 KiB

  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.
  7. START=42
  8. SERVICE_USE_PID=1
  9. BIN=/usr/sbin/tincd
  10. if ( type extra_command >/dev/null 2>&1 ); then
  11. extra_command "up" "<instance> Setting instance up"
  12. extra_command "down" "<instance> Setting instance down"
  13. else
  14. EXTRA_COMMANDS="up down"
  15. fi
  16. LIST_SEP="
  17. "
  18. TMP_TINC="/tmp/tinc"
  19. append_param() {
  20. local v="$1"
  21. case "$v" in
  22. *_*_*_*) v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
  23. *_*_*) v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
  24. *_*) v=${v%%_*}-${v#*_} ;;
  25. esac
  26. ARGS="$ARGS --$v"
  27. return 0
  28. }
  29. append_conf_bools() {
  30. local p; local v; local s="$1"; local f="$2"; shift; shift
  31. for p in $*; do
  32. config_get_bool v "$s" "$p"
  33. [ "$v" == 1 ] && echo "$p = yes" >> "$f"
  34. [ "$v" == 0 ] && echo "$p = no" >> "$f"
  35. done
  36. }
  37. append_params() {
  38. local p; local v; local s="$1"; shift
  39. for p in $*; do
  40. config_get v "$s" "$p"
  41. IFS="$LIST_SEP"
  42. for v in $v; do
  43. [ -n "$v" ] && append_param "$p" && ARGS="$ARGS=$v"
  44. done
  45. unset IFS
  46. done
  47. }
  48. append_conf_params() {
  49. local p; local v; local s="$1"; local f="$2"; shift; shift
  50. for p in $*; do
  51. config_get v "$s" "$p"
  52. IFS="$LIST_SEP"
  53. for v in $v; do
  54. # Look up OpenWRT interface names
  55. [ "$p" = "BindToInterface" ] && {
  56. local ifname=$(uci -P /var/state get network.$v.ifname 2>&-)
  57. [ -n "$ifname" ] && v="$ifname"
  58. }
  59. [ -n "$v" ] && echo "$p = $v" >> "$f"
  60. done
  61. unset IFS
  62. done
  63. }
  64. section_enabled() {
  65. config_get_bool enabled "$1" 'enabled' 0
  66. [ $enabled -gt 0 ]
  67. }
  68. prepare_host() {
  69. local s="$1"
  70. local n
  71. # net disabled?
  72. config_get n "$s" net
  73. section_enabled "$n" || return 1
  74. if [ "$#" = "2" ]; then
  75. [ "$2" != "$n" ] && return 1
  76. fi
  77. # host disabled?
  78. section_enabled "$s" || {
  79. [ -f "$TMP_TINC/$n/hosts/$s" ] && rm "$TMP_TINC/$n/hosts/$s"
  80. return 1
  81. }
  82. [ ! -f "/etc/tinc/$n/hosts/$s" ] && {
  83. echo -n "tinc: Warning, public key for $s for network $n "
  84. echo -n "missing in /etc/tinc/$n/hosts/$s, "
  85. echo "skipping configuration of $s"
  86. return 1
  87. }
  88. # append flags
  89. append_conf_bools "$s" "$TMP_TINC/$n/hosts/$s" \
  90. ClampMSS IndirectData PMTUDiscovery TCPOnly
  91. # append params
  92. append_conf_params "$s" "$TMP_TINC/$n/hosts/$s" \
  93. Address Cipher Compression Digest MACLength PMTU \
  94. Port PublicKey PublicKeyFile Subnet
  95. }
  96. check_gen_own_key() {
  97. local s="$1"; local n; local k
  98. config_get n "$s" Name
  99. config_get_bool k "$s" generate_keys 0
  100. [ "$k" == 0 ] && return 0
  101. ([ -z "$n" ] || [ -f "$TMP_TINC/$s/hosts/$n" ] || [ -f "$TMP_TINC/$s/rsa_key.priv" ]) && \
  102. return 0
  103. [ ! -d "$TMP_TINC/$s/hosts" ] && mkdir -p "$TMP_TINC/$s/hosts"
  104. config_get k "$s" key_size
  105. if [ -z "$k" ]; then
  106. $BIN -c "$TMP_TINC/$s" --generate-keys </dev/null
  107. else
  108. $BIN -c "$TMP_TINC/$s" "--generate-keys=$k" </dev/null
  109. fi
  110. [ ! -d "/etc/tinc/$s/hosts" ] && mkdir -p "/etc/tinc/$s/hosts"
  111. cp "$TMP_TINC/$s/rsa_key.priv" "/etc/tinc/$s/"
  112. [ -n "$n" ] && cp "$TMP_TINC/$s/hosts/$n" "/etc/tinc/$s/hosts/"
  113. }
  114. prepare_net() {
  115. local s="$1"
  116. local n
  117. section_enabled "$s" || return 1
  118. [ -d "$TMP_TINC/$s" ] && rm -rf "$TMP_TINC/$s/"
  119. mkdir -p "$TMP_TINC/$s"
  120. [ -d "/etc/tinc/$s" ] && cp -r "/etc/tinc/$s" "$TMP_TINC/"
  121. # append flags
  122. append_conf_bools "$s" "$TMP_TINC/$s/tinc.conf" \
  123. AutoConnect \
  124. DecrementTTL \
  125. DeviceStandby \
  126. DirectOnly \
  127. ExperimentalProtocol \
  128. Hostnames \
  129. LocalDiscovery \
  130. PriorityInheritance \
  131. StrictSubnets \
  132. TunnelServer \
  133. ClampMSS \
  134. IndirectData \
  135. PMTUDiscovery \
  136. TCPOnly
  137. # append params
  138. append_conf_params "$s" "$TMP_TINC/$s/tinc.conf" \
  139. AddressFamily \
  140. BindToAddress \
  141. BindToInterface \
  142. Broadcast \
  143. BroadcastSubnet \
  144. ConnectTo \
  145. Device \
  146. DeviceType \
  147. Ed25519PrivateKeyFile \
  148. ECDSAPublicKey \
  149. Forwarding \
  150. Interface \
  151. ListenAddress \
  152. LocalDiscoveryAddress \
  153. Mode \
  154. KeyExpire \
  155. MACExpire \
  156. MaxConnectionBurst \
  157. Name \
  158. PingInterval \
  159. PingTimeout \
  160. PrivateKey \
  161. PrivateKeyFile \
  162. ProcessPriority \
  163. Proxy \
  164. ReplayWindow \
  165. UDPRcvBuf \
  166. UDPSndBuf \
  167. Address \
  168. Cipher \
  169. Compression \
  170. Digest \
  171. MACLength \
  172. PMTU \
  173. Port \
  174. PublicKey \
  175. PublicKeyFile \
  176. Subnet \
  177. Weight
  178. check_gen_own_key "$s" && return 0
  179. }
  180. start_instance() {
  181. local s="$1"
  182. section_enabled "$s" || return 1
  183. ARGS=""
  184. # append params
  185. append_params "$s" logfile debug
  186. SERVICE_PID_FILE="/var/run/tinc.$s.pid"
  187. service_start $BIN -c "$TMP_TINC/$s" $ARGS --pidfile="$SERVICE_PID_FILE"
  188. }
  189. stop_instance() {
  190. local s="$1"
  191. section_enabled "$s" || return 1
  192. SERVICE_PID_FILE="/var/run/tinc.$s.pid"
  193. service_stop $BIN
  194. # rm old config
  195. rm -rf "$TMP_TINC/$s/"
  196. }
  197. reload_instance() {
  198. local s="$1"
  199. section_enabled "$s" || return 1
  200. SERVICE_PID_FILE="/var/run/tinc.$s.pid"
  201. service_reload $BIN
  202. }
  203. start() {
  204. config_load 'tinc'
  205. config_foreach prepare_net 'tinc-net'
  206. config_foreach prepare_host 'tinc-host'
  207. config_foreach start_instance 'tinc-net'
  208. }
  209. stop() {
  210. config_load 'tinc'
  211. config_foreach stop_instance 'tinc-net'
  212. }
  213. reload() {
  214. config_load 'tinc'
  215. config_foreach reload_instance 'tinc-net'
  216. }
  217. up() {
  218. local exists
  219. local instance
  220. config_load 'tinc'
  221. for instance in "$@"; do
  222. config_get exists "$instance" 'TYPE'
  223. if [ "$exists" == "tinc-net" ]; then
  224. prepare_net "$instance"
  225. config_foreach prepare_host 'tinc-host' "$instance"
  226. start_instance "$instance"
  227. fi
  228. done
  229. }
  230. down() {
  231. local exists
  232. local instance
  233. config_load 'tinc'
  234. for instance in "$@"; do
  235. config_get exists "$instance" 'TYPE'
  236. if [ "$exists" == "tinc-net" ]; then
  237. stop_instance "$instance"
  238. fi
  239. done
  240. }