2002-09-03 15:52:59 +04:00
|
|
|
|
/***************************************************************************
|
2004-05-12 16:04:38 +04:00
|
|
|
|
* _ _ ____ _
|
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
|
* | (__| |_| | _ <| |___
|
2001-01-03 12:29:33 +03:00
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
|
*
|
2014-01-16 11:37:29 +04:00
|
|
|
|
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2001-01-03 12:29:33 +03:00
|
|
|
|
*
|
2002-09-03 15: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
|
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
2004-05-12 16:04:38 +04:00
|
|
|
|
*
|
2001-01-03 12:29:33 +03: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 15:52:59 +04:00
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
2001-01-03 12:29:33 +03:00
|
|
|
|
*
|
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
|
* KIND, either express or implied.
|
|
|
|
|
*
|
2002-09-03 15:52:59 +04:00
|
|
|
|
***************************************************************************/
|
1999-12-29 17:20:26 +03:00
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RECEIVING COOKIE INFORMATION
|
|
|
|
|
============================
|
|
|
|
|
|
|
|
|
|
struct CookieInfo *cookie_init(char *file);
|
2004-05-12 16:04:38 +04:00
|
|
|
|
|
2004-10-06 11: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 17:20:26 +03:00
|
|
|
|
|
|
|
|
|
int cookies_set(struct CookieInfo *cookie, char *cookie_line);
|
|
|
|
|
|
2004-10-06 11:50:18 +04:00
|
|
|
|
The 'cookie_line' parameter is a full "Set-cookie:" line as
|
|
|
|
|
received from a server.
|
1999-12-29 17:20:26 +03:00
|
|
|
|
|
2004-10-06 11:50:18 +04:00
|
|
|
|
The function need to replace previously stored lines that this new
|
|
|
|
|
line superceeds.
|
1999-12-29 17:20:26 +03:00
|
|
|
|
|
2004-10-06 11:50:18 +04:00
|
|
|
|
It may remove lines that are expired.
|
1999-12-29 17:20:26 +03:00
|
|
|
|
|
2004-10-06 11:50:18 +04:00
|
|
|
|
It should return an indication of success/error.
|
1999-12-29 17:20:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SENDING COOKIE INFORMATION
|
|
|
|
|
==========================
|
|
|
|
|
|
|
|
|
|
struct Cookies *cookie_getlist(struct CookieInfo *cookie,
|
|
|
|
|
char *host, char *path, bool secure);
|
|
|
|
|
|
2004-10-06 11: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 17:20:26 +03:00
|
|
|
|
|
2004-10-06 11:50:18 +04:00
|
|
|
|
It shall only return cookies that haven't expired.
|
1999-12-29 17:20:26 +03:00
|
|
|
|
|
2004-05-12 16:04:38 +04:00
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
Example set of cookies:
|
2004-05-12 16:04:38 +04:00
|
|
|
|
|
1999-12-29 17:20:26 +03: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 19:59:17 +04:00
|
|
|
|
|
2013-01-06 22:06:49 +04:00
|
|
|
|
#include "curl_setup.h"
|
2000-08-24 18:26:33 +04:00
|
|
|
|
|
2004-12-06 02:59:32 +03:00
|
|
|
|
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
|
2002-06-11 15:13:01 +04:00
|
|
|
|
|
2011-04-20 17:17:42 +04:00
|
|
|
|
#define _MPRINTF_REPLACE
|
2005-07-28 02:17:14 +04:00
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
|
2013-01-04 05:50:28 +04:00
|
|
|
|
#include "urldata.h"
|
|
|
|
|
#include "cookie.h"
|
|
|
|
|
#include "strequal.h"
|
|
|
|
|
#include "strtok.h"
|
|
|
|
|
#include "sendf.h"
|
2013-07-15 18:53:43 +04:00
|
|
|
|
#include "slist.h"
|
2009-04-21 15:46:16 +04:00
|
|
|
|
#include "curl_memory.h"
|
2013-01-04 05:50:28 +04:00
|
|
|
|
#include "share.h"
|
|
|
|
|
#include "strtoofft.h"
|
|
|
|
|
#include "rawstr.h"
|
2009-09-28 20:05:20 +04:00
|
|
|
|
#include "curl_memrchr.h"
|
2014-08-19 23:01:28 +04:00
|
|
|
|
#include "inet_pton.h"
|
1999-12-29 17:20:26 +03:00
|
|
|
|
|
2000-10-09 15:12:34 +04:00
|
|
|
|
/* The last #include file should be: */
|
2013-01-04 05:50:28 +04:00
|
|
|
|
#include "memdebug.h"
|
2000-10-09 15:12:34 +04:00
|
|
|
|
|
2004-05-12 16:04:38 +04:00
|
|
|
|
static void freecookie(struct Cookie *co)
|
2002-05-07 13:58:13 +04:00
|
|
|
|
{
|
2004-05-12 16:04:38 +04:00
|
|
|
|
if(co->expirestr)
|
|
|
|
|
free(co->expirestr);
|
2002-05-07 13:58:13 +04:00
|
|
|
|
if(co->domain)
|
|
|
|
|
free(co->domain);
|
|
|
|
|
if(co->path)
|
|
|
|
|
free(co->path);
|
2013-06-12 13:19:56 +04:00
|
|
|
|
if(co->spath)
|
|
|
|
|
free(co->spath);
|
2002-05-07 13:58:13 +04:00
|
|
|
|
if(co->name)
|
|
|
|
|
free(co->name);
|
|
|
|
|
if(co->value)
|
|
|
|
|
free(co->value);
|
2004-10-16 17:54:40 +04:00
|
|
|
|
if(co->maxage)
|
|
|
|
|
free(co->maxage);
|
2004-12-23 01:33:31 +03:00
|
|
|
|
if(co->version)
|
|
|
|
|
free(co->version);
|
2002-05-07 13:58:13 +04:00
|
|
|
|
|
|
|
|
|
free(co);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-11 02:17:15 +04:00
|
|
|
|
static bool tailmatch(const char *cooke_domain, const char *hostname)
|
2003-04-30 21:03:43 +04:00
|
|
|
|
{
|
2013-04-11 02:17:15 +04:00
|
|
|
|
size_t cookie_domain_len = strlen(cooke_domain);
|
|
|
|
|
size_t hostname_len = strlen(hostname);
|
2003-04-30 21:03:43 +04:00
|
|
|
|
|
2013-04-11 02:17:15 +04:00
|
|
|
|
if(hostname_len < cookie_domain_len)
|
2003-04-30 21:03:43 +04:00
|
|
|
|
return FALSE;
|
|
|
|
|
|
2013-04-11 02:17:15 +04:00
|
|
|
|
if(!Curl_raw_equal(cooke_domain, hostname+hostname_len-cookie_domain_len))
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
|
*/
|
|
|
|
|
if(hostname_len == cookie_domain_len)
|
|
|
|
|
return TRUE;
|
|
|
|
|
if('.' == *(hostname + hostname_len - cookie_domain_len - 1))
|
|
|
|
|
return TRUE;
|
|
|
|
|
return FALSE;
|
2003-04-30 21:03:43 +04:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-12 13:19:56 +04:00
|
|
|
|
/*
|
|
|
|
|
* matching cookie path and url path
|
|
|
|
|
* RFC6265 5.1.4 Paths and Path-Match
|
|
|
|
|
*/
|
|
|
|
|
static bool pathmatch(const char* cookie_path, const char* request_uri)
|
2013-05-19 00:51:31 +04:00
|
|
|
|
{
|
2013-06-12 13:19:56 +04:00
|
|
|
|
size_t cookie_path_len;
|
|
|
|
|
size_t uri_path_len;
|
|
|
|
|
char* uri_path = NULL;
|
|
|
|
|
char* pos;
|
|
|
|
|
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-19 00:51:31 +04:00
|
|
|
|
|
2013-06-12 13:19:56 +04:00
|
|
|
|
uri_path = strdup(request_uri);
|
|
|
|
|
if(!uri_path)
|
2013-05-19 00:51:31 +04:00
|
|
|
|
return FALSE;
|
2013-06-12 13: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] != '/') {
|
|
|
|
|
free(uri_path);
|
|
|
|
|
uri_path = strdup("/");
|
|
|
|
|
if(!uri_path)
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 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 /).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
uri_path_len = strlen(uri_path);
|
|
|
|
|
|
|
|
|
|
if(uri_path_len < cookie_path_len) {
|
|
|
|
|
ret = FALSE;
|
|
|
|
|
goto pathmatched;
|
|
|
|
|
}
|
2013-05-19 00:51:31 +04:00
|
|
|
|
|
|
|
|
|
/* not using checkprefix() because matching should be case-sensitive */
|
2013-06-12 13:19:56 +04:00
|
|
|
|
if(strncmp(cookie_path, uri_path, cookie_path_len)) {
|
|
|
|
|
ret = FALSE;
|
|
|
|
|
goto pathmatched;
|
|
|
|
|
}
|
2013-05-19 00:51:31 +04:00
|
|
|
|
|
2013-06-12 13: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-19 00:51:31 +04:00
|
|
|
|
|
|
|
|
|
/* here, cookie_path_len < url_path_len */
|
2013-06-12 13:19:56 +04:00
|
|
|
|
if(uri_path[cookie_path_len] == '/') {
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
goto pathmatched;
|
|
|
|
|
}
|
2013-05-19 00:51:31 +04:00
|
|
|
|
|
2013-06-12 13:19:56 +04:00
|
|
|
|
ret = FALSE;
|
|
|
|
|
|
|
|
|
|
pathmatched:
|
|
|
|
|
free(uri_path);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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 '"'. */
|
|
|
|
|
if(new_path[0] == '\"') {
|
|
|
|
|
memmove((void *)new_path, (const void *)(new_path + 1), strlen(new_path));
|
|
|
|
|
}
|
|
|
|
|
if(new_path[strlen(new_path) - 1] == '\"') {
|
|
|
|
|
new_path[strlen(new_path) - 1] = 0x0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* RFC6265 5.2.4 The Path Attribute */
|
|
|
|
|
if(new_path[0] != '/') {
|
|
|
|
|
/* Let cookie-path be the default-path. */
|
|
|
|
|
free(new_path);
|
|
|
|
|
new_path = strdup("/");
|
|
|
|
|
return new_path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* convert /hoge/ to /hoge */
|
|
|
|
|
len = strlen(new_path);
|
|
|
|
|
if(1 < len && new_path[len - 1] == '/') {
|
|
|
|
|
new_path[len - 1] = 0x0;
|
2013-05-19 00:51:31 +04:00
|
|
|
|
}
|
2013-06-12 13:19:56 +04:00
|
|
|
|
|
|
|
|
|
return new_path;
|
2013-05-19 00:51:31 +04:00
|
|
|
|
}
|
|
|
|
|
|
2005-08-17 12:55:43 +04:00
|
|
|
|
/*
|
|
|
|
|
* Load cookies from all given cookie files (CURLOPT_COOKIEFILE).
|
|
|
|
|
*/
|
|
|
|
|
void Curl_cookie_loadfiles(struct SessionHandle *data)
|
|
|
|
|
{
|
|
|
|
|
struct curl_slist *list = data->change.cookielist;
|
|
|
|
|
if(list) {
|
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
while(list) {
|
|
|
|
|
data->cookies = Curl_cookie_init(data,
|
|
|
|
|
list->data,
|
|
|
|
|
data->cookies,
|
|
|
|
|
data->set.cookiesession);
|
|
|
|
|
list = list->next;
|
|
|
|
|
}
|
|
|
|
|
curl_slist_free_all(data->change.cookielist); /* clean up list */
|
|
|
|
|
data->change.cookielist = NULL; /* don't do this again! */
|
2011-10-11 21:41:30 +04:00
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
|
2005-08-17 12:55:43 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-25 16:23:22 +04:00
|
|
|
|
/*
|
|
|
|
|
* strstore() makes a strdup() on the 'newstr' and if '*str' is non-NULL
|
|
|
|
|
* that will be freed before the allocated string is stored there.
|
|
|
|
|
*
|
|
|
|
|
* It is meant to easily replace strdup()
|
|
|
|
|
*/
|
|
|
|
|
static void strstore(char **str, const char *newstr)
|
|
|
|
|
{
|
|
|
|
|
if(*str)
|
|
|
|
|
free(*str);
|
|
|
|
|
*str = strdup(newstr);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 10:51:22 +04:00
|
|
|
|
/*
|
|
|
|
|
* remove_expired() removes expired cookies.
|
|
|
|
|
*/
|
|
|
|
|
static void remove_expired(struct CookieInfo *cookies)
|
|
|
|
|
{
|
|
|
|
|
struct Cookie *co, *nx, *pv;
|
|
|
|
|
curl_off_t now = (curl_off_t)time(NULL);
|
|
|
|
|
|
|
|
|
|
co = cookies->cookies;
|
|
|
|
|
pv = NULL;
|
|
|
|
|
while(co) {
|
|
|
|
|
nx = co->next;
|
|
|
|
|
if((co->expirestr || co->maxage) && co->expires < now) {
|
|
|
|
|
if(co == cookies->cookies) {
|
|
|
|
|
cookies->cookies = co->next;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
pv->next = co->next;
|
|
|
|
|
}
|
|
|
|
|
cookies->numcookies--;
|
|
|
|
|
freecookie(co);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
pv = co;
|
|
|
|
|
}
|
|
|
|
|
co = nx;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-09-27 00:51:51 +04:00
|
|
|
|
|
2014-08-19 23:01:28 +04:00
|
|
|
|
/*
|
|
|
|
|
* Return true if the given string is an IP(v4|v6) address.
|
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
*
|
2001-08-23 18:05:25 +04:00
|
|
|
|
* Curl_cookie_add()
|
1999-12-29 17:20:26 +03:00
|
|
|
|
*
|
|
|
|
|
* Add a single cookie line to the cookie keeping object.
|
|
|
|
|
*
|
2013-05-22 01:28:59 +04:00
|
|
|
|
* Be aware that sometimes we get an IP-only host name, and that might also be
|
|
|
|
|
* a numerical IPv6 address.
|
|
|
|
|
*
|
1999-12-29 17:20:26 +03:00
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
2001-01-05 13:11:41 +03:00
|
|
|
|
struct Cookie *
|
2003-08-11 13:55:11 +04:00
|
|
|
|
Curl_cookie_add(struct SessionHandle *data,
|
|
|
|
|
/* 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 */
|
2004-05-12 16:04:38 +04:00
|
|
|
|
|
2003-08-11 13:55:11 +04:00
|
|
|
|
struct CookieInfo *c,
|
2001-01-05 13:11:41 +03:00
|
|
|
|
bool httpheader, /* TRUE if HTTP header-style line */
|
2002-07-30 02:22:49 +04:00
|
|
|
|
char *lineptr, /* first character of the line */
|
2007-08-29 09:36:53 +04:00
|
|
|
|
const char *domain, /* default domain */
|
|
|
|
|
const char *path) /* full path used when this cookie is set,
|
2009-09-27 00:51:51 +04:00
|
|
|
|
used to get default path for the cookie
|
|
|
|
|
unless set */
|
1999-12-29 17:20:26 +03:00
|
|
|
|
{
|
|
|
|
|
struct Cookie *clist;
|
|
|
|
|
char name[MAX_NAME];
|
|
|
|
|
struct Cookie *co;
|
2001-08-23 18:05:25 +04:00
|
|
|
|
struct Cookie *lastc=NULL;
|
1999-12-29 17:20:26 +03:00
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
bool replace_old = FALSE;
|
2003-04-30 21:03:43 +04:00
|
|
|
|
bool badcookie = FALSE; /* cookies are good by default. mmmmm yummy */
|
1999-12-29 17:20:26 +03:00
|
|
|
|
|
2007-03-25 06:30:58 +04:00
|
|
|
|
#ifdef CURL_DISABLE_VERBOSE_STRINGS
|
|
|
|
|
(void)data;
|
|
|
|
|
#endif
|
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
/* First, alloc and init a new struct for it */
|
2009-11-18 13:33:54 +03:00
|
|
|
|
co = calloc(1, sizeof(struct Cookie));
|
1999-12-29 17:20:26 +03:00
|
|
|
|
if(!co)
|
|
|
|
|
return NULL; /* bail out if we're this low on memory */
|
|
|
|
|
|
|
|
|
|
if(httpheader) {
|
|
|
|
|
/* This line was read off a HTTP-header */
|
2007-08-29 09:36:53 +04:00
|
|
|
|
const char *ptr;
|
|
|
|
|
const char *semiptr;
|
|
|
|
|
char *what;
|
2004-06-23 01:15:51 +04:00
|
|
|
|
|
|
|
|
|
what = malloc(MAX_COOKIE_LINE);
|
|
|
|
|
if(!what) {
|
|
|
|
|
free(co);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
semiptr=strchr(lineptr, ';'); /* first, find a semicolon */
|
2002-07-30 02:22:49 +04:00
|
|
|
|
|
2007-02-13 20:47:27 +03:00
|
|
|
|
while(*lineptr && ISBLANK(*lineptr))
|
2002-07-30 02:22:49 +04:00
|
|
|
|
lineptr++;
|
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
ptr = lineptr;
|
2000-09-26 02:14:42 +04:00
|
|
|
|
do {
|
2011-08-09 16:02:05 +04:00
|
|
|
|
/* we have a <what>=<this> pair or a stand-alone word here */
|
|
|
|
|
name[0]=what[0]=0; /* init the buffers */
|
|
|
|
|
if(1 <= sscanf(ptr, "%" MAX_NAME_TXT "[^;\r\n =]=%"
|
|
|
|
|
MAX_COOKIE_LINE_TXT "[^;\r\n]",
|
|
|
|
|
name, what)) {
|
|
|
|
|
/* 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. */
|
|
|
|
|
const char *whatptr;
|
|
|
|
|
bool done = FALSE;
|
|
|
|
|
bool sep;
|
|
|
|
|
size_t len=strlen(what);
|
|
|
|
|
const char *endofn = &ptr[ strlen(name) ];
|
|
|
|
|
|
|
|
|
|
/* skip trailing spaces in name */
|
|
|
|
|
while(*endofn && ISBLANK(*endofn))
|
|
|
|
|
endofn++;
|
|
|
|
|
|
|
|
|
|
/* name ends with a '=' ? */
|
2011-09-05 22:46:09 +04:00
|
|
|
|
sep = (*endofn == '=')?TRUE:FALSE;
|
2011-08-09 16:02:05 +04:00
|
|
|
|
|
|
|
|
|
/* Strip off trailing whitespace from the 'what' */
|
|
|
|
|
while(len && ISBLANK(what[len-1])) {
|
|
|
|
|
what[len-1]=0;
|
|
|
|
|
len--;
|
|
|
|
|
}
|
2002-02-27 10:38:04 +03:00
|
|
|
|
|
2011-08-09 16:02:05 +04:00
|
|
|
|
/* Skip leading whitespace from the 'what' */
|
|
|
|
|
whatptr=what;
|
|
|
|
|
while(*whatptr && ISBLANK(*whatptr))
|
|
|
|
|
whatptr++;
|
2002-04-14 22:21:17 +04:00
|
|
|
|
|
2011-08-09 16:02:05 +04:00
|
|
|
|
if(!len) {
|
|
|
|
|
/* this was a "<name>=" with no content, and we must allow
|
|
|
|
|
'secure' and 'httponly' specified this weirdly */
|
|
|
|
|
done = TRUE;
|
|
|
|
|
if(Curl_raw_equal("secure", name))
|
|
|
|
|
co->secure = TRUE;
|
|
|
|
|
else if(Curl_raw_equal("httponly", name))
|
|
|
|
|
co->httponly = TRUE;
|
|
|
|
|
else if(sep)
|
|
|
|
|
/* there was a '=' so we're not done parsing this field */
|
|
|
|
|
done = FALSE;
|
|
|
|
|
}
|
|
|
|
|
if(done)
|
|
|
|
|
;
|
|
|
|
|
else if(Curl_raw_equal("path", name)) {
|
|
|
|
|
strstore(&co->path, whatptr);
|
|
|
|
|
if(!co->path) {
|
|
|
|
|
badcookie = TRUE; /* out of memory bad */
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-06-12 13:19:56 +04:00
|
|
|
|
co->spath = sanitize_cookie_path(co->path);
|
|
|
|
|
if(!co->spath) {
|
|
|
|
|
badcookie = TRUE; /* out of memory bad */
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-08-09 16:02:05 +04:00
|
|
|
|
}
|
|
|
|
|
else if(Curl_raw_equal("domain", name)) {
|
2014-08-19 23:01:28 +04:00
|
|
|
|
bool is_ip;
|
2014-08-19 23:11:20 +04:00
|
|
|
|
const char *dotp;
|
2014-08-19 23:01:28 +04:00
|
|
|
|
|
2013-05-22 01:28:59 +04: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 16:02:05 +04:00
|
|
|
|
|
|
|
|
|
if('.' == whatptr[0])
|
2013-05-22 01:28:59 +04:00
|
|
|
|
whatptr++; /* ignore preceding dot */
|
|
|
|
|
|
2014-08-19 23:01:28 +04:00
|
|
|
|
is_ip = isip(domain ? domain : whatptr);
|
|
|
|
|
|
2014-08-19 23:11:20 +04:00
|
|
|
|
/* check for more dots */
|
|
|
|
|
dotp = strchr(whatptr, '.');
|
|
|
|
|
if(!dotp)
|
|
|
|
|
domain=":";
|
|
|
|
|
|
2014-08-19 23:01:28 +04:00
|
|
|
|
if(!domain
|
|
|
|
|
|| (is_ip && !strcmp(whatptr, domain))
|
|
|
|
|
|| (!is_ip && tailmatch(whatptr, domain))) {
|
|
|
|
|
strstore(&co->domain, whatptr);
|
2013-05-22 01:28:59 +04:00
|
|
|
|
if(!co->domain) {
|
|
|
|
|
badcookie = TRUE;
|
|
|
|
|
break;
|
2004-05-12 16:04:38 +04:00
|
|
|
|
}
|
2014-08-19 23:01:28 +04:00
|
|
|
|
if(!is_ip)
|
|
|
|
|
co->tailmatch=TRUE; /* we always do that if the domain name was
|
|
|
|
|
given */
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
2011-08-09 16:02:05 +04:00
|
|
|
|
else {
|
2013-05-22 01:28:59 +04: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. */
|
|
|
|
|
badcookie=TRUE;
|
|
|
|
|
infof(data, "skipped cookie with bad tailmatch domain: %s\n",
|
|
|
|
|
whatptr);
|
2000-02-02 02:54:51 +03:00
|
|
|
|
}
|
2011-08-09 16:02:05 +04:00
|
|
|
|
}
|
|
|
|
|
else if(Curl_raw_equal("version", name)) {
|
|
|
|
|
strstore(&co->version, whatptr);
|
|
|
|
|
if(!co->version) {
|
|
|
|
|
badcookie = TRUE;
|
|
|
|
|
break;
|
2000-02-02 02:54:51 +03:00
|
|
|
|
}
|
2011-08-09 16:02:05 +04:00
|
|
|
|
}
|
|
|
|
|
else if(Curl_raw_equal("max-age", name)) {
|
|
|
|
|
/* 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.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
strstore(&co->maxage, whatptr);
|
|
|
|
|
if(!co->maxage) {
|
|
|
|
|
badcookie = TRUE;
|
|
|
|
|
break;
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
2011-08-09 16:02:05 +04:00
|
|
|
|
}
|
|
|
|
|
else if(Curl_raw_equal("expires", name)) {
|
|
|
|
|
strstore(&co->expirestr, whatptr);
|
|
|
|
|
if(!co->expirestr) {
|
|
|
|
|
badcookie = TRUE;
|
|
|
|
|
break;
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-08-09 16:02:05 +04:00
|
|
|
|
else if(!co->name) {
|
|
|
|
|
co->name = strdup(name);
|
|
|
|
|
co->value = strdup(whatptr);
|
|
|
|
|
if(!co->name || !co->value) {
|
|
|
|
|
badcookie = TRUE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
2011-08-09 16:02:05 +04:00
|
|
|
|
/*
|
|
|
|
|
else this is the second (or more) name we don't know
|
|
|
|
|
about! */
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
|
|
|
|
else {
|
2011-08-09 16:02:05 +04:00
|
|
|
|
/* this is an "illegal" <what>=<this> pair */
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
2011-08-09 16:02:05 +04:00
|
|
|
|
|
2002-02-26 16:07:53 +03:00
|
|
|
|
if(!semiptr || !*semiptr) {
|
|
|
|
|
/* we already know there are no more cookies */
|
|
|
|
|
semiptr = NULL;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2000-09-26 02:14:42 +04:00
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
ptr=semiptr+1;
|
2009-06-11 01:26:11 +04:00
|
|
|
|
while(*ptr && ISBLANK(*ptr))
|
1999-12-29 17:20:26 +03:00
|
|
|
|
ptr++;
|
|
|
|
|
semiptr=strchr(ptr, ';'); /* now, find the next semicolon */
|
2002-01-08 02:05:36 +03:00
|
|
|
|
|
|
|
|
|
if(!semiptr && *ptr)
|
|
|
|
|
/* There are no more semicolons, but there's a final name=value pair
|
|
|
|
|
coming up */
|
2002-02-26 16:07:53 +03:00
|
|
|
|
semiptr=strchr(ptr, '\0');
|
2000-09-26 02:14:42 +04:00
|
|
|
|
} while(semiptr);
|
2001-09-26 11:08:29 +04:00
|
|
|
|
|
2014-01-16 11:51:30 +04:00
|
|
|
|
if(co->maxage) {
|
|
|
|
|
co->expires =
|
|
|
|
|
curlx_strtoofft((*co->maxage=='\"')?
|
|
|
|
|
&co->maxage[1]:&co->maxage[0], NULL, 10);
|
|
|
|
|
if(CURL_OFF_T_MAX - now < co->expires)
|
|
|
|
|
/* avoid overflow */
|
|
|
|
|
co->expires = CURL_OFF_T_MAX;
|
|
|
|
|
else
|
|
|
|
|
co->expires += now;
|
|
|
|
|
}
|
|
|
|
|
else if(co->expirestr) {
|
|
|
|
|
/* Note that if the date couldn't get parsed for whatever reason,
|
|
|
|
|
the cookie will be treated as a session cookie */
|
|
|
|
|
co->expires = curl_getdate(co->expirestr, NULL);
|
|
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
if(co->expires == 0)
|
|
|
|
|
co->expires = 1;
|
|
|
|
|
else if(co->expires < 0)
|
|
|
|
|
co->expires = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-12 16:04:38 +04:00
|
|
|
|
if(!badcookie && !co->domain) {
|
|
|
|
|
if(domain) {
|
|
|
|
|
/* no domain was given in the header line, set the default */
|
|
|
|
|
co->domain=strdup(domain);
|
|
|
|
|
if(!co->domain)
|
|
|
|
|
badcookie = TRUE;
|
|
|
|
|
}
|
2002-02-26 16:07:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
2004-05-12 16:04:38 +04:00
|
|
|
|
if(!badcookie && !co->path && path) {
|
2009-09-27 00:51:51 +04: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. */
|
|
|
|
|
char *queryp = strchr(path, '?');
|
|
|
|
|
|
|
|
|
|
/* queryp is where the interesting part of the path ends, so now we
|
|
|
|
|
want to the find the last */
|
|
|
|
|
char *endslash;
|
|
|
|
|
if(!queryp)
|
|
|
|
|
endslash = strrchr(path, '/');
|
|
|
|
|
else
|
2009-09-28 21:01:23 +04:00
|
|
|
|
endslash = memrchr(path, '/', (size_t)(queryp - path));
|
2003-04-30 21:03:43 +04:00
|
|
|
|
if(endslash) {
|
2009-09-28 21:01:23 +04:00
|
|
|
|
size_t pathlen = (size_t)(endslash-path+1); /* include ending slash */
|
2003-04-30 21:03:43 +04:00
|
|
|
|
co->path=malloc(pathlen+1); /* one extra for the zero byte */
|
|
|
|
|
if(co->path) {
|
|
|
|
|
memcpy(co->path, path, pathlen);
|
|
|
|
|
co->path[pathlen]=0; /* zero terminate */
|
2013-06-12 13:19:56 +04:00
|
|
|
|
co->spath = sanitize_cookie_path(co->path);
|
|
|
|
|
if(!co->spath)
|
|
|
|
|
badcookie = TRUE; /* out of memory bad */
|
2003-04-30 21:03:43 +04:00
|
|
|
|
}
|
2004-05-12 16:04:38 +04:00
|
|
|
|
else
|
|
|
|
|
badcookie = TRUE;
|
2003-04-30 21:03:43 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2004-05-12 16:04:38 +04:00
|
|
|
|
|
2004-06-23 01:15:51 +04:00
|
|
|
|
free(what);
|
|
|
|
|
|
2004-05-12 16:04:38 +04:00
|
|
|
|
if(badcookie || !co->name) {
|
|
|
|
|
/* we didn't get a cookie name or a bad one,
|
|
|
|
|
this is an illegal line, bail out */
|
|
|
|
|
freecookie(co);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* 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 09:36:53 +04:00
|
|
|
|
char *ptr;
|
1999-12-29 17:20:26 +03:00
|
|
|
|
char *firstptr;
|
2009-01-15 11:32:58 +03:00
|
|
|
|
char *tok_buf=NULL;
|
1999-12-29 17:20:26 +03:00
|
|
|
|
int fields;
|
|
|
|
|
|
2008-09-08 15:36:19 +04: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
|
2011-04-19 17:54:13 +04:00
|
|
|
|
lines are preceded with #HttpOnly_ and then everything is
|
2008-01-31 15:21:57 +03:00
|
|
|
|
as usual, so we skip 10 characters of the line..
|
|
|
|
|
*/
|
2011-04-20 17:17:42 +04:00
|
|
|
|
if(strncmp(lineptr, "#HttpOnly_", 10) == 0) {
|
2008-01-31 15:21:57 +03:00
|
|
|
|
lineptr += 10;
|
|
|
|
|
co->httponly = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
if(lineptr[0]=='#') {
|
|
|
|
|
/* don't even try the comments */
|
|
|
|
|
free(co);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
/* strip off the possible end-of-line characters */
|
2000-05-22 18:12:12 +04:00
|
|
|
|
ptr=strchr(lineptr, '\r');
|
|
|
|
|
if(ptr)
|
1999-12-29 17:20:26 +03:00
|
|
|
|
*ptr=0; /* clear it */
|
2000-05-22 18:12:12 +04:00
|
|
|
|
ptr=strchr(lineptr, '\n');
|
|
|
|
|
if(ptr)
|
1999-12-29 17:20:26 +03:00
|
|
|
|
*ptr=0; /* clear it */
|
|
|
|
|
|
2004-05-12 16:04:38 +04:00
|
|
|
|
firstptr=strtok_r(lineptr, "\t", &tok_buf); /* tokenize it on the TAB */
|
1999-12-29 17:20:26 +03:00
|
|
|
|
|
|
|
|
|
/* Now loop through the fields and init the struct we already have
|
|
|
|
|
allocated */
|
2004-05-12 16:04:38 +04:00
|
|
|
|
for(ptr=firstptr, fields=0; ptr && !badcookie;
|
2003-04-30 21:03:43 +04:00
|
|
|
|
ptr=strtok_r(NULL, "\t", &tok_buf), fields++) {
|
1999-12-29 17:20:26 +03:00
|
|
|
|
switch(fields) {
|
|
|
|
|
case 0:
|
2011-04-19 17:54:13 +04:00
|
|
|
|
if(ptr[0]=='.') /* skip preceding dots */
|
2003-05-16 02:28:19 +04:00
|
|
|
|
ptr++;
|
1999-12-29 17:20:26 +03:00
|
|
|
|
co->domain = strdup(ptr);
|
2004-05-12 16:04:38 +04:00
|
|
|
|
if(!co->domain)
|
|
|
|
|
badcookie = TRUE;
|
1999-12-29 17:20:26 +03:00
|
|
|
|
break;
|
|
|
|
|
case 1:
|
2001-05-23 17:04:19 +04:00
|
|
|
|
/* This field got its explanation on the 23rd of May 2001 by
|
|
|
|
|
Andr<EFBFBD>s Garc<EFBFBD>a:
|
|
|
|
|
|
|
|
|
|
flag: A TRUE/FALSE value indicating if all machines within a given
|
|
|
|
|
domain can access the variable. This value is set automatically by
|
|
|
|
|
the browser, depending on the value you set for the domain.
|
|
|
|
|
|
|
|
|
|
As far as I can see, it is set to true when the cookie says
|
|
|
|
|
.domain.com and to false when the domain is complete www.domain.com
|
|
|
|
|
*/
|
2011-09-05 22:46:09 +04:00
|
|
|
|
co->tailmatch = Curl_raw_equal(ptr, "TRUE")?TRUE:FALSE;
|
1999-12-29 17:20:26 +03:00
|
|
|
|
break;
|
|
|
|
|
case 2:
|
2001-05-23 13:26:45 +04:00
|
|
|
|
/* It turns out, that sometimes the file format allows the path
|
|
|
|
|
field to remain not filled in, we try to detect this and work
|
|
|
|
|
around it! Andr<EFBFBD>s Garc<EFBFBD>a made us aware of this... */
|
2007-11-07 12:21:35 +03:00
|
|
|
|
if(strcmp("TRUE", ptr) && strcmp("FALSE", ptr)) {
|
2001-05-23 13:26:45 +04:00
|
|
|
|
/* only if the path doesn't look like a boolean option! */
|
|
|
|
|
co->path = strdup(ptr);
|
2004-05-12 16:04:38 +04:00
|
|
|
|
if(!co->path)
|
|
|
|
|
badcookie = TRUE;
|
2013-06-12 13:19:56 +04:00
|
|
|
|
else {
|
|
|
|
|
co->spath = sanitize_cookie_path(co->path);
|
|
|
|
|
if(!co->spath) {
|
|
|
|
|
badcookie = TRUE; /* out of memory bad */
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-05-23 13:26:45 +04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* this doesn't look like a path, make one up! */
|
|
|
|
|
co->path = strdup("/");
|
2004-05-12 16:04:38 +04:00
|
|
|
|
if(!co->path)
|
|
|
|
|
badcookie = TRUE;
|
2013-06-12 13:19:56 +04:00
|
|
|
|
co->spath = strdup("/");
|
|
|
|
|
if(!co->spath)
|
|
|
|
|
badcookie = TRUE;
|
2001-05-23 13:26:45 +04:00
|
|
|
|
fields++; /* add a field and fall down to secure */
|
|
|
|
|
/* FALLTHROUGH */
|
1999-12-29 17:20:26 +03:00
|
|
|
|
case 3:
|
2011-09-05 22:46:09 +04:00
|
|
|
|
co->secure = Curl_raw_equal(ptr, "TRUE")?TRUE:FALSE;
|
1999-12-29 17:20:26 +03:00
|
|
|
|
break;
|
|
|
|
|
case 4:
|
2005-08-17 12:55:43 +04:00
|
|
|
|
co->expires = curlx_strtoofft(ptr, NULL, 10);
|
1999-12-29 17:20:26 +03:00
|
|
|
|
break;
|
|
|
|
|
case 5:
|
|
|
|
|
co->name = strdup(ptr);
|
2004-05-12 16:04:38 +04:00
|
|
|
|
if(!co->name)
|
|
|
|
|
badcookie = TRUE;
|
1999-12-29 17:20:26 +03:00
|
|
|
|
break;
|
|
|
|
|
case 6:
|
|
|
|
|
co->value = strdup(ptr);
|
2004-05-12 16:04:38 +04:00
|
|
|
|
if(!co->value)
|
|
|
|
|
badcookie = TRUE;
|
1999-12-29 17:20:26 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-08-11 13:55:11 +04:00
|
|
|
|
if(6 == fields) {
|
|
|
|
|
/* we got a cookie with blank contents, fix it */
|
|
|
|
|
co->value = strdup("");
|
2004-05-12 16:04:38 +04:00
|
|
|
|
if(!co->value)
|
|
|
|
|
badcookie = TRUE;
|
|
|
|
|
else
|
|
|
|
|
fields++;
|
2003-08-11 13:55:11 +04:00
|
|
|
|
}
|
2004-05-12 16: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 17:20:26 +03:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2004-05-12 16:04:38 +04:00
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2002-05-07 13: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 16:04:38 +04:00
|
|
|
|
freecookie(co);
|
2002-05-07 13:58:13 +04:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2001-08-23 18:05:25 +04:00
|
|
|
|
co->livecookie = c->running;
|
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
/* now, we have parsed the incoming line, we must now check if this
|
|
|
|
|
superceeds an already existing cookie, which it may if the previous have
|
|
|
|
|
the same domain and path as this */
|
|
|
|
|
|
2013-09-17 10:51:22 +04:00
|
|
|
|
/* at first, remove expired cookies */
|
|
|
|
|
remove_expired(c);
|
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
clist = c->cookies;
|
|
|
|
|
replace_old = FALSE;
|
|
|
|
|
while(clist) {
|
2008-10-16 12:23:48 +04:00
|
|
|
|
if(Curl_raw_equal(clist->name, co->name)) {
|
1999-12-29 17:20:26 +03:00
|
|
|
|
/* the names are identical */
|
|
|
|
|
|
|
|
|
|
if(clist->domain && co->domain) {
|
2008-10-16 12:23:48 +04:00
|
|
|
|
if(Curl_raw_equal(clist->domain, co->domain))
|
2003-05-16 02:28:19 +04:00
|
|
|
|
/* The domains are identical */
|
1999-12-29 17:20:26 +03:00
|
|
|
|
replace_old=TRUE;
|
|
|
|
|
}
|
|
|
|
|
else if(!clist->domain && !co->domain)
|
|
|
|
|
replace_old = TRUE;
|
|
|
|
|
|
|
|
|
|
if(replace_old) {
|
|
|
|
|
/* the domains were identical */
|
|
|
|
|
|
2013-06-12 13:19:56 +04:00
|
|
|
|
if(clist->spath && co->spath) {
|
|
|
|
|
if(Curl_raw_equal(clist->spath, co->spath)) {
|
1999-12-29 17:20:26 +03:00
|
|
|
|
replace_old = TRUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
replace_old = FALSE;
|
|
|
|
|
}
|
2013-06-12 13:19:56 +04:00
|
|
|
|
else if(!clist->spath && !co->spath)
|
1999-12-29 17:20:26 +03:00
|
|
|
|
replace_old = TRUE;
|
|
|
|
|
else
|
|
|
|
|
replace_old = FALSE;
|
2004-05-12 16:04:38 +04:00
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2001-08-23 18:05:25 +04:00
|
|
|
|
if(replace_old && !co->livecookie && clist->livecookie) {
|
|
|
|
|
/* Both cookies matched fine, except that the already present
|
|
|
|
|
cookie is "live", which means it was set from a header, while
|
|
|
|
|
the new one isn't "live" and thus only read from a file. We let
|
|
|
|
|
live cookies stay alive */
|
|
|
|
|
|
|
|
|
|
/* Free the newcomer and get out of here! */
|
2004-05-12 16:04:38 +04:00
|
|
|
|
freecookie(co);
|
2001-08-23 18:05:25 +04:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
if(replace_old) {
|
|
|
|
|
co->next = clist->next; /* get the next-pointer first */
|
|
|
|
|
|
|
|
|
|
/* then free all the old pointers */
|
2008-10-14 01:03:12 +04:00
|
|
|
|
free(clist->name);
|
1999-12-29 17:20:26 +03:00
|
|
|
|
if(clist->value)
|
|
|
|
|
free(clist->value);
|
|
|
|
|
if(clist->domain)
|
|
|
|
|
free(clist->domain);
|
|
|
|
|
if(clist->path)
|
|
|
|
|
free(clist->path);
|
2013-06-12 13:19:56 +04:00
|
|
|
|
if(clist->spath)
|
|
|
|
|
free(clist->spath);
|
1999-12-29 17:20:26 +03:00
|
|
|
|
if(clist->expirestr)
|
|
|
|
|
free(clist->expirestr);
|
|
|
|
|
|
2000-02-02 02:54:51 +03:00
|
|
|
|
if(clist->version)
|
|
|
|
|
free(clist->version);
|
|
|
|
|
if(clist->maxage)
|
|
|
|
|
free(clist->maxage);
|
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
*clist = *co; /* then store all the new data */
|
2001-10-24 15:36:55 +04:00
|
|
|
|
|
|
|
|
|
free(co); /* free the newly alloced memory */
|
|
|
|
|
co = clist; /* point to the previous struct instead */
|
1999-12-29 17:20:26 +03:00
|
|
|
|
|
2002-01-07 17:56:15 +03:00
|
|
|
|
/* We have replaced a cookie, now skip the rest of the list but
|
|
|
|
|
make sure the 'lastc' pointer is properly set */
|
|
|
|
|
do {
|
|
|
|
|
lastc = clist;
|
|
|
|
|
clist = clist->next;
|
|
|
|
|
} while(clist);
|
|
|
|
|
break;
|
|
|
|
|
}
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
2001-08-23 18:05:25 +04:00
|
|
|
|
lastc = clist;
|
1999-12-29 17:20:26 +03:00
|
|
|
|
clist = clist->next;
|
|
|
|
|
}
|
|
|
|
|
|
2003-08-11 13:55:11 +04:00
|
|
|
|
if(c->running)
|
|
|
|
|
/* Only show this when NOT reading the cookies from a file */
|
2010-01-28 18:34:18 +03:00
|
|
|
|
infof(data, "%s cookie %s=\"%s\" for domain %s, path %s, "
|
2013-12-31 15:10:25 +04:00
|
|
|
|
"expire %" CURL_FORMAT_CURL_OFF_T "\n",
|
2003-08-11 13:55:11 +04:00
|
|
|
|
replace_old?"Replaced":"Added", co->name, co->value,
|
|
|
|
|
co->domain, co->path, co->expires);
|
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
if(!replace_old) {
|
2001-08-23 18:05:25 +04:00
|
|
|
|
/* then make the last item point on this new one */
|
|
|
|
|
if(lastc)
|
|
|
|
|
lastc->next = co;
|
|
|
|
|
else
|
|
|
|
|
c->cookies = co;
|
2013-04-02 13:45:15 +04:00
|
|
|
|
c->numcookies++; /* one more cookie in the jar */
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
2001-08-23 18:05:25 +04:00
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
return co;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
|
*
|
2001-08-23 18:05:25 +04:00
|
|
|
|
* Curl_cookie_init()
|
1999-12-29 17:20:26 +03:00
|
|
|
|
*
|
|
|
|
|
* Inits a cookie struct to read data from a local file. This is always
|
|
|
|
|
* called before any cookies are set. File may be NULL.
|
|
|
|
|
*
|
2002-05-07 13:58:13 +04:00
|
|
|
|
* If 'newsession' is TRUE, discard all "session cookies" on read from file.
|
|
|
|
|
*
|
1999-12-29 17:20:26 +03:00
|
|
|
|
****************************************************************************/
|
2003-08-11 13:55:11 +04:00
|
|
|
|
struct CookieInfo *Curl_cookie_init(struct SessionHandle *data,
|
2007-08-29 09:36:53 +04:00
|
|
|
|
const char *file,
|
2002-05-07 13:58:13 +04:00
|
|
|
|
struct CookieInfo *inc,
|
|
|
|
|
bool newsession)
|
1999-12-29 17:20:26 +03:00
|
|
|
|
{
|
|
|
|
|
struct CookieInfo *c;
|
|
|
|
|
FILE *fp;
|
2000-02-11 02:14:53 +03:00
|
|
|
|
bool fromfile=TRUE;
|
2004-05-12 16:04:38 +04:00
|
|
|
|
|
2001-08-23 18:05:25 +04:00
|
|
|
|
if(NULL == inc) {
|
|
|
|
|
/* we didn't get a struct, create one */
|
2008-09-06 08:47:14 +04:00
|
|
|
|
c = calloc(1, sizeof(struct CookieInfo));
|
2001-08-23 18:05:25 +04:00
|
|
|
|
if(!c)
|
|
|
|
|
return NULL; /* failed to get memory */
|
|
|
|
|
c->filename = strdup(file?file:"none"); /* copy the name just in case */
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* we got an already existing one, use that */
|
|
|
|
|
c = inc;
|
|
|
|
|
}
|
|
|
|
|
c->running = FALSE; /* this is not running, this is init */
|
1999-12-29 17:20:26 +03:00
|
|
|
|
|
2001-10-10 16:48:32 +04:00
|
|
|
|
if(file && strequal(file, "-")) {
|
2000-02-11 02:14:53 +03:00
|
|
|
|
fp = stdin;
|
|
|
|
|
fromfile=FALSE;
|
|
|
|
|
}
|
2005-03-04 03:26:50 +03:00
|
|
|
|
else if(file && !*file) {
|
|
|
|
|
/* points to a "" string */
|
|
|
|
|
fp = NULL;
|
|
|
|
|
}
|
2000-02-11 02:14:53 +03:00
|
|
|
|
else
|
|
|
|
|
fp = file?fopen(file, "r"):NULL;
|
|
|
|
|
|
2002-05-07 13:58:13 +04:00
|
|
|
|
c->newsession = newsession; /* new session? */
|
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
if(fp) {
|
2001-08-23 18:05:25 +04:00
|
|
|
|
char *lineptr;
|
|
|
|
|
bool headerline;
|
|
|
|
|
|
2008-09-06 09:29:05 +04:00
|
|
|
|
char *line = malloc(MAX_COOKIE_LINE);
|
2004-06-30 16:05:07 +04:00
|
|
|
|
if(line) {
|
|
|
|
|
while(fgets(line, MAX_COOKIE_LINE, fp)) {
|
|
|
|
|
if(checkprefix("Set-Cookie:", line)) {
|
|
|
|
|
/* This is a cookie line, get it! */
|
|
|
|
|
lineptr=&line[11];
|
|
|
|
|
headerline=TRUE;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
lineptr=line;
|
|
|
|
|
headerline=FALSE;
|
|
|
|
|
}
|
2007-02-13 20:47:27 +03:00
|
|
|
|
while(*lineptr && ISBLANK(*lineptr))
|
2004-06-30 16:05:07 +04:00
|
|
|
|
lineptr++;
|
|
|
|
|
|
|
|
|
|
Curl_cookie_add(data, c, headerline, lineptr, NULL, NULL);
|
|
|
|
|
}
|
|
|
|
|
free(line); /* free the line buffer */
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
2000-02-11 02:14:53 +03:00
|
|
|
|
if(fromfile)
|
|
|
|
|
fclose(fp);
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2002-05-07 13:58:13 +04:00
|
|
|
|
c->running = TRUE; /* now, we're running */
|
2001-08-23 18:05:25 +04:00
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-20 02:19:59 +03:00
|
|
|
|
/* sort this so that the longest path gets before the shorter path */
|
|
|
|
|
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 12:34:32 +04:00
|
|
|
|
size_t l1, l2;
|
2010-01-20 02:19:59 +03:00
|
|
|
|
|
2013-04-07 12: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-20 02:19:59 +03:00
|
|
|
|
|
2013-04-07 12: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 */
|
|
|
|
|
|
|
|
|
|
/* 3 - compare cookie names */
|
|
|
|
|
if(c1->name && c2->name)
|
|
|
|
|
return strcmp(c1->name, c2->name);
|
|
|
|
|
|
|
|
|
|
/* sorry, can't be more deterministic */
|
|
|
|
|
return 0;
|
2010-01-20 02:19:59 +03:00
|
|
|
|
}
|
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
/*****************************************************************************
|
|
|
|
|
*
|
2001-08-23 18:05:25 +04:00
|
|
|
|
* Curl_cookie_getlist()
|
1999-12-29 17:20:26 +03: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.
|
|
|
|
|
*
|
|
|
|
|
* It shall only return cookies that haven't expired.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2001-01-05 13:11:41 +03:00
|
|
|
|
struct Cookie *Curl_cookie_getlist(struct CookieInfo *c,
|
2007-08-29 09:36:53 +04:00
|
|
|
|
const char *host, const char *path,
|
2008-09-08 15:36:19 +04:00
|
|
|
|
bool secure)
|
1999-12-29 17:20:26 +03:00
|
|
|
|
{
|
2006-05-25 02:46:38 +04:00
|
|
|
|
struct Cookie *newco;
|
|
|
|
|
struct Cookie *co;
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
struct Cookie *mainco=NULL;
|
2010-01-27 06:43:34 +03:00
|
|
|
|
size_t matches = 0;
|
2014-08-19 23:01:28 +04:00
|
|
|
|
bool is_ip;
|
2006-05-25 02:46:38 +04:00
|
|
|
|
|
|
|
|
|
if(!c || !c->cookies)
|
|
|
|
|
return NULL; /* no cookie struct or no cookies in the struct */
|
|
|
|
|
|
2013-09-17 10:51:22 +04:00
|
|
|
|
/* at first, remove expired cookies */
|
|
|
|
|
remove_expired(c);
|
|
|
|
|
|
2014-08-19 23:01:28 +04:00
|
|
|
|
/* check if host is an IP(v4|v6) address */
|
|
|
|
|
is_ip = isip(host);
|
|
|
|
|
|
2006-05-25 02:46:38 +04:00
|
|
|
|
co = c->cookies;
|
|
|
|
|
|
|
|
|
|
while(co) {
|
|
|
|
|
/* only process this cookie if it is not expired or had no expire
|
|
|
|
|
date AND that if the cookie requires we're secure we must only
|
|
|
|
|
continue if we are! */
|
2011-04-23 01:01:30 +04:00
|
|
|
|
if((!co->expires || (co->expires > now)) &&
|
|
|
|
|
(co->secure?secure:TRUE)) {
|
2006-05-25 02:46:38 +04:00
|
|
|
|
|
|
|
|
|
/* now check if the domain is correct */
|
|
|
|
|
if(!co->domain ||
|
2014-08-19 23:01:28 +04:00
|
|
|
|
(co->tailmatch && !is_ip && tailmatch(co->domain, host)) ||
|
|
|
|
|
((!co->tailmatch || is_ip) && Curl_raw_equal(host, co->domain)) ) {
|
2006-05-25 02:46:38 +04:00
|
|
|
|
/* the right part of the host matches the domain stuff in the
|
|
|
|
|
cookie data */
|
|
|
|
|
|
|
|
|
|
/* now check the left part of the path with the cookies path
|
|
|
|
|
requirement */
|
2013-06-12 13:19:56 +04:00
|
|
|
|
if(!co->spath || pathmatch(co->spath, path) ) {
|
2006-05-25 02:46:38 +04:00
|
|
|
|
|
|
|
|
|
/* and now, we know this is a match and we should create an
|
|
|
|
|
entry for the return-linked-list */
|
|
|
|
|
|
2008-09-06 09:29:05 +04:00
|
|
|
|
newco = malloc(sizeof(struct Cookie));
|
2006-05-25 02:46:38 +04:00
|
|
|
|
if(newco) {
|
|
|
|
|
/* first, copy the whole source cookie: */
|
|
|
|
|
memcpy(newco, co, sizeof(struct Cookie));
|
|
|
|
|
|
|
|
|
|
/* then modify our next */
|
|
|
|
|
newco->next = mainco;
|
|
|
|
|
|
|
|
|
|
/* point the main to us */
|
|
|
|
|
mainco = newco;
|
2010-01-20 02:19:59 +03:00
|
|
|
|
|
|
|
|
|
matches++;
|
2006-05-25 02:46:38 +04:00
|
|
|
|
}
|
|
|
|
|
else {
|
2010-01-20 02:19:59 +03:00
|
|
|
|
fail:
|
2006-05-25 02:46:38 +04:00
|
|
|
|
/* failure, clear up the allocated chain and return NULL */
|
|
|
|
|
while(mainco) {
|
|
|
|
|
co = mainco->next;
|
|
|
|
|
free(mainco);
|
|
|
|
|
mainco = co;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
co = co->next;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-20 02:19:59 +03:00
|
|
|
|
if(matches) {
|
|
|
|
|
/* 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. */
|
|
|
|
|
struct Cookie **array;
|
2010-01-27 06:43:34 +03:00
|
|
|
|
size_t i;
|
2010-01-20 02:19:59 +03:00
|
|
|
|
|
|
|
|
|
/* alloc an array and store all cookie pointers */
|
2011-06-02 21:42:24 +04:00
|
|
|
|
array = malloc(sizeof(struct Cookie *) * matches);
|
2010-01-20 02:19:59 +03:00
|
|
|
|
if(!array)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
co = mainco;
|
|
|
|
|
|
|
|
|
|
for(i=0; co; co = co->next)
|
|
|
|
|
array[i++] = co;
|
|
|
|
|
|
2012-07-09 17:25:34 +04:00
|
|
|
|
/* now sort the cookie pointers in path length order */
|
2010-01-20 02:19:59 +03:00
|
|
|
|
qsort(array, matches, sizeof(struct Cookie *), cookie_sort);
|
|
|
|
|
|
|
|
|
|
/* remake the linked list order according to the new order */
|
|
|
|
|
|
|
|
|
|
mainco = array[0]; /* start here */
|
|
|
|
|
for(i=0; i<matches-1; i++)
|
|
|
|
|
array[i]->next = array[i+1];
|
|
|
|
|
array[matches-1]->next = NULL; /* terminate the list */
|
|
|
|
|
|
|
|
|
|
free(array); /* remove the temporary data again */
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-25 02:46:38 +04:00
|
|
|
|
return mainco; /* return the new list */
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-25 02:46:38 +04:00
|
|
|
|
/*****************************************************************************
|
|
|
|
|
*
|
|
|
|
|
* Curl_cookie_clearall()
|
|
|
|
|
*
|
|
|
|
|
* Clear all existing cookies and reset the counter.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
void Curl_cookie_clearall(struct CookieInfo *cookies)
|
|
|
|
|
{
|
2007-01-14 02:32:14 +03:00
|
|
|
|
if(cookies) {
|
2008-01-24 01:22:12 +03:00
|
|
|
|
Curl_cookie_freelist(cookies->cookies, TRUE);
|
2007-01-14 02:32:14 +03:00
|
|
|
|
cookies->cookies = NULL;
|
|
|
|
|
cookies->numcookies = 0;
|
|
|
|
|
}
|
2006-05-25 02:46:38 +04:00
|
|
|
|
}
|
1999-12-29 17:20:26 +03:00
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
|
*
|
2001-08-23 18:05:25 +04:00
|
|
|
|
* Curl_cookie_freelist()
|
1999-12-29 17:20:26 +03:00
|
|
|
|
*
|
2001-08-23 18:05:25 +04:00
|
|
|
|
* Free a list of cookies previously returned by Curl_cookie_getlist();
|
1999-12-29 17:20:26 +03:00
|
|
|
|
*
|
2008-01-24 01:22:12 +03:00
|
|
|
|
* The 'cookiestoo' argument tells this function whether to just free the
|
|
|
|
|
* list or actually also free all cookies within the list as well.
|
|
|
|
|
*
|
1999-12-29 17:20:26 +03:00
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2008-01-24 01:22:12 +03:00
|
|
|
|
void Curl_cookie_freelist(struct Cookie *co, bool cookiestoo)
|
1999-12-29 17:20:26 +03:00
|
|
|
|
{
|
2006-05-25 02:46:38 +04:00
|
|
|
|
struct Cookie *next;
|
|
|
|
|
if(co) {
|
|
|
|
|
while(co) {
|
|
|
|
|
next = co->next;
|
2008-01-24 01:22:12 +03:00
|
|
|
|
if(cookiestoo)
|
|
|
|
|
freecookie(co);
|
|
|
|
|
else
|
|
|
|
|
free(co); /* we only free the struct since the "members" are all just
|
|
|
|
|
pointed out in the main cookie list! */
|
2006-05-25 02:46:38 +04:00
|
|
|
|
co = next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
|
*
|
|
|
|
|
* Curl_cookie_clearsess()
|
|
|
|
|
*
|
|
|
|
|
* Free all session cookies in the cookies list.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
void Curl_cookie_clearsess(struct CookieInfo *cookies)
|
|
|
|
|
{
|
|
|
|
|
struct Cookie *first, *curr, *next, *prev = NULL;
|
|
|
|
|
|
2009-12-19 22:20:26 +03:00
|
|
|
|
if(!cookies || !cookies->cookies)
|
2006-05-25 02:46:38 +04:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
first = curr = prev = cookies->cookies;
|
|
|
|
|
|
|
|
|
|
for(; curr; curr = next) {
|
|
|
|
|
next = curr->next;
|
|
|
|
|
if(!curr->expires) {
|
|
|
|
|
if(first == curr)
|
|
|
|
|
first = next;
|
|
|
|
|
|
|
|
|
|
if(prev == curr)
|
|
|
|
|
prev = next;
|
|
|
|
|
else
|
|
|
|
|
prev->next = next;
|
|
|
|
|
|
2008-01-24 01:22:12 +03:00
|
|
|
|
freecookie(curr);
|
2006-05-25 02:46:38 +04:00
|
|
|
|
cookies->numcookies--;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
prev = curr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cookies->cookies = first;
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-25 02:46:38 +04:00
|
|
|
|
|
1999-12-29 17:20:26 +03:00
|
|
|
|
/*****************************************************************************
|
|
|
|
|
*
|
2001-08-23 18:05:25 +04:00
|
|
|
|
* Curl_cookie_cleanup()
|
1999-12-29 17:20:26 +03:00
|
|
|
|
*
|
|
|
|
|
* Free a "cookie object" previous created with cookie_init().
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
2001-01-05 13:11:41 +03:00
|
|
|
|
void Curl_cookie_cleanup(struct CookieInfo *c)
|
1999-12-29 17:20:26 +03:00
|
|
|
|
{
|
2006-05-25 02:46:38 +04:00
|
|
|
|
struct Cookie *co;
|
|
|
|
|
struct Cookie *next;
|
|
|
|
|
if(c) {
|
|
|
|
|
if(c->filename)
|
|
|
|
|
free(c->filename);
|
|
|
|
|
co = c->cookies;
|
|
|
|
|
|
|
|
|
|
while(co) {
|
|
|
|
|
next = co->next;
|
|
|
|
|
freecookie(co);
|
|
|
|
|
co = next;
|
|
|
|
|
}
|
|
|
|
|
free(c); /* free the base struct as well */
|
|
|
|
|
}
|
1999-12-29 17:20:26 +03:00
|
|
|
|
}
|
|
|
|
|
|
2005-07-28 02:17:14 +04:00
|
|
|
|
/* get_netscape_format()
|
|
|
|
|
*
|
|
|
|
|
* Formats a string for Netscape output file, w/o a newline at the end.
|
|
|
|
|
*
|
|
|
|
|
* Function returns a char * to a formatted line. Has to be free()d
|
|
|
|
|
*/
|
|
|
|
|
static char *get_netscape_format(const struct Cookie *co)
|
|
|
|
|
{
|
2006-05-25 02:46:38 +04:00
|
|
|
|
return aprintf(
|
2008-01-31 15:21:57 +03:00
|
|
|
|
"%s" /* httponly preamble */
|
2006-05-25 02:46:38 +04:00
|
|
|
|
"%s%s\t" /* domain */
|
|
|
|
|
"%s\t" /* tailmatch */
|
|
|
|
|
"%s\t" /* path */
|
|
|
|
|
"%s\t" /* secure */
|
2013-12-31 15:10:25 +04:00
|
|
|
|
"%" CURL_FORMAT_CURL_OFF_T "\t" /* expires */
|
2006-05-25 02:46:38 +04:00
|
|
|
|
"%s\t" /* name */
|
|
|
|
|
"%s", /* value */
|
2008-01-31 15:21:57 +03:00
|
|
|
|
co->httponly?"#HttpOnly_":"",
|
2006-05-25 02:46:38 +04:00
|
|
|
|
/* Make sure all domains are prefixed with a dot if they allow
|
|
|
|
|
tailmatching. This is Mozilla-style. */
|
|
|
|
|
(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-28 02:17:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
2001-08-23 18:05:25 +04:00
|
|
|
|
/*
|
2011-06-03 22:02:07 +04:00
|
|
|
|
* cookie_output()
|
2001-08-23 18:05:25 +04:00
|
|
|
|
*
|
2001-08-29 13:32:18 +04:00
|
|
|
|
* Writes all internally known cookies to the specified file. Specify
|
|
|
|
|
* "-" as file name to write to stdout.
|
2001-08-23 18:05:25 +04:00
|
|
|
|
*
|
2001-08-29 13:32:18 +04:00
|
|
|
|
* The function returns non-zero on write failure.
|
2001-08-23 18:05:25 +04:00
|
|
|
|
*/
|
2011-06-03 22:02:07 +04:00
|
|
|
|
static int cookie_output(struct CookieInfo *c, const char *dumphere)
|
2001-08-23 18:05:25 +04:00
|
|
|
|
{
|
|
|
|
|
struct Cookie *co;
|
2001-08-29 13:32:18 +04:00
|
|
|
|
FILE *out;
|
|
|
|
|
bool use_stdout=FALSE;
|
|
|
|
|
|
2001-09-10 11:43:08 +04:00
|
|
|
|
if((NULL == c) || (0 == c->numcookies))
|
2001-08-29 13:32:18 +04:00
|
|
|
|
/* If there are no known cookies, we don't write or even create any
|
|
|
|
|
destination file */
|
|
|
|
|
return 0;
|
|
|
|
|
|
2013-09-17 10:51:22 +04:00
|
|
|
|
/* at first, remove expired cookies */
|
|
|
|
|
remove_expired(c);
|
|
|
|
|
|
2001-08-29 13:32:18 +04:00
|
|
|
|
if(strequal("-", dumphere)) {
|
|
|
|
|
/* use stdout */
|
|
|
|
|
out = stdout;
|
|
|
|
|
use_stdout=TRUE;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
out = fopen(dumphere, "w");
|
|
|
|
|
if(!out)
|
|
|
|
|
return 1; /* failure */
|
|
|
|
|
}
|
|
|
|
|
|
2001-08-23 18:05:25 +04:00
|
|
|
|
if(c) {
|
2005-07-28 02:17:14 +04:00
|
|
|
|
char *format_ptr;
|
|
|
|
|
|
2001-08-29 13:32:18 +04:00
|
|
|
|
fputs("# Netscape HTTP Cookie File\n"
|
2012-07-03 13:27:45 +04:00
|
|
|
|
"# http://curl.haxx.se/docs/http-cookies.html\n"
|
2001-10-08 10:43:22 +04:00
|
|
|
|
"# This file was generated by libcurl! Edit at your own risk.\n\n",
|
2001-08-29 13:32:18 +04:00
|
|
|
|
out);
|
2001-08-23 18:05:25 +04:00
|
|
|
|
co = c->cookies;
|
2004-05-12 16:04:38 +04:00
|
|
|
|
|
2001-08-23 18:05:25 +04:00
|
|
|
|
while(co) {
|
2005-07-28 02:17:14 +04:00
|
|
|
|
format_ptr = get_netscape_format(co);
|
2007-11-07 12:21:35 +03:00
|
|
|
|
if(format_ptr == NULL) {
|
2005-07-28 02:17:14 +04:00
|
|
|
|
fprintf(out, "#\n# Fatal libcurl error\n");
|
2008-12-03 18:08:09 +03:00
|
|
|
|
if(!use_stdout)
|
|
|
|
|
fclose(out);
|
2005-07-28 02:17:14 +04:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
fprintf(out, "%s\n", format_ptr);
|
|
|
|
|
free(format_ptr);
|
2001-08-23 18:05:25 +04:00
|
|
|
|
co=co->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-08-29 13:32:18 +04:00
|
|
|
|
|
|
|
|
|
if(!use_stdout)
|
|
|
|
|
fclose(out);
|
|
|
|
|
|
|
|
|
|
return 0;
|
2001-08-23 18:05:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
2005-07-28 02:17:14 +04:00
|
|
|
|
struct curl_slist *Curl_cookie_list(struct SessionHandle *data)
|
|
|
|
|
{
|
2005-07-29 01:49:58 +04:00
|
|
|
|
struct curl_slist *list = NULL;
|
|
|
|
|
struct curl_slist *beg;
|
|
|
|
|
struct Cookie *c;
|
|
|
|
|
char *line;
|
2005-07-28 02:17:14 +04:00
|
|
|
|
|
2007-11-07 12:21:35 +03:00
|
|
|
|
if((data->cookies == NULL) ||
|
2005-07-29 01:49:58 +04:00
|
|
|
|
(data->cookies->numcookies == 0))
|
|
|
|
|
return NULL;
|
2005-07-28 02:17:14 +04:00
|
|
|
|
|
2005-07-29 01:49:58 +04:00
|
|
|
|
c = data->cookies->cookies;
|
2005-07-28 02:17:14 +04:00
|
|
|
|
|
2007-11-07 12:21:35 +03:00
|
|
|
|
while(c) {
|
2005-07-29 01:49:58 +04:00
|
|
|
|
/* fill the list with _all_ the cookies we know */
|
|
|
|
|
line = get_netscape_format(c);
|
2011-10-07 22:50:57 +04:00
|
|
|
|
if(!line) {
|
|
|
|
|
curl_slist_free_all(list);
|
2005-07-29 01:49:58 +04:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2013-07-15 18:53:43 +04:00
|
|
|
|
beg = Curl_slist_append_nodup(list, line);
|
2011-10-07 22:50:57 +04:00
|
|
|
|
if(!beg) {
|
2013-07-15 18:53:43 +04:00
|
|
|
|
free(line);
|
2011-10-07 22:50:57 +04:00
|
|
|
|
curl_slist_free_all(list);
|
2007-04-07 08:51:35 +04:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2011-10-07 22:50:57 +04:00
|
|
|
|
list = beg;
|
2005-07-29 01:49:58 +04:00
|
|
|
|
c = c->next;
|
|
|
|
|
}
|
2005-07-28 02:17:14 +04:00
|
|
|
|
|
2005-07-29 01:49:58 +04:00
|
|
|
|
return list;
|
2005-07-28 02:17:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-04 17:46:42 +04:00
|
|
|
|
void Curl_flush_cookies(struct SessionHandle *data, int cleanup)
|
|
|
|
|
{
|
|
|
|
|
if(data->set.str[STRING_COOKIEJAR]) {
|
|
|
|
|
if(data->change.cookielist) {
|
|
|
|
|
/* 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 */
|
2011-06-03 22:02:07 +04:00
|
|
|
|
if(cookie_output(data->cookies, data->set.str[STRING_COOKIEJAR]))
|
2011-04-04 17:46:42 +04:00
|
|
|
|
infof(data, "WARNING: failed to save cookies in %s\n",
|
|
|
|
|
data->set.str[STRING_COOKIEJAR]);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2011-10-07 22:50:57 +04:00
|
|
|
|
if(cleanup && data->change.cookielist) {
|
2011-04-04 17:46:42 +04:00
|
|
|
|
/* since nothing is written, we can just free the list of cookie file
|
|
|
|
|
names */
|
|
|
|
|
curl_slist_free_all(data->change.cookielist); /* clean up list */
|
2011-10-07 22:50:57 +04:00
|
|
|
|
data->change.cookielist = NULL;
|
|
|
|
|
}
|
2011-04-04 17: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);
|
|
|
|
|
}
|
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-06 02:59:32 +03:00
|
|
|
|
#endif /* CURL_DISABLE_HTTP || CURL_DISABLE_COOKIES */
|