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.

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