[PATCH 03/10] Clean up dodgy query-string building in gpst.c

Daniel Lenski dlenski at gmail.com
Tue Jan 9 00:01:17 PST 2018


This function helps a lot:

    static int filter_opts(struct oc_text_buf *buf, const char *query, const char *incexc, int include)

It takes a URL query string and a comma-separated list of fields to include
or exclude, and copies fields into the buffer, e.g.

    /* include=1: copy only the named fields into the buffer */
    filter_opts(buf, vpninfo->cookie, "user,authcookie", 1);

    /* include=0: copy all fields except the named ones into the buffer */
    filter_opts(buf, vpninfo->cookie, "authcookie,junk", 0);

Signed-off-by: Daniel Lenski <dlenski at gmail.com>
---
 gpst.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)

diff --git a/gpst.c b/gpst.c
index 87cfbc9..994f45d 100644
--- a/gpst.c
+++ b/gpst.c
@@ -100,6 +100,32 @@ static const char *add_option(struct openconnect_info *vpninfo, const char *opt,
 	return new->value;
 }
 
+static int filter_opts(struct oc_text_buf *buf, const char *query, const char *incexc, int include)
+{
+	const char *f, *endf, *eq;
+	const char *found, *comma;
+
+	for (f = query; *f; f=(*endf) ? endf+1 : endf) {
+		endf = strchr(f, '&') ? : f+strlen(f);
+		eq = strchr(f, '=');
+		if (!eq || eq > endf)
+			eq = endf;
+
+		for (found = incexc; *found; found=(*comma) ? comma+1 : comma) {
+			comma = strchr(found, ',') ? : found+strlen(found);
+			if (!strncmp(found, f, MAX(comma-found, eq-f)))
+				break;
+		}
+
+		if ((include && *found) || (!include && !*found)) {
+			if (buf->pos && buf->data[buf->pos-1] != '?' && buf->data[buf->pos-1] != '&')
+				buf_append(buf, "&");
+			buf_append_bytes(buf, f, (int)(endf-f));
+		}
+	}
+	return buf_error(buf);
+}
+
 /* Parse this JavaScript-y mess:
 
 	"var respStatus = \"Challenge|Error\";\n"
@@ -504,9 +530,11 @@ static int gpst_get_config(struct openconnect_info *vpninfo)
 	append_opt(request_body, "clientos", vpninfo->platname);
 	append_opt(request_body, "hmac-algo", "sha1,md5");
 	append_opt(request_body, "enc-algo", "aes-128-cbc,aes-256-cbc");
-	if (old_addr)
+	if (old_addr) {
 		append_opt(request_body, "preferred-ip", old_addr);
-	buf_append(request_body, "&%s", vpninfo->cookie);
+		filter_opts(request_body, vpninfo->cookie, "preferred-ip", 0);
+	} else
+		buf_append(request_body, "&%s", vpninfo->cookie);
 
 	orig_path = vpninfo->urlpath;
 	vpninfo->urlpath = strdup("ssl-vpn/getconfig.esp");
@@ -577,7 +605,9 @@ static int gpst_connect(struct openconnect_info *vpninfo)
 		return ret;
 
 	reqbuf = buf_alloc();
-	buf_append(reqbuf, "GET /ssl-tunnel-connect.sslvpn?%s HTTP/1.1\r\n\r\n", vpninfo->cookie);
+	buf_append(reqbuf, "GET /ssl-tunnel-connect.sslvpn?");
+	filter_opts(reqbuf, vpninfo->cookie, "user,authcookie", 1);
+	buf_append(reqbuf, " HTTP/1.1\r\n\r\n");
 
 	if (vpninfo->dump_http_traffic)
 		dump_buf(vpninfo, '>', reqbuf->data);
-- 
2.7.4




More information about the openconnect-devel mailing list