2
0

openvpn.init 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. section_enabled() {
  45. config_get_bool enable "$1" 'enable' 0
  46. config_get_bool enabled "$1" 'enabled' 0
  47. [ $enable -gt 0 ] || [ $enabled -gt 0 ]
  48. }
  49. openvpn_add_instance() {
  50. local name="$1"
  51. local dir="$2"
  52. local conf="$3"
  53. procd_open_instance
  54. procd_set_param command "$PROG" \
  55. --syslog "openvpn($name)" \
  56. --status "/var/run/openvpn.$name.status" \
  57. --cd "$dir" \
  58. --config "$conf"
  59. procd_set_param file "$dir/$conf"
  60. procd_set_param respawn
  61. procd_append_param respawn 3600
  62. procd_append_param respawn 5
  63. procd_append_param respawn -1
  64. procd_close_instance
  65. }
  66. start_instance() {
  67. local s="$1"
  68. config_get config "$s" config
  69. config="${config:+$(readlink -f "$config")}"
  70. section_enabled "$s" || {
  71. append UCI_DISABLED "$config" "$LIST_SEP"
  72. return 1
  73. }
  74. [ ! -d "/var/run" ] && mkdir -p "/var/run"
  75. if [ ! -z "$config" ]; then
  76. append UCI_STARTED "$config" "$LIST_SEP"
  77. openvpn_add_instance "$s" "${config%/*}" "$config"
  78. return
  79. fi
  80. [ ! -d "/var/etc" ] && mkdir -p "/var/etc"
  81. [ -f "/var/etc/openvpn-$s.conf" ] && rm "/var/etc/openvpn-$s.conf"
  82. append_bools "$s" $OPENVPN_BOOLS
  83. append_params "$s" $OPENVPN_PARAMS
  84. openvpn_add_instance "$s" "/var/etc" "openvpn-$s.conf"
  85. }
  86. start_service() {
  87. . /usr/share/openvpn/openvpn.options
  88. config_load 'openvpn'
  89. config_foreach start_instance 'openvpn'
  90. local path name
  91. for path in /etc/openvpn/*.conf; do
  92. if [ -f "$path" ]; then
  93. name="${path##*/}"; name="${name%.conf}"
  94. # don't start configs again that are already started by uci
  95. if echo "$UCI_STARTED" | grep -qxF "$path"; then
  96. continue
  97. # don't start configs which are set to disabled in uci
  98. elif echo "$UCI_DISABLED" | grep -qxF "$path"; then
  99. logger -t openvpn "$name.conf is disabled in /etc/config/openvpn"
  100. continue
  101. fi
  102. openvpn_add_instance "$name" "${path%/*}" "$path"
  103. fi
  104. done
  105. }
  106. service_triggers() {
  107. procd_add_reload_trigger openvpn
  108. }