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.

144 lines
4.4 KiB

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