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 #define MAX_BUF_LEN 131072
45 * We didn't really want to have to do this for ourselves -- one might have
46 * thought that it would be available in a library somewhere. But neither
47 * cURL nor Neon have reliable cross-platform ways of either using a cert
48 * from the TPM, or just reading from / writing to a transport which is
49 * provided by their caller.
52 int http_add_cookie(struct openconnect_info *vpninfo, const char *option, const char *value)
54 struct vpn_option *new, **this;
57 new = malloc(sizeof(*new));
59 vpninfo->progress(vpninfo, PRG_ERR, "No memory for allocating cookies\n");
63 new->option = strdup(option);
64 new->value = strdup(value);
65 if (!new->option || !new->value) {
72 /* Kill cookie; don't replace it */
75 for (this = &vpninfo->cookies; *this; this = &(*this)->next) {
76 if (!strcmp(option, (*this)->option)) {
77 /* Replace existing cookie */
79 new->next = (*this)->next;
83 free((*this)->option);
97 #define BODY_HTTP10 -1
98 #define BODY_CHUNKED -2
100 static int process_http_response(struct openconnect_info *vpninfo, int *result,
101 int (*header_cb)(struct openconnect_info *, char *, char *),
104 char buf[MAX_BUF_LEN];
106 int bodylen = BODY_HTTP10;
112 if (openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)) < 0) {
113 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching HTTPS response\n");
117 if (!strncmp(buf, "HTTP/1.0 ", 9))
120 if ((!closeconn && strncmp(buf, "HTTP/1.1 ", 9)) || !(*result = atoi(buf+9))) {
121 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse HTTP response '%s'\n", buf);
125 vpninfo->progress(vpninfo, PRG_TRACE, "Got HTTP response: %s\n", buf);
128 while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
131 vpninfo->progress(vpninfo, PRG_TRACE, "%s\n", buf);
134 vpninfo->progress(vpninfo, PRG_ERR, "Error processing HTTP response\n");
137 colon = strchr(buf, ':');
139 vpninfo->progress(vpninfo, PRG_ERR, "Ignoring unknown HTTP response line '%s'\n", buf);
146 if (!strcasecmp(buf, "Connection")) {
147 if (!strcasecmp(colon, "Close"))
149 else if (!strcasecmp(colon, "Keep-Alive"))
152 if (!strcasecmp(buf, "Location")) {
153 vpninfo->redirect_url = strdup(colon);
154 if (!vpninfo->redirect_url)
157 if (!strcasecmp(buf, "Content-Length")) {
158 bodylen = atoi(colon);
160 vpninfo->progress(vpninfo, PRG_ERR, "Response body has negative size (%d)\n",
165 if (!strcasecmp(buf, "Set-Cookie")) {
166 char *semicolon = strchr(colon, ';');
167 char *equals = strchr(colon, '=');
174 vpninfo->progress(vpninfo, PRG_ERR, "Invalid cookie offered: %s\n", buf);
179 ret = http_add_cookie(vpninfo, colon, equals);
183 if (!strcasecmp(buf, "Transfer-Encoding")) {
184 if (!strcasecmp(colon, "chunked"))
185 bodylen = BODY_CHUNKED;
187 vpninfo->progress(vpninfo, PRG_ERR, "Unknown Transfer-Encoding: %s\n", colon);
191 if (header_cb && !strncmp(buf, "X-", 2))
192 header_cb(vpninfo, buf, colon);
195 /* Handle 'HTTP/1.1 100 Continue'. Not that we should ever see it */
199 /* Now the body, if there is one */
200 vpninfo->progress(vpninfo, PRG_TRACE, "HTTP body %s (%d)\n",
201 bodylen==BODY_HTTP10?"http 1.0" :
202 bodylen==BODY_CHUNKED?"chunked" : "length: ",
205 /* If we were given Content-Length, it's nice and easy... */
207 body = malloc(bodylen + 1);
210 while (done < bodylen) {
211 i = SSL_read(vpninfo->https_ssl, body + done, bodylen - done);
213 vpninfo->progress(vpninfo, PRG_ERR, "Error reading HTTP response body\n");
218 } else if (bodylen == BODY_CHUNKED) {
219 /* ... else, chunked */
220 while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
221 int chunklen, lastchunk = 0;
224 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching chunk header\n");
227 chunklen = strtol(buf, NULL, 16);
232 body = realloc(body, done + chunklen + 1);
236 i = SSL_read(vpninfo->https_ssl, body + done, chunklen);
238 vpninfo->progress(vpninfo, PRG_ERR, "Error reading HTTP response body\n");
245 if ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
247 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching HTTP response body\n");
249 vpninfo->progress(vpninfo, PRG_ERR, "Error in chunked decoding. Expected '', got: '%s'",
258 } else if (bodylen == BODY_HTTP10) {
260 vpninfo->progress(vpninfo, PRG_ERR, "Cannot receive HTTP 1.0 body without closing connection\n");
264 /* HTTP 1.0 response. Just eat all we can in 16KiB chunks */
266 body = realloc(body, done + 16385);
267 i = SSL_read(vpninfo->https_ssl, body + done, 16384);
275 SSL_free(vpninfo->https_ssl);
276 vpninfo->https_ssl = NULL;
277 close(vpninfo->ssl_fd);
278 vpninfo->ssl_fd = -1;
287 static int fetch_config(struct openconnect_info *vpninfo, char *fu, char *bu,
290 struct vpn_option *opt;
291 char buf[MAX_BUF_LEN];
292 char *config_buf = NULL;
294 unsigned char local_sha1_bin[SHA_DIGEST_LENGTH];
295 char local_sha1_ascii[(SHA_DIGEST_LENGTH * 2)+1];
299 sprintf(buf, "GET %s%s HTTP/1.1\r\n", fu, bu);
300 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
301 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
302 sprintf(buf + strlen(buf), "Accept: */*\r\n");
303 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
305 if (vpninfo->cookies) {
306 sprintf(buf + strlen(buf), "Cookie: ");
307 for (opt = vpninfo->cookies; opt; opt = opt->next)
308 sprintf(buf + strlen(buf), "%s=%s%s", opt->option,
309 opt->value, opt->next ? "; " : "\r\n");
311 sprintf(buf + strlen(buf), "X-Transcend-Version: 1\r\n\r\n");
313 SSL_write(vpninfo->https_ssl, buf, strlen(buf));
315 buflen = process_http_response(vpninfo, &result, NULL, &config_buf);
317 /* We'll already have complained about whatever offended us */
327 EVP_Digest(config_buf, buflen, local_sha1_bin, NULL, EVP_sha1(), NULL);
328 EVP_MD_CTX_cleanup(&c);
330 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
331 sprintf(&local_sha1_ascii[i*2], "%02x", local_sha1_bin[i]);
333 if (strcasecmp(server_sha1, local_sha1_ascii)) {
334 vpninfo->progress(vpninfo, PRG_ERR, "Downloaded config file did not match intended SHA1\n");
339 result = vpninfo->write_new_config(vpninfo, config_buf, buflen);
344 static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int buflen)
349 if (!vpninfo->uid_csd_given) {
350 vpninfo->progress(vpninfo, PRG_ERR, "Error: You are trying to "
351 "run insecure CSD code without specifying the CSD user.\n"
352 " Use command line option \"--csd-user\"\n");
357 vpninfo->progress(vpninfo, PRG_INFO,
358 "Trying to run Linux CSD trojan script.");
361 sprintf(fname, "/tmp/csdXXXXXX");
365 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open temporary CSD script file: %s\n",
369 write(fd, buf, buflen);
374 X509 *scert = SSL_get_peer_certificate(vpninfo->https_ssl);
375 X509 *ccert = SSL_get_certificate(vpninfo->https_ssl);
376 char scertbuf[EVP_MAX_MD_SIZE * 2 + 1];
377 char ccertbuf[EVP_MAX_MD_SIZE * 2 + 1];
381 if (vpninfo->uid_csd != getuid()) {
384 if (setuid(vpninfo->uid_csd)) {
385 fprintf(stderr, "Failed to set uid %d\n",
389 if (!(pw = getpwuid(vpninfo->uid_csd))) {
390 fprintf(stderr, "Invalid user uid=%d\n",
394 setenv("HOME", pw->pw_dir, 1);
395 if (chdir(pw->pw_dir)) {
396 fprintf(stderr, "Failed to change to CSD home directory '%s': %s\n",
397 pw->pw_dir, strerror(errno));
401 if (vpninfo->uid_csd == 0) {
402 fprintf(stderr, "Warning: you are running insecure "
403 "CSD code with root privileges\n"
404 "\t Use command line option \"--csd-user\"\n");
407 csd_argv[i++] = fname;
408 csd_argv[i++] = "-ticket";
409 if (asprintf(&csd_argv[i++], "\"%s\"", vpninfo->csd_ticket) == -1)
411 csd_argv[i++] = "-stub";
412 csd_argv[i++] = "\"0\"";
413 csd_argv[i++] = "-group";
414 if (asprintf(&csd_argv[i++], "\"%s\"", vpninfo->authgroup?:"") == -1)
417 get_cert_md5_fingerprint(vpninfo, scert, scertbuf);
419 get_cert_md5_fingerprint(vpninfo, ccert, ccertbuf);
423 csd_argv[i++] = "-certhash";
424 if (asprintf(&csd_argv[i++], "\"%s:%s\"", scertbuf, ccertbuf) == -1)
426 csd_argv[i++] = "-url";
427 if (asprintf(&csd_argv[i++], "\"https://%s%s\"", vpninfo->hostname, vpninfo->csd_starturl) == -1)
429 /* WTF would it want to know this for? */
430 csd_argv[i++] = "-vpnclient";
431 csd_argv[i++] = "\"/opt/cisco/vpn/bin/vpnui";
432 csd_argv[i++] = "-connect";
433 if (asprintf(&csd_argv[i++], "https://%s/%s", vpninfo->hostname, vpninfo->csd_preurl) == -1)
435 csd_argv[i++] = "-connectparam";
436 if (asprintf(&csd_argv[i++], "#csdtoken=%s\"", vpninfo->csd_token) == -1)
438 csd_argv[i++] = "-langselen";
439 csd_argv[i++] = NULL;
441 execv(fname, csd_argv);
442 vpninfo->progress(vpninfo, PRG_ERR, "Failed to exec CSD script %s\n", fname);
446 free(vpninfo->csd_stuburl);
447 vpninfo->csd_stuburl = NULL;
448 vpninfo->urlpath = strdup(vpninfo->csd_waiturl +
449 (vpninfo->csd_waiturl[0] == '/' ? 1 : 0));
450 vpninfo->csd_waiturl = NULL;
451 vpninfo->csd_scriptname = strdup(fname);
453 http_add_cookie(vpninfo, "sdesktop", vpninfo->csd_token);
459 char *local_strcasestr(const char *haystack, const char *needle)
461 int hlen = strlen(haystack);
462 int nlen = strlen(needle);
465 for (i = 0; i < hlen - nlen + 1; i++) {
466 for (j = 0; j < nlen; j++) {
467 if (tolower(haystack[i + j]) !=
472 return (char *)haystack + i;
476 #define strcasestr local_strcasestr
479 int parse_url(char *url, char **res_proto, char **res_host, int *res_port,
480 char **res_path, int default_port)
483 char *host, *path, *port_str;
486 host = strstr(url, "://");
491 if (!strcasecmp(proto, "https"))
493 else if (!strcasecmp(proto, "http"))
495 else if (!strcasecmp(proto, "socks") ||
496 !strcasecmp(proto, "socks4") ||
497 !strcasecmp(proto, "socks5"))
500 return -EPROTONOSUPPORT;
510 path = strchr(host, '/');
517 port_str = strrchr(host, ':');
520 int new_port = strtol(port_str + 1, &end, 10);
529 *res_proto = proto ? strdup(proto) : NULL;
531 *res_host = strdup(host);
535 *res_path = path ? strdup(path) : NULL;
541 * = 0, no cookie (user cancel)
542 * = 1, obtained cookie
544 int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
546 struct vpn_option *opt, *next;
547 char buf[MAX_BUF_LEN];
548 char *form_buf = NULL;
550 char request_body[2048];
551 char *request_body_type = NULL;
552 char *method = "GET";
555 if (!vpninfo->https_ssl && openconnect_open_https(vpninfo)) {
556 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open HTTPS connection to %s\n",
562 * It would be nice to use cURL for this, but we really need to guarantee
563 * that we'll be using OpenSSL (for the TPM stuff), and it doesn't seem
564 * to have any way to let us provide our own socket read/write functions.
565 * We can only provide a socket _open_ function. Which would require having
566 * a socketpair() and servicing the "other" end of it.
568 * So we process the HTTP for ourselves...
570 sprintf(buf, "%s /%s HTTP/1.1\r\n", method, vpninfo->urlpath ?: "");
571 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
572 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
573 sprintf(buf + strlen(buf), "Accept: */*\r\n");
574 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
576 if (vpninfo->cookies) {
577 sprintf(buf + strlen(buf), "Cookie: ");
578 for (opt = vpninfo->cookies; opt; opt = opt->next)
579 sprintf(buf + strlen(buf), "%s=%s%s", opt->option,
580 opt->value, opt->next ? "; " : "\r\n");
582 if (request_body_type) {
583 sprintf(buf + strlen(buf), "Content-Type: %s\r\n",
585 sprintf(buf + strlen(buf), "Content-Length: %zd\r\n",
586 strlen(request_body));
588 sprintf(buf + strlen(buf), "X-Transcend-Version: 1\r\n\r\n");
589 if (request_body_type)
590 sprintf(buf + strlen(buf), "%s", request_body);
592 if (vpninfo->port == 443)
593 vpninfo->progress(vpninfo, PRG_INFO, "%s https://%s/%s\n",
594 method, vpninfo->hostname,
595 vpninfo->urlpath ?: "");
597 vpninfo->progress(vpninfo, PRG_INFO, "%s https://%s:%d/%s\n",
598 method, vpninfo->hostname, vpninfo->port,
599 vpninfo->urlpath ?: "");
601 SSL_write(vpninfo->https_ssl, buf, strlen(buf));
603 buflen = process_http_response(vpninfo, &result, NULL, &form_buf);
605 /* We'll already have complained about whatever offended us */
609 if (result != 200 && vpninfo->redirect_url) {
611 if (!strncmp(vpninfo->redirect_url, "https://", 8)) {
612 /* New host. Tear down the existing connection and make a new one */
617 free(vpninfo->urlpath);
618 vpninfo->urlpath = NULL;
620 ret = parse_url(vpninfo->redirect_url, NULL, &host, &port, &vpninfo->urlpath, 0);
622 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse redirected URL '%s': %s\n",
623 vpninfo->redirect_url, strerror(-ret));
624 free(vpninfo->redirect_url);
629 if (strcasecmp(vpninfo->hostname, host) || port != vpninfo->port) {
630 free(vpninfo->hostname);
631 vpninfo->hostname = host;
632 vpninfo->port = port;
634 /* Kill the existing connection, and a new one will happen */
635 free(vpninfo->peer_addr);
636 vpninfo->peer_addr = NULL;
637 if (vpninfo->https_ssl) {
638 SSL_free(vpninfo->https_ssl);
639 vpninfo->https_ssl = NULL;
640 close(vpninfo->ssl_fd);
641 vpninfo->ssl_fd = -1;
644 for (opt = vpninfo->cookies; opt; opt = next) {
651 vpninfo->cookies = NULL;
655 free(vpninfo->redirect_url);
656 vpninfo->redirect_url = NULL;
659 } else if (vpninfo->redirect_url[0] == '/') {
660 /* Absolute redirect within same host */
661 free(vpninfo->urlpath);
662 vpninfo->urlpath = strdup(vpninfo->redirect_url + 1);
663 free(vpninfo->redirect_url);
664 vpninfo->redirect_url = NULL;
667 vpninfo->progress(vpninfo, PRG_ERR, "Relative redirect (to '%s') not supported\n",
668 vpninfo->redirect_url);
674 if (vpninfo->csd_stuburl) {
675 /* This is the CSD stub script, which we now need to run */
676 result = run_csd_script(vpninfo, form_buf, buflen);
682 /* Now we'll be redirected to the waiturl */
685 if (strncmp(form_buf, "<?xml", 5)) {
686 /* Not XML? Perhaps it's HTML with a refresh... */
687 if (strcasestr(form_buf, "http-equiv=\"refresh\"")) {
688 vpninfo->progress(vpninfo, PRG_INFO, "Refreshing %s after 1 second...\n",
693 vpninfo->progress(vpninfo, PRG_ERR, "Unknown response from server\n");
698 result = parse_xml_response(vpninfo, form_buf, request_body, sizeof(request_body),
699 &method, &request_body_type);
709 /* A return value of 2 means the XML form indicated
710 success. We _should_ have a cookie... */
712 for (opt = vpninfo->cookies; opt; opt = opt->next) {
714 if (!strcmp(opt->option, "webvpn"))
715 vpninfo->cookie = opt->value;
716 else if (vpninfo->write_new_config && !strcmp(opt->option, "webvpnc")) {
717 char *tok = opt->value;
718 char *bu = NULL, *fu = NULL, *sha = NULL;
721 if (tok != opt->value)
724 if (!strncmp(tok, "bu:", 3))
726 else if (!strncmp(tok, "fu:", 3))
728 else if (!strncmp(tok, "fh:", 3)) {
729 if (!strncasecmp(tok+3, vpninfo->xmlsha1,
730 SHA_DIGEST_LENGTH * 2))
734 } while ((tok = strchr(tok, '&')));
737 fetch_config(vpninfo, bu, fu, sha);
740 if (vpninfo->csd_scriptname) {
741 unlink(vpninfo->csd_scriptname);
742 free(vpninfo->csd_scriptname);
743 vpninfo->csd_scriptname = NULL;
748 char *openconnect_create_useragent(char *base)
752 if (asprintf(&uagent, "%s %s", base, openconnect_version) < 0)
758 static int proxy_gets(int fd, char *buf, size_t len)
766 while ( (ret = read(fd, buf + i, 1)) == 1) {
767 if (buf[i] == '\n') {
769 if (i && buf[i-1] == '\r') {
789 static int proxy_write(int fd, unsigned char *buf, size_t len)
793 for (count = 0; count < len; ) {
794 int i = write(fd, buf + count, len - count);
803 static int proxy_read(int fd, unsigned char *buf, size_t len)
807 for (count = 0; count < len; ) {
808 int i = read(fd, buf + count, len - count);
817 static const char *socks_errors[] = {
820 "connection not allowed by ruleset",
821 "network unreachable",
823 "connection refused by destination host",
825 "command not supported / protocol error",
826 "address type not supported"
829 static int process_socks_proxy(struct openconnect_info *vpninfo, int ssl_sock)
831 unsigned char buf[1024];
834 buf[0] = 5; /* SOCKS version */
835 buf[1] = 1; /* # auth methods */
836 buf[2] = 0; /* No auth supported */
838 if ((i = proxy_write(ssl_sock, buf, 3))) {
839 vpninfo->progress(vpninfo, PRG_ERR,
840 "Error writing auth request to SOCKS proxy: %s\n",
845 if ((i = proxy_read(ssl_sock, buf, 2))) {
846 vpninfo->progress(vpninfo, PRG_ERR,
847 "Error reading auth response from SOCKS proxy: %s\n",
852 vpninfo->progress(vpninfo, PRG_ERR,
853 "Unexpected auth response from SOCKS proxy: %02x %02x\n",
859 if (buf[1] < sizeof(socks_errors) / sizeof(socks_errors[0]))
860 vpninfo->progress(vpninfo, PRG_ERR,
861 "SOCKS proxy error %02x: %s\n",
862 buf[1], socks_errors[buf[1]]);
864 vpninfo->progress(vpninfo, PRG_ERR,
865 "SOCKS proxy error %02x\n",
870 vpninfo->progress(vpninfo, PRG_INFO, "Requesting SOCKS proxy connection to %s:%d\n",
871 vpninfo->hostname, vpninfo->port);
873 buf[0] = 5; /* SOCKS version */
874 buf[1] = 1; /* CONNECT */
875 buf[2] = 0; /* Reserved */
876 buf[3] = 3; /* Address type is domain name */
877 buf[4] = strlen(vpninfo->hostname);
878 strcpy((char *)buf + 5, vpninfo->hostname);
879 i = strlen(vpninfo->hostname) + 5;
880 buf[i++] = vpninfo->port >> 8;
881 buf[i++] = vpninfo->port & 0xff;
883 if ((i = proxy_write(ssl_sock, buf, i))) {
884 vpninfo->progress(vpninfo, PRG_ERR,
885 "Error writing connect request to SOCKS proxy: %s\n",
889 /* Read 5 bytes -- up to and including the first byte of the returned
890 address (which might be the length byte of a domain name) */
891 if ((i = proxy_read(ssl_sock, buf, 5))) {
892 vpninfo->progress(vpninfo, PRG_ERR,
893 "Error reading connect response from SOCKS proxy: %s\n",
898 vpninfo->progress(vpninfo, PRG_ERR,
899 "Unexpected connect response from SOCKS proxy: %02x %02x...\n",
906 /* Connect responses contain an address */
908 case 1: /* Legacy IP */
911 case 3: /* Domain name */
918 vpninfo->progress(vpninfo, PRG_ERR,
919 "Unexpected address type %02x in SOCKS connect response\n",
924 if ((i = proxy_read(ssl_sock, buf, i))) {
925 vpninfo->progress(vpninfo, PRG_ERR,
926 "Error reading connect response from SOCKS proxy: %s\n",
933 static int process_http_proxy(struct openconnect_info *vpninfo, int ssl_sock)
935 char buf[MAX_BUF_LEN];
938 sprintf(buf, "CONNECT %s:%d HTTP/1.1\r\n", vpninfo->hostname, vpninfo->port);
939 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
940 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
941 sprintf(buf + strlen(buf), "Proxy-Connection: keep-alive\r\n");
942 sprintf(buf + strlen(buf), "Connection: keep-alive\r\n");
943 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
944 sprintf(buf + strlen(buf), "\r\n");
946 vpninfo->progress(vpninfo, PRG_INFO, "Requesting HTTP proxy connection to %s:%d\n",
947 vpninfo->hostname, vpninfo->port);
949 if (proxy_write(ssl_sock, (unsigned char *)buf, strlen(buf))) {
951 vpninfo->progress(vpninfo, PRG_ERR, "Sending proxy request failed: %s\n",
956 if (proxy_gets(ssl_sock, buf, sizeof(buf)) < 0) {
957 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching proxy response\n");
961 if (strncmp(buf, "HTTP/1.", 7) || (buf[7] != '0' && buf[7] != '1') ||
962 buf[8] != ' ' || !(result = atoi(buf+9))) {
963 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse proxy response '%s'\n",
969 vpninfo->progress(vpninfo, PRG_ERR, "Proxy CONNECT request failed: %s\n",
974 while ((buflen = proxy_gets(ssl_sock, buf, sizeof(buf)))) {
976 vpninfo->progress(vpninfo, PRG_ERR, "Failed to read proxy response\n");
979 vpninfo->progress(vpninfo, PRG_ERR,
980 "Unexpected continuation line after CONNECT response: '%s'\n",
987 int process_proxy(struct openconnect_info *vpninfo, int ssl_sock)
989 if (!vpninfo->proxy_type || !strcmp(vpninfo->proxy_type, "http"))
990 return process_http_proxy(vpninfo, ssl_sock);
992 if (!strcmp(vpninfo->proxy_type, "socks") ||
993 !strcmp(vpninfo->proxy_type, "socks5"))
994 return process_socks_proxy(vpninfo, ssl_sock);
996 vpninfo->progress(vpninfo, PRG_ERR, "Unknown proxy type '%s'\n",
997 vpninfo->proxy_type);
1001 int set_http_proxy(struct openconnect_info *vpninfo, char *proxy)
1003 char *url = strdup(proxy);
1009 free(vpninfo->proxy_type);
1010 vpninfo->proxy_type = NULL;
1011 free(vpninfo->proxy);
1012 vpninfo->proxy = NULL;
1014 ret = parse_url(url, &vpninfo->proxy_type, &vpninfo->proxy,
1015 &vpninfo->proxy_port, NULL, 80);
1019 if (vpninfo->proxy_type &&
1020 strcmp(vpninfo->proxy_type, "http") &&
1021 strcmp(vpninfo->proxy_type, "socks") &&
1022 strcmp(vpninfo->proxy_type, "socks5")) {
1023 vpninfo->progress(vpninfo, PRG_ERR,
1024 "Only http or socks(5) proxies supported\n");
1025 free(vpninfo->proxy_type);
1026 vpninfo->proxy_type = NULL;
1027 free(vpninfo->proxy);
1028 vpninfo->proxy = NULL;