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.

125 lines
4.1 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  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_openconnect_init_config() {
  12. proto_config_add_string "server"
  13. proto_config_add_int "port"
  14. proto_config_add_int "mtu"
  15. proto_config_add_int "juniper"
  16. proto_config_add_string "interface"
  17. proto_config_add_string "username"
  18. proto_config_add_string "serverhash"
  19. proto_config_add_string "authgroup"
  20. proto_config_add_string "password"
  21. proto_config_add_string "password2"
  22. proto_config_add_string "token_mode"
  23. proto_config_add_string "token_secret"
  24. proto_config_add_string "token_script"
  25. proto_config_add_string "os"
  26. proto_config_add_string "csd_wrapper"
  27. proto_config_add_array 'form_entry:regex("[^:]+:[^=]+=.*")'
  28. no_device=1
  29. available=1
  30. }
  31. proto_openconnect_add_form_entry() {
  32. [ -n "$1" ] && append_args --form-entry "$1"
  33. }
  34. proto_openconnect_setup() {
  35. local config="$1"
  36. json_get_vars server port interface username serverhash authgroup password password2 token_mode token_secret token_script os csd_wrapper mtu juniper form_entry
  37. grep -q tun /proc/modules || insmod tun
  38. ifname="vpn-$config"
  39. logger -t openconnect "initializing..."
  40. logger -t "openconnect" "adding host dependency for $server at $config"
  41. for ip in $(resolveip -t 10 "$server"); do
  42. logger -t "openconnect" "adding host dependency for $ip at $config"
  43. proto_add_host_dependency "$config" "$ip" "$interface"
  44. done
  45. [ -n "$port" ] && port=":$port"
  46. append_args "$server$port" -i "$ifname" --non-inter --syslog --script /lib/netifd/vpnc-script
  47. [ -n "$mtu" ] && append_args --mtu "$mtu"
  48. # migrate to standard config files
  49. [ -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"
  50. [ -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"
  51. [ -f "/etc/config/openconnect-ca-vpn-$config.pem" ] && mv "/etc/config/openconnect-ca-vpn-$config.pem" "/etc/openconnect/ca-vpn-$config.pem"
  52. [ -f /etc/openconnect/user-cert-vpn-$config.pem ] && append_args -c "/etc/openconnect/user-cert-vpn-$config.pem"
  53. [ -f /etc/openconnect/user-key-vpn-$config.pem ] && append_args --sslkey "/etc/openconnect/user-key-vpn-$config.pem"
  54. [ -f /etc/openconnect/ca-vpn-$config.pem ] && {
  55. append_args --cafile "/etc/openconnect/ca-vpn-$config.pem"
  56. append_args --no-system-trust
  57. }
  58. if [ "${juniper:-0}" -gt 0 ]; then
  59. append_args --juniper
  60. fi
  61. [ -n "$serverhash" ] && {
  62. append_args "--servercert=$serverhash"
  63. append_args --no-system-trust
  64. }
  65. [ -n "$authgroup" ] && append_args --authgroup "$authgroup"
  66. [ -n "$username" ] && append_args -u "$username"
  67. [ -n "$password" ] || [ "$token_mode" = "script" ] && {
  68. umask 077
  69. mkdir -p /var/etc
  70. pwfile="/var/etc/openconnect-$config.passwd"
  71. [ -n "$password" ] && {
  72. echo "$password" > "$pwfile"
  73. [ -n "$password2" ] && echo "$password2" >> "$pwfile"
  74. }
  75. [ "$token_mode" = "script" ] && {
  76. $token_script > "$pwfile" 2> /dev/null || {
  77. logger -t openconenct "Cannot get password from script '$token_script'"
  78. proto_setup_failed "$config"
  79. }
  80. }
  81. append_args --passwd-on-stdin
  82. }
  83. [ -n "$token_mode" -a "$token_mode" != "script" ] && append_args "--token-mode=$token_mode"
  84. [ -n "$token_secret" ] && append_args "--token-secret=$token_secret"
  85. [ -n "$os" ] && append_args "--os=$os"
  86. [ -n "$csd_wrapper" ] && [ -x "$csd_wrapper" ] && append_args "--csd-wrapper=$csd_wrapper"
  87. json_for_each_item proto_openconnect_add_form_entry form_entry
  88. proto_export INTERFACE="$config"
  89. logger -t openconnect "executing 'openconnect $cmdline'"
  90. if [ -f "$pwfile" ]; then
  91. eval "proto_run_command '$config' /usr/sbin/openconnect-wrapper '$pwfile' $cmdline"
  92. else
  93. eval "proto_run_command '$config' /usr/sbin/openconnect $cmdline"
  94. fi
  95. }
  96. proto_openconnect_teardown() {
  97. local config="$1"
  98. pwfile="/var/etc/openconnect-$config.passwd"
  99. rm -f $pwfile
  100. logger -t openconnect "bringing down openconnect"
  101. proto_kill_command "$config" 2
  102. }
  103. add_protocol openconnect