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, PRG_TRACE, "Got HTTP response: %s\n", buf);
130 while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
133 vpninfo->progress(vpninfo, PRG_TRACE, "%s\n", buf);
136 vpninfo->progress(vpninfo, PRG_ERR, "Error processing HTTP response\n");
139 colon = strchr(buf, ':');
141 vpninfo->progress(vpninfo, PRG_ERR, "Ignoring unknown HTTP response line '%s'\n", buf);
148 if (!strcasecmp(buf, "Connection")) {
149 if (!strcasecmp(colon, "Close"))
152 /* This might seem reasonable, but in fact it breaks
153 certificate authentication with some servers. If
154 they give an HTTP/1.0 response, even if they
155 explicitly give a Connection: Keep-Alive header,
156 just close the connection. */
157 else if (!strcasecmp(colon, "Keep-Alive"))
161 if (!strcasecmp(buf, "Location")) {
162 vpninfo->redirect_url = strdup(colon);
163 if (!vpninfo->redirect_url)
166 if (!strcasecmp(buf, "Content-Length")) {
167 bodylen = atoi(colon);
169 vpninfo->progress(vpninfo, PRG_ERR, "Response body has negative size (%d)\n",
174 if (!strcasecmp(buf, "Set-Cookie")) {
175 char *semicolon = strchr(colon, ';');
176 char *equals = strchr(colon, '=');
183 vpninfo->progress(vpninfo, PRG_ERR, "Invalid cookie offered: %s\n", buf);
188 ret = http_add_cookie(vpninfo, colon, equals);
192 if (!strcasecmp(buf, "Transfer-Encoding")) {
193 if (!strcasecmp(colon, "chunked"))
194 bodylen = BODY_CHUNKED;
196 vpninfo->progress(vpninfo, PRG_ERR, "Unknown Transfer-Encoding: %s\n", colon);
200 if (header_cb && !strncmp(buf, "X-", 2))
201 header_cb(vpninfo, buf, colon);
204 /* Handle 'HTTP/1.1 100 Continue'. Not that we should ever see it */
208 /* Now the body, if there is one */
209 vpninfo->progress(vpninfo, PRG_TRACE, "HTTP body %s (%d)\n",
210 bodylen==BODY_HTTP10?"http 1.0" :
211 bodylen==BODY_CHUNKED?"chunked" : "length: ",
214 /* If we were given Content-Length, it's nice and easy... */
216 body = malloc(bodylen + 1);
219 while (done < bodylen) {
220 i = SSL_read(vpninfo->https_ssl, body + done, bodylen - done);
222 vpninfo->progress(vpninfo, PRG_ERR, "Error reading HTTP response body\n");
228 } else if (bodylen == BODY_CHUNKED) {
229 /* ... else, chunked */
230 while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
231 int chunklen, lastchunk = 0;
234 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching chunk header\n");
237 chunklen = strtol(buf, NULL, 16);
242 body = realloc(body, done + chunklen + 1);
246 i = SSL_read(vpninfo->https_ssl, body + done, chunklen);
248 vpninfo->progress(vpninfo, PRG_ERR, "Error reading HTTP response body\n");
256 if ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
258 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching HTTP response body\n");
260 vpninfo->progress(vpninfo, PRG_ERR, "Error in chunked decoding. Expected '', got: '%s'",
270 } else if (bodylen == BODY_HTTP10) {
272 vpninfo->progress(vpninfo, PRG_ERR, "Cannot receive HTTP 1.0 body without closing connection\n");
276 /* HTTP 1.0 response. Just eat all we can in 16KiB chunks */
278 body = realloc(body, done + 16384);
281 i = SSL_read(vpninfo->https_ssl, body + done, 16384);
283 body = realloc(body, done + 1);
293 SSL_free(vpninfo->https_ssl);
294 vpninfo->https_ssl = NULL;
295 close(vpninfo->ssl_fd);
296 vpninfo->ssl_fd = -1;
305 static int fetch_config(struct openconnect_info *vpninfo, char *fu, char *bu,
308 struct vpn_option *opt;
309 char buf[MAX_BUF_LEN];
310 char *config_buf = NULL;
312 unsigned char local_sha1_bin[SHA_DIGEST_LENGTH];
313 char local_sha1_ascii[(SHA_DIGEST_LENGTH * 2)+1];
317 sprintf(buf, "GET %s%s HTTP/1.1\r\n", fu, bu);
318 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
319 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
320 sprintf(buf + strlen(buf), "Accept: */*\r\n");
321 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
323 if (vpninfo->cookies) {
324 sprintf(buf + strlen(buf), "Cookie: ");
325 for (opt = vpninfo->cookies; opt; opt = opt->next)
326 sprintf(buf + strlen(buf), "%s=%s%s", opt->option,
327 opt->value, opt->next ? "; " : "\r\n");
329 sprintf(buf + strlen(buf), "X-Transcend-Version: 1\r\n\r\n");
331 SSL_write(vpninfo->https_ssl, buf, strlen(buf));
333 buflen = process_http_response(vpninfo, &result, NULL, &config_buf);
335 /* We'll already have complained about whatever offended us */
345 EVP_Digest(config_buf, buflen, local_sha1_bin, NULL, EVP_sha1(), NULL);
346 EVP_MD_CTX_cleanup(&c);
348 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
349 sprintf(&local_sha1_ascii[i*2], "%02x", local_sha1_bin[i]);
351 if (strcasecmp(server_sha1, local_sha1_ascii)) {
352 vpninfo->progress(vpninfo, PRG_ERR, "Downloaded config file did not match intended SHA1\n");
357 result = vpninfo->write_new_config(vpninfo, config_buf, buflen);
362 static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int buflen)
367 if (!vpninfo->uid_csd_given) {
368 vpninfo->progress(vpninfo, PRG_ERR, "Error: You are trying to "
369 "run insecure CSD code without specifying the CSD user.\n"
370 " Use command line option \"--csd-user\"\n");
375 vpninfo->progress(vpninfo, PRG_INFO,
376 "Trying to run Linux CSD trojan script.");
379 sprintf(fname, "/tmp/csdXXXXXX");
383 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open temporary CSD script file: %s\n",
388 ret = proxy_write(fd, (void *)buf, buflen);
390 vpninfo->progress(vpninfo, PRG_ERR, "Failed to write temporary CSD script file: %s\n",
398 X509 *scert = SSL_get_peer_certificate(vpninfo->https_ssl);
399 X509 *ccert = SSL_get_certificate(vpninfo->https_ssl);
400 char scertbuf[EVP_MAX_MD_SIZE * 2 + 1];
401 char ccertbuf[EVP_MAX_MD_SIZE * 2 + 1];
405 if (vpninfo->uid_csd != getuid()) {
408 if (setuid(vpninfo->uid_csd)) {
409 fprintf(stderr, "Failed to set uid %d\n",
413 if (!(pw = getpwuid(vpninfo->uid_csd))) {
414 fprintf(stderr, "Invalid user uid=%d\n",
418 setenv("HOME", pw->pw_dir, 1);
419 if (chdir(pw->pw_dir)) {
420 fprintf(stderr, "Failed to change to CSD home directory '%s': %s\n",
421 pw->pw_dir, strerror(errno));
425 if (vpninfo->uid_csd == 0) {
426 fprintf(stderr, "Warning: you are running insecure "
427 "CSD code with root privileges\n"
428 "\t Use command line option \"--csd-user\"\n");
431 csd_argv[i++] = fname;
432 csd_argv[i++] = "-ticket";
433 if (asprintf(&csd_argv[i++], "\"%s\"", vpninfo->csd_ticket) == -1)
435 csd_argv[i++] = "-stub";
436 csd_argv[i++] = "\"0\"";
437 csd_argv[i++] = "-group";
438 if (asprintf(&csd_argv[i++], "\"%s\"", vpninfo->authgroup?:"") == -1)
441 get_cert_md5_fingerprint(vpninfo, scert, scertbuf);
443 get_cert_md5_fingerprint(vpninfo, ccert, ccertbuf);
447 csd_argv[i++] = "-certhash";
448 if (asprintf(&csd_argv[i++], "\"%s:%s\"", scertbuf, ccertbuf) == -1)
450 csd_argv[i++] = "-url";
451 if (asprintf(&csd_argv[i++], "\"https://%s%s\"", vpninfo->hostname, vpninfo->csd_starturl) == -1)
453 /* WTF would it want to know this for? */
454 csd_argv[i++] = "-vpnclient";
455 csd_argv[i++] = "\"/opt/cisco/vpn/bin/vpnui";
456 csd_argv[i++] = "-connect";
457 if (asprintf(&csd_argv[i++], "https://%s/%s", vpninfo->hostname, vpninfo->csd_preurl) == -1)
459 csd_argv[i++] = "-connectparam";
460 if (asprintf(&csd_argv[i++], "#csdtoken=%s\"", vpninfo->csd_token) == -1)
462 csd_argv[i++] = "-langselen";
463 csd_argv[i++] = NULL;
465 execv(fname, csd_argv);
466 vpninfo->progress(vpninfo, PRG_ERR, "Failed to exec CSD script %s\n", fname);
470 free(vpninfo->csd_stuburl);
471 vpninfo->csd_stuburl = NULL;
472 vpninfo->urlpath = strdup(vpninfo->csd_waiturl +
473 (vpninfo->csd_waiturl[0] == '/' ? 1 : 0));
474 vpninfo->csd_waiturl = NULL;
475 vpninfo->csd_scriptname = strdup(fname);
477 http_add_cookie(vpninfo, "sdesktop", vpninfo->csd_token);
483 char *local_strcasestr(const char *haystack, const char *needle)
485 int hlen = strlen(haystack);
486 int nlen = strlen(needle);
489 for (i = 0; i < hlen - nlen + 1; i++) {
490 for (j = 0; j < nlen; j++) {
491 if (tolower(haystack[i + j]) !=
496 return (char *)haystack + i;
500 #define strcasestr local_strcasestr
503 int parse_url(char *url, char **res_proto, char **res_host, int *res_port,
504 char **res_path, int default_port)
507 char *host, *path, *port_str;
510 host = strstr(url, "://");
515 if (!strcasecmp(proto, "https"))
517 else if (!strcasecmp(proto, "http"))
519 else if (!strcasecmp(proto, "socks") ||
520 !strcasecmp(proto, "socks4") ||
521 !strcasecmp(proto, "socks5"))
524 return -EPROTONOSUPPORT;
534 path = strchr(host, '/');
541 port_str = strrchr(host, ':');
544 int new_port = strtol(port_str + 1, &end, 10);
553 *res_proto = proto ? strdup(proto) : NULL;
555 *res_host = strdup(host);
559 *res_path = path ? strdup(path) : NULL;
565 * = 0, no cookie (user cancel)
566 * = 1, obtained cookie
568 int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
570 struct vpn_option *opt, *next;
571 char buf[MAX_BUF_LEN];
572 char *form_buf = NULL;
574 char request_body[2048];
575 char *request_body_type = NULL;
576 char *method = "GET";
579 if (!vpninfo->https_ssl && openconnect_open_https(vpninfo)) {
580 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open HTTPS connection to %s\n",
586 * It would be nice to use cURL for this, but we really need to guarantee
587 * that we'll be using OpenSSL (for the TPM stuff), and it doesn't seem
588 * to have any way to let us provide our own socket read/write functions.
589 * We can only provide a socket _open_ function. Which would require having
590 * a socketpair() and servicing the "other" end of it.
592 * So we process the HTTP for ourselves...
594 sprintf(buf, "%s /%s HTTP/1.1\r\n", method, vpninfo->urlpath ?: "");
595 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
596 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
597 sprintf(buf + strlen(buf), "Accept: */*\r\n");
598 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
600 if (vpninfo->cookies) {
601 sprintf(buf + strlen(buf), "Cookie: ");
602 for (opt = vpninfo->cookies; opt; opt = opt->next)
603 sprintf(buf + strlen(buf), "%s=%s%s", opt->option,
604 opt->value, opt->next ? "; " : "\r\n");
606 if (request_body_type) {
607 sprintf(buf + strlen(buf), "Content-Type: %s\r\n",
609 sprintf(buf + strlen(buf), "Content-Length: %zd\r\n",
610 strlen(request_body));
612 sprintf(buf + strlen(buf), "X-Transcend-Version: 1\r\n\r\n");
613 if (request_body_type)
614 sprintf(buf + strlen(buf), "%s", request_body);
616 if (vpninfo->port == 443)
617 vpninfo->progress(vpninfo, PRG_INFO, "%s https://%s/%s\n",
618 method, vpninfo->hostname,
619 vpninfo->urlpath ?: "");
621 vpninfo->progress(vpninfo, PRG_INFO, "%s https://%s:%d/%s\n",
622 method, vpninfo->hostname, vpninfo->port,
623 vpninfo->urlpath ?: "");
625 SSL_write(vpninfo->https_ssl, buf, strlen(buf));
627 buflen = process_http_response(vpninfo, &result, NULL, &form_buf);
629 /* We'll already have complained about whatever offended us */
633 if (result != 200 && vpninfo->redirect_url) {
635 if (!strncmp(vpninfo->redirect_url, "https://", 8)) {
636 /* New host. Tear down the existing connection and make a new one */
641 free(vpninfo->urlpath);
642 vpninfo->urlpath = NULL;
644 ret = parse_url(vpninfo->redirect_url, NULL, &host, &port, &vpninfo->urlpath, 0);
646 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse redirected URL '%s': %s\n",
647 vpninfo->redirect_url, strerror(-ret));
648 free(vpninfo->redirect_url);
653 if (strcasecmp(vpninfo->hostname, host) || port != vpninfo->port) {
654 free(vpninfo->hostname);
655 vpninfo->hostname = host;
656 vpninfo->port = port;
658 /* Kill the existing connection, and a new one will happen */
659 free(vpninfo->peer_addr);
660 vpninfo->peer_addr = NULL;
661 if (vpninfo->https_ssl) {
662 SSL_free(vpninfo->https_ssl);
663 vpninfo->https_ssl = NULL;
664 close(vpninfo->ssl_fd);
665 vpninfo->ssl_fd = -1;
668 for (opt = vpninfo->cookies; opt; opt = next) {
675 vpninfo->cookies = NULL;
679 free(vpninfo->redirect_url);
680 vpninfo->redirect_url = NULL;
683 } else if (vpninfo->redirect_url[0] == '/') {
684 /* Absolute redirect within same host */
685 free(vpninfo->urlpath);
686 vpninfo->urlpath = strdup(vpninfo->redirect_url + 1);
687 free(vpninfo->redirect_url);
688 vpninfo->redirect_url = NULL;
691 char *lastslash = strrchr(vpninfo->urlpath, '/');
693 free(vpninfo->urlpath);
694 vpninfo->urlpath = vpninfo->redirect_url;
695 vpninfo->redirect_url = NULL;
697 char *oldurl = vpninfo->urlpath;
699 vpninfo->urlpath = NULL;
700 if (asprintf(&vpninfo->urlpath, "%s/%s",
701 oldurl, vpninfo->redirect_url) == -1) {
703 vpninfo->progress(vpninfo, PRG_ERR,
704 "Allocating new path for relative redirect failed: %s\n",
709 free(vpninfo->redirect_url);
710 vpninfo->redirect_url = NULL;
716 if (vpninfo->csd_stuburl) {
717 /* This is the CSD stub script, which we now need to run */
718 result = run_csd_script(vpninfo, form_buf, buflen);
724 /* Now we'll be redirected to the waiturl */
727 if (strncmp(form_buf, "<?xml", 5)) {
728 /* Not XML? Perhaps it's HTML with a refresh... */
729 if (strcasestr(form_buf, "http-equiv=\"refresh\"")) {
730 vpninfo->progress(vpninfo, PRG_INFO, "Refreshing %s after 1 second...\n",
735 vpninfo->progress(vpninfo, PRG_ERR, "Unknown response from server\n");
740 result = parse_xml_response(vpninfo, form_buf, request_body, sizeof(request_body),
741 &method, &request_body_type);
751 /* A return value of 2 means the XML form indicated
752 success. We _should_ have a cookie... */
754 for (opt = vpninfo->cookies; opt; opt = opt->next) {
756 if (!strcmp(opt->option, "webvpn"))
757 vpninfo->cookie = opt->value;
758 else if (vpninfo->write_new_config && !strcmp(opt->option, "webvpnc")) {
759 char *tok = opt->value;
760 char *bu = NULL, *fu = NULL, *sha = NULL;
763 if (tok != opt->value)
766 if (!strncmp(tok, "bu:", 3))
768 else if (!strncmp(tok, "fu:", 3))
770 else if (!strncmp(tok, "fh:", 3)) {
771 if (!strncasecmp(tok+3, vpninfo->xmlsha1,
772 SHA_DIGEST_LENGTH * 2))
776 } while ((tok = strchr(tok, '&')));
779 fetch_config(vpninfo, bu, fu, sha);
782 if (vpninfo->csd_scriptname) {
783 unlink(vpninfo->csd_scriptname);
784 free(vpninfo->csd_scriptname);
785 vpninfo->csd_scriptname = NULL;
790 char *openconnect_create_useragent(char *base)
794 if (asprintf(&uagent, "%s %s", base, openconnect_version) < 0)
800 static int proxy_gets(int fd, char *buf, size_t len)
808 while ( (ret = read(fd, buf + i, 1)) == 1) {
809 if (buf[i] == '\n') {
811 if (i && buf[i-1] == '\r') {
831 static int proxy_write(int fd, unsigned char *buf, size_t len)
835 for (count = 0; count < len; ) {
836 int i = write(fd, buf + count, len - count);
845 static int proxy_read(int fd, unsigned char *buf, size_t len)
849 for (count = 0; count < len; ) {
850 int i = read(fd, buf + count, len - count);
859 static const char *socks_errors[] = {
862 "connection not allowed by ruleset",
863 "network unreachable",
865 "connection refused by destination host",
867 "command not supported / protocol error",
868 "address type not supported"
871 static int process_socks_proxy(struct openconnect_info *vpninfo, int ssl_sock)
873 unsigned char buf[1024];
876 buf[0] = 5; /* SOCKS version */
877 buf[1] = 1; /* # auth methods */
878 buf[2] = 0; /* No auth supported */
880 if ((i = proxy_write(ssl_sock, buf, 3))) {
881 vpninfo->progress(vpninfo, PRG_ERR,
882 "Error writing auth request to SOCKS proxy: %s\n",
887 if ((i = proxy_read(ssl_sock, buf, 2))) {
888 vpninfo->progress(vpninfo, PRG_ERR,
889 "Error reading auth response from SOCKS proxy: %s\n",
894 vpninfo->progress(vpninfo, PRG_ERR,
895 "Unexpected auth response from SOCKS proxy: %02x %02x\n",
901 if (buf[1] < sizeof(socks_errors) / sizeof(socks_errors[0]))
902 vpninfo->progress(vpninfo, PRG_ERR,
903 "SOCKS proxy error %02x: %s\n",
904 buf[1], socks_errors[buf[1]]);
906 vpninfo->progress(vpninfo, PRG_ERR,
907 "SOCKS proxy error %02x\n",
912 vpninfo->progress(vpninfo, PRG_INFO, "Requesting SOCKS proxy connection to %s:%d\n",
913 vpninfo->hostname, vpninfo->port);
915 buf[0] = 5; /* SOCKS version */
916 buf[1] = 1; /* CONNECT */
917 buf[2] = 0; /* Reserved */
918 buf[3] = 3; /* Address type is domain name */
919 buf[4] = strlen(vpninfo->hostname);
920 strcpy((char *)buf + 5, vpninfo->hostname);
921 i = strlen(vpninfo->hostname) + 5;
922 buf[i++] = vpninfo->port >> 8;
923 buf[i++] = vpninfo->port & 0xff;
925 if ((i = proxy_write(ssl_sock, buf, i))) {
926 vpninfo->progress(vpninfo, PRG_ERR,
927 "Error writing connect request to SOCKS proxy: %s\n",
931 /* Read 5 bytes -- up to and including the first byte of the returned
932 address (which might be the length byte of a domain name) */
933 if ((i = proxy_read(ssl_sock, buf, 5))) {
934 vpninfo->progress(vpninfo, PRG_ERR,
935 "Error reading connect response from SOCKS proxy: %s\n",
940 vpninfo->progress(vpninfo, PRG_ERR,
941 "Unexpected connect response from SOCKS proxy: %02x %02x...\n",
948 /* Connect responses contain an address */
950 case 1: /* Legacy IP */
953 case 3: /* Domain name */
960 vpninfo->progress(vpninfo, PRG_ERR,
961 "Unexpected address type %02x in SOCKS connect response\n",
966 if ((i = proxy_read(ssl_sock, buf, i))) {
967 vpninfo->progress(vpninfo, PRG_ERR,
968 "Error reading connect response from SOCKS proxy: %s\n",
975 static int process_http_proxy(struct openconnect_info *vpninfo, int ssl_sock)
977 char buf[MAX_BUF_LEN];
980 sprintf(buf, "CONNECT %s:%d HTTP/1.1\r\n", vpninfo->hostname, vpninfo->port);
981 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
982 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
983 sprintf(buf + strlen(buf), "Proxy-Connection: keep-alive\r\n");
984 sprintf(buf + strlen(buf), "Connection: keep-alive\r\n");
985 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
986 sprintf(buf + strlen(buf), "\r\n");
988 vpninfo->progress(vpninfo, PRG_INFO, "Requesting HTTP proxy connection to %s:%d\n",
989 vpninfo->hostname, vpninfo->port);
991 if (proxy_write(ssl_sock, (unsigned char *)buf, strlen(buf))) {
993 vpninfo->progress(vpninfo, PRG_ERR, "Sending proxy request failed: %s\n",
998 if (proxy_gets(ssl_sock, buf, sizeof(buf)) < 0) {
999 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching proxy response\n");
1003 if (strncmp(buf, "HTTP/1.", 7) || (buf[7] != '0' && buf[7] != '1') ||
1004 buf[8] != ' ' || !(result = atoi(buf+9))) {
1005 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse proxy response '%s'\n",
1010 if (result != 200) {
1011 vpninfo->progress(vpninfo, PRG_ERR, "Proxy CONNECT request failed: %s\n",
1016 while ((buflen = proxy_gets(ssl_sock, buf, sizeof(buf)))) {
1018 vpninfo->progress(vpninfo, PRG_ERR, "Failed to read proxy response\n");
1021 vpninfo->progress(vpninfo, PRG_ERR,
1022 "Unexpected continuation line after CONNECT response: '%s'\n",
1029 int process_proxy(struct openconnect_info *vpninfo, int ssl_sock)
1031 if (!vpninfo->proxy_type || !strcmp(vpninfo->proxy_type, "http"))
1032 return process_http_proxy(vpninfo, ssl_sock);
1034 if (!strcmp(vpninfo->proxy_type, "socks") ||
1035 !strcmp(vpninfo->proxy_type, "socks5"))
1036 return process_socks_proxy(vpninfo, ssl_sock);
1038 vpninfo->progress(vpninfo, PRG_ERR, "Unknown proxy type '%s'\n",
1039 vpninfo->proxy_type);
1043 int set_http_proxy(struct openconnect_info *vpninfo, char *proxy)
1045 char *url = strdup(proxy);
1051 free(vpninfo->proxy_type);
1052 vpninfo->proxy_type = NULL;
1053 free(vpninfo->proxy);
1054 vpninfo->proxy = NULL;
1056 ret = parse_url(url, &vpninfo->proxy_type, &vpninfo->proxy,
1057 &vpninfo->proxy_port, NULL, 80);
1061 if (vpninfo->proxy_type &&
1062 strcmp(vpninfo->proxy_type, "http") &&
1063 strcmp(vpninfo->proxy_type, "socks") &&
1064 strcmp(vpninfo->proxy_type, "socks5")) {
1065 vpninfo->progress(vpninfo, PRG_ERR,
1066 "Only http or socks(5) proxies supported\n");
1067 free(vpninfo->proxy_type);
1068 vpninfo->proxy_type = NULL;
1069 free(vpninfo->proxy);
1070 vpninfo->proxy = NULL;