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.

209 lines
4.7 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2008-2013 OpenWrt.org
  3. # Copyright (C) 2008 Jo-Philipp Wich
  4. # This is free software, licensed under the GNU General Public License v2.
  5. # See /LICENSE for more information.
  6. START=90
  7. STOP=10
  8. USE_PROCD=1
  9. PROG=/usr/sbin/openvpn
  10. LIST_SEP="
  11. "
  12. UCI_STARTED=
  13. UCI_DISABLED=
  14. append_param() {
  15. local s="$1"
  16. local v="$2"
  17. case "$v" in
  18. *_*_*_*) v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
  19. *_*_*) v=${v%%_*}-${v#*_}; v=${v%%_*}-${v#*_} ;;
  20. *_*) v=${v%%_*}-${v#*_} ;;
  21. esac
  22. echo -n "$v" >> "/var/etc/openvpn-$s.conf"
  23. return 0
  24. }
  25. append_bools() {
  26. local p; local v; local s="$1"; shift
  27. for p in $*; do
  28. config_get_bool v "$s" "$p"
  29. [ "$v" = 1 ] && append_param "$s" "$p" && echo >> "/var/etc/openvpn-$s.conf"
  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" ] && [ "$p" != "push" ] && append_param "$s" "$p" && echo " $v" >> "/var/etc/openvpn-$s.conf"
  39. [ -n "$v" ] && [ "$p" = "push" ] && append_param "$s" "$p" && echo " \"$v\"" >> "/var/etc/openvpn-$s.conf"
  40. done
  41. unset IFS
  42. done
  43. }
  44. append_list() {
  45. local p; local v; local s="$1"; shift
  46. list_cb_append() {
  47. v="${v}:$1"
  48. }
  49. for p in $*; do
  50. unset v
  51. config_list_foreach "$s" "$p" list_cb_append
  52. [ -n "$v" ] && append_param "$s" "$p" && echo " ${v:1}" >> "/var/etc/openvpn-$s.conf"
  53. done
  54. }
  55. section_enabled() {
  56. config_get_bool enable "$1" 'enable' 0
  57. config_get_bool enabled "$1" 'enabled' 0
  58. [ $enable -gt 0 ] || [ $enabled -gt 0 ]
  59. }
  60. openvpn_get_dev() {
  61. local dev dev_type
  62. local name="$1"
  63. local conf="$2"
  64. # Do override only for configurations with config_file
  65. config_get config_file "$name" config
  66. [ -n "$config_file" ] || return
  67. # Check there is someething to override
  68. config_get dev "$name" dev
  69. config_get dev_type "$name" dev_type
  70. [ -n "$dev" ] || return
  71. # If there is a no dev_type, try to guess it
  72. if [ -z "$dev_type" ]; then
  73. . /lib/functions/openvpn.sh
  74. local odev odev_type
  75. get_openvpn_option "$conf" odev dev
  76. get_openvpn_option "$conf" odev_type dev-type
  77. [ -n "$odev_type" ] || odev_type="$odev"
  78. case "$odev_type" in
  79. tun*) dev_type="tun" ;;
  80. tap*) dev_type="tap" ;;
  81. *) return;;
  82. esac
  83. fi
  84. # Return overrides
  85. echo "--dev-type $dev_type --dev $dev"
  86. }
  87. openvpn_add_instance() {
  88. local name="$1"
  89. local dir="$2"
  90. local conf="$3"
  91. local security="$4"
  92. procd_open_instance "$name"
  93. procd_set_param command "$PROG" \
  94. --syslog "openvpn($name)" \
  95. --status "/var/run/openvpn.$name.status" \
  96. --cd "$dir" \
  97. --config "$conf" \
  98. --up "/usr/libexec/openvpn-hotplug up $name" \
  99. --down "/usr/libexec/openvpn-hotplug down $name" \
  100. --script-security "${security:-2}" \
  101. $(openvpn_get_dev "$name" "$conf")
  102. procd_set_param file "$dir/$conf"
  103. procd_set_param term_timeout 15
  104. procd_set_param respawn
  105. procd_append_param respawn 3600
  106. procd_append_param respawn 5
  107. procd_append_param respawn -1
  108. procd_close_instance
  109. }
  110. start_instance() {
  111. local s="$1"
  112. config_get config "$s" config
  113. config="${config:+$(readlink -f "$config")}"
  114. section_enabled "$s" || {
  115. append UCI_DISABLED "$config" "$LIST_SEP"
  116. return 1
  117. }
  118. local script_security
  119. config_get script_security "$s" script_security
  120. [ ! -d "/var/run" ] && mkdir -p "/var/run"
  121. if [ ! -z "$config" ]; then
  122. append UCI_STARTED "$config" "$LIST_SEP"
  123. openvpn_add_instance "$s" "${config%/*}" "$config" "$script_security"
  124. return
  125. fi
  126. [ ! -d "/var/etc" ] && mkdir -p "/var/etc"
  127. [ -f "/var/etc/openvpn-$s.conf" ] && rm "/var/etc/openvpn-$s.conf"
  128. append_bools "$s" $OPENVPN_BOOLS
  129. append_params "$s" $OPENVPN_PARAMS
  130. append_list "$s" $OPENVPN_LIST
  131. openvpn_add_instance "$s" "/var/etc" "openvpn-$s.conf" "$script_security"
  132. }
  133. start_service() {
  134. local instance="$1"
  135. local instance_found=0
  136. config_cb() {
  137. local type="$1"
  138. local name="$2"
  139. if [ "$type" = "openvpn" ]; then
  140. if [ -n "$instance" -a "$instance" = "$name" ]; then
  141. instance_found=1
  142. fi
  143. fi
  144. }
  145. . /usr/share/openvpn/openvpn.options
  146. config_load 'openvpn'
  147. if [ -n "$instance" ]; then
  148. [ "$instance_found" -gt 0 ] || return
  149. start_instance "$instance"
  150. else
  151. config_foreach start_instance 'openvpn'
  152. local path name
  153. for path in /etc/openvpn/*.conf; do
  154. if [ -f "$path" ]; then
  155. name="${path##*/}"; name="${name%.conf}"
  156. # don't start configs again that are already started by uci
  157. if echo "$UCI_STARTED" | grep -qxF "$path"; then
  158. continue
  159. # don't start configs which are set to disabled in uci
  160. elif echo "$UCI_DISABLED" | grep -qxF "$path"; then
  161. logger -t openvpn "$name.conf is disabled in /etc/config/openvpn"
  162. continue
  163. fi
  164. openvpn_add_instance "$name" "${path%/*}" "$path"
  165. fi
  166. done
  167. fi
  168. }
  169. service_triggers() {
  170. procd_add_reload_trigger openvpn
  171. }