~ubuntu-branches/ubuntu/feisty/edbrowse/feisty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* cookies.c
 * Cookies
 * (c) 2002 Mikulas Patocka
 * This file is part of the Links project, released under GPL
 *
 * Modified by Karl Dahlke for integration with edbrowse.
 */

#include "eb.h"

struct cookie {
    struct cookie *next;
    struct cookie *prev;
/* These are allocated */
    char *name, *value;
    char *server, *path, *domain;
    time_t expires;		/* zero means undefined */
    bool secure;
};

static void
freeCookie(struct cookie *c)
{
    nzFree(c->name);
    nzFree(c->value);
    nzFree(c->server);
    nzFree(c->path);
    nzFree(c->domain);
}				/* freeCookie */

static struct listHead cookies = { &cookies, &cookies };

static bool displacedCookie;
static void
acceptCookie(struct cookie *c)
{
    struct cookie *d;
    displacedCookie = false;
    foreach(d, cookies) {
	if(stringEqualCI(d->name, c->name) &&
	   stringEqualCI(d->domain, c->domain)) {
	    displacedCookie = true;
	    delFromList(d);
	    freeCookie(d);
	    nzFree(d);
	    break;
	}
    }
    addToListBack(&cookies, c);
}				/* acceptCookie */

static void
cookieIntoJar(const struct cookie *c)
{
    FILE *f;
    if(!cookieFile) {
	static bool warn = false;
	if(warn)
	    return;
	puts("your config file does not specify a cooky jar");
	warn = true;
	return;
    }
    if(c->expires <= time(0))
	return;			/* not persistent, or out of date */
    f = fopen(cookieFile, "a");
    if(!f)
	return;
/* Netscape format */
/* I have no clue what the second argument is suppose to be, */
/* I'm always calling it false. */
    fprintf(f, "%s\tFALSE\t%s\t%s\t%u\t%s\t%s\n",
       c->domain, c->path,
       c->secure ? "TRUE" : "FALSE", (unsigned)c->expires, c->name, c->value);
    fclose(f);
    debugPrint(3, "into jar");
}				/* cookieIntoJar */

/* Should this server really specify this domain in a cookie? */
/* Domain must be the trailing substring of server. */
bool
domainSecurityCheck(const char *server, const char *domain)
{
    int i, dl, nd;
    dl = strlen(domain);
/* x.com or x.y.z */
    if(dl < 5)
	return false;
    if(dl > strlen(server))
	return false;
    i = strlen(server) - dl;
    if(!stringEqualCI(server + i, domain))
	return false;
    if(i && server[i - 1] != '.')
	return false;
    nd = 2;			/* number of dots */
    if(dl > 4 && domain[dl - 4] == '.') {
	static const char *const tld[] = {
	    "com", "edu", "net", "org", "gov", "mil", "int", "biz", NULL
	};
	if(stringInListCI(tld, domain + dl - 3) >= 0)
	    nd = 1;
    }
    for(i = 0; domain[i]; i++)
	if(domain[i] == '.')
	    if(!--nd)
		return true;
    return false;
}				/* domainSecurityCheck */

/* Let's jump right into it - parse a cookie, as received from a website. */
bool
receiveCookie(const char *url, const char *str)
{
    struct cookie *c;
    const char *p, *q, *server;
    char *date, *s;

    debugPrint(3, "%s", str);

    server = getHostURL(url);
    if(server == 0 || !*server)
	return false;

/* Cookie starts with name=value.  If we can't get that, go home. */
    for(p = str; *p != ';' && *p; p++) ;
    for(q = str; *q != '='; q++)
	if(!*q || q >= p)
	    return false;
    if(str == q)
	return false;

    c = allocZeroMem(sizeof (struct cookie));
    c->name = pullString1(str, q);
    ++q;
    if(p - q > 1)
	c->value = pullString1(q, p);
    else
	c->value = EMPTYSTRING;

    c->server = cloneString(server);

    if(date = extractHeaderParam(str, "expires")) {
	c->expires = parseHeaderDate(date);
	nzFree(date);
    } else if(date = extractHeaderParam(str, "max-age")) {
	int n = stringIsNum(date);
	if(n >= 0) {
	    time_t now = time(0);
	    c->expires = now + n;
	}
	nzFree(date);
    }

    c->path = extractHeaderParam(str, "path");
    if(!c->path) {
/* The url indicates the path for this cookie, if a path is not explicitly given */
	const char *dir, *dirend;
	getDirURL(url, &dir, &dirend);
	c->path = pullString1(dir, dirend);
    } else {
	if(!c->path[0] || c->path[strlen(c->path) - 1] != '/')
	    c->path = appendString(c->path, "/");
	if(c->path[0] != '/')
	    c->path = prependString(c->path, "/");
    }

    if(!(c->domain = extractHeaderParam(str, "domain")))
	c->domain = cloneString(server);
    if(c->domain[0] == '.')
	strcpy(c->domain, c->domain + 1);
    if(!domainSecurityCheck(server, c->domain)) {
	nzFree(c->domain);
	c->domain = cloneString(server);
    }

    if(s = extractHeaderParam(str, "secure")) {
	c->secure = true;
	nzFree(s);
    }

    acceptCookie(c);
    cookieIntoJar(c);
    return true;
}				/* receiveCookie */

/* I'm assuming I can read the cookie file, process it,
 * and if necessary, write it out again, with the expired cookies deleted,
 * all before another edbrowse process interferes.
 * I've given it some thought, and I think I can ignore the race conditions. */
void
cookiesFromJar(void)
{
    char *cbuf, *s, *t;
    FILE *f;
    int n, cnt, expired, displaced;
    time_t now;
    struct cookie *c;

    if(!cookieFile)
	return;
    if(!fileIntoMemory(cookieFile, &cbuf, &n))
	showErrorAbort();
    cbuf[n] = 0;
    time(&now);

    cnt = expired = displaced = 0;
    s = cbuf;
    while(*s) {
	++cnt;
	c = allocZeroMem(sizeof (struct cookie));
	t = strchr(s, '\t');
	*t = 0;
	c->domain = cloneString(s);
	s = t + 1;
	t = strchr(s, '\t');
	s = t + 1;
	t = strchr(s, '\t');
	*t = 0;
	c->path = cloneString(s);
	s = t + 1;
	t = strchr(s, '\t');
	c->secure = (*s == 'T');
	s = t + 1;
	t = strchr(s, '\t');
	*t = 0;
	c->expires = (time_t) atol(s);
	s = t + 1;
	t = strchr(s, '\t');
	*t = 0;
	c->name = cloneString(s);
	s = t + 1;
	t = strchr(s, '\n');
	*t = 0;
	c->value = cloneString(s);
	s = t + 1;

	if(c->expires < now) {
	    freeCookie(c);
	    nzFree(c);
	    ++expired;
	} else {
	    acceptCookie(c);
	    displaced += displacedCookie;
	}
    }

    debugPrint(3, "%d persistent cookies, %d expired, %d displaced",
       cnt, expired, displaced);
    nzFree(cbuf);
    if(!(expired + displaced))
	return;

/* Pour the cookies back into the jar */
    f = fopen(cookieFile, "w");
    if(!f)
	errorPrint("1cannot rebuild your cookie jar %s", cookieFile);
    foreach(c, cookies)
       fprintf(f, "%s\tFALSE\t%s\t%s\t%u\t%s\t%s\n",
       c->domain, c->path,
       c->secure ? "TRUE" : "FALSE", (unsigned)c->expires, c->name, c->value);
    fclose(f);
}				/* cookiesFromJar */

static bool
isInDomain(const char *d, const char *s)
{
    int dl = strlen(d);
    int sl = strlen(s);
    int j = sl - dl;
    if(j < 0)
	return false;
    if(!memEqualCI(d, s + j, dl))
	return false;
    if(j && s[j - 1] != '.')
	return false;
    return true;
}				/* isInDomain */

static bool
isPathPrefix(const char *d, const char *s)
{
    int dl = strlen(d);
    int sl = strlen(s);
    if(dl > sl)
	return false;
    return !memcmp(d, s, dl);
}				/* isPathPrefix */

void
sendCookies(char **s, int *l, const char *url, bool issecure)
{
    const char *server = getHostURL(url);
    const char *data = getDataURL(url);
    int nc = 0;			/* new cookie */
    struct cookie *c, *d;
    time_t now;

    if(!url || !server || !data)
	return;

    if(data > url && data[-1] == '/')
	data--;
    if(!*data)
	data = "/";
    time(&now);

    foreach(c, cookies) {
	if(!isInDomain(c->domain, server))
	    continue;
	if(!isPathPrefix(c->path, data))
	    continue;
	if(c->expires && c->expires < now) {
	    d = c;
	    c = c->prev;
	    delFromList(d);
	    freeCookie(d);
	    nzFree(d);
	    continue;
	}
	if(c->secure && !issecure)
	    continue;
/* We're good to go. */
	if(!nc)
	    stringAndString(s, l, "Cookie: "), nc = 1;
	else
	    stringAndString(s, l, "; ");
	stringAndString(s, l, c->name);
	stringAndChar(s, l, '=');
	stringAndString(s, l, c->value);
	debugPrint(3, "send cookie %s=%s", c->name, c->value);
    }

    if(nc)
	stringAndString(s, l, eol);
}				/* sendCookies */