1
0

tinc.init 5.1 KB

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