⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webcookies.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Cookie file system.  Allows hget and multiple webfs's to collaborate. * Conventionally mounted on /mnt/webcookies. */#include <u.h>#include <libc.h>#include <bio.h>#include <ndb.h>#include <fcall.h>#include <thread.h>#include <9p.h>#include <ctype.h>int debug = 0;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 */intjarfmt(Fmt *fmt){	int i;	Jar *jar;	jar = va_arg(fmt->args, Jar*);	if(jar == nil || jar->nc == 0)		return fmtstrcpy(fmt, "");	fmtprint(fmt, "Cookie: ");	if(jar->c[0].version)		fmtprint(fmt, "$Version=%s; ", jar->c[0].version);	for(i=0; i<jar->nc; i++)		fmtprint(fmt, "%s%s=%s", i ? "; ":"", jar->c[i].name, jar->c[i].value);	fmtprint(fmt, "\r\n");	return 0;}/* individual cookie */intcookiefmt(Fmt *fmt){	int j, k, first;	char *t;	Cookie *c;	c = va_arg(fmt->args, Cookie*);	first = 1;	for(j=0; j<nelem(stab); j++){		t = *(char**)((char*)c+stab[j].offset);		if(t == nil)			continue;		if(first)			first = 0;		else			fmtprint(fmt, " ");		fmtprint(fmt, "%s=%q", stab[j].s, t);	}	for(j=0; j<nelem(itab); j++){		k = *(int*)((char*)c+itab[j].offset);		if(k == 0)			continue;		if(first)			first = 0;		else			fmtprint(fmt, " ");		fmtprint(fmt, "%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) */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;}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;}voidfreecookie(Cookie *c){	int i;	for(i=0; i<nelem(stab); i++)		free(*(char**)((char*)c+stab[i].offset));}voidcopycookie(Cookie *c){	int i;	char **ps;	for(i=0; i<nelem(stab); i++){		ps = (char**)((char*)c+stab[i].offset);		if(*ps)			*ps = estrdup9p(*ps);	}}voiddelcookie(Jar *j, Cookie *c){	int i;	j->dirty = 1;	i = c - j->c;	if(i < 0 || i >= j->nc)		abort();	c->deleted = 1;}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(debug)		fprint(2, "add %K\n", c);	for(i=0; i<j->nc; i++)		if(cookiecmp(&j->c[i], c) == 0){			if(debug)				fprint(2, "cookie %K matches %K\n", &j->c[i], c);			if(exactcookiecmp(&j->c[i], c) == 0){				if(debug)					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++;}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];	}}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**)((char*)&c+stab[j].offset);				*pstr = val;			}		}		/* integer attributes */		for(j=0; j<nelem(itab); j++){			if(strcmp(itab[j].s, attr) == 0){				pint = (int*)((char*)&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(debug)			fprint(2, "ignoring fractional cookie %K\n", &c);		return;	}	addcookie(jar, &c);}Jar*newjar(void){	Jar *jar;	jar = emalloc9p(sizeof(Jar));	return jar;}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;}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(debug)			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(debug)			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)			delcookie(jar, &jar->c[i]);	purgejar(jar);	b = Bopen(jar->file, OWRITE);	if(b == nil){		if(debug)			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;}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;}voidclosejar(Jar *jar){	int i;	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.) */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 */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. */Jar*cookiesearch(Jar *jar, char *dom, char *path, int issecure){	int i;	Jar *j;	uint now;	now = time(0);	j = newjar();	for(i=0; i<jar->nc; i++)		if((issecure || !jar->c[i].secure) && iscookiematch(&jar->c[i], dom, path, now))			addcookie(j, &jar->c[i]);	if(j->nc == 0){		closejar(j);		werrstr("no cookies found");		return nil;	}	qsort(j->c, j->nc, sizeof(j->c[0]), (int(*)(const void*, const void*))cookiecmp);	return j;}/* * RFC2109 4.3.2 security checks */char*isbadcookie(Cookie *c, char *dom, char *path){	if(strncmp(c->path, path, strlen(c->path)) != 0)		return "cookie path is not a prefix of the request path";	if(c->explicitdom && c->dom[0] != '.')		return "cookie domain doesn't start with dot";	if(memchr(c->dom+1, '.', strlen(c->dom)-1-1) == nil)		return "cookie domain doesn't have embedded dots";	if(!isdomainmatch(dom, c->dom))		return "request host does not match cookie domain";	if(strcmp(ipattr(dom), "dom")==0	&& memchr(dom, '.', strlen(dom)-strlen(c->dom)) != nil)		return "request host contains dots before cookie domain";	return 0;}/* * Sunday, 25-Jan-2002 12:24:36 GMT * Sunday, 25 Jan 2002 12:24:36 GMT * Sun, 25 Jan 02 12:24:36 GMT */intisleap(int year){	return year%4==0 && (year%100!=0 || year%400==0);}uintstrtotime(char *s){	char *os;	int i;	Tm tm;	static int mday[2][12] = {		31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,		31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,	};	static char *wday[] = {		"Sunday", "Monday", "Tuesday", "Wednesday",		"Thursday", "Friday", "Saturday",	};	static char *mon[] = {		"Jan", "Feb", "Mar", "Apr", "May", "Jun",		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",	};	os = s;	/* Sunday, */	for(i=0; i<nelem(wday); i++){		if(cistrncmp(s, wday[i], strlen(wday[i])) == 0){			s += strlen(wday[i]);			break;		}		if(cistrncmp(s, wday[i], 3) == 0){			s += 3;			break;		}	}	if(i==nelem(wday)){		if(debug)			fprint(2, "bad wday (%s)\n", os);		return -1;	}	if(*s++ != ',' || *s++ != ' '){		if(debug)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -