cookie.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /***
  23. RECEIVING COOKIE INFORMATION
  24. ============================
  25. struct CookieInfo *Curl_cookie_init(struct Curl_easy *data,
  26. const char *file, struct CookieInfo *inc, bool newsession);
  27. Inits a cookie struct to store data in a local file. This is always
  28. called before any cookies are set.
  29. struct Cookie *Curl_cookie_add(struct Curl_easy *data,
  30. struct CookieInfo *c, bool httpheader, char *lineptr,
  31. const char *domain, const char *path);
  32. The 'lineptr' parameter is a full "Set-cookie:" line as
  33. received from a server.
  34. The function need to replace previously stored lines that this new
  35. line superceeds.
  36. It may remove lines that are expired.
  37. It should return an indication of success/error.
  38. SENDING COOKIE INFORMATION
  39. ==========================
  40. struct Cookies *Curl_cookie_getlist(struct CookieInfo *cookie,
  41. char *host, char *path, bool secure);
  42. For a given host and path, return a linked list of cookies that
  43. the client should send to the server if used now. The secure
  44. boolean informs the cookie if a secure connection is achieved or
  45. not.
  46. It shall only return cookies that haven't expired.
  47. Example set of cookies:
  48. Set-cookie: PRODUCTINFO=webxpress; domain=.fidelity.com; path=/; secure
  49. Set-cookie: PERSONALIZE=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  50. domain=.fidelity.com; path=/ftgw; secure
  51. Set-cookie: FidHist=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  52. domain=.fidelity.com; path=/; secure
  53. Set-cookie: FidOrder=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  54. domain=.fidelity.com; path=/; secure
  55. Set-cookie: DisPend=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  56. domain=.fidelity.com; path=/; secure
  57. Set-cookie: FidDis=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  58. domain=.fidelity.com; path=/; secure
  59. Set-cookie:
  60. Session_Key@6791a9e0-901a-11d0-a1c8-9b012c88aa77=none;expires=Monday,
  61. 13-Jun-1988 03:04:55 GMT; domain=.fidelity.com; path=/; secure
  62. ****/
  63. #include "curl_setup.h"
  64. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  65. #ifdef USE_LIBPSL
  66. # include <libpsl.h>
  67. #endif
  68. #include "urldata.h"
  69. #include "cookie.h"
  70. #include "strtok.h"
  71. #include "sendf.h"
  72. #include "slist.h"
  73. #include "share.h"
  74. #include "strtoofft.h"
  75. #include "strcase.h"
  76. #include "curl_memrchr.h"
  77. #include "inet_pton.h"
  78. /* The last 3 #include files should be in this order */
  79. #include "curl_printf.h"
  80. #include "curl_memory.h"
  81. #include "memdebug.h"
  82. static void freecookie(struct Cookie *co)
  83. {
  84. free(co->expirestr);
  85. free(co->domain);
  86. free(co->path);
  87. free(co->spath);
  88. free(co->name);
  89. free(co->value);
  90. free(co->maxage);
  91. free(co->version);
  92. free(co);
  93. }
  94. static bool tailmatch(const char *cooke_domain, const char *hostname)
  95. {
  96. size_t cookie_domain_len = strlen(cooke_domain);
  97. size_t hostname_len = strlen(hostname);
  98. if(hostname_len < cookie_domain_len)
  99. return FALSE;
  100. if(!strcasecompare(cooke_domain, hostname+hostname_len-cookie_domain_len))
  101. return FALSE;
  102. /* A lead char of cookie_domain is not '.'.
  103. RFC6265 4.1.2.3. The Domain Attribute says:
  104. For example, if the value of the Domain attribute is
  105. "example.com", the user agent will include the cookie in the Cookie
  106. header when making HTTP requests to example.com, www.example.com, and
  107. www.corp.example.com.
  108. */
  109. if(hostname_len == cookie_domain_len)
  110. return TRUE;
  111. if('.' == *(hostname + hostname_len - cookie_domain_len - 1))
  112. return TRUE;
  113. return FALSE;
  114. }
  115. /*
  116. * matching cookie path and url path
  117. * RFC6265 5.1.4 Paths and Path-Match
  118. */
  119. static bool pathmatch(const char *cookie_path, const char *request_uri)
  120. {
  121. size_t cookie_path_len;
  122. size_t uri_path_len;
  123. char *uri_path = NULL;
  124. char *pos;
  125. bool ret = FALSE;
  126. /* cookie_path must not have last '/' separator. ex: /sample */
  127. cookie_path_len = strlen(cookie_path);
  128. if(1 == cookie_path_len) {
  129. /* cookie_path must be '/' */
  130. return TRUE;
  131. }
  132. uri_path = strdup(request_uri);
  133. if(!uri_path)
  134. return FALSE;
  135. pos = strchr(uri_path, '?');
  136. if(pos)
  137. *pos = 0x0;
  138. /* #-fragments are already cut off! */
  139. if(0 == strlen(uri_path) || uri_path[0] != '/') {
  140. free(uri_path);
  141. uri_path = strdup("/");
  142. if(!uri_path)
  143. return FALSE;
  144. }
  145. /* here, RFC6265 5.1.4 says
  146. 4. Output the characters of the uri-path from the first character up
  147. to, but not including, the right-most %x2F ("/").
  148. but URL path /hoge?fuga=xxx means /hoge/index.cgi?fuga=xxx in some site
  149. without redirect.
  150. Ignore this algorithm because /hoge is uri path for this case
  151. (uri path is not /).
  152. */
  153. uri_path_len = strlen(uri_path);
  154. if(uri_path_len < cookie_path_len) {
  155. ret = FALSE;
  156. goto pathmatched;
  157. }
  158. /* not using checkprefix() because matching should be case-sensitive */
  159. if(strncmp(cookie_path, uri_path, cookie_path_len)) {
  160. ret = FALSE;
  161. goto pathmatched;
  162. }
  163. /* The cookie-path and the uri-path are identical. */
  164. if(cookie_path_len == uri_path_len) {
  165. ret = TRUE;
  166. goto pathmatched;
  167. }
  168. /* here, cookie_path_len < url_path_len */
  169. if(uri_path[cookie_path_len] == '/') {
  170. ret = TRUE;
  171. goto pathmatched;
  172. }
  173. ret = FALSE;
  174. pathmatched:
  175. free(uri_path);
  176. return ret;
  177. }
  178. /*
  179. * cookie path sanitize
  180. */
  181. static char *sanitize_cookie_path(const char *cookie_path)
  182. {
  183. size_t len;
  184. char *new_path = strdup(cookie_path);
  185. if(!new_path)
  186. return NULL;
  187. /* some stupid site sends path attribute with '"'. */
  188. len = strlen(new_path);
  189. if(new_path[0] == '\"') {
  190. memmove((void *)new_path, (const void *)(new_path + 1), len);
  191. len--;
  192. }
  193. if(len && (new_path[len - 1] == '\"')) {
  194. new_path[len - 1] = 0x0;
  195. len--;
  196. }
  197. /* RFC6265 5.2.4 The Path Attribute */
  198. if(new_path[0] != '/') {
  199. /* Let cookie-path be the default-path. */
  200. free(new_path);
  201. new_path = strdup("/");
  202. return new_path;
  203. }
  204. /* convert /hoge/ to /hoge */
  205. if(len && new_path[len - 1] == '/') {
  206. new_path[len - 1] = 0x0;
  207. }
  208. return new_path;
  209. }
  210. /*
  211. * Load cookies from all given cookie files (CURLOPT_COOKIEFILE).
  212. *
  213. * NOTE: OOM or cookie parsing failures are ignored.
  214. */
  215. void Curl_cookie_loadfiles(struct Curl_easy *data)
  216. {
  217. struct curl_slist *list = data->change.cookielist;
  218. if(list) {
  219. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
  220. while(list) {
  221. struct CookieInfo *newcookies = Curl_cookie_init(data,
  222. list->data,
  223. data->cookies,
  224. data->set.cookiesession);
  225. if(!newcookies)
  226. /* Failure may be due to OOM or a bad cookie; both are ignored
  227. * but only the first should be
  228. */
  229. infof(data, "ignoring failed cookie_init for %s\n", list->data);
  230. else
  231. data->cookies = newcookies;
  232. list = list->next;
  233. }
  234. curl_slist_free_all(data->change.cookielist); /* clean up list */
  235. data->change.cookielist = NULL; /* don't do this again! */
  236. Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
  237. }
  238. }
  239. /*
  240. * strstore() makes a strdup() on the 'newstr' and if '*str' is non-NULL
  241. * that will be freed before the allocated string is stored there.
  242. *
  243. * It is meant to easily replace strdup()
  244. */
  245. static void strstore(char **str, const char *newstr)
  246. {
  247. free(*str);
  248. *str = strdup(newstr);
  249. }
  250. /*
  251. * remove_expired() removes expired cookies.
  252. */
  253. static void remove_expired(struct CookieInfo *cookies)
  254. {
  255. struct Cookie *co, *nx, *pv;
  256. curl_off_t now = (curl_off_t)time(NULL);
  257. co = cookies->cookies;
  258. pv = NULL;
  259. while(co) {
  260. nx = co->next;
  261. if(co->expires && co->expires < now) {
  262. if(co == cookies->cookies) {
  263. cookies->cookies = co->next;
  264. }
  265. else {
  266. pv->next = co->next;
  267. }
  268. cookies->numcookies--;
  269. freecookie(co);
  270. }
  271. else {
  272. pv = co;
  273. }
  274. co = nx;
  275. }
  276. }
  277. /*
  278. * Return true if the given string is an IP(v4|v6) address.
  279. */
  280. static bool isip(const char *domain)
  281. {
  282. struct in_addr addr;
  283. #ifdef ENABLE_IPV6
  284. struct in6_addr addr6;
  285. #endif
  286. if(Curl_inet_pton(AF_INET, domain, &addr)
  287. #ifdef ENABLE_IPV6
  288. || Curl_inet_pton(AF_INET6, domain, &addr6)
  289. #endif
  290. ) {
  291. /* domain name given as IP address */
  292. return TRUE;
  293. }
  294. return FALSE;
  295. }
  296. /****************************************************************************
  297. *
  298. * Curl_cookie_add()
  299. *
  300. * Add a single cookie line to the cookie keeping object.
  301. *
  302. * Be aware that sometimes we get an IP-only host name, and that might also be
  303. * a numerical IPv6 address.
  304. *
  305. * Returns NULL on out of memory or invalid cookie. This is suboptimal,
  306. * as they should be treated separately.
  307. ***************************************************************************/
  308. struct Cookie *
  309. Curl_cookie_add(struct Curl_easy *data,
  310. /* The 'data' pointer here may be NULL at times, and thus
  311. must only be used very carefully for things that can deal
  312. with data being NULL. Such as infof() and similar */
  313. struct CookieInfo *c,
  314. bool httpheader, /* TRUE if HTTP header-style line */
  315. char *lineptr, /* first character of the line */
  316. const char *domain, /* default domain */
  317. const char *path) /* full path used when this cookie is set,
  318. used to get default path for the cookie
  319. unless set */
  320. {
  321. struct Cookie *clist;
  322. char name[MAX_NAME];
  323. struct Cookie *co;
  324. struct Cookie *lastc=NULL;
  325. time_t now = time(NULL);
  326. bool replace_old = FALSE;
  327. bool badcookie = FALSE; /* cookies are good by default. mmmmm yummy */
  328. #ifdef USE_LIBPSL
  329. const psl_ctx_t *psl;
  330. #endif
  331. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  332. (void)data;
  333. #endif
  334. /* First, alloc and init a new struct for it */
  335. co = calloc(1, sizeof(struct Cookie));
  336. if(!co)
  337. return NULL; /* bail out if we're this low on memory */
  338. if(httpheader) {
  339. /* This line was read off a HTTP-header */
  340. const char *ptr;
  341. const char *semiptr;
  342. char *what;
  343. what = malloc(MAX_COOKIE_LINE);
  344. if(!what) {
  345. free(co);
  346. return NULL;
  347. }
  348. semiptr=strchr(lineptr, ';'); /* first, find a semicolon */
  349. while(*lineptr && ISBLANK(*lineptr))
  350. lineptr++;
  351. ptr = lineptr;
  352. do {
  353. /* we have a <what>=<this> pair or a stand-alone word here */
  354. name[0]=what[0]=0; /* init the buffers */
  355. if(1 <= sscanf(ptr, "%" MAX_NAME_TXT "[^;\r\n=] =%"
  356. MAX_COOKIE_LINE_TXT "[^;\r\n]",
  357. name, what)) {
  358. /* Use strstore() below to properly deal with received cookie
  359. headers that have the same string property set more than once,
  360. and then we use the last one. */
  361. const char *whatptr;
  362. bool done = FALSE;
  363. bool sep;
  364. size_t len=strlen(what);
  365. size_t nlen = strlen(name);
  366. const char *endofn = &ptr[ nlen ];
  367. /* name ends with a '=' ? */
  368. sep = (*endofn == '=')?TRUE:FALSE;
  369. if(nlen) {
  370. endofn--; /* move to the last character */
  371. if(ISBLANK(*endofn)) {
  372. /* skip trailing spaces in name */
  373. while(*endofn && ISBLANK(*endofn) && nlen) {
  374. endofn--;
  375. nlen--;
  376. }
  377. name[nlen]=0; /* new end of name */
  378. }
  379. }
  380. /* Strip off trailing whitespace from the 'what' */
  381. while(len && ISBLANK(what[len-1])) {
  382. what[len-1]=0;
  383. len--;
  384. }
  385. /* Skip leading whitespace from the 'what' */
  386. whatptr=what;
  387. while(*whatptr && ISBLANK(*whatptr))
  388. whatptr++;
  389. if(!co->name && sep) {
  390. /* The very first name/value pair is the actual cookie name */
  391. co->name = strdup(name);
  392. co->value = strdup(whatptr);
  393. if(!co->name || !co->value) {
  394. badcookie = TRUE;
  395. break;
  396. }
  397. }
  398. else if(!len) {
  399. /* this was a "<name>=" with no content, and we must allow
  400. 'secure' and 'httponly' specified this weirdly */
  401. done = TRUE;
  402. if(strcasecompare("secure", name))
  403. co->secure = TRUE;
  404. else if(strcasecompare("httponly", name))
  405. co->httponly = TRUE;
  406. else if(sep)
  407. /* there was a '=' so we're not done parsing this field */
  408. done = FALSE;
  409. }
  410. if(done)
  411. ;
  412. else if(strcasecompare("path", name)) {
  413. strstore(&co->path, whatptr);
  414. if(!co->path) {
  415. badcookie = TRUE; /* out of memory bad */
  416. break;
  417. }
  418. co->spath = sanitize_cookie_path(co->path);
  419. if(!co->spath) {
  420. badcookie = TRUE; /* out of memory bad */
  421. break;
  422. }
  423. }
  424. else if(strcasecompare("domain", name)) {
  425. bool is_ip;
  426. /* Now, we make sure that our host is within the given domain,
  427. or the given domain is not valid and thus cannot be set. */
  428. if('.' == whatptr[0])
  429. whatptr++; /* ignore preceding dot */
  430. #ifndef USE_LIBPSL
  431. /*
  432. * Without PSL we don't know when the incoming cookie is set on a
  433. * TLD or otherwise "protected" suffix. To reduce risk, we require a
  434. * dot OR the exact host name being "localhost".
  435. */
  436. {
  437. const char *dotp;
  438. /* check for more dots */
  439. dotp = strchr(whatptr, '.');
  440. if(!dotp && !strcasecompare("localhost", whatptr))
  441. domain=":";
  442. }
  443. #endif
  444. is_ip = isip(domain ? domain : whatptr);
  445. if(!domain
  446. || (is_ip && !strcmp(whatptr, domain))
  447. || (!is_ip && tailmatch(whatptr, domain))) {
  448. strstore(&co->domain, whatptr);
  449. if(!co->domain) {
  450. badcookie = TRUE;
  451. break;
  452. }
  453. if(!is_ip)
  454. co->tailmatch=TRUE; /* we always do that if the domain name was
  455. given */
  456. }
  457. else {
  458. /* we did not get a tailmatch and then the attempted set domain
  459. is not a domain to which the current host belongs. Mark as
  460. bad. */
  461. badcookie=TRUE;
  462. infof(data, "skipped cookie with bad tailmatch domain: %s\n",
  463. whatptr);
  464. }
  465. }
  466. else if(strcasecompare("version", name)) {
  467. strstore(&co->version, whatptr);
  468. if(!co->version) {
  469. badcookie = TRUE;
  470. break;
  471. }
  472. }
  473. else if(strcasecompare("max-age", name)) {
  474. /* Defined in RFC2109:
  475. Optional. The Max-Age attribute defines the lifetime of the
  476. cookie, in seconds. The delta-seconds value is a decimal non-
  477. negative integer. After delta-seconds seconds elapse, the
  478. client should discard the cookie. A value of zero means the
  479. cookie should be discarded immediately.
  480. */
  481. strstore(&co->maxage, whatptr);
  482. if(!co->maxage) {
  483. badcookie = TRUE;
  484. break;
  485. }
  486. }
  487. else if(strcasecompare("expires", name)) {
  488. strstore(&co->expirestr, whatptr);
  489. if(!co->expirestr) {
  490. badcookie = TRUE;
  491. break;
  492. }
  493. }
  494. /*
  495. else this is the second (or more) name we don't know
  496. about! */
  497. }
  498. else {
  499. /* this is an "illegal" <what>=<this> pair */
  500. }
  501. if(!semiptr || !*semiptr) {
  502. /* we already know there are no more cookies */
  503. semiptr = NULL;
  504. continue;
  505. }
  506. ptr=semiptr+1;
  507. while(*ptr && ISBLANK(*ptr))
  508. ptr++;
  509. semiptr=strchr(ptr, ';'); /* now, find the next semicolon */
  510. if(!semiptr && *ptr)
  511. /* There are no more semicolons, but there's a final name=value pair
  512. coming up */
  513. semiptr=strchr(ptr, '\0');
  514. } while(semiptr);
  515. if(co->maxage) {
  516. co->expires =
  517. curlx_strtoofft((*co->maxage=='\"')?
  518. &co->maxage[1]:&co->maxage[0], NULL, 10);
  519. if(CURL_OFF_T_MAX - now < co->expires)
  520. /* avoid overflow */
  521. co->expires = CURL_OFF_T_MAX;
  522. else
  523. co->expires += now;
  524. }
  525. else if(co->expirestr) {
  526. /* Note that if the date couldn't get parsed for whatever reason,
  527. the cookie will be treated as a session cookie */
  528. co->expires = curl_getdate(co->expirestr, NULL);
  529. /* Session cookies have expires set to 0 so if we get that back
  530. from the date parser let's add a second to make it a
  531. non-session cookie */
  532. if(co->expires == 0)
  533. co->expires = 1;
  534. else if(co->expires < 0)
  535. co->expires = 0;
  536. }
  537. if(!badcookie && !co->domain) {
  538. if(domain) {
  539. /* no domain was given in the header line, set the default */
  540. co->domain=strdup(domain);
  541. if(!co->domain)
  542. badcookie = TRUE;
  543. }
  544. }
  545. if(!badcookie && !co->path && path) {
  546. /* No path was given in the header line, set the default.
  547. Note that the passed-in path to this function MAY have a '?' and
  548. following part that MUST not be stored as part of the path. */
  549. char *queryp = strchr(path, '?');
  550. /* queryp is where the interesting part of the path ends, so now we
  551. want to the find the last */
  552. char *endslash;
  553. if(!queryp)
  554. endslash = strrchr(path, '/');
  555. else
  556. endslash = memrchr(path, '/', (size_t)(queryp - path));
  557. if(endslash) {
  558. size_t pathlen = (size_t)(endslash-path+1); /* include ending slash */
  559. co->path=malloc(pathlen+1); /* one extra for the zero byte */
  560. if(co->path) {
  561. memcpy(co->path, path, pathlen);
  562. co->path[pathlen]=0; /* zero terminate */
  563. co->spath = sanitize_cookie_path(co->path);
  564. if(!co->spath)
  565. badcookie = TRUE; /* out of memory bad */
  566. }
  567. else
  568. badcookie = TRUE;
  569. }
  570. }
  571. free(what);
  572. if(badcookie || !co->name) {
  573. /* we didn't get a cookie name or a bad one,
  574. this is an illegal line, bail out */
  575. freecookie(co);
  576. return NULL;
  577. }
  578. }
  579. else {
  580. /* This line is NOT a HTTP header style line, we do offer support for
  581. reading the odd netscape cookies-file format here */
  582. char *ptr;
  583. char *firstptr;
  584. char *tok_buf=NULL;
  585. int fields;
  586. /* IE introduced HTTP-only cookies to prevent XSS attacks. Cookies
  587. marked with httpOnly after the domain name are not accessible
  588. from javascripts, but since curl does not operate at javascript
  589. level, we include them anyway. In Firefox's cookie files, these
  590. lines are preceded with #HttpOnly_ and then everything is
  591. as usual, so we skip 10 characters of the line..
  592. */
  593. if(strncmp(lineptr, "#HttpOnly_", 10) == 0) {
  594. lineptr += 10;
  595. co->httponly = TRUE;
  596. }
  597. if(lineptr[0]=='#') {
  598. /* don't even try the comments */
  599. free(co);
  600. return NULL;
  601. }
  602. /* strip off the possible end-of-line characters */
  603. ptr=strchr(lineptr, '\r');
  604. if(ptr)
  605. *ptr=0; /* clear it */
  606. ptr=strchr(lineptr, '\n');
  607. if(ptr)
  608. *ptr=0; /* clear it */
  609. firstptr=strtok_r(lineptr, "\t", &tok_buf); /* tokenize it on the TAB */
  610. /* Now loop through the fields and init the struct we already have
  611. allocated */
  612. for(ptr=firstptr, fields=0; ptr && !badcookie;
  613. ptr=strtok_r(NULL, "\t", &tok_buf), fields++) {
  614. switch(fields) {
  615. case 0:
  616. if(ptr[0]=='.') /* skip preceding dots */
  617. ptr++;
  618. co->domain = strdup(ptr);
  619. if(!co->domain)
  620. badcookie = TRUE;
  621. break;
  622. case 1:
  623. /* This field got its explanation on the 23rd of May 2001 by
  624. Andrés García:
  625. flag: A TRUE/FALSE value indicating if all machines within a given
  626. domain can access the variable. This value is set automatically by
  627. the browser, depending on the value you set for the domain.
  628. As far as I can see, it is set to true when the cookie says
  629. .domain.com and to false when the domain is complete www.domain.com
  630. */
  631. co->tailmatch = strcasecompare(ptr, "TRUE")?TRUE:FALSE;
  632. break;
  633. case 2:
  634. /* It turns out, that sometimes the file format allows the path
  635. field to remain not filled in, we try to detect this and work
  636. around it! Andrés García made us aware of this... */
  637. if(strcmp("TRUE", ptr) && strcmp("FALSE", ptr)) {
  638. /* only if the path doesn't look like a boolean option! */
  639. co->path = strdup(ptr);
  640. if(!co->path)
  641. badcookie = TRUE;
  642. else {
  643. co->spath = sanitize_cookie_path(co->path);
  644. if(!co->spath) {
  645. badcookie = TRUE; /* out of memory bad */
  646. }
  647. }
  648. break;
  649. }
  650. /* this doesn't look like a path, make one up! */
  651. co->path = strdup("/");
  652. if(!co->path)
  653. badcookie = TRUE;
  654. co->spath = strdup("/");
  655. if(!co->spath)
  656. badcookie = TRUE;
  657. fields++; /* add a field and fall down to secure */
  658. /* FALLTHROUGH */
  659. case 3:
  660. co->secure = strcasecompare(ptr, "TRUE")?TRUE:FALSE;
  661. break;
  662. case 4:
  663. co->expires = curlx_strtoofft(ptr, NULL, 10);
  664. break;
  665. case 5:
  666. co->name = strdup(ptr);
  667. if(!co->name)
  668. badcookie = TRUE;
  669. break;
  670. case 6:
  671. co->value = strdup(ptr);
  672. if(!co->value)
  673. badcookie = TRUE;
  674. break;
  675. }
  676. }
  677. if(6 == fields) {
  678. /* we got a cookie with blank contents, fix it */
  679. co->value = strdup("");
  680. if(!co->value)
  681. badcookie = TRUE;
  682. else
  683. fields++;
  684. }
  685. if(!badcookie && (7 != fields))
  686. /* we did not find the sufficient number of fields */
  687. badcookie = TRUE;
  688. if(badcookie) {
  689. freecookie(co);
  690. return NULL;
  691. }
  692. }
  693. if(!c->running && /* read from a file */
  694. c->newsession && /* clean session cookies */
  695. !co->expires) { /* this is a session cookie since it doesn't expire! */
  696. freecookie(co);
  697. return NULL;
  698. }
  699. co->livecookie = c->running;
  700. /* now, we have parsed the incoming line, we must now check if this
  701. superceeds an already existing cookie, which it may if the previous have
  702. the same domain and path as this */
  703. /* at first, remove expired cookies */
  704. remove_expired(c);
  705. #ifdef USE_LIBPSL
  706. /* Check if the domain is a Public Suffix and if yes, ignore the cookie.
  707. This needs a libpsl compiled with builtin data. */
  708. if(domain && co->domain && !isip(co->domain)) {
  709. psl = psl_builtin();
  710. if(psl && !psl_is_cookie_domain_acceptable(psl, domain, co->domain)) {
  711. infof(data,
  712. "cookie '%s' dropped, domain '%s' must not set cookies for '%s'\n",
  713. co->name, domain, co->domain);
  714. freecookie(co);
  715. return NULL;
  716. }
  717. }
  718. #endif
  719. clist = c->cookies;
  720. replace_old = FALSE;
  721. while(clist) {
  722. if(strcasecompare(clist->name, co->name)) {
  723. /* the names are identical */
  724. if(clist->domain && co->domain) {
  725. if(strcasecompare(clist->domain, co->domain) &&
  726. (clist->tailmatch == co->tailmatch))
  727. /* The domains are identical */
  728. replace_old=TRUE;
  729. }
  730. else if(!clist->domain && !co->domain)
  731. replace_old = TRUE;
  732. if(replace_old) {
  733. /* the domains were identical */
  734. if(clist->spath && co->spath) {
  735. if(strcasecompare(clist->spath, co->spath)) {
  736. replace_old = TRUE;
  737. }
  738. else
  739. replace_old = FALSE;
  740. }
  741. else if(!clist->spath && !co->spath)
  742. replace_old = TRUE;
  743. else
  744. replace_old = FALSE;
  745. }
  746. if(replace_old && !co->livecookie && clist->livecookie) {
  747. /* Both cookies matched fine, except that the already present
  748. cookie is "live", which means it was set from a header, while
  749. the new one isn't "live" and thus only read from a file. We let
  750. live cookies stay alive */
  751. /* Free the newcomer and get out of here! */
  752. freecookie(co);
  753. return NULL;
  754. }
  755. if(replace_old) {
  756. co->next = clist->next; /* get the next-pointer first */
  757. /* then free all the old pointers */
  758. free(clist->name);
  759. free(clist->value);
  760. free(clist->domain);
  761. free(clist->path);
  762. free(clist->spath);
  763. free(clist->expirestr);
  764. free(clist->version);
  765. free(clist->maxage);
  766. *clist = *co; /* then store all the new data */
  767. free(co); /* free the newly alloced memory */
  768. co = clist; /* point to the previous struct instead */
  769. /* We have replaced a cookie, now skip the rest of the list but
  770. make sure the 'lastc' pointer is properly set */
  771. do {
  772. lastc = clist;
  773. clist = clist->next;
  774. } while(clist);
  775. break;
  776. }
  777. }
  778. lastc = clist;
  779. clist = clist->next;
  780. }
  781. if(c->running)
  782. /* Only show this when NOT reading the cookies from a file */
  783. infof(data, "%s cookie %s=\"%s\" for domain %s, path %s, "
  784. "expire %" CURL_FORMAT_CURL_OFF_T "\n",
  785. replace_old?"Replaced":"Added", co->name, co->value,
  786. co->domain, co->path, co->expires);
  787. if(!replace_old) {
  788. /* then make the last item point on this new one */
  789. if(lastc)
  790. lastc->next = co;
  791. else
  792. c->cookies = co;
  793. c->numcookies++; /* one more cookie in the jar */
  794. }
  795. return co;
  796. }
  797. /*
  798. * get_line() makes sure to only return complete whole lines that fit in 'len'
  799. * bytes and end with a newline.
  800. */
  801. static char *get_line(char *buf, int len, FILE *input)
  802. {
  803. bool partial = FALSE;
  804. while(1) {
  805. char *b = fgets(buf, len, input);
  806. if(b) {
  807. size_t rlen = strlen(b);
  808. if(rlen && (b[rlen-1] == '\n')) {
  809. if(partial) {
  810. partial = FALSE;
  811. continue;
  812. }
  813. return b;
  814. }
  815. /* read a partial, discard the next piece that ends with newline */
  816. partial = TRUE;
  817. }
  818. else
  819. break;
  820. }
  821. return NULL;
  822. }
  823. /*****************************************************************************
  824. *
  825. * Curl_cookie_init()
  826. *
  827. * Inits a cookie struct to read data from a local file. This is always
  828. * called before any cookies are set. File may be NULL.
  829. *
  830. * If 'newsession' is TRUE, discard all "session cookies" on read from file.
  831. *
  832. * Returns NULL on out of memory. Invalid cookies are ignored.
  833. ****************************************************************************/
  834. struct CookieInfo *Curl_cookie_init(struct Curl_easy *data,
  835. const char *file,
  836. struct CookieInfo *inc,
  837. bool newsession)
  838. {
  839. struct CookieInfo *c;
  840. FILE *fp = NULL;
  841. bool fromfile=TRUE;
  842. char *line = NULL;
  843. if(NULL == inc) {
  844. /* we didn't get a struct, create one */
  845. c = calloc(1, sizeof(struct CookieInfo));
  846. if(!c)
  847. return NULL; /* failed to get memory */
  848. c->filename = strdup(file?file:"none"); /* copy the name just in case */
  849. if(!c->filename)
  850. goto fail; /* failed to get memory */
  851. }
  852. else {
  853. /* we got an already existing one, use that */
  854. c = inc;
  855. }
  856. c->running = FALSE; /* this is not running, this is init */
  857. if(file && !strcmp(file, "-")) {
  858. fp = stdin;
  859. fromfile=FALSE;
  860. }
  861. else if(file && !*file) {
  862. /* points to a "" string */
  863. fp = NULL;
  864. }
  865. else
  866. fp = file?fopen(file, FOPEN_READTEXT):NULL;
  867. c->newsession = newsession; /* new session? */
  868. if(fp) {
  869. char *lineptr;
  870. bool headerline;
  871. line = malloc(MAX_COOKIE_LINE);
  872. if(!line)
  873. goto fail;
  874. while(get_line(line, MAX_COOKIE_LINE, fp)) {
  875. if(checkprefix("Set-Cookie:", line)) {
  876. /* This is a cookie line, get it! */
  877. lineptr=&line[11];
  878. headerline=TRUE;
  879. }
  880. else {
  881. lineptr=line;
  882. headerline=FALSE;
  883. }
  884. while(*lineptr && ISBLANK(*lineptr))
  885. lineptr++;
  886. Curl_cookie_add(data, c, headerline, lineptr, NULL, NULL);
  887. }
  888. free(line); /* free the line buffer */
  889. if(fromfile)
  890. fclose(fp);
  891. }
  892. c->running = TRUE; /* now, we're running */
  893. return c;
  894. fail:
  895. free(line);
  896. if(!inc)
  897. /* Only clean up if we allocated it here, as the original could still be in
  898. * use by a share handle */
  899. Curl_cookie_cleanup(c);
  900. if(fromfile && fp)
  901. fclose(fp);
  902. return NULL; /* out of memory */
  903. }
  904. /* sort this so that the longest path gets before the shorter path */
  905. static int cookie_sort(const void *p1, const void *p2)
  906. {
  907. struct Cookie *c1 = *(struct Cookie **)p1;
  908. struct Cookie *c2 = *(struct Cookie **)p2;
  909. size_t l1, l2;
  910. /* 1 - compare cookie path lengths */
  911. l1 = c1->path ? strlen(c1->path) : 0;
  912. l2 = c2->path ? strlen(c2->path) : 0;
  913. if(l1 != l2)
  914. return (l2 > l1) ? 1 : -1 ; /* avoid size_t <=> int conversions */
  915. /* 2 - compare cookie domain lengths */
  916. l1 = c1->domain ? strlen(c1->domain) : 0;
  917. l2 = c2->domain ? strlen(c2->domain) : 0;
  918. if(l1 != l2)
  919. return (l2 > l1) ? 1 : -1 ; /* avoid size_t <=> int conversions */
  920. /* 3 - compare cookie names */
  921. if(c1->name && c2->name)
  922. return strcmp(c1->name, c2->name);
  923. /* sorry, can't be more deterministic */
  924. return 0;
  925. }
  926. #define CLONE(field) \
  927. do { \
  928. if(src->field) { \
  929. d->field = strdup(src->field); \
  930. if(!d->field) \
  931. goto fail; \
  932. } \
  933. } while(0)
  934. static struct Cookie *dup_cookie(struct Cookie *src)
  935. {
  936. struct Cookie *d = calloc(sizeof(struct Cookie), 1);
  937. if(d) {
  938. CLONE(expirestr);
  939. CLONE(domain);
  940. CLONE(path);
  941. CLONE(spath);
  942. CLONE(name);
  943. CLONE(value);
  944. CLONE(maxage);
  945. CLONE(version);
  946. d->expires = src->expires;
  947. d->tailmatch = src->tailmatch;
  948. d->secure = src->secure;
  949. d->livecookie = src->livecookie;
  950. d->httponly = src->httponly;
  951. }
  952. return d;
  953. fail:
  954. freecookie(d);
  955. return NULL;
  956. }
  957. /*****************************************************************************
  958. *
  959. * Curl_cookie_getlist()
  960. *
  961. * For a given host and path, return a linked list of cookies that the
  962. * client should send to the server if used now. The secure boolean informs
  963. * the cookie if a secure connection is achieved or not.
  964. *
  965. * It shall only return cookies that haven't expired.
  966. *
  967. ****************************************************************************/
  968. struct Cookie *Curl_cookie_getlist(struct CookieInfo *c,
  969. const char *host, const char *path,
  970. bool secure)
  971. {
  972. struct Cookie *newco;
  973. struct Cookie *co;
  974. time_t now = time(NULL);
  975. struct Cookie *mainco=NULL;
  976. size_t matches = 0;
  977. bool is_ip;
  978. if(!c || !c->cookies)
  979. return NULL; /* no cookie struct or no cookies in the struct */
  980. /* at first, remove expired cookies */
  981. remove_expired(c);
  982. /* check if host is an IP(v4|v6) address */
  983. is_ip = isip(host);
  984. co = c->cookies;
  985. while(co) {
  986. /* only process this cookie if it is not expired or had no expire
  987. date AND that if the cookie requires we're secure we must only
  988. continue if we are! */
  989. if((!co->expires || (co->expires > now)) &&
  990. (co->secure?secure:TRUE)) {
  991. /* now check if the domain is correct */
  992. if(!co->domain ||
  993. (co->tailmatch && !is_ip && tailmatch(co->domain, host)) ||
  994. ((!co->tailmatch || is_ip) && strcasecompare(host, co->domain)) ) {
  995. /* the right part of the host matches the domain stuff in the
  996. cookie data */
  997. /* now check the left part of the path with the cookies path
  998. requirement */
  999. if(!co->spath || pathmatch(co->spath, path) ) {
  1000. /* and now, we know this is a match and we should create an
  1001. entry for the return-linked-list */
  1002. newco = dup_cookie(co);
  1003. if(newco) {
  1004. /* then modify our next */
  1005. newco->next = mainco;
  1006. /* point the main to us */
  1007. mainco = newco;
  1008. matches++;
  1009. }
  1010. else {
  1011. fail:
  1012. /* failure, clear up the allocated chain and return NULL */
  1013. Curl_cookie_freelist(mainco);
  1014. return NULL;
  1015. }
  1016. }
  1017. }
  1018. }
  1019. co = co->next;
  1020. }
  1021. if(matches) {
  1022. /* Now we need to make sure that if there is a name appearing more than
  1023. once, the longest specified path version comes first. To make this
  1024. the swiftest way, we just sort them all based on path length. */
  1025. struct Cookie **array;
  1026. size_t i;
  1027. /* alloc an array and store all cookie pointers */
  1028. array = malloc(sizeof(struct Cookie *) * matches);
  1029. if(!array)
  1030. goto fail;
  1031. co = mainco;
  1032. for(i=0; co; co = co->next)
  1033. array[i++] = co;
  1034. /* now sort the cookie pointers in path length order */
  1035. qsort(array, matches, sizeof(struct Cookie *), cookie_sort);
  1036. /* remake the linked list order according to the new order */
  1037. mainco = array[0]; /* start here */
  1038. for(i=0; i<matches-1; i++)
  1039. array[i]->next = array[i+1];
  1040. array[matches-1]->next = NULL; /* terminate the list */
  1041. free(array); /* remove the temporary data again */
  1042. }
  1043. return mainco; /* return the new list */
  1044. }
  1045. /*****************************************************************************
  1046. *
  1047. * Curl_cookie_clearall()
  1048. *
  1049. * Clear all existing cookies and reset the counter.
  1050. *
  1051. ****************************************************************************/
  1052. void Curl_cookie_clearall(struct CookieInfo *cookies)
  1053. {
  1054. if(cookies) {
  1055. Curl_cookie_freelist(cookies->cookies);
  1056. cookies->cookies = NULL;
  1057. cookies->numcookies = 0;
  1058. }
  1059. }
  1060. /*****************************************************************************
  1061. *
  1062. * Curl_cookie_freelist()
  1063. *
  1064. * Free a list of cookies previously returned by Curl_cookie_getlist();
  1065. *
  1066. ****************************************************************************/
  1067. void Curl_cookie_freelist(struct Cookie *co)
  1068. {
  1069. struct Cookie *next;
  1070. while(co) {
  1071. next = co->next;
  1072. freecookie(co);
  1073. co = next;
  1074. }
  1075. }
  1076. /*****************************************************************************
  1077. *
  1078. * Curl_cookie_clearsess()
  1079. *
  1080. * Free all session cookies in the cookies list.
  1081. *
  1082. ****************************************************************************/
  1083. void Curl_cookie_clearsess(struct CookieInfo *cookies)
  1084. {
  1085. struct Cookie *first, *curr, *next, *prev = NULL;
  1086. if(!cookies || !cookies->cookies)
  1087. return;
  1088. first = curr = prev = cookies->cookies;
  1089. for(; curr; curr = next) {
  1090. next = curr->next;
  1091. if(!curr->expires) {
  1092. if(first == curr)
  1093. first = next;
  1094. if(prev == curr)
  1095. prev = next;
  1096. else
  1097. prev->next = next;
  1098. freecookie(curr);
  1099. cookies->numcookies--;
  1100. }
  1101. else
  1102. prev = curr;
  1103. }
  1104. cookies->cookies = first;
  1105. }
  1106. /*****************************************************************************
  1107. *
  1108. * Curl_cookie_cleanup()
  1109. *
  1110. * Free a "cookie object" previous created with Curl_cookie_init().
  1111. *
  1112. ****************************************************************************/
  1113. void Curl_cookie_cleanup(struct CookieInfo *c)
  1114. {
  1115. if(c) {
  1116. free(c->filename);
  1117. Curl_cookie_freelist(c->cookies);
  1118. free(c); /* free the base struct as well */
  1119. }
  1120. }
  1121. /* get_netscape_format()
  1122. *
  1123. * Formats a string for Netscape output file, w/o a newline at the end.
  1124. *
  1125. * Function returns a char * to a formatted line. Has to be free()d
  1126. */
  1127. static char *get_netscape_format(const struct Cookie *co)
  1128. {
  1129. return aprintf(
  1130. "%s" /* httponly preamble */
  1131. "%s%s\t" /* domain */
  1132. "%s\t" /* tailmatch */
  1133. "%s\t" /* path */
  1134. "%s\t" /* secure */
  1135. "%" CURL_FORMAT_CURL_OFF_T "\t" /* expires */
  1136. "%s\t" /* name */
  1137. "%s", /* value */
  1138. co->httponly?"#HttpOnly_":"",
  1139. /* Make sure all domains are prefixed with a dot if they allow
  1140. tailmatching. This is Mozilla-style. */
  1141. (co->tailmatch && co->domain && co->domain[0] != '.')? ".":"",
  1142. co->domain?co->domain:"unknown",
  1143. co->tailmatch?"TRUE":"FALSE",
  1144. co->path?co->path:"/",
  1145. co->secure?"TRUE":"FALSE",
  1146. co->expires,
  1147. co->name,
  1148. co->value?co->value:"");
  1149. }
  1150. /*
  1151. * cookie_output()
  1152. *
  1153. * Writes all internally known cookies to the specified file. Specify
  1154. * "-" as file name to write to stdout.
  1155. *
  1156. * The function returns non-zero on write failure.
  1157. */
  1158. static int cookie_output(struct CookieInfo *c, const char *dumphere)
  1159. {
  1160. struct Cookie *co;
  1161. FILE *out;
  1162. bool use_stdout=FALSE;
  1163. char *format_ptr;
  1164. if((NULL == c) || (0 == c->numcookies))
  1165. /* If there are no known cookies, we don't write or even create any
  1166. destination file */
  1167. return 0;
  1168. /* at first, remove expired cookies */
  1169. remove_expired(c);
  1170. if(!strcmp("-", dumphere)) {
  1171. /* use stdout */
  1172. out = stdout;
  1173. use_stdout=TRUE;
  1174. }
  1175. else {
  1176. out = fopen(dumphere, FOPEN_WRITETEXT);
  1177. if(!out)
  1178. return 1; /* failure */
  1179. }
  1180. fputs("# Netscape HTTP Cookie File\n"
  1181. "# https://curl.haxx.se/docs/http-cookies.html\n"
  1182. "# This file was generated by libcurl! Edit at your own risk.\n\n",
  1183. out);
  1184. for(co = c->cookies; co; co = co->next) {
  1185. if(!co->domain)
  1186. continue;
  1187. format_ptr = get_netscape_format(co);
  1188. if(format_ptr == NULL) {
  1189. fprintf(out, "#\n# Fatal libcurl error\n");
  1190. if(!use_stdout)
  1191. fclose(out);
  1192. return 1;
  1193. }
  1194. fprintf(out, "%s\n", format_ptr);
  1195. free(format_ptr);
  1196. }
  1197. if(!use_stdout)
  1198. fclose(out);
  1199. return 0;
  1200. }
  1201. struct curl_slist *Curl_cookie_list(struct Curl_easy *data)
  1202. {
  1203. struct curl_slist *list = NULL;
  1204. struct curl_slist *beg;
  1205. struct Cookie *c;
  1206. char *line;
  1207. if((data->cookies == NULL) ||
  1208. (data->cookies->numcookies == 0))
  1209. return NULL;
  1210. for(c = data->cookies->cookies; c; c = c->next) {
  1211. if(!c->domain)
  1212. continue;
  1213. line = get_netscape_format(c);
  1214. if(!line) {
  1215. curl_slist_free_all(list);
  1216. return NULL;
  1217. }
  1218. beg = Curl_slist_append_nodup(list, line);
  1219. if(!beg) {
  1220. free(line);
  1221. curl_slist_free_all(list);
  1222. return NULL;
  1223. }
  1224. list = beg;
  1225. }
  1226. return list;
  1227. }
  1228. void Curl_flush_cookies(struct Curl_easy *data, int cleanup)
  1229. {
  1230. if(data->set.str[STRING_COOKIEJAR]) {
  1231. if(data->change.cookielist) {
  1232. /* If there is a list of cookie files to read, do it first so that
  1233. we have all the told files read before we write the new jar.
  1234. Curl_cookie_loadfiles() LOCKS and UNLOCKS the share itself! */
  1235. Curl_cookie_loadfiles(data);
  1236. }
  1237. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
  1238. /* if we have a destination file for all the cookies to get dumped to */
  1239. if(cookie_output(data->cookies, data->set.str[STRING_COOKIEJAR]))
  1240. infof(data, "WARNING: failed to save cookies in %s\n",
  1241. data->set.str[STRING_COOKIEJAR]);
  1242. }
  1243. else {
  1244. if(cleanup && data->change.cookielist) {
  1245. /* since nothing is written, we can just free the list of cookie file
  1246. names */
  1247. curl_slist_free_all(data->change.cookielist); /* clean up list */
  1248. data->change.cookielist = NULL;
  1249. }
  1250. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
  1251. }
  1252. if(cleanup && (!data->share || (data->cookies != data->share->cookies))) {
  1253. Curl_cookie_cleanup(data->cookies);
  1254. }
  1255. Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
  1256. }
  1257. #endif /* CURL_DISABLE_HTTP || CURL_DISABLE_COOKIES */