cookie.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2002, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * In order to be useful for every potential user, curl and libcurl are
  11. * dual-licensed under the MPL and the MIT/X-derivate licenses.
  12. *
  13. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  14. * copies of the Software, and permit persons to whom the Software is
  15. * furnished to do so, under the terms of the MPL or the MIT/X-derivate
  16. * licenses. You may pick one of these licenses.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. *****************************************************************************/
  23. /***
  24. RECEIVING COOKIE INFORMATION
  25. ============================
  26. struct CookieInfo *cookie_init(char *file);
  27. Inits a cookie struct to store data in a local file. This is always
  28. called before any cookies are set.
  29. int cookies_set(struct CookieInfo *cookie, char *cookie_line);
  30. The 'cookie_line' parameter is a full "Set-cookie:" line as
  31. received from a server.
  32. The function need to replace previously stored lines that this new
  33. line superceeds.
  34. It may remove lines that are expired.
  35. It should return an indication of success/error.
  36. SENDING COOKIE INFORMATION
  37. ==========================
  38. struct Cookies *cookie_getlist(struct CookieInfo *cookie,
  39. char *host, char *path, bool secure);
  40. For a given host and path, return a linked list of cookies that
  41. the client should send to the server if used now. The secure
  42. boolean informs the cookie if a secure connection is achieved or
  43. not.
  44. It shall only return cookies that haven't expired.
  45. Example set of cookies:
  46. Set-cookie: PRODUCTINFO=webxpress; domain=.fidelity.com; path=/; secure
  47. Set-cookie: PERSONALIZE=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  48. domain=.fidelity.com; path=/ftgw; secure
  49. Set-cookie: FidHist=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  50. domain=.fidelity.com; path=/; secure
  51. Set-cookie: FidOrder=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  52. domain=.fidelity.com; path=/; secure
  53. Set-cookie: DisPend=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  54. domain=.fidelity.com; path=/; secure
  55. Set-cookie: FidDis=none;expires=Monday, 13-Jun-1988 03:04:55 GMT;
  56. domain=.fidelity.com; path=/; secure
  57. Set-cookie:
  58. Session_Key@6791a9e0-901a-11d0-a1c8-9b012c88aa77=none;expires=Monday,
  59. 13-Jun-1988 03:04:55 GMT; domain=.fidelity.com; path=/; secure
  60. ****/
  61. #include "setup.h"
  62. #include <stdlib.h>
  63. #include <string.h>
  64. #include <ctype.h>
  65. #include "cookie.h"
  66. #include "getdate.h"
  67. #include "strequal.h"
  68. #include "strtok.h"
  69. /* The last #include file should be: */
  70. #ifdef MALLOCDEBUG
  71. #include "memdebug.h"
  72. #endif
  73. /****************************************************************************
  74. *
  75. * Curl_cookie_add()
  76. *
  77. * Add a single cookie line to the cookie keeping object.
  78. *
  79. ***************************************************************************/
  80. struct Cookie *
  81. Curl_cookie_add(struct CookieInfo *c,
  82. bool httpheader, /* TRUE if HTTP header-style line */
  83. char *lineptr, /* first non-space of the line */
  84. char *domain) /* default domain */
  85. {
  86. struct Cookie *clist;
  87. char what[MAX_COOKIE_LINE];
  88. char name[MAX_NAME];
  89. char *ptr;
  90. char *semiptr;
  91. struct Cookie *co;
  92. struct Cookie *lastc=NULL;
  93. time_t now = time(NULL);
  94. bool replace_old = FALSE;
  95. /* First, alloc and init a new struct for it */
  96. co = (struct Cookie *)malloc(sizeof(struct Cookie));
  97. if(!co)
  98. return NULL; /* bail out if we're this low on memory */
  99. /* clear the whole struct first */
  100. memset(co, 0, sizeof(struct Cookie));
  101. if(httpheader) {
  102. /* This line was read off a HTTP-header */
  103. char *sep;
  104. semiptr=strchr(lineptr, ';'); /* first, find a semicolon */
  105. ptr = lineptr;
  106. do {
  107. /* we have a <what>=<this> pair or a 'secure' word here */
  108. sep = strchr(ptr, '=');
  109. if(sep && (!semiptr || (semiptr>sep)) ) {
  110. /*
  111. * There is a = sign and if there was a semicolon too, which make sure
  112. * that the semicolon comes _after_ the equal sign.
  113. */
  114. name[0]=what[0]=0; /* init the buffers */
  115. if(1 <= sscanf(ptr, "%" MAX_NAME_TXT "[^;=]=%"
  116. MAX_COOKIE_LINE_TXT "[^;\r\n]",
  117. name, what)) {
  118. /* this is a <name>=<what> pair */
  119. /* Strip off trailing whitespace from the 'what' */
  120. int len=strlen(what);
  121. while(len && isspace((int)what[len-1])) {
  122. what[len-1]=0;
  123. len--;
  124. }
  125. if(strequal("path", name)) {
  126. co->path=strdup(what);
  127. }
  128. else if(strequal("domain", name)) {
  129. co->domain=strdup(what);
  130. co->field1= (what[0]=='.')?2:1;
  131. }
  132. else if(strequal("version", name)) {
  133. co->version=strdup(what);
  134. }
  135. else if(strequal("max-age", name)) {
  136. /* Defined in RFC2109:
  137. Optional. The Max-Age attribute defines the lifetime of the
  138. cookie, in seconds. The delta-seconds value is a decimal non-
  139. negative integer. After delta-seconds seconds elapse, the
  140. client should discard the cookie. A value of zero means the
  141. cookie should be discarded immediately.
  142. */
  143. co->maxage = strdup(what);
  144. co->expires =
  145. atoi((*co->maxage=='\"')?&co->maxage[1]:&co->maxage[0]) + now;
  146. }
  147. else if(strequal("expires", name)) {
  148. co->expirestr=strdup(what);
  149. co->expires = curl_getdate(what, &now);
  150. }
  151. else if(!co->name) {
  152. co->name = strdup(name);
  153. co->value = strdup(what);
  154. }
  155. /*
  156. else this is the second (or more) name we don't know
  157. about! */
  158. }
  159. else {
  160. /* this is an "illegal" <what>=<this> pair */
  161. }
  162. }
  163. else {
  164. if(sscanf(ptr, "%" MAX_COOKIE_LINE_TXT "[^;\r\n]",
  165. what)) {
  166. if(strequal("secure", what))
  167. co->secure = TRUE;
  168. /* else,
  169. unsupported keyword without assign! */
  170. }
  171. }
  172. if(!semiptr || !*semiptr) {
  173. /* we already know there are no more cookies */
  174. semiptr = NULL;
  175. continue;
  176. }
  177. ptr=semiptr+1;
  178. while(ptr && *ptr && isspace((int)*ptr))
  179. ptr++;
  180. semiptr=strchr(ptr, ';'); /* now, find the next semicolon */
  181. if(!semiptr && *ptr)
  182. /* There are no more semicolons, but there's a final name=value pair
  183. coming up */
  184. semiptr=strchr(ptr, '\0');
  185. } while(semiptr);
  186. if(NULL == co->name) {
  187. /* we didn't get a cookie name, this is an illegal line, bail out */
  188. if(co->domain)
  189. free(co->domain);
  190. if(co->path)
  191. free(co->path);
  192. if(co->name)
  193. free(co->name);
  194. if(co->value)
  195. free(co->value);
  196. free(co);
  197. return NULL;
  198. }
  199. if(NULL == co->domain)
  200. /* no domain given in the header line, set the default now */
  201. co->domain=domain?strdup(domain):NULL;
  202. }
  203. else {
  204. /* This line is NOT a HTTP header style line, we do offer support for
  205. reading the odd netscape cookies-file format here */
  206. char *firstptr;
  207. char *tok_buf;
  208. int fields;
  209. if(lineptr[0]=='#') {
  210. /* don't even try the comments */
  211. free(co);
  212. return NULL;
  213. }
  214. /* strip off the possible end-of-line characters */
  215. ptr=strchr(lineptr, '\r');
  216. if(ptr)
  217. *ptr=0; /* clear it */
  218. ptr=strchr(lineptr, '\n');
  219. if(ptr)
  220. *ptr=0; /* clear it */
  221. firstptr=strtok_r(lineptr, "\t", &tok_buf); /* first tokenize it on the TAB */
  222. /* Here's a quick check to eliminate normal HTTP-headers from this */
  223. if(!firstptr || strchr(firstptr, ':')) {
  224. free(co);
  225. return NULL;
  226. }
  227. /* Now loop through the fields and init the struct we already have
  228. allocated */
  229. for(ptr=firstptr, fields=0; ptr; ptr=strtok_r(NULL, "\t", &tok_buf), fields++) {
  230. switch(fields) {
  231. case 0:
  232. co->domain = strdup(ptr);
  233. break;
  234. case 1:
  235. /* This field got its explanation on the 23rd of May 2001 by
  236. Andrés García:
  237. flag: A TRUE/FALSE value indicating if all machines within a given
  238. domain can access the variable. This value is set automatically by
  239. the browser, depending on the value you set for the domain.
  240. As far as I can see, it is set to true when the cookie says
  241. .domain.com and to false when the domain is complete www.domain.com
  242. We don't currently take advantage of this knowledge.
  243. */
  244. co->field1=strequal(ptr, "TRUE")+1; /* store information */
  245. break;
  246. case 2:
  247. /* It turns out, that sometimes the file format allows the path
  248. field to remain not filled in, we try to detect this and work
  249. around it! Andrés García made us aware of this... */
  250. if (strcmp("TRUE", ptr) && strcmp("FALSE", ptr)) {
  251. /* only if the path doesn't look like a boolean option! */
  252. co->path = strdup(ptr);
  253. break;
  254. }
  255. /* this doesn't look like a path, make one up! */
  256. co->path = strdup("/");
  257. fields++; /* add a field and fall down to secure */
  258. /* FALLTHROUGH */
  259. case 3:
  260. co->secure = strequal(ptr, "TRUE");
  261. break;
  262. case 4:
  263. co->expires = atoi(ptr);
  264. break;
  265. case 5:
  266. co->name = strdup(ptr);
  267. break;
  268. case 6:
  269. co->value = strdup(ptr);
  270. break;
  271. }
  272. }
  273. if(7 != fields) {
  274. /* we did not find the sufficient number of fields to recognize this
  275. as a valid line, abort and go home */
  276. if(co->domain)
  277. free(co->domain);
  278. if(co->path)
  279. free(co->path);
  280. if(co->name)
  281. free(co->name);
  282. if(co->value)
  283. free(co->value);
  284. free(co);
  285. return NULL;
  286. }
  287. }
  288. co->livecookie = c->running;
  289. /* now, we have parsed the incoming line, we must now check if this
  290. superceeds an already existing cookie, which it may if the previous have
  291. the same domain and path as this */
  292. clist = c->cookies;
  293. replace_old = FALSE;
  294. while(clist) {
  295. if(strequal(clist->name, co->name)) {
  296. /* the names are identical */
  297. if(clist->domain && co->domain) {
  298. if(strequal(clist->domain, co->domain))
  299. replace_old=TRUE;
  300. }
  301. else if(!clist->domain && !co->domain)
  302. replace_old = TRUE;
  303. if(replace_old) {
  304. /* the domains were identical */
  305. if(clist->path && co->path) {
  306. if(strequal(clist->path, co->path)) {
  307. replace_old = TRUE;
  308. }
  309. else
  310. replace_old = FALSE;
  311. }
  312. else if(!clist->path && !co->path)
  313. replace_old = TRUE;
  314. else
  315. replace_old = FALSE;
  316. }
  317. if(replace_old && !co->livecookie && clist->livecookie) {
  318. /* Both cookies matched fine, except that the already present
  319. cookie is "live", which means it was set from a header, while
  320. the new one isn't "live" and thus only read from a file. We let
  321. live cookies stay alive */
  322. /* Free the newcomer and get out of here! */
  323. if(co->domain)
  324. free(co->domain);
  325. if(co->path)
  326. free(co->path);
  327. if(co->name)
  328. free(co->name);
  329. if(co->value)
  330. free(co->value);
  331. free(co);
  332. return NULL;
  333. }
  334. if(replace_old) {
  335. co->next = clist->next; /* get the next-pointer first */
  336. /* then free all the old pointers */
  337. if(clist->name)
  338. free(clist->name);
  339. if(clist->value)
  340. free(clist->value);
  341. if(clist->domain)
  342. free(clist->domain);
  343. if(clist->path)
  344. free(clist->path);
  345. if(clist->expirestr)
  346. free(clist->expirestr);
  347. if(clist->version)
  348. free(clist->version);
  349. if(clist->maxage)
  350. free(clist->maxage);
  351. *clist = *co; /* then store all the new data */
  352. free(co); /* free the newly alloced memory */
  353. co = clist; /* point to the previous struct instead */
  354. /* We have replaced a cookie, now skip the rest of the list but
  355. make sure the 'lastc' pointer is properly set */
  356. do {
  357. lastc = clist;
  358. clist = clist->next;
  359. } while(clist);
  360. break;
  361. }
  362. }
  363. lastc = clist;
  364. clist = clist->next;
  365. }
  366. if(!replace_old) {
  367. /* then make the last item point on this new one */
  368. if(lastc)
  369. lastc->next = co;
  370. else
  371. c->cookies = co;
  372. }
  373. c->numcookies++; /* one more cookie in the jar */
  374. return co;
  375. }
  376. /*****************************************************************************
  377. *
  378. * Curl_cookie_init()
  379. *
  380. * Inits a cookie struct to read data from a local file. This is always
  381. * called before any cookies are set. File may be NULL.
  382. *
  383. ****************************************************************************/
  384. struct CookieInfo *Curl_cookie_init(char *file, struct CookieInfo *inc)
  385. {
  386. char line[MAX_COOKIE_LINE];
  387. struct CookieInfo *c;
  388. FILE *fp;
  389. bool fromfile=TRUE;
  390. if(NULL == inc) {
  391. /* we didn't get a struct, create one */
  392. c = (struct CookieInfo *)malloc(sizeof(struct CookieInfo));
  393. if(!c)
  394. return NULL; /* failed to get memory */
  395. memset(c, 0, sizeof(struct CookieInfo));
  396. c->filename = strdup(file?file:"none"); /* copy the name just in case */
  397. }
  398. else {
  399. /* we got an already existing one, use that */
  400. c = inc;
  401. }
  402. c->running = FALSE; /* this is not running, this is init */
  403. if(file && strequal(file, "-")) {
  404. fp = stdin;
  405. fromfile=FALSE;
  406. }
  407. else
  408. fp = file?fopen(file, "r"):NULL;
  409. if(fp) {
  410. char *lineptr;
  411. bool headerline;
  412. while(fgets(line, MAX_COOKIE_LINE, fp)) {
  413. if(strnequal("Set-Cookie:", line, 11)) {
  414. /* This is a cookie line, get it! */
  415. lineptr=&line[11];
  416. headerline=TRUE;
  417. }
  418. else {
  419. lineptr=line;
  420. headerline=FALSE;
  421. }
  422. while(*lineptr && isspace((int)*lineptr))
  423. lineptr++;
  424. Curl_cookie_add(c, headerline, lineptr, NULL);
  425. }
  426. if(fromfile)
  427. fclose(fp);
  428. }
  429. c->running = TRUE; /* now, we're running */
  430. return c;
  431. }
  432. /*****************************************************************************
  433. *
  434. * Curl_cookie_getlist()
  435. *
  436. * For a given host and path, return a linked list of cookies that the
  437. * client should send to the server if used now. The secure boolean informs
  438. * the cookie if a secure connection is achieved or not.
  439. *
  440. * It shall only return cookies that haven't expired.
  441. *
  442. ****************************************************************************/
  443. struct Cookie *Curl_cookie_getlist(struct CookieInfo *c,
  444. char *host, char *path, bool secure)
  445. {
  446. struct Cookie *newco;
  447. struct Cookie *co;
  448. time_t now = time(NULL);
  449. int hostlen=strlen(host);
  450. int domlen;
  451. struct Cookie *mainco=NULL;
  452. if(!c || !c->cookies)
  453. return NULL; /* no cookie struct or no cookies in the struct */
  454. co = c->cookies;
  455. while(co) {
  456. /* only process this cookie if it is not expired or had no expire
  457. date AND that if the cookie requires we're secure we must only
  458. continue if we are! */
  459. if( (co->expires<=0 || (co->expires> now)) &&
  460. (co->secure?secure:TRUE) ) {
  461. /* now check if the domain is correct */
  462. domlen=co->domain?strlen(co->domain):0;
  463. if(!co->domain ||
  464. ((domlen<=hostlen) &&
  465. strequal(host+(hostlen-domlen), co->domain)) ) {
  466. /* the right part of the host matches the domain stuff in the
  467. cookie data */
  468. /* now check the left part of the path with the cookies path
  469. requirement */
  470. if(!co->path ||
  471. strnequal(path, co->path, strlen(co->path))) {
  472. /* and now, we know this is a match and we should create an
  473. entry for the return-linked-list */
  474. newco = (struct Cookie *)malloc(sizeof(struct Cookie));
  475. if(newco) {
  476. /* first, copy the whole source cookie: */
  477. memcpy(newco, co, sizeof(struct Cookie));
  478. /* then modify our next */
  479. newco->next = mainco;
  480. /* point the main to us */
  481. mainco = newco;
  482. }
  483. }
  484. }
  485. }
  486. co = co->next;
  487. }
  488. return mainco; /* return the new list */
  489. }
  490. /*****************************************************************************
  491. *
  492. * Curl_cookie_freelist()
  493. *
  494. * Free a list of cookies previously returned by Curl_cookie_getlist();
  495. *
  496. ****************************************************************************/
  497. void Curl_cookie_freelist(struct Cookie *co)
  498. {
  499. struct Cookie *next;
  500. if(co) {
  501. while(co) {
  502. next = co->next;
  503. free(co); /* we only free the struct since the "members" are all
  504. just copied! */
  505. co = next;
  506. }
  507. }
  508. }
  509. /*****************************************************************************
  510. *
  511. * Curl_cookie_cleanup()
  512. *
  513. * Free a "cookie object" previous created with cookie_init().
  514. *
  515. ****************************************************************************/
  516. void Curl_cookie_cleanup(struct CookieInfo *c)
  517. {
  518. struct Cookie *co;
  519. struct Cookie *next;
  520. if(c) {
  521. if(c->filename)
  522. free(c->filename);
  523. co = c->cookies;
  524. while(co) {
  525. if(co->name)
  526. free(co->name);
  527. if(co->value)
  528. free(co->value);
  529. if(co->domain)
  530. free(co->domain);
  531. if(co->path)
  532. free(co->path);
  533. if(co->expirestr)
  534. free(co->expirestr);
  535. if(co->version)
  536. free(co->version);
  537. if(co->maxage)
  538. free(co->maxage);
  539. next = co->next;
  540. free(co);
  541. co = next;
  542. }
  543. free(c); /* free the base struct as well */
  544. }
  545. }
  546. /*
  547. * Curl_cookie_output()
  548. *
  549. * Writes all internally known cookies to the specified file. Specify
  550. * "-" as file name to write to stdout.
  551. *
  552. * The function returns non-zero on write failure.
  553. */
  554. int Curl_cookie_output(struct CookieInfo *c, char *dumphere)
  555. {
  556. struct Cookie *co;
  557. FILE *out;
  558. bool use_stdout=FALSE;
  559. if((NULL == c) || (0 == c->numcookies))
  560. /* If there are no known cookies, we don't write or even create any
  561. destination file */
  562. return 0;
  563. if(strequal("-", dumphere)) {
  564. /* use stdout */
  565. out = stdout;
  566. use_stdout=TRUE;
  567. }
  568. else {
  569. out = fopen(dumphere, "w");
  570. if(!out)
  571. return 1; /* failure */
  572. }
  573. if(c) {
  574. fputs("# Netscape HTTP Cookie File\n"
  575. "# http://www.netscape.com/newsref/std/cookie_spec.html\n"
  576. "# This file was generated by libcurl! Edit at your own risk.\n\n",
  577. out);
  578. co = c->cookies;
  579. while(co) {
  580. fprintf(out,
  581. "%s\t" /* domain */
  582. "%s\t" /* field1 */
  583. "%s\t" /* path */
  584. "%s\t" /* secure */
  585. "%u\t" /* expires */
  586. "%s\t" /* name */
  587. "%s\n", /* value */
  588. co->domain?co->domain:"unknown",
  589. co->field1==2?"TRUE":"FALSE",
  590. co->path?co->path:"/",
  591. co->secure?"TRUE":"FALSE",
  592. (unsigned int)co->expires,
  593. co->name,
  594. co->value?co->value:"");
  595. co=co->next;
  596. }
  597. }
  598. if(!use_stdout)
  599. fclose(out);
  600. return 0;
  601. }
  602. #ifdef CURL_COOKIE_DEBUG
  603. /*
  604. * On my Solaris box, this command line builds this test program:
  605. *
  606. * gcc -g -o cooktest -DCURL_COOKIE_DEBUG -DHAVE_CONFIG_H -I.. -I../include cookie.c strequal.o getdate.o memdebug.o mprintf.o strtok.o -lnsl -lsocket
  607. *
  608. */
  609. int main(int argc, char **argv)
  610. {
  611. struct CookieInfo *c=NULL;
  612. if(argc>1) {
  613. c = Curl_cookie_init(argv[1], c);
  614. Curl_cookie_add(c, TRUE, "PERSONALIZE=none;expires=Monday, 13-Jun-1988 03:04:55 GMT; domain=.fidelity.com; path=/ftgw; secure");
  615. Curl_cookie_add(c, TRUE, "foobar=yes; domain=.haxx.se; path=/looser;");
  616. c = Curl_cookie_init(argv[1], c);
  617. Curl_cookie_output(c);
  618. Curl_cookie_cleanup(c);
  619. return 0;
  620. }
  621. return 1;
  622. }
  623. #endif
  624. /*
  625. * local variables:
  626. * eval: (load-file "../curl-mode.el")
  627. * end:
  628. * vim600: fdm=marker
  629. * vim: et sw=2 ts=2 sts=2 tw=78
  630. */