Require "--setuid-csd=USER" option for servers with CSD functionality.
[platform/upstream/openconnect.git] / http.c
1 /*
2  * OpenConnect (SSL + DTLS) VPN client
3  *
4  * Copyright © 2008 Intel Corporation.
5  * Copyright © 2008 Nick Andrew <nick@nick-andrew.net>
6  *
7  * Author: David Woodhouse <dwmw2@infradead.org>
8  *
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.
12  *
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.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to:
20  *
21  *   Free Software Foundation, Inc.
22  *   51 Franklin Street, Fifth Floor,
23  *   Boston, MA 02110-1301 USA
24  */
25
26 #define _GNU_SOURCE
27 #include <netdb.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <time.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <pwd.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36
37 #include <openssl/ssl.h>
38 #include <openssl/err.h>
39 #include <openssl/engine.h>
40
41 #include "openconnect.h"
42
43 #define MAX_BUF_LEN 131072
44 /*
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.
50  */
51
52 static int process_http_response(struct openconnect_info *vpninfo, int *result,
53                                  int (*header_cb)(struct openconnect_info *, char *, char *),
54                                  char *body, int buf_len)
55 {
56         char buf[MAX_BUF_LEN];
57         int bodylen = 0;
58         int done = 0;
59         int http10 = 0, closeconn = 0;
60         int i;
61
62         if (openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)) < 0) {
63                 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching HTTPS response\n");
64                 return -EINVAL;
65         }
66
67  cont:
68         if (!strncmp(buf, "HTTP/1.0 ", 9)) {
69                 http10 = 1;
70                 closeconn = 1;
71         }
72
73         if ((!http10 && strncmp(buf, "HTTP/1.1 ", 9)) || !(*result = atoi(buf+9))) {
74                 vpninfo->progress(vpninfo, PRG_ERR, "Failed to parse HTTP response '%s'\n", buf);
75                 return -EINVAL;
76         }
77
78         vpninfo->progress(vpninfo, PRG_TRACE, "Got HTTP response: %s\n", buf);
79
80         /* Eat headers... */
81         while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
82                 char *colon;
83
84                 vpninfo->progress(vpninfo, PRG_TRACE, "%s\n", buf);
85
86                 if (i < 0) {
87                         vpninfo->progress(vpninfo, PRG_ERR, "Error processing HTTP response\n");
88                         return -EINVAL;
89                 }
90                 colon = strchr(buf, ':');
91                 if (!colon) {
92                         vpninfo->progress(vpninfo, PRG_ERR, "Ignoring unknown HTTP response line '%s'\n", buf);
93                         continue;
94                 }
95                 *(colon++) = 0;
96                 if (*colon == ' ')
97                         colon++;
98
99                 if (!strcmp(buf, "Connection") && !strcmp(colon, "Close"))
100                         closeconn = 1;
101
102                 if (!strcmp(buf, "Location")) {
103                         vpninfo->redirect_url = strdup(colon);
104                         if (!vpninfo->redirect_url)
105                                 return -ENOMEM;
106                 }
107                 if (!strcmp(buf, "Content-Length")) {
108                         bodylen = atoi(colon);
109                         if (bodylen < 0 || bodylen > buf_len) {
110                                 vpninfo->progress(vpninfo, PRG_ERR, "Response body too large for buffer (%d > %d)\n",
111                                         bodylen, buf_len);
112                                 return -EINVAL;
113                         }
114                 }
115                 if (!strcmp(buf, "Set-Cookie")) {
116                         struct vpn_option *new, **this;
117                         char *semicolon = strchr(colon, ';');
118                         char *equals = strchr(colon, '=');
119
120                         if (semicolon)
121                                 *semicolon = 0;
122
123                         if (!equals) {
124                                 vpninfo->progress(vpninfo, PRG_ERR, "Invalid cookie offered: %s\n", buf);
125                                 return -EINVAL;
126                         }
127                         *(equals++) = 0;
128
129                         if (*equals) {
130                                 new = malloc(sizeof(*new));
131                                 if (!new) {
132                                         vpninfo->progress(vpninfo, PRG_ERR, "No memory for allocating cookies\n");
133                                         return -ENOMEM;
134                                 }
135                                 new->next = NULL;
136                                 new->option = strdup(colon);
137                                 new->value = strdup(equals);
138                         } else {
139                                 /* Kill cookie; don't replace it */
140                                 new = NULL;
141                         }
142                         for (this = &vpninfo->cookies; *this; this = &(*this)->next) {
143                                 if (!strcmp(colon, (*this)->option)) {
144                                         /* Replace existing cookie */
145                                         if (new)
146                                                 new->next = (*this)->next;
147                                         else
148                                                 new = (*this)->next;
149
150                                         free((*this)->option);
151                                         free((*this)->value);
152                                         free(*this);
153                                         *this = new;
154                                         break;
155                                 }
156                         }
157                         if (new && !*this) {
158                                 *this = new;
159                                 new->next = NULL;
160                         }
161                 }
162                 if (!strcmp(buf, "Transfer-Encoding")) {
163                         if (!strcmp(colon, "chunked"))
164                                 bodylen = -1;
165                         else {
166                                 vpninfo->progress(vpninfo, PRG_ERR, "Unknown Transfer-Encoding: %s\n", colon);
167                                 return -EINVAL;
168                         }
169                 }
170                 if (header_cb && !strncmp(buf, "X-", 2))
171                         header_cb(vpninfo, buf, colon);
172         }
173
174         /* Handle 'HTTP/1.1 100 Continue'. Not that we should ever see it */
175         if (*result == 100)
176                 goto cont;
177
178         /* Now the body, if there is one */
179         if (!bodylen)
180                 goto fin;
181
182         if (http10) {
183                 /* HTTP 1.0 response. Just eat all we can. */
184                 while (1) {
185                         i = SSL_read(vpninfo->https_ssl, body + done, bodylen - done);
186                         if (i < 0)
187                                 goto fin;
188                         done += i;
189                 }
190         }
191         /* If we were given Content-Length, it's nice and easy... */
192         if (bodylen > 0) {
193                 while (done < bodylen) {
194                         i = SSL_read(vpninfo->https_ssl, body + done, bodylen - done);
195                         if (i < 0) {
196                                 vpninfo->progress(vpninfo, PRG_ERR, "Error reading HTTP response body\n");
197                                 return -EINVAL;
198                         }
199                         done += i;
200                 }
201                 goto fin;
202         }
203
204         /* ... else, chunked */
205         while ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
206                 int chunklen, lastchunk = 0;
207
208                 if (i < 0) {
209                         vpninfo->progress(vpninfo, PRG_ERR, "Error fetching chunk header\n");
210                         exit(1);
211                 }
212                 chunklen = strtol(buf, NULL, 16);
213                 if (!chunklen) {
214                         lastchunk = 1;
215                         goto skip;
216                 }
217                 if (chunklen + done > buf_len) {
218                         vpninfo->progress(vpninfo, PRG_ERR, "Response body too large for buffer (%d > %d)\n",
219                                 chunklen + done, buf_len);
220                         return -EINVAL;
221                 }
222                 while (chunklen) {
223                         i = SSL_read(vpninfo->https_ssl, body + done, chunklen);
224                         if (i < 0) {
225                                 vpninfo->progress(vpninfo, PRG_ERR, "Error reading HTTP response body\n");
226                                 return -EINVAL;
227                         }
228                         chunklen -= i;
229                         done += i;
230                 }
231         skip:
232                 if ((i = openconnect_SSL_gets(vpninfo->https_ssl, buf, sizeof(buf)))) {
233                         if (i < 0) {
234                                 vpninfo->progress(vpninfo, PRG_ERR, "Error fetching HTTP response body\n");
235                         } else {
236                                 vpninfo->progress(vpninfo, PRG_ERR, "Error in chunked decoding. Expected '', got: '%s'",
237                                         buf);
238                         }
239                         return -EINVAL;
240                 }
241
242                 if (lastchunk)
243                         break;
244         }
245  fin:
246         if (closeconn) {
247                 SSL_free(vpninfo->https_ssl);
248                 vpninfo->https_ssl = NULL;
249                 close(vpninfo->ssl_fd);
250                 vpninfo->ssl_fd = -1;
251         }
252         body[done] = 0;
253         return done;
254 }
255
256 static int fetch_config(struct openconnect_info *vpninfo, char *fu, char *bu,
257                         char *server_sha1)
258 {
259         struct vpn_option *opt;
260         char buf[MAX_BUF_LEN];
261         int result, buflen;
262         unsigned char local_sha1_bin[SHA_DIGEST_LENGTH];
263         char local_sha1_ascii[(SHA_DIGEST_LENGTH * 2)+1];
264         EVP_MD_CTX c;
265         int i;
266
267         sprintf(buf, "GET %s%s HTTP/1.1\r\n", fu, bu);
268         sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
269         sprintf(buf + strlen(buf),  "User-Agent: %s\r\n", vpninfo->useragent);
270         sprintf(buf + strlen(buf),  "Accept: */*\r\n");
271         sprintf(buf + strlen(buf),  "Accept-Encoding: identity\r\n");
272
273         if (vpninfo->cookies) {
274                 sprintf(buf + strlen(buf),  "Cookie: ");
275                 for (opt = vpninfo->cookies; opt; opt = opt->next)
276                         sprintf(buf + strlen(buf),  "%s=%s%s", opt->option,
277                                       opt->value, opt->next ? "; " : "\r\n");
278         }
279         sprintf(buf + strlen(buf),  "X-Transcend-Version: 1\r\n\r\n");
280
281         SSL_write(vpninfo->https_ssl, buf, strlen(buf));
282
283         buflen = process_http_response(vpninfo, &result, NULL, buf, MAX_BUF_LEN);
284         if (buflen < 0) {
285                 /* We'll already have complained about whatever offended us */
286                 return -EINVAL;
287         }
288
289         if (result != 200)
290                 return -EINVAL;
291
292
293         EVP_MD_CTX_init(&c);
294         EVP_Digest(buf, buflen, local_sha1_bin, NULL, EVP_sha1(), NULL);
295         EVP_MD_CTX_cleanup(&c);
296
297         for (i = 0; i < SHA_DIGEST_LENGTH; i++)
298                 sprintf(&local_sha1_ascii[i*2], "%02x", local_sha1_bin[i]);
299
300         if (strcasecmp(server_sha1, local_sha1_ascii)) {
301                 vpninfo->progress(vpninfo, PRG_ERR, "Downloaded config file did not match intended SHA1\n");
302                 return -EINVAL;
303         }
304
305         return vpninfo->write_new_config(vpninfo, buf, buflen);
306 }
307
308 static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int buflen)
309 {
310         char fname[16];
311         int fd;
312
313         if (!vpninfo->uid_csd_given) {
314                 vpninfo->progress(vpninfo, PRG_ERR, "Error: You are trying to "
315                                   "run unsecure CSD code without specifying the CSD user.\n"
316                                   "       Use command line option \"--setuid-csd\"\n");
317                 exit(1);
318         }
319
320         sprintf(fname, "/tmp/csdXXXXXX");
321         fd = mkstemp(fname);
322         if (fd < 0) {
323                 int err = -errno;
324                 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open temporary CSD script file: %s\n",
325                                   strerror(errno));
326                 return err;
327         }
328         write(fd, buf, buflen);
329         fchmod(fd, 0755);
330         close(fd);
331
332         if (!fork()) {
333                 X509 *cert = SSL_get_peer_certificate(vpninfo->https_ssl);
334                 char certbuf[EVP_MAX_MD_SIZE * 2 + 1];
335                 char *csd_argv[32];
336                 int i = 0;
337
338                 if (vpninfo->uid_csd != getuid()) {
339                         struct passwd *pw;
340
341                         if (setuid(vpninfo->uid_csd)) {
342                                 fprintf(stderr, "Failed to set uid %d\n",
343                                         vpninfo->uid_csd);
344                                 exit(1);
345                         }
346                         if (!(pw = getpwuid(vpninfo->uid_csd))) {
347                                 fprintf(stderr, "Invalid user uid=%d\n",
348                                         vpninfo->uid_csd);
349                                 exit(1);
350                         }
351                         setenv("HOME", pw->pw_dir, 1);
352                         chdir(pw->pw_dir);
353                 }
354                 if (vpninfo->uid_csd == 0) {
355                         fprintf(stderr, "Warning: you are running unsecure "
356                                 "CSD code with root privileges\n"
357                                 "\t Use command line option \"--setuid-csd\"\n");
358                 }
359
360                 csd_argv[i++] = fname;
361                 csd_argv[i++] = "-ticket";
362                 asprintf(&csd_argv[i++], "\"%s\"", vpninfo->csd_ticket);
363                 csd_argv[i++] = "-stub";
364                 csd_argv[i++] = "\"0\"";
365                 csd_argv[i++] = "-group";
366                 asprintf(&csd_argv[i++], "\"%s\"", vpninfo->authgroup?:"");
367                 get_cert_md5_fingerprint(vpninfo, cert, certbuf);
368                 csd_argv[i++] = "-certhash";
369                 asprintf(&csd_argv[i++], "\"%s:%s\"", certbuf, vpninfo->cert_md5_fingerprint ?: "");
370                 csd_argv[i++] = "-url";
371                 asprintf(&csd_argv[i++], "\"https://%s%s\"", vpninfo->hostname, vpninfo->csd_starturl);
372                 /* WTF would it want to know this for? */
373                 csd_argv[i++] = "-vpnclient";
374                 csd_argv[i++] = "\"/opt/cisco/vpn/bin/vpnui";
375                 csd_argv[i++] = "-connect";
376                 asprintf(&csd_argv[i++], "https://%s/%s", vpninfo->hostname, vpninfo->csd_preurl);
377                 csd_argv[i++] = "-connectparam";
378                 asprintf(&csd_argv[i++], "#csdtoken=%s\"", vpninfo->csd_token);
379                 csd_argv[i++] = "-langselen";
380                 csd_argv[i++] = NULL;
381
382                 execv(fname, csd_argv);
383                 vpninfo->progress(vpninfo, PRG_ERR, "Failed to exec CSD script %s\n", fname);
384                 exit(1);
385         }
386
387         free(vpninfo->csd_stuburl);
388         vpninfo->csd_stuburl = NULL;
389         vpninfo->urlpath = strdup(vpninfo->csd_waiturl +
390                                   (vpninfo->csd_waiturl[0] == '/' ? 1 : 0));
391         vpninfo->csd_waiturl = NULL;
392         vpninfo->csd_scriptname = strdup(fname);
393
394         /* AB: add cookie "sdesktop=__TOKEN__" */
395         {
396                 struct vpn_option *new, **this;
397                 new = malloc(sizeof(*new));
398                 if (!new) {
399                         vpninfo->progress(vpninfo, PRG_ERR, "No memory for allocating cookies\n");
400                         return -ENOMEM;
401                 }
402                 new->next = NULL;
403                 new->option = strdup("sdesktop");
404                 new->value = strdup(vpninfo->csd_token);
405
406                 for (this = &vpninfo->cookies; *this; this = &(*this)->next) {
407                         if (!strcmp(new->option, (*this)->option)) {
408                                 /* Replace existing cookie */
409                                 if (new)
410                                         new->next = (*this)->next;
411                                 else
412                                         new = (*this)->next;
413
414                                 free((*this)->option);
415                                 free((*this)->value);
416                                 free(*this);
417                                 *this = new;
418                                 break;
419                         }
420                 }
421                 if (new && !*this) {
422                         *this = new;
423                         new->next = NULL;
424                 }
425         }
426         return 0;
427 }
428
429 /* Return value:
430  *  < 0, on error
431  *  = 0, no cookie (user cancel)
432  *  = 1, obtained cookie
433  */
434 int openconnect_obtain_cookie(struct openconnect_info *vpninfo)
435 {
436         struct vpn_option *opt, *next;
437         char buf[MAX_BUF_LEN];
438         int result, buflen;
439         char request_body[2048];
440         char *request_body_type = NULL;
441         char *method = "GET";
442
443  retry:
444         if (!vpninfo->https_ssl && openconnect_open_https(vpninfo)) {
445                 vpninfo->progress(vpninfo, PRG_ERR, "Failed to open HTTPS connection to %s\n",
446                         vpninfo->hostname);
447                 return -EINVAL;
448         }
449
450         /*
451          * It would be nice to use cURL for this, but we really need to guarantee
452          * that we'll be using OpenSSL (for the TPM stuff), and it doesn't seem
453          * to have any way to let us provide our own socket read/write functions.
454          * We can only provide a socket _open_ function. Which would require having
455          * a socketpair() and servicing the "other" end of it.
456          *
457          * So we process the HTTP for ourselves...
458          */
459         sprintf(buf, "%s /%s HTTP/1.1\r\n", method, vpninfo->urlpath ?: "");
460         sprintf(buf + strlen(buf), "Host: %s\r\n", vpninfo->hostname);
461         sprintf(buf + strlen(buf),  "User-Agent: %s\r\n", vpninfo->useragent);
462         sprintf(buf + strlen(buf),  "Accept: */*\r\n");
463         sprintf(buf + strlen(buf),  "Accept-Encoding: identity\r\n");
464
465         if (vpninfo->cookies) {
466                 sprintf(buf + strlen(buf),  "Cookie: ");
467                 for (opt = vpninfo->cookies; opt; opt = opt->next)
468                         sprintf(buf + strlen(buf),  "%s=%s%s", opt->option,
469                                       opt->value, opt->next ? "; " : "\r\n");
470         }
471         if (request_body_type) {
472                 sprintf(buf + strlen(buf),  "Content-Type: %s\r\n",
473                               request_body_type);
474                 sprintf(buf + strlen(buf),  "Content-Length: %zd\r\n",
475                               strlen(request_body));
476         }
477         sprintf(buf + strlen(buf),  "X-Transcend-Version: 1\r\n\r\n");
478         if (request_body_type)
479                 sprintf(buf + strlen(buf), "%s", request_body);
480
481         vpninfo->progress(vpninfo, PRG_INFO, "%s %s/%s\n", method,
482                           vpninfo->hostname, vpninfo->urlpath ?: "");
483
484         SSL_write(vpninfo->https_ssl, buf, strlen(buf));
485
486         buflen = process_http_response(vpninfo, &result, NULL, buf, MAX_BUF_LEN);
487         if (buflen < 0) {
488                 /* We'll already have complained about whatever offended us */
489                 exit(1);
490         }
491
492         if (result != 200 && vpninfo->redirect_url) {
493         redirect:
494                 if (!strncmp(vpninfo->redirect_url, "https://", 8)) {
495                         /* New host. Tear down the existing connection and make a new one */
496                         char *host = vpninfo->redirect_url + 8;
497                         char *path = strchr(host, '/');
498
499                         free(vpninfo->urlpath);
500                         if (path) {
501                                 *(path++) = 0;
502                                 vpninfo->urlpath = strdup(path);
503                         } else
504                                 vpninfo->urlpath = NULL;
505
506                         if (strcmp(vpninfo->hostname, host)) {
507                                 free(vpninfo->hostname);
508                                 vpninfo->hostname = strdup(host);
509
510                                 /* Kill the existing connection, and a new one will happen */
511                                 SSL_free(vpninfo->https_ssl);
512                                 vpninfo->https_ssl = NULL;
513                                 close(vpninfo->ssl_fd);
514                                 vpninfo->ssl_fd = -1;
515
516                                 for (opt = vpninfo->cookies; opt; opt = next) {
517                                         next = opt->next;
518
519                                         free(opt->option);
520                                         free(opt->value);
521                                         free(opt);
522                                 }
523                                 vpninfo->cookies = NULL;
524                         }
525                         free(vpninfo->redirect_url);
526                         vpninfo->redirect_url = NULL;
527
528                         goto retry;
529                 } else if (vpninfo->redirect_url[0] == '/') {
530                         /* Absolute redirect within same host */
531                         free(vpninfo->urlpath);
532                         vpninfo->urlpath = strdup(vpninfo->redirect_url + 1);
533                         free(vpninfo->redirect_url);
534                         vpninfo->redirect_url = NULL;
535                         goto retry;
536                 } else {
537                         vpninfo->progress(vpninfo, PRG_ERR, "Relative redirect (to '%s') not supported\n",
538                                 vpninfo->redirect_url);
539                         return -EINVAL;
540                 }
541         }
542
543         if (vpninfo->csd_stuburl) {
544                 /* This is the CSD stub script, which we now need to run */
545                 result = run_csd_script(vpninfo, buf, buflen);
546                 if (result)
547                         return result;
548
549                 /* Now we'll be redirected to the waiturl */
550                 goto retry;
551         }
552         if (strncmp(buf, "<?xml", 5)) {
553                 /* Not XML? Perhaps it's HTML with a refresh... */
554                 if (strcasestr(buf, "http-equiv=\"refresh\"")) {
555                         vpninfo->progress(vpninfo, PRG_INFO, "Refreshing %s after 1 second...\n",
556                                           vpninfo->urlpath);
557                         sleep(1);
558                         goto retry;
559                 }
560                 vpninfo->progress(vpninfo, PRG_ERR, "Unknown response from server\n");
561                 return -EINVAL;
562         }
563         request_body[0] = 0;
564         result = parse_xml_response(vpninfo, buf, request_body, sizeof(request_body),
565                                     &method, &request_body_type);
566         if (!result)
567                 goto redirect;
568
569         if (result != 2)
570                 return result;
571         /* A return value of 2 means the XML form indicated
572            success. We _should_ have a cookie... */
573
574         for (opt = vpninfo->cookies; opt; opt = opt->next) {
575
576                 if (!strcmp(opt->option, "webvpn"))
577                         vpninfo->cookie = opt->value;
578                 else if (vpninfo->write_new_config && !strcmp(opt->option, "webvpnc")) {
579                         char *tok = opt->value;
580                         char *bu = NULL, *fu = NULL, *sha = NULL;
581
582                         do {
583                                 if (tok != opt->value)
584                                         *(tok++) = 0;
585
586                                 if (!strncmp(tok, "bu:", 3))
587                                         bu = tok + 3;
588                                 else if (!strncmp(tok, "fu:", 3))
589                                         fu = tok + 3;
590                                 else if (!strncmp(tok, "fh:", 3)) {
591                                         if (!strncasecmp(tok+3, vpninfo->xmlsha1,
592                                                          SHA_DIGEST_LENGTH * 2))
593                                                 break;
594                                         sha = tok + 3;
595                                 }
596                         } while ((tok = strchr(tok, '&')));
597
598                         if (bu && fu && sha)
599                                 fetch_config(vpninfo, bu, fu, sha);
600                 }
601         }
602         if (vpninfo->csd_scriptname) {
603                 unlink(vpninfo->csd_scriptname);
604                 free(vpninfo->csd_scriptname);
605                 vpninfo->csd_scriptname = NULL;
606         }
607         return 0;
608 }
609
610 char *openconnect_create_useragent(char *base)
611 {
612         char *uagent = malloc(strlen(base) + 1 + strlen(openconnect_version));
613         sprintf(uagent, "%s %s", base, openconnect_version);
614         return uagent;
615 }