Coverage Report

Created: 2026-12-01 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/cookie.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
27
28
#include "urldata.h"
29
#include "cookie.h"
30
#include "psl.h"
31
#include "curl_trc.h"
32
#include "transfer.h"
33
#include "slist.h"
34
#include "curl_share.h"
35
#include "strcase.h"
36
#include "curl_fopen.h"
37
#include "curl_get_line.h"
38
#include "curl_memrchr.h"
39
#include "parsedate.h"
40
#include "curlx/strdup.h"
41
#include "curlx/dynbuf.h"
42
#include "llist.h"
43
#include "curlx/strparse.h"
44
45
/* number of seconds in 400 days */
46
0
#define COOKIES_MAXAGE (400 * 24 * 3600)
47
48
/* struct to hold information about a single cookie while parsing */
49
struct tempcookie {
50
  struct Curl_str name;
51
  struct Curl_str value;
52
  struct Curl_str domain;
53
  struct Curl_str path;
54
  curl_off_t expires;
55
  BIT(tailmatch);
56
  BIT(secure);
57
  BIT(httponly);
58
  BIT(prefix_secure);
59
  BIT(prefix_host);
60
};
61
62
/* Make sure cookies never expire further away in time than 400 days into the
63
   future. (from RFC6265bis draft-19)
64
65
   For the sake of easier testing, align the capped time to an even 60 second
66
   boundary. */
67
static void cap_expires(time_t now, struct tempcookie *co)
68
0
{
69
0
  if(co->expires && (TIME_T_MAX - COOKIES_MAXAGE - 30) > now) {
70
0
    timediff_t cap = now + COOKIES_MAXAGE;
71
0
    if(co->expires > cap) {
72
0
      cap += 30;
73
0
      co->expires = (cap / 60) * 60;
74
0
    }
75
0
  }
76
0
}
77
78
static bool cookie_tailmatch(const char *cookie_domain,
79
                             const size_t cookie_domain_len,
80
                             const char *hostname,
81
                             const size_t hostname_len)
82
0
{
83
0
  if(hostname_len < cookie_domain_len)
84
0
    return FALSE;
85
86
0
  if(!curl_strnequal(cookie_domain,
87
0
                     hostname + hostname_len - cookie_domain_len,
88
0
                     cookie_domain_len))
89
0
    return FALSE;
90
91
  /*
92
   * A lead char of cookie_domain is not '.'.
93
   * RFC6265 4.1.2.3. The Domain Attribute says:
94
   * For example, if the value of the Domain attribute is
95
   * "example.com", the user agent will include the cookie in the Cookie
96
   * header when making HTTP requests to example.com, www.example.com, and
97
   * www.corp.example.com.
98
   */
99
0
  if(hostname_len == cookie_domain_len)
100
0
    return TRUE;
101
0
  if('.' == *(hostname + hostname_len - cookie_domain_len - 1))
102
0
    return TRUE;
103
0
  return FALSE;
104
0
}
105
106
/*
107
 * matching cookie path and URL path
108
 * RFC6265 5.1.4 Paths and Path-Match
109
 */
110
static bool pathmatch(const char *cookie_path, const char *uri_path)
111
0
{
112
0
  size_t cookie_path_len;
113
0
  size_t uri_path_len;
114
0
  bool ret = FALSE;
115
116
  /* cookie_path must not have last '/' separator. ex: /sample */
117
0
  cookie_path_len = strlen(cookie_path);
118
0
  if(cookie_path_len == 1) {
119
    /* cookie_path must be '/' */
120
0
    return TRUE;
121
0
  }
122
123
  /* #-fragments are already cut off! */
124
0
  if(strlen(uri_path) == 0 || uri_path[0] != '/')
125
0
    uri_path = "/";
126
127
  /*
128
   * here, RFC6265 5.1.4 says
129
   *  4. Output the characters of the uri-path from the first character up
130
   *     to, but not including, the right-most %x2F ("/").
131
   *  but URL path /hoge?fuga=xxx means /hoge/index.cgi?fuga=xxx in some site
132
   *  without redirect.
133
   *  Ignore this algorithm because /hoge is uri path for this case
134
   *  (uri path is not /).
135
   */
136
137
0
  uri_path_len = strlen(uri_path);
138
139
0
  if(uri_path_len < cookie_path_len)
140
0
    goto pathmatched;
141
142
  /* not using checkprefix() because matching should be case-sensitive */
143
0
  if(strncmp(cookie_path, uri_path, cookie_path_len))
144
0
    goto pathmatched;
145
146
  /* The cookie-path and the uri-path are identical. */
147
0
  if(cookie_path_len == uri_path_len) {
148
0
    ret = TRUE;
149
0
    goto pathmatched;
150
0
  }
151
152
  /* here, cookie_path_len < uri_path_len */
153
0
  if(uri_path[cookie_path_len] == '/') {
154
0
    ret = TRUE;
155
0
    goto pathmatched;
156
0
  }
157
158
0
pathmatched:
159
0
  return ret;
160
0
}
161
162
/*
163
 * Return the top-level domain, for optimal hashing.
164
 */
165
static const char *get_top_domain(const struct Curl_str *domain,
166
                                  size_t *outlen)
167
0
{
168
0
  size_t len = curlx_strlen(domain);
169
0
  const char *first = NULL, *last;
170
0
  const char *domainp = curlx_str(domain);
171
172
0
  if(len) {
173
0
    last = memrchr(domainp, '.', len);
174
0
    if(last) {
175
0
      first = memrchr(domainp, '.', (last - domainp));
176
0
      if(first)
177
0
        len -= (++first - domainp);
178
0
    }
179
0
  }
180
181
0
  if(outlen)
182
0
    *outlen = len;
183
184
0
  return first ? first : domainp;
185
0
}
186
187
/* Avoid C1001, an "internal error" with MSVC14 */
188
#if defined(_MSC_VER) && (_MSC_VER == 1900)
189
#pragma optimize("", off)
190
#endif
191
192
/*
193
 * A case-insensitive hash for the cookie domains.
194
 */
195
static size_t cookie_hash_domain(const char *domain, const size_t len)
196
0
{
197
0
  const char *end = domain + len;
198
0
  size_t h = 5381;
199
200
0
  while(domain < end) {
201
0
    size_t j = (size_t)Curl_raw_toupper(*domain++);
202
0
    h += h << 5;
203
0
    h ^= j;
204
0
  }
205
206
0
  return (h % COOKIE_HASH_SIZE);
207
0
}
208
209
#if defined(_MSC_VER) && (_MSC_VER == 1900)
210
#pragma optimize("", on)
211
#endif
212
213
/*
214
 * Hash this domain. Must not be IP address only.
215
 */
216
static size_t cookiehash(const struct Curl_str *domain)
217
0
{
218
0
  const char *top;
219
0
  size_t len;
220
221
0
  if(!curlx_strlen(domain))
222
0
    return 0;
223
224
0
  top = get_top_domain(domain, &len);
225
0
  return cookie_hash_domain(top, len);
226
0
}
227
228
/*
229
 * cookie path sanitize. No alloc or strdup.
230
 */
231
static void sanitize_cookie_path(struct Curl_str *dest,
232
                                 const char *cookie_path, size_t len)
233
0
{
234
  /* some sites send path attribute within '"'. */
235
0
  if(len && (cookie_path[0] == '\"')) {
236
0
    cookie_path++;
237
0
    len--;
238
239
0
    if(len && (cookie_path[len - 1] == '\"'))
240
0
      len--;
241
0
  }
242
243
  /* RFC6265 5.2.4 The Path Attribute */
244
0
  if(!len || (cookie_path[0] != '/'))
245
    /* Let cookie-path be the default-path. */
246
0
    curlx_str_assign(dest, "/", 1);
247
0
  else {
248
    /* remove trailing slash when path is non-empty */
249
0
    if(len > 1 && cookie_path[len - 1] == '/')
250
0
      len--;
251
252
0
    curlx_str_assign(dest, cookie_path, len);
253
0
  }
254
0
}
255
256
/*
257
 * strstore
258
 *
259
 * Store pointers and length. No malloc or strdup.
260
 */
261
static void strstore(struct Curl_str *dest, const char *str, size_t len)
262
0
{
263
0
  DEBUGASSERT(dest);
264
0
  curlx_str_assign(dest, str, len);
265
0
}
266
267
/*
268
 * remove_expired
269
 *
270
 * Remove expired cookies from the hash by inspecting the expires timestamp on
271
 * each cookie in the hash, freeing and deleting any where the timestamp is in
272
 * the past. If the cookiejar has recorded the next timestamp at which one or
273
 * more cookies expire, then processing will exit early in case this timestamp
274
 * is in the future.
275
 */
276
static void remove_expired(struct CookieInfo *ci)
277
5.43k
{
278
5.43k
  struct Cookie *co;
279
5.43k
  curl_off_t now;
280
5.43k
  unsigned int i;
281
282
5.43k
  if(!ci->numcookies)
283
5.43k
    return;
284
285
0
  now = (curl_off_t)time(NULL);
286
  /*
287
   * If the earliest expiration timestamp in the jar is in the future we can
288
   * skip scanning the whole jar and instead exit early as there will not be
289
   * any cookies to evict. If we need to evict, reset the next_expiration
290
   * counter in order to track the next one. In case the recorded first
291
   * expiration is the max offset, then perform the safe fallback of checking
292
   * all cookies.
293
   */
294
0
  if(now < ci->next_expiration &&
295
0
     ci->next_expiration != CURL_OFF_T_MAX)
296
0
    return;
297
0
  else
298
0
    ci->next_expiration = CURL_OFF_T_MAX;
299
300
0
  for(i = 0; i < COOKIE_HASH_SIZE; i++) {
301
0
    struct Curl_llist_node *n;
302
0
    struct Curl_llist_node *e = NULL;
303
304
0
    for(n = Curl_llist_head(&ci->cookielist[i]); n; n = e) {
305
0
      co = Curl_node_elem(n);
306
0
      e = Curl_node_next(n);
307
0
      if(co->expires) {
308
0
        if(co->expires < now) {
309
0
          Curl_node_remove(n);
310
0
          curlx_free(co);
311
0
          ci->numcookies--;
312
0
        }
313
0
        else if(co->expires < ci->next_expiration)
314
          /*
315
           * If this cookie has an expiration timestamp earlier than what we
316
           * have seen so far then record it for the next round of expirations.
317
           */
318
0
          ci->next_expiration = co->expires;
319
0
      }
320
0
    }
321
0
  }
322
0
}
323
324
#ifndef USE_LIBPSL
325
/* Make sure domain contains a dot or is localhost. */
326
static bool bad_domain(const char *domain, size_t len)
327
0
{
328
0
  if((len == 9) && curl_strnequal(domain, "localhost", 9))
329
0
    return FALSE;
330
0
  else {
331
    /* there must be a dot present, but that dot must not be a trailing dot */
332
0
    const char *dot = memchr(domain, '.', len);
333
0
    if(dot) {
334
0
      size_t i = dot - domain;
335
0
      if((len - i) > 1)
336
        /* the dot is not the last byte */
337
0
        return FALSE;
338
0
    }
339
0
  }
340
0
  return TRUE;
341
0
}
342
#endif
343
344
/* RFC 6265 section 4.1.1 says a server should accept this range:
345
346
   cookie-octet    = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
347
348
   Yet, Firefox and Chrome as of June 2022 accept space, comma and
349
   double-quotes fine. The prime reason for filtering out control bytes is that
350
   some HTTP servers return 400 for requests that contain such.
351
 */
352
static bool invalid_octets(const char *ptr, size_t len)
353
0
{
354
0
  const unsigned char *p = (const unsigned char *)ptr;
355
  /* Reject all bytes \x01 - \x1f + \x7f */
356
0
  while(len--) {
357
0
    if((*p < 0x20) || (*p == 0x7f))
358
0
      return TRUE;
359
0
    p++;
360
0
  }
361
0
  return FALSE;
362
0
}
363
364
/* The maximum length we accept a date string for the 'expire' keyword. The
365
   standard date formats are within the 30 bytes range. This adds an extra
366
   margin to make sure it realistically works with what is used out there. */
367
0
#define MAX_DATE_LENGTH 80
368
369
0
#define COOKIE_NAME   0
370
0
#define COOKIE_VALUE  1
371
0
#define COOKIE_DOMAIN 2
372
0
#define COOKIE_PATH   3
373
374
#define COOKIE_PIECES 4 /* the list above */
375
376
static void storecookie(struct tempcookie *co, const struct Curl_str *cp,
377
                        const char *path, const char *domain)
378
0
{
379
0
  size_t plen = 0;
380
0
  strstore(&co->name, curlx_str(&cp[COOKIE_NAME]),
381
0
           curlx_strlen(&cp[COOKIE_NAME]));
382
0
  strstore(&co->value, curlx_str(&cp[COOKIE_VALUE]),
383
0
           curlx_strlen(&cp[COOKIE_VALUE]));
384
385
0
  if(curlx_strlen(&cp[COOKIE_PATH])) {
386
0
    path = curlx_str(&cp[COOKIE_PATH]);
387
0
    plen = curlx_strlen(&cp[COOKIE_PATH]);
388
0
  }
389
0
  else if(path) {
390
    /* No path was given in the header line, set the default */
391
0
    const char *endslash = strrchr(path, '/');
392
0
    if(endslash)
393
0
      plen = endslash - path + 1; /* include end slash */
394
0
    else
395
0
      plen = strlen(path);
396
0
  }
397
398
0
  sanitize_cookie_path(&co->path, path, plen);
399
400
0
  if(curlx_strlen(&cp[COOKIE_DOMAIN]))
401
0
    strstore(&co->domain, curlx_str(&cp[COOKIE_DOMAIN]),
402
0
             curlx_strlen(&cp[COOKIE_DOMAIN]));
403
0
  else if(domain) {
404
    /* no domain was given in the header line, set the default */
405
0
    strstore(&co->domain, domain, strlen(domain));
406
0
  }
407
0
}
408
409
/*
410
 * Parse the first name/value pair of the cookie header, which is the actual
411
 * cookie name and value.
412
 */
413
static bool parse_first_pair(struct Curl_easy *data, struct tempcookie *co,
414
                             struct Curl_str *cookie,
415
                             struct Curl_str *name,
416
                             struct Curl_str *val,
417
                             bool sep)
418
0
{
419
  /* The first name/value pair is the actual cookie name */
420
0
  if(!sep || !curlx_strlen(name)) {
421
0
    infof(data, "invalid cookie, dropped");
422
0
    return FALSE;
423
0
  }
424
425
  /*
426
   * Check for too long individual name or contents. Chrome and Firefox
427
   * support 4095 or 4096 bytes combo
428
   */
429
0
  if((curlx_strlen(name) + curlx_strlen(val)) > MAX_NAME) {
430
0
    infof(data, "oversized cookie dropped, name/val %zu + %zu bytes",
431
0
          curlx_strlen(name), curlx_strlen(val));
432
0
    return FALSE;
433
0
  }
434
435
  /* Check if we have a reserved prefix set. */
436
0
  if(!strncmp("__Secure-", curlx_str(name), 9))
437
0
    co->prefix_secure = TRUE;
438
0
  else if(!strncmp("__Host-", curlx_str(name), 7))
439
0
    co->prefix_host = TRUE;
440
441
0
  cookie[COOKIE_NAME] = *name;
442
0
  cookie[COOKIE_VALUE] = *val;
443
0
  return TRUE;
444
0
}
445
446
static bool parse_domain(struct Curl_easy *data, struct tempcookie *co,
447
                         struct Curl_str *cookie_domain,
448
                         struct Curl_str *val,
449
                         const char **domainp)
450
0
{
451
0
  bool is_ip;
452
0
  const char *domain = *domainp;
453
0
  const char *v = curlx_str(val);
454
  /*
455
   * Now, we make sure that our host is within the given domain, or
456
   * the given domain is not valid and thus cannot be set.
457
   */
458
459
0
  if('.' == *v)
460
0
    curlx_str_nudge(val, 1);
461
462
0
#ifndef USE_LIBPSL
463
  /*
464
   * Without PSL we do not know when the incoming cookie is set on a
465
   * TLD or otherwise "protected" suffix. To reduce risk, we require a
466
   * dot OR the exact hostname being "localhost".
467
   */
468
0
  if(bad_domain(curlx_str(val), curlx_strlen(val))) {
469
0
    *domainp = ":";
470
0
    domain = ":";
471
0
  }
472
0
#endif
473
474
0
  is_ip = Curl_host_is_ipnum(domain ? domain : curlx_str(val));
475
476
0
  if(!domain ||
477
0
     (is_ip &&
478
0
      !strncmp(curlx_str(val), domain, curlx_strlen(val)) &&
479
0
      (curlx_strlen(val) == strlen(domain))) ||
480
0
     (!is_ip && cookie_tailmatch(curlx_str(val),
481
0
                                 curlx_strlen(val), domain, strlen(domain)))) {
482
0
    *cookie_domain = *val;
483
0
    if(!is_ip)
484
0
      co->tailmatch = TRUE; /* we always do that if the domain name was
485
                               given */
486
0
  }
487
0
  else {
488
    /*
489
     * We did not get a tailmatch and then the attempted set domain is
490
     * not a domain to which the current host belongs. Mark as bad.
491
     */
492
0
    infof(data, "skipped cookie with bad tailmatch domain: %s",
493
0
          curlx_str(val));
494
0
    return FALSE;
495
0
  }
496
0
  return TRUE;
497
0
}
498
499
static void parse_maxage(struct tempcookie *co, struct Curl_str *val,
500
                         time_t *nowp)
501
0
{
502
0
  int rc;
503
0
  const char *maxage = curlx_str(val);
504
0
  if(*maxage == '\"')
505
0
    maxage++;
506
0
  rc = curlx_str_number(&maxage, &co->expires, CURL_OFF_T_MAX);
507
0
  if(!*nowp)
508
0
    *nowp = time(NULL);
509
0
  switch(rc) {
510
0
  case STRE_OVERFLOW:
511
    /* overflow, used max value */
512
0
    co->expires = CURL_OFF_T_MAX;
513
0
    break;
514
0
  default:
515
    /* negative or otherwise bad, expire */
516
0
    co->expires = 1;
517
0
    break;
518
0
  case STRE_OK:
519
0
    if(!co->expires)
520
0
      co->expires = 1; /* expire now */
521
0
    else if(CURL_OFF_T_MAX - *nowp < co->expires)
522
      /* would overflow */
523
0
      co->expires = CURL_OFF_T_MAX;
524
0
    else
525
0
      co->expires += *nowp;
526
0
    break;
527
0
  }
528
0
  cap_expires(*nowp, co);
529
0
}
530
531
static void parse_expires(struct tempcookie *co, struct Curl_str *val,
532
                          time_t *nowp)
533
0
{
534
  /*
535
   * Let max-age have priority.
536
   *
537
   * If the date cannot get parsed for whatever reason, the cookie
538
   * will be treated as a session cookie
539
   */
540
0
  if(!co->expires && (curlx_strlen(val) < MAX_DATE_LENGTH)) {
541
0
    char dbuf[MAX_DATE_LENGTH + 1];
542
0
    time_t date = 0;
543
0
    memcpy(dbuf, curlx_str(val), curlx_strlen(val));
544
0
    dbuf[curlx_strlen(val)] = 0;
545
0
    if(!Curl_getdate_capped(dbuf, &date)) {
546
0
      if(!date)
547
0
        date++;
548
0
      co->expires = (curl_off_t)date;
549
0
    }
550
0
    else
551
0
      co->expires = 0;
552
0
    if(!*nowp)
553
0
      *nowp = time(NULL);
554
0
    cap_expires(*nowp, co);
555
0
  }
556
0
}
557
558
/* this function returns errors on OOM etc, not for cookie format problems */
559
static CURLcode
560
parse_cookie_header(struct Curl_easy *data,
561
                    struct tempcookie *co,
562
                    bool *okay, /* if the cookie was fine */
563
                    const char *ptr, /* the header */
564
                    const char *domain, /* default domain */
565
                    /* full path used when this cookie is set */
566
                    const char *path,
567
                    bool secure_origin)
568
0
{
569
  /* This line was read off an HTTP-header */
570
0
  time_t now = 0;
571
0
  CURLcode result = CURLE_OK;
572
0
  struct Curl_str cookie[COOKIE_PIECES];
573
0
  *okay = FALSE;
574
575
  /* memset instead of initializer because gcc 4.8.1 is silly */
576
0
  memset(cookie, 0, sizeof(cookie));
577
0
  if(strlen(ptr) > MAX_COOKIE_LINE)
578
0
    return CURLE_OK;
579
580
0
  while(*ptr && (*ptr != '\r') && (*ptr != '\n')) {
581
0
    struct Curl_str name;
582
0
    struct Curl_str val;
583
0
    bool sep = FALSE;
584
0
    bool bad_octets = FALSE;
585
0
    bool got_tab = FALSE;
586
0
    const char *namestart, *nameend;
587
588
    /* skip leading blanks */
589
0
    while(ISBLANK(*ptr))
590
0
      ptr++;
591
592
0
    namestart = ptr;
593
0
    nameend = ptr;
594
0
    while(*ptr && (*ptr != '=') && (*ptr != ';') && !ISNEWLINE(*ptr)) {
595
0
      unsigned char c = (unsigned char)*ptr++;
596
0
      if(c == 0x09)
597
0
        got_tab = TRUE;
598
0
      else if((c < 0x20) || (c == 0x7f))
599
0
        bad_octets = TRUE;
600
0
      else if(got_tab && !ISBLANK(c))
601
        /* tabs are ok last, but not within the name */
602
0
        bad_octets = TRUE;
603
0
      if(!ISBLANK(c))
604
0
        nameend = ptr;
605
0
    }
606
607
0
    if(bad_octets) {
608
0
      infof(data, "invalid octets in name, cookie dropped");
609
0
      return CURLE_OK;
610
0
    }
611
612
0
    curlx_str_assign(&name, namestart, nameend - namestart);
613
614
0
    if(*ptr == '=') {
615
0
      const char *valstart, *valend;
616
0
      got_tab = FALSE;
617
0
      ptr++;
618
0
      sep = TRUE;
619
0
      while(ISBLANK(*ptr))
620
0
        ptr++;
621
0
      valstart = ptr;
622
0
      valend = ptr;
623
0
      while(*ptr && (*ptr != ';') && !ISNEWLINE(*ptr)) {
624
0
        unsigned char c = (unsigned char)*ptr++;
625
0
        if(c == 0x09)
626
0
          got_tab = TRUE;
627
0
        else if((c < 0x20) || (c == 0x7f))
628
0
          bad_octets = TRUE;
629
0
        else if(got_tab && !ISBLANK(c))
630
          /* tabs are ok last, but not within the value */
631
0
          bad_octets = TRUE;
632
0
        if(!ISBLANK(c))
633
0
          valend = ptr;
634
0
      }
635
0
      if(bad_octets) {
636
0
        infof(data, "invalid octets in value, cookie dropped");
637
0
        return CURLE_OK;
638
0
      }
639
0
      curlx_str_assign(&val, valstart, (size_t)(valend - valstart));
640
0
    }
641
0
    else
642
0
      curlx_str_init(&val);
643
644
0
    if(!curlx_strlen(&cookie[COOKIE_NAME])) {
645
0
      if(!parse_first_pair(data, co, cookie, &name, &val, sep))
646
0
        return CURLE_OK;
647
0
    }
648
0
    else {
649
0
      size_t nlen = curlx_strlen(&name);
650
0
      const char *nstr = curlx_str(&name);
651
0
      switch(nlen) {
652
0
      case 4: /* path */
653
0
        if(sep && curl_strnequal(nstr, "path", 4))
654
0
          cookie[COOKIE_PATH] = val;
655
0
        break;
656
0
      case 6: /* secure, domain */
657
0
        if(curl_strnequal(nstr, "secure", 6)) {
658
0
          if(secure_origin)
659
0
            co->secure = TRUE;
660
0
          else {
661
0
            infof(data, "skipped cookie because not 'secure'");
662
0
            return CURLE_OK;
663
0
          }
664
0
        }
665
0
        else if(sep && curl_strnequal(nstr, "domain", 6)) {
666
0
          if(curlx_strlen(&val) &&
667
0
             !parse_domain(data, co, &cookie[COOKIE_DOMAIN], &val, &domain))
668
0
            return CURLE_OK;
669
0
        }
670
0
        break;
671
0
      case 7: /* max-age, expires */
672
0
        if(sep && curlx_strlen(&val)) {
673
0
          if(curl_strnequal(nstr, "max-age", 7))
674
0
            parse_maxage(co, &val, &now);
675
0
          else if(curl_strnequal(nstr, "expires", 7))
676
0
            parse_expires(co, &val, &now);
677
0
        }
678
0
        break;
679
0
      case 8: /* httponly */
680
0
        if(curl_strnequal(nstr, "httponly", 8))
681
0
          co->httponly = TRUE;
682
0
        break;
683
0
      }
684
0
    }
685
686
0
    if(*ptr == ';')
687
0
      ptr++;
688
0
    else
689
0
      break;
690
0
  }
691
692
0
  if(curlx_strlen(&cookie[COOKIE_NAME])) {
693
    /* the header was fine, now store the data */
694
0
    storecookie(co, &cookie[0], path, domain);
695
0
    *okay = TRUE;
696
0
  }
697
0
  return result;
698
0
}
699
700
static CURLcode parse_netscape(struct tempcookie *co,
701
                               const struct CookieInfo *ci,
702
                               bool *okay,
703
                               const char *lineptr,
704
                               bool secure_origin)
705
2.71k
{
706
  /*
707
   * This line is NOT an HTTP header style line, we do offer support for
708
   * reading the odd netscape cookies-file format here
709
   */
710
2.71k
  const char *ptr, *next;
711
2.71k
  int fields;
712
2.71k
  size_t len;
713
2.71k
  *okay = FALSE;
714
715
  /*
716
   * In 2008, Internet Explorer introduced HTTP-only cookies to prevent XSS
717
   * attacks. Cookies marked httpOnly are not accessible to JavaScript. In
718
   * Firefox's cookie files, they are prefixed #HttpOnly_ and the rest
719
   * remains as usual, so we skip 10 characters of the line.
720
   */
721
2.71k
  if(!strncmp(lineptr, "#HttpOnly_", 10)) {
722
0
    lineptr += 10;
723
0
    co->httponly = TRUE;
724
0
  }
725
726
2.71k
  if(lineptr[0] == '#')
727
    /* do not even try the comments */
728
0
    return CURLE_OK;
729
730
  /*
731
   * Now loop through the fields and init the struct we already have
732
   * allocated
733
   */
734
2.71k
  fields = 0;
735
2.71k
  for(next = lineptr; next; fields++) {
736
2.71k
    ptr = next;
737
2.71k
    len = strcspn(ptr, "\t\r\n");
738
2.71k
    next = (ptr[len] == '\t' ? &ptr[len + 1] : NULL);
739
2.71k
    switch(fields) {
740
2.71k
    case 0:
741
2.71k
      if(ptr[0] == '.') { /* skip preceding dots */
742
0
        ptr++;
743
0
        len--;
744
0
      }
745
2.71k
      if(!len)
746
2.71k
        return CURLE_OK;
747
0
      strstore(&co->domain, ptr, len);
748
0
      break;
749
0
    case 1:
750
      /*
751
       * flag: A TRUE/FALSE value indicating if all machines within a given
752
       * domain can access the variable. Set TRUE when the cookie says
753
       * .example.com and to false when the domain is complete www.example.com
754
       */
755
0
      co->tailmatch = !!curl_strnequal(ptr, "TRUE", len);
756
0
      break;
757
0
    case 2:
758
      /* The file format allows the path field to remain not filled in */
759
0
      if(strncmp("TRUE", ptr, len) && strncmp("FALSE", ptr, len)) {
760
        /* only if the path does not look like a boolean option! */
761
0
        sanitize_cookie_path(&co->path, ptr, len);
762
0
        break;
763
0
      }
764
0
      else {
765
        /* this does not look like a path, make one up! */
766
0
        strstore(&co->path, "/", 1);
767
0
      }
768
0
      fields++; /* add a field and fall down to secure */
769
0
      FALLTHROUGH();
770
0
    case 3:
771
0
      co->secure = FALSE;
772
0
      if(curl_strnequal(ptr, "TRUE", len)) {
773
0
        if(secure_origin || ci->running)
774
0
          co->secure = TRUE;
775
0
        else
776
0
          return CURLE_OK;
777
0
      }
778
0
      break;
779
0
    case 4:
780
0
      if(curlx_str_number(&ptr, &co->expires, CURL_OFF_T_MAX))
781
0
        return CURLE_OK;
782
0
      break;
783
0
    case 5:
784
0
      if(!len)
785
        /* reject blank names */
786
0
        return CURLE_OK;
787
0
      strstore(&co->name, ptr, len);
788
789
      /* For Netscape file format cookies we check prefix on the name. These
790
         prefixes are matched case sensitively, same as on the header path and
791
         as the 6265bis document specifies. */
792
0
      if(len >= 9 && !memcmp("__Secure-", ptr, 9))
793
0
        co->prefix_secure = TRUE;
794
0
      else if(len >= 7 && !memcmp("__Host-", ptr, 7))
795
0
        co->prefix_host = TRUE;
796
797
0
      break;
798
0
    case 6:
799
0
      strstore(&co->value, ptr, len);
800
0
      break;
801
2.71k
    }
802
2.71k
  }
803
0
  if(fields == 6) {
804
    /* we got a cookie with blank contents, fix it */
805
0
    strstore(&co->value, "", 0);
806
0
    fields++;
807
0
  }
808
809
0
  if(fields != 7)
810
    /* we did not find the sufficient number of fields */
811
0
    return CURLE_OK;
812
813
  /* Reject control octets in the name or value, matching the filtering done
814
     for cookies set over HTTP. A cookie loaded from a file is later sent in
815
     request headers, so the same bytes that make a server reject a request
816
     must not slip in through the file. */
817
0
  if(invalid_octets(curlx_str(&co->name), curlx_strlen(&co->name)) ||
818
0
     invalid_octets(curlx_str(&co->value), curlx_strlen(&co->value)))
819
0
    return CURLE_OK;
820
821
0
  *okay = TRUE;
822
0
  return CURLE_OK;
823
0
}
824
825
/* called with a hostname that is non-zero length */
826
static bool host_is_ipnum(struct Curl_str *host)
827
0
{
828
0
  const char *hp = curlx_str(host);
829
0
  size_t len = curlx_strlen(host);
830
0
  char temp[MAX_IPADR_LEN]; /* the define includes the null terminator */
831
0
  if((!ISXDIGIT(hp[0]) && (hp[0] != ':')) ||
832
0
     (!ISXDIGIT(hp[len - 1]) && (hp[len - 1] != ':')) ||
833
0
     (!ISDIGIT(hp[0]) && !memchr(hp, ':', len)) ||
834
0
     (len >= MAX_IPADR_LEN))
835
0
    return FALSE;
836
0
  curl_msnprintf(temp, sizeof(temp), "%.*s", (int)len, hp);
837
0
  return Curl_host_is_ipnum(temp);
838
0
}
839
840
static bool is_public_suffix(struct Curl_easy *data,
841
                             struct tempcookie *co,
842
                             const char *domain)
843
0
{
844
#ifdef USE_LIBPSL
845
  /*
846
   * Check if the domain is a Public Suffix and if yes, ignore the cookie.
847
   * 'domain' is NULL when the cookie is loaded from file or
848
   * CURLOPT_COOKIELIST.
849
   */
850
  DEBUGASSERT(data);
851
  DEBUGASSERT(co);
852
  DEBUGF(infof(data, "PSL check set-cookie '%.*s' for domain='%.*s' in %s",
853
               (int)curlx_strlen(&co->name), curlx_str(&co->name),
854
               (int)curlx_strlen(&co->domain), curlx_str(&co->domain),
855
               domain ? domain : "[file]"));
856
  if(!curlx_strlen(&co->domain))
857
    return FALSE;
858
859
  else {
860
    bool acceptable = FALSE;
861
    char lcase[256];
862
    char lcookie[256];
863
    size_t dlen = domain ? strlen(domain) : 0;
864
    size_t clen = curlx_strlen(&co->domain);
865
    const char *codomain = curlx_str(&co->domain);
866
867
    /* trim trailing dots */
868
    if(dlen && (domain[dlen - 1] == '.'))
869
      dlen--;
870
    if(clen && (codomain[clen - 1] == '.'))
871
      clen--;
872
873
    if((dlen < sizeof(lcase)) && (clen < sizeof(lcookie))) {
874
      const psl_ctx_t *psl = Curl_psl_use(data);
875
      if(psl) {
876
        /* the PSL check requires lowercase domain name and pattern */
877
        Curl_strntolower(lcookie, codomain, clen);
878
        lcookie[clen] = 0;
879
        if(domain) {
880
          Curl_strntolower(lcase, domain, dlen);
881
          lcase[dlen] = 0;
882
          acceptable = psl_is_cookie_domain_acceptable(psl, lcase, lcookie);
883
884
          /* if the cookie is acceptable, but is set for a PSL domain, then it
885
             cannot be tailmatching */
886
          if(acceptable && co->tailmatch &&
887
             (dlen == clen) &&
888
             curl_strnequal(codomain, domain, dlen) &&
889
             psl_is_public_suffix(psl, lcookie))
890
            co->tailmatch = FALSE;
891
        }
892
        else {
893
          bool suffix = psl_is_public_suffix(psl, lcookie);
894
          acceptable = TRUE;
895
          if(suffix)
896
            /* switch off tailmatching when set for a PSL domain */
897
            co->tailmatch = FALSE;
898
        }
899
        Curl_psl_release(data);
900
      }
901
      else
902
        infof(data, "libpsl problem, rejecting cookie for safety");
903
    }
904
905
    if(!acceptable) {
906
      infof(data, "cookie '%.*s' dropped, domain '%s' must not "
907
            "set cookies for '%.*s'",
908
            (int)curlx_strlen(&co->name), curlx_str(&co->name),
909
            domain ? domain : "[file]", (int)clen, codomain);
910
      return TRUE;
911
    }
912
  }
913
#else
914
0
  (void)data;
915
0
  (void)co;
916
0
  (void)domain;
917
0
  DEBUGF(infof(data, "NO PSL to check set-cookie '%.*s' for domain=%.*s in %s",
918
0
               (int)curlx_strlen(&co->name), curlx_str(&co->name),
919
0
               (int)curlx_strlen(&co->domain), curlx_str(&co->domain),
920
0
               domain ? domain : "[file]"));
921
0
#endif
922
0
  return FALSE;
923
0
}
924
925
/* returns TRUE when the cookie is accepted, and sets '*replacep' to the
926
   cookie to replace (or NULL if not replaced) */
927
static bool cookie_accept(struct Curl_easy *data,
928
                          const struct tempcookie *co,
929
                          const struct CookieInfo *ci,
930
                          const size_t myhash,
931
                          const int flags,
932
                          struct Cookie **replacep)
933
0
{
934
0
  bool replace_old = FALSE;
935
0
  struct Curl_llist_node *replace_n = NULL;
936
0
  struct Curl_llist_node *n;
937
0
  const char *name = curlx_str(&co->name);
938
0
  const size_t nlen = curlx_strlen(&co->name);
939
0
  const char *domain = curlx_str(&co->domain);
940
0
  const size_t dlen = curlx_strlen(&co->domain);
941
0
  for(n = Curl_llist_head(&ci->cookielist[myhash]); n; n = Curl_node_next(n)) {
942
0
    struct Cookie *clist = Curl_node_elem(n);
943
0
    if((clist->name[0] == name[0]) && (nlen == strlen(clist->name)) &&
944
0
       !memcmp(clist->name, name, nlen)) {
945
      /* the names are identical */
946
0
      if(clist->secure && !co->secure && !(flags & COOKIE_SECURE) &&
947
0
         clist->path && curlx_strlen(&co->path)) {
948
0
        bool matching_domains = FALSE;
949
950
0
        if(clist->domain && dlen) {
951
0
          if(cookie_tailmatch(clist->domain, strlen(clist->domain),
952
0
                              domain, dlen) ||
953
0
             cookie_tailmatch(domain, dlen,
954
0
                              clist->domain, strlen(clist->domain)))
955
            /* The existing one is a tail of the new or vice versa */
956
0
            matching_domains = TRUE;
957
0
        }
958
0
        else if(!clist->domain && !dlen)
959
0
          matching_domains = TRUE;
960
961
0
        if(matching_domains) {
962
0
          size_t cllen;
963
0
          const char *sep = NULL;
964
965
          /*
966
           * A non-secure cookie may not overlay an existing secure cookie.
967
           * For an existing cookie "a" with path "/login", refuse a new
968
           * cookie "a" with for example path "/login/en", while the path
969
           * "/loginhelper" is ok.
970
           */
971
972
0
          DEBUGASSERT(clist->path[0]);
973
0
          if(clist->path[0])
974
0
            sep = strchr(clist->path + 1, '/');
975
0
          if(sep)
976
0
            cllen = sep - clist->path;
977
0
          else
978
0
            cllen = strlen(clist->path);
979
980
0
          if((cllen <= curlx_strlen(&co->path)) &&
981
0
             !strncmp(clist->path, curlx_str(&co->path), cllen)) {
982
0
            infof(data, "cookie '%s' for domain '%.*s' dropped, would "
983
0
                  "overlay an existing cookie", clist->name,
984
0
                  (int)dlen, domain);
985
0
            return FALSE;
986
0
          }
987
0
        }
988
0
      }
989
990
0
      if(!replace_n) {
991
0
        if(clist->domain && (dlen == strlen(clist->domain))) {
992
0
          if((!dlen || curl_strnequal(clist->domain, domain, dlen)) &&
993
0
             (clist->tailmatch == co->tailmatch))
994
            /* The domains are identical */
995
0
            replace_old = TRUE;
996
0
        }
997
0
        else if(!clist->domain && !dlen)
998
0
          replace_old = TRUE;
999
1000
0
        if(replace_old) {
1001
          /* the domains were identical */
1002
0
          const char *path = curlx_str(&co->path);
1003
0
          const size_t plen = curlx_strlen(&co->path);
1004
1005
0
          if(clist->path && plen &&
1006
0
             ((strlen(clist->path) != plen) ||
1007
0
              memcmp(clist->path, path, plen)))
1008
0
            replace_old = FALSE;
1009
0
          else if(!clist->path || !plen)
1010
0
            replace_old = FALSE;
1011
0
          else if(!(flags & COOKIE_LIVE) && clist->livecookie) {
1012
            /*
1013
             * The already present cookie is "live", which means it was set
1014
             * from a header, while the new one was read from a file and thus
1015
             * is not "live". "live" cookies are preferred so the new cookie
1016
             * is freed.
1017
             */
1018
0
            return FALSE;
1019
0
          }
1020
0
          else
1021
0
            replace_n = n;
1022
0
        }
1023
0
      }
1024
0
    }
1025
0
  }
1026
0
  *replacep = replace_n ? Curl_node_elem(replace_n) : NULL;
1027
0
  return TRUE;
1028
0
}
1029
1030
static struct Cookie *cookiedup(struct tempcookie *co)
1031
0
{
1032
0
  struct Cookie *c;
1033
0
  size_t len = curlx_strlen(&co->name) +
1034
0
    curlx_strlen(&co->value) +
1035
0
    curlx_strlen(&co->domain) +
1036
0
    curlx_strlen(&co->path) + 4; /* one null terminator each */
1037
0
  char *area;
1038
0
  const char *p;
1039
1040
0
  c = curlx_calloc(1, sizeof(struct Cookie) + len);
1041
0
  if(!c)
1042
0
    return NULL;
1043
0
  c->expires = co->expires;
1044
0
  c->tailmatch = co->tailmatch;
1045
0
  c->secure = co->secure;
1046
0
  c->httponly = co->httponly;
1047
0
  c->prefix_secure = co->prefix_secure;
1048
0
  c->prefix_host = co->prefix_host;
1049
1050
0
  area = c->buf;
1051
1052
  /* copy the name */
1053
0
  memcpy(area, curlx_str(&co->name), curlx_strlen(&co->name));
1054
0
  area[curlx_strlen(&co->name)] = 0;
1055
0
  c->name = area;
1056
1057
  /* copy the value */
1058
0
  area += curlx_strlen(&co->name) + 1;
1059
0
  memcpy(area, curlx_str(&co->value), curlx_strlen(&co->value));
1060
0
  area[curlx_strlen(&co->value)] = 0;
1061
0
  c->value = area;
1062
1063
  /* copy the domain */
1064
0
  area += curlx_strlen(&co->value) + 1;
1065
0
  p = curlx_str(&co->domain);
1066
0
  memcpy(area, p ? p : "", curlx_strlen(&co->domain));
1067
0
  area[curlx_strlen(&co->domain)] = 0;
1068
0
  c->domain = area;
1069
1070
  /* copy the path */
1071
0
  area += curlx_strlen(&co->domain) + 1;
1072
0
  p = curlx_str(&co->path);
1073
0
  memcpy(area, p ? p : "", curlx_strlen(&co->path));
1074
0
  area[curlx_strlen(&co->path)] = 0;
1075
0
  c->path = area;
1076
1077
0
  return c;
1078
0
}
1079
1080
/*
1081
 * Curl_cookie_add
1082
 *
1083
 * Add a single cookie line to the cookie keeping object. Be aware that
1084
 * sometimes we get an IP-only hostname, and that might also be a numerical
1085
 * IPv6 address.
1086
 *
1087
 */
1088
CURLcode Curl_cookie_add(struct Curl_easy *data,
1089
                         struct CookieInfo *ci,
1090
                         const char *lineptr, /* first character of the line */
1091
                         const char *domain,  /* default domain */
1092
                         const char *path,    /* full path used when this
1093
                                                 cookie is set, used to get
1094
                                                 default path for the cookie
1095
                                                 unless set */
1096
                         const int flags)
1097
2.71k
{
1098
2.71k
  struct tempcookie cookie;
1099
2.71k
  struct Cookie *real;
1100
2.71k
  struct Cookie *replace = NULL;
1101
2.71k
  size_t myhash;
1102
2.71k
  CURLcode result;
1103
2.71k
  unsigned int replacetime = 0;
1104
2.71k
  bool okay;
1105
2.71k
  bool iponly;
1106
1107
2.71k
  DEBUGASSERT(data);
1108
2.71k
  DEBUGASSERT(MAX_SET_COOKIE_AMOUNT <= 255); /* counter is an unsigned char */
1109
2.71k
  if(data->req.setcookies >= MAX_SET_COOKIE_AMOUNT)
1110
0
    return CURLE_OK; /* silently ignore */
1111
1112
2.71k
  memset(&cookie, 0, sizeof(cookie));
1113
1114
2.71k
  if(flags & COOKIE_HTTPHEADER)
1115
0
    result = parse_cookie_header(data, &cookie, &okay,
1116
0
                                 lineptr, domain, path,
1117
0
                                 flags & COOKIE_SECURE);
1118
2.71k
  else
1119
2.71k
    result = parse_netscape(&cookie, ci, &okay, lineptr,
1120
2.71k
                            flags & COOKIE_SECURE);
1121
1122
2.71k
  if(result || !okay)
1123
2.71k
    return result;
1124
1125
0
  if(cookie.prefix_secure && !cookie.secure)
1126
    /* The __Secure- prefix only requires that the cookie be set secure */
1127
0
    return result;
1128
1129
0
  iponly = curlx_strlen(&cookie.domain) ? host_is_ipnum(&cookie.domain) :
1130
0
    FALSE;
1131
1132
0
  if(!iponly && !(flags & COOKIE_NOPSL) &&
1133
0
     is_public_suffix(data, &cookie, domain))
1134
0
    return result;
1135
1136
0
  if(cookie.prefix_host) {
1137
    /*
1138
     * The __Host- prefix requires the cookie to be secure, have a "/" path
1139
     * and not have a domain set.
1140
     */
1141
0
    if(cookie.secure && curlx_strlen(&cookie.path) &&
1142
0
       curlx_str_cmp(&cookie.path, "/") && !cookie.tailmatch)
1143
0
      ;
1144
0
    else
1145
0
      return result;
1146
0
  }
1147
1148
0
  if(!ci->running &&                /* read from a file */
1149
0
     (flags & COOKIE_NOSESSION) &&  /* clean session cookies */
1150
0
     !cookie.expires)               /* this is a session cookie */
1151
0
    return result;
1152
1153
  /*
1154
   * Now we have parsed the incoming line, we must now check if this supersedes
1155
   * an already existing cookie, which it may if the previous have the same
1156
   * domain and path as this.
1157
   */
1158
0
  if(ci->running)
1159
0
    remove_expired(ci);
1160
1161
0
  myhash = iponly ? 0 : cookiehash(&cookie.domain);
1162
0
  if(!cookie_accept(data, &cookie, ci, myhash, flags |
1163
0
                    (ci->running ? COOKIE_LIVE : 0), &replace))
1164
0
    return result;
1165
1166
  /* clone the temp cookie into a real one */
1167
0
  real = cookiedup(&cookie);
1168
0
  if(!real)
1169
0
    return CURLE_OUT_OF_MEMORY;
1170
1171
0
  if(replace) {
1172
0
    replacetime = replace->creationtime;
1173
0
    Curl_node_remove(&replace->node);
1174
0
    curlx_free(replace);
1175
0
  }
1176
1177
0
  real->livecookie = ci->running || (flags & COOKIE_LIVE);
1178
1179
  /* use the time from the cookie this replaces, or get a new one */
1180
0
  real->creationtime = replacetime ? replacetime : ++ci->lastct;
1181
1182
  /* add this cookie to the list */
1183
0
  Curl_llist_append(&ci->cookielist[myhash], real, &real->node);
1184
1185
0
  if(ci->running)
1186
    /* Only show this when NOT reading the cookies from a file */
1187
0
    infof(data, "%s cookie %s=\"%s\" for domain %s, path %s, "
1188
0
          "expire %" FMT_OFF_T,
1189
0
          replacetime ? "Replaced" : "Added", real->name, real->value,
1190
0
          real->domain, real->path, real->expires);
1191
1192
0
  if(!replacetime)
1193
0
    ci->numcookies++; /* one more cookie in the jar */
1194
1195
  /*
1196
   * Now that we have added a new cookie to the jar, update the expiration
1197
   * tracker in case it is the next one to expire.
1198
   */
1199
0
  if(cookie.expires && (cookie.expires < ci->next_expiration))
1200
0
    ci->next_expiration = cookie.expires;
1201
1202
0
  if(flags & COOKIE_HTTPHEADER)
1203
0
    data->req.setcookies++;
1204
1205
0
  return result;
1206
0
}
1207
1208
/*
1209
 * Curl_cookie_init()
1210
 *
1211
 * Inits a cookie struct to read data from a local file. This is always
1212
 * called before any cookies are set. File may be NULL in which case only the
1213
 * struct is initialized. Is file is "-" then STDIN is read.
1214
 *
1215
 * Note that 'data' might be called as NULL pointer. If data is NULL, 'file'
1216
 * will be ignored.
1217
 *
1218
 * Returns NULL on out of memory.
1219
 */
1220
struct CookieInfo *Curl_cookie_init(void)
1221
2.92k
{
1222
2.92k
  int i;
1223
2.92k
  struct CookieInfo *ci = curlx_calloc(1, sizeof(struct CookieInfo));
1224
2.92k
  if(!ci)
1225
9
    return NULL;
1226
1227
  /* This does not use the destructor callback since we want to add
1228
     and remove to lists while keeping the cookie struct intact */
1229
186k
  for(i = 0; i < COOKIE_HASH_SIZE; i++)
1230
183k
    Curl_llist_init(&ci->cookielist[i], NULL);
1231
  /*
1232
   * Initialize the next_expiration time to signal that we do not have enough
1233
   * information yet.
1234
   */
1235
2.92k
  ci->next_expiration = CURL_OFF_T_MAX;
1236
1237
2.92k
  return ci;
1238
2.92k
}
1239
1240
/*
1241
 * cookie_load()
1242
 *
1243
 * Reads cookies from a local file. This is always called before any cookies
1244
 * are set. If file is "-" then STDIN is read.
1245
 *
1246
 * If 'flags' has the COOKIE_NOSESSION bit set, discard all "session cookies"
1247
 * read from file.
1248
 */
1249
static CURLcode cookie_load(struct Curl_easy *data, const char *file,
1250
                            struct CookieInfo *ci, int flags)
1251
2.71k
{
1252
2.71k
  FILE *handle = NULL;
1253
2.71k
  CURLcode result = CURLE_OK;
1254
2.71k
  FILE *fp = NULL;
1255
2.71k
  DEBUGASSERT(ci);
1256
2.71k
  DEBUGASSERT(data);
1257
2.71k
  DEBUGASSERT(file);
1258
1259
2.71k
  ci->running = FALSE; /* this is not running, this is init */
1260
1261
2.71k
  if(file && *file) {
1262
2.71k
    if(!strcmp(file, "-"))
1263
0
      fp = stdin;
1264
2.71k
    else {
1265
2.71k
      fp = curlx_fopen(file, "rb");
1266
2.71k
      if(!fp)
1267
0
        infof(data, "WARNING: failed to open cookie file \"%s\"", file);
1268
2.71k
      else {
1269
2.71k
        curlx_struct_stat stat;
1270
2.71k
        if((curlx_fstat(fileno(fp), &stat) != -1) && S_ISDIR(stat.st_mode)) {
1271
0
          curlx_fclose(fp);
1272
0
          fp = NULL;
1273
0
          infof(data, "WARNING: cookie filename points to a directory: \"%s\"",
1274
0
                file);
1275
0
        }
1276
2.71k
        else
1277
2.71k
          handle = fp;
1278
2.71k
      }
1279
2.71k
    }
1280
2.71k
  }
1281
1282
2.71k
  if(fp) {
1283
2.71k
    struct dynbuf buf;
1284
2.71k
    bool eof = FALSE;
1285
    /* get_netscape_format() uses curl_maprintf(), which returns at most
1286
       DYN_APRINTF - 1 bytes. cookie_output() adds a newline (CRLF on Windows),
1287
       and this dynbuf needs one more byte for its null-terminator. This is a
1288
       cap, not an allocation. */
1289
2.71k
    curlx_dyn_init(&buf, DYN_APRINTF + 2);
1290
2.71k
    do {
1291
2.71k
      result = Curl_get_line(&buf, fp, &eof);
1292
2.71k
      if(!result) {
1293
2.71k
        const char *lineptr = curlx_dyn_ptr(&buf);
1294
2.71k
        bool headerline = FALSE;
1295
2.71k
        if(checkprefix("Set-Cookie:", lineptr)) {
1296
          /* This is a cookie line, get it! */
1297
0
          lineptr += 11;
1298
0
          headerline = TRUE;
1299
0
          curlx_str_passblanks(&lineptr);
1300
0
        }
1301
1302
2.71k
        result = Curl_cookie_add(data, ci, lineptr, NULL, NULL,
1303
2.71k
                                 (headerline ? COOKIE_HTTPHEADER : 0) |
1304
2.71k
                                 COOKIE_SECURE |
1305
2.71k
                                 (flags & (COOKIE_NOPSL | COOKIE_NOSESSION)));
1306
        /* Ignore individual cookie problems unless we ran out of memory */
1307
2.71k
        if(result != CURLE_OUT_OF_MEMORY)
1308
2.71k
          result = CURLE_OK;
1309
2.71k
      }
1310
2.71k
    } while(!result && !eof);
1311
2.71k
    curlx_dyn_free(&buf); /* free the line buffer */
1312
1313
    /*
1314
     * Remove expired cookies from the hash. We must make sure to run this
1315
     * after reading the file, and not on every cookie.
1316
     */
1317
2.71k
    remove_expired(ci);
1318
1319
2.71k
    if(handle)
1320
2.71k
      curlx_fclose(handle);
1321
2.71k
  }
1322
2.71k
  data->state.cookie_engine = TRUE;
1323
2.71k
  ci->running = TRUE; /* now, we are running */
1324
1325
2.71k
  return result;
1326
2.71k
}
1327
1328
/*
1329
 * Load cookies from all given cookie files (CURLOPT_COOKIEFILE).
1330
 */
1331
CURLcode Curl_cookie_loadfiles(struct Curl_easy *data,
1332
                               int flags)
1333
2.74k
{
1334
2.74k
  CURLcode result = CURLE_OK;
1335
2.74k
  struct curl_slist *list = data->state.cookielist;
1336
2.74k
  if(list) {
1337
2.71k
    Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1338
2.71k
    if(!data->cookies)
1339
21
      data->cookies = Curl_cookie_init();
1340
2.71k
    if(!data->cookies)
1341
0
      result = CURLE_OUT_OF_MEMORY;
1342
2.71k
    else {
1343
2.71k
      data->state.cookie_engine = TRUE;
1344
5.42k
      while(list) {
1345
2.71k
        result = cookie_load(data, list->data, data->cookies, flags);
1346
2.71k
        if(result)
1347
3
          break;
1348
2.71k
        list = list->next;
1349
2.71k
      }
1350
2.71k
    }
1351
2.71k
    Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1352
2.71k
  }
1353
2.74k
  return result;
1354
2.74k
}
1355
1356
/*
1357
 * cookie_sort
1358
 *
1359
 * Helper function to sort cookies such that the longest path gets before the
1360
 * shorter path. Path, domain and name lengths are considered in that order,
1361
 * with the creationtime as the tiebreaker. The creationtime is guaranteed to
1362
 * be unique per cookie, so we know we will get an ordering at that point.
1363
 */
1364
static int cookie_sort(const void *p1, const void *p2)
1365
0
{
1366
0
  const struct Cookie *c1 = *(const struct Cookie * const *)p1;
1367
0
  const struct Cookie *c2 = *(const struct Cookie * const *)p2;
1368
0
  size_t l1, l2;
1369
1370
  /* 1 - compare cookie path lengths */
1371
0
  l1 = c1->path ? strlen(c1->path) : 0;
1372
0
  l2 = c2->path ? strlen(c2->path) : 0;
1373
1374
0
  if(l1 != l2)
1375
0
    return (l2 > l1) ? 1 : -1; /* avoid size_t <=> int conversions */
1376
1377
  /* 2 - compare cookie domain lengths */
1378
0
  l1 = c1->domain ? strlen(c1->domain) : 0;
1379
0
  l2 = c2->domain ? strlen(c2->domain) : 0;
1380
1381
0
  if(l1 != l2)
1382
0
    return (l2 > l1) ? 1 : -1; /* avoid size_t <=> int conversions */
1383
1384
  /* 3 - compare cookie name lengths */
1385
0
  l1 = c1->name ? strlen(c1->name) : 0;
1386
0
  l2 = c2->name ? strlen(c2->name) : 0;
1387
1388
0
  if(l1 != l2)
1389
0
    return (l2 > l1) ? 1 : -1;
1390
1391
  /* 4 - compare cookie creation time */
1392
0
  return (c2->creationtime > c1->creationtime) ? 1 : -1;
1393
0
}
1394
1395
/*
1396
 * cookie_sort_ct
1397
 *
1398
 * Helper function to sort cookies according to creation time.
1399
 */
1400
static int cookie_sort_ct(const void *p1, const void *p2)
1401
0
{
1402
0
  const struct Cookie *c1 = *(const struct Cookie * const *)p1;
1403
0
  const struct Cookie *c2 = *(const struct Cookie * const *)p2;
1404
1405
0
  return (c2->creationtime > c1->creationtime) ? 1 : -1;
1406
0
}
1407
1408
bool Curl_secure_context(struct Curl_easy *data, const char *host,
1409
                         const size_t hlen)
1410
0
{
1411
0
  return Curl_xfer_is_secure(data) ||
1412
0
    (!data->info.used_proxy &&
1413
0
     (((hlen == 9) && curl_strnequal("localhost", host, 9)) ||
1414
0
      ((hlen == 9) && !memcmp(host, "127.0.0.1", 9)) ||
1415
0
      ((hlen == 3) && !memcmp(host, "::1", 3))));
1416
0
}
1417
1418
/*
1419
 * Curl_cookie_getlist
1420
 *
1421
 * For a given host and path, return a linked list of cookies that the client
1422
 * should send to the server if used now.
1423
 *
1424
 * This MUST be a read-only operation, do NOT update the cookie list in this
1425
 * function. For example. this function must not expire old cookies, just
1426
 * avoid them.
1427
 *
1428
 * It shall only return cookies that have not expired.
1429
 *
1430
 * 'okay' is TRUE when there is a list returned.
1431
 */
1432
CURLcode Curl_cookie_getlist(struct Curl_easy *data,
1433
                             bool *okay,
1434
                             const char *hostp,
1435
                             struct Curl_llist *list)
1436
0
{
1437
0
  size_t matches = 0;
1438
0
  bool is_ip = FALSE;
1439
0
  size_t myhash = 0;
1440
0
  struct Curl_llist_node *n;
1441
0
  bool secure;
1442
0
  const struct CookieInfo *ci = data->cookies;
1443
0
  const char *path = data->state.up.path;
1444
0
  CURLcode result = CURLE_OK;
1445
0
  struct Curl_str host;
1446
0
  curl_off_t now;
1447
0
  *okay = FALSE;
1448
1449
0
  Curl_llist_init(list, NULL);
1450
1451
0
  curlx_str_assign(&host, hostp, strlen(hostp));
1452
0
  secure = Curl_secure_context(data, hostp, curlx_strlen(&host));
1453
0
  if(!host_is_ipnum(&host))
1454
0
    myhash = cookiehash(&host);
1455
0
  else
1456
0
    is_ip = TRUE;
1457
1458
0
  if(!ci || !Curl_llist_count(&ci->cookielist[myhash]))
1459
0
    return CURLE_OK; /* no cookie struct or no cookies in the struct */
1460
1461
0
  now = (curl_off_t)time(NULL);
1462
1463
0
  for(n = Curl_llist_head(&ci->cookielist[myhash]); n; n = Curl_node_next(n)) {
1464
0
    struct Cookie *co = Curl_node_elem(n);
1465
1466
0
    if(co->expires && (co->expires < now))
1467
0
      continue;
1468
1469
    /* if the cookie requires we are secure we must only continue if we are! */
1470
0
    if(co->secure ? secure : TRUE) {
1471
1472
      /* now check if the domain is correct */
1473
0
      if(!co->domain[0] ||
1474
0
         (co->tailmatch && !is_ip &&
1475
0
          cookie_tailmatch(co->domain, strlen(co->domain),
1476
0
                           curlx_str(&host), curlx_strlen(&host))) ||
1477
0
         ((!co->tailmatch || is_ip) && curl_strequal(hostp, co->domain))) {
1478
        /*
1479
         * the right part of the host matches the domain stuff in the
1480
         * cookie data
1481
         */
1482
1483
        /*
1484
         * now check the left part of the path with the cookies path
1485
         * requirement
1486
         */
1487
0
        if(!co->path || pathmatch(co->path, path)) {
1488
1489
          /*
1490
           * This is a match and we add it to the return-linked-list
1491
           */
1492
0
          Curl_llist_append(list, co, &co->getnode);
1493
0
          matches++;
1494
0
          if(matches >= MAX_COOKIE_SEND_AMOUNT) {
1495
0
            infof(data, "Included max number of cookies (%zu) in request!",
1496
0
                  matches);
1497
0
            break;
1498
0
          }
1499
0
        }
1500
0
      }
1501
0
    }
1502
0
  }
1503
1504
0
  if(matches > 1) {
1505
0
#define NUM_ON_STACK 16
1506
1507
    /*
1508
     * Now we need to make sure that if there is a name appearing more than
1509
     * once, the longest specified path version comes first. To make this the
1510
     * swiftest way, we sort them all based on path length.
1511
     */
1512
0
    struct Cookie **array;
1513
0
    struct Cookie *arrbuf[NUM_ON_STACK];
1514
0
    size_t i;
1515
1516
0
    if(matches > NUM_ON_STACK) {
1517
      /* alloc an array and store all cookie pointers */
1518
0
      array = curlx_malloc(sizeof(struct Cookie *) * matches);
1519
0
      if(!array) {
1520
0
        result = CURLE_OUT_OF_MEMORY;
1521
0
        goto fail;
1522
0
      }
1523
0
    }
1524
0
    else
1525
0
      array = arrbuf;
1526
1527
0
    n = Curl_llist_head(list);
1528
1529
0
    for(i = 0; n; n = Curl_node_next(n))
1530
0
      array[i++] = Curl_node_elem(n);
1531
1532
    /* now sort the cookie pointers in path length order */
1533
0
    qsort(array, matches, sizeof(struct Cookie *), cookie_sort);
1534
1535
    /* remake the linked list order according to the new order */
1536
0
    Curl_llist_destroy(list, NULL);
1537
1538
0
    for(i = 0; i < matches; i++)
1539
0
      Curl_llist_append(list, array[i], &array[i]->getnode);
1540
1541
0
    if(array != arrbuf)
1542
0
      curlx_free(array); /* remove the temporary data again */
1543
0
  }
1544
1545
0
  *okay = TRUE;
1546
0
  return CURLE_OK; /* success */
1547
1548
0
fail:
1549
  /* failure, clear up the allocated chain and return NULL */
1550
0
  Curl_llist_destroy(list, NULL);
1551
0
  return result; /* error */
1552
0
}
1553
1554
/*
1555
 * Curl_cookie_clearall
1556
 *
1557
 * Clear all existing cookies and reset the counter.
1558
 */
1559
void Curl_cookie_clearall(struct CookieInfo *ci)
1560
2.92k
{
1561
2.92k
  if(ci) {
1562
2.92k
    unsigned int i;
1563
186k
    for(i = 0; i < COOKIE_HASH_SIZE; i++) {
1564
183k
      struct Curl_llist_node *n;
1565
183k
      for(n = Curl_llist_head(&ci->cookielist[i]); n;) {
1566
0
        struct Cookie *c = Curl_node_elem(n);
1567
0
        struct Curl_llist_node *e = Curl_node_next(n);
1568
0
        Curl_node_remove(n);
1569
0
        curlx_free(c);
1570
0
        n = e;
1571
0
      }
1572
183k
    }
1573
2.92k
    ci->numcookies = 0;
1574
2.92k
  }
1575
2.92k
}
1576
1577
/*
1578
 * Curl_cookie_clearsess
1579
 *
1580
 * Free all session cookies in the cookies list.
1581
 */
1582
void Curl_cookie_clearsess(struct CookieInfo *ci)
1583
0
{
1584
0
  unsigned int i;
1585
1586
0
  if(!ci)
1587
0
    return;
1588
1589
0
  for(i = 0; i < COOKIE_HASH_SIZE; i++) {
1590
0
    struct Curl_llist_node *n = Curl_llist_head(&ci->cookielist[i]);
1591
0
    struct Curl_llist_node *e = NULL;
1592
1593
0
    for(; n; n = e) {
1594
0
      struct Cookie *curr = Curl_node_elem(n);
1595
0
      e = Curl_node_next(n); /* in case the node is removed, get it early */
1596
0
      if(!curr->expires) {
1597
0
        Curl_node_remove(n);
1598
0
        curlx_free(curr);
1599
0
        ci->numcookies--;
1600
0
      }
1601
0
    }
1602
0
  }
1603
0
}
1604
1605
/*
1606
 * Curl_cookie_cleanup()
1607
 *
1608
 * Free a "cookie object" previous created with Curl_cookie_init().
1609
 */
1610
void Curl_cookie_cleanup(struct CookieInfo *ci)
1611
2.92k
{
1612
2.92k
  if(ci) {
1613
2.92k
    Curl_cookie_clearall(ci);
1614
2.92k
    curlx_free(ci); /* free the base struct as well */
1615
2.92k
  }
1616
2.92k
}
1617
1618
/*
1619
 * get_netscape_format()
1620
 *
1621
 * Formats a string for Netscape output file, w/o a newline at the end.
1622
 * Function returns a char * to a formatted line. The caller is responsible
1623
 * for freeing the returned pointer.
1624
 */
1625
static char *get_netscape_format(const struct Cookie *co)
1626
0
{
1627
0
  return curl_maprintf(
1628
0
    "%s"               /* httponly preamble */
1629
0
    "%s%s\t"           /* domain */
1630
0
    "%s\t"             /* tailmatch */
1631
0
    "%s\t"             /* path */
1632
0
    "%s\t"             /* secure */
1633
0
    "%" FMT_OFF_T "\t" /* expires */
1634
0
    "%s\t"             /* name */
1635
0
    "%s",              /* value */
1636
0
    co->httponly ? "#HttpOnly_" : "",
1637
    /*
1638
     * Make sure all domains are prefixed with a dot if they allow
1639
     * tailmatching. This is Mozilla-style.
1640
     */
1641
0
    (co->tailmatch && co->domain && co->domain[0] != '.') ? "." : "",
1642
0
    co->domain ? co->domain : "unknown",
1643
0
    co->tailmatch ? "TRUE" : "FALSE",
1644
0
    co->path ? co->path : "/",
1645
0
    co->secure ? "TRUE" : "FALSE",
1646
0
    co->expires,
1647
0
    co->name,
1648
0
    co->value ? co->value : "");
1649
0
}
1650
1651
/*
1652
 * cookie_output()
1653
 *
1654
 * Writes all internally known cookies to the specified file. Specify
1655
 * "-" as filename to write to stdout.
1656
 *
1657
 * The function returns non-zero on write failure.
1658
 */
1659
static CURLcode cookie_output(struct Curl_easy *data,
1660
                              struct CookieInfo *ci,
1661
                              const char *filename)
1662
2.71k
{
1663
2.71k
  FILE *out = NULL;
1664
2.71k
  bool use_stdout = FALSE;
1665
2.71k
  char *tempstore = NULL;
1666
2.71k
  CURLcode result = CURLE_OK;
1667
1668
2.71k
  if(!ci)
1669
    /* no cookie engine alive */
1670
0
    return CURLE_OK;
1671
1672
  /* at first, remove expired cookies */
1673
2.71k
  remove_expired(ci);
1674
1675
2.71k
  if(!strcmp("-", filename)) {
1676
    /* use stdout */
1677
0
    out = stdout;
1678
0
    use_stdout = TRUE;
1679
0
  }
1680
2.71k
  else {
1681
2.71k
    result = Curl_fopen(data, filename, &out, &tempstore);
1682
2.71k
    if(result)
1683
0
      goto error;
1684
2.71k
  }
1685
1686
2.71k
  if(fputs("# Netscape HTTP Cookie File\n"
1687
2.71k
           "# https://curl.se/docs/http-cookies.html\n"
1688
2.71k
           "# This file was generated by libcurl! Edit at your own risk.\n\n",
1689
2.71k
           out) == EOF) {
1690
0
    result = CURLE_WRITE_ERROR;
1691
0
    goto error;
1692
0
  }
1693
1694
2.71k
  if(ci->numcookies) {
1695
0
    unsigned int i;
1696
0
    size_t nvalid = 0;
1697
0
    struct Cookie **array;
1698
0
    struct Curl_llist_node *n;
1699
1700
0
    array = curlx_calloc(1, sizeof(struct Cookie *) * ci->numcookies);
1701
0
    if(!array) {
1702
0
      result = CURLE_OUT_OF_MEMORY;
1703
0
      goto error;
1704
0
    }
1705
1706
    /* only sort the cookies with a domain property */
1707
0
    for(i = 0; i < COOKIE_HASH_SIZE; i++) {
1708
0
      for(n = Curl_llist_head(&ci->cookielist[i]); n; n = Curl_node_next(n)) {
1709
0
        struct Cookie *co = Curl_node_elem(n);
1710
0
        if(!co->domain || !co->domain[0])
1711
0
          continue;
1712
0
        array[nvalid++] = co;
1713
0
      }
1714
0
    }
1715
1716
0
    qsort(array, nvalid, sizeof(struct Cookie *), cookie_sort_ct);
1717
1718
0
    for(i = 0; i < nvalid; i++) {
1719
0
      char *format_ptr = get_netscape_format(array[i]);
1720
0
      if(!format_ptr) {
1721
0
        curlx_free(array);
1722
0
        result = CURLE_OUT_OF_MEMORY;
1723
0
        goto error;
1724
0
      }
1725
0
      if(curl_mfprintf(out, "%s\n", format_ptr) < 0) {
1726
0
        curlx_free(format_ptr);
1727
0
        curlx_free(array);
1728
0
        result = CURLE_WRITE_ERROR;
1729
0
        goto error;
1730
0
      }
1731
0
      curlx_free(format_ptr);
1732
0
    }
1733
1734
0
    curlx_free(array);
1735
0
  }
1736
1737
  /* Catch buffered and earlier write errors before publishing the jar. */
1738
2.71k
  if(fflush(out) || ferror(out)) {
1739
0
    result = CURLE_WRITE_ERROR;
1740
0
    goto error;
1741
0
  }
1742
1743
2.71k
  if(!use_stdout) {
1744
2.71k
    int close_result = curlx_fclose(out);
1745
2.71k
    out = NULL;
1746
2.71k
    if(close_result) {
1747
0
      result = CURLE_WRITE_ERROR;
1748
0
      goto error;
1749
0
    }
1750
2.71k
    if(tempstore && curlx_rename(tempstore, filename)) {
1751
0
      result = CURLE_WRITE_ERROR;
1752
0
      goto error;
1753
0
    }
1754
2.71k
  }
1755
1756
  /*
1757
   * If we reach here we have successfully written a cookie file so there is
1758
   * no need to inspect the error, any error case should have jumped into the
1759
   * error block below.
1760
   */
1761
2.71k
  curlx_free(tempstore);
1762
2.71k
  return CURLE_OK;
1763
1764
0
error:
1765
0
  if(out && !use_stdout)
1766
0
    curlx_fclose(out);
1767
0
  if(tempstore) {
1768
0
    unlink(tempstore);
1769
0
    curlx_free(tempstore);
1770
0
  }
1771
0
  return result;
1772
2.71k
}
1773
1774
/* keep this function read-only, do not modify the source list */
1775
1776
static struct curl_slist *cookie_list(const struct Curl_easy *data)
1777
0
{
1778
0
  struct curl_slist *list = NULL;
1779
0
  struct curl_slist *beg;
1780
0
  struct curl_slist **tail = &list;
1781
0
  unsigned int i;
1782
0
  struct Curl_llist_node *n;
1783
0
  curl_off_t now;
1784
1785
0
  if(!data->cookies || (data->cookies->numcookies == 0))
1786
0
    return NULL;
1787
1788
0
  now = (curl_off_t)time(NULL);
1789
1790
0
  for(i = 0; i < COOKIE_HASH_SIZE; i++) {
1791
0
    for(n = Curl_llist_head(&data->cookies->cookielist[i]); n;
1792
0
        n = Curl_node_next(n)) {
1793
0
      struct Cookie *c = Curl_node_elem(n);
1794
0
      char *line;
1795
0
      if((c->expires && (c->expires < now)) || !c->domain || !c->domain[0])
1796
0
        continue;
1797
0
      line = get_netscape_format(c);
1798
0
      if(!line) {
1799
0
        curl_slist_free_all(list);
1800
0
        return NULL;
1801
0
      }
1802
0
      beg = Curl_slist_append_nodup(NULL, line);
1803
0
      if(!beg) {
1804
0
        curlx_free(line);
1805
0
        curl_slist_free_all(list);
1806
0
        return NULL;
1807
0
      }
1808
0
      *tail = beg;
1809
0
      tail = &beg->next;
1810
0
    }
1811
0
  }
1812
1813
0
  return list;
1814
0
}
1815
1816
struct curl_slist *Curl_cookie_list(struct Curl_easy *data)
1817
0
{
1818
0
  struct curl_slist *list;
1819
0
  Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1820
0
  list = cookie_list(data);
1821
0
  Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1822
0
  return list;
1823
0
}
1824
1825
void Curl_flush_cookies(struct Curl_easy *data, bool cleanup)
1826
5.83k
{
1827
5.83k
  Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1828
  /* only save the cookie file if a transfer was started (cookies->running is
1829
     set), as otherwise the cookies were not completely initialized and there
1830
     might be cookie files that were not loaded so saving the file is the
1831
     wrong thing. */
1832
5.83k
  if(data->cookies) {
1833
2.92k
    const char *cookiejar = CURL_EASY_STR(data, STRING_COOKIEJAR);
1834
2.92k
    if(cookiejar && data->cookies->running) {
1835
      /* if we have a destination file for all the cookies to get dumped to */
1836
2.71k
      CURLcode result = cookie_output(data, data->cookies, cookiejar);
1837
2.71k
      if(result)
1838
0
        infof(data, "WARNING: failed to save cookies in %s: %s",
1839
2.71k
              cookiejar, curl_easy_strerror(result));
1840
2.71k
    }
1841
1842
2.92k
    if(cleanup && (!data->share || (data->cookies != data->share->cookies))) {
1843
2.92k
      Curl_cookie_cleanup(data->cookies);
1844
2.92k
      data->cookies = NULL;
1845
2.92k
    }
1846
2.92k
  }
1847
5.83k
  Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1848
5.83k
}
1849
1850
void Curl_cookie_run(struct Curl_easy *data)
1851
2.74k
{
1852
2.74k
  Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
1853
2.74k
  if(data->cookies)
1854
2.73k
    data->cookies->running = TRUE;
1855
2.74k
  Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
1856
2.74k
}
1857
1858
#endif /* CURL_DISABLE_HTTP || CURL_DISABLE_COOKIES */