stoken: Implement new auth form to gather soft token information
[platform/upstream/openconnect.git] / http.c
diff --git a/http.c b/http.c
index 9b539d5..38fc9a5 100644 (file)
--- a/http.c
+++ b/http.c
@@ -335,7 +335,7 @@ static int process_http_response(struct openconnect_info *vpninfo, int *result,
        }
 
        if (closeconn || vpninfo->no_http_keepalive)
-               openconnect_close_https(vpninfo);
+               openconnect_close_https(vpninfo, 0);
 
        if (body)
                body[done] = 0;
@@ -354,6 +354,13 @@ static int fetch_config(struct openconnect_info *vpninfo, char *fu, char *bu,
        char local_sha1_ascii[(SHA1_SIZE * 2)+1];
        int i;
 
+       if (openconnect_open_https(vpninfo)) {
+               vpn_progress(vpninfo, PRG_ERR,
+                            _("Failed to open HTTPS connection to %s\n"),
+                            vpninfo->hostname);
+               return -EINVAL;
+       }
+
        sprintf(buf, "GET %s%s HTTP/1.1\r\n", fu, bu);
        sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
        sprintf(buf + strlen(buf),  "User-Agent: %s\r\n", vpninfo->useragent);
@@ -368,7 +375,7 @@ static int fetch_config(struct openconnect_info *vpninfo, char *fu, char *bu,
        }
        sprintf(buf + strlen(buf),  "X-Transcend-Version: 1\r\n\r\n");
 
-       if (openconnect_SSL_write(vpninfo, buf, strlen(buf))) {
+       if (openconnect_SSL_write(vpninfo, buf, strlen(buf)) != strlen(buf)) {
                vpn_progress(vpninfo, PRG_ERR,
                             _("Failed to send GET request for new config\n"));
                return -EIO;
@@ -433,7 +440,7 @@ static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int bufle
        if (ret) {
                vpn_progress(vpninfo, PRG_ERR,
                             _("Failed to write temporary CSD script file: %s\n"),
-                            strerror(ret));
+                            strerror(-ret));
                return ret;
        }
        fchmod(fd, 0755);
@@ -445,7 +452,7 @@ static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int bufle
                char *csd_argv[32];
                int i = 0;
 
-               if (vpninfo->uid_csd != getuid()) {
+               if (vpninfo->uid_csd_given && vpninfo->uid_csd != getuid()) {
                        struct passwd *pw;
 
                        if (setuid(vpninfo->uid_csd)) {
@@ -465,12 +472,12 @@ static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int bufle
                                exit(1);
                        }
                }
-               if (vpninfo->uid_csd == 0 && !vpninfo->csd_wrapper) {
+               if (getuid() == 0 && !vpninfo->csd_wrapper) {
                        fprintf(stderr, _("Warning: you are running insecure "
                                          "CSD code with root privileges\n"
                                          "\t Use command line option \"--csd-user\"\n"));
                }
-               if (vpninfo->uid_csd_given == 2) {             
+               if (vpninfo->uid_csd_given == 2) {
                        /* The NM tool really needs not to get spurious output
                           on stdout, which the CSD trojan spews. */
                        dup2(2, 1);
@@ -497,15 +504,7 @@ static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int bufle
                csd_argv[i++]= (char *)"-url";
                if (asprintf(&csd_argv[i++], "\"https://%s%s\"", vpninfo->hostname, vpninfo->csd_starturl) == -1)
                        return -ENOMEM;
-               /* WTF would it want to know this for? */
-               csd_argv[i++]= (char *)"-vpnclient";
-               csd_argv[i++]= (char *)"\"/opt/cisco/vpn/bin/vpnui";
-               csd_argv[i++]= (char *)"-connect";
-               if (asprintf(&csd_argv[i++], "https://%s/%s", vpninfo->hostname, vpninfo->csd_preurl) == -1)
-                       return -ENOMEM;
-               csd_argv[i++]= (char *)"-connectparam";
-               if (asprintf(&csd_argv[i++], "#csdtoken=%s\"", vpninfo->csd_token) == -1)
-                       return -ENOMEM;
+
                csd_argv[i++]= (char *)"-langselen";
                csd_argv[i++] = NULL;
 
@@ -527,28 +526,6 @@ static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int bufle
        return 0;
 }
 
-#ifndef HAVE_STRCASESTR
-static char *openconnect__strcasestr(const char *haystack, const char *needle)
-{
-       int hlen = strlen(haystack);
-       int nlen = strlen(needle);
-       int i, j;
-
-       for (i = 0; i < hlen - nlen + 1; i++) {
-               for (j = 0; j < nlen; j++) {
-                       if (tolower(haystack[i + j]) != 
-                           tolower(needle[j]))
-                               break;
-               }
-               if (j == nlen)
-                       return (char *)haystack + i;
-       }
-       return NULL;
-}
-#define strcasestr openconnect__strcasestr
-#endif
-
-
 int internal_parse_url(char *url, char **res_proto, char **res_host,
                       int *res_port, char **res_path, int default_port)
 {
@@ -616,8 +593,8 @@ int internal_parse_url(char *url, char **res_proto, char **res_host,
 
 /* Return value:
  *  < 0, on error
- *  = 0, no cookie (user cancel)
- *  = 1, obtained cookie
+ *  > 0, no cookie (user cancel)
+ *  = 0, obtained cookie
  */
 int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
 {
@@ -629,6 +606,12 @@ int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
        const char *request_body_type = NULL;
        const char *method = "GET";
 
+       if (vpninfo->use_stoken) {
+               result = prepare_stoken(vpninfo);
+               if (result)
+                       return result;
+       }
+
  retry:
        if (form_buf) {
                free(form_buf);
@@ -721,7 +704,7 @@ int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
                                /* Kill the existing connection, and a new one will happen */
                                free(vpninfo->peer_addr);
                                vpninfo->peer_addr = NULL;
-                               openconnect_close_https(vpninfo);
+                               openconnect_close_https(vpninfo, 0);
 
                                for (opt = vpninfo->cookies; opt; opt = next) {
                                        next = opt->next;