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.

110 lines
4.1 KiB

  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . ../netifd-proto.sh
  4. init_proto "$@"
  5. proto_vpnc_init_config() {
  6. proto_config_add_string "server"
  7. proto_config_add_string "username"
  8. proto_config_add_string "hexpasswd"
  9. proto_config_add_string "authgroup"
  10. proto_config_add_string "password"
  11. proto_config_add_string "token_mode"
  12. proto_config_add_string "token_secret"
  13. proto_config_add_string "interface"
  14. proto_config_add_string "passgroup"
  15. proto_config_add_string "hexpassgroup"
  16. proto_config_add_string "domain"
  17. proto_config_add_string "vendor"
  18. proto_config_add_string "natt_mode"
  19. proto_config_add_string "dh_group"
  20. proto_config_add_string "pfs"
  21. proto_config_add_boolean "enable_single_des"
  22. proto_config_add_boolean "enable_no_enc"
  23. proto_config_add_int "mtu"
  24. proto_config_add_string "local_addr"
  25. proto_config_add_int "local_port"
  26. proto_config_add_int "udp_port"
  27. proto_config_add_int "dpd_idle"
  28. proto_config_add_string "auth_mode"
  29. proto_config_add_string "target_network"
  30. proto_config_add_boolean "authfail"
  31. no_device=1
  32. available=1
  33. }
  34. proto_vpnc_setup() {
  35. local config="$1"
  36. json_get_vars server username hexpasswd authgroup password token_mode token_secret interface passgroup hexpassgroup domain vendor natt_mode dh_group pfs enable_single_des enable_no_enc mtu local_addr local_port udp_port dpd_idle auth_mode target_network authfail
  37. grep -q tun /proc/modules || insmod tun
  38. logger -t vpnc "initializing..."
  39. serv_addr=
  40. for ip in $(resolveip -t 10 "$server"); do
  41. ( proto_add_host_dependency "vpn-$config" "$ip" $interface )
  42. serv_addr=1
  43. done
  44. [ -n "$serv_addr" ] || {
  45. logger -t vpnc "Could not resolve server address: '$server'"
  46. sleep 60
  47. proto_setup_failed "$config"
  48. exit 1
  49. }
  50. mkdir -p /var/etc
  51. umask 077
  52. pwfile="/var/etc/vpnc-$config.conf"
  53. echo "IPSec gateway $server" > "$pwfile"
  54. cmdline="--no-detach --pid-file /var/run/vpnc-$config.pid --ifname vpn-$config --non-inter --script /lib/netifd/vpnc-script $pwfile"
  55. [ -f /etc/vpnc/ca-vpn-$config.pem ] && echo "CA-File /etc/vpnc/ca-vpn-$config.pem" >> "$pwfile"
  56. [ -n "$hexpasswd" ] && echo "Xauth obfuscated password $hexpasswd" >> "$pwfile"
  57. [ -n "$authgroup" ] && echo "IPSec ID $authgroup" >> "$pwfile"
  58. [ -n "$username" ] && echo "Xauth username $username" >> "$pwfile"
  59. [ -n "$password" ] && echo "Xauth password $password" >> "$pwfile"
  60. [ -n "$passgroup" ] && echo "IPSec secret $passgroup" >> "$pwfile"
  61. [ -n "$hexpassgroup" ] && echo "IPSec obfuscated secret $hexpassgroup" >> "$pwfile"
  62. [ -n "$domain" ] && echo "Domain $domain" >> "$pwfile"
  63. [ -n "$vendor" ] && echo "Vendor $vendor" >> "$pwfile"
  64. [ -n "$natt_mode" ] && echo "NAT Traversal Mode $natt_mode" >> "$pwfile"
  65. [ -n "$dh_group" ] && echo "IKE DH Group $dh_group" >> "$pwfile"
  66. [ -n "$pfs" ] && echo "Perfect Forward Secrecy $pfs" >> "$pwfile"
  67. [ "${enable_single_des:-0}" -gt 0 ] && echo "Enable Single DES" >> "$pwfile"
  68. [ "${enable_no_enc:-0}" -gt 0 ] && echo "Enable no encryption" >> "$pwfile"
  69. [ -n "$mtu" ] && echo "Interface MTU $mtu" >> "$pwfile"
  70. [ -n "$local_addr" ] && echo "Local Addr $local_addr" >> "$pwfile"
  71. [ -n "$local_port" ] && echo "Local Port $local_port" >> "$pwfile"
  72. [ -n "$udp_port" ] && echo "Cisco UDP Encapsulation Port $udp_port" >> "$pwfile"
  73. [ -n "$dpd_idle" ] && echo "DPD idle timeout (our side) $dpd_idle" >> "$pwfile"
  74. [ -n "$auth_mode" ] && echo "IKE Authmode $auth_mode" >> "$pwfile"
  75. [ -n "$target_network" ] && echo "IPSEC target network $target_network" >> "$pwfile"
  76. proto_export INTERFACE="$config"
  77. logger -t vpnc "executing 'vpnc $cmdline'"
  78. proto_run_command "$config" /usr/sbin/vpnc $cmdline
  79. }
  80. proto_vpnc_teardown() {
  81. local config="$1"
  82. pwfile="/var/etc/vpnc-$config.conf"
  83. json_get_var authfail authfail
  84. # On error exit (vpnc only has success = 0 and error = 1, so
  85. # we can't be fine-grained and say only auth error)
  86. # and authfail setting true, then don't retry starting vpnc
  87. # This is used for the case were the server blocks repeated
  88. # failed authentication attempts (which will occur if the password
  89. # is wrong, for example).
  90. if [ ${ERROR:-0} -gt 0 ] && [ "${authfail:-0}" -gt 0 ]; then
  91. proto_block_restart "$config"
  92. fi
  93. rm -f $pwfile
  94. logger -t vpnc "bringing down vpnc"
  95. proto_kill_command "$config" 2
  96. }
  97. add_protocol vpnc