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.

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