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.

168 lines
4.7 KiB

  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . /lib/functions/network.sh
  4. . ../netifd-proto.sh
  5. init_proto "$@"
  6. IPv4_REGEX="((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
  7. append_args() {
  8. while [ $# -gt 0 ]; do
  9. append cmdline "'${1//\'/\'\\\'\'}'"
  10. shift
  11. done
  12. }
  13. proto_openfortivpn_init_config() {
  14. proto_config_add_string "peeraddr"
  15. proto_config_add_int "port"
  16. proto_config_add_string "tunlink"
  17. proto_config_add_string "local_ip"
  18. proto_config_add_string "username"
  19. proto_config_add_string "password"
  20. proto_config_add_string "trusted_cert"
  21. proto_config_add_string "remote_status_check"
  22. no_device=1
  23. available=1
  24. }
  25. proto_openfortivpn_setup() {
  26. local config="$1"
  27. local msg ifname ip server_ips pwfile callfile
  28. local peeraddr port tunlink local_ip username password trusted_cert \
  29. remote_status_check
  30. json_get_vars host peeraddr port tunlink local_ip username password trusted_cert \
  31. remote_status_check
  32. ifname="vpn-$config"
  33. [ -n "$tunlink" ] && {
  34. network_get_device iface_device_name "$tunlink"
  35. network_is_up "$tunlink" || {
  36. msg="$tunlink is not up $iface_device_name"
  37. logger -t "openfortivpn" "$config: $msg"
  38. proto_notify_error "$config" "$msg"
  39. proto_block_restart "$config"
  40. exit 1
  41. }
  42. }
  43. if echo "$peeraddr" | grep -q -E "$IPv4_REGEX"; then
  44. server_ips="$peeraddr"
  45. elif command -v resolveip >/dev/null ; then
  46. server_ips="$(resolveip -4 -t 10 "$peeraddr")" || {
  47. msg="$config: failed to resolve server ip for $peeraddr"
  48. logger -t "openfortivpn" "$msg"
  49. sleep 10
  50. proto_notify_error "$config" "$msg"
  51. proto_setup_failed "$config"
  52. exit 1
  53. }
  54. else
  55. logger -t "openfortivpn" "resolveip not present, could not resolve $peeraddr"
  56. fi
  57. [ "$remote_status_check" = "curl" ] && {
  58. curl -k --head -s --connect-timeout 10 ${tunlink:+--interface} "$iface_device_name" "https://$peeraddr" > /dev/null || {
  59. msg="failed to reach https://$peeraddr${tunlink:+ on $iface_device_name}"
  60. logger -t "openfortivpn" "$config: $msg"
  61. sleep 10
  62. proto_notify_error "$config" "$msg"
  63. proto_setup_failed "$config"
  64. exit 1
  65. }
  66. }
  67. [ "$remote_status_check" = "ping" ] && {
  68. ping ${tunlink:+-I} "$iface_device_name" -c 1 -w 10 "$peeraddr" > /dev/null 2>&1 || {
  69. msg="$config: failed to ping $peeraddr on $iface_device_name"
  70. logger -t "openfortvpn" "$config: $msg"
  71. sleep 10
  72. proto_notify_error "$config" "$msg"
  73. proto_setup_failed "$config"
  74. exit 1
  75. }
  76. }
  77. if [ -n "$server_ips" ]; then
  78. for ip in $server_ips; do
  79. logger -p 6 -t "openfortivpn" "$config: adding host dependency for $ip on $tunlink at $config"
  80. proto_add_host_dependency "$config" "$ip" "$tunlink"
  81. done
  82. fi
  83. # uclient-fetch cannot bind to interface, so perform check after adding host dependency
  84. [ "$remote_status_check" = "fetch" ] && {
  85. uclient-fetch --no-check-certificate -q -s --timeout=10 "https://$peeraddr" > /dev/null 2>&1 || {
  86. msg="$config: failed to reach ${server_ip:-$peeraddr} on $iface_device_name"
  87. logger -t "openfortvpn" "$config: $msg"
  88. sleep 10
  89. proto_notify_error "$config" "$msg"
  90. proto_setup_failed "$config"
  91. exit 1
  92. }
  93. }
  94. [ -n "$port" ] && port=":$port"
  95. append_args "$peeraddr$port" --pppd-ifname="$ifname" --use-syslog -c /dev/null
  96. append_args "--set-dns=0"
  97. append_args "--no-routes"
  98. append_args "--pppd-use-peerdns=1"
  99. [ -n "$tunlink" ] && {
  100. append_args "--ifname=$iface_device_name"
  101. }
  102. [ -n "$trusted_cert" ] && append_args "--trusted-cert=$trusted_cert"
  103. [ -n "$username" ] && append_args -u "$username"
  104. [ -n "$password" ] && {
  105. umask 077
  106. mkdir -p '/var/etc/openfortivpn'
  107. pwfile="/var/etc/openfortivpn/$config.passwd"
  108. echo "$password" > "$pwfile"
  109. }
  110. [ -n "$local_ip" ] || local_ip=192.0.2.1
  111. [ -e '/etc/ppp/peers' ] || mkdir -p '/etc/ppp/peers'
  112. [ -e '/etc/ppp/peers/openfortivpn' ] || {
  113. ln -s -T '/var/etc/openfortivpn/peers' '/etc/ppp/peers/openfortivpn' 2> /dev/null
  114. mkdir -p '/var/etc/openfortivpn/peers'
  115. }
  116. callfile="/var/etc/openfortivpn/peers/$config"
  117. echo "115200
  118. :$local_ip
  119. noipdefault
  120. noaccomp
  121. noauth
  122. default-asyncmap
  123. nopcomp
  124. receive-all
  125. nodetach
  126. ipparam $config
  127. lcp-max-configure 40
  128. ip-up-script /lib/netifd/openfortivpn-ppp-up
  129. ip-down-script /lib/netifd/ppp-down
  130. mru 1354" > "$callfile"
  131. append_args "--pppd-call=openfortivpn/$config"
  132. logger -p 6 -t openfortivpn "$config: executing 'openfortivpn $cmdline'"
  133. eval "proto_run_command '$config' /usr/sbin/openfortivpn-wrapper '$pwfile' '$config' $cmdline"
  134. }
  135. proto_openfortivpn_teardown() {
  136. local config="$1"
  137. pwfile="/var/etc/openfortivpn/$config.passwd"
  138. callfile="/var/etc/openfortivpn/peers/$config"
  139. rm -f "$pwfile"
  140. rm -f "$callfile"
  141. proto_kill_command "$config" 2
  142. }
  143. add_protocol openfortivpn