webcookies.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Cookie file system. Allows hget and multiple webfs's to collaborate.
  11. * Conventionally mounted on /mnt/webcookies.
  12. */
  13. #include <u.h>
  14. #include <libc.h>
  15. #include <bio.h>
  16. #include <ndb.h>
  17. #include <fcall.h>
  18. #include <thread.h>
  19. #include <9p.h>
  20. #include <ctype.h>
  21. int debug = 0;
  22. typedef struct Cookie Cookie;
  23. typedef struct Jar Jar;
  24. struct Cookie
  25. {
  26. /* external info */
  27. char* name;
  28. char* value;
  29. char* dom; /* starts with . */
  30. char* path;
  31. char* version;
  32. char* comment; /* optional, may be nil */
  33. uint expire; /* time of expiration: ~0 means when webcookies dies */
  34. int secure;
  35. int explicitdom; /* dom was explicitly set */
  36. int explicitpath; /* path was explicitly set */
  37. int netscapestyle;
  38. /* internal info */
  39. int deleted;
  40. int mark;
  41. int ondisk;
  42. };
  43. struct Jar
  44. {
  45. Cookie *c;
  46. int nc;
  47. int mc;
  48. Qid qid;
  49. int dirty;
  50. char *file;
  51. char *lockfile;
  52. };
  53. struct {
  54. char *s;
  55. int offset;
  56. int ishttp;
  57. } stab[] = {
  58. "domain", offsetof(Cookie, dom), 1,
  59. "path", offsetof(Cookie, path), 1,
  60. "name", offsetof(Cookie, name), 0,
  61. "value", offsetof(Cookie, value), 0,
  62. "comment", offsetof(Cookie, comment), 1,
  63. "version", offsetof(Cookie, version), 1,
  64. };
  65. struct {
  66. char *s;
  67. int offset;
  68. } itab[] = {
  69. "expire", offsetof(Cookie, expire),
  70. "secure", offsetof(Cookie, secure),
  71. "explicitdomain", offsetof(Cookie, explicitdom),
  72. "explicitpath", offsetof(Cookie, explicitpath),
  73. "netscapestyle", offsetof(Cookie, netscapestyle),
  74. };
  75. /* HTTP format */
  76. int
  77. jarfmt(Fmt *fmt)
  78. {
  79. int i;
  80. Jar *jar;
  81. jar = va_arg(fmt->args, Jar*);
  82. if(jar == nil || jar->nc == 0)
  83. return fmtstrcpy(fmt, "");
  84. fmtprint(fmt, "Cookie: ");
  85. if(jar->c[0].version)
  86. fmtprint(fmt, "$Version=%s; ", jar->c[0].version);
  87. for(i=0; i<jar->nc; i++)
  88. fmtprint(fmt, "%s%s=%s", i ? "; ":"", jar->c[i].name, jar->c[i].value);
  89. fmtprint(fmt, "\r\n");
  90. return 0;
  91. }
  92. /* individual cookie */
  93. int
  94. cookiefmt(Fmt *fmt)
  95. {
  96. int j, k, first;
  97. char *t;
  98. Cookie *c;
  99. c = va_arg(fmt->args, Cookie*);
  100. first = 1;
  101. for(j=0; j<nelem(stab); j++){
  102. t = *(char**)((char*)c+stab[j].offset);
  103. if(t == nil)
  104. continue;
  105. if(first)
  106. first = 0;
  107. else
  108. fmtprint(fmt, " ");
  109. fmtprint(fmt, "%s=%q", stab[j].s, t);
  110. }
  111. for(j=0; j<nelem(itab); j++){
  112. k = *(int*)((char*)c+itab[j].offset);
  113. if(k == 0)
  114. continue;
  115. if(first)
  116. first = 0;
  117. else
  118. fmtprint(fmt, " ");
  119. fmtprint(fmt, "%s=%u", itab[j].s, k);
  120. }
  121. return 0;
  122. }
  123. /*
  124. * sort cookies:
  125. * - alpha by name
  126. * - alpha by domain
  127. * - longer paths first, then alpha by path (RFC2109 4.3.4)
  128. */
  129. int
  130. cookiecmp(const Cookie *a, const Cookie *b)
  131. {
  132. int i;
  133. if((i = strcmp(a->name, b->name)) != 0)
  134. return i;
  135. if((i = cistrcmp(a->dom, b->dom)) != 0)
  136. return i;
  137. if((i = strlen(b->path) - strlen(a->path)) != 0)
  138. return i;
  139. if((i = strcmp(a->path, b->path)) != 0)
  140. return i;
  141. return 0;
  142. }
  143. int
  144. exactcookiecmp(Cookie *a, Cookie *b)
  145. {
  146. int i;
  147. if((i = cookiecmp(a, b)) != 0)
  148. return i;
  149. if((i = strcmp(a->value, b->value)) != 0)
  150. return i;
  151. if(a->version || b->version){
  152. if(!a->version)
  153. return -1;
  154. if(!b->version)
  155. return 1;
  156. if((i = strcmp(a->version, b->version)) != 0)
  157. return i;
  158. }
  159. if(a->comment || b->comment){
  160. if(!a->comment)
  161. return -1;
  162. if(!b->comment)
  163. return 1;
  164. if((i = strcmp(a->comment, b->comment)) != 0)
  165. return i;
  166. }
  167. if((i = b->expire - a->expire) != 0)
  168. return i;
  169. if((i = b->secure - a->secure) != 0)
  170. return i;
  171. if((i = b->explicitdom - a->explicitdom) != 0)
  172. return i;
  173. if((i = b->explicitpath - a->explicitpath) != 0)
  174. return i;
  175. if((i = b->netscapestyle - a->netscapestyle) != 0)
  176. return i;
  177. return 0;
  178. }
  179. void
  180. freecookie(Cookie *c)
  181. {
  182. int i;
  183. for(i=0; i<nelem(stab); i++)
  184. free(*(char**)((char*)c+stab[i].offset));
  185. }
  186. void
  187. copycookie(Cookie *c)
  188. {
  189. int i;
  190. char **ps;
  191. for(i=0; i<nelem(stab); i++){
  192. ps = (char**)((char*)c+stab[i].offset);
  193. if(*ps)
  194. *ps = estrdup9p(*ps);
  195. }
  196. }
  197. void
  198. delcookie(Jar *j, Cookie *c)
  199. {
  200. int i;
  201. j->dirty = 1;
  202. i = c - j->c;
  203. if(i < 0 || i >= j->nc)
  204. abort();
  205. c->deleted = 1;
  206. }
  207. void
  208. addcookie(Jar *j, Cookie *c)
  209. {
  210. int i;
  211. if(!c->name || !c->value || !c->path || !c->dom){
  212. fprint(2, "not adding incomplete cookie\n");
  213. return;
  214. }
  215. if(debug)
  216. fprint(2, "add %K\n", c);
  217. for(i=0; i<j->nc; i++)
  218. if(cookiecmp(&j->c[i], c) == 0){
  219. if(debug)
  220. fprint(2, "cookie %K matches %K\n", &j->c[i], c);
  221. if(exactcookiecmp(&j->c[i], c) == 0){
  222. if(debug)
  223. fprint(2, "\texactly\n");
  224. j->c[i].mark = 0;
  225. return;
  226. }
  227. delcookie(j, &j->c[i]);
  228. }
  229. j->dirty = 1;
  230. if(j->nc == j->mc){
  231. j->mc += 16;
  232. j->c = erealloc9p(j->c, j->mc*sizeof(Cookie));
  233. }
  234. j->c[j->nc] = *c;
  235. copycookie(&j->c[j->nc]);
  236. j->nc++;
  237. }
  238. void
  239. purgejar(Jar *j)
  240. {
  241. int i;
  242. for(i=j->nc-1; i>=0; i--){
  243. if(!j->c[i].deleted)
  244. continue;
  245. freecookie(&j->c[i]);
  246. --j->nc;
  247. j->c[i] = j->c[j->nc];
  248. }
  249. }
  250. void
  251. addtojar(Jar *jar, char *line, int ondisk)
  252. {
  253. Cookie c;
  254. int i, j, nf, *pint;
  255. char *f[20], *attr, *val, **pstr;
  256. memset(&c, 0, sizeof c);
  257. c.expire = ~0;
  258. c.ondisk = ondisk;
  259. nf = tokenize(line, f, nelem(f));
  260. for(i=0; i<nf; i++){
  261. attr = f[i];
  262. if((val = strchr(attr, '=')) != nil)
  263. *val++ = '\0';
  264. else
  265. val = "";
  266. /* string attributes */
  267. for(j=0; j<nelem(stab); j++){
  268. if(strcmp(stab[j].s, attr) == 0){
  269. pstr = (char**)((char*)&c+stab[j].offset);
  270. *pstr = val;
  271. }
  272. }
  273. /* integer attributes */
  274. for(j=0; j<nelem(itab); j++){
  275. if(strcmp(itab[j].s, attr) == 0){
  276. pint = (int*)((char*)&c+itab[j].offset);
  277. if(val[0]=='\0')
  278. *pint = 1;
  279. else
  280. *pint = strtoul(val, 0, 0);
  281. }
  282. }
  283. }
  284. if(c.name==nil || c.value==nil || c.dom==nil || c.path==nil){
  285. if(debug)
  286. fprint(2, "ignoring fractional cookie %K\n", &c);
  287. return;
  288. }
  289. addcookie(jar, &c);
  290. }
  291. Jar*
  292. newjar(void)
  293. {
  294. Jar *jar;
  295. jar = emalloc9p(sizeof(Jar));
  296. return jar;
  297. }
  298. int
  299. expirejar(Jar *jar, int exiting)
  300. {
  301. int i, n;
  302. uint now;
  303. now = time(0);
  304. n = 0;
  305. for(i=0; i<jar->nc; i++){
  306. if(jar->c[i].expire < now || (exiting && jar->c[i].expire==~0)){
  307. delcookie(jar, &jar->c[i]);
  308. n++;
  309. }
  310. }
  311. return n;
  312. }
  313. int
  314. syncjar(Jar *jar)
  315. {
  316. int i, fd;
  317. char *line;
  318. Dir *d;
  319. Biobuf *b;
  320. Qid q;
  321. if(jar->file==nil)
  322. return 0;
  323. memset(&q, 0, sizeof q);
  324. if((d = dirstat(jar->file)) != nil){
  325. q = d->qid;
  326. if(d->qid.path != jar->qid.path || d->qid.vers != jar->qid.vers)
  327. jar->dirty = 1;
  328. free(d);
  329. }
  330. if(jar->dirty == 0)
  331. return 0;
  332. fd = -1;
  333. for(i=0; i<50; i++){
  334. if((fd = create(jar->lockfile, OWRITE, DMEXCL|0666)) < 0){
  335. sleep(100);
  336. continue;
  337. }
  338. break;
  339. }
  340. if(fd < 0){
  341. if(debug)
  342. fprint(2, "open %s: %r", jar->lockfile);
  343. werrstr("cannot acquire jar lock: %r");
  344. return -1;
  345. }
  346. for(i=0; i<jar->nc; i++) /* mark is cleared by addcookie */
  347. jar->c[i].mark = jar->c[i].ondisk;
  348. if((b = Bopen(jar->file, OREAD)) == nil){
  349. if(debug)
  350. fprint(2, "Bopen %s: %r", jar->file);
  351. werrstr("cannot read cookie file %s: %r", jar->file);
  352. close(fd);
  353. return -1;
  354. }
  355. for(; (line = Brdstr(b, '\n', 1)) != nil; free(line)){
  356. if(*line == '#')
  357. continue;
  358. addtojar(jar, line, 1);
  359. }
  360. Bterm(b);
  361. for(i=0; i<jar->nc; i++)
  362. if(jar->c[i].mark)
  363. delcookie(jar, &jar->c[i]);
  364. purgejar(jar);
  365. b = Bopen(jar->file, OWRITE);
  366. if(b == nil){
  367. if(debug)
  368. fprint(2, "Bopen write %s: %r", jar->file);
  369. close(fd);
  370. return -1;
  371. }
  372. Bprint(b, "# webcookies cookie jar\n");
  373. Bprint(b, "# comments and non-standard fields will be lost\n");
  374. for(i=0; i<jar->nc; i++){
  375. if(jar->c[i].expire == ~0)
  376. continue;
  377. Bprint(b, "%K\n", &jar->c[i]);
  378. jar->c[i].ondisk = 1;
  379. }
  380. Bterm(b);
  381. jar->dirty = 0;
  382. close(fd);
  383. if((d = dirstat(jar->file)) != nil){
  384. jar->qid = d->qid;
  385. free(d);
  386. }
  387. return 0;
  388. }
  389. Jar*
  390. readjar(char *file)
  391. {
  392. char *lock, *p;
  393. Jar *jar;
  394. jar = newjar();
  395. lock = emalloc9p(strlen(file)+10);
  396. strcpy(lock, file);
  397. if((p = strrchr(lock, '/')) != nil)
  398. p++;
  399. else
  400. p = lock;
  401. memmove(p+2, p, strlen(p)+1);
  402. p[0] = 'L';
  403. p[1] = '.';
  404. jar->lockfile = lock;
  405. jar->file = file;
  406. jar->dirty = 1;
  407. if(syncjar(jar) < 0){
  408. free(jar->file);
  409. free(jar->lockfile);
  410. free(jar);
  411. return nil;
  412. }
  413. return jar;
  414. }
  415. void
  416. closejar(Jar *jar)
  417. {
  418. int i;
  419. expirejar(jar, 0);
  420. if(syncjar(jar) < 0)
  421. fprint(2, "warning: cannot rewrite cookie jar: %r\n");
  422. for(i=0; i<jar->nc; i++)
  423. freecookie(&jar->c[i]);
  424. free(jar->file);
  425. free(jar);
  426. }
  427. /*
  428. * Domain name matching is per RFC2109, section 2:
  429. *
  430. * Hosts names can be specified either as an IP address or a FQHN
  431. * string. Sometimes we compare one host name with another. Host A's
  432. * name domain-matches host B's if
  433. *
  434. * * both host names are IP addresses and their host name strings match
  435. * exactly; or
  436. *
  437. * * both host names are FQDN strings and their host name strings match
  438. * exactly; or
  439. *
  440. * * A is a FQDN string and has the form NB, where N is a non-empty name
  441. * string, B has the form .B', and B' is a FQDN string. (So, x.y.com
  442. * domain-matches .y.com but not y.com.)
  443. *
  444. * Note that domain-match is not a commutative operation: a.b.c.com
  445. * domain-matches .c.com, but not the reverse.
  446. *
  447. * (This does not verify that IP addresses and FQDN's are well-formed.)
  448. */
  449. int
  450. isdomainmatch(char *name, char *pattern)
  451. {
  452. int lname, lpattern;
  453. if(cistrcmp(name, pattern)==0)
  454. return 1;
  455. if(strcmp(ipattr(name), "dom")==0 && pattern[0]=='.'){
  456. lname = strlen(name);
  457. lpattern = strlen(pattern);
  458. if(lname >= lpattern && cistrcmp(name+lname-lpattern, pattern)==0)
  459. return 1;
  460. }
  461. return 0;
  462. }
  463. /*
  464. * RFC2109 4.3.4:
  465. * - domain must match
  466. * - path in cookie must be a prefix of request path
  467. * - cookie must not have expired
  468. */
  469. int
  470. iscookiematch(Cookie *c, char *dom, char *path, uint now)
  471. {
  472. return isdomainmatch(dom, c->dom)
  473. && strncmp(c->path, path, strlen(c->path))==0
  474. && c->expire >= now;
  475. }
  476. /*
  477. * Produce a subjar of matching cookies.
  478. * Secure cookies are only included if secure is set.
  479. */
  480. Jar*
  481. cookiesearch(Jar *jar, char *dom, char *path, int issecure)
  482. {
  483. int i;
  484. Jar *j;
  485. uint now;
  486. now = time(0);
  487. j = newjar();
  488. for(i=0; i<jar->nc; i++)
  489. if((issecure || !jar->c[i].secure) && iscookiematch(&jar->c[i], dom, path, now))
  490. addcookie(j, &jar->c[i]);
  491. if(j->nc == 0){
  492. closejar(j);
  493. werrstr("no cookies found");
  494. return nil;
  495. }
  496. qsort(j->c, j->nc, sizeof(j->c[0]), (int(*)(const void*, const void*))cookiecmp);
  497. return j;
  498. }
  499. /*
  500. * RFC2109 4.3.2 security checks
  501. */
  502. char*
  503. isbadcookie(Cookie *c, char *dom, char *path)
  504. {
  505. if(strncmp(c->path, path, strlen(c->path)) != 0)
  506. return "cookie path is not a prefix of the request path";
  507. if(c->explicitdom && c->dom[0] != '.')
  508. return "cookie domain doesn't start with dot";
  509. if(memchr(c->dom+1, '.', strlen(c->dom)-1-1) == nil)
  510. return "cookie domain doesn't have embedded dots";
  511. if(!isdomainmatch(dom, c->dom))
  512. return "request host does not match cookie domain";
  513. if(strcmp(ipattr(dom), "dom")==0
  514. && memchr(dom, '.', strlen(dom)-strlen(c->dom)) != nil)
  515. return "request host contains dots before cookie domain";
  516. return 0;
  517. }
  518. /*
  519. * Sunday, 25-Jan-2002 12:24:36 GMT
  520. * Sunday, 25 Jan 2002 12:24:36 GMT
  521. * Sun, 25 Jan 02 12:24:36 GMT
  522. */
  523. int
  524. isleap(int year)
  525. {
  526. return year%4==0 && (year%100!=0 || year%400==0);
  527. }
  528. uint
  529. strtotime(char *s)
  530. {
  531. char *os;
  532. int i;
  533. Tm tm;
  534. static int mday[2][12] = {
  535. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
  536. 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
  537. };
  538. static char *wday[] = {
  539. "Sunday", "Monday", "Tuesday", "Wednesday",
  540. "Thursday", "Friday", "Saturday",
  541. };
  542. static char *mon[] = {
  543. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  544. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  545. };
  546. os = s;
  547. /* Sunday, */
  548. for(i=0; i<nelem(wday); i++){
  549. if(cistrncmp(s, wday[i], strlen(wday[i])) == 0){
  550. s += strlen(wday[i]);
  551. break;
  552. }
  553. if(cistrncmp(s, wday[i], 3) == 0){
  554. s += 3;
  555. break;
  556. }
  557. }
  558. if(i==nelem(wday)){
  559. if(debug)
  560. fprint(2, "bad wday (%s)\n", os);
  561. return -1;
  562. }
  563. if(*s++ != ',' || *s++ != ' '){
  564. if(debug)
  565. fprint(2, "bad wday separator (%s)\n", os);
  566. return -1;
  567. }
  568. /* 25- */
  569. if(!isdigit(s[0]) || !isdigit(s[1]) || (s[2]!='-' && s[2]!=' ')){
  570. if(debug)
  571. fprint(2, "bad day of month (%s)\n", os);
  572. return -1;
  573. }
  574. tm.mday = strtol(s, 0, 10);
  575. s += 3;
  576. /* Jan- */
  577. for(i=0; i<nelem(mon); i++)
  578. if(cistrncmp(s, mon[i], 3) == 0){
  579. tm.mon = i;
  580. s += 3;
  581. break;
  582. }
  583. if(i==nelem(mon)){
  584. if(debug)
  585. fprint(2, "bad month (%s)\n", os);
  586. return -1;
  587. }
  588. if(s[0] != '-' && s[0] != ' '){
  589. if(debug)
  590. fprint(2, "bad month separator (%s)\n", os);
  591. return -1;
  592. }
  593. s++;
  594. /* 2002 */
  595. if(!isdigit(s[0]) || !isdigit(s[1])){
  596. if(debug)
  597. fprint(2, "bad year (%s)\n", os);
  598. return -1;
  599. }
  600. tm.year = strtol(s, 0, 10);
  601. s += 2;
  602. if(isdigit(s[0]) && isdigit(s[1]))
  603. s += 2;
  604. else{
  605. if(tm.year <= 68)
  606. tm.year += 2000;
  607. else
  608. tm.year += 1900;
  609. }
  610. if(tm.mday==0 || tm.mday > mday[isleap(tm.year)][tm.mon]){
  611. if(debug)
  612. fprint(2, "invalid day of month (%s)\n", os);
  613. return -1;
  614. }
  615. tm.year -= 1900;
  616. if(*s++ != ' '){
  617. if(debug)
  618. fprint(2, "bad year separator (%s)\n", os);
  619. return -1;
  620. }
  621. if(!isdigit(s[0]) || !isdigit(s[1]) || s[2]!=':'
  622. || !isdigit(s[3]) || !isdigit(s[4]) || s[5]!=':'
  623. || !isdigit(s[6]) || !isdigit(s[7]) || s[8]!=' '){
  624. if(debug)
  625. fprint(2, "bad time (%s)\n", os);
  626. return -1;
  627. }
  628. tm.hour = atoi(s);
  629. tm.min = atoi(s+3);
  630. tm.sec = atoi(s+6);
  631. if(tm.hour >= 24 || tm.min >= 60 || tm.sec >= 60){
  632. if(debug)
  633. fprint(2, "invalid time (%s)\n", os);
  634. return -1;
  635. }
  636. s += 9;
  637. if(cistrcmp(s, "GMT") != 0){
  638. if(debug)
  639. fprint(2, "time zone not GMT (%s)\n", os);
  640. return -1;
  641. }
  642. strcpy(tm.zone, "GMT");
  643. tm.yday = 0;
  644. return tm2sec(&tm);
  645. }
  646. /*
  647. * skip linear whitespace. we're a bit more lenient than RFC2616 2.2.
  648. */
  649. char*
  650. skipspace(char *s)
  651. {
  652. while(*s=='\r' || *s=='\n' || *s==' ' || *s=='\t')
  653. s++;
  654. return s;
  655. }
  656. /*
  657. * Try to identify old netscape headers.
  658. * The old headers:
  659. * - didn't allow spaces around the '='
  660. * - used an 'Expires' attribute
  661. * - had no 'Version' attribute
  662. * - had no quotes
  663. * - allowed whitespace in values
  664. * - apparently separated attr/value pairs with ';' exclusively
  665. */
  666. int
  667. isnetscape(char *hdr)
  668. {
  669. char *s;
  670. for(s=hdr; (s=strchr(s, '=')) != nil; s++){
  671. if(isspace(s[1]) || (s > hdr && isspace(s[-1])))
  672. return 0;
  673. if(s[1]=='"')
  674. return 0;
  675. }
  676. if(cistrstr(hdr, "version="))
  677. return 0;
  678. return 1;
  679. }
  680. /*
  681. * Parse HTTP response headers, adding cookies to jar.
  682. * Overwrites the headers. May overwrite path.
  683. */
  684. char* parsecookie(Cookie*, char*, char**, int, char*, char*);
  685. int
  686. parsehttp(Jar *jar, char *hdr, char *dom, char *path)
  687. {
  688. static char setcookie[] = "Set-Cookie:";
  689. char *e, *p, *nextp;
  690. Cookie c;
  691. int isns, n;
  692. isns = isnetscape(hdr);
  693. n = 0;
  694. for(p=hdr; p; p=nextp){
  695. p = skipspace(p);
  696. if(*p == '\0')
  697. break;
  698. nextp = strchr(p, '\n');
  699. if(nextp != nil)
  700. *nextp++ = '\0';
  701. if(debug)
  702. fprint(2, "?%s\n", p);
  703. if(cistrncmp(p, setcookie, strlen(setcookie)) != 0)
  704. continue;
  705. if(debug)
  706. fprint(2, "%s\n", p);
  707. p = skipspace(p+strlen(setcookie));
  708. for(; *p; p=skipspace(p)){
  709. if((e = parsecookie(&c, p, &p, isns, dom, path)) != nil){
  710. if(debug)
  711. fprint(2, "parse cookie: %s\n", e);
  712. break;
  713. }
  714. if((e = isbadcookie(&c, dom, path)) != nil){
  715. if(debug)
  716. fprint(2, "reject cookie; %s\n", e);
  717. continue;
  718. }
  719. addcookie(jar, &c);
  720. n++;
  721. }
  722. }
  723. return n;
  724. }
  725. static char*
  726. skipquoted(char *s)
  727. {
  728. /*
  729. * Sec 2.2 of RFC2616 defines a "quoted-string" as:
  730. *
  731. * quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
  732. * qdtext = <any TEXT except <">>
  733. * quoted-pair = "\" CHAR
  734. *
  735. * TEXT is any octet except CTLs, but including LWS;
  736. * LWS is [CR LF] 1*(SP | HT);
  737. * CHARs are ASCII octets 0-127; (NOTE: we reject 0's)
  738. * CTLs are octets 0-31 and 127;
  739. */
  740. if(*s != '"')
  741. return s;
  742. for(s++; 32 <= *s && *s < 127 && *s != '"'; s++)
  743. if(*s == '\\' && *(s+1) != '\0')
  744. s++;
  745. return s;
  746. }
  747. static char*
  748. skiptoken(char *s)
  749. {
  750. /*
  751. * Sec 2.2 of RFC2616 defines a "token" as
  752. * 1*<any CHAR except CTLs or separators>;
  753. * CHARs are ASCII octets 0-127;
  754. * CTLs are octets 0-31 and 127;
  755. * separators are "()<>@,;:\/[]?={}", double-quote, SP (32), and HT (9)
  756. */
  757. while(32 <= *s && *s < 127 && strchr("()<>@,;:[]?={}\" \t\\", *s)==nil)
  758. s++;
  759. return s;
  760. }
  761. static char*
  762. skipvalue(char *s, int isns)
  763. {
  764. char *t;
  765. /*
  766. * An RFC2109 value is an HTTP token or an HTTP quoted string.
  767. * Netscape servers ignore the spec and rely on semicolons, apparently.
  768. */
  769. if(isns){
  770. if((t = strchr(s, ';')) == nil)
  771. t = s+strlen(s);
  772. return t;
  773. }
  774. if(*s == '"')
  775. return skipquoted(s);
  776. return skiptoken(s);
  777. }
  778. /*
  779. * RMID=80b186bb64c03c65fab767f8; expires=Monday, 10-Feb-2003 04:44:39 GMT;
  780. * path=/; domain=.nytimes.com
  781. */
  782. char*
  783. parsecookie(Cookie *c, char *p, char **e, int isns, char *dom,
  784. char *path)
  785. {
  786. int i, done;
  787. char *t, *u, *attr, *val;
  788. memset(c, 0, sizeof *c);
  789. c->expire = ~0;
  790. /* NAME=VALUE */
  791. t = skiptoken(p);
  792. c->name = p;
  793. p = skipspace(t);
  794. if(*p != '='){
  795. Badname:
  796. return "malformed cookie: no NAME=VALUE";
  797. }
  798. *t = '\0';
  799. p = skipspace(p+1);
  800. t = skipvalue(p, isns);
  801. if(*t)
  802. *t++ = '\0';
  803. c->value = p;
  804. p = skipspace(t);
  805. if(c->name[0]=='\0' || c->value[0]=='\0')
  806. goto Badname;
  807. done = 0;
  808. for(; *p && !done; p=skipspace(p)){
  809. attr = p;
  810. t = skiptoken(p);
  811. u = skipspace(t);
  812. switch(*u){
  813. case '\0':
  814. *t = '\0';
  815. p = val = u;
  816. break;
  817. case ';':
  818. *t = '\0';
  819. val = "";
  820. p = u+1;
  821. break;
  822. case '=':
  823. *t = '\0';
  824. val = skipspace(u+1);
  825. p = skipvalue(val, isns);
  826. if(*p==',')
  827. done = 1;
  828. if(*p)
  829. *p++ = '\0';
  830. break;
  831. case ',':
  832. if(!isns){
  833. val = "";
  834. p = u;
  835. *p++ = '\0';
  836. done = 1;
  837. break;
  838. }
  839. default:
  840. if(debug)
  841. fprint(2, "syntax: %s\n", p);
  842. return "syntax error";
  843. }
  844. for(i=0; i<nelem(stab); i++)
  845. if(stab[i].ishttp && cistrcmp(stab[i].s, attr)==0)
  846. *(char**)((char*)c+stab[i].offset) = val;
  847. if(cistrcmp(attr, "expires") == 0){
  848. if(!isns)
  849. return "non-netscape cookie has Expires tag";
  850. if(!val[0])
  851. return "bad expires tag";
  852. c->expire = strtotime(val);
  853. if(c->expire == ~0)
  854. return "cannot parse netscape expires tag";
  855. }
  856. if(cistrcmp(attr, "max-age") == 0)
  857. c->expire = time(0)+atoi(val);
  858. if(cistrcmp(attr, "secure") == 0)
  859. c->secure = 1;
  860. }
  861. if(c->dom)
  862. c->explicitdom = 1;
  863. else
  864. c->dom = dom;
  865. if(c->path)
  866. c->explicitpath = 1;
  867. else{
  868. c->path = path;
  869. if((t = strchr(c->path, '?')) != 0)
  870. *t = '\0';
  871. if((t = strrchr(c->path, '/')) != 0)
  872. *t = '\0';
  873. }
  874. c->netscapestyle = isns;
  875. *e = p;
  876. return nil;
  877. }
  878. Jar *jar;
  879. enum
  880. {
  881. Xhttp = 1,
  882. Xcookies,
  883. NeedUrl = 0,
  884. HaveUrl,
  885. };
  886. typedef struct Aux Aux;
  887. struct Aux
  888. {
  889. int state;
  890. char *dom;
  891. char *path;
  892. char *inhttp;
  893. char *outhttp;
  894. char *ctext;
  895. int rdoff;
  896. };
  897. enum
  898. {
  899. AuxBuf = 4096,
  900. MaxCtext = 16*1024*1024,
  901. };
  902. void
  903. fsopen(Req *r)
  904. {
  905. char *s, *es;
  906. int i, sz;
  907. Aux *a;
  908. switch((uintptr)r->fid->file->aux){
  909. case Xhttp:
  910. syncjar(jar);
  911. a = emalloc9p(sizeof(Aux));
  912. r->fid->aux = a;
  913. a->inhttp = emalloc9p(AuxBuf);
  914. a->outhttp = emalloc9p(AuxBuf);
  915. break;
  916. case Xcookies:
  917. syncjar(jar);
  918. a = emalloc9p(sizeof(Aux));
  919. r->fid->aux = a;
  920. if(r->ifcall.mode&OTRUNC){
  921. a->ctext = emalloc9p(1);
  922. a->ctext[0] = '\0';
  923. }else{
  924. sz = 256*jar->nc+1024; /* BUG should do better */
  925. a->ctext = emalloc9p(sz);
  926. a->ctext[0] = '\0';
  927. s = a->ctext;
  928. es = s+sz;
  929. for(i=0; i<jar->nc; i++)
  930. s = seprint(s, es, "%K\n", &jar->c[i]);
  931. }
  932. break;
  933. }
  934. respond(r, nil);
  935. }
  936. void
  937. fsread(Req *r)
  938. {
  939. Aux *a;
  940. a = r->fid->aux;
  941. switch((uintptr)r->fid->file->aux){
  942. case Xhttp:
  943. if(a->state == NeedUrl){
  944. respond(r, "must write url before read");
  945. return;
  946. }
  947. r->ifcall.offset = a->rdoff;
  948. readstr(r, a->outhttp);
  949. a->rdoff += r->ofcall.count;
  950. respond(r, nil);
  951. return;
  952. case Xcookies:
  953. readstr(r, a->ctext);
  954. respond(r, nil);
  955. return;
  956. default:
  957. respond(r, "bug in webcookies");
  958. return;
  959. }
  960. }
  961. void
  962. fswrite(Req *r)
  963. {
  964. Aux *a;
  965. int i, sz, hlen, issecure;
  966. char buf[1024], *p;
  967. Jar *j;
  968. a = r->fid->aux;
  969. switch((uintptr)r->fid->file->aux){
  970. case Xhttp:
  971. if(a->state == NeedUrl){
  972. if(r->ifcall.count >= sizeof buf){
  973. respond(r, "url too long");
  974. return;
  975. }
  976. memmove(buf, r->ifcall.data, r->ifcall.count);
  977. buf[r->ifcall.count] = '\0';
  978. issecure = 0;
  979. if(cistrncmp(buf, "http://", 7) == 0)
  980. hlen = 7;
  981. else if(cistrncmp(buf, "https://", 8) == 0){
  982. hlen = 8;
  983. issecure = 1;
  984. }else{
  985. respond(r, "url must begin http:// or https://");
  986. return;
  987. }
  988. if(buf[hlen]=='/'){
  989. respond(r, "url without host name");
  990. return;
  991. }
  992. p = strchr(buf+hlen, '/');
  993. if(p == nil)
  994. a->path = estrdup9p("/");
  995. else{
  996. a->path = estrdup9p(p);
  997. *p = '\0';
  998. }
  999. a->dom = estrdup9p(buf+hlen);
  1000. a->state = HaveUrl;
  1001. j = cookiesearch(jar, a->dom, a->path, issecure);
  1002. if(debug){
  1003. fprint(2, "search %s %s got %p\n", a->dom, a->path, j);
  1004. if(j){
  1005. fprint(2, "%d cookies\n", j->nc);
  1006. for(i=0; i<j->nc; i++)
  1007. fprint(2, "%K\n", &j->c[i]);
  1008. }
  1009. }
  1010. snprint(a->outhttp, AuxBuf, "%J", j);
  1011. if(j)
  1012. closejar(j);
  1013. }else{
  1014. if(strlen(a->inhttp)+r->ifcall.count >= AuxBuf){
  1015. respond(r, "http headers too large");
  1016. return;
  1017. }
  1018. memmove(a->inhttp+strlen(a->inhttp), r->ifcall.data, r->ifcall.count);
  1019. }
  1020. r->ofcall.count = r->ifcall.count;
  1021. respond(r, nil);
  1022. return;
  1023. case Xcookies:
  1024. sz = r->ifcall.count+r->ifcall.offset;
  1025. if(sz > strlen(a->ctext)){
  1026. if(sz >= MaxCtext){
  1027. respond(r, "cookie file too large");
  1028. return;
  1029. }
  1030. a->ctext = erealloc9p(a->ctext, sz+1);
  1031. a->ctext[sz] = '\0';
  1032. }
  1033. memmove(a->ctext+r->ifcall.offset, r->ifcall.data, r->ifcall.count);
  1034. r->ofcall.count = r->ifcall.count;
  1035. respond(r, nil);
  1036. return;
  1037. default:
  1038. respond(r, "bug in webcookies");
  1039. return;
  1040. }
  1041. }
  1042. void
  1043. fsdestroyfid(Fid *fid)
  1044. {
  1045. char *p, *nextp;
  1046. Aux *a;
  1047. int i;
  1048. a = fid->aux;
  1049. if(a == nil)
  1050. return;
  1051. switch((uintptr)fid->file->aux){
  1052. case Xhttp:
  1053. parsehttp(jar, a->inhttp, a->dom, a->path);
  1054. break;
  1055. case Xcookies:
  1056. for(i=0; i<jar->nc; i++)
  1057. jar->c[i].mark = 1;
  1058. for(p=a->ctext; *p; p=nextp){
  1059. if((nextp = strchr(p, '\n')) != nil)
  1060. *nextp++ = '\0';
  1061. else
  1062. nextp = "";
  1063. addtojar(jar, p, 0);
  1064. }
  1065. for(i=0; i<jar->nc; i++)
  1066. if(jar->c[i].mark)
  1067. delcookie(jar, &jar->c[i]);
  1068. break;
  1069. }
  1070. syncjar(jar);
  1071. free(a->dom);
  1072. free(a->path);
  1073. free(a->inhttp);
  1074. free(a->outhttp);
  1075. free(a->ctext);
  1076. free(a);
  1077. }
  1078. void
  1079. fsend(Srv *s)
  1080. {
  1081. closejar(jar);
  1082. }
  1083. Srv fs =
  1084. {
  1085. .open= fsopen,
  1086. .read= fsread,
  1087. .write= fswrite,
  1088. .destroyfid= fsdestroyfid,
  1089. .end= fsend,
  1090. };
  1091. void
  1092. usage(void)
  1093. {
  1094. fprint(2, "usage: webcookies [-f file] [-m mtpt] [-s service]\n");
  1095. exits("usage");
  1096. }
  1097. void
  1098. main(int argc, char **argv)
  1099. {
  1100. char *file, *mtpt, *home, *srv;
  1101. file = nil;
  1102. srv = nil;
  1103. mtpt = "/mnt/webcookies";
  1104. ARGBEGIN{
  1105. case 'D':
  1106. chatty9p++;
  1107. break;
  1108. case 'd':
  1109. debug = 1;
  1110. break;
  1111. case 'f':
  1112. file = EARGF(usage());
  1113. break;
  1114. case 's':
  1115. srv = EARGF(usage());
  1116. break;
  1117. case 'm':
  1118. mtpt = EARGF(usage());
  1119. break;
  1120. default:
  1121. usage();
  1122. }ARGEND
  1123. if(argc != 0)
  1124. usage();
  1125. quotefmtinstall();
  1126. fmtinstall('J', jarfmt);
  1127. fmtinstall('K', cookiefmt);
  1128. if(file == nil){
  1129. home = getenv("home");
  1130. if(home == nil)
  1131. sysfatal("no cookie file specified and no $home");
  1132. file = emalloc9p(strlen(home)+30);
  1133. strcpy(file, home);
  1134. strcat(file, "/lib/webcookies");
  1135. }
  1136. if(access(file, AEXIST) < 0)
  1137. close(create(file, OWRITE, 0666));
  1138. jar = readjar(file);
  1139. if(jar == nil)
  1140. sysfatal("readjar: %r");
  1141. fs.tree = alloctree("cookie", "cookie", DMDIR|0555, nil);
  1142. closefile(createfile(fs.tree->root, "http", "cookie", 0666, (void*)Xhttp));
  1143. closefile(createfile(fs.tree->root, "cookies", "cookie", 0666, (void*)Xcookies));
  1144. postmountsrv(&fs, srv, mtpt, MREPL);
  1145. exits(nil);
  1146. }