From c8ca180f02a569e6692c699eb8aa70df976d49a5 Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Sun, 7 Feb 2021 14:20:06 -0800 Subject: [PATCH] factor out internal_get_url function Returns 'https://hostname[:port][/urlpath]' in a newly malloc'ed string. Signed-off-by: Daniel Lenski --- auth-juniper.c | 24 ++++++++---------------- auth.c | 18 +++++------------- http.c | 9 +++++++++ openconnect-internal.h | 1 + 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/auth-juniper.c b/auth-juniper.c index 17c0b707..64d2d82e 100644 --- a/auth-juniper.c +++ b/auth-juniper.c @@ -453,7 +453,7 @@ int oncp_obtain_cookie(struct openconnect_info *vpninfo) while (1) { char *form_buf = NULL; int role_select = 0; - struct oc_text_buf *url; + char *url; if (resp_buf && resp_buf->pos) ret = do_https_request(vpninfo, "POST", @@ -466,30 +466,22 @@ int oncp_obtain_cookie(struct openconnect_info *vpninfo) if (ret < 0) break; - url = buf_alloc(); - buf_append(url, "https://%s", vpninfo->hostname); - if (vpninfo->port != 443) - buf_append(url, ":%d", vpninfo->port); - buf_append(url, "/"); - if (vpninfo->urlpath) - buf_append(url, "%s", vpninfo->urlpath); - - if (buf_error(url)) { + if (!check_cookie_success(vpninfo)) { free(form_buf); - ret = buf_free(url); + ret = 0; break; } - if (!check_cookie_success(vpninfo)) { - buf_free(url); + url = internal_get_url(vpninfo); + if (!url) { free(form_buf); - ret = 0; + ret = -ENOMEM; break; } - doc = htmlReadMemory(form_buf, ret, url->data, NULL, + doc = htmlReadMemory(form_buf, ret, url, NULL, HTML_PARSE_RECOVER|HTML_PARSE_NOERROR|HTML_PARSE_NOWARNING|HTML_PARSE_NONET); - buf_free(url); + free(url); free(form_buf); if (!doc) { vpn_progress(vpninfo, PRG_ERR, diff --git a/auth.c b/auth.c index 643cfc61..4a63a017 100644 --- a/auth.c +++ b/auth.c @@ -812,23 +812,16 @@ static int xmlpost_initial_req(struct openconnect_info *vpninfo, { xmlNodePtr root, node; xmlDocPtr doc = xmlpost_new_query(vpninfo, "init", &root); - struct oc_text_buf *url_buf; + char *url; if (!doc) return -ENOMEM; - url_buf = buf_alloc(); - buf_append(url_buf, "https://%s", vpninfo->hostname); - if (vpninfo->port != 443) - buf_append(url_buf, ":%d", vpninfo->port); - /* Do we *need* to omit the trailing / here when no path? */ - if (vpninfo->urlpath) - buf_append(url_buf, "/%s", vpninfo->urlpath); - - if (buf_error(url_buf)) + url = internal_get_url(vpninfo); + if (!url) goto bad; - node = xmlNewTextChild(root, NULL, XCAST("group-access"), XCAST(url_buf->data)); + node = xmlNewTextChild(root, NULL, XCAST("group-access"), XCAST(url)); if (!node) goto bad; if (cert_fail) { @@ -841,11 +834,10 @@ static int xmlpost_initial_req(struct openconnect_info *vpninfo, if (!node) goto bad; } - buf_free(url_buf); + free(url); return xmlpost_complete(doc, request_body); bad: - buf_free(url_buf); xmlpost_complete(doc, NULL); return -ENOMEM; } diff --git a/http.c b/http.c index 1fa30142..7598862e 100644 --- a/http.c +++ b/http.c @@ -811,6 +811,15 @@ int internal_parse_url(const char *url, char **res_proto, char **res_host, return 0; } +char *internal_get_url(struct openconnect_info *vpninfo) +{ + char *url; + if (asprintf(&url, "https://%s%s%s", vpninfo->hostname, vpninfo->urlpath ? "/" : "", vpninfo->urlpath) < 0) + return NULL; + else + return url; +} + void openconnect_clear_cookies(struct openconnect_info *vpninfo) { struct oc_vpn_option *opt, *next; diff --git a/openconnect-internal.h b/openconnect-internal.h index b3e084f8..033190b3 100644 --- a/openconnect-internal.h +++ b/openconnect-internal.h @@ -1157,6 +1157,7 @@ char *openconnect_create_useragent(const char *base); int process_proxy(struct openconnect_info *vpninfo, int ssl_sock); int internal_parse_url(const char *url, char **res_proto, char **res_host, int *res_port, char **res_path, int default_port); +char *internal_get_url(struct openconnect_info *vpninfo); int do_https_request(struct openconnect_info *vpninfo, const char *method, const char *request_body_type, struct oc_text_buf *request_body, char **form_buf, int fetch_redirect); -- 2.49.0