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

📄 cookies.c

📁 一个很有名的浏览器
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Internal cookies implementation *//* $Id: cookies.c,v 1.192.2.4 2005/05/01 22:03:23 jonas Exp $ */#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h> /* OS/2 needs this after sys/types.h */#ifdef HAVE_TIME_H#include <time.h>#endif#include "elinks.h"#if 0#define DEBUG_COOKIES#endif#include "bfu/dialog.h"#include "cookies/cookies.h"#include "cookies/dialogs.h"#include "cookies/parser.h"#include "config/kbdbind.h"#include "config/options.h"#include "intl/gettext/libintl.h"#include "lowlevel/home.h"#include "modules/module.h"#include "protocol/date.h"#include "protocol/header.h"#include "protocol/protocol.h"#include "protocol/uri.h"#include "sched/session.h"#include "terminal/terminal.h"#include "util/conv.h"#ifdef DEBUG_COOKIES#include "util/error.h"#endif#include "util/file.h"#include "util/memory.h"#include "util/object.h"#include "util/secsave.h"#include "util/string.h"#include "util/ttime.h"#define COOKIES_FILENAME		"cookies"static int cookies_nosave = 0;static INIT_LIST_HEAD(cookies);struct c_domain {	LIST_HEAD(struct c_domain);	unsigned char domain[1]; /* Must be at end of struct. */};static INIT_LIST_HEAD(c_domains);static INIT_LIST_HEAD(cookie_servers);static int cookies_dirty = 0;enum cookies_option {	COOKIES_TREE,	COOKIES_ACCEPT_POLICY,	COOKIES_MAX_AGE,	COOKIES_PARANOID_SECURITY,	COOKIES_SAVE,	COOKIES_RESAVE,	COOKIES_OPTIONS,};static struct option_info cookies_options[] = {	INIT_OPT_TREE("", N_("Cookies"),		"cookies", 0,		N_("Cookies options.")),	INIT_OPT_INT("cookies", N_("Accept policy"),		"accept_policy", 0,		COOKIES_ACCEPT_NONE, COOKIES_ACCEPT_ALL, COOKIES_ACCEPT_ALL,		N_("Cookies accepting policy:\n"		"0 is accept no cookies\n"		"1 is ask for confirmation before accepting cookie\n"		"2 is accept all cookies")),	INIT_OPT_INT("cookies", N_("Maximum age"),		"max_age", 0, -1, 10000, -1,		N_("Cookie maximum age (in days):\n"		"-1 is use cookie's expiration date if any\n"		"0  is force expiration at the end of session, ignoring cookie's\n"		"   expiration date\n"		"1+ is use cookie's expiration date, but limit age to the given\n"		"   number of days")),	INIT_OPT_BOOL("cookies", N_("Paranoid security"),		"paranoid_security", 0, 0,		N_("When enabled, we'll require three dots in cookies domain for all\n"		"non-international domains (instead of just two dots). Some countries\n"		"have generic second level domains (eg. .com.pl, .co.uk) and allowing\n"		"sites to set cookies for these generic domains could potentially be\n"		"very bad. Note, it is off by default as it breaks a lot of sites.")),	INIT_OPT_BOOL("cookies", N_("Saving"),		"save", 0, 1,		N_("Whether cookies should be loaded from and save to disk.")),	INIT_OPT_BOOL("cookies", N_("Resaving"),		"resave", 0, 1,		N_("Save cookies after each change in cookies list? No effect when\n"		"cookie saving (cookies.save) is off.")),	NULL_OPTION_INFO,};#define get_opt_cookies(which)		cookies_options[(which)].option.value#define get_cookies_accept_policy()	get_opt_cookies(COOKIES_ACCEPT_POLICY).number#define get_cookies_max_age()		get_opt_cookies(COOKIES_MAX_AGE).number#define get_cookies_paranoid_security()	get_opt_cookies(COOKIES_PARANOID_SECURITY).number#define get_cookies_save()		get_opt_cookies(COOKIES_SAVE).number#define get_cookies_resave()		get_opt_cookies(COOKIES_RESAVE).numberstatic struct cookie_server *get_cookie_server(unsigned char *host, int hostlen){	struct cookie_server *sort_spot = NULL;	struct cookie_server *cs;	foreach (cs, cookie_servers) {		/* XXX: We must count with cases like "x.co" vs "x.co.uk"		 * below! */		int cslen = strlen(cs->host);		int cmp = strncasecmp(cs->host, host, hostlen);		if (!sort_spot && (cmp > 0 || (cmp == 0 && cslen > hostlen))) {			/* This is the first @cs with name greater than @host,			 * our dream sort spot! */			sort_spot = cs->prev;		}		if (cmp || cslen != hostlen)			continue;		object_lock(cs);		return cs;	}	cs = mem_calloc(1, sizeof(*cs) + hostlen);	if (!cs) return NULL;	memcpy(cs->host, host, hostlen);	object_nolock(cs, "cookie_server");	cs->box_item = add_listbox_folder(&cookie_browser, NULL, cs);	object_lock(cs);	if (!sort_spot) {		/* No sort spot found, therefore this sorts at the end. */		add_to_list_end(cookie_servers, cs);		del_from_list(cs->box_item);		add_to_list_end(cookie_browser.root.child, cs->box_item);	} else {		/* Sort spot found, sort after it. */		add_at_pos(sort_spot, cs);		if (sort_spot != (struct cookie_server *) &cookie_servers) {			del_from_list(cs->box_item);			add_at_pos(sort_spot->box_item, cs->box_item);		} /* else we are already at the top anyway. */	}	return cs;}static voiddone_cookie_server(struct cookie_server *cs){	object_unlock(cs);	if (is_object_used(cs)) return;	if (cs->box_item) done_listbox_item(&cookie_browser, cs->box_item);	del_from_list(cs);	mem_free(cs);}voidfree_cookie(struct cookie *c){	if (c->box_item) done_listbox_item(&cookie_browser, c->box_item);	if (c->server) done_cookie_server(c->server);	mem_free_if(c->name);	mem_free_if(c->value);	mem_free_if(c->path);	mem_free_if(c->domain);	mem_free(c);}/* Check whether cookie's domain matches server. * It returns 1 if ok, 0 else. */static intis_domain_security_ok(unsigned char *domain, unsigned char *server, int server_len){	int i;	int domain_len;	int need_dots;	if (domain[0] == '.') domain++;	domain_len = strlen(domain);	/* Match domain and server.. */	/* XXX: Hmm, can't we use strlcasecmp() here? --pasky */	if (domain_len > server_len) return 0;	/* Ensure that the domain is atleast a substring of the server before	 * continuing. */	if (strncasecmp(domain, server + server_len - domain_len, domain_len))		return 0;	/* Allow domains which are same as servers. --<rono@sentuny.com.au> */	/* Mozilla does it as well ;))) and I can't figure out any security	 * risk. --pasky */	if (server_len == domain_len)		return 1;	/* Check whether the server is an IP address, and require an exact host	 * match for the cookie, so any chance of IP address funkiness is	 * eliminated (e.g. the alias 127.1 domain-matching 99.54.127.1). Idea	 * from mozilla. (bug 562) */	if (is_ip_address(server, server_len))		return 0;	/* Also test if domain is secure en ugh.. */	need_dots = 1;	if (get_cookies_paranoid_security()) {		/* This is somehow controversial attempt (by the way violating		 * RFC) to increase cookies security in national domains, done		 * by Mikulas. As it breaks a lot of sites, I decided to make		 * this optional and off by default. I also don't think this		 * improves security considerably, as it's SITE'S fault and		 * also no other browser probably does it. --pasky */		/* Mikulas' comment: Some countries have generic 2-nd level		 * domains (like .com.pl, .co.uk ...) and it would be very bad		 * if someone set cookies for these generic domains.  Imagine		 * for example that server http://brutalporn.com.pl sets cookie		 * Set-Cookie: user_is=perverse_pig; domain=.com.pl -- then		 * this cookie would be sent to all commercial servers in		 * Poland. */		need_dots = 2;		if (domain_len > 0) {			int pos = end_with_known_tld(domain, domain_len);			if (pos >= 1 && domain[pos - 1] == '.')				need_dots = 1;		}	}	for (i = 0; domain[i]; i++)		if (domain[i] == '.' && !--need_dots)			break;	if (need_dots > 0) return 0;	return 1;}voidset_cookie(struct uri *uri, unsigned char *str){	unsigned char *secure, *path;	struct cookie *cookie;	struct cookie_str cstr;	int max_age;	if (get_cookies_accept_policy() == COOKIES_ACCEPT_NONE)		return;#ifdef DEBUG_COOKIES	DBG("set_cookie -> (%s) %s", struri(uri), str);#endif	if (!parse_cookie_str(&cstr, str)) return;	cookie = mem_calloc(1, sizeof(*cookie));	if (!cookie) return;	object_nolock(cookie, "cookie"); /* Debugging purpose. */	/* Fill main fields */	cookie->name = memacpy(str, cstr.nam_end - str);	cookie->value = memacpy(cstr.val_start, cstr.val_end - cstr.val_start);	cookie->server = get_cookie_server(uri->host, uri->hostlen);	cookie->domain = parse_header_param(str, "domain");	if (!cookie->domain) cookie->domain = memacpy(uri->host, uri->hostlen);	/* Now check that all is well */	if (!cookie->domain	    || !cookie->name	    || !cookie->value	    || !cookie->server) {		free_cookie(cookie);		return;	}#if 0	/* We don't actually set ->accept at the moment. But I have kept it	 * since it will maybe help to fix bug 77 - Support for more	 * finegrained control upon accepting of cookies. */	if (!cookie->server->accept) {#ifdef DEBUG_COOKIES		DBG("Dropped.");#endif		free_cookie(cookie);		return;	}#endif	/* Set cookie expiration if needed.	 * Cookie expires at end of session by default,	 * set to 0 by calloc().	 *	 * max_age:	 * -1 is use cookie's expiration date if any	 * 0  is force expiration at the end of session,	 *    ignoring cookie's expiration date	 * 1+ is use cookie's expiration date,	 *    but limit age to the given number of days.	 */	max_age = get_cookies_max_age();	if (max_age) {		unsigned char *date = parse_header_param(str, "expires");		if (date) {			ttime expires = parse_date(&date, NULL, 0, 1); /* Convert date to seconds. */			mem_free(date);			if (expires) {				if (max_age > 0) {					int seconds = max_age*24*3600;					ttime deadline = time(NULL) + seconds;					if (expires > deadline) /* Over-aged cookie ? */						expires = deadline;				}				cookie->expires = expires;			}		}	}	path = parse_header_param(str, "path");	if (!path) {		unsigned char *path_end;		path = get_uri_string(uri, URI_PATH);		if (!path) {			free_cookie(cookie);			return;		}		for (path_end = path + strlen(path) - 1;		     path_end >= path; path_end--) {			if (*path_end == '/') {				path_end[1] = '\0';				break;			}		}	} else {		if (!path[0]		    || path[strlen(path) - 1] != '/')			add_to_strn(&path, "/");		if (path[0] != '/') {			add_to_strn(&path, "x");			memmove(path + 1, path, strlen(path) - 1);			path[0] = '/';		}	}	cookie->path = path;	if (cookie->domain[0] == '.')		memmove(cookie->domain, cookie->domain + 1,			strlen(cookie->domain));	/* cookie->secure is set to 0 by default by calloc(). */	secure = parse_header_param(str, "secure");	if (secure) {		cookie->secure = 1;		mem_free(secure);	}#ifdef DEBUG_COOKIES	{		DBG("Got cookie %s = %s from %s, domain %s, "		      "expires at %d, secure %d", cookie->name,		      cookie->value, cookie->server->host, cookie->domain,		      cookie->expires, cookie->secure);	}#endif	if (!is_domain_security_ok(cookie->domain, uri->host, uri->hostlen)) {#ifdef DEBUG_COOKIES		DBG("Domain security violated: %s vs %.*s", cookie->domain,

⌨️ 快捷键说明

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