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 static int process_http_response(struct openconnect_info *vpninfo, int *result,
98 int (*header_cb)(struct openconnect_info *, char *, char *),
99 char *body, int buf_len)
101 char buf[MAX_BUF_LEN];
104 int http10 = 0, closeconn = 0;
108 if (openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)) < 0) {
109 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching HTTPS response\n");
113 if (!strncmp(buf, "HTTP/1.0 ", 9)) {
118 if ((!http10 && strncmp(buf, "HTTP/1.1 ", 9)) || !(*result = atoi(buf+9))) {
119 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse HTTP response '%s'\n", buf);
123 vpninfo->progress(vpninfo, PRG_TRACE, "Got HTTP response: %s\n", buf);
126 while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
129 vpninfo->progress(vpninfo, PRG_TRACE, "%s\n", buf);
132 vpninfo->progress(vpninfo, PRG_ERR, "Error processing HTTP response\n");
135 colon = strchr(buf, ':');
137 vpninfo->progress(vpninfo, PRG_ERR, "Ignoring unknown HTTP response line '%s'\n", buf);
144 if (!strcasecmp(buf, "Connection") && !strcasecmp(colon, "Close"))
147 if (!strcasecmp(buf, "Location")) {
148 vpninfo->redirect_url = strdup(colon);
149 if (!vpninfo->redirect_url)
152 if (!strcasecmp(buf, "Content-Length")) {
153 bodylen = atoi(colon);
154 if (bodylen < 0 || bodylen > buf_len) {
155 vpninfo->progress(vpninfo, PRG_ERR, "Response body too large for buffer (%d > %d)\n",
160 if (!strcasecmp(buf, "Set-Cookie")) {
161 char *semicolon = strchr(colon, ';');
162 char *equals = strchr(colon, '=');
169 vpninfo->progress(vpninfo, PRG_ERR, "Invalid cookie offered: %s\n", buf);
174 ret = http_add_cookie(vpninfo, colon, equals);
178 if (!strcasecmp(buf, "Transfer-Encoding")) {
179 if (!strcasecmp(colon, "chunked"))
182 vpninfo->progress(vpninfo, PRG_ERR, "Unknown Transfer-Encoding: %s\n", colon);
186 if (header_cb && !strncmp(buf, "X-", 2))
187 header_cb(vpninfo, buf, colon);
190 /* Handle 'HTTP/1.1 100 Continue'. Not that we should ever see it */
195 /* HTTP 1.0 response. Just eat all we can. */
197 i = SSL_read(vpninfo->https_ssl, body + done, bodylen - done);
204 /* Now the body, if there is one */
208 /* If we were given Content-Length, it's nice and easy... */
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");
221 /* ... else, chunked */
222 while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
223 int chunklen, lastchunk = 0;
226 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching chunk header\n");
229 chunklen = strtol(buf, NULL, 16);
234 if (chunklen + done > buf_len) {
235 vpninfo->progress(vpninfo, PRG_ERR, "Response body too large for buffer (%d > %d)\n",
236 chunklen + done, buf_len);
240 i = SSL_read(vpninfo->https_ssl, body + done, chunklen);
242 vpninfo->progress(vpninfo, PRG_ERR, "Error reading HTTP response body\n");
249 if ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
251 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching HTTP response body\n");
253 vpninfo->progress(vpninfo, PRG_ERR, "Error in chunked decoding. Expected '', got: '%s'",
264 SSL_free(vpninfo->https_ssl);
265 vpninfo->https_ssl = NULL;
266 close(vpninfo->ssl_fd);
267 vpninfo->ssl_fd = -1;
273 static int fetch_config(struct openconnect_info *vpninfo, char *fu, char *bu,
276 struct vpn_option *opt;
277 char buf[MAX_BUF_LEN];
279 unsigned char local_sha1_bin[SHA_DIGEST_LENGTH];
280 char local_sha1_ascii[(SHA_DIGEST_LENGTH * 2)+1];
284 sprintf(buf, "GET %s%s HTTP/1.1\r\n", fu, bu);
285 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
286 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
287 sprintf(buf + strlen(buf), "Accept: */*\r\n");
288 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
290 if (vpninfo->cookies) {
291 sprintf(buf + strlen(buf), "Cookie: ");
292 for (opt = vpninfo->cookies; opt; opt = opt->next)
293 sprintf(buf + strlen(buf), "%s=%s%s", opt->option,
294 opt->value, opt->next ? "; " : "\r\n");
296 sprintf(buf + strlen(buf), "X-Transcend-Version: 1\r\n\r\n");
298 SSL_write(vpninfo->https_ssl, buf, strlen(buf));
300 buflen = process_http_response(vpninfo, &result, NULL, buf, MAX_BUF_LEN);
302 /* We'll already have complained about whatever offended us */
311 EVP_Digest(buf, buflen, local_sha1_bin, NULL, EVP_sha1(), NULL);
312 EVP_MD_CTX_cleanup(&c);
314 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
315 sprintf(&local_sha1_ascii[i*2], "%02x", local_sha1_bin[i]);
317 if (strcasecmp(server_sha1, local_sha1_ascii)) {
318 vpninfo->progress(vpninfo, PRG_ERR, "Downloaded config file did not match intended SHA1\n");
322 return vpninfo->write_new_config(vpninfo, buf, buflen);
325 static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int buflen)
330 if (!vpninfo->uid_csd_given) {
331 vpninfo->progress(vpninfo, PRG_ERR, "Error: You are trying to "
332 "run insecure CSD code without specifying the CSD user.\n"
333 " Use command line option \"--csd-user\"\n");
338 vpninfo->progress(vpninfo, PRG_INFO,
339 "Trying to run Linux CSD trojan script.");
342 sprintf(fname, "/tmp/csdXXXXXX");
346 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open temporary CSD script file: %s\n",
350 write(fd, buf, buflen);
355 X509 *scert = SSL_get_peer_certificate(vpninfo->https_ssl);
356 X509 *ccert = SSL_get_certificate(vpninfo->https_ssl);
357 char scertbuf[EVP_MAX_MD_SIZE * 2 + 1];
358 char ccertbuf[EVP_MAX_MD_SIZE * 2 + 1];
362 if (vpninfo->uid_csd != getuid()) {
365 if (setuid(vpninfo->uid_csd)) {
366 fprintf(stderr, "Failed to set uid %d\n",
370 if (!(pw = getpwuid(vpninfo->uid_csd))) {
371 fprintf(stderr, "Invalid user uid=%d\n",
375 setenv("HOME", pw->pw_dir, 1);
376 if (chdir(pw->pw_dir)) {
377 fprintf(stderr, "Failed to change to CSD home directory '%s': %s\n",
378 pw->pw_dir, strerror(errno));
382 if (vpninfo->uid_csd == 0) {
383 fprintf(stderr, "Warning: you are running insecure "
384 "CSD code with root privileges\n"
385 "\t Use command line option \"--csd-user\"\n");
388 csd_argv[i++] = fname;
389 csd_argv[i++] = "-ticket";
390 if (asprintf(&csd_argv[i++], "\"%s\"", vpninfo->csd_ticket) == -1)
392 csd_argv[i++] = "-stub";
393 csd_argv[i++] = "\"0\"";
394 csd_argv[i++] = "-group";
395 if (asprintf(&csd_argv[i++], "\"%s\"", vpninfo->authgroup?:"") == -1)
398 get_cert_md5_fingerprint(vpninfo, scert, scertbuf);
400 get_cert_md5_fingerprint(vpninfo, ccert, ccertbuf);
404 csd_argv[i++] = "-certhash";
405 if (asprintf(&csd_argv[i++], "\"%s:%s\"", scertbuf, ccertbuf) == -1)
407 csd_argv[i++] = "-url";
408 if (asprintf(&csd_argv[i++], "\"https://%s%s\"", vpninfo->hostname, vpninfo->csd_starturl) == -1)
410 /* WTF would it want to know this for? */
411 csd_argv[i++] = "-vpnclient";
412 csd_argv[i++] = "\"/opt/cisco/vpn/bin/vpnui";
413 csd_argv[i++] = "-connect";
414 if (asprintf(&csd_argv[i++], "https://%s/%s", vpninfo->hostname, vpninfo->csd_preurl) == -1)
416 csd_argv[i++] = "-connectparam";
417 if (asprintf(&csd_argv[i++], "#csdtoken=%s\"", vpninfo->csd_token) == -1)
419 csd_argv[i++] = "-langselen";
420 csd_argv[i++] = NULL;
422 execv(fname, csd_argv);
423 vpninfo->progress(vpninfo, PRG_ERR, "Failed to exec CSD script %s\n", fname);
427 free(vpninfo->csd_stuburl);
428 vpninfo->csd_stuburl = NULL;
429 vpninfo->urlpath = strdup(vpninfo->csd_waiturl +
430 (vpninfo->csd_waiturl[0] == '/' ? 1 : 0));
431 vpninfo->csd_waiturl = NULL;
432 vpninfo->csd_scriptname = strdup(fname);
434 http_add_cookie(vpninfo, "sdesktop", vpninfo->csd_token);
440 char *local_strcasestr(const char *haystack, const char *needle)
442 int hlen = strlen(haystack);
443 int nlen = strlen(needle);
446 for (i = 0; i < hlen - nlen + 1; i++) {
447 for (j = 0; j < nlen; j++) {
448 if (tolower(haystack[i + j]) !=
453 return (char *)haystack + i;
457 #define strcasestr local_strcasestr
460 int parse_url(char *url, char **res_proto, char **res_host, int *res_port,
461 char **res_path, int default_port)
464 char *host, *path, *port_str;
467 host = strstr(url, "://");
472 if (!strcasecmp(proto, "https"))
474 else if (!strcasecmp(proto, "http"))
476 else if (!strcasecmp(proto, "socks") ||
477 !strcasecmp(proto, "socks4") ||
478 !strcasecmp(proto, "socks5"))
481 return -EPROTONOSUPPORT;
491 path = strchr(host, '/');
498 port_str = strrchr(host, ':');
501 int new_port = strtol(port_str + 1, &end, 10);
510 *res_proto = proto ? strdup(proto) : NULL;
512 *res_host = strdup(host);
516 *res_path = path ? strdup(path) : NULL;
522 * = 0, no cookie (user cancel)
523 * = 1, obtained cookie
525 int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
527 struct vpn_option *opt, *next;
528 char buf[MAX_BUF_LEN];
530 char request_body[2048];
531 char *request_body_type = NULL;
532 char *method = "GET";
535 if (!vpninfo->https_ssl && openconnect_open_https(vpninfo)) {
536 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open HTTPS connection to %s\n",
542 * It would be nice to use cURL for this, but we really need to guarantee
543 * that we'll be using OpenSSL (for the TPM stuff), and it doesn't seem
544 * to have any way to let us provide our own socket read/write functions.
545 * We can only provide a socket _open_ function. Which would require having
546 * a socketpair() and servicing the "other" end of it.
548 * So we process the HTTP for ourselves...
550 sprintf(buf, "%s /%s HTTP/1.1\r\n", method, vpninfo->urlpath ?: "");
551 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
552 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
553 sprintf(buf + strlen(buf), "Accept: */*\r\n");
554 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
556 if (vpninfo->cookies) {
557 sprintf(buf + strlen(buf), "Cookie: ");
558 for (opt = vpninfo->cookies; opt; opt = opt->next)
559 sprintf(buf + strlen(buf), "%s=%s%s", opt->option,
560 opt->value, opt->next ? "; " : "\r\n");
562 if (request_body_type) {
563 sprintf(buf + strlen(buf), "Content-Type: %s\r\n",
565 sprintf(buf + strlen(buf), "Content-Length: %zd\r\n",
566 strlen(request_body));
568 sprintf(buf + strlen(buf), "X-Transcend-Version: 1\r\n\r\n");
569 if (request_body_type)
570 sprintf(buf + strlen(buf), "%s", request_body);
572 if (vpninfo->port == 443)
573 vpninfo->progress(vpninfo, PRG_INFO, "%s https://%s/%s\n",
574 method, vpninfo->hostname,
575 vpninfo->urlpath ?: "");
577 vpninfo->progress(vpninfo, PRG_INFO, "%s https://%s:%d/%s\n",
578 method, vpninfo->hostname, vpninfo->port,
579 vpninfo->urlpath ?: "");
581 SSL_write(vpninfo->https_ssl, buf, strlen(buf));
583 buflen = process_http_response(vpninfo, &result, NULL, buf, MAX_BUF_LEN);
585 /* We'll already have complained about whatever offended us */
589 if (result != 200 && vpninfo->redirect_url) {
591 if (!strncmp(vpninfo->redirect_url, "https://", 8)) {
592 /* New host. Tear down the existing connection and make a new one */
597 free(vpninfo->urlpath);
598 vpninfo->urlpath = NULL;
600 ret = parse_url(vpninfo->redirect_url, NULL, &host, &port, &vpninfo->urlpath, 0);
602 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse redirected URL '%s': %s\n",
603 vpninfo->redirect_url, strerror(-ret));
604 free(vpninfo->redirect_url);
608 if (strcasecmp(vpninfo->hostname, host) || port != vpninfo->port) {
609 free(vpninfo->hostname);
610 vpninfo->hostname = host;
611 vpninfo->port = port;
613 /* Kill the existing connection, and a new one will happen */
614 free(vpninfo->peer_addr);
615 vpninfo->peer_addr = NULL;
616 if (vpninfo->https_ssl) {
617 SSL_free(vpninfo->https_ssl);
618 vpninfo->https_ssl = NULL;
619 close(vpninfo->ssl_fd);
620 vpninfo->ssl_fd = -1;
623 for (opt = vpninfo->cookies; opt; opt = next) {
630 vpninfo->cookies = NULL;
634 free(vpninfo->redirect_url);
635 vpninfo->redirect_url = NULL;
638 } else if (vpninfo->redirect_url[0] == '/') {
639 /* Absolute redirect within same host */
640 free(vpninfo->urlpath);
641 vpninfo->urlpath = strdup(vpninfo->redirect_url + 1);
642 free(vpninfo->redirect_url);
643 vpninfo->redirect_url = NULL;
646 vpninfo->progress(vpninfo, PRG_ERR, "Relative redirect (to '%s') not supported\n",
647 vpninfo->redirect_url);
652 if (vpninfo->csd_stuburl) {
653 /* This is the CSD stub script, which we now need to run */
654 result = run_csd_script(vpninfo, buf, buflen);
658 /* Now we'll be redirected to the waiturl */
661 if (strncmp(buf, "<?xml", 5)) {
662 /* Not XML? Perhaps it's HTML with a refresh... */
663 if (strcasestr(buf, "http-equiv=\"refresh\"")) {
664 vpninfo->progress(vpninfo, PRG_INFO, "Refreshing %s after 1 second...\n",
669 vpninfo->progress(vpninfo, PRG_ERR, "Unknown response from server\n");
673 result = parse_xml_response(vpninfo, buf, request_body, sizeof(request_body),
674 &method, &request_body_type);
680 /* A return value of 2 means the XML form indicated
681 success. We _should_ have a cookie... */
683 for (opt = vpninfo->cookies; opt; opt = opt->next) {
685 if (!strcmp(opt->option, "webvpn"))
686 vpninfo->cookie = opt->value;
687 else if (vpninfo->write_new_config && !strcmp(opt->option, "webvpnc")) {
688 char *tok = opt->value;
689 char *bu = NULL, *fu = NULL, *sha = NULL;
692 if (tok != opt->value)
695 if (!strncmp(tok, "bu:", 3))
697 else if (!strncmp(tok, "fu:", 3))
699 else if (!strncmp(tok, "fh:", 3)) {
700 if (!strncasecmp(tok+3, vpninfo->xmlsha1,
701 SHA_DIGEST_LENGTH * 2))
705 } while ((tok = strchr(tok, '&')));
708 fetch_config(vpninfo, bu, fu, sha);
711 if (vpninfo->csd_scriptname) {
712 unlink(vpninfo->csd_scriptname);
713 free(vpninfo->csd_scriptname);
714 vpninfo->csd_scriptname = NULL;
719 char *openconnect_create_useragent(char *base)
723 if (asprintf(&uagent, "%s %s", base, openconnect_version) < 0)
729 static int proxy_gets(int fd, char *buf, size_t len)
737 while ( (ret = read(fd, buf + i, 1)) == 1) {
738 if (buf[i] == '\n') {
740 if (i && buf[i-1] == '\r') {
760 static int proxy_write(int fd, unsigned char *buf, size_t len)
764 for (count = 0; count < len; ) {
765 int i = write(fd, buf + count, len - count);
774 static int proxy_read(int fd, unsigned char *buf, size_t len)
778 for (count = 0; count < len; ) {
779 int i = read(fd, buf + count, len - count);
788 static const char *socks_errors[] = {
791 "connection not allowed by ruleset",
792 "network unreachable",
794 "connection refused by destination host",
796 "command not supported / protocol error",
797 "address type not supported"
800 static int process_socks_proxy(struct openconnect_info *vpninfo, int ssl_sock)
802 unsigned char buf[1024];
805 buf[0] = 5; /* SOCKS version */
806 buf[1] = 1; /* # auth methods */
807 buf[2] = 0; /* No auth supported */
809 if ((i = proxy_write(ssl_sock, buf, 3))) {
810 vpninfo->progress(vpninfo, PRG_ERR,
811 "Error writing auth request to SOCKS proxy: %s\n",
816 if ((i = proxy_read(ssl_sock, buf, 2))) {
817 vpninfo->progress(vpninfo, PRG_ERR,
818 "Error reading auth response from SOCKS proxy: %s\n",
823 vpninfo->progress(vpninfo, PRG_ERR,
824 "Unexpected auth response from SOCKS proxy: %02x %02x\n",
830 if (buf[1] < sizeof(socks_errors) / sizeof(socks_errors[0]))
831 vpninfo->progress(vpninfo, PRG_ERR,
832 "SOCKS proxy error %02x: %s\n",
833 buf[1], socks_errors[buf[1]]);
835 vpninfo->progress(vpninfo, PRG_ERR,
836 "SOCKS proxy error %02x\n",
841 vpninfo->progress(vpninfo, PRG_INFO, "Requesting SOCKS proxy connection to %s:%d\n",
842 vpninfo->hostname, vpninfo->port);
844 buf[0] = 5; /* SOCKS version */
845 buf[1] = 1; /* CONNECT */
846 buf[2] = 0; /* Reserved */
847 buf[3] = 3; /* Address type is domain name */
848 buf[4] = strlen(vpninfo->hostname);
849 strcpy((char *)buf + 5, vpninfo->hostname);
850 i = strlen(vpninfo->hostname) + 5;
851 buf[i++] = vpninfo->port >> 8;
852 buf[i++] = vpninfo->port & 0xff;
854 if ((i = proxy_write(ssl_sock, buf, i))) {
855 vpninfo->progress(vpninfo, PRG_ERR,
856 "Error writing connect request to SOCKS proxy: %s\n",
860 /* Read 5 bytes -- up to and including the first byte of the returned
861 address (which might be the length byte of a domain name) */
862 if ((i = proxy_read(ssl_sock, buf, 5))) {
863 vpninfo->progress(vpninfo, PRG_ERR,
864 "Error reading connect response from SOCKS proxy: %s\n",
869 vpninfo->progress(vpninfo, PRG_ERR,
870 "Unexpected connect response from SOCKS proxy: %02x %02x...\n",
877 /* Connect responses contain an address */
879 case 1: /* Legacy IP */
882 case 3: /* Domain name */
889 vpninfo->progress(vpninfo, PRG_ERR,
890 "Unexpected address type %02x in SOCKS connect response\n",
895 if ((i = proxy_read(ssl_sock, buf, i))) {
896 vpninfo->progress(vpninfo, PRG_ERR,
897 "Error reading connect response from SOCKS proxy: %s\n",
904 static int process_http_proxy(struct openconnect_info *vpninfo, int ssl_sock)
906 char buf[MAX_BUF_LEN];
909 sprintf(buf, "CONNECT %s:%d HTTP/1.1\r\n", vpninfo->hostname, vpninfo->port);
910 sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
911 sprintf(buf + strlen(buf), "User-Agent: %s\r\n", vpninfo->useragent);
912 sprintf(buf + strlen(buf), "Proxy-Connection: keep-alive\r\n");
913 sprintf(buf + strlen(buf), "Connection: keep-alive\r\n");
914 sprintf(buf + strlen(buf), "Accept-Encoding: identity\r\n");
915 sprintf(buf + strlen(buf), "\r\n");
917 vpninfo->progress(vpninfo, PRG_INFO, "Requesting HTTP proxy connection to %s:%d\n",
918 vpninfo->hostname, vpninfo->port);
920 if (proxy_write(ssl_sock, (unsigned char *)buf, strlen(buf))) {
922 vpninfo->progress(vpninfo, PRG_ERR, "Sending proxy request failed: %s\n",
927 if (proxy_gets(ssl_sock, buf, sizeof(buf)) < 0) {
928 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching proxy response\n");
932 if (strncmp(buf, "HTTP/1.", 7) || (buf[7] != '0' && buf[7] != '1') ||
933 buf[8] != ' ' || !(result = atoi(buf+9))) {
934 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse proxy response '%s'\n",
940 vpninfo->progress(vpninfo, PRG_ERR, "Proxy CONNECT request failed: %s\n",
945 while ((buflen = proxy_gets(ssl_sock, buf, sizeof(buf)))) {
947 vpninfo->progress(vpninfo, PRG_ERR, "Failed to read proxy response\n");
950 vpninfo->progress(vpninfo, PRG_ERR,
951 "Unexpected continuation line after CONNECT response: '%s'\n",
958 int process_proxy(struct openconnect_info *vpninfo, int ssl_sock)
960 if (!vpninfo->proxy_type || !strcmp(vpninfo->proxy_type, "http"))
961 return process_http_proxy(vpninfo, ssl_sock);
963 if (!strcmp(vpninfo->proxy_type, "socks") ||
964 !strcmp(vpninfo->proxy_type, "socks5"))
965 return process_socks_proxy(vpninfo, ssl_sock);
967 vpninfo->progress(vpninfo, PRG_ERR, "Unknown proxy type '%s'\n",
968 vpninfo->proxy_type);
972 int set_http_proxy(struct openconnect_info *vpninfo, char *proxy)
974 char *url = strdup(proxy);
980 free(vpninfo->proxy_type);
981 vpninfo->proxy_type = NULL;
982 free(vpninfo->proxy);
983 vpninfo->proxy = NULL;
985 ret = parse_url(url, &vpninfo->proxy_type, &vpninfo->proxy,
986 &vpninfo->proxy_port, NULL, 80);
990 if (vpninfo->proxy_type &&
991 strcmp(vpninfo->proxy_type, "http") &&
992 strcmp(vpninfo->proxy_type, "socks") &&
993 strcmp(vpninfo->proxy_type, "socks5")) {
994 vpninfo->progress(vpninfo, PRG_ERR,
995 "Only http or socks(5) proxies supported\n");
996 free(vpninfo->proxy_type);
997 vpninfo->proxy_type = NULL;
998 free(vpninfo->proxy);
999 vpninfo->proxy = NULL;