cookie.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, Daniel Stenberg, <[email protected]>, 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. else
  816. /* read a partial, discard the next piece that ends with newline */
  817. partial = TRUE;
  818. }
  819. else
  820. break;
  821. }
  822. return NULL;
  823. }
  824. /*****************************************************************************
  825. *
  826. * Curl_cookie_init()
  827. *
  828. * Inits a cookie struct to read data from a local file. This is always
  829. * called before any cookies are set. File may be NULL.
  830. *
  831. * If 'newsession' is TRUE, discard all "session cookies" on read from file.
  832. *
  833. * Returns NULL on out of memory. Invalid cookies are ignored.
  834. ****************************************************************************/
  835. struct CookieInfo *Curl_cookie_init(struct Curl_easy *data,
  836. const char *file,
  837. struct CookieInfo *inc,
  838. bool newsession)
  839. {
  840. struct CookieInfo *c;
  841. FILE *fp = NULL;
  842. bool fromfile=TRUE;
  843. char *line = NULL;
  844. if(NULL == inc) {
  845. /* we didn't get a struct, create one */
  846. c = calloc(1, sizeof(struct CookieInfo));
  847. if(!c)
  848. return NULL; /* failed to get memory */
  849. c->filename = strdup(file?file:"none"); /* copy the name just in case */
  850. if(!c->filename)
  851. goto fail; /* failed to get memory */
  852. }
  853. else {
  854. /* we got an already existing one, use that */
  855. c = inc;
  856. }
  857. c->running = FALSE; /* this is not running, this is init */
  858. if(file && !strcmp(file, "-")) {
  859. fp = stdin;
  860. fromfile=FALSE;
  861. }
  862. else if(file && !*file) {
  863. /* points to a "" string */
  864. fp = NULL;
  865. }
  866. else
  867. fp = file?fopen(file, FOPEN_READTEXT):NULL;
  868. c->newsession = newsession; /* new session? */
  869. if(fp) {
  870. char *lineptr;
  871. bool headerline;
  872. line = malloc(MAX_COOKIE_LINE);
  873. if(!line)
  874. goto fail;
  875. while(get_line(line, MAX_COOKIE_LINE, fp)) {
  876. if(checkprefix("Set-Cookie:", line)) {
  877. /* This is a cookie line, get it! */
  878. lineptr=&line[11];
  879. headerline=TRUE;
  880. }
  881. else {
  882. lineptr=line;
  883. headerline=FALSE;
  884. }
  885. while(*lineptr && ISBLANK(*lineptr))
  886. lineptr++;
  887. Curl_cookie_add(data, c, headerline, lineptr, NULL, NULL);
  888. }
  889. free(line); /* free the line buffer */
  890. if(fromfile)
  891. fclose(fp);
  892. }
  893. c->running = TRUE; /* now, we're running */
  894. return c;
  895. fail:
  896. free(line);
  897. if(!inc)
  898. /* Only clean up if we allocated it here, as the original could still be in
  899. * use by a share handle */
  900. Curl_cookie_cleanup(c);
  901. if(fromfile && fp)
  902. fclose(fp);
  903. return NULL; /* out of memory */
  904. }
  905. /* sort this so that the longest path gets before the shorter path */
  906. static int cookie_sort(const void *p1, const void *p2)
  907. {
  908. struct Cookie *c1 = *(struct Cookie **)p1;
  909. struct Cookie *c2 = *(struct Cookie **)p2;
  910. size_t l1, l2;
  911. /* 1 - compare cookie path lengths */
  912. l1 = c1->path ? strlen(c1->path) : 0;
  913. l2 = c2->path ? strlen(c2->path) : 0;
  914. if(l1 != l2)
  915. return (l2 > l1) ? 1 : -1 ; /* avoid size_t <=> int conversions */
  916. /* 2 - compare cookie domain lengths */
  917. l1 = c1->domain ? strlen(c1->domain) : 0;
  918. l2 = c2->domain ? strlen(c2->domain) : 0;
  919. if(l1 != l2)
  920. return (l2 > l1) ? 1 : -1 ; /* avoid size_t <=> int conversions */
  921. /* 3 - compare cookie names */
  922. if(c1->name && c2->name)
  923. return strcmp(c1->name, c2->name);
  924. /* sorry, can't be more deterministic */
  925. return 0;
  926. }
  927. #define CLONE(field) \
  928. do { \
  929. if(src->field) { \
  930. d->field = strdup(src->field); \
  931. if(!d->field) \
  932. goto fail; \
  933. } \
  934. } while(0)
  935. static struct Cookie *dup_cookie(struct Cookie *src)
  936. {
  937. struct Cookie *d = calloc(sizeof(struct Cookie), 1);
  938. if(d) {
  939. CLONE(expirestr);
  940. CLONE(domain);
  941. CLONE(path);
  942. CLONE(spath);
  943. CLONE(name);
  944. CLONE(value);
  945. CLONE(maxage);
  946. CLONE(version);
  947. d->expires = src->expires;
  948. d->tailmatch = src->tailmatch;
  949. d->secure = src->secure;
  950. d->livecookie = src->livecookie;
  951. d->httponly = src->httponly;
  952. }
  953. return d;
  954. fail:
  955. freecookie(d);
  956. return NULL;
  957. }
  958. /*****************************************************************************
  959. *
  960. * Curl_cookie_getlist()
  961. *
  962. * For a given host and path, return a linked list of cookies that the
  963. * client should send to the server if used now. The secure boolean informs
  964. * the cookie if a secure connection is achieved or not.
  965. *
  966. * It shall only return cookies that haven't expired.
  967. *
  968. ****************************************************************************/
  969. struct Cookie *Curl_cookie_getlist(struct CookieInfo *c,
  970. const char *host, const char *path,
  971. bool secure)
  972. {
  973. struct Cookie *newco;
  974. struct Cookie *co;
  975. time_t now = time(NULL);
  976. struct Cookie *mainco=NULL;
  977. size_t matches = 0;
  978. bool is_ip;
  979. if(!c || !c->cookies)
  980. return NULL; /* no cookie struct or no cookies in the struct */
  981. /* at first, remove expired cookies */
  982. remove_expired(c);
  983. /* check if host is an IP(v4|v6) address */
  984. is_ip = isip(host);
  985. co = c->cookies;
  986. while(co) {
  987. /* only process this cookie if it is not expired or had no expire
  988. date AND that if the cookie requires we're secure we must only
  989. continue if we are! */
  990. if((!co->expires || (co->expires > now)) &&
  991. (co->secure?secure:TRUE)) {
  992. /* now check if the domain is correct */
  993. if(!co->domain ||
  994. (co->tailmatch && !is_ip && tailmatch(co->domain, host)) ||
  995. ((!co->tailmatch || is_ip) && strcasecompare(host, co->domain)) ) {
  996. /* the right part of the host matches the domain stuff in the
  997. cookie data */
  998. /* now check the left part of the path with the cookies path
  999. requirement */
  1000. if(!co->spath || pathmatch(co->spath, path) ) {
  1001. /* and now, we know this is a match and we should create an
  1002. entry for the return-linked-list */
  1003. newco = dup_cookie(co);
  1004. if(newco) {
  1005. /* then modify our next */
  1006. newco->next = mainco;
  1007. /* point the main to us */
  1008. mainco = newco;
  1009. matches++;
  1010. }
  1011. else {
  1012. fail:
  1013. /* failure, clear up the allocated chain and return NULL */
  1014. Curl_cookie_freelist(mainco);
  1015. return NULL;
  1016. }
  1017. }
  1018. }
  1019. }
  1020. co = co->next;
  1021. }
  1022. if(matches) {
  1023. /* Now we need to make sure that if there is a name appearing more than
  1024. once, the longest specified path version comes first. To make this
  1025. the swiftest way, we just sort them all based on path length. */
  1026. struct Cookie **array;
  1027. size_t i;
  1028. /* alloc an array and store all cookie pointers */
  1029. array = malloc(sizeof(struct Cookie *) * matches);
  1030. if(!array)
  1031. goto fail;
  1032. co = mainco;
  1033. for(i=0; co; co = co->next)
  1034. array[i++] = co;
  1035. /* now sort the cookie pointers in path length order */
  1036. qsort(array, matches, sizeof(struct Cookie *), cookie_sort);
  1037. /* remake the linked list order according to the new order */
  1038. mainco = array[0]; /* start here */
  1039. for(i=0; i<matches-1; i++)
  1040. array[i]->next = array[i+1];
  1041. array[matches-1]->next = NULL; /* terminate the list */
  1042. free(array); /* remove the temporary data again */
  1043. }
  1044. return mainco; /* return the new list */
  1045. }
  1046. /*****************************************************************************
  1047. *
  1048. * Curl_cookie_clearall()
  1049. *
  1050. * Clear all existing cookies and reset the counter.
  1051. *
  1052. ****************************************************************************/
  1053. void Curl_cookie_clearall(struct CookieInfo *cookies)
  1054. {
  1055. if(cookies) {
  1056. Curl_cookie_freelist(cookies->cookies);
  1057. cookies->cookies = NULL;
  1058. cookies->numcookies = 0;
  1059. }
  1060. }
  1061. /*****************************************************************************
  1062. *
  1063. * Curl_cookie_freelist()
  1064. *
  1065. * Free a list of cookies previously returned by Curl_cookie_getlist();
  1066. *
  1067. ****************************************************************************/
  1068. void Curl_cookie_freelist(struct Cookie *co)
  1069. {
  1070. struct Cookie *next;
  1071. while(co) {
  1072. next = co->next;
  1073. freecookie(co);
  1074. co = next;
  1075. }
  1076. }
  1077. /*****************************************************************************
  1078. *
  1079. * Curl_cookie_clearsess()
  1080. *
  1081. * Free all session cookies in the cookies list.
  1082. *
  1083. ****************************************************************************/
  1084. void Curl_cookie_clearsess(struct CookieInfo *cookies)
  1085. {
  1086. struct Cookie *first, *curr, *next, *prev = NULL;
  1087. if(!cookies || !cookies->cookies)
  1088. return;
  1089. first = curr = prev = cookies->cookies;
  1090. for(; curr; curr = next) {
  1091. next = curr->next;
  1092. if(!curr->expires) {
  1093. if(first == curr)
  1094. first = next;
  1095. if(prev == curr)
  1096. prev = next;
  1097. else
  1098. prev->next = next;
  1099. freecookie(curr);
  1100. cookies->numcookies--;
  1101. }
  1102. else
  1103. prev = curr;
  1104. }
  1105. cookies->cookies = first;
  1106. }
  1107. /*****************************************************************************
  1108. *
  1109. * Curl_cookie_cleanup()
  1110. *
  1111. * Free a "cookie object" previous created with Curl_cookie_init().
  1112. *
  1113. ****************************************************************************/
  1114. void Curl_cookie_cleanup(struct CookieInfo *c)
  1115. {
  1116. if(c) {
  1117. free(c->filename);
  1118. Curl_cookie_freelist(c->cookies);
  1119. free(c); /* free the base struct as well */
  1120. }
  1121. }
  1122. /* get_netscape_format()
  1123. *
  1124. * Formats a string for Netscape output file, w/o a newline at the end.
  1125. *
  1126. * Function returns a char * to a formatted line. Has to be free()d
  1127. */
  1128. static char *get_netscape_format(const struct Cookie *co)
  1129. {
  1130. return aprintf(
  1131. "%s" /* httponly preamble */
  1132. "%s%s\t" /* domain */
  1133. "%s\t" /* tailmatch */
  1134. "%s\t" /* path */
  1135. "%s\t" /* secure */
  1136. "%" CURL_FORMAT_CURL_OFF_T "\t" /* expires */
  1137. "%s\t" /* name */
  1138. "%s", /* value */
  1139. co->httponly?"#HttpOnly_":"",
  1140. /* Make sure all domains are prefixed with a dot if they allow
  1141. tailmatching. This is Mozilla-style. */
  1142. (co->tailmatch && co->domain && co->domain[0] != '.')? ".":"",
  1143. co->domain?co->domain:"unknown",
  1144. co->tailmatch?"TRUE":"FALSE",
  1145. co->path?co->path:"/",
  1146. co->secure?"TRUE":"FALSE",
  1147. co->expires,
  1148. co->name,
  1149. co->value?co->value:"");
  1150. }
  1151. /*
  1152. * cookie_output()
  1153. *
  1154. * Writes all internally known cookies to the specified file. Specify
  1155. * "-" as file name to write to stdout.
  1156. *
  1157. * The function returns non-zero on write failure.
  1158. */
  1159. static int cookie_output(struct CookieInfo *c, const char *dumphere)
  1160. {
  1161. struct Cookie *co;
  1162. FILE *out;
  1163. bool use_stdout=FALSE;
  1164. char *format_ptr;
  1165. if((NULL == c) || (0 == c->numcookies))
  1166. /* If there are no known cookies, we don't write or even create any
  1167. destination file */
  1168. return 0;
  1169. /* at first, remove expired cookies */
  1170. remove_expired(c);
  1171. if(!strcmp("-", dumphere)) {
  1172. /* use stdout */
  1173. out = stdout;
  1174. use_stdout=TRUE;
  1175. }
  1176. else {
  1177. out = fopen(dumphere, FOPEN_WRITETEXT);
  1178. if(!out)
  1179. return 1; /* failure */
  1180. }
  1181. fputs("# Netscape HTTP Cookie File\n"
  1182. "# https://curl.haxx.se/docs/http-cookies.html\n"
  1183. "# This file was generated by libcurl! Edit at your own risk.\n\n",
  1184. out);
  1185. for(co = c->cookies; co; co = co->next) {
  1186. if(!co->domain)
  1187. continue;
  1188. format_ptr = get_netscape_format(co);
  1189. if(format_ptr == NULL) {
  1190. fprintf(out, "#\n# Fatal libcurl error\n");
  1191. if(!use_stdout)
  1192. fclose(out);
  1193. return 1;
  1194. }
  1195. fprintf(out, "%s\n", format_ptr);
  1196. free(format_ptr);
  1197. }
  1198. if(!use_stdout)
  1199. fclose(out);
  1200. return 0;
  1201. }
  1202. struct curl_slist *Curl_cookie_list(struct Curl_easy *data)
  1203. {
  1204. struct curl_slist *list = NULL;
  1205. struct curl_slist *beg;
  1206. struct Cookie *c;
  1207. char *line;
  1208. if((data->cookies == NULL) ||
  1209. (data->cookies->numcookies == 0))
  1210. return NULL;
  1211. for(c = data->cookies->cookies; c; c = c->next) {
  1212. if(!c->domain)
  1213. continue;
  1214. line = get_netscape_format(c);
  1215. if(!line) {
  1216. curl_slist_free_all(list);
  1217. return NULL;
  1218. }
  1219. beg = Curl_slist_append_nodup(list, line);
  1220. if(!beg) {
  1221. free(line);
  1222. curl_slist_free_all(list);
  1223. return NULL;
  1224. }
  1225. list = beg;
  1226. }
  1227. return list;
  1228. }
  1229. void Curl_flush_cookies(struct Curl_easy *data, int cleanup)
  1230. {
  1231. if(data->set.str[STRING_COOKIEJAR]) {
  1232. if(data->change.cookielist) {
  1233. /* If there is a list of cookie files to read, do it first so that
  1234. we have all the told files read before we write the new jar.
  1235. Curl_cookie_loadfiles() LOCKS and UNLOCKS the share itself! */
  1236. Curl_cookie_loadfiles(data);
  1237. }
  1238. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
  1239. /* if we have a destination file for all the cookies to get dumped to */
  1240. if(cookie_output(data->cookies, data->set.str[STRING_COOKIEJAR]))
  1241. infof(data, "WARNING: failed to save cookies in %s\n",
  1242. data->set.str[STRING_COOKIEJAR]);
  1243. }
  1244. else {
  1245. if(cleanup && data->change.cookielist) {
  1246. /* since nothing is written, we can just free the list of cookie file
  1247. names */
  1248. curl_slist_free_all(data->change.cookielist); /* clean up list */
  1249. data->change.cookielist = NULL;
  1250. }
  1251. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
  1252. }
  1253. if(cleanup && (!data->share || (data->cookies != data->share->cookies))) {
  1254. Curl_cookie_cleanup(data->cookies);
  1255. }
  1256. Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
  1257. }
  1258. #endif /* CURL_DISABLE_HTTP || CURL_DISABLE_COOKIES */