📄 cookies.c
字号:
#include <u.h>#include <libc.h>#include <bio.h>#include <ndb.h>#include <fcall.h>#include <thread.h>#include <9p.h>#include <ctype.h>#include "dat.h"#include "fns.h"int cookiedebug;typedef struct Cookie Cookie;typedef struct Jar Jar;struct Cookie{ /* external info */ char* name; char* value; char* dom; /* starts with . */ char* path; char* version; char* comment; /* optional, may be nil */ uint expire; /* time of expiration: ~0 means when webcookies dies */ int secure; int explicitdom; /* dom was explicitly set */ int explicitpath; /* path was explicitly set */ int netscapestyle; /* internal info */ int deleted; int mark; int ondisk;};struct Jar{ Cookie *c; int nc; int mc; Qid qid; int dirty; char *file; char *lockfile;};struct { char *s; int offset; int ishttp;} stab[] = { "domain", offsetof(Cookie, dom), 1, "path", offsetof(Cookie, path), 1, "name", offsetof(Cookie, name), 0, "value", offsetof(Cookie, value), 0, "comment", offsetof(Cookie, comment), 1, "version", offsetof(Cookie, version), 1,};struct { char *s; int offset;} itab[] = { "expire", offsetof(Cookie, expire), "secure", offsetof(Cookie, secure), "explicitdomain", offsetof(Cookie, explicitdom), "explicitpath", offsetof(Cookie, explicitpath), "netscapestyle", offsetof(Cookie, netscapestyle),};#pragma varargck type "J" Jar*#pragma varargck type "K" Cookie*/* HTTP format */static intjarfmt(Fmt *fp){ int i; Jar *jar; jar = va_arg(fp->args, Jar*); if(jar == nil || jar->nc == 0) return 0; fmtstrcpy(fp, "Cookie: "); if(jar->c[0].version) fmtprint(fp, "$Version=%s; ", jar->c[0].version); for(i=0; i<jar->nc; i++) fmtprint(fp, "%s%s=%s", i ? "; ": "", jar->c[i].name, jar->c[i].value); fmtstrcpy(fp, "\r\n"); return 0;}/* individual cookie */static intcookiefmt(Fmt *fp){ int j, k, first; char *t; Cookie *c; c = va_arg(fp->args, Cookie*); first = 1; for(j=0; j<nelem(stab); j++){ t = *(char**)((uintptr)c+stab[j].offset); if(t == nil) continue; if(first) first = 0; else fmtstrcpy(fp, " "); fmtprint(fp, "%s=%q", stab[j].s, t); } for(j=0; j<nelem(itab); j++){ k = *(int*)((uintptr)c+itab[j].offset); if(k == 0) continue; if(first) first = 0; else fmtstrcpy(fp, " "); fmtprint(fp, "%s=%ud", itab[j].s, k); } return 0;}/* * sort cookies: * - alpha by name * - alpha by domain * - longer paths first, then alpha by path (RFC2109 4.3.4) */static intcookiecmp(Cookie *a, Cookie *b){ int i; if((i = strcmp(a->name, b->name)) != 0) return i; if((i = cistrcmp(a->dom, b->dom)) != 0) return i; if((i = strlen(b->path) - strlen(a->path)) != 0) return i; if((i = strcmp(a->path, b->path)) != 0) return i; return 0;}static intexactcookiecmp(Cookie *a, Cookie *b){ int i; if((i = cookiecmp(a, b)) != 0) return i; if((i = strcmp(a->value, b->value)) != 0) return i; if(a->version || b->version){ if(!a->version) return -1; if(!b->version) return 1; if((i = strcmp(a->version, b->version)) != 0) return i; } if(a->comment || b->comment){ if(!a->comment) return -1; if(!b->comment) return 1; if((i = strcmp(a->comment, b->comment)) != 0) return i; } if((i = b->expire - a->expire) != 0) return i; if((i = b->secure - a->secure) != 0) return i; if((i = b->explicitdom - a->explicitdom) != 0) return i; if((i = b->explicitpath - a->explicitpath) != 0) return i; if((i = b->netscapestyle - a->netscapestyle) != 0) return i; return 0;}static voidfreecookie(Cookie *c){ int i; for(i=0; i<nelem(stab); i++) free(*(char**)((uintptr)c+stab[i].offset));}static voidcopycookie(Cookie *c){ int i; char **ps; for(i=0; i<nelem(stab); i++){ ps = (char**)((uintptr)c+stab[i].offset); if(*ps) *ps = estrdup9p(*ps); }}static voiddelcookie(Jar *j, Cookie *c){ int i; j->dirty = 1; i = c - j->c; if(i < 0 || i >= j->nc) abort(); c->deleted = 1;}static voidaddcookie(Jar *j, Cookie *c){ int i; if(!c->name || !c->value || !c->path || !c->dom){ fprint(2, "not adding incomplete cookie\n"); return; } if(cookiedebug) fprint(2, "add %K\n", c); for(i=0; i<j->nc; i++) if(cookiecmp(&j->c[i], c) == 0){ if(cookiedebug) fprint(2, "cookie %K matches %K\n", &j->c[i], c); if(exactcookiecmp(&j->c[i], c) == 0){ if(cookiedebug) fprint(2, "\texactly\n"); j->c[i].mark = 0; return; } delcookie(j, &j->c[i]); } j->dirty = 1; if(j->nc == j->mc){ j->mc += 16; j->c = erealloc9p(j->c, j->mc*sizeof(Cookie)); } j->c[j->nc] = *c; copycookie(&j->c[j->nc]); j->nc++;}static voidpurgejar(Jar *j){ int i; for(i=j->nc-1; i>=0; i--){ if(!j->c[i].deleted) continue; freecookie(&j->c[i]); --j->nc; j->c[i] = j->c[j->nc]; }}static voidaddtojar(Jar *jar, char *line, int ondisk){ Cookie c; int i, j, nf, *pint; char *f[20], *attr, *val, **pstr; memset(&c, 0, sizeof c); c.expire = ~0; c.ondisk = ondisk; nf = tokenize(line, f, nelem(f)); for(i=0; i<nf; i++){ attr = f[i]; if((val = strchr(attr, '=')) != nil) *val++ = '\0'; else val = ""; /* string attributes */ for(j=0; j<nelem(stab); j++){ if(strcmp(stab[j].s, attr) == 0){ pstr = (char**)((uintptr)&c+stab[j].offset); *pstr = val; } } /* integer attributes */ for(j=0; j<nelem(itab); j++){ if(strcmp(itab[j].s, attr) == 0){ pint = (int*)((uintptr)&c+itab[j].offset); if(val[0]=='\0') *pint = 1; else *pint = strtoul(val, 0, 0); } } } if(c.name==nil || c.value==nil || c.dom==nil || c.path==nil){ if(cookiedebug) fprint(2, "ignoring fractional cookie %K\n", &c); return; } addcookie(jar, &c);}static Jar*newjar(void){ Jar *jar; jar = emalloc9p(sizeof(Jar)); return jar;}static intexpirejar(Jar *jar, int exiting){ int i, n; uint now; now = time(0); n = 0; for(i=0; i<jar->nc; i++){ if(jar->c[i].expire < now || (exiting && jar->c[i].expire==~0)){ delcookie(jar, &jar->c[i]); n++; } } return n;}static voiddumpjar(Jar *jar, char *desc){ int i; Biobuf *b; char *s; print("%s\n", desc); print("\tin memory:\n"); for(i=0; i<jar->nc; i++) print("\t%K%s%s%s\n", &jar->c[i], jar->c[i].ondisk ? " ondisk" : "", jar->c[i].deleted ? " deleted" : "", jar->c[i].mark ? " mark" : ""); print("\n\ton disk:\n"); if((b = Bopen(jar->file, OREAD)) == nil){ print("\tno file\n"); }else{ while((s = Brdstr(b, '\n', 1)) != nil){ print("\t%s\n", s); free(s); } Bterm(b); } print("\n");}static intsyncjar(Jar *jar){ int i, fd; char *line; Dir *d; Biobuf *b; Qid q; if(jar->file==nil) return 0; memset(&q, 0, sizeof q); if((d = dirstat(jar->file)) != nil){ q = d->qid; if(d->qid.path != jar->qid.path || d->qid.vers != jar->qid.vers) jar->dirty = 1; free(d); } if(jar->dirty == 0) return 0; fd = -1; for(i=0; i<50; i++){ if((fd = create(jar->lockfile, OWRITE, DMEXCL|0666)) < 0){ sleep(100); continue; } break; } if(fd < 0){ if(cookiedebug) fprint(2, "open %s: %r", jar->lockfile); werrstr("cannot acquire jar lock: %r"); return -1; } for(i=0; i<jar->nc; i++) /* mark is cleared by addcookie */ jar->c[i].mark = jar->c[i].ondisk; if((b = Bopen(jar->file, OREAD)) == nil){ if(cookiedebug) fprint(2, "Bopen %s: %r", jar->file); werrstr("cannot read cookie file %s: %r", jar->file); close(fd); return -1; } for(; (line = Brdstr(b, '\n', 1)) != nil; free(line)){ if(*line == '#') continue; addtojar(jar, line, 1); } Bterm(b); for(i=0; i<jar->nc; i++) if(jar->c[i].mark && jar->c[i].expire != ~0) delcookie(jar, &jar->c[i]); purgejar(jar); b = Bopen(jar->file, OWRITE); if(b == nil){ if(cookiedebug) fprint(2, "Bopen write %s: %r", jar->file); close(fd); return -1; } Bprint(b, "# webcookies cookie jar\n"); Bprint(b, "# comments and non-standard fields will be lost\n"); for(i=0; i<jar->nc; i++){ if(jar->c[i].expire == ~0) continue; Bprint(b, "%K\n", &jar->c[i]); jar->c[i].ondisk = 1; } Bterm(b); jar->dirty = 0; close(fd); if((d = dirstat(jar->file)) != nil){ jar->qid = d->qid; free(d); } return 0;}static Jar*readjar(char *file){ char *lock, *p; Jar *jar; jar = newjar(); lock = emalloc9p(strlen(file)+10); strcpy(lock, file); if((p = strrchr(lock, '/')) != nil) p++; else p = lock; memmove(p+2, p, strlen(p)+1); p[0] = 'L'; p[1] = '.'; jar->lockfile = lock; jar->file = file; jar->dirty = 1; if(syncjar(jar) < 0){ free(jar->file); free(jar->lockfile); free(jar); return nil; } return jar;}static voidclosejar(Jar *jar){ int i; if(jar == nil) return; expirejar(jar, 0); if(syncjar(jar) < 0) fprint(2, "warning: cannot rewrite cookie jar: %r\n"); for(i=0; i<jar->nc; i++) freecookie(&jar->c[i]); free(jar->file); free(jar); }/* * Domain name matching is per RFC2109, section 2: * * Hosts names can be specified either as an IP address or a FQHN * string. Sometimes we compare one host name with another. Host A's * name domain-matches host B's if * * * both host names are IP addresses and their host name strings match * exactly; or * * * both host names are FQDN strings and their host name strings match * exactly; or * * * A is a FQDN string and has the form NB, where N is a non-empty name * string, B has the form .B', and B' is a FQDN string. (So, x.y.com * domain-matches .y.com but not y.com.) * * Note that domain-match is not a commutative operation: a.b.c.com * domain-matches .c.com, but not the reverse. * * (This does not verify that IP addresses and FQDN's are well-formed.) */static intisdomainmatch(char *name, char *pattern){ int lname, lpattern; if(cistrcmp(name, pattern)==0) return 1; if(strcmp(ipattr(name), "dom")==0 && pattern[0]=='.'){ lname = strlen(name); lpattern = strlen(pattern); if(lname >= lpattern && cistrcmp(name+lname-lpattern, pattern)==0) return 1; } return 0;}/* * RFC2109 4.3.4: * - domain must match * - path in cookie must be a prefix of request path * - cookie must not have expired */static intiscookiematch(Cookie *c, char *dom, char *path, uint now){ return isdomainmatch(dom, c->dom) && strncmp(c->path, path, strlen(c->path))==0 && c->expire >= now;}/* * Produce a subjar of matching cookies. * Secure cookies are only included if secure is set. */static Jar*cookiesearch(Jar *jar, char *dom, char *path, int issecure){ int i; Jar *j; uint now; if(cookiedebug) fprint(2, "cookiesearch %s %s %d\n", dom, path, issecure); now = time(0); j = newjar(); for(i=0; i<jar->nc; i++){ if(cookiedebug) fprint(2, "\ttry %s %s %d %s\n", jar->c[i].dom, jar->c[i].path, jar->c[i].secure, jar->c[i].name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -