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.

149 lines
4.6 KiB

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