openconnect.sh 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . ../netifd-proto.sh
  4. init_proto "$@"
  5. proto_openconnect_init_config() {
  6. proto_config_add_string "server"
  7. proto_config_add_int "port"
  8. proto_config_add_int "mtu"
  9. proto_config_add_string "username"
  10. proto_config_add_string "serverhash"
  11. proto_config_add_string "authgroup"
  12. proto_config_add_string "password"
  13. proto_config_add_string "password2"
  14. proto_config_add_string "token_mode"
  15. proto_config_add_string "token_secret"
  16. proto_config_add_string "os"
  17. proto_config_add_string "csd_wrapper"
  18. no_device=1
  19. available=1
  20. }
  21. proto_openconnect_setup() {
  22. local config="$1"
  23. json_get_vars server port username serverhash authgroup password password2 token_mode token_secret os csd_wrapper mtu
  24. grep -q tun /proc/modules || insmod tun
  25. ifname="vpn-$config"
  26. logger -t openconnect "initializing..."
  27. logger -t "openconnect" "adding host dependency for $server at $config"
  28. for ip in $(resolveip -t 10 "$server"); do
  29. logger -t "openconnect" "adding host dependency for $ip at $config"
  30. proto_add_host_dependency "$config" "$ip"
  31. done
  32. [ -n "$port" ] && port=":$port"
  33. cmdline="$server$port -i "$ifname" --non-inter --syslog --script /lib/netifd/vpnc-script"
  34. [ -n "$mtu" ] && cmdline="$cmdline --mtu $mtu"
  35. # migrate to standard config files
  36. [ -f "/etc/config/openconnect-user-cert-vpn-$config.pem" ] && mv "/etc/config/openconnect-user-cert-vpn-$config.pem" "/etc/openconnect/user-cert-vpn-$config.pem"
  37. [ -f "/etc/config/openconnect-user-key-vpn-$config.pem" ] && mv "/etc/config/openconnect-user-key-vpn-$config.pem" "/etc/openconnect/user-key-vpn-$config.pem"
  38. [ -f "/etc/config/openconnect-ca-vpn-$config.pem" ] && mv "/etc/config/openconnect-ca-vpn-$config.pem" "/etc/openconnect/ca-vpn-$config.pem"
  39. [ -f /etc/openconnect/user-cert-vpn-$config.pem ] && append cmdline "-c /etc/openconnect/user-cert-vpn-$config.pem"
  40. [ -f /etc/openconnect/user-key-vpn-$config.pem ] && append cmdline "--sslkey /etc/openconnect/user-key-vpn-$config.pem"
  41. [ -f /etc/openconnect/ca-vpn-$config.pem ] && {
  42. append cmdline "--cafile /etc/openconnect/ca-vpn-$config.pem"
  43. append cmdline "--no-system-trust"
  44. }
  45. [ -n "$serverhash" ] && {
  46. append cmdline " --servercert=$serverhash"
  47. append cmdline "--no-system-trust"
  48. }
  49. [ -n "$authgroup" ] && append cmdline "--authgroup $authgroup"
  50. [ -n "$username" ] && append cmdline "-u $username"
  51. [ -n "$password" ] && {
  52. umask 077
  53. mkdir -p /var/etc
  54. pwfile="/var/etc/openconnect-$config.passwd"
  55. echo "$password" > "$pwfile"
  56. [ -n "$password2" ] && echo "$password2" >> "$pwfile"
  57. append cmdline "--passwd-on-stdin"
  58. }
  59. [ -n "$token_mode" ] && append cmdline "--token-mode=$token_mode"
  60. [ -n "$token_secret" ] && append cmdline "--token-secret=$token_secret"
  61. [ -n "$os" ] && append cmdline "--os=$os"
  62. [ -n "$csd_wrapper" ] && [ -x "$csd_wrapper" ] && append cmdline "--csd-wrapper=$csd_wrapper"
  63. proto_export INTERFACE="$config"
  64. logger -t openconnect "executing 'openconnect $cmdline'"
  65. if [ -f "$pwfile" ]; then
  66. proto_run_command "$config" /usr/sbin/openconnect-wrapper $pwfile $cmdline
  67. else
  68. proto_run_command "$config" /usr/sbin/openconnect $cmdline
  69. fi
  70. }
  71. proto_openconnect_teardown() {
  72. local config="$1"
  73. pwfile="/var/etc/openconnect-$config.passwd"
  74. rm -f $pwfile
  75. logger -t openconnect "bringing down openconnect"
  76. proto_kill_command "$config" 2
  77. }
  78. add_protocol openconnect