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