~ubuntu-branches/ubuntu/lucid/curl/lucid-security

« back to all changes in this revision

Viewing changes to lib/cookie.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2009-04-29 11:10:29 UTC
  • mfrom: (3.2.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090429111029-2j5eiyokfw2bw049
Tags: 7.19.4-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Drop build dependencies: stunnel, libdb4.6-dev, libssh2-1-dev
  - Add build-dependency on openssh-server
  - Drop libssh2-1-dev from libcurl4-openssl-dev's Depends.
  - Call automake-1.9 with --add-missing --copy --force
* drop debian/patches/security_CVE-2009-0037.patch 
  - this patch is part of 7.19.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: cookie.c,v 1.87 2008-01-31 12:21:57 bagder Exp $
 
21
 * $Id: cookie.c,v 1.101 2009-01-15 08:32:58 bagder Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
/***
96
96
#include "memory.h"
97
97
#include "share.h"
98
98
#include "strtoofft.h"
 
99
#include "rawstr.h"
99
100
 
100
101
/* The last #include file should be: */
101
102
#ifdef CURLDEBUG
131
132
  if(littlelen > biglen)
132
133
    return FALSE;
133
134
 
134
 
  return (bool)strequal(little, bigone+biglen-littlelen);
 
135
  return (bool)Curl_raw_equal(little, bigone+biglen-littlelen);
135
136
}
136
137
 
137
138
/*
190
191
#endif
191
192
 
192
193
  /* First, alloc and init a new struct for it */
193
 
  co = (struct Cookie *)calloc(sizeof(struct Cookie), 1);
 
194
  co = calloc(sizeof(struct Cookie), 1);
194
195
  if(!co)
195
196
    return NULL; /* bail out if we're this low on memory */
196
197
 
243
244
            whatptr++;
244
245
          }
245
246
 
246
 
          if(strequal("path", name)) {
 
247
          if(Curl_raw_equal("path", name)) {
247
248
            co->path=strdup(whatptr);
248
249
            if(!co->path) {
249
250
              badcookie = TRUE; /* out of memory bad */
250
251
              break;
251
252
            }
252
253
          }
253
 
          else if(strequal("domain", name)) {
 
254
          else if(Curl_raw_equal("domain", name)) {
254
255
            /* note that this name may or may not have a preceeding dot, but
255
256
               we don't care about that, we treat the names the same anyway */
256
257
 
315
316
              }
316
317
            }
317
318
          }
318
 
          else if(strequal("version", name)) {
 
319
          else if(Curl_raw_equal("version", name)) {
319
320
            co->version=strdup(whatptr);
320
321
            if(!co->version) {
321
322
              badcookie = TRUE;
322
323
              break;
323
324
            }
324
325
          }
325
 
          else if(strequal("max-age", name)) {
 
326
          else if(Curl_raw_equal("max-age", name)) {
326
327
            /* Defined in RFC2109:
327
328
 
328
329
               Optional.  The Max-Age attribute defines the lifetime of the
338
339
              break;
339
340
            }
340
341
            co->expires =
341
 
              atoi((*co->maxage=='\"')?&co->maxage[1]:&co->maxage[0]) + (long)now;
 
342
              atoi((*co->maxage=='\"')?&co->maxage[1]:&co->maxage[0]) +
 
343
              (long)now;
342
344
          }
343
 
          else if(strequal("expires", name)) {
 
345
          else if(Curl_raw_equal("expires", name)) {
344
346
            co->expirestr=strdup(whatptr);
345
347
            if(!co->expirestr) {
346
348
              badcookie = TRUE;
347
349
              break;
348
350
            }
 
351
            /* Note that we store -1 in 'expires' here if the date couldn't
 
352
               get parsed for whatever reason. This will have the effect that
 
353
               the cookie won't match. */
349
354
            co->expires = curl_getdate(what, &now);
350
355
          }
351
356
          else if(!co->name) {
367
372
      else {
368
373
        if(sscanf(ptr, "%" MAX_COOKIE_LINE_TXT "[^;\r\n]",
369
374
                  what)) {
370
 
          if(strequal("secure", what)) {
 
375
          if(Curl_raw_equal("secure", what)) {
371
376
            co->secure = TRUE;
372
377
          }
373
 
          else if (strequal("httponly", what)) {
 
378
          else if (Curl_raw_equal("httponly", what)) {
374
379
            co->httponly = TRUE;
375
380
          }
376
381
          /* else,
434
439
       reading the odd netscape cookies-file format here */
435
440
    char *ptr;
436
441
    char *firstptr;
437
 
    char *tok_buf;
 
442
    char *tok_buf=NULL;
438
443
    int fields;
439
444
 
440
 
    /* IE introduced HTTP-only cookies to prevent XSS attacks. Cookies 
441
 
       marked with httpOnly after the domain name are not accessible  
442
 
       from javascripts, but since curl does not operate at javascript  
443
 
       level, we include them anyway. In Firefox's cookie files, these 
 
445
    /* IE introduced HTTP-only cookies to prevent XSS attacks. Cookies
 
446
       marked with httpOnly after the domain name are not accessible
 
447
       from javascripts, but since curl does not operate at javascript
 
448
       level, we include them anyway. In Firefox's cookie files, these
444
449
       lines are preceeded with #HttpOnly_ and then everything is
445
450
       as usual, so we skip 10 characters of the line..
446
451
    */
449
454
      co->httponly = TRUE;
450
455
    }
451
456
 
452
 
 
453
457
    if(lineptr[0]=='#') {
454
458
      /* don't even try the comments */
455
459
      free(co);
494
498
           As far as I can see, it is set to true when the cookie says
495
499
           .domain.com and to false when the domain is complete www.domain.com
496
500
        */
497
 
        co->tailmatch=(bool)strequal(ptr, "TRUE"); /* store information */
 
501
        co->tailmatch=(bool)Curl_raw_equal(ptr, "TRUE"); /* store information */
498
502
        break;
499
503
      case 2:
500
504
        /* It turns out, that sometimes the file format allows the path
514
518
        fields++; /* add a field and fall down to secure */
515
519
        /* FALLTHROUGH */
516
520
      case 3:
517
 
        co->secure = (bool)strequal(ptr, "TRUE");
 
521
        co->secure = (bool)Curl_raw_equal(ptr, "TRUE");
518
522
        break;
519
523
      case 4:
520
524
        co->expires = curlx_strtoofft(ptr, NULL, 10);
567
571
  clist = c->cookies;
568
572
  replace_old = FALSE;
569
573
  while(clist) {
570
 
    if(strequal(clist->name, co->name)) {
 
574
    if(Curl_raw_equal(clist->name, co->name)) {
571
575
      /* the names are identical */
572
576
 
573
577
      if(clist->domain && co->domain) {
574
 
        if(strequal(clist->domain, co->domain))
 
578
        if(Curl_raw_equal(clist->domain, co->domain))
575
579
          /* The domains are identical */
576
580
          replace_old=TRUE;
577
581
      }
582
586
        /* the domains were identical */
583
587
 
584
588
        if(clist->path && co->path) {
585
 
          if(strequal(clist->path, co->path)) {
 
589
          if(Curl_raw_equal(clist->path, co->path)) {
586
590
            replace_old = TRUE;
587
591
          }
588
592
          else
610
614
        co->next = clist->next; /* get the next-pointer first */
611
615
 
612
616
        /* then free all the old pointers */
613
 
        if(clist->name)
614
 
          free(clist->name);
 
617
        free(clist->name);
615
618
        if(clist->value)
616
619
          free(clist->value);
617
620
        if(clist->domain)
683
686
 
684
687
  if(NULL == inc) {
685
688
    /* we didn't get a struct, create one */
686
 
    c = (struct CookieInfo *)calloc(1, sizeof(struct CookieInfo));
 
689
    c = calloc(1, sizeof(struct CookieInfo));
687
690
    if(!c)
688
691
      return NULL; /* failed to get memory */
689
692
    c->filename = strdup(file?file:"none"); /* copy the name just in case */
711
714
    char *lineptr;
712
715
    bool headerline;
713
716
 
714
 
    char *line = (char *)malloc(MAX_COOKIE_LINE);
 
717
    char *line = malloc(MAX_COOKIE_LINE);
715
718
    if(line) {
716
719
      while(fgets(line, MAX_COOKIE_LINE, fp)) {
717
720
        if(checkprefix("Set-Cookie:", line)) {
753
756
 
754
757
struct Cookie *Curl_cookie_getlist(struct CookieInfo *c,
755
758
                                   const char *host, const char *path,
756
 
                                   bool secure)
 
759
                                   bool secure)
757
760
{
758
761
  struct Cookie *newco;
759
762
  struct Cookie *co;
769
772
    /* only process this cookie if it is not expired or had no expire
770
773
       date AND that if the cookie requires we're secure we must only
771
774
       continue if we are! */
772
 
    if( (co->expires<=0 || (co->expires> now)) &&
 
775
    if( (!co->expires || (co->expires > now)) &&
773
776
        (co->secure?secure:TRUE) ) {
774
777
 
775
778
      /* now check if the domain is correct */
776
779
      if(!co->domain ||
777
780
         (co->tailmatch && tailmatch(co->domain, host)) ||
778
 
         (!co->tailmatch && strequal(host, co->domain)) ) {
 
781
         (!co->tailmatch && Curl_raw_equal(host, co->domain)) ) {
779
782
        /* the right part of the host matches the domain stuff in the
780
783
           cookie data */
781
784
 
789
792
          /* and now, we know this is a match and we should create an
790
793
             entry for the return-linked-list */
791
794
 
792
 
          newco = (struct Cookie *)malloc(sizeof(struct Cookie));
 
795
          newco = malloc(sizeof(struct Cookie));
793
796
          if(newco) {
794
797
            /* first, copy the whole source cookie: */
795
798
            memcpy(newco, co, sizeof(struct Cookie));
874
877
{
875
878
  struct Cookie *first, *curr, *next, *prev = NULL;
876
879
 
877
 
  if(!cookies->cookies)
 
880
  if(!cookies->cookies || !cookies->cookies)
878
881
    return;
879
882
 
880
883
  first = curr = prev = cookies->cookies;
990
993
    char *format_ptr;
991
994
 
992
995
    fputs("# Netscape HTTP Cookie File\n"
993
 
          "# http://curlm.haxx.se/rfc/cookie_spec.html\n"
 
996
          "# http://curl.haxx.se/rfc/cookie_spec.html\n"
994
997
          "# This file was generated by libcurl! Edit at your own risk.\n\n",
995
998
          out);
996
999
    co = c->cookies;
999
1002
      format_ptr = get_netscape_format(co);
1000
1003
      if(format_ptr == NULL) {
1001
1004
        fprintf(out, "#\n# Fatal libcurl error\n");
1002
 
        fclose(out);
 
1005
        if(!use_stdout)
 
1006
          fclose(out);
1003
1007
        return 1;
1004
1008
      }
1005
1009
      fprintf(out, "%s\n", format_ptr);