openvpn.init 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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" ] && append_param "$s" "$p" && echo " $v" >> "/var/etc/openvpn-$s.conf"
  39. done
  40. unset IFS
  41. done
  42. }
  43. section_enabled() {
  44. config_get_bool enable "$1" 'enable' 0
  45. config_get_bool enabled "$1" 'enabled' 0
  46. [ $enable -gt 0 ] || [ $enabled -gt 0 ]
  47. }
  48. openvpn_add_instance() {
  49. local name="$1"
  50. local dir="$2"
  51. local conf="$3"
  52. procd_open_instance
  53. procd_set_param command "$PROG" \
  54. --syslog "openvpn($name)" \
  55. --status "/var/run/openvpn.$name.status" \
  56. --cd "$dir" \
  57. --config "$conf"
  58. procd_set_param file "$dir/$conf"
  59. procd_set_param respawn
  60. procd_close_instance
  61. }
  62. start_instance() {
  63. local s="$1"
  64. config_get config "$s" config
  65. config="${config:+$(readlink -f "$config")}"
  66. section_enabled "$s" || {
  67. append UCI_DISABLED "$config" "$LIST_SEP"
  68. return 1
  69. }
  70. [ ! -d "/var/run" ] && mkdir -p "/var/run"
  71. if [ ! -z "$config" ]; then
  72. append UCI_STARTED "$config" "$LIST_SEP"
  73. openvpn_add_instance "$s" "${config%/*}" "$config"
  74. return
  75. fi
  76. [ ! -d "/var/etc" ] && mkdir -p "/var/etc"
  77. [ -f "/var/etc/openvpn-$s.conf" ] && rm "/var/etc/openvpn-$s.conf"
  78. # append flags
  79. append_bools "$s" \
  80. auth_nocache auth_user_pass_optional bind ccd_exclusive client client_cert_not_required \
  81. client_to_client comp_noadapt disable \
  82. disable_occ down_pre duplicate_cn fast_io float http_proxy_retry \
  83. ifconfig_noexec ifconfig_nowarn ifconfig_pool_linear management_forget_disconnect management_hold \
  84. management_query_passwords management_signal mktun mlock mtu_test multihome mute_replay_warnings \
  85. nobind no_iv no_name_remapping no_replay opt_verify passtos persist_key persist_local_ip \
  86. persist_remote_ip persist_tun ping_timer_rem pull push_reset \
  87. remote_random rmtun route_noexec route_nopull single_session socks_proxy_retry \
  88. suppress_timestamps tcp_nodelay test_crypto tls_client tls_exit tls_server \
  89. tun_ipv6 up_delay up_restart username_as_common_name
  90. # append params
  91. append_params "$s" \
  92. cd askpass auth auth_retry auth_user_pass auth_user_pass_verify bcast_buffers ca cert \
  93. chroot cipher client_config_dir client_connect client_disconnect comp_lzo connect_freq \
  94. connect_retry connect_timeout connect_retry_max crl_verify dev dev_node dev_type dh \
  95. echo engine explicit_exit_notify fragment group hand_window hash_size \
  96. http_proxy http_proxy_option http_proxy_timeout ifconfig ifconfig_pool \
  97. ifconfig_pool_persist ifconfig_push inactive ipchange iroute keepalive \
  98. key key_method keysize learn_address link_mtu lladdr local log log_append \
  99. lport management management_log_cache max_clients \
  100. max_routes_per_client mode mssfix mtu_disc mute nice ns_cert_type ping \
  101. ping_exit ping_restart pkcs12 plugin port port_share prng proto rcvbuf \
  102. redirect_gateway remap_usr1 remote remote_cert_eku remote_cert_ku remote_cert_tls \
  103. reneg_bytes reneg_pkts reneg_sec \
  104. replay_persist replay_window resolv_retry route route_delay route_gateway \
  105. route_metric route_pre_down route_up rport script_security secret server server_bridge setenv shaper sndbuf \
  106. socks_proxy status status_version syslog tcp_queue_limit tls_auth tls_version_min \
  107. tls_cipher tls_remote tls_timeout tls_verify tmp_dir topology tran_window \
  108. tun_mtu tun_mtu_extra txqueuelen user verb down push up \
  109. verify_x509_name x509_username_field \
  110. ifconfig_ipv6 route_ipv6 server_ipv6 ifconfig_ipv6_pool ifconfig_ipv6_push iroute_ipv6
  111. openvpn_add_instance "$s" "/var/etc" "openvpn-$s.conf"
  112. }
  113. start_service() {
  114. config_load 'openvpn'
  115. config_foreach start_instance 'openvpn'
  116. local path name
  117. for path in /etc/openvpn/*.conf; do
  118. if [ -f "$path" ]; then
  119. name="${path##*/}"; name="${name%.conf}"
  120. # don't start configs again that are already started by uci
  121. if echo "$UCI_STARTED" | grep -qxF "$path"; then
  122. continue
  123. # don't start configs which are set to disabled in uci
  124. elif echo "$UCI_DISABLED" | grep -qxF "$path"; then
  125. logger -t openvpn "$name.conf is disabled in /etc/config/openvpn"
  126. continue
  127. fi
  128. openvpn_add_instance "$name" "${path%/*}" "$path"
  129. fi
  130. done
  131. }
  132. service_triggers() {
  133. procd_add_reload_trigger openvpn
  134. }