2 * OpenConnect (SSL + DTLS) VPN client
4 * Copyright © 2008-2012 Intel Corporation.
5 * Copyright © 2008 Nick Andrew <nick@nick-andrew.net>
7 * Author: David Woodhouse <dwmw2@infradead.org>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * version 2.1, as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to:
21 * Free Software Foundation, Inc.
22 * 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301 USA
34 #include <sys/types.h>
40 #include "openconnect-internal.h"
42 static int proxy_write(struct openconnect_info *vpninfo, int fd,
43 unsigned char *buf, size_t len);
44 static int proxy_read(struct openconnect_info *vpninfo, int fd,
45 unsigned char *buf, size_t len);
47 #define MAX_BUF_LEN 131072
48 #define BUF_CHUNK_SIZE 4096
57 static struct oc_text_buf *buf_alloc(void)
59 return calloc(1, sizeof(struct oc_text_buf));
62 static void buf_append(struct oc_text_buf *buf, const char *fmt, ...)
66 if (!buf || buf->error)
70 buf->data = malloc(BUF_CHUNK_SIZE);
75 buf->buf_len = BUF_CHUNK_SIZE;
79 int max_len = buf->buf_len - buf->pos, ret;
82 ret = vsnprintf(buf->data + buf->pos, max_len, fmt, ap);
87 } else if (ret < max_len) {
91 int new_buf_len = buf->buf_len + BUF_CHUNK_SIZE;
93 if (new_buf_len > MAX_BUF_LEN) {
94 /* probably means somebody is messing with us */
99 buf->data = realloc(buf->data, new_buf_len);
101 buf->error = -ENOMEM;
104 buf->buf_len = new_buf_len;
109 static int buf_error(struct oc_text_buf *buf)
111 return buf ? buf->error : -ENOMEM;
114 static int buf_free(struct oc_text_buf *buf)
116 int error = buf_error(buf);
128 * We didn't really want to have to do this for ourselves -- one might have
129 * thought that it would be available in a library somewhere. But neither
130 * cURL nor Neon have reliable cross-platform ways of either using a cert
131 * from the TPM, or just reading from / writing to a transport which is
132 * provided by their caller.
135 static int http_add_cookie(struct openconnect_info *vpninfo,
136 const char *option, const char *value)
138 struct vpn_option *new, **this;
141 new = malloc(sizeof(*new));
143 vpn_progress(vpninfo, PRG_ERR,
144 _("No memory for allocating cookies\n"));
148 new->option = strdup(option);
149 new->value = strdup(value);
150 if (!new->option || !new->value) {
157 /* Kill cookie; don't replace it */
160 for (this = &vpninfo->cookies; *this; this = &(*this)->next) {
161 if (!strcmp(option, (*this)->option)) {
162 /* Replace existing cookie */
164 new->next = (*this)->next;
168 free((*this)->option);
169 free((*this)->value);
182 #define BODY_HTTP10 -1
183 #define BODY_CHUNKED -2
185 static int process_http_response(struct openconnect_info *vpninfo, int *result,
186 int (*header_cb)(struct openconnect_info *, char *, char *),
189 char buf[MAX_BUF_LEN];
191 int bodylen = BODY_HTTP10;
197 if (openconnect_SSL_gets(vpninfo, buf, sizeof(buf)) < 0) {
198 vpn_progress(vpninfo, PRG_ERR,
199 _("Error fetching HTTPS response\n"));
203 if (!strncmp(buf, "HTTP/1.0 ", 9))
206 if ((!closeconn && strncmp(buf, "HTTP/1.1 ", 9)) || !(*result = atoi(buf+9))) {
207 vpn_progress(vpninfo, PRG_ERR,
208 _("Failed to parse HTTP response '%s'\n"), buf);
212 vpn_progress(vpninfo, (*result==200)?PRG_TRACE:PRG_INFO,
213 _("Got HTTP response: %s\n"), buf);
216 while ((i = openconnect_SSL_gets(vpninfo, buf, sizeof(buf)))) {
220 vpn_progress(vpninfo, PRG_ERR,
221 _("Error processing HTTP response\n"));
224 colon = strchr(buf, ':');
226 vpn_progress(vpninfo, PRG_ERR,
227 _("Ignoring unknown HTTP response line '%s'\n"), buf);
234 /* Handle Set-Cookie first so that we can avoid printing the
235 webvpn cookie in the verbose debug output */
236 if (!strcasecmp(buf, "Set-Cookie")) {
237 char *semicolon = strchr(colon, ';');
238 const char *print_equals;
239 char *equals = strchr(colon, '=');
246 vpn_progress(vpninfo, PRG_ERR,
247 _("Invalid cookie offered: %s\n"), buf);
252 print_equals = equals;
253 /* Don't print the webvpn cookie unless it's empty; we don't
254 want people posting it in public with debugging output */
255 if (!strcmp(colon, "webvpn") && *equals)
256 print_equals = _("<elided>");
257 vpn_progress(vpninfo, PRG_TRACE, "%s: %s=%s%s%s\n",
258 buf, colon, print_equals, semicolon?";":"",
259 semicolon?(semicolon+1):"");
261 /* The server tends to ask for the username and password as
262 usual, even if we've already failed because it didn't like
263 our cert. Thankfully it does give us this hint... */
264 if (!strcmp(colon, "ClientCertAuthFailed"))
265 vpn_progress(vpninfo, PRG_ERR,
266 _("SSL certificate authentication failed\n"));
268 ret = http_add_cookie(vpninfo, colon, equals);
272 vpn_progress(vpninfo, PRG_TRACE, "%s: %s\n", buf, colon);
275 if (!strcasecmp(buf, "Connection")) {
276 if (!strcasecmp(colon, "Close"))
279 /* This might seem reasonable, but in fact it breaks
280 certificate authentication with some servers. If
281 they give an HTTP/1.0 response, even if they
282 explicitly give a Connection: Keep-Alive header,
283 just close the connection. */
284 else if (!strcasecmp(colon, "Keep-Alive"))
288 if (!strcasecmp(buf, "Location")) {
289 vpninfo->redirect_url = strdup(colon);
290 if (!vpninfo->redirect_url)
293 if (!strcasecmp(buf, "Content-Length")) {
294 bodylen = atoi(colon);
296 vpn_progress(vpninfo, PRG_ERR,
297 _("Response body has negative size (%d)\n"),
302 if (!strcasecmp(buf, "Transfer-Encoding")) {
303 if (!strcasecmp(colon, "chunked"))
304 bodylen = BODY_CHUNKED;
306 vpn_progress(vpninfo, PRG_ERR,
307 _("Unknown Transfer-Encoding: %s\n"),
312 if (header_cb && !strncmp(buf, "X-", 2))
313 header_cb(vpninfo, buf, colon);
316 /* Handle 'HTTP/1.1 100 Continue'. Not that we should ever see it */
320 /* Now the body, if there is one */
321 vpn_progress(vpninfo, PRG_TRACE, _("HTTP body %s (%d)\n"),
322 bodylen==BODY_HTTP10?"http 1.0" :
323 bodylen==BODY_CHUNKED?"chunked" : "length: ",
326 /* If we were given Content-Length, it's nice and easy... */
328 body = malloc(bodylen + 1);
331 while (done < bodylen) {
332 i = openconnect_SSL_read(vpninfo, body + done, bodylen - done);
334 vpn_progress(vpninfo, PRG_ERR,
335 _("Error reading HTTP response body\n"));
341 } else if (bodylen == BODY_CHUNKED) {
342 /* ... else, chunked */
343 while ((i = openconnect_SSL_gets(vpninfo, buf, sizeof(buf)))) {
344 int chunklen, lastchunk = 0;
347 vpn_progress(vpninfo, PRG_ERR,
348 _("Error fetching chunk header\n"));
351 chunklen = strtol(buf, NULL, 16);
356 body = realloc(body, done + chunklen + 1);
360 i = openconnect_SSL_read(vpninfo, body + done, chunklen);
362 vpn_progress(vpninfo, PRG_ERR,
363 _("Error reading HTTP response body\n"));
371 if ((i = openconnect_SSL_gets(vpninfo, buf, sizeof(buf)))) {
373 vpn_progress(vpninfo, PRG_ERR,
374 _("Error fetching HTTP response body\n"));
376 vpn_progress(vpninfo, PRG_ERR,
377 _("Error in chunked decoding. Expected '', got: '%s'"),
387 } else if (bodylen == BODY_HTTP10) {
389 vpn_progress(vpninfo, PRG_ERR,
390 _("Cannot receive HTTP 1.0 body without closing connection\n"));
394 /* HTTP 1.0 response. Just eat all we can in 16KiB chunks */
396 body = realloc(body, done + 16384);
399 i = openconnect_SSL_read(vpninfo, body + done, 16384);
408 /* Connection closed. Reduce allocation to just what we need */
409 body = realloc(body, done + 1);
417 if (closeconn || vpninfo->no_http_keepalive)
418 openconnect_close_https(vpninfo, 0);
426 static void add_common_headers(struct openconnect_info *vpninfo, struct oc_text_buf *buf)
428 struct vpn_option *opt;
430 buf_append(buf, "Host: %s\r\n", vpninfo->hostname);
431 buf_append(buf, "User-Agent: %s\r\n", vpninfo->useragent);
432 buf_append(buf, "Accept: */*\r\n");
433 buf_append(buf, "Accept-Encoding: identity\r\n");
435 if (vpninfo->cookies) {
436 buf_append(buf, "Cookie: ");
437 for (opt = vpninfo->cookies; opt; opt = opt->next)
438 buf_append(buf, "%s=%s%s", opt->option,
439 opt->value, opt->next ? "; " : "\r\n");
441 buf_append(buf, "X-Transcend-Version: 1\r\n");
442 buf_append(buf, "X-Aggregate-Auth: 1\r\n");
443 buf_append(buf, "X-AnyConnect-Platform: %s\r\n", vpninfo->platname);
446 static int fetch_config(struct openconnect_info *vpninfo, char *fu, char *bu,
449 struct oc_text_buf *buf;
450 char *config_buf = NULL;
452 unsigned char local_sha1_bin[SHA1_SIZE];
453 char local_sha1_ascii[(SHA1_SIZE * 2)+1];
456 if (openconnect_open_https(vpninfo)) {
457 vpn_progress(vpninfo, PRG_ERR,
458 _("Failed to open HTTPS connection to %s\n"),
464 buf_append(buf, "GET %s%s HTTP/1.1\r\n", fu, bu);
465 add_common_headers(vpninfo, buf);
466 buf_append(buf, "\r\n");
469 return buf_free(buf);
471 if (openconnect_SSL_write(vpninfo, buf->data, buf->pos) != buf->pos) {
472 vpn_progress(vpninfo, PRG_ERR,
473 _("Failed to send GET request for new config\n"));
479 buflen = process_http_response(vpninfo, &result, NULL, &config_buf);
481 /* We'll already have complained about whatever offended us */
490 openconnect_sha1(local_sha1_bin, config_buf, buflen);
492 for (i = 0; i < SHA1_SIZE; i++)
493 sprintf(&local_sha1_ascii[i*2], "%02x", local_sha1_bin[i]);
495 if (strcasecmp(server_sha1, local_sha1_ascii)) {
496 vpn_progress(vpninfo, PRG_ERR,
497 _("Downloaded config file did not match intended SHA1\n"));
502 result = vpninfo->write_new_config(vpninfo->cbdata, config_buf, buflen);
507 static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int buflen)
512 if (!vpninfo->uid_csd_given && !vpninfo->csd_wrapper) {
513 vpn_progress(vpninfo, PRG_ERR,
514 _("Error: Server asked us to download and run a 'Cisco Secure Desktop' trojan.\n"
515 "This facility is disabled by default for security reasons, so you may wish to enable it."));
520 vpn_progress(vpninfo, PRG_INFO,
521 _("Trying to run Linux CSD trojan script."));
524 sprintf(fname, "/tmp/csdXXXXXX");
528 vpn_progress(vpninfo, PRG_ERR,
529 _("Failed to open temporary CSD script file: %s\n"),
534 ret = proxy_write(vpninfo, fd, (void *)buf, buflen);
536 vpn_progress(vpninfo, PRG_ERR,
537 _("Failed to write temporary CSD script file: %s\n"),
545 char scertbuf[MD5_SIZE * 2 + 1];
546 char ccertbuf[MD5_SIZE * 2 + 1];
550 if (vpninfo->uid_csd_given && vpninfo->uid_csd != getuid()) {
553 if (setuid(vpninfo->uid_csd)) {
554 fprintf(stderr, _("Failed to set uid %ld\n"),
555 (long)vpninfo->uid_csd);
558 if (!(pw = getpwuid(vpninfo->uid_csd))) {
559 fprintf(stderr, _("Invalid user uid=%ld\n"),
560 (long)vpninfo->uid_csd);
563 setenv("HOME", pw->pw_dir, 1);
564 if (chdir(pw->pw_dir)) {
565 fprintf(stderr, _("Failed to change to CSD home directory '%s': %s\n"),
566 pw->pw_dir, strerror(errno));
570 if (getuid() == 0 && !vpninfo->csd_wrapper) {
571 fprintf(stderr, _("Warning: you are running insecure "
572 "CSD code with root privileges\n"
573 "\t Use command line option \"--csd-user\"\n"));
575 if (vpninfo->uid_csd_given == 2) {
576 /* The NM tool really needs not to get spurious output
577 on stdout, which the CSD trojan spews. */
580 if (vpninfo->csd_wrapper)
581 csd_argv[i++] = vpninfo->csd_wrapper;
582 csd_argv[i++] = fname;
583 csd_argv[i++]= (char *)"-ticket";
584 if (asprintf(&csd_argv[i++], "\"%s\"", vpninfo->csd_ticket) == -1)
586 csd_argv[i++]= (char *)"-stub";
587 csd_argv[i++]= (char *)"\"0\"";
588 csd_argv[i++]= (char *)"-group";
589 if (asprintf(&csd_argv[i++], "\"%s\"", vpninfo->authgroup?:"") == -1)
592 openconnect_local_cert_md5(vpninfo, ccertbuf);
594 get_cert_md5_fingerprint(vpninfo, vpninfo->peer_cert, scertbuf);
595 csd_argv[i++]= (char *)"-certhash";
596 if (asprintf(&csd_argv[i++], "\"%s:%s\"", scertbuf, ccertbuf) == -1)
599 csd_argv[i++]= (char *)"-url";
600 if (asprintf(&csd_argv[i++], "\"https://%s%s\"", vpninfo->hostname, vpninfo->csd_starturl) == -1)
603 csd_argv[i++]= (char *)"-langselen";
604 csd_argv[i++] = NULL;
606 if (setenv("CSD_TOKEN", vpninfo->csd_token, 1))
608 if (setenv("CSD_HOSTNAME", vpninfo->hostname, 1))
611 execv(csd_argv[0], csd_argv);
614 vpn_progress(vpninfo, PRG_ERR,
615 _("Failed to exec CSD script %s\n"), csd_argv[0]);
619 free(vpninfo->csd_stuburl);
620 vpninfo->csd_stuburl = NULL;
621 vpninfo->urlpath = strdup(vpninfo->csd_waiturl +
622 (vpninfo->csd_waiturl[0] == '/' ? 1 : 0));
623 vpninfo->csd_waiturl = NULL;
624 vpninfo->csd_scriptname = strdup(fname);
626 http_add_cookie(vpninfo, "sdesktop", vpninfo->csd_token);
631 int internal_parse_url(char *url, char **res_proto, char **res_host,
632 int *res_port, char **res_path, int default_port)
635 char *host, *path, *port_str;
638 host = strstr(url, "://");
643 if (!strcasecmp(proto, "https"))
645 else if (!strcasecmp(proto, "http"))
647 else if (!strcasecmp(proto, "socks") ||
648 !strcasecmp(proto, "socks4") ||
649 !strcasecmp(proto, "socks5"))
652 return -EPROTONOSUPPORT;
662 path = strchr(host, '/');
666 port_str = strrchr(host, ':');
669 int new_port = strtol(port_str + 1, &end, 10);
678 *res_proto = proto ? strdup(proto) : NULL;
680 *res_host = strdup(host);
684 *res_path = (path && *path) ? strdup(path) : NULL;
686 /* Undo the damage we did to the original string */
696 static void clear_cookies(struct openconnect_info *vpninfo)
698 struct vpn_option *opt, *next;
700 for (opt = vpninfo->cookies; opt; opt = next) {
707 vpninfo->cookies = NULL;
712 * = 0, on success (go ahead and retry with the latest vpninfo->{hostname,urlpath,port,...})
714 static int handle_redirect(struct openconnect_info *vpninfo)
716 vpninfo->redirect_type = REDIR_TYPE_LOCAL;
718 if (!strncmp(vpninfo->redirect_url, "https://", 8)) {
719 /* New host. Tear down the existing connection and make a new one */
724 free(vpninfo->urlpath);
725 vpninfo->urlpath = NULL;
727 ret = internal_parse_url(vpninfo->redirect_url, NULL, &host, &port, &vpninfo->urlpath, 0);
729 vpn_progress(vpninfo, PRG_ERR,
730 _("Failed to parse redirected URL '%s': %s\n"),
731 vpninfo->redirect_url, strerror(-ret));
732 free(vpninfo->redirect_url);
733 vpninfo->redirect_url = NULL;
737 if (strcasecmp(vpninfo->hostname, host) || port != vpninfo->port) {
738 free(vpninfo->hostname);
739 vpninfo->hostname = host;
740 vpninfo->port = port;
742 /* Kill the existing connection, and a new one will happen */
743 free(vpninfo->peer_addr);
744 vpninfo->peer_addr = NULL;
745 openconnect_close_https(vpninfo, 0);
746 clear_cookies(vpninfo);
747 vpninfo->redirect_type = REDIR_TYPE_NEWHOST;
751 free(vpninfo->redirect_url);
752 vpninfo->redirect_url = NULL;
755 } else if (strstr(vpninfo->redirect_url, "://")) {
756 vpn_progress(vpninfo, PRG_ERR,
757 _("Cannot follow redirection to non-https URL '%s'\n"),
758 vpninfo->redirect_url);
759 free(vpninfo->redirect_url);
760 vpninfo->redirect_url = NULL;
762 } else if (vpninfo->redirect_url[0] == '/') {
763 /* Absolute redirect within same host */
764 free(vpninfo->urlpath);
765 vpninfo->urlpath = strdup(vpninfo->redirect_url + 1);
766 free(vpninfo->redirect_url);
767 vpninfo->redirect_url = NULL;
770 char *lastslash = NULL;
771 if (vpninfo->urlpath)
772 lastslash = strrchr(vpninfo->urlpath, '/');
774 free(vpninfo->urlpath);
775 vpninfo->urlpath = vpninfo->redirect_url;
776 vpninfo->redirect_url = NULL;
778 char *oldurl = vpninfo->urlpath;
780 vpninfo->urlpath = NULL;
781 if (asprintf(&vpninfo->urlpath, "%s/%s",
782 oldurl, vpninfo->redirect_url) == -1) {
784 vpn_progress(vpninfo, PRG_ERR,
785 _("Allocating new path for relative redirect failed: %s\n"),
790 free(vpninfo->redirect_url);
791 vpninfo->redirect_url = NULL;
798 * method: GET or POST
799 * vpninfo->hostname: Host DNS name
800 * vpninfo->port: TCP port, typically 443
801 * vpninfo->urlpath: Relative path, e.g. /+webvpn+/foo.html
802 * request_body_type: Content type for a POST (e.g. text/html). Can be NULL.
803 * request_body: POST content
804 * form_buf: Callee-allocated buffer for server content
808 * >=0, on success, indicating the length of the data in *form_buf
810 static int do_https_request(struct openconnect_info *vpninfo, const char *method,
811 const char *request_body_type, const char *request_body,
812 char **form_buf, int fetch_redirect)
814 struct oc_text_buf *buf;
818 vpninfo->redirect_type = REDIR_TYPE_NONE;
824 if (openconnect_open_https(vpninfo)) {
825 vpn_progress(vpninfo, PRG_ERR,
826 _("Failed to open HTTPS connection to %s\n"),
832 * It would be nice to use cURL for this, but we really need to guarantee
833 * that we'll be using OpenSSL (for the TPM stuff), and it doesn't seem
834 * to have any way to let us provide our own socket read/write functions.
835 * We can only provide a socket _open_ function. Which would require having
836 * a socketpair() and servicing the "other" end of it.
838 * So we process the HTTP for ourselves...
841 buf_append(buf, "%s /%s HTTP/1.1\r\n", method, vpninfo->urlpath ?: "");
842 add_common_headers(vpninfo, buf);
844 if (request_body_type) {
845 buf_append(buf, "Content-Type: %s\r\n", request_body_type);
846 buf_append(buf, "Content-Length: %zd\r\n", strlen(request_body));
848 buf_append(buf, "\r\n");
850 if (request_body_type)
851 buf_append(buf, "%s", request_body);
853 if (vpninfo->port == 443)
854 vpn_progress(vpninfo, PRG_INFO, "%s https://%s/%s\n",
855 method, vpninfo->hostname,
856 vpninfo->urlpath ?: "");
858 vpn_progress(vpninfo, PRG_INFO, "%s https://%s:%d/%s\n",
859 method, vpninfo->hostname, vpninfo->port,
860 vpninfo->urlpath ?: "");
863 return buf_free(buf);
865 result = openconnect_SSL_write(vpninfo, buf->data, buf->pos);
870 buflen = process_http_response(vpninfo, &result, NULL, form_buf);
872 /* We'll already have complained about whatever offended us */
876 if (result != 200 && vpninfo->redirect_url) {
877 result = handle_redirect(vpninfo);
885 if (!*form_buf || result != 200) {
886 vpn_progress(vpninfo, PRG_ERR,
887 _("Unexpected %d result from server\n"),
902 * < 0, if the data is unrecognized
903 * = 0, if the page contains an XML document
904 * = 1, if the page is a wait/refresh HTML page
906 static int check_response_type(struct openconnect_info *vpninfo, char *form_buf)
908 if (strncmp(form_buf, "<?xml", 5)) {
909 /* Not XML? Perhaps it's HTML with a refresh... */
910 if (strcasestr(form_buf, "http-equiv=\"refresh\""))
912 vpn_progress(vpninfo, PRG_ERR,
913 _("Unknown response from server\n"));
921 * > 0, no cookie (user cancel)
922 * = 0, obtained cookie
924 int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
926 struct vpn_option *opt;
927 char *form_buf = NULL;
928 struct oc_auth_form *form = NULL;
929 int result, buflen, tries;
930 char request_body[2048];
931 const char *request_body_type = "application/x-www-form-urlencoded";
932 const char *method = "POST";
935 /* Step 1: Unlock software token (if applicable) */
936 if (vpninfo->use_stoken) {
937 result = prepare_stoken(vpninfo);
943 * Step 2: Probe for XML POST compatibility
945 * This can get stuck in a redirect loop, so give up after any of:
947 * a) HTTP error (e.g. 400 Bad Request)
948 * b) Same-host redirect (e.g. Location: /foo/bar)
949 * c) Three redirects without seeing a plausible login form
951 result = xmlpost_initial_req(vpninfo, request_body, sizeof(request_body));
955 for (tries = 0; ; tries++) {
958 buflen = do_https_request(vpninfo, method, request_body_type, request_body,
960 if (buflen == -EINVAL)
965 if (vpninfo->redirect_type == REDIR_TYPE_LOCAL)
967 else if (vpninfo->redirect_type != REDIR_TYPE_NONE)
970 result = parse_xml_response(vpninfo, form_buf, &form);
975 vpn_progress(vpninfo, PRG_INFO, _("XML POST enabled\n"));
979 /* Step 3: Fetch and parse the login form, if not using XML POST */
981 buflen = do_https_request(vpninfo, "GET", NULL, NULL, &form_buf, 0);
985 result = parse_xml_response(vpninfo, form_buf, &form);
992 /* Step 4: Run the CSD trojan, if applicable */
993 if (vpninfo->csd_starturl) {
994 char *form_path = NULL;
996 if (vpninfo->urlpath) {
997 form_path = strdup(vpninfo->urlpath);
1004 /* fetch the CSD program, if available */
1005 if (vpninfo->csd_stuburl) {
1006 buflen = do_https_request(vpninfo, "GET", NULL, NULL, &form_buf, 0);
1013 /* This is the CSD stub script, which we now need to run */
1014 result = run_csd_script(vpninfo, form_buf, buflen);
1018 /* vpninfo->urlpath now points to the wait page */
1020 result = do_https_request(vpninfo, "GET", NULL, NULL, &form_buf, 0);
1024 result = check_response_type(vpninfo, form_buf);
1028 vpn_progress(vpninfo, PRG_INFO,
1029 _("Refreshing %s after 1 second...\n"),
1036 /* refresh the form page, to see if we're authorized now */
1037 free(vpninfo->urlpath);
1038 vpninfo->urlpath = form_path;
1040 result = do_https_request(vpninfo, xmlpost ? "POST" : "GET",
1041 request_body_type, request_body, &form_buf, 1);
1045 result = parse_xml_response(vpninfo, form_buf, &form);
1050 /* Step 5: Ask the user to fill in the auth form; repeat as necessary */
1052 request_body[0] = 0;
1053 result = handle_auth_form(vpninfo, form, request_body, sizeof(request_body),
1054 &method, &request_body_type, xmlpost);
1055 if (result < 0 || result == 1)
1060 result = do_https_request(vpninfo, method, request_body_type, request_body,
1065 result = parse_xml_response(vpninfo, form_buf, &form);
1070 /* A return value of 2 means the XML form indicated
1071 success. We _should_ have a cookie... */
1073 for (opt = vpninfo->cookies; opt; opt = opt->next) {
1075 if (!strcmp(opt->option, "webvpn"))
1076 vpninfo->cookie = opt->value;
1077 else if (vpninfo->write_new_config && !strcmp(opt->option, "webvpnc")) {
1078 char *tok = opt->value;
1079 char *bu = NULL, *fu = NULL, *sha = NULL;
1082 if (tok != opt->value)
1085 if (!strncmp(tok, "bu:", 3))
1087 else if (!strncmp(tok, "fu:", 3))
1089 else if (!strncmp(tok, "fh:", 3)) {
1090 if (!strncasecmp(tok+3, vpninfo->xmlsha1,
1095 } while ((tok = strchr(tok, '&')));
1097 if (bu && fu && sha)
1098 fetch_config(vpninfo, bu, fu, sha);
1101 if (vpninfo->csd_scriptname) {
1102 unlink(vpninfo->csd_scriptname);
1103 free(vpninfo->csd_scriptname);
1104 vpninfo->csd_scriptname = NULL;
1110 free_auth_form(form);
1114 char *openconnect_create_useragent(const char *base)
1118 if (asprintf(&uagent, "%s %s", base, openconnect_version_str) < 0)
1124 static int proxy_gets(struct openconnect_info *vpninfo, int fd,
1125 char *buf, size_t len)
1133 while ( (ret = proxy_read(vpninfo, fd, (void *)(buf + i), 1)) == 0) {
1134 if (buf[i] == '\n') {
1136 if (i && buf[i-1] == '\r') {
1153 static int proxy_write(struct openconnect_info *vpninfo, int fd,
1154 unsigned char *buf, size_t len)
1158 for (count = 0; count < len; ) {
1159 fd_set rd_set, wr_set;
1165 FD_SET(fd, &wr_set);
1166 if (vpninfo->cancel_fd != -1) {
1167 FD_SET(vpninfo->cancel_fd, &rd_set);
1168 if (vpninfo->cancel_fd > fd)
1169 maxfd = vpninfo->cancel_fd;
1172 select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
1173 if (vpninfo->cancel_fd != -1 &&
1174 FD_ISSET(vpninfo->cancel_fd, &rd_set))
1177 /* Not that this should ever be able to happen... */
1178 if (!FD_ISSET(fd, &wr_set))
1181 i = write(fd, buf + count, len - count);
1190 static int proxy_read(struct openconnect_info *vpninfo, int fd,
1191 unsigned char *buf, size_t len)
1195 for (count = 0; count < len; ) {
1201 FD_SET(fd, &rd_set);
1202 if (vpninfo->cancel_fd != -1) {
1203 FD_SET(vpninfo->cancel_fd, &rd_set);
1204 if (vpninfo->cancel_fd > fd)
1205 maxfd = vpninfo->cancel_fd;
1208 select(maxfd + 1, &rd_set, NULL, NULL, NULL);
1209 if (vpninfo->cancel_fd != -1 &&
1210 FD_ISSET(vpninfo->cancel_fd, &rd_set))
1213 /* Not that this should ever be able to happen... */
1214 if (!FD_ISSET(fd, &rd_set))
1217 i = read(fd, buf + count, len - count);
1226 static const char *socks_errors[] = {
1227 N_("request granted"),
1228 N_("general failure"),
1229 N_("connection not allowed by ruleset"),
1230 N_("network unreachable"),
1231 N_("host unreachable"),
1232 N_("connection refused by destination host"),
1234 N_("command not supported / protocol error"),
1235 N_("address type not supported")
1238 static int process_socks_proxy(struct openconnect_info *vpninfo, int ssl_sock)
1240 unsigned char buf[1024];
1243 buf[0] = 5; /* SOCKS version */
1244 buf[1] = 1; /* # auth methods */
1245 buf[2] = 0; /* No auth supported */
1247 if ((i = proxy_write(vpninfo, ssl_sock, buf, 3))) {
1248 vpn_progress(vpninfo, PRG_ERR,
1249 _("Error writing auth request to SOCKS proxy: %s\n"),
1254 if ((i = proxy_read(vpninfo, ssl_sock, buf, 2))) {
1255 vpn_progress(vpninfo, PRG_ERR,
1256 _("Error reading auth response from SOCKS proxy: %s\n"),
1261 vpn_progress(vpninfo, PRG_ERR,
1262 _("Unexpected auth response from SOCKS proxy: %02x %02x\n"),
1268 if (buf[1] < sizeof(socks_errors) / sizeof(socks_errors[0]))
1269 vpn_progress(vpninfo, PRG_ERR,
1270 _("SOCKS proxy error %02x: %s\n"),
1271 buf[1], _(socks_errors[buf[1]]));
1273 vpn_progress(vpninfo, PRG_ERR,
1274 _("SOCKS proxy error %02x\n"),
1279 vpn_progress(vpninfo, PRG_INFO,
1280 _("Requesting SOCKS proxy connection to %s:%d\n"),
1281 vpninfo->hostname, vpninfo->port);
1283 buf[0] = 5; /* SOCKS version */
1284 buf[1] = 1; /* CONNECT */
1285 buf[2] = 0; /* Reserved */
1286 buf[3] = 3; /* Address type is domain name */
1287 buf[4] = strlen(vpninfo->hostname);
1288 strcpy((char *)buf + 5, vpninfo->hostname);
1289 i = strlen(vpninfo->hostname) + 5;
1290 buf[i++] = vpninfo->port >> 8;
1291 buf[i++] = vpninfo->port & 0xff;
1293 if ((i = proxy_write(vpninfo, ssl_sock, buf, i))) {
1294 vpn_progress(vpninfo, PRG_ERR,
1295 _("Error writing connect request to SOCKS proxy: %s\n"),
1299 /* Read 5 bytes -- up to and including the first byte of the returned
1300 address (which might be the length byte of a domain name) */
1301 if ((i = proxy_read(vpninfo, ssl_sock, buf, 5))) {
1302 vpn_progress(vpninfo, PRG_ERR,
1303 _("Error reading connect response from SOCKS proxy: %s\n"),
1308 vpn_progress(vpninfo, PRG_ERR,
1309 _("Unexpected connect response from SOCKS proxy: %02x %02x...\n"),
1316 /* Connect responses contain an address */
1318 case 1: /* Legacy IP */
1321 case 3: /* Domain name */
1328 vpn_progress(vpninfo, PRG_ERR,
1329 _("Unexpected address type %02x in SOCKS connect response\n"),
1334 if ((i = proxy_read(vpninfo, ssl_sock, buf, i))) {
1335 vpn_progress(vpninfo, PRG_ERR,
1336 _("Error reading connect response from SOCKS proxy: %s\n"),
1343 static int process_http_proxy(struct openconnect_info *vpninfo, int ssl_sock)
1345 char buf[MAX_BUF_LEN];
1346 struct oc_text_buf *reqbuf;
1349 reqbuf = buf_alloc();
1350 buf_append(reqbuf, "CONNECT %s:%d HTTP/1.1\r\n", vpninfo->hostname, vpninfo->port);
1351 buf_append(reqbuf, "Host: %s\r\n", vpninfo->hostname);
1352 buf_append(reqbuf, "User-Agent: %s\r\n", vpninfo->useragent);
1353 buf_append(reqbuf, "Proxy-Connection: keep-alive\r\n");
1354 buf_append(reqbuf, "Connection: keep-alive\r\n");
1355 buf_append(reqbuf, "Accept-Encoding: identity\r\n");
1356 buf_append(reqbuf, "\r\n");
1358 if (buf_error(reqbuf))
1359 return buf_free(reqbuf);
1361 vpn_progress(vpninfo, PRG_INFO,
1362 _("Requesting HTTP proxy connection to %s:%d\n"),
1363 vpninfo->hostname, vpninfo->port);
1365 result = proxy_write(vpninfo, ssl_sock, (unsigned char *)reqbuf->data, reqbuf->pos);
1369 vpn_progress(vpninfo, PRG_ERR,
1370 _("Sending proxy request failed: %s\n"),
1375 if (proxy_gets(vpninfo, ssl_sock, buf, sizeof(buf)) < 0) {
1376 vpn_progress(vpninfo, PRG_ERR,
1377 _("Error fetching proxy response\n"));
1381 if (strncmp(buf, "HTTP/1.", 7) || (buf[7] != '0' && buf[7] != '1') ||
1382 buf[8] != ' ' || !(result = atoi(buf+9))) {
1383 vpn_progress(vpninfo, PRG_ERR,
1384 _("Failed to parse proxy response '%s'\n"), buf);
1388 if (result != 200) {
1389 vpn_progress(vpninfo, PRG_ERR,
1390 _("Proxy CONNECT request failed: %s\n"), buf);
1394 while ((buflen = proxy_gets(vpninfo, ssl_sock, buf, sizeof(buf)))) {
1396 vpn_progress(vpninfo, PRG_ERR,
1397 _("Failed to read proxy response\n"));
1400 vpn_progress(vpninfo, PRG_ERR,
1401 _("Unexpected continuation line after CONNECT response: '%s'\n"),
1408 int process_proxy(struct openconnect_info *vpninfo, int ssl_sock)
1410 if (!vpninfo->proxy_type || !strcmp(vpninfo->proxy_type, "http"))
1411 return process_http_proxy(vpninfo, ssl_sock);
1413 if (!strcmp(vpninfo->proxy_type, "socks") ||
1414 !strcmp(vpninfo->proxy_type, "socks5"))
1415 return process_socks_proxy(vpninfo, ssl_sock);
1417 vpn_progress(vpninfo, PRG_ERR, _("Unknown proxy type '%s'\n"),
1418 vpninfo->proxy_type);
1422 int openconnect_set_http_proxy(struct openconnect_info *vpninfo, char *proxy)
1430 free(vpninfo->proxy_type);
1431 vpninfo->proxy_type = NULL;
1432 free(vpninfo->proxy);
1433 vpninfo->proxy = NULL;
1435 ret = internal_parse_url(url, &vpninfo->proxy_type, &vpninfo->proxy,
1436 &vpninfo->proxy_port, NULL, 80);
1440 if (vpninfo->proxy_type &&
1441 strcmp(vpninfo->proxy_type, "http") &&
1442 strcmp(vpninfo->proxy_type, "socks") &&
1443 strcmp(vpninfo->proxy_type, "socks5")) {
1444 vpn_progress(vpninfo, PRG_ERR,
1445 _("Only http or socks(5) proxies supported\n"));
1446 free(vpninfo->proxy_type);
1447 vpninfo->proxy_type = NULL;
1448 free(vpninfo->proxy);
1449 vpninfo->proxy = NULL;