2002-09-03 07:52:59 -04:00
|
|
|
/***************************************************************************
|
2004-05-12 08:04:38 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2001-01-03 04:29:33 -05:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2021-03-26 09:25:45 -04:00
|
|
|
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2001-01-03 04:29:33 -05:00
|
|
|
*
|
2002-09-03 07:52:59 -04:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 08:02:01 -05:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2004-05-12 08:04:38 -04:00
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
2002-09-03 07:52:59 -04:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
2001-01-03 04:29:33 -05:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
2002-09-03 07:52:59 -04:00
|
|
|
***************************************************************************/
|
1999-12-29 09:20:26 -05:00
|
|
|
|
|
|
|
/***
|
|
|
|
|
|
|
|
|
|
|
|
RECEIVING COOKIE INFORMATION
|
|
|
|
============================
|
|
|
|
|
2016-06-21 09:47:12 -04:00
|
|
|
struct CookieInfo *Curl_cookie_init(struct Curl_easy *data,
|
2014-12-07 06:22:52 -05:00
|
|
|
const char *file, struct CookieInfo *inc, bool newsession);
|
2004-05-12 08:04:38 -04:00
|
|
|
|
2004-10-06 03:50:18 -04:00
|
|
|
Inits a cookie struct to store data in a local file. This is always
|
|
|
|
called before any cookies are set.
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Cookie *Curl_cookie_add(struct Curl_easy *data,
|
2014-12-07 06:22:52 -05:00
|
|
|
struct CookieInfo *c, bool httpheader, char *lineptr,
|
|
|
|
const char *domain, const char *path);
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2014-12-07 06:22:52 -05:00
|
|
|
The 'lineptr' parameter is a full "Set-cookie:" line as
|
2004-10-06 03:50:18 -04:00
|
|
|
received from a server.
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2004-10-06 03:50:18 -04:00
|
|
|
The function need to replace previously stored lines that this new
|
2018-08-28 05:28:50 -04:00
|
|
|
line supersedes.
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2004-10-06 03:50:18 -04:00
|
|
|
It may remove lines that are expired.
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2004-10-06 03:50:18 -04:00
|
|
|
It should return an indication of success/error.
|
1999-12-29 09:20:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
SENDING COOKIE INFORMATION
|
|
|
|
==========================
|
|
|
|
|
2014-12-07 06:22:52 -05:00
|
|
|
struct Cookies *Curl_cookie_getlist(struct CookieInfo *cookie,
|
|
|
|
char *host, char *path, bool secure);
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2004-10-06 03:50:18 -04:00
|
|
|
For a given host and path, return a linked list of cookies that
|
|
|
|
the client should send to the server if used now. The secure
|
|
|
|
boolean informs the cookie if a secure connection is achieved or
|
|
|
|
not.
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2004-10-06 03:50:18 -04:00
|
|
|
It shall only return cookies that haven't expired.
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2004-05-12 08:04:38 -04:00
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
Example set of cookies:
|
2004-05-12 08:04:38 -04:00
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
Set-cookie: PRODUCTINFO=webxpress; domain=.fidelity.com; path=/; secure
|
|
|
|
Set-cookie: PERSONALIZE=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
|
|
|
|
domain=.fidelity.com; path=/ftgw; secure
|
|
|
|
Set-cookie: FidHist=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
|
|
|
|
domain=.fidelity.com; path=/; secure
|
|
|
|
Set-cookie: FidOrder=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
|
|
|
|
domain=.fidelity.com; path=/; secure
|
|
|
|
Set-cookie: DisPend=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
|
|
|
|
domain=.fidelity.com; path=/; secure
|
|
|
|
Set-cookie: FidDis=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
|
|
|
|
domain=.fidelity.com; path=/; secure
|
|
|
|
Set-cookie:
|
|
|
|
Session_Key@6791a9e0-901a-11d0-a1c8-9b012c88aa77=none;expires=Monday,
|
|
|
|
13-Jun-1988 03:04:55 GMT; domain=.fidelity.com; path=/; secure
|
|
|
|
****/
|
|
|
|
|
2003-03-31 10:59:17 -05:00
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2000-08-24 10:26:33 -04:00
|
|
|
|
2004-12-05 18:59:32 -05:00
|
|
|
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
|
2002-06-11 07:13:01 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "cookie.h"
|
2018-05-28 14:29:15 -04:00
|
|
|
#include "psl.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "strtok.h"
|
|
|
|
#include "sendf.h"
|
2013-07-15 10:53:43 -04:00
|
|
|
#include "slist.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "share.h"
|
|
|
|
#include "strtoofft.h"
|
2016-09-30 12:54:02 -04:00
|
|
|
#include "strcase.h"
|
2019-03-30 15:11:57 -04:00
|
|
|
#include "curl_get_line.h"
|
2009-09-28 12:05:20 -04:00
|
|
|
#include "curl_memrchr.h"
|
2014-08-19 15:01:28 -04:00
|
|
|
#include "inet_pton.h"
|
2019-11-28 09:27:58 -05:00
|
|
|
#include "parsedate.h"
|
2020-02-14 08:36:50 -05:00
|
|
|
#include "rand.h"
|
2020-02-17 16:55:34 -05:00
|
|
|
#include "rename.h"
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2016-04-29 09:46:40 -04:00
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#include "curl_printf.h"
|
2015-03-24 18:12:03 -04:00
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2000-10-09 07:12:34 -04:00
|
|
|
|
2021-03-12 11:36:08 -05:00
|
|
|
static void strstore(char **str, const char *newstr);
|
|
|
|
|
2004-05-12 08:04:38 -04:00
|
|
|
static void freecookie(struct Cookie *co)
|
2002-05-07 05:58:13 -04:00
|
|
|
{
|
2015-03-11 12:41:01 -04:00
|
|
|
free(co->expirestr);
|
|
|
|
free(co->domain);
|
|
|
|
free(co->path);
|
|
|
|
free(co->spath);
|
|
|
|
free(co->name);
|
|
|
|
free(co->value);
|
|
|
|
free(co->maxage);
|
|
|
|
free(co->version);
|
2002-05-07 05:58:13 -04:00
|
|
|
free(co);
|
|
|
|
}
|
|
|
|
|
2013-04-10 18:17:15 -04:00
|
|
|
static bool tailmatch(const char *cooke_domain, const char *hostname)
|
2003-04-30 13:03:43 -04:00
|
|
|
{
|
2013-04-10 18:17:15 -04:00
|
|
|
size_t cookie_domain_len = strlen(cooke_domain);
|
|
|
|
size_t hostname_len = strlen(hostname);
|
2003-04-30 13:03:43 -04:00
|
|
|
|
2013-04-10 18:17:15 -04:00
|
|
|
if(hostname_len < cookie_domain_len)
|
2003-04-30 13:03:43 -04:00
|
|
|
return FALSE;
|
|
|
|
|
2017-09-09 17:55:08 -04:00
|
|
|
if(!strcasecompare(cooke_domain, hostname + hostname_len-cookie_domain_len))
|
2013-04-10 18:17:15 -04:00
|
|
|
return FALSE;
|
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* A lead char of cookie_domain is not '.'.
|
|
|
|
* RFC6265 4.1.2.3. The Domain Attribute says:
|
|
|
|
* For example, if the value of the Domain attribute is
|
|
|
|
* "example.com", the user agent will include the cookie in the Cookie
|
|
|
|
* header when making HTTP requests to example.com, www.example.com, and
|
|
|
|
* www.corp.example.com.
|
2013-04-10 18:17:15 -04:00
|
|
|
*/
|
|
|
|
if(hostname_len == cookie_domain_len)
|
|
|
|
return TRUE;
|
|
|
|
if('.' == *(hostname + hostname_len - cookie_domain_len - 1))
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
2003-04-30 13:03:43 -04:00
|
|
|
}
|
|
|
|
|
2018-03-30 10:35:46 -04:00
|
|
|
/*
|
2021-03-12 11:20:28 -05:00
|
|
|
* isip
|
|
|
|
*
|
|
|
|
* Returns true if the given string is an IPv4 or IPv6 address (if IPv6 has
|
|
|
|
* been enabled while building libcurl, and false otherwise.
|
2018-03-30 10:35:46 -04:00
|
|
|
*/
|
|
|
|
static bool isip(const char *domain)
|
|
|
|
{
|
|
|
|
struct in_addr addr;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct in6_addr addr6;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(Curl_inet_pton(AF_INET, domain, &addr)
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
|| Curl_inet_pton(AF_INET6, domain, &addr6)
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
/* domain name given as IP address */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-06-12 05:19:56 -04:00
|
|
|
/*
|
|
|
|
* matching cookie path and url path
|
|
|
|
* RFC6265 5.1.4 Paths and Path-Match
|
|
|
|
*/
|
2016-11-23 01:53:24 -05:00
|
|
|
static bool pathmatch(const char *cookie_path, const char *request_uri)
|
2013-05-18 16:51:31 -04:00
|
|
|
{
|
2013-06-12 05:19:56 -04:00
|
|
|
size_t cookie_path_len;
|
|
|
|
size_t uri_path_len;
|
2016-11-23 01:53:24 -05:00
|
|
|
char *uri_path = NULL;
|
|
|
|
char *pos;
|
2013-06-12 05:19:56 -04:00
|
|
|
bool ret = FALSE;
|
|
|
|
|
|
|
|
/* cookie_path must not have last '/' separator. ex: /sample */
|
|
|
|
cookie_path_len = strlen(cookie_path);
|
|
|
|
if(1 == cookie_path_len) {
|
|
|
|
/* cookie_path must be '/' */
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-05-18 16:51:31 -04:00
|
|
|
|
2013-06-12 05:19:56 -04:00
|
|
|
uri_path = strdup(request_uri);
|
|
|
|
if(!uri_path)
|
2013-05-18 16:51:31 -04:00
|
|
|
return FALSE;
|
2013-06-12 05:19:56 -04:00
|
|
|
pos = strchr(uri_path, '?');
|
|
|
|
if(pos)
|
|
|
|
*pos = 0x0;
|
|
|
|
|
|
|
|
/* #-fragments are already cut off! */
|
|
|
|
if(0 == strlen(uri_path) || uri_path[0] != '/') {
|
2021-03-12 11:36:08 -05:00
|
|
|
strstore(&uri_path, "/");
|
2013-06-12 05:19:56 -04:00
|
|
|
if(!uri_path)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* here, RFC6265 5.1.4 says
|
|
|
|
* 4. Output the characters of the uri-path from the first character up
|
|
|
|
* to, but not including, the right-most %x2F ("/").
|
|
|
|
* but URL path /hoge?fuga=xxx means /hoge/index.cgi?fuga=xxx in some site
|
|
|
|
* without redirect.
|
|
|
|
* Ignore this algorithm because /hoge is uri path for this case
|
|
|
|
* (uri path is not /).
|
2013-06-12 05:19:56 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
uri_path_len = strlen(uri_path);
|
|
|
|
|
|
|
|
if(uri_path_len < cookie_path_len) {
|
|
|
|
ret = FALSE;
|
|
|
|
goto pathmatched;
|
|
|
|
}
|
2013-05-18 16:51:31 -04:00
|
|
|
|
|
|
|
/* not using checkprefix() because matching should be case-sensitive */
|
2013-06-12 05:19:56 -04:00
|
|
|
if(strncmp(cookie_path, uri_path, cookie_path_len)) {
|
|
|
|
ret = FALSE;
|
|
|
|
goto pathmatched;
|
|
|
|
}
|
2013-05-18 16:51:31 -04:00
|
|
|
|
2013-06-12 05:19:56 -04:00
|
|
|
/* The cookie-path and the uri-path are identical. */
|
|
|
|
if(cookie_path_len == uri_path_len) {
|
|
|
|
ret = TRUE;
|
|
|
|
goto pathmatched;
|
|
|
|
}
|
2013-05-18 16:51:31 -04:00
|
|
|
|
2019-01-14 10:31:20 -05:00
|
|
|
/* here, cookie_path_len < uri_path_len */
|
2013-06-12 05:19:56 -04:00
|
|
|
if(uri_path[cookie_path_len] == '/') {
|
|
|
|
ret = TRUE;
|
|
|
|
goto pathmatched;
|
|
|
|
}
|
2013-05-18 16:51:31 -04:00
|
|
|
|
2013-06-12 05:19:56 -04:00
|
|
|
ret = FALSE;
|
|
|
|
|
|
|
|
pathmatched:
|
|
|
|
free(uri_path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-30 10:35:46 -04:00
|
|
|
/*
|
|
|
|
* Return the top-level domain, for optimal hashing.
|
|
|
|
*/
|
|
|
|
static const char *get_top_domain(const char * const domain, size_t *outlen)
|
|
|
|
{
|
2020-01-28 04:23:41 -05:00
|
|
|
size_t len = 0;
|
2018-04-04 09:28:28 -04:00
|
|
|
const char *first = NULL, *last;
|
2018-03-30 10:35:46 -04:00
|
|
|
|
2020-01-28 04:23:41 -05:00
|
|
|
if(domain) {
|
|
|
|
len = strlen(domain);
|
|
|
|
last = memrchr(domain, '.', len);
|
|
|
|
if(last) {
|
|
|
|
first = memrchr(domain, '.', (last - domain));
|
|
|
|
if(first)
|
|
|
|
len -= (++first - domain);
|
|
|
|
}
|
2018-04-04 09:28:28 -04:00
|
|
|
}
|
2018-03-30 10:35:46 -04:00
|
|
|
|
|
|
|
if(outlen)
|
|
|
|
*outlen = len;
|
|
|
|
|
2018-04-04 09:28:28 -04:00
|
|
|
return first? first: domain;
|
2018-03-30 10:35:46 -04:00
|
|
|
}
|
|
|
|
|
2020-10-27 15:47:57 -04:00
|
|
|
/* Avoid C1001, an "internal error" with MSVC14 */
|
|
|
|
#if defined(_MSC_VER) && (_MSC_VER == 1900)
|
|
|
|
#pragma optimize("", off)
|
|
|
|
#endif
|
|
|
|
|
2018-04-05 08:55:59 -04:00
|
|
|
/*
|
|
|
|
* A case-insensitive hash for the cookie domains.
|
|
|
|
*/
|
|
|
|
static size_t cookie_hash_domain(const char *domain, const size_t len)
|
|
|
|
{
|
|
|
|
const char *end = domain + len;
|
2018-04-06 08:19:39 -04:00
|
|
|
size_t h = 5381;
|
2018-04-05 08:55:59 -04:00
|
|
|
|
|
|
|
while(domain < end) {
|
|
|
|
h += h << 5;
|
2018-04-06 08:19:39 -04:00
|
|
|
h ^= Curl_raw_toupper(*domain++);
|
2018-04-05 08:55:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (h % COOKIE_HASH_SIZE);
|
|
|
|
}
|
|
|
|
|
2020-10-27 15:47:57 -04:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER == 1900)
|
|
|
|
#pragma optimize("", on)
|
|
|
|
#endif
|
|
|
|
|
2018-03-30 10:35:46 -04:00
|
|
|
/*
|
|
|
|
* Hash this domain.
|
|
|
|
*/
|
|
|
|
static size_t cookiehash(const char * const domain)
|
|
|
|
{
|
|
|
|
const char *top;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if(!domain || isip(domain))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
top = get_top_domain(domain, &len);
|
2018-04-05 08:55:59 -04:00
|
|
|
return cookie_hash_domain(top, len);
|
2018-03-30 10:35:46 -04:00
|
|
|
}
|
|
|
|
|
2013-06-12 05:19:56 -04:00
|
|
|
/*
|
|
|
|
* cookie path sanitize
|
|
|
|
*/
|
|
|
|
static char *sanitize_cookie_path(const char *cookie_path)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *new_path = strdup(cookie_path);
|
|
|
|
if(!new_path)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* some stupid site sends path attribute with '"'. */
|
2015-04-16 10:37:40 -04:00
|
|
|
len = strlen(new_path);
|
2013-06-12 05:19:56 -04:00
|
|
|
if(new_path[0] == '\"') {
|
2015-04-16 10:37:40 -04:00
|
|
|
memmove((void *)new_path, (const void *)(new_path + 1), len);
|
|
|
|
len--;
|
2013-06-12 05:19:56 -04:00
|
|
|
}
|
2015-04-16 10:37:40 -04:00
|
|
|
if(len && (new_path[len - 1] == '\"')) {
|
|
|
|
new_path[len - 1] = 0x0;
|
|
|
|
len--;
|
2013-06-12 05:19:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* RFC6265 5.2.4 The Path Attribute */
|
|
|
|
if(new_path[0] != '/') {
|
|
|
|
/* Let cookie-path be the default-path. */
|
2021-03-12 11:36:08 -05:00
|
|
|
strstore(&new_path, "/");
|
2013-06-12 05:19:56 -04:00
|
|
|
return new_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert /hoge/ to /hoge */
|
2015-04-16 10:37:40 -04:00
|
|
|
if(len && new_path[len - 1] == '/') {
|
2013-06-12 05:19:56 -04:00
|
|
|
new_path[len - 1] = 0x0;
|
2013-05-18 16:51:31 -04:00
|
|
|
}
|
2013-06-12 05:19:56 -04:00
|
|
|
|
|
|
|
return new_path;
|
2013-05-18 16:51:31 -04:00
|
|
|
}
|
|
|
|
|
2005-08-17 04:55:43 -04:00
|
|
|
/*
|
|
|
|
* Load cookies from all given cookie files (CURLOPT_COOKIEFILE).
|
2014-12-07 06:24:29 -05:00
|
|
|
*
|
|
|
|
* NOTE: OOM or cookie parsing failures are ignored.
|
2005-08-17 04:55:43 -04:00
|
|
|
*/
|
2016-06-21 09:47:12 -04:00
|
|
|
void Curl_cookie_loadfiles(struct Curl_easy *data)
|
2005-08-17 04:55:43 -04:00
|
|
|
{
|
2021-03-26 09:25:45 -04:00
|
|
|
struct curl_slist *list = data->state.cookielist;
|
2005-08-17 04:55:43 -04:00
|
|
|
if(list) {
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
while(list) {
|
2014-12-07 06:24:29 -05:00
|
|
|
struct CookieInfo *newcookies = Curl_cookie_init(data,
|
|
|
|
list->data,
|
|
|
|
data->cookies,
|
|
|
|
data->set.cookiesession);
|
|
|
|
if(!newcookies)
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Failure may be due to OOM or a bad cookie; both are ignored
|
2014-12-07 06:24:29 -05:00
|
|
|
* but only the first should be
|
|
|
|
*/
|
|
|
|
infof(data, "ignoring failed cookie_init for %s\n", list->data);
|
|
|
|
else
|
|
|
|
data->cookies = newcookies;
|
2005-08-17 04:55:43 -04:00
|
|
|
list = list->next;
|
|
|
|
}
|
2021-03-26 09:25:45 -04:00
|
|
|
curl_slist_free_all(data->state.cookielist); /* clean up list */
|
|
|
|
data->state.cookielist = NULL; /* don't do this again! */
|
2011-10-11 13:41:30 -04:00
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
|
2005-08-17 04:55:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-25 08:23:22 -04:00
|
|
|
/*
|
2021-03-12 11:20:28 -05:00
|
|
|
* strstore
|
2009-05-25 08:23:22 -04:00
|
|
|
*
|
2021-03-12 11:20:28 -05:00
|
|
|
* A thin wrapper around strdup which ensures that any memory allocated at
|
|
|
|
* *str will be freed before the string allocated by strdup is stored there.
|
|
|
|
* The intended usecase is repeated assignments to the same variable during
|
|
|
|
* parsing in a last-wins scenario. The caller is responsible for checking
|
|
|
|
* for OOM errors.
|
2009-05-25 08:23:22 -04:00
|
|
|
*/
|
|
|
|
static void strstore(char **str, const char *newstr)
|
|
|
|
{
|
2015-03-11 12:41:01 -04:00
|
|
|
free(*str);
|
2009-05-25 08:23:22 -04:00
|
|
|
*str = strdup(newstr);
|
|
|
|
}
|
|
|
|
|
2013-09-17 02:51:22 -04:00
|
|
|
/*
|
2021-03-12 11:20:28 -05:00
|
|
|
* remove_expired
|
|
|
|
*
|
|
|
|
* Remove expired cookies from the hash by inspecting the expires timestamp on
|
|
|
|
* each cookie in the hash, freeing and deleting any where the timestamp is in
|
|
|
|
* the past.
|
2013-09-17 02:51:22 -04:00
|
|
|
*/
|
|
|
|
static void remove_expired(struct CookieInfo *cookies)
|
|
|
|
{
|
2018-06-02 16:52:56 -04:00
|
|
|
struct Cookie *co, *nx;
|
2013-09-17 02:51:22 -04:00
|
|
|
curl_off_t now = (curl_off_t)time(NULL);
|
2018-03-30 10:35:46 -04:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for(i = 0; i < COOKIE_HASH_SIZE; i++) {
|
2018-06-02 16:52:56 -04:00
|
|
|
struct Cookie *pv = NULL;
|
2018-03-30 10:35:46 -04:00
|
|
|
co = cookies->cookies[i];
|
|
|
|
while(co) {
|
|
|
|
nx = co->next;
|
|
|
|
if(co->expires && co->expires < now) {
|
|
|
|
if(!pv) {
|
|
|
|
cookies->cookies[i] = co->next;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pv->next = co->next;
|
|
|
|
}
|
|
|
|
cookies->numcookies--;
|
|
|
|
freecookie(co);
|
2013-09-17 02:51:22 -04:00
|
|
|
}
|
|
|
|
else {
|
2018-03-30 10:35:46 -04:00
|
|
|
pv = co;
|
2013-09-17 02:51:22 -04:00
|
|
|
}
|
2018-03-30 10:35:46 -04:00
|
|
|
co = nx;
|
2013-09-17 02:51:22 -04:00
|
|
|
}
|
2014-08-19 15:01:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 14:29:15 -04:00
|
|
|
/* Make sure domain contains a dot or is localhost. */
|
|
|
|
static bool bad_domain(const char *domain)
|
|
|
|
{
|
|
|
|
return !strchr(domain, '.') && !strcasecompare(domain, "localhost");
|
|
|
|
}
|
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Curl_cookie_add
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2021-03-12 11:20:28 -05:00
|
|
|
* Add a single cookie line to the cookie keeping object. Be aware that
|
|
|
|
* sometimes we get an IP-only host name, and that might also be a numerical
|
|
|
|
* IPv6 address.
|
2013-05-21 17:28:59 -04:00
|
|
|
*
|
2014-12-07 06:24:29 -05:00
|
|
|
* Returns NULL on out of memory or invalid cookie. This is suboptimal,
|
|
|
|
* as they should be treated separately.
|
2021-03-12 11:20:28 -05:00
|
|
|
*/
|
2001-01-05 05:11:41 -05:00
|
|
|
struct Cookie *
|
2016-06-21 09:47:12 -04:00
|
|
|
Curl_cookie_add(struct Curl_easy *data,
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* The 'data' pointer here may be NULL at times, and thus
|
|
|
|
* must only be used very carefully for things that can deal
|
|
|
|
* with data being NULL. Such as infof() and similar
|
|
|
|
*/
|
2003-08-11 05:55:11 -04:00
|
|
|
struct CookieInfo *c,
|
2001-01-05 05:11:41 -05:00
|
|
|
bool httpheader, /* TRUE if HTTP header-style line */
|
2018-03-30 11:33:52 -04:00
|
|
|
bool noexpire, /* if TRUE, skip remove_expired() */
|
2002-07-29 18:22:49 -04:00
|
|
|
char *lineptr, /* first character of the line */
|
2007-08-29 01:36:53 -04:00
|
|
|
const char *domain, /* default domain */
|
2018-12-13 03:57:58 -05:00
|
|
|
const char *path, /* full path used when this cookie is set,
|
2009-09-26 16:51:51 -04:00
|
|
|
used to get default path for the cookie
|
|
|
|
unless set */
|
2018-12-13 03:57:58 -05:00
|
|
|
bool secure) /* TRUE if connection is over secure origin */
|
1999-12-29 09:20:26 -05:00
|
|
|
{
|
|
|
|
struct Cookie *clist;
|
|
|
|
struct Cookie *co;
|
2017-09-09 17:09:06 -04:00
|
|
|
struct Cookie *lastc = NULL;
|
1999-12-29 09:20:26 -05:00
|
|
|
time_t now = time(NULL);
|
|
|
|
bool replace_old = FALSE;
|
2003-04-30 13:03:43 -04:00
|
|
|
bool badcookie = FALSE; /* cookies are good by default. mmmmm yummy */
|
2018-03-30 10:35:46 -04:00
|
|
|
size_t myhash;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2007-03-24 22:30:58 -04:00
|
|
|
#ifdef CURL_DISABLE_VERBOSE_STRINGS
|
|
|
|
(void)data;
|
|
|
|
#endif
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
/* First, alloc and init a new struct for it */
|
2009-11-18 05:33:54 -05:00
|
|
|
co = calloc(1, sizeof(struct Cookie));
|
1999-12-29 09:20:26 -05:00
|
|
|
if(!co)
|
|
|
|
return NULL; /* bail out if we're this low on memory */
|
|
|
|
|
|
|
|
if(httpheader) {
|
|
|
|
/* This line was read off a HTTP-header */
|
2017-09-17 18:55:07 -04:00
|
|
|
char name[MAX_NAME];
|
|
|
|
char what[MAX_NAME];
|
2007-08-29 01:36:53 -04:00
|
|
|
const char *ptr;
|
|
|
|
const char *semiptr;
|
2004-06-22 17:15:51 -04:00
|
|
|
|
2017-09-17 18:55:07 -04:00
|
|
|
size_t linelength = strlen(lineptr);
|
|
|
|
if(linelength > MAX_COOKIE_LINE) {
|
|
|
|
/* discard overly long lines at once */
|
2004-06-22 17:15:51 -04:00
|
|
|
free(co);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
semiptr = strchr(lineptr, ';'); /* first, find a semicolon */
|
2002-07-29 18:22:49 -04:00
|
|
|
|
2007-02-13 12:47:27 -05:00
|
|
|
while(*lineptr && ISBLANK(*lineptr))
|
2002-07-29 18:22:49 -04:00
|
|
|
lineptr++;
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
ptr = lineptr;
|
2000-09-25 18:14:42 -04:00
|
|
|
do {
|
2011-08-09 08:02:05 -04:00
|
|
|
/* we have a <what>=<this> pair or a stand-alone word here */
|
2017-09-09 17:09:06 -04:00
|
|
|
name[0] = what[0] = 0; /* init the buffers */
|
2016-02-08 09:48:18 -05:00
|
|
|
if(1 <= sscanf(ptr, "%" MAX_NAME_TXT "[^;\r\n=] =%"
|
2017-09-17 18:55:07 -04:00
|
|
|
MAX_NAME_TXT "[^;\r\n]",
|
2011-08-09 08:02:05 -04:00
|
|
|
name, what)) {
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Use strstore() below to properly deal with received cookie
|
|
|
|
* headers that have the same string property set more than once,
|
|
|
|
* and then we use the last one.
|
|
|
|
*/
|
2011-08-09 08:02:05 -04:00
|
|
|
const char *whatptr;
|
|
|
|
bool done = FALSE;
|
|
|
|
bool sep;
|
2017-09-09 17:09:06 -04:00
|
|
|
size_t len = strlen(what);
|
2016-02-08 09:48:18 -05:00
|
|
|
size_t nlen = strlen(name);
|
|
|
|
const char *endofn = &ptr[ nlen ];
|
2011-08-09 08:02:05 -04:00
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Check for too long individual name or contents, or too long
|
|
|
|
* combination of name + contents. Chrome and Firefox support 4095 or
|
|
|
|
* 4096 bytes combo
|
|
|
|
*/
|
2017-09-17 18:55:07 -04:00
|
|
|
if(nlen >= (MAX_NAME-1) || len >= (MAX_NAME-1) ||
|
|
|
|
((nlen + len) > MAX_NAME)) {
|
2017-09-29 02:50:15 -04:00
|
|
|
freecookie(co);
|
2018-05-28 15:46:22 -04:00
|
|
|
infof(data, "oversized cookie dropped, name/val %zu + %zu bytes\n",
|
2017-09-17 18:55:07 -04:00
|
|
|
nlen, len);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-08-09 08:02:05 -04:00
|
|
|
/* name ends with a '=' ? */
|
2011-09-05 14:46:09 -04:00
|
|
|
sep = (*endofn == '=')?TRUE:FALSE;
|
2011-08-09 08:02:05 -04:00
|
|
|
|
2016-02-08 09:48:18 -05:00
|
|
|
if(nlen) {
|
|
|
|
endofn--; /* move to the last character */
|
|
|
|
if(ISBLANK(*endofn)) {
|
|
|
|
/* skip trailing spaces in name */
|
|
|
|
while(*endofn && ISBLANK(*endofn) && nlen) {
|
|
|
|
endofn--;
|
|
|
|
nlen--;
|
|
|
|
}
|
2017-09-09 17:09:06 -04:00
|
|
|
name[nlen] = 0; /* new end of name */
|
2016-02-08 09:48:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-09 08:02:05 -04:00
|
|
|
/* Strip off trailing whitespace from the 'what' */
|
|
|
|
while(len && ISBLANK(what[len-1])) {
|
2017-09-09 17:09:06 -04:00
|
|
|
what[len-1] = 0;
|
2011-08-09 08:02:05 -04:00
|
|
|
len--;
|
|
|
|
}
|
2002-02-27 02:38:04 -05:00
|
|
|
|
2011-08-09 08:02:05 -04:00
|
|
|
/* Skip leading whitespace from the 'what' */
|
2017-09-09 17:09:06 -04:00
|
|
|
whatptr = what;
|
2011-08-09 08:02:05 -04:00
|
|
|
while(*whatptr && ISBLANK(*whatptr))
|
|
|
|
whatptr++;
|
2002-04-14 14:21:17 -04:00
|
|
|
|
2019-02-16 18:09:30 -05:00
|
|
|
/*
|
|
|
|
* Check if we have a reserved prefix set before anything else, as we
|
|
|
|
* otherwise have to test for the prefix in both the cookie name and
|
|
|
|
* "the rest". Prefixes must start with '__' and end with a '-', so
|
|
|
|
* only test for names where that can possibly be true.
|
|
|
|
*/
|
|
|
|
if(nlen > 3 && name[0] == '_' && name[1] == '_') {
|
2020-01-29 03:57:50 -05:00
|
|
|
if(!strncmp("__Secure-", name, 9))
|
2019-02-16 18:09:30 -05:00
|
|
|
co->prefix |= COOKIE_PREFIX__SECURE;
|
2020-01-29 03:57:50 -05:00
|
|
|
else if(!strncmp("__Host-", name, 7))
|
2019-02-16 18:09:30 -05:00
|
|
|
co->prefix |= COOKIE_PREFIX__HOST;
|
|
|
|
}
|
|
|
|
|
2018-05-12 19:23:10 -04:00
|
|
|
if(!co->name) {
|
2016-03-10 05:20:56 -05:00
|
|
|
/* The very first name/value pair is the actual cookie name */
|
2018-05-12 19:23:10 -04:00
|
|
|
if(!sep) {
|
|
|
|
/* Bad name/value pair. */
|
|
|
|
badcookie = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2016-03-10 05:20:56 -05:00
|
|
|
co->name = strdup(name);
|
|
|
|
co->value = strdup(whatptr);
|
2018-05-12 19:23:10 -04:00
|
|
|
done = TRUE;
|
2016-03-10 05:20:56 -05:00
|
|
|
if(!co->name || !co->value) {
|
|
|
|
badcookie = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!len) {
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* this was a "<name>=" with no content, and we must allow
|
|
|
|
* 'secure' and 'httponly' specified this weirdly
|
|
|
|
*/
|
2011-08-09 08:02:05 -04:00
|
|
|
done = TRUE;
|
2018-12-13 03:57:58 -05:00
|
|
|
/*
|
|
|
|
* secure cookies are only allowed to be set when the connection is
|
|
|
|
* using a secure protocol, or when the cookie is being set by
|
|
|
|
* reading from file
|
|
|
|
*/
|
|
|
|
if(strcasecompare("secure", name)) {
|
|
|
|
if(secure || !c->running) {
|
|
|
|
co->secure = TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
badcookie = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-30 12:54:02 -04:00
|
|
|
else if(strcasecompare("httponly", name))
|
2011-08-09 08:02:05 -04:00
|
|
|
co->httponly = TRUE;
|
|
|
|
else if(sep)
|
|
|
|
/* there was a '=' so we're not done parsing this field */
|
|
|
|
done = FALSE;
|
|
|
|
}
|
|
|
|
if(done)
|
|
|
|
;
|
2016-09-30 12:54:02 -04:00
|
|
|
else if(strcasecompare("path", name)) {
|
2011-08-09 08:02:05 -04:00
|
|
|
strstore(&co->path, whatptr);
|
|
|
|
if(!co->path) {
|
|
|
|
badcookie = TRUE; /* out of memory bad */
|
|
|
|
break;
|
|
|
|
}
|
2017-09-30 08:10:12 -04:00
|
|
|
free(co->spath); /* if this is set again */
|
2013-06-12 05:19:56 -04:00
|
|
|
co->spath = sanitize_cookie_path(co->path);
|
|
|
|
if(!co->spath) {
|
|
|
|
badcookie = TRUE; /* out of memory bad */
|
|
|
|
break;
|
|
|
|
}
|
2011-08-09 08:02:05 -04:00
|
|
|
}
|
2016-09-30 12:54:02 -04:00
|
|
|
else if(strcasecompare("domain", name)) {
|
2014-08-19 15:01:28 -04:00
|
|
|
bool is_ip;
|
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Now, we make sure that our host is within the given domain, or
|
|
|
|
* the given domain is not valid and thus cannot be set.
|
|
|
|
*/
|
2011-08-09 08:02:05 -04:00
|
|
|
|
|
|
|
if('.' == whatptr[0])
|
2013-05-21 17:28:59 -04:00
|
|
|
whatptr++; /* ignore preceding dot */
|
|
|
|
|
2017-01-27 06:59:12 -05:00
|
|
|
#ifndef USE_LIBPSL
|
|
|
|
/*
|
|
|
|
* Without PSL we don't know when the incoming cookie is set on a
|
|
|
|
* TLD or otherwise "protected" suffix. To reduce risk, we require a
|
|
|
|
* dot OR the exact host name being "localhost".
|
|
|
|
*/
|
2018-05-28 14:29:15 -04:00
|
|
|
if(bad_domain(whatptr))
|
|
|
|
domain = ":";
|
2017-01-27 06:59:12 -05:00
|
|
|
#endif
|
2014-08-19 15:01:28 -04:00
|
|
|
|
2017-01-27 06:59:12 -05:00
|
|
|
is_ip = isip(domain ? domain : whatptr);
|
2014-08-19 15:11:20 -04:00
|
|
|
|
2014-08-19 15:01:28 -04:00
|
|
|
if(!domain
|
|
|
|
|| (is_ip && !strcmp(whatptr, domain))
|
|
|
|
|| (!is_ip && tailmatch(whatptr, domain))) {
|
|
|
|
strstore(&co->domain, whatptr);
|
2013-05-21 17:28:59 -04:00
|
|
|
if(!co->domain) {
|
|
|
|
badcookie = TRUE;
|
|
|
|
break;
|
2004-05-12 08:04:38 -04:00
|
|
|
}
|
2014-08-19 15:01:28 -04:00
|
|
|
if(!is_ip)
|
2017-09-09 17:09:06 -04:00
|
|
|
co->tailmatch = TRUE; /* we always do that if the domain name was
|
|
|
|
given */
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2011-08-09 08:02:05 -04:00
|
|
|
else {
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* We did not get a tailmatch and then the attempted set domain is
|
|
|
|
* not a domain to which the current host belongs. Mark as bad.
|
|
|
|
*/
|
2017-09-09 17:09:06 -04:00
|
|
|
badcookie = TRUE;
|
2013-05-21 17:28:59 -04:00
|
|
|
infof(data, "skipped cookie with bad tailmatch domain: %s\n",
|
|
|
|
whatptr);
|
2000-02-01 18:54:51 -05:00
|
|
|
}
|
2011-08-09 08:02:05 -04:00
|
|
|
}
|
2016-09-30 12:54:02 -04:00
|
|
|
else if(strcasecompare("version", name)) {
|
2011-08-09 08:02:05 -04:00
|
|
|
strstore(&co->version, whatptr);
|
|
|
|
if(!co->version) {
|
|
|
|
badcookie = TRUE;
|
|
|
|
break;
|
2000-02-01 18:54:51 -05:00
|
|
|
}
|
2011-08-09 08:02:05 -04:00
|
|
|
}
|
2016-09-30 12:54:02 -04:00
|
|
|
else if(strcasecompare("max-age", name)) {
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Defined in RFC2109:
|
|
|
|
*
|
|
|
|
* Optional. The Max-Age attribute defines the lifetime of the
|
|
|
|
* cookie, in seconds. The delta-seconds value is a decimal non-
|
|
|
|
* negative integer. After delta-seconds seconds elapse, the
|
|
|
|
* client should discard the cookie. A value of zero means the
|
|
|
|
* cookie should be discarded immediately.
|
|
|
|
*/
|
2011-08-09 08:02:05 -04:00
|
|
|
strstore(&co->maxage, whatptr);
|
|
|
|
if(!co->maxage) {
|
|
|
|
badcookie = TRUE;
|
|
|
|
break;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2011-08-09 08:02:05 -04:00
|
|
|
}
|
2016-09-30 12:54:02 -04:00
|
|
|
else if(strcasecompare("expires", name)) {
|
2011-08-09 08:02:05 -04:00
|
|
|
strstore(&co->expirestr, whatptr);
|
|
|
|
if(!co->expirestr) {
|
|
|
|
badcookie = TRUE;
|
|
|
|
break;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-12 11:20:28 -05:00
|
|
|
|
2011-08-09 08:02:05 -04:00
|
|
|
/*
|
2021-03-12 11:20:28 -05:00
|
|
|
* Else, this is the second (or more) name we don't know about!
|
|
|
|
*/
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
else {
|
2011-08-09 08:02:05 -04:00
|
|
|
/* this is an "illegal" <what>=<this> pair */
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2011-08-09 08:02:05 -04:00
|
|
|
|
2002-02-26 08:07:53 -05:00
|
|
|
if(!semiptr || !*semiptr) {
|
|
|
|
/* we already know there are no more cookies */
|
|
|
|
semiptr = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
2000-09-25 18:14:42 -04:00
|
|
|
|
2017-09-09 17:55:08 -04:00
|
|
|
ptr = semiptr + 1;
|
2009-06-10 17:26:11 -04:00
|
|
|
while(*ptr && ISBLANK(*ptr))
|
1999-12-29 09:20:26 -05:00
|
|
|
ptr++;
|
2017-09-09 17:09:06 -04:00
|
|
|
semiptr = strchr(ptr, ';'); /* now, find the next semicolon */
|
2002-01-07 18:05:36 -05:00
|
|
|
|
|
|
|
if(!semiptr && *ptr)
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* There are no more semicolons, but there's a final name=value pair
|
|
|
|
* coming up
|
|
|
|
*/
|
2017-09-09 17:09:06 -04:00
|
|
|
semiptr = strchr(ptr, '\0');
|
2000-09-25 18:14:42 -04:00
|
|
|
} while(semiptr);
|
2001-09-26 03:08:29 -04:00
|
|
|
|
2014-01-16 02:51:30 -05:00
|
|
|
if(co->maxage) {
|
2017-08-14 17:33:23 -04:00
|
|
|
CURLofft offt;
|
2017-09-09 17:09:06 -04:00
|
|
|
offt = curlx_strtoofft((*co->maxage == '\"')?
|
2017-08-14 17:33:23 -04:00
|
|
|
&co->maxage[1]:&co->maxage[0], NULL, 10,
|
|
|
|
&co->expires);
|
|
|
|
if(offt == CURL_OFFT_FLOW)
|
|
|
|
/* overflow, used max value */
|
2014-01-16 02:51:30 -05:00
|
|
|
co->expires = CURL_OFF_T_MAX;
|
2017-08-14 17:33:23 -04:00
|
|
|
else if(!offt) {
|
2018-12-08 11:29:55 -05:00
|
|
|
if(!co->expires)
|
|
|
|
/* already expired */
|
|
|
|
co->expires = 1;
|
|
|
|
else if(CURL_OFF_T_MAX - now < co->expires)
|
2017-08-14 17:33:23 -04:00
|
|
|
/* would overflow */
|
|
|
|
co->expires = CURL_OFF_T_MAX;
|
|
|
|
else
|
|
|
|
co->expires += now;
|
|
|
|
}
|
2014-01-16 02:51:30 -05:00
|
|
|
}
|
|
|
|
else if(co->expirestr) {
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Note that if the date couldn't get parsed for whatever reason, the
|
|
|
|
* cookie will be treated as a session cookie
|
|
|
|
*/
|
2019-11-28 09:27:58 -05:00
|
|
|
co->expires = Curl_getdate_capped(co->expirestr);
|
2014-01-16 02:51:30 -05:00
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Session cookies have expires set to 0 so if we get that back from the
|
|
|
|
* date parser let's add a second to make it a non-session cookie
|
|
|
|
*/
|
2014-01-16 02:51:30 -05:00
|
|
|
if(co->expires == 0)
|
|
|
|
co->expires = 1;
|
|
|
|
else if(co->expires < 0)
|
|
|
|
co->expires = 0;
|
|
|
|
}
|
|
|
|
|
2004-05-12 08:04:38 -04:00
|
|
|
if(!badcookie && !co->domain) {
|
|
|
|
if(domain) {
|
|
|
|
/* no domain was given in the header line, set the default */
|
2017-09-09 17:09:06 -04:00
|
|
|
co->domain = strdup(domain);
|
2004-05-12 08:04:38 -04:00
|
|
|
if(!co->domain)
|
|
|
|
badcookie = TRUE;
|
|
|
|
}
|
2002-02-26 08:07:53 -05:00
|
|
|
}
|
|
|
|
|
2004-05-12 08:04:38 -04:00
|
|
|
if(!badcookie && !co->path && path) {
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* No path was given in the header line, set the default. Note that the
|
|
|
|
* passed-in path to this function MAY have a '?' and following part that
|
|
|
|
* MUST NOT be stored as part of the path.
|
|
|
|
*/
|
2009-09-26 16:51:51 -04:00
|
|
|
char *queryp = strchr(path, '?');
|
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* queryp is where the interesting part of the path ends, so now we
|
|
|
|
* want to the find the last
|
|
|
|
*/
|
2009-09-26 16:51:51 -04:00
|
|
|
char *endslash;
|
|
|
|
if(!queryp)
|
|
|
|
endslash = strrchr(path, '/');
|
|
|
|
else
|
2018-08-31 04:17:40 -04:00
|
|
|
endslash = memrchr(path, '/', (queryp - path));
|
2003-04-30 13:03:43 -04:00
|
|
|
if(endslash) {
|
2018-08-31 04:17:40 -04:00
|
|
|
size_t pathlen = (endslash-path + 1); /* include end slash */
|
2017-09-09 17:55:08 -04:00
|
|
|
co->path = malloc(pathlen + 1); /* one extra for the zero byte */
|
2003-04-30 13:03:43 -04:00
|
|
|
if(co->path) {
|
|
|
|
memcpy(co->path, path, pathlen);
|
2020-06-25 05:38:25 -04:00
|
|
|
co->path[pathlen] = 0; /* null-terminate */
|
2013-06-12 05:19:56 -04:00
|
|
|
co->spath = sanitize_cookie_path(co->path);
|
|
|
|
if(!co->spath)
|
|
|
|
badcookie = TRUE; /* out of memory bad */
|
2003-04-30 13:03:43 -04:00
|
|
|
}
|
2004-05-12 08:04:38 -04:00
|
|
|
else
|
|
|
|
badcookie = TRUE;
|
2003-04-30 13:03:43 -04:00
|
|
|
}
|
|
|
|
}
|
2004-05-12 08:04:38 -04:00
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* If we didn't get a cookie name, or a bad one, the this is an illegal
|
|
|
|
* line so bail out.
|
|
|
|
*/
|
2004-05-12 08:04:38 -04:00
|
|
|
if(badcookie || !co->name) {
|
|
|
|
freecookie(co);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
else {
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* This line is NOT a HTTP header style line, we do offer support for
|
|
|
|
* reading the odd netscape cookies-file format here
|
|
|
|
*/
|
2007-08-29 01:36:53 -04:00
|
|
|
char *ptr;
|
1999-12-29 09:20:26 -05:00
|
|
|
char *firstptr;
|
2017-09-09 17:09:06 -04:00
|
|
|
char *tok_buf = NULL;
|
1999-12-29 09:20:26 -05:00
|
|
|
int fields;
|
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* IE introduced HTTP-only cookies to prevent XSS attacks. Cookies marked
|
|
|
|
* with httpOnly after the domain name are not accessible from javascripts,
|
|
|
|
* but since curl does not operate at javascript level, we include them
|
|
|
|
* anyway. In Firefox's cookie files, these lines are preceded with
|
|
|
|
* #HttpOnly_ and then everything is as usual, so we skip 10 characters of
|
|
|
|
* the line..
|
|
|
|
*/
|
2011-04-20 09:17:42 -04:00
|
|
|
if(strncmp(lineptr, "#HttpOnly_", 10) == 0) {
|
2008-01-31 07:21:57 -05:00
|
|
|
lineptr += 10;
|
|
|
|
co->httponly = TRUE;
|
|
|
|
}
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
if(lineptr[0]=='#') {
|
|
|
|
/* don't even try the comments */
|
|
|
|
free(co);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* strip off the possible end-of-line characters */
|
2017-09-09 17:09:06 -04:00
|
|
|
ptr = strchr(lineptr, '\r');
|
2000-05-22 10:12:12 -04:00
|
|
|
if(ptr)
|
2017-09-09 17:09:06 -04:00
|
|
|
*ptr = 0; /* clear it */
|
|
|
|
ptr = strchr(lineptr, '\n');
|
2000-05-22 10:12:12 -04:00
|
|
|
if(ptr)
|
2017-09-09 17:09:06 -04:00
|
|
|
*ptr = 0; /* clear it */
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
firstptr = strtok_r(lineptr, "\t", &tok_buf); /* tokenize it on the TAB */
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Now loop through the fields and init the struct we already have
|
|
|
|
* allocated
|
|
|
|
*/
|
2017-09-09 17:09:06 -04:00
|
|
|
for(ptr = firstptr, fields = 0; ptr && !badcookie;
|
|
|
|
ptr = strtok_r(NULL, "\t", &tok_buf), fields++) {
|
1999-12-29 09:20:26 -05:00
|
|
|
switch(fields) {
|
|
|
|
case 0:
|
2011-04-19 09:54:13 -04:00
|
|
|
if(ptr[0]=='.') /* skip preceding dots */
|
2003-05-15 18:28:19 -04:00
|
|
|
ptr++;
|
1999-12-29 09:20:26 -05:00
|
|
|
co->domain = strdup(ptr);
|
2004-05-12 08:04:38 -04:00
|
|
|
if(!co->domain)
|
|
|
|
badcookie = TRUE;
|
1999-12-29 09:20:26 -05:00
|
|
|
break;
|
|
|
|
case 1:
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* flag: A TRUE/FALSE value indicating if all machines within a given
|
|
|
|
* domain can access the variable. Set TRUE when the cookie says
|
|
|
|
* .domain.com and to false when the domain is complete www.domain.com
|
|
|
|
*/
|
2016-09-30 12:54:02 -04:00
|
|
|
co->tailmatch = strcasecompare(ptr, "TRUE")?TRUE:FALSE;
|
1999-12-29 09:20:26 -05:00
|
|
|
break;
|
|
|
|
case 2:
|
2019-07-19 06:05:05 -04:00
|
|
|
/* The file format allows the path field to remain not filled in */
|
2007-11-07 04:21:35 -05:00
|
|
|
if(strcmp("TRUE", ptr) && strcmp("FALSE", ptr)) {
|
2001-05-23 05:26:45 -04:00
|
|
|
/* only if the path doesn't look like a boolean option! */
|
|
|
|
co->path = strdup(ptr);
|
2004-05-12 08:04:38 -04:00
|
|
|
if(!co->path)
|
|
|
|
badcookie = TRUE;
|
2013-06-12 05:19:56 -04:00
|
|
|
else {
|
|
|
|
co->spath = sanitize_cookie_path(co->path);
|
|
|
|
if(!co->spath) {
|
|
|
|
badcookie = TRUE; /* out of memory bad */
|
|
|
|
}
|
|
|
|
}
|
2001-05-23 05:26:45 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* this doesn't look like a path, make one up! */
|
|
|
|
co->path = strdup("/");
|
2004-05-12 08:04:38 -04:00
|
|
|
if(!co->path)
|
|
|
|
badcookie = TRUE;
|
2013-06-12 05:19:56 -04:00
|
|
|
co->spath = strdup("/");
|
|
|
|
if(!co->spath)
|
|
|
|
badcookie = TRUE;
|
2001-05-23 05:26:45 -04:00
|
|
|
fields++; /* add a field and fall down to secure */
|
|
|
|
/* FALLTHROUGH */
|
1999-12-29 09:20:26 -05:00
|
|
|
case 3:
|
2018-12-13 03:57:58 -05:00
|
|
|
co->secure = FALSE;
|
|
|
|
if(strcasecompare(ptr, "TRUE")) {
|
|
|
|
if(secure || c->running)
|
|
|
|
co->secure = TRUE;
|
|
|
|
else
|
|
|
|
badcookie = TRUE;
|
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
break;
|
|
|
|
case 4:
|
2017-08-14 17:33:23 -04:00
|
|
|
if(curlx_strtoofft(ptr, NULL, 10, &co->expires))
|
|
|
|
badcookie = TRUE;
|
1999-12-29 09:20:26 -05:00
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
co->name = strdup(ptr);
|
2004-05-12 08:04:38 -04:00
|
|
|
if(!co->name)
|
|
|
|
badcookie = TRUE;
|
2019-05-01 07:14:15 -04:00
|
|
|
else {
|
|
|
|
/* For Netscape file format cookies we check prefix on the name */
|
|
|
|
if(strncasecompare("__Secure-", co->name, 9))
|
|
|
|
co->prefix |= COOKIE_PREFIX__SECURE;
|
|
|
|
else if(strncasecompare("__Host-", co->name, 7))
|
|
|
|
co->prefix |= COOKIE_PREFIX__HOST;
|
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
co->value = strdup(ptr);
|
2004-05-12 08:04:38 -04:00
|
|
|
if(!co->value)
|
|
|
|
badcookie = TRUE;
|
1999-12-29 09:20:26 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-08-11 05:55:11 -04:00
|
|
|
if(6 == fields) {
|
|
|
|
/* we got a cookie with blank contents, fix it */
|
|
|
|
co->value = strdup("");
|
2004-05-12 08:04:38 -04:00
|
|
|
if(!co->value)
|
|
|
|
badcookie = TRUE;
|
|
|
|
else
|
|
|
|
fields++;
|
2003-08-11 05:55:11 -04:00
|
|
|
}
|
2004-05-12 08:04:38 -04:00
|
|
|
|
|
|
|
if(!badcookie && (7 != fields))
|
|
|
|
/* we did not find the sufficient number of fields */
|
|
|
|
badcookie = TRUE;
|
|
|
|
|
|
|
|
if(badcookie) {
|
|
|
|
freecookie(co);
|
1999-12-29 09:20:26 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-05-12 08:04:38 -04:00
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2019-02-16 18:09:30 -05:00
|
|
|
if(co->prefix & COOKIE_PREFIX__SECURE) {
|
|
|
|
/* The __Secure- prefix only requires that the cookie be set secure */
|
|
|
|
if(!co->secure) {
|
|
|
|
freecookie(co);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(co->prefix & COOKIE_PREFIX__HOST) {
|
|
|
|
/*
|
|
|
|
* The __Host- prefix requires the cookie to be secure, have a "/" path
|
|
|
|
* and not have a domain set.
|
|
|
|
*/
|
|
|
|
if(co->secure && co->path && strcmp(co->path, "/") == 0 && !co->tailmatch)
|
|
|
|
;
|
|
|
|
else {
|
|
|
|
freecookie(co);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-07 05:58:13 -04:00
|
|
|
if(!c->running && /* read from a file */
|
|
|
|
c->newsession && /* clean session cookies */
|
|
|
|
!co->expires) { /* this is a session cookie since it doesn't expire! */
|
2004-05-12 08:04:38 -04:00
|
|
|
freecookie(co);
|
2002-05-07 05:58:13 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-08-23 10:05:25 -04:00
|
|
|
co->livecookie = c->running;
|
2018-08-28 05:28:50 -04:00
|
|
|
co->creationtime = ++c->lastct;
|
2001-08-23 10:05:25 -04:00
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Now we have parsed the incoming line, we must now check if this supersedes
|
|
|
|
* an already existing cookie, which it may if the previous have the same
|
|
|
|
* domain and path as this.
|
|
|
|
*/
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2013-09-17 02:51:22 -04:00
|
|
|
/* at first, remove expired cookies */
|
2018-03-30 11:33:52 -04:00
|
|
|
if(!noexpire)
|
|
|
|
remove_expired(c);
|
2013-09-17 02:51:22 -04:00
|
|
|
|
2018-12-19 14:59:09 -05:00
|
|
|
#ifdef USE_LIBPSL
|
2021-03-11 20:34:03 -05:00
|
|
|
/*
|
|
|
|
* Check if the domain is a Public Suffix and if yes, ignore the cookie. We
|
|
|
|
* must also check that the data handle isn't NULL since the psl code will
|
|
|
|
* dereference it.
|
|
|
|
*/
|
|
|
|
if(data && (domain && co->domain && !isip(co->domain))) {
|
2018-12-19 14:59:09 -05:00
|
|
|
const psl_ctx_t *psl = Curl_psl_use(data);
|
2019-03-08 10:55:27 -05:00
|
|
|
int acceptable;
|
2018-05-28 14:29:15 -04:00
|
|
|
|
|
|
|
if(psl) {
|
|
|
|
acceptable = psl_is_cookie_domain_acceptable(psl, domain, co->domain);
|
|
|
|
Curl_psl_release(data);
|
|
|
|
}
|
|
|
|
else
|
2019-03-08 10:55:27 -05:00
|
|
|
acceptable = !bad_domain(domain);
|
2018-05-28 14:29:15 -04:00
|
|
|
|
|
|
|
if(!acceptable) {
|
|
|
|
infof(data, "cookie '%s' dropped, domain '%s' must not "
|
|
|
|
"set cookies for '%s'\n", co->name, domain, co->domain);
|
2015-09-29 05:33:01 -04:00
|
|
|
freecookie(co);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 10:55:27 -05:00
|
|
|
#endif
|
2015-09-29 05:33:01 -04:00
|
|
|
|
2018-03-30 10:35:46 -04:00
|
|
|
myhash = cookiehash(co->domain);
|
|
|
|
clist = c->cookies[myhash];
|
1999-12-29 09:20:26 -05:00
|
|
|
replace_old = FALSE;
|
|
|
|
while(clist) {
|
2016-09-30 12:54:02 -04:00
|
|
|
if(strcasecompare(clist->name, co->name)) {
|
1999-12-29 09:20:26 -05:00
|
|
|
/* the names are identical */
|
|
|
|
|
|
|
|
if(clist->domain && co->domain) {
|
2016-09-30 12:54:02 -04:00
|
|
|
if(strcasecompare(clist->domain, co->domain) &&
|
2016-10-03 00:34:59 -04:00
|
|
|
(clist->tailmatch == co->tailmatch))
|
2003-05-15 18:28:19 -04:00
|
|
|
/* The domains are identical */
|
2017-09-09 17:09:06 -04:00
|
|
|
replace_old = TRUE;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
else if(!clist->domain && !co->domain)
|
|
|
|
replace_old = TRUE;
|
|
|
|
|
|
|
|
if(replace_old) {
|
|
|
|
/* the domains were identical */
|
|
|
|
|
2013-06-12 05:19:56 -04:00
|
|
|
if(clist->spath && co->spath) {
|
2019-01-09 04:11:58 -05:00
|
|
|
if(clist->secure && !co->secure && !secure) {
|
2018-12-13 03:57:58 -05:00
|
|
|
size_t cllen;
|
|
|
|
const char *sep;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A non-secure cookie may not overlay an existing secure cookie.
|
|
|
|
* For an existing cookie "a" with path "/login", refuse a new
|
|
|
|
* cookie "a" with for example path "/login/en", while the path
|
|
|
|
* "/loginhelper" is ok.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sep = strchr(clist->spath + 1, '/');
|
|
|
|
|
|
|
|
if(sep)
|
|
|
|
cllen = sep - clist->spath;
|
|
|
|
else
|
|
|
|
cllen = strlen(clist->spath);
|
|
|
|
|
|
|
|
if(strncasecompare(clist->spath, co->spath, cllen)) {
|
|
|
|
freecookie(co);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2018-12-13 03:57:58 -05:00
|
|
|
else if(strcasecompare(clist->spath, co->spath))
|
|
|
|
replace_old = TRUE;
|
1999-12-29 09:20:26 -05:00
|
|
|
else
|
|
|
|
replace_old = FALSE;
|
|
|
|
}
|
2013-06-12 05:19:56 -04:00
|
|
|
else if(!clist->spath && !co->spath)
|
1999-12-29 09:20:26 -05:00
|
|
|
replace_old = TRUE;
|
|
|
|
else
|
|
|
|
replace_old = FALSE;
|
2004-05-12 08:04:38 -04:00
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2001-08-23 10:05:25 -04:00
|
|
|
if(replace_old && !co->livecookie && clist->livecookie) {
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Both cookies matched fine, except that the already present cookie is
|
|
|
|
* "live", which means it was set from a header, while the new one was
|
|
|
|
* read from a file and thus isn't "live". "live" cookies are preferred
|
|
|
|
* so the new cookie is freed.
|
|
|
|
*/
|
2004-05-12 08:04:38 -04:00
|
|
|
freecookie(co);
|
2001-08-23 10:05:25 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
if(replace_old) {
|
|
|
|
co->next = clist->next; /* get the next-pointer first */
|
|
|
|
|
2018-08-28 05:28:50 -04:00
|
|
|
/* when replacing, creationtime is kept from old */
|
|
|
|
co->creationtime = clist->creationtime;
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
/* then free all the old pointers */
|
2008-10-13 17:03:12 -04:00
|
|
|
free(clist->name);
|
2015-03-11 12:41:01 -04:00
|
|
|
free(clist->value);
|
|
|
|
free(clist->domain);
|
|
|
|
free(clist->path);
|
|
|
|
free(clist->spath);
|
|
|
|
free(clist->expirestr);
|
|
|
|
free(clist->version);
|
|
|
|
free(clist->maxage);
|
2000-02-01 18:54:51 -05:00
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
*clist = *co; /* then store all the new data */
|
2001-10-24 07:36:55 -04:00
|
|
|
|
2020-02-20 09:18:38 -05:00
|
|
|
free(co); /* free the newly allocated memory */
|
2001-10-24 07:36:55 -04:00
|
|
|
co = clist; /* point to the previous struct instead */
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* We have replaced a cookie, now skip the rest of the list but make
|
|
|
|
* sure the 'lastc' pointer is properly set
|
|
|
|
*/
|
2002-01-07 09:56:15 -05:00
|
|
|
do {
|
|
|
|
lastc = clist;
|
|
|
|
clist = clist->next;
|
|
|
|
} while(clist);
|
|
|
|
break;
|
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2001-08-23 10:05:25 -04:00
|
|
|
lastc = clist;
|
1999-12-29 09:20:26 -05:00
|
|
|
clist = clist->next;
|
|
|
|
}
|
|
|
|
|
2003-08-11 05:55:11 -04:00
|
|
|
if(c->running)
|
|
|
|
/* Only show this when NOT reading the cookies from a file */
|
2010-01-28 10:34:18 -05:00
|
|
|
infof(data, "%s cookie %s=\"%s\" for domain %s, path %s, "
|
2013-12-31 06:10:25 -05:00
|
|
|
"expire %" CURL_FORMAT_CURL_OFF_T "\n",
|
2003-08-11 05:55:11 -04:00
|
|
|
replace_old?"Replaced":"Added", co->name, co->value,
|
|
|
|
co->domain, co->path, co->expires);
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
if(!replace_old) {
|
2001-08-23 10:05:25 -04:00
|
|
|
/* then make the last item point on this new one */
|
|
|
|
if(lastc)
|
|
|
|
lastc->next = co;
|
|
|
|
else
|
2018-03-30 10:35:46 -04:00
|
|
|
c->cookies[myhash] = co;
|
2013-04-02 05:45:15 -04:00
|
|
|
c->numcookies++; /* one more cookie in the jar */
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2001-08-23 10:05:25 -04:00
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
return co;
|
|
|
|
}
|
|
|
|
|
2016-09-27 11:36:19 -04:00
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
2001-08-23 10:05:25 -04:00
|
|
|
* Curl_cookie_init()
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
|
|
|
* Inits a cookie struct to read data from a local file. This is always
|
2021-03-12 11:20:28 -05:00
|
|
|
* called before any cookies are set. File may be NULL in which case only the
|
|
|
|
* struct is initialized. Is file is "-" then STDIN is read.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2002-05-07 05:58:13 -04:00
|
|
|
* If 'newsession' is TRUE, discard all "session cookies" on read from file.
|
|
|
|
*
|
2019-09-27 03:41:43 -04:00
|
|
|
* Note that 'data' might be called as NULL pointer.
|
|
|
|
*
|
2014-12-07 06:24:29 -05:00
|
|
|
* Returns NULL on out of memory. Invalid cookies are ignored.
|
2021-03-12 11:20:28 -05:00
|
|
|
*/
|
2016-06-21 09:47:12 -04:00
|
|
|
struct CookieInfo *Curl_cookie_init(struct Curl_easy *data,
|
2007-08-29 01:36:53 -04:00
|
|
|
const char *file,
|
2002-05-07 05:58:13 -04:00
|
|
|
struct CookieInfo *inc,
|
|
|
|
bool newsession)
|
1999-12-29 09:20:26 -05:00
|
|
|
{
|
|
|
|
struct CookieInfo *c;
|
2014-12-07 06:24:29 -05:00
|
|
|
FILE *fp = NULL;
|
2017-09-09 17:09:06 -04:00
|
|
|
bool fromfile = TRUE;
|
2014-12-07 06:24:29 -05:00
|
|
|
char *line = NULL;
|
2004-05-12 08:04:38 -04:00
|
|
|
|
2001-08-23 10:05:25 -04:00
|
|
|
if(NULL == inc) {
|
|
|
|
/* we didn't get a struct, create one */
|
2008-09-06 00:47:14 -04:00
|
|
|
c = calloc(1, sizeof(struct CookieInfo));
|
2001-08-23 10:05:25 -04:00
|
|
|
if(!c)
|
|
|
|
return NULL; /* failed to get memory */
|
|
|
|
c->filename = strdup(file?file:"none"); /* copy the name just in case */
|
2014-12-07 06:24:29 -05:00
|
|
|
if(!c->filename)
|
|
|
|
goto fail; /* failed to get memory */
|
2001-08-23 10:05:25 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* we got an already existing one, use that */
|
|
|
|
c = inc;
|
|
|
|
}
|
|
|
|
c->running = FALSE; /* this is not running, this is init */
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2016-09-30 11:15:05 -04:00
|
|
|
if(file && !strcmp(file, "-")) {
|
2000-02-10 18:14:53 -05:00
|
|
|
fp = stdin;
|
2017-09-09 17:09:06 -04:00
|
|
|
fromfile = FALSE;
|
2000-02-10 18:14:53 -05:00
|
|
|
}
|
2005-03-03 19:26:50 -05:00
|
|
|
else if(file && !*file) {
|
|
|
|
/* points to a "" string */
|
|
|
|
fp = NULL;
|
|
|
|
}
|
2000-02-10 18:14:53 -05:00
|
|
|
else
|
2015-06-01 03:20:18 -04:00
|
|
|
fp = file?fopen(file, FOPEN_READTEXT):NULL;
|
2000-02-10 18:14:53 -05:00
|
|
|
|
2002-05-07 05:58:13 -04:00
|
|
|
c->newsession = newsession; /* new session? */
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
if(fp) {
|
2001-08-23 10:05:25 -04:00
|
|
|
char *lineptr;
|
|
|
|
bool headerline;
|
|
|
|
|
2014-12-07 06:24:29 -05:00
|
|
|
line = malloc(MAX_COOKIE_LINE);
|
|
|
|
if(!line)
|
|
|
|
goto fail;
|
2019-03-03 05:17:52 -05:00
|
|
|
while(Curl_get_line(line, MAX_COOKIE_LINE, fp)) {
|
2014-12-07 06:24:29 -05:00
|
|
|
if(checkprefix("Set-Cookie:", line)) {
|
|
|
|
/* This is a cookie line, get it! */
|
2017-09-09 17:09:06 -04:00
|
|
|
lineptr = &line[11];
|
|
|
|
headerline = TRUE;
|
2004-06-30 08:05:07 -04:00
|
|
|
}
|
2014-12-07 06:24:29 -05:00
|
|
|
else {
|
2017-09-09 17:09:06 -04:00
|
|
|
lineptr = line;
|
|
|
|
headerline = FALSE;
|
2014-12-07 06:24:29 -05:00
|
|
|
}
|
|
|
|
while(*lineptr && ISBLANK(*lineptr))
|
|
|
|
lineptr++;
|
|
|
|
|
2018-12-13 03:57:58 -05:00
|
|
|
Curl_cookie_add(data, c, headerline, TRUE, lineptr, NULL, NULL, TRUE);
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2014-12-07 06:24:29 -05:00
|
|
|
free(line); /* free the line buffer */
|
2021-03-12 11:20:28 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove expired cookies from the hash. We must make sure to run this
|
|
|
|
* after reading the file, and not not on every cookie.
|
|
|
|
*/
|
|
|
|
remove_expired(c);
|
2014-12-07 06:24:29 -05:00
|
|
|
|
2000-02-10 18:14:53 -05:00
|
|
|
if(fromfile)
|
|
|
|
fclose(fp);
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2002-05-07 05:58:13 -04:00
|
|
|
c->running = TRUE; /* now, we're running */
|
2019-09-27 03:41:43 -04:00
|
|
|
if(data)
|
|
|
|
data->state.cookie_engine = TRUE;
|
2001-08-23 10:05:25 -04:00
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
return c;
|
2014-12-07 06:24:29 -05:00
|
|
|
|
|
|
|
fail:
|
2015-03-16 10:01:15 -04:00
|
|
|
free(line);
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Only clean up if we allocated it here, as the original could still be in
|
|
|
|
* use by a share handle.
|
|
|
|
*/
|
2014-12-07 06:24:29 -05:00
|
|
|
if(!inc)
|
|
|
|
Curl_cookie_cleanup(c);
|
|
|
|
if(fromfile && fp)
|
|
|
|
fclose(fp);
|
|
|
|
return NULL; /* out of memory */
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* cookie_sort
|
|
|
|
*
|
|
|
|
* Helper function to sort cookies such that the longest path gets before the
|
|
|
|
* shorter path. Path, domain and name lengths are considered in that order,
|
|
|
|
* with tge creationtime as the tiebreaker.
|
|
|
|
*/
|
2010-01-19 18:19:59 -05:00
|
|
|
static int cookie_sort(const void *p1, const void *p2)
|
|
|
|
{
|
|
|
|
struct Cookie *c1 = *(struct Cookie **)p1;
|
|
|
|
struct Cookie *c2 = *(struct Cookie **)p2;
|
2013-04-07 04:34:32 -04:00
|
|
|
size_t l1, l2;
|
2010-01-19 18:19:59 -05:00
|
|
|
|
2013-04-07 04:34:32 -04:00
|
|
|
/* 1 - compare cookie path lengths */
|
|
|
|
l1 = c1->path ? strlen(c1->path) : 0;
|
|
|
|
l2 = c2->path ? strlen(c2->path) : 0;
|
2010-01-19 18:19:59 -05:00
|
|
|
|
2013-04-07 04:34:32 -04:00
|
|
|
if(l1 != l2)
|
|
|
|
return (l2 > l1) ? 1 : -1 ; /* avoid size_t <=> int conversions */
|
|
|
|
|
|
|
|
/* 2 - compare cookie domain lengths */
|
|
|
|
l1 = c1->domain ? strlen(c1->domain) : 0;
|
|
|
|
l2 = c2->domain ? strlen(c2->domain) : 0;
|
|
|
|
|
|
|
|
if(l1 != l2)
|
|
|
|
return (l2 > l1) ? 1 : -1 ; /* avoid size_t <=> int conversions */
|
|
|
|
|
2018-08-28 05:28:50 -04:00
|
|
|
/* 3 - compare cookie name lengths */
|
|
|
|
l1 = c1->name ? strlen(c1->name) : 0;
|
|
|
|
l2 = c2->name ? strlen(c2->name) : 0;
|
2013-04-07 04:34:32 -04:00
|
|
|
|
2018-08-28 05:28:50 -04:00
|
|
|
if(l1 != l2)
|
|
|
|
return (l2 > l1) ? 1 : -1;
|
|
|
|
|
|
|
|
/* 4 - compare cookie creation time */
|
|
|
|
return (c2->creationtime > c1->creationtime) ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* cookie_sort_ct
|
|
|
|
*
|
|
|
|
* Helper function to sort cookies according to creation time.
|
|
|
|
*/
|
2018-08-28 05:28:50 -04:00
|
|
|
static int cookie_sort_ct(const void *p1, const void *p2)
|
|
|
|
{
|
|
|
|
struct Cookie *c1 = *(struct Cookie **)p1;
|
|
|
|
struct Cookie *c2 = *(struct Cookie **)p2;
|
|
|
|
|
|
|
|
return (c2->creationtime > c1->creationtime) ? 1 : -1;
|
2010-01-19 18:19:59 -05:00
|
|
|
}
|
|
|
|
|
2016-10-04 17:26:13 -04:00
|
|
|
#define CLONE(field) \
|
|
|
|
do { \
|
|
|
|
if(src->field) { \
|
2017-02-21 11:44:02 -05:00
|
|
|
d->field = strdup(src->field); \
|
|
|
|
if(!d->field) \
|
2016-10-04 17:26:13 -04:00
|
|
|
goto fail; \
|
|
|
|
} \
|
2019-11-30 03:29:36 -05:00
|
|
|
} while(0)
|
2016-10-04 17:26:13 -04:00
|
|
|
|
|
|
|
static struct Cookie *dup_cookie(struct Cookie *src)
|
|
|
|
{
|
2017-02-21 11:44:02 -05:00
|
|
|
struct Cookie *d = calloc(sizeof(struct Cookie), 1);
|
|
|
|
if(d) {
|
2016-10-04 17:26:13 -04:00
|
|
|
CLONE(expirestr);
|
|
|
|
CLONE(domain);
|
|
|
|
CLONE(path);
|
|
|
|
CLONE(spath);
|
|
|
|
CLONE(name);
|
|
|
|
CLONE(value);
|
|
|
|
CLONE(maxage);
|
|
|
|
CLONE(version);
|
2017-02-21 11:44:02 -05:00
|
|
|
d->expires = src->expires;
|
|
|
|
d->tailmatch = src->tailmatch;
|
|
|
|
d->secure = src->secure;
|
|
|
|
d->livecookie = src->livecookie;
|
|
|
|
d->httponly = src->httponly;
|
2018-08-28 05:28:50 -04:00
|
|
|
d->creationtime = src->creationtime;
|
2016-10-04 17:26:13 -04:00
|
|
|
}
|
2017-02-21 11:44:02 -05:00
|
|
|
return d;
|
2016-10-04 17:26:13 -04:00
|
|
|
|
|
|
|
fail:
|
2017-02-21 11:44:02 -05:00
|
|
|
freecookie(d);
|
2016-10-04 17:26:13 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Curl_cookie_getlist
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2021-03-12 11:20:28 -05:00
|
|
|
* For a given host and path, return a linked list of cookies that the client
|
|
|
|
* should send to the server if used now. The secure boolean informs the cookie
|
|
|
|
* if a secure connection is achieved or not.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
|
|
|
* It shall only return cookies that haven't expired.
|
2021-03-12 11:20:28 -05:00
|
|
|
*/
|
2001-01-05 05:11:41 -05:00
|
|
|
struct Cookie *Curl_cookie_getlist(struct CookieInfo *c,
|
2007-08-29 01:36:53 -04:00
|
|
|
const char *host, const char *path,
|
2008-09-08 07:36:19 -04:00
|
|
|
bool secure)
|
1999-12-29 09:20:26 -05:00
|
|
|
{
|
2006-05-24 18:46:38 -04:00
|
|
|
struct Cookie *newco;
|
|
|
|
struct Cookie *co;
|
2017-09-09 17:09:06 -04:00
|
|
|
struct Cookie *mainco = NULL;
|
2010-01-26 22:43:34 -05:00
|
|
|
size_t matches = 0;
|
2014-08-19 15:01:28 -04:00
|
|
|
bool is_ip;
|
2018-03-30 10:35:46 -04:00
|
|
|
const size_t myhash = cookiehash(host);
|
2006-05-24 18:46:38 -04:00
|
|
|
|
2018-03-30 10:35:46 -04:00
|
|
|
if(!c || !c->cookies[myhash])
|
2006-05-24 18:46:38 -04:00
|
|
|
return NULL; /* no cookie struct or no cookies in the struct */
|
|
|
|
|
2013-09-17 02:51:22 -04:00
|
|
|
/* at first, remove expired cookies */
|
|
|
|
remove_expired(c);
|
|
|
|
|
2014-08-19 15:01:28 -04:00
|
|
|
/* check if host is an IP(v4|v6) address */
|
|
|
|
is_ip = isip(host);
|
|
|
|
|
2018-03-30 10:35:46 -04:00
|
|
|
co = c->cookies[myhash];
|
2006-05-24 18:46:38 -04:00
|
|
|
|
|
|
|
while(co) {
|
2018-09-09 02:52:04 -04:00
|
|
|
/* if the cookie requires we're secure we must only continue if we are! */
|
|
|
|
if(co->secure?secure:TRUE) {
|
2006-05-24 18:46:38 -04:00
|
|
|
|
|
|
|
/* now check if the domain is correct */
|
|
|
|
if(!co->domain ||
|
2014-08-19 15:01:28 -04:00
|
|
|
(co->tailmatch && !is_ip && tailmatch(co->domain, host)) ||
|
2016-09-30 12:54:02 -04:00
|
|
|
((!co->tailmatch || is_ip) && strcasecompare(host, co->domain)) ) {
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* the right part of the host matches the domain stuff in the
|
|
|
|
* cookie data
|
|
|
|
*/
|
2006-05-24 18:46:38 -04:00
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* now check the left part of the path with the cookies path
|
|
|
|
* requirement
|
|
|
|
*/
|
2013-06-12 05:19:56 -04:00
|
|
|
if(!co->spath || pathmatch(co->spath, path) ) {
|
2006-05-24 18:46:38 -04:00
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* and now, we know this is a match and we should create an
|
|
|
|
* entry for the return-linked-list
|
|
|
|
*/
|
2006-05-24 18:46:38 -04:00
|
|
|
|
2016-10-04 17:26:13 -04:00
|
|
|
newco = dup_cookie(co);
|
2006-05-24 18:46:38 -04:00
|
|
|
if(newco) {
|
|
|
|
/* then modify our next */
|
|
|
|
newco->next = mainco;
|
|
|
|
|
|
|
|
/* point the main to us */
|
|
|
|
mainco = newco;
|
2010-01-19 18:19:59 -05:00
|
|
|
|
|
|
|
matches++;
|
2006-05-24 18:46:38 -04:00
|
|
|
}
|
2018-09-09 18:20:34 -04:00
|
|
|
else
|
|
|
|
goto fail;
|
2006-05-24 18:46:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
co = co->next;
|
|
|
|
}
|
|
|
|
|
2010-01-19 18:19:59 -05:00
|
|
|
if(matches) {
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Now we need to make sure that if there is a name appearing more than
|
|
|
|
* once, the longest specified path version comes first. To make this
|
|
|
|
* the swiftest way, we just sort them all based on path length.
|
|
|
|
*/
|
2010-01-19 18:19:59 -05:00
|
|
|
struct Cookie **array;
|
2010-01-26 22:43:34 -05:00
|
|
|
size_t i;
|
2010-01-19 18:19:59 -05:00
|
|
|
|
|
|
|
/* alloc an array and store all cookie pointers */
|
2011-06-02 13:42:24 -04:00
|
|
|
array = malloc(sizeof(struct Cookie *) * matches);
|
2010-01-19 18:19:59 -05:00
|
|
|
if(!array)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
co = mainco;
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
for(i = 0; co; co = co->next)
|
2010-01-19 18:19:59 -05:00
|
|
|
array[i++] = co;
|
|
|
|
|
2012-07-09 09:25:34 -04:00
|
|
|
/* now sort the cookie pointers in path length order */
|
2010-01-19 18:19:59 -05:00
|
|
|
qsort(array, matches, sizeof(struct Cookie *), cookie_sort);
|
|
|
|
|
|
|
|
/* remake the linked list order according to the new order */
|
|
|
|
|
|
|
|
mainco = array[0]; /* start here */
|
2017-09-09 17:09:06 -04:00
|
|
|
for(i = 0; i<matches-1; i++)
|
2017-09-09 17:55:08 -04:00
|
|
|
array[i]->next = array[i + 1];
|
2010-01-19 18:19:59 -05:00
|
|
|
array[matches-1]->next = NULL; /* terminate the list */
|
|
|
|
|
|
|
|
free(array); /* remove the temporary data again */
|
|
|
|
}
|
|
|
|
|
2006-05-24 18:46:38 -04:00
|
|
|
return mainco; /* return the new list */
|
2018-09-09 18:20:34 -04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
/* failure, clear up the allocated chain and return NULL */
|
|
|
|
Curl_cookie_freelist(mainco);
|
|
|
|
return NULL;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Curl_cookie_clearall
|
2006-05-24 18:46:38 -04:00
|
|
|
*
|
|
|
|
* Clear all existing cookies and reset the counter.
|
2021-03-12 11:20:28 -05:00
|
|
|
*/
|
2006-05-24 18:46:38 -04:00
|
|
|
void Curl_cookie_clearall(struct CookieInfo *cookies)
|
|
|
|
{
|
2007-01-13 18:32:14 -05:00
|
|
|
if(cookies) {
|
2018-03-30 10:35:46 -04:00
|
|
|
unsigned int i;
|
|
|
|
for(i = 0; i < COOKIE_HASH_SIZE; i++) {
|
|
|
|
Curl_cookie_freelist(cookies->cookies[i]);
|
|
|
|
cookies->cookies[i] = NULL;
|
|
|
|
}
|
2007-01-13 18:32:14 -05:00
|
|
|
cookies->numcookies = 0;
|
|
|
|
}
|
2006-05-24 18:46:38 -04:00
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Curl_cookie_freelist
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2001-08-23 10:05:25 -04:00
|
|
|
* Free a list of cookies previously returned by Curl_cookie_getlist();
|
2021-03-12 11:20:28 -05:00
|
|
|
*/
|
2016-10-04 17:26:13 -04:00
|
|
|
void Curl_cookie_freelist(struct Cookie *co)
|
1999-12-29 09:20:26 -05:00
|
|
|
{
|
2006-05-24 18:46:38 -04:00
|
|
|
struct Cookie *next;
|
2014-12-07 06:22:52 -05:00
|
|
|
while(co) {
|
|
|
|
next = co->next;
|
2016-10-04 17:26:13 -04:00
|
|
|
freecookie(co);
|
2014-12-07 06:22:52 -05:00
|
|
|
co = next;
|
2006-05-24 18:46:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Curl_cookie_clearsess
|
2006-05-24 18:46:38 -04:00
|
|
|
*
|
|
|
|
* Free all session cookies in the cookies list.
|
2021-03-12 11:20:28 -05:00
|
|
|
*/
|
2006-05-24 18:46:38 -04:00
|
|
|
void Curl_cookie_clearsess(struct CookieInfo *cookies)
|
|
|
|
{
|
|
|
|
struct Cookie *first, *curr, *next, *prev = NULL;
|
2018-03-30 10:35:46 -04:00
|
|
|
unsigned int i;
|
2006-05-24 18:46:38 -04:00
|
|
|
|
2018-03-30 10:35:46 -04:00
|
|
|
if(!cookies)
|
2006-05-24 18:46:38 -04:00
|
|
|
return;
|
|
|
|
|
2018-03-30 10:35:46 -04:00
|
|
|
for(i = 0; i < COOKIE_HASH_SIZE; i++) {
|
|
|
|
if(!cookies->cookies[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
first = curr = prev = cookies->cookies[i];
|
2006-05-24 18:46:38 -04:00
|
|
|
|
2018-03-30 10:35:46 -04:00
|
|
|
for(; curr; curr = next) {
|
|
|
|
next = curr->next;
|
|
|
|
if(!curr->expires) {
|
|
|
|
if(first == curr)
|
|
|
|
first = next;
|
2006-05-24 18:46:38 -04:00
|
|
|
|
2018-03-30 10:35:46 -04:00
|
|
|
if(prev == curr)
|
|
|
|
prev = next;
|
|
|
|
else
|
|
|
|
prev->next = next;
|
2006-05-24 18:46:38 -04:00
|
|
|
|
2018-03-30 10:35:46 -04:00
|
|
|
freecookie(curr);
|
|
|
|
cookies->numcookies--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
prev = curr;
|
2006-05-24 18:46:38 -04:00
|
|
|
}
|
|
|
|
|
2018-03-30 10:35:46 -04:00
|
|
|
cookies->cookies[i] = first;
|
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
2001-08-23 10:05:25 -04:00
|
|
|
* Curl_cookie_cleanup()
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2014-12-07 06:22:52 -05:00
|
|
|
* Free a "cookie object" previous created with Curl_cookie_init().
|
2021-03-12 11:20:28 -05:00
|
|
|
*/
|
2001-01-05 05:11:41 -05:00
|
|
|
void Curl_cookie_cleanup(struct CookieInfo *c)
|
1999-12-29 09:20:26 -05:00
|
|
|
{
|
2006-05-24 18:46:38 -04:00
|
|
|
if(c) {
|
2018-06-02 16:52:56 -04:00
|
|
|
unsigned int i;
|
2015-03-11 12:41:01 -04:00
|
|
|
free(c->filename);
|
2018-03-30 10:35:46 -04:00
|
|
|
for(i = 0; i < COOKIE_HASH_SIZE; i++)
|
|
|
|
Curl_cookie_freelist(c->cookies[i]);
|
2006-05-24 18:46:38 -04:00
|
|
|
free(c); /* free the base struct as well */
|
|
|
|
}
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* get_netscape_format()
|
2005-07-27 18:17:14 -04:00
|
|
|
*
|
|
|
|
* Formats a string for Netscape output file, w/o a newline at the end.
|
2021-03-12 11:20:28 -05:00
|
|
|
* Function returns a char * to a formatted line. The caller is responsible
|
|
|
|
* for freeing the returned pointer.
|
|
|
|
*/
|
2005-07-27 18:17:14 -04:00
|
|
|
static char *get_netscape_format(const struct Cookie *co)
|
|
|
|
{
|
2006-05-24 18:46:38 -04:00
|
|
|
return aprintf(
|
2008-01-31 07:21:57 -05:00
|
|
|
"%s" /* httponly preamble */
|
2006-05-24 18:46:38 -04:00
|
|
|
"%s%s\t" /* domain */
|
|
|
|
"%s\t" /* tailmatch */
|
|
|
|
"%s\t" /* path */
|
|
|
|
"%s\t" /* secure */
|
2013-12-31 06:10:25 -05:00
|
|
|
"%" CURL_FORMAT_CURL_OFF_T "\t" /* expires */
|
2006-05-24 18:46:38 -04:00
|
|
|
"%s\t" /* name */
|
|
|
|
"%s", /* value */
|
2008-01-31 07:21:57 -05:00
|
|
|
co->httponly?"#HttpOnly_":"",
|
2021-03-12 11:20:28 -05:00
|
|
|
/*
|
|
|
|
* Make sure all domains are prefixed with a dot if they allow
|
|
|
|
* tailmatching. This is Mozilla-style.
|
|
|
|
*/
|
2006-05-24 18:46:38 -04:00
|
|
|
(co->tailmatch && co->domain && co->domain[0] != '.')? ".":"",
|
|
|
|
co->domain?co->domain:"unknown",
|
|
|
|
co->tailmatch?"TRUE":"FALSE",
|
|
|
|
co->path?co->path:"/",
|
|
|
|
co->secure?"TRUE":"FALSE",
|
|
|
|
co->expires,
|
|
|
|
co->name,
|
|
|
|
co->value?co->value:"");
|
2005-07-27 18:17:14 -04:00
|
|
|
}
|
|
|
|
|
2001-08-23 10:05:25 -04:00
|
|
|
/*
|
2011-06-03 14:02:07 -04:00
|
|
|
* cookie_output()
|
2001-08-23 10:05:25 -04:00
|
|
|
*
|
2001-08-29 05:32:18 -04:00
|
|
|
* Writes all internally known cookies to the specified file. Specify
|
|
|
|
* "-" as file name to write to stdout.
|
2001-08-23 10:05:25 -04:00
|
|
|
*
|
2001-08-29 05:32:18 -04:00
|
|
|
* The function returns non-zero on write failure.
|
2001-08-23 10:05:25 -04:00
|
|
|
*/
|
2021-03-14 22:08:31 -04:00
|
|
|
static CURLcode cookie_output(struct Curl_easy *data,
|
|
|
|
struct CookieInfo *c, const char *filename)
|
2001-08-23 10:05:25 -04:00
|
|
|
{
|
|
|
|
struct Cookie *co;
|
2020-02-14 08:36:50 -05:00
|
|
|
FILE *out = NULL;
|
2017-09-09 17:09:06 -04:00
|
|
|
bool use_stdout = FALSE;
|
2020-02-14 08:36:50 -05:00
|
|
|
char *tempstore = NULL;
|
2021-03-14 22:08:31 -04:00
|
|
|
CURLcode error = CURLE_OK;
|
2001-08-29 05:32:18 -04:00
|
|
|
|
2019-02-26 10:35:07 -05:00
|
|
|
if(!c)
|
|
|
|
/* no cookie engine alive */
|
2021-03-14 22:08:31 -04:00
|
|
|
return CURLE_OK;
|
2019-02-26 10:35:07 -05:00
|
|
|
|
2013-09-17 02:51:22 -04:00
|
|
|
/* at first, remove expired cookies */
|
2019-02-26 10:35:07 -05:00
|
|
|
remove_expired(c);
|
2013-09-17 02:51:22 -04:00
|
|
|
|
2020-02-14 08:36:50 -05:00
|
|
|
if(!strcmp("-", filename)) {
|
2001-08-29 05:32:18 -04:00
|
|
|
/* use stdout */
|
|
|
|
out = stdout;
|
2017-09-09 17:09:06 -04:00
|
|
|
use_stdout = TRUE;
|
2001-08-29 05:32:18 -04:00
|
|
|
}
|
|
|
|
else {
|
2020-02-14 08:36:50 -05:00
|
|
|
unsigned char randsuffix[9];
|
|
|
|
|
|
|
|
if(Curl_rand_hex(data, randsuffix, sizeof(randsuffix)))
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
tempstore = aprintf("%s.%s.tmp", filename, randsuffix);
|
|
|
|
if(!tempstore)
|
2021-03-14 22:08:31 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2020-02-14 08:36:50 -05:00
|
|
|
|
|
|
|
out = fopen(tempstore, FOPEN_WRITETEXT);
|
2021-03-14 22:08:31 -04:00
|
|
|
if(!out) {
|
|
|
|
error = CURLE_WRITE_ERROR;
|
2020-02-14 08:36:50 -05:00
|
|
|
goto error;
|
2021-03-14 22:08:31 -04:00
|
|
|
}
|
2001-08-29 05:32:18 -04:00
|
|
|
}
|
|
|
|
|
2016-03-03 15:14:36 -05:00
|
|
|
fputs("# Netscape HTTP Cookie File\n"
|
2020-11-04 08:02:01 -05:00
|
|
|
"# https://curl.se/docs/http-cookies.html\n"
|
2016-03-03 15:14:36 -05:00
|
|
|
"# This file was generated by libcurl! Edit at your own risk.\n\n",
|
|
|
|
out);
|
2004-05-12 08:04:38 -04:00
|
|
|
|
2019-02-26 10:35:07 -05:00
|
|
|
if(c->numcookies) {
|
2019-05-11 15:42:48 -04:00
|
|
|
unsigned int i;
|
2019-09-18 08:29:35 -04:00
|
|
|
size_t nvalid = 0;
|
2019-05-11 15:42:48 -04:00
|
|
|
struct Cookie **array;
|
|
|
|
|
2019-09-18 08:29:35 -04:00
|
|
|
array = calloc(1, sizeof(struct Cookie *) * c->numcookies);
|
2018-11-22 07:24:02 -05:00
|
|
|
if(!array) {
|
2021-03-14 22:08:31 -04:00
|
|
|
error = CURLE_OUT_OF_MEMORY;
|
2020-02-14 08:36:50 -05:00
|
|
|
goto error;
|
2001-08-23 10:05:25 -04:00
|
|
|
}
|
2001-08-29 05:32:18 -04:00
|
|
|
|
2019-09-18 08:29:35 -04:00
|
|
|
/* only sort the cookies with a domain property */
|
2018-11-22 07:24:02 -05:00
|
|
|
for(i = 0; i < COOKIE_HASH_SIZE; i++) {
|
|
|
|
for(co = c->cookies[i]; co; co = co->next) {
|
|
|
|
if(!co->domain)
|
|
|
|
continue;
|
2019-09-18 08:29:35 -04:00
|
|
|
array[nvalid++] = co;
|
2018-11-22 07:24:02 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-28 05:28:50 -04:00
|
|
|
|
2019-09-18 08:29:35 -04:00
|
|
|
qsort(array, nvalid, sizeof(struct Cookie *), cookie_sort_ct);
|
2018-11-22 07:24:02 -05:00
|
|
|
|
2019-09-18 08:29:35 -04:00
|
|
|
for(i = 0; i < nvalid; i++) {
|
2019-05-11 15:42:48 -04:00
|
|
|
char *format_ptr = get_netscape_format(array[i]);
|
2021-04-19 04:46:11 -04:00
|
|
|
if(!format_ptr) {
|
2018-11-22 07:24:02 -05:00
|
|
|
free(array);
|
2021-03-14 22:08:31 -04:00
|
|
|
error = CURLE_OUT_OF_MEMORY;
|
2020-02-14 08:36:50 -05:00
|
|
|
goto error;
|
2018-11-22 07:24:02 -05:00
|
|
|
}
|
|
|
|
fprintf(out, "%s\n", format_ptr);
|
|
|
|
free(format_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(array);
|
|
|
|
}
|
2020-02-14 08:36:50 -05:00
|
|
|
|
2020-02-26 17:50:03 -05:00
|
|
|
if(!use_stdout) {
|
2001-08-29 05:32:18 -04:00
|
|
|
fclose(out);
|
2020-02-14 08:36:50 -05:00
|
|
|
out = NULL;
|
2020-02-17 16:55:34 -05:00
|
|
|
if(Curl_rename(tempstore, filename)) {
|
2020-02-14 08:36:50 -05:00
|
|
|
unlink(tempstore);
|
2021-03-14 22:08:31 -04:00
|
|
|
error = CURLE_WRITE_ERROR;
|
2020-02-14 08:36:50 -05:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2001-08-29 05:32:18 -04:00
|
|
|
|
2021-03-14 22:08:31 -04:00
|
|
|
/*
|
|
|
|
* If we reach here we have successfully written a cookie file so theree is
|
|
|
|
* no need to inspect the error, any error case should have jumped into the
|
|
|
|
* error block below.
|
|
|
|
*/
|
|
|
|
free(tempstore);
|
|
|
|
return CURLE_OK;
|
|
|
|
|
2020-02-14 08:36:50 -05:00
|
|
|
error:
|
|
|
|
if(out && !use_stdout)
|
|
|
|
fclose(out);
|
|
|
|
free(tempstore);
|
2021-03-14 22:08:31 -04:00
|
|
|
return error;
|
2001-08-23 10:05:25 -04:00
|
|
|
}
|
|
|
|
|
2017-09-18 18:50:16 -04:00
|
|
|
static struct curl_slist *cookie_list(struct Curl_easy *data)
|
2005-07-27 18:17:14 -04:00
|
|
|
{
|
2005-07-28 17:49:58 -04:00
|
|
|
struct curl_slist *list = NULL;
|
|
|
|
struct curl_slist *beg;
|
|
|
|
struct Cookie *c;
|
|
|
|
char *line;
|
2018-03-30 10:35:46 -04:00
|
|
|
unsigned int i;
|
2005-07-27 18:17:14 -04:00
|
|
|
|
2021-04-19 04:46:11 -04:00
|
|
|
if(!data->cookies || (data->cookies->numcookies == 0))
|
2005-07-28 17:49:58 -04:00
|
|
|
return NULL;
|
2005-07-27 18:17:14 -04:00
|
|
|
|
2018-03-30 10:35:46 -04:00
|
|
|
for(i = 0; i < COOKIE_HASH_SIZE; i++) {
|
|
|
|
for(c = data->cookies->cookies[i]; c; c = c->next) {
|
|
|
|
if(!c->domain)
|
|
|
|
continue;
|
|
|
|
line = get_netscape_format(c);
|
|
|
|
if(!line) {
|
|
|
|
curl_slist_free_all(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
beg = Curl_slist_append_nodup(list, line);
|
|
|
|
if(!beg) {
|
|
|
|
free(line);
|
|
|
|
curl_slist_free_all(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
list = beg;
|
2007-04-07 00:51:35 -04:00
|
|
|
}
|
2005-07-28 17:49:58 -04:00
|
|
|
}
|
2005-07-27 18:17:14 -04:00
|
|
|
|
2005-07-28 17:49:58 -04:00
|
|
|
return list;
|
2005-07-27 18:17:14 -04:00
|
|
|
}
|
|
|
|
|
2017-09-18 18:50:16 -04:00
|
|
|
struct curl_slist *Curl_cookie_list(struct Curl_easy *data)
|
|
|
|
{
|
|
|
|
struct curl_slist *list;
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
list = cookie_list(data);
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2019-10-03 08:29:57 -04:00
|
|
|
void Curl_flush_cookies(struct Curl_easy *data, bool cleanup)
|
2011-04-04 09:46:42 -04:00
|
|
|
{
|
2021-03-14 22:08:31 -04:00
|
|
|
CURLcode res;
|
|
|
|
|
2011-04-04 09:46:42 -04:00
|
|
|
if(data->set.str[STRING_COOKIEJAR]) {
|
2021-03-26 09:25:45 -04:00
|
|
|
if(data->state.cookielist) {
|
2011-04-04 09:46:42 -04:00
|
|
|
/* If there is a list of cookie files to read, do it first so that
|
|
|
|
we have all the told files read before we write the new jar.
|
|
|
|
Curl_cookie_loadfiles() LOCKS and UNLOCKS the share itself! */
|
|
|
|
Curl_cookie_loadfiles(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
|
|
|
/* if we have a destination file for all the cookies to get dumped to */
|
2021-03-14 22:08:31 -04:00
|
|
|
res = cookie_output(data, data->cookies, data->set.str[STRING_COOKIEJAR]);
|
|
|
|
if(res)
|
|
|
|
infof(data, "WARNING: failed to save cookies in %s: %s\n",
|
|
|
|
data->set.str[STRING_COOKIEJAR], curl_easy_strerror(res));
|
2011-04-04 09:46:42 -04:00
|
|
|
}
|
|
|
|
else {
|
2021-03-26 09:25:45 -04:00
|
|
|
if(cleanup && data->state.cookielist) {
|
2011-04-04 09:46:42 -04:00
|
|
|
/* since nothing is written, we can just free the list of cookie file
|
|
|
|
names */
|
2021-03-26 09:25:45 -04:00
|
|
|
curl_slist_free_all(data->state.cookielist); /* clean up list */
|
|
|
|
data->state.cookielist = NULL;
|
2011-10-07 14:50:57 -04:00
|
|
|
}
|
2011-04-04 09:46:42 -04:00
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cleanup && (!data->share || (data->cookies != data->share->cookies))) {
|
|
|
|
Curl_cookie_cleanup(data->cookies);
|
2019-10-03 04:57:09 -04:00
|
|
|
data->cookies = NULL;
|
2011-04-04 09:46:42 -04:00
|
|
|
}
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
|
|
|
|
}
|
|
|
|
|
2004-12-05 18:59:32 -05:00
|
|
|
#endif /* CURL_DISABLE_HTTP || CURL_DISABLE_COOKIES */
|