2 * OpenConnect (SSL + DTLS) VPN client
4 * Copyright © 2008-2010 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
35 #include <sys/types.h>
37 #include <openssl/ssl.h>
38 #include <openssl/err.h>
39 #include <openssl/engine.h>
41 #include "openconnect.h"
43 static int proxy_write(int fd, unsigned char *buf, size_t len);
45 #define MAX_BUF_LEN 131072
47 * We didn't really want to have to do this for ourselves -- one might have
48 * thought that it would be available in a library somewhere. But neither
49 * cURL nor Neon have reliable cross-platform ways of either using a cert
50 * from the TPM, or just reading from / writing to a transport which is
51 * provided by their caller.
54 int http_add_cookie(struct openconnect_info *vpninfo, const char *option, const char *value)
56 struct vpn_option *new, **this;
59 new = malloc(sizeof(*new));
61 vpninfo->progress(vpninfo, PRG_ERR, "No memory for allocating cookies\n");
65 new->option = strdup(option);
66 new->value = strdup(value);
67 if (!new->option || !new->value) {
74 /* Kill cookie; don't replace it */
77 for (this = &vpninfo->cookies; *this; this = &(*this)->next) {
78 if (!strcmp(option, (*this)->option)) {
79 /* Replace existing cookie */
81 new->next = (*this)->next;
85 free((*this)->option);
99 #define BODY_HTTP10 -1
100 #define BODY_CHUNKED -2
102 static int process_http_response(struct openconnect_info *vpninfo, int *result,
103 int (*header_cb)(struct openconnect_info *, char *, char *),
106 char buf[MAX_BUF_LEN];
108 int bodylen = BODY_HTTP10;
114 if (openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)) < 0) {
115 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching HTTPS response\n");
119 if (!strncmp(buf, "HTTP/1.0 ", 9))
122 if ((!closeconn && strncmp(buf, "HTTP/1.1 ", 9)) || !(*result = atoi(buf+9))) {
123 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse HTTP response '%s'\n", buf);
127 vpninfo->progress(vpninfo, (*result==200)?PRG_TRACE:PRG_INFO,
128 "Got HTTP response: %s\n", buf);
131 while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
134 vpninfo->progress(vpninfo, PRG_TRACE, "%s\n", buf);
137 vpninfo->progress(vpninfo, PRG_ERR, "Error processing HTTP response\n");
140 colon = strchr(buf, ':');
142 vpninfo->progress(vpninfo, PRG_ERR, "Ignoring unknown HTTP response line '%s'\n", buf);
149 if (!strcasecmp(buf, "Connection")) {
150 if (!strcasecmp(colon, "Close"))
153 /* This might seem reasonable, but in fact it breaks
154 certificate authentication with some servers. If
155 they give an HTTP/1.0 response, even if they
156 explicitly give a Connection: Keep-Alive header,
157 just close the connection. */
158 else if (!strcasecmp(colon, "Keep-Alive"))
162 if (!strcasecmp(buf, "Location")) {
163 vpninfo->redirect_url = strdup(colon);
164 if (!vpninfo->redirect_url)
167 if (!strcasecmp(buf, "Content-Length")) {
168 bodylen = atoi(colon);
170 vpninfo->progress(vpninfo, PRG_ERR, "Response body has negative size (%d)\n",
175 if (!strcasecmp(buf, "Set-Cookie")) {
176 char *semicolon = strchr(colon, ';');
177 char *equals = strchr(colon, '=');
184 vpninfo->progress(vpninfo, PRG_ERR, "Invalid cookie offered: %s\n", buf);
189 ret = http_add_cookie(vpninfo, colon, equals);
193 if (!strcasecmp(buf, "Transfer-Encoding")) {
194 if (!strcasecmp(colon, "chunked"))
195 bodylen = BODY_CHUNKED;
197 vpninfo->progress(vpninfo, PRG_ERR, "Unknown Transfer-Encoding: %s\n", colon);
201 if (header_cb && !strncmp(buf, "X-", 2))
202 header_cb(vpninfo, buf, colon);
205 /* Handle 'HTTP/1.1 100 Continue'. Not that we should ever see it */
209 /* Now the body, if there is one */
210 vpninfo->progress(vpninfo, PRG_TRACE, "HTTP body %s (%d)\n",
211 bodylen==BODY_HTTP10?"http 1.0" :
212 bodylen==BODY_CHUNKED?"chunked" : "length: ",
215 /* If we were given Content-Length, it's nice and easy... */
217 body = malloc(bodylen + 1);
220 while (done < bodylen) {
221 i = SSL_read(vpninfo->https_ssl, body + done, bodylen - done);
223 vpninfo->progress(vpninfo, PRG_ERR, "Error reading HTTP response body\n");
229 } else if (bodylen == BODY_CHUNKED) {
230 /* ... else, chunked */
231 while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
232 int chunklen, lastchunk = 0;
235 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching chunk header\n");
238 chunklen = strtol(buf, NULL, 16);
243 body = realloc(body, done + chunklen + 1);
247 i = SSL_read(vpninfo->https_ssl, body + done, chunklen);
249 vpninfo->progress(vpninfo, PRG_ERR, "Error reading HTTP response body\n");
257 if ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
259 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching HTTP response body\n");
261 vpninfo->progress(vpninfo, PRG_ERR, "Error in chunked decoding. Expected '', got: '%s'",
271 } else if (bodylen == BODY_HTTP10) {
273 vpninfo->progress(vpninfo, PRG_ERR, "Cannot receive HTTP 1.0 body without closing connection\n");
277 /* HTTP 1.0 response. Just eat all we can in 16KiB chunks */
279 body = realloc(body, done + 16384);
282 i = SSL_read(vpninfo->https_ssl, body + done, 16384);
284 body = realloc(body, done + 1);
294 SSL_free(vpninfo->https_ssl);
295 vpninfo->https_ssl = NULL;
296 close(vpninfo->ssl_fd);
297 vpninfo->ssl_fd = -1;
306 static int fetch_config(struct openconnect_info *vpninfo, char *fu, char *bu,
309 struct vpn_option *opt;
310 char buf[MAX_BUF_LEN];
311 char *config_buf = NULL;
313 unsigned char local_sha1_bin[SHA_DIGEST_LENGTH];
314 char local_sha1_ascii[(SHA_DIGEST_LENGTH * 2)+1];
318 sprintf(buf, "GET %s%s HTTP/1.1\r\n", fu, bu);
319 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
320 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
321 sprintf(buf + strlen(buf), "Accept: */*\r\n");
322 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
324 if (vpninfo->cookies) {
325 sprintf(buf + strlen(buf), "Cookie: ");
326 for (opt = vpninfo->cookies; opt; opt = opt->next)
327 sprintf(buf + strlen(buf), "%s=%s%s", opt->option,
328 opt->value, opt->next ? "; " : "\r\n");
330 sprintf(buf + strlen(buf), "X-Transcend-Version: 1\r\n\r\n");
332 SSL_write(vpninfo->https_ssl, buf, strlen(buf));
334 buflen = process_http_response(vpninfo, &result, NULL, &config_buf);
336 /* We'll already have complained about whatever offended us */
346 EVP_Digest(config_buf, buflen, local_sha1_bin, NULL, EVP_sha1(), NULL);
347 EVP_MD_CTX_cleanup(&c);
349 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
350 sprintf(&local_sha1_ascii[i*2], "%02x", local_sha1_bin[i]);
352 if (strcasecmp(server_sha1, local_sha1_ascii)) {
353 vpninfo->progress(vpninfo, PRG_ERR, "Downloaded config file did not match intended SHA1\n");
358 result = vpninfo->write_new_config(vpninfo, config_buf, buflen);
363 static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int buflen)
368 if (!vpninfo->uid_csd_given) {
369 vpninfo->progress(vpninfo, PRG_ERR,
370 "Error: Server asked us to download and run a 'Cisco Secure Desktop' trojan.\n"
371 "This facility is disabled by default for security reasons, so you may wish to enable it.");
376 vpninfo->progress(vpninfo, PRG_INFO,
377 "Trying to run Linux CSD trojan script.");
380 sprintf(fname, "/tmp/csdXXXXXX");
384 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open temporary CSD script file: %s\n",
389 ret = proxy_write(fd, (void *)buf, buflen);
391 vpninfo->progress(vpninfo, PRG_ERR, "Failed to write temporary CSD script file: %s\n",
399 X509 *scert = SSL_get_peer_certificate(vpninfo->https_ssl);
400 X509 *ccert = SSL_get_certificate(vpninfo->https_ssl);
401 char scertbuf[EVP_MAX_MD_SIZE * 2 + 1];
402 char ccertbuf[EVP_MAX_MD_SIZE * 2 + 1];
406 if (vpninfo->uid_csd != getuid()) {
409 if (setuid(vpninfo->uid_csd)) {
410 fprintf(stderr, "Failed to set uid %d\n",
414 if (!(pw = getpwuid(vpninfo->uid_csd))) {
415 fprintf(stderr, "Invalid user uid=%d\n",
419 setenv("HOME", pw->pw_dir, 1);
420 if (chdir(pw->pw_dir)) {
421 fprintf(stderr, "Failed to change to CSD home directory '%s': %s\n",
422 pw->pw_dir, strerror(errno));
426 if (vpninfo->uid_csd == 0) {
427 fprintf(stderr, "Warning: you are running insecure "
428 "CSD code with root privileges\n"
429 "\t Use command line option \"--csd-user\"\n");
431 if (vpninfo->uid_csd_given == 2) {
432 /* The NM tool really needs not to get spurious output
433 on stdout, which the CSD trojan spews. */
436 csd_argv[i++] = fname;
437 csd_argv[i++] = "-ticket";
438 if (asprintf(&csd_argv[i++], "\"%s\"", vpninfo->csd_ticket) == -1)
440 csd_argv[i++] = "-stub";
441 csd_argv[i++] = "\"0\"";
442 csd_argv[i++] = "-group";
443 if (asprintf(&csd_argv[i++], "\"%s\"", vpninfo->authgroup?:"") == -1)
446 get_cert_md5_fingerprint(vpninfo, scert, scertbuf);
448 get_cert_md5_fingerprint(vpninfo, ccert, ccertbuf);
452 csd_argv[i++] = "-certhash";
453 if (asprintf(&csd_argv[i++], "\"%s:%s\"", scertbuf, ccertbuf) == -1)
455 csd_argv[i++] = "-url";
456 if (asprintf(&csd_argv[i++], "\"https://%s%s\"", vpninfo->hostname, vpninfo->csd_starturl) == -1)
458 /* WTF would it want to know this for? */
459 csd_argv[i++] = "-vpnclient";
460 csd_argv[i++] = "\"/opt/cisco/vpn/bin/vpnui";
461 csd_argv[i++] = "-connect";
462 if (asprintf(&csd_argv[i++], "https://%s/%s", vpninfo->hostname, vpninfo->csd_preurl) == -1)
464 csd_argv[i++] = "-connectparam";
465 if (asprintf(&csd_argv[i++], "#csdtoken=%s\"", vpninfo->csd_token) == -1)
467 csd_argv[i++] = "-langselen";
468 csd_argv[i++] = NULL;
470 execv(fname, csd_argv);
471 vpninfo->progress(vpninfo, PRG_ERR, "Failed to exec CSD script %s\n", fname);
475 free(vpninfo->csd_stuburl);
476 vpninfo->csd_stuburl = NULL;
477 vpninfo->urlpath = strdup(vpninfo->csd_waiturl +
478 (vpninfo->csd_waiturl[0] == '/' ? 1 : 0));
479 vpninfo->csd_waiturl = NULL;
480 vpninfo->csd_scriptname = strdup(fname);
482 http_add_cookie(vpninfo, "sdesktop", vpninfo->csd_token);
488 char *local_strcasestr(const char *haystack, const char *needle)
490 int hlen = strlen(haystack);
491 int nlen = strlen(needle);
494 for (i = 0; i < hlen - nlen + 1; i++) {
495 for (j = 0; j < nlen; j++) {
496 if (tolower(haystack[i + j]) !=
501 return (char *)haystack + i;
505 #define strcasestr local_strcasestr
508 int parse_url(char *url, char **res_proto, char **res_host, int *res_port,
509 char **res_path, int default_port)
512 char *host, *path, *port_str;
515 host = strstr(url, "://");
520 if (!strcasecmp(proto, "https"))
522 else if (!strcasecmp(proto, "http"))
524 else if (!strcasecmp(proto, "socks") ||
525 !strcasecmp(proto, "socks4") ||
526 !strcasecmp(proto, "socks5"))
529 return -EPROTONOSUPPORT;
539 path = strchr(host, '/');
546 port_str = strrchr(host, ':');
549 int new_port = strtol(port_str + 1, &end, 10);
558 *res_proto = proto ? strdup(proto) : NULL;
560 *res_host = strdup(host);
564 *res_path = path ? strdup(path) : NULL;
570 * = 0, no cookie (user cancel)
571 * = 1, obtained cookie
573 int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
575 struct vpn_option *opt, *next;
576 char buf[MAX_BUF_LEN];
577 char *form_buf = NULL;
579 char request_body[2048];
580 char *request_body_type = NULL;
581 char *method = "GET";
584 if (!vpninfo->https_ssl && openconnect_open_https(vpninfo)) {
585 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open HTTPS connection to %s\n",
591 * It would be nice to use cURL for this, but we really need to guarantee
592 * that we'll be using OpenSSL (for the TPM stuff), and it doesn't seem
593 * to have any way to let us provide our own socket read/write functions.
594 * We can only provide a socket _open_ function. Which would require having
595 * a socketpair() and servicing the "other" end of it.
597 * So we process the HTTP for ourselves...
599 sprintf(buf, "%s /%s HTTP/1.1\r\n", method, vpninfo->urlpath ?: "");
600 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
601 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
602 sprintf(buf + strlen(buf), "Accept: */*\r\n");
603 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
605 if (vpninfo->cookies) {
606 sprintf(buf + strlen(buf), "Cookie: ");
607 for (opt = vpninfo->cookies; opt; opt = opt->next)
608 sprintf(buf + strlen(buf), "%s=%s%s", opt->option,
609 opt->value, opt->next ? "; " : "\r\n");
611 if (request_body_type) {
612 sprintf(buf + strlen(buf), "Content-Type: %s\r\n",
614 sprintf(buf + strlen(buf), "Content-Length: %zd\r\n",
615 strlen(request_body));
617 sprintf(buf + strlen(buf), "X-Transcend-Version: 1\r\n\r\n");
618 if (request_body_type)
619 sprintf(buf + strlen(buf), "%s", request_body);
621 if (vpninfo->port == 443)
622 vpninfo->progress(vpninfo, PRG_INFO, "%s https://%s/%s\n",
623 method, vpninfo->hostname,
624 vpninfo->urlpath ?: "");
626 vpninfo->progress(vpninfo, PRG_INFO, "%s https://%s:%d/%s\n",
627 method, vpninfo->hostname, vpninfo->port,
628 vpninfo->urlpath ?: "");
630 SSL_write(vpninfo->https_ssl, buf, strlen(buf));
632 buflen = process_http_response(vpninfo, &result, NULL, &form_buf);
634 /* We'll already have complained about whatever offended us */
638 if (result != 200 && vpninfo->redirect_url) {
640 if (!strncmp(vpninfo->redirect_url, "https://", 8)) {
641 /* New host. Tear down the existing connection and make a new one */
646 free(vpninfo->urlpath);
647 vpninfo->urlpath = NULL;
649 ret = parse_url(vpninfo->redirect_url, NULL, &host, &port, &vpninfo->urlpath, 0);
651 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse redirected URL '%s': %s\n",
652 vpninfo->redirect_url, strerror(-ret));
653 free(vpninfo->redirect_url);
658 if (strcasecmp(vpninfo->hostname, host) || port != vpninfo->port) {
659 free(vpninfo->hostname);
660 vpninfo->hostname = host;
661 vpninfo->port = port;
663 /* Kill the existing connection, and a new one will happen */
664 free(vpninfo->peer_addr);
665 vpninfo->peer_addr = NULL;
666 if (vpninfo->https_ssl) {
667 SSL_free(vpninfo->https_ssl);
668 vpninfo->https_ssl = NULL;
669 close(vpninfo->ssl_fd);
670 vpninfo->ssl_fd = -1;
673 for (opt = vpninfo->cookies; opt; opt = next) {
680 vpninfo->cookies = NULL;
684 free(vpninfo->redirect_url);
685 vpninfo->redirect_url = NULL;
688 } else if (vpninfo->redirect_url[0] == '/') {
689 /* Absolute redirect within same host */
690 free(vpninfo->urlpath);
691 vpninfo->urlpath = strdup(vpninfo->redirect_url + 1);
692 free(vpninfo->redirect_url);
693 vpninfo->redirect_url = NULL;
696 char *lastslash = strrchr(vpninfo->urlpath, '/');
698 free(vpninfo->urlpath);
699 vpninfo->urlpath = vpninfo->redirect_url;
700 vpninfo->redirect_url = NULL;
702 char *oldurl = vpninfo->urlpath;
704 vpninfo->urlpath = NULL;
705 if (asprintf(&vpninfo->urlpath, "%s/%s",
706 oldurl, vpninfo->redirect_url) == -1) {
708 vpninfo->progress(vpninfo, PRG_ERR,
709 "Allocating new path for relative redirect failed: %s\n",
714 free(vpninfo->redirect_url);
715 vpninfo->redirect_url = NULL;
720 if (!form_buf || result != 200) {
721 vpninfo->progress(vpninfo, PRG_ERR,
722 "Unexpected %d result from server\n",
726 if (vpninfo->csd_stuburl) {
727 /* This is the CSD stub script, which we now need to run */
728 result = run_csd_script(vpninfo, form_buf, buflen);
734 /* Now we'll be redirected to the waiturl */
737 if (strncmp(form_buf, "<?xml", 5)) {
738 /* Not XML? Perhaps it's HTML with a refresh... */
739 if (strcasestr(form_buf, "http-equiv=\"refresh\"")) {
740 vpninfo->progress(vpninfo, PRG_INFO, "Refreshing %s after 1 second...\n",
745 vpninfo->progress(vpninfo, PRG_ERR, "Unknown response from server\n");
750 result = parse_xml_response(vpninfo, form_buf, request_body, sizeof(request_body),
751 &method, &request_body_type);
761 /* A return value of 2 means the XML form indicated
762 success. We _should_ have a cookie... */
764 for (opt = vpninfo->cookies; opt; opt = opt->next) {
766 if (!strcmp(opt->option, "webvpn"))
767 vpninfo->cookie = opt->value;
768 else if (vpninfo->write_new_config && !strcmp(opt->option, "webvpnc")) {
769 char *tok = opt->value;
770 char *bu = NULL, *fu = NULL, *sha = NULL;
773 if (tok != opt->value)
776 if (!strncmp(tok, "bu:", 3))
778 else if (!strncmp(tok, "fu:", 3))
780 else if (!strncmp(tok, "fh:", 3)) {
781 if (!strncasecmp(tok+3, vpninfo->xmlsha1,
782 SHA_DIGEST_LENGTH * 2))
786 } while ((tok = strchr(tok, '&')));
789 fetch_config(vpninfo, bu, fu, sha);
792 if (vpninfo->csd_scriptname) {
793 unlink(vpninfo->csd_scriptname);
794 free(vpninfo->csd_scriptname);
795 vpninfo->csd_scriptname = NULL;
800 char *openconnect_create_useragent(char *base)
804 if (asprintf(&uagent, "%s %s", base, openconnect_version) < 0)
810 static int proxy_gets(int fd, char *buf, size_t len)
818 while ( (ret = read(fd, buf + i, 1)) == 1) {
819 if (buf[i] == '\n') {
821 if (i && buf[i-1] == '\r') {
841 static int proxy_write(int fd, unsigned char *buf, size_t len)
845 for (count = 0; count < len; ) {
846 int i = write(fd, buf + count, len - count);
855 static int proxy_read(int fd, unsigned char *buf, size_t len)
859 for (count = 0; count < len; ) {
860 int i = read(fd, buf + count, len - count);
869 static const char *socks_errors[] = {
872 "connection not allowed by ruleset",
873 "network unreachable",
875 "connection refused by destination host",
877 "command not supported / protocol error",
878 "address type not supported"
881 static int process_socks_proxy(struct openconnect_info *vpninfo, int ssl_sock)
883 unsigned char buf[1024];
886 buf[0] = 5; /* SOCKS version */
887 buf[1] = 1; /* # auth methods */
888 buf[2] = 0; /* No auth supported */
890 if ((i = proxy_write(ssl_sock, buf, 3))) {
891 vpninfo->progress(vpninfo, PRG_ERR,
892 "Error writing auth request to SOCKS proxy: %s\n",
897 if ((i = proxy_read(ssl_sock, buf, 2))) {
898 vpninfo->progress(vpninfo, PRG_ERR,
899 "Error reading auth response from SOCKS proxy: %s\n",
904 vpninfo->progress(vpninfo, PRG_ERR,
905 "Unexpected auth response from SOCKS proxy: %02x %02x\n",
911 if (buf[1] < sizeof(socks_errors) / sizeof(socks_errors[0]))
912 vpninfo->progress(vpninfo, PRG_ERR,
913 "SOCKS proxy error %02x: %s\n",
914 buf[1], socks_errors[buf[1]]);
916 vpninfo->progress(vpninfo, PRG_ERR,
917 "SOCKS proxy error %02x\n",
922 vpninfo->progress(vpninfo, PRG_INFO, "Requesting SOCKS proxy connection to %s:%d\n",
923 vpninfo->hostname, vpninfo->port);
925 buf[0] = 5; /* SOCKS version */
926 buf[1] = 1; /* CONNECT */
927 buf[2] = 0; /* Reserved */
928 buf[3] = 3; /* Address type is domain name */
929 buf[4] = strlen(vpninfo->hostname);
930 strcpy((char *)buf + 5, vpninfo->hostname);
931 i = strlen(vpninfo->hostname) + 5;
932 buf[i++] = vpninfo->port >> 8;
933 buf[i++] = vpninfo->port & 0xff;
935 if ((i = proxy_write(ssl_sock, buf, i))) {
936 vpninfo->progress(vpninfo, PRG_ERR,
937 "Error writing connect request to SOCKS proxy: %s\n",
941 /* Read 5 bytes -- up to and including the first byte of the returned
942 address (which might be the length byte of a domain name) */
943 if ((i = proxy_read(ssl_sock, buf, 5))) {
944 vpninfo->progress(vpninfo, PRG_ERR,
945 "Error reading connect response from SOCKS proxy: %s\n",
950 vpninfo->progress(vpninfo, PRG_ERR,
951 "Unexpected connect response from SOCKS proxy: %02x %02x...\n",
958 /* Connect responses contain an address */
960 case 1: /* Legacy IP */
963 case 3: /* Domain name */
970 vpninfo->progress(vpninfo, PRG_ERR,
971 "Unexpected address type %02x in SOCKS connect response\n",
976 if ((i = proxy_read(ssl_sock, buf, i))) {
977 vpninfo->progress(vpninfo, PRG_ERR,
978 "Error reading connect response from SOCKS proxy: %s\n",
985 static int process_http_proxy(struct openconnect_info *vpninfo, int ssl_sock)
987 char buf[MAX_BUF_LEN];
990 sprintf(buf, "CONNECT %s:%d HTTP/1.1\r\n", vpninfo->hostname, vpninfo->port);
991 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
992 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
993 sprintf(buf + strlen(buf), "Proxy-Connection: keep-alive\r\n");
994 sprintf(buf + strlen(buf), "Connection: keep-alive\r\n");
995 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
996 sprintf(buf + strlen(buf), "\r\n");
998 vpninfo->progress(vpninfo, PRG_INFO, "Requesting HTTP proxy connection to %s:%d\n",
999 vpninfo->hostname, vpninfo->port);
1001 if (proxy_write(ssl_sock, (unsigned char *)buf, strlen(buf))) {
1003 vpninfo->progress(vpninfo, PRG_ERR, "Sending proxy request failed: %s\n",
1008 if (proxy_gets(ssl_sock, buf, sizeof(buf)) < 0) {
1009 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching proxy response\n");
1013 if (strncmp(buf, "HTTP/1.", 7) || (buf[7] != '0' && buf[7] != '1') ||
1014 buf[8] != ' ' || !(result = atoi(buf+9))) {
1015 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse proxy response '%s'\n",
1020 if (result != 200) {
1021 vpninfo->progress(vpninfo, PRG_ERR, "Proxy CONNECT request failed: %s\n",
1026 while ((buflen = proxy_gets(ssl_sock, buf, sizeof(buf)))) {
1028 vpninfo->progress(vpninfo, PRG_ERR, "Failed to read proxy response\n");
1031 vpninfo->progress(vpninfo, PRG_ERR,
1032 "Unexpected continuation line after CONNECT response: '%s'\n",
1039 int process_proxy(struct openconnect_info *vpninfo, int ssl_sock)
1041 if (!vpninfo->proxy_type || !strcmp(vpninfo->proxy_type, "http"))
1042 return process_http_proxy(vpninfo, ssl_sock);
1044 if (!strcmp(vpninfo->proxy_type, "socks") ||
1045 !strcmp(vpninfo->proxy_type, "socks5"))
1046 return process_socks_proxy(vpninfo, ssl_sock);
1048 vpninfo->progress(vpninfo, PRG_ERR, "Unknown proxy type '%s'\n",
1049 vpninfo->proxy_type);
1053 int set_http_proxy(struct openconnect_info *vpninfo, char *proxy)
1055 char *url = strdup(proxy);
1061 free(vpninfo->proxy_type);
1062 vpninfo->proxy_type = NULL;
1063 free(vpninfo->proxy);
1064 vpninfo->proxy = NULL;
1066 ret = parse_url(url, &vpninfo->proxy_type, &vpninfo->proxy,
1067 &vpninfo->proxy_port, NULL, 80);
1071 if (vpninfo->proxy_type &&
1072 strcmp(vpninfo->proxy_type, "http") &&
1073 strcmp(vpninfo->proxy_type, "socks") &&
1074 strcmp(vpninfo->proxy_type, "socks5")) {
1075 vpninfo->progress(vpninfo, PRG_ERR,
1076 "Only http or socks(5) proxies supported\n");
1077 free(vpninfo->proxy_type);
1078 vpninfo->proxy_type = NULL;
1079 free(vpninfo->proxy);
1080 vpninfo->proxy = NULL;