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.

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