annotate dpi/cookies.c @ 1499:1803fe44871e

cookies follow draft spec's simple parsing
author corvid <corvid@lavabit.com>
date Sat, 09 Jan 2010 20:15:18 +0000
parents c04b0de69b00
children 335f1b3692c9
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 * File: cookies.c
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3 * Cookies server.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
4 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 * Copyright 2001 Lars Clausen <lrclause@cs.uiuc.edu>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 * Jörgen Viksell <jorgen.viksell@telia.com>
35
d9e7b35430de Updated copyright lines
jcid
parents: 0
diff changeset
7 * Copyright 2002-2007 Jorge Arellano Cid <jcid@dillo.org>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9 * This program is free software; you can redistribute it and/or modify
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10 * it under the terms of the GNU General Public License as published by
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11 * the Free Software Foundation; either version 3 of the License, or
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12 * (at your option) any later version.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
16 /* This is written to follow the HTTP State Working Group's
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
17 * draft-ietf-httpstate-cookie-01.txt.
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
18 *
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
19 * We depart from the draft spec's domain format in that, rather than
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
20 * using a host-only flag, we continue to use the .domain notation
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
21 * internally to indicate cookies that may also be returned to subdomains.
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
22 *
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
23 * Info on cookies in the wild:
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
24 * http://www.ietf.org/mail-archive/web/http-state/current/msg00078.html
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
25 * And dates specifically:
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
26 * http://www.ietf.org/mail-archive/web/http-state/current/msg00128.html
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29 #ifdef DISABLE_COOKIES
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31 int main(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33 return 0; /* never called */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
36 #else
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39 #include <sys/types.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 #include <sys/socket.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 #include <sys/stat.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 #include <sys/un.h>
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
43 #include <netinet/in.h>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 #include <fcntl.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 #include <unistd.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 #include <errno.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 #include <stddef.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 #include <stdlib.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 #include <stdio.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 #include <time.h> /* for time() and time_t */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 #include <ctype.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53 #include <netdb.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 #include <signal.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 #include "dpiutil.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 #include "../dpip/dpip.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60 * Debugging macros
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 #define _MSG(...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63 #define MSG(...) printf("[cookies dpi]: " __VA_ARGS__)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67 * a_List_add()
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69 * Make sure there's space for 'num_items' items within the list
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 * (First, allocate an 'alloc_step' sized chunk, after that, double the
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 * list size --to make it faster)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 #define a_List_add(list,num_items,alloc_step) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 if (!list) { \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 list = dMalloc(alloc_step * sizeof((*list))); \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 } \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 if (num_items >= alloc_step){ \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 while ( num_items >= alloc_step ) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 alloc_step <<= 1; \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 list = dRealloc(list, alloc_step * sizeof((*list))); \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 /* The maximum length of a line in the cookie file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 #define LINE_MAXLEN 4096
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 typedef enum {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 COOKIE_ACCEPT,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 COOKIE_ACCEPT_SESSION,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 COOKIE_DENY
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 } CookieControlAction;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 typedef struct {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 char *domain;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 CookieControlAction action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 } CookieControl;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 typedef struct {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 char *domain;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 Dlist *dlist;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 } CookieNode;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 typedef struct {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 char *name;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 char *value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 char *domain;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 char *path;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 time_t expires_at;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 bool_t secure;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 bool_t session_only;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 } CookieData_t;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
112 typedef struct {
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
113 Dsh *sh;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
114 int status;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
115 } ClientInfo;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
116
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 * Local data
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 /* List of CookieNode. Each node holds a domain and its list of cookies */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 static Dlist *cookies;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 /* Variables for access control */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 static CookieControl *ccontrol = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 static int num_ccontrol = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 static int num_ccontrol_max = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 static CookieControlAction default_action = COOKIE_DENY;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 static bool_t disabled;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 static FILE *file_stream;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 static char *cookies_txt_header_str =
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 "# HTTP Cookie File\n"
235
480d368c4b2f - Updated some old URLs.
jcid
parents: 219
diff changeset
134 "# http://wp.netscape.com/newsref/std/cookie_spec.html\n"
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135 "# This is a generated file! Do not edit.\n\n";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 * Forward declarations
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142 static CookieControlAction Cookies_control_check_domain(const char *domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 static int Cookie_control_init(void);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 static void Cookies_add_cookie(CookieData_t *cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 static void Cookies_remove_cookie(CookieData_t *cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 static int Cookies_cmp(const void *a, const void *b);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 * Compare function for searching a cookie node
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
151 static int Cookie_node_cmp(const void *v1, const void *v2)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153 const CookieNode *n1 = v1, *n2 = v2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154
1484
29d892265da0 domain comparison not case-sensitive
corvid <corvid@lavabit.com>
parents: 1483
diff changeset
155 return dStrcasecmp(n1->domain, n2->domain);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 * Compare function for searching a cookie node by domain
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
161 static int Cookie_node_by_domain_cmp(const void *v1, const void *v2)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 const CookieNode *node = v1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164 const char *domain = v2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165
1484
29d892265da0 domain comparison not case-sensitive
corvid <corvid@lavabit.com>
parents: 1483
diff changeset
166 return dStrcasecmp(node->domain, domain);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
167 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
168
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
169 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
170 * Return a file pointer. If the file doesn't exist, try to create it,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
171 * with the optional 'init_str' as its content.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
172 */
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
173 static FILE *Cookies_fopen(const char *filename, const char *mode,
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
174 char *init_str)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
175 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
176 FILE *F_in;
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
177 int fd, rc;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
178
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
179 if ((F_in = fopen(filename, mode)) == NULL) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
180 /* Create the file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
181 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
182 if (fd != -1) {
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
183 if (init_str) {
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
184 rc = write(fd, init_str, strlen(init_str));
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
185 if (rc == -1) {
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
186 MSG("Cookies: Could not write initial string to file %s: %s\n",
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
187 filename, dStrerror(errno));
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
188 }
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
189 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
190 close(fd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
191
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
192 MSG("Created file: %s\n", filename);
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
193 F_in = fopen(filename, mode);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
194 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
195 MSG("Could not create file: %s!\n", filename);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
196 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
197 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
198
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
199 if (F_in) {
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
200 /* set close on exec */
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
201 fcntl(fileno(F_in), F_SETFD, FD_CLOEXEC | fcntl(fileno(F_in), F_GETFD));
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
202 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
203
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
204 return F_in;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
205 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
206
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
207 static void Cookies_free_cookie(CookieData_t *cookie)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
208 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
209 dFree(cookie->name);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
210 dFree(cookie->value);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
211 dFree(cookie->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
212 dFree(cookie->path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
213 dFree(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
214 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
215
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
216 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
217 * Initialize the cookies module
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
218 * (The 'disabled' variable is writable only within Cookies_init)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
219 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
220 static void Cookies_init()
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
221 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
222 CookieData_t *cookie;
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
223 char *filename, *rc = NULL;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
224 char line[LINE_MAXLEN];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
225 #ifndef HAVE_LOCKF
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
226 struct flock lck;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
227 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
228
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
229 /* Default setting */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
230 disabled = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
231
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
232 /* Read and parse the cookie control file (cookiesrc) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
233 if (Cookie_control_init() != 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
234 MSG("Disabling cookies.\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
235 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
236 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
237
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
238 /* Get a stream for the cookies file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
239 filename = dStrconcat(dGethomedir(), "/.dillo/cookies.txt", NULL);
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
240 file_stream = Cookies_fopen(filename, "r+", cookies_txt_header_str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
241
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
242 dFree(filename);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
243
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
244 if (!file_stream) {
1495
a83b066cbcb3 rm support for ancient ~/.dillo/cookies file
corvid <corvid@lavabit.com>
parents: 1491
diff changeset
245 MSG("ERROR: Can't open ~/.dillo/cookies.txt; disabling cookies\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
246 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
247 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
248
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
249 /* Try to get a lock from the file descriptor */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
250 #ifdef HAVE_LOCKF
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
251 disabled = (lockf(fileno(file_stream), F_TLOCK, 0) == -1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
252 #else /* POSIX lock */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
253 lck.l_start = 0; /* start at beginning of file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
254 lck.l_len = 0; /* lock entire file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
255 lck.l_type = F_WRLCK;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
256 lck.l_whence = SEEK_SET; /* absolute offset */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
257
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
258 disabled = (fcntl(fileno(file_stream), F_SETLK, &lck) == -1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
259 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
260 if (disabled) {
1495
a83b066cbcb3 rm support for ancient ~/.dillo/cookies file
corvid <corvid@lavabit.com>
parents: 1491
diff changeset
261 MSG("The cookies file has a file lock; disabling cookies!\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
262 fclose(file_stream);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
263 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
264 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
265
1495
a83b066cbcb3 rm support for ancient ~/.dillo/cookies file
corvid <corvid@lavabit.com>
parents: 1491
diff changeset
266 MSG("Enabling cookies as per cookiesrc...\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
267
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
268 cookies = dList_new(32);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
269
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
270 /* Get all lines in the file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
271 while (!feof(file_stream)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
272 line[0] = '\0';
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
273 rc = fgets(line, LINE_MAXLEN, file_stream);
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
274 if (!rc && ferror(file_stream)) {
1201
cabde55d0e05 fix cookies dpi MSGs
corvid <corvid@lavabit.com>
parents: 1200
diff changeset
275 MSG("Cookies1: Error while reading from cookies.txt: %s\n",
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
276 dStrerror(errno));
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
277 break; /* bail out */
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
278 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
279
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
280 /* Remove leading and trailing whitespaces */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
281 dStrstrip(line);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
282
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
283 if ((line[0] != '\0') && (line[0] != '#')) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
284 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
285 * Split the row into pieces using a tab as the delimiter.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
286 * pieces[0] The domain name
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
287 * pieces[1] TRUE/FALSE: is the domain a suffix, or a full domain?
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
288 * pieces[2] The path
1495
a83b066cbcb3 rm support for ancient ~/.dillo/cookies file
corvid <corvid@lavabit.com>
parents: 1491
diff changeset
289 * pieces[3] TRUE/FALSE: is the cookie for secure use only?
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
290 * pieces[4] Timestamp of expire date
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
291 * pieces[5] Name of the cookie
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
292 * pieces[6] Value of the cookie
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
293 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
294 CookieControlAction action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
295 char *piece;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
296 char *line_marker = line;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
297
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
298 cookie = dNew0(CookieData_t, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
299
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
300 cookie->session_only = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
301 cookie->domain = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
302 dStrsep(&line_marker, "\t"); /* we use domain always as sufix */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
303 cookie->path = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
304 piece = dStrsep(&line_marker, "\t");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
305 if (piece != NULL && piece[0] == 'T')
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
306 cookie->secure = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
307 piece = dStrsep(&line_marker, "\t");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
308 if (piece != NULL)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
309 cookie->expires_at = (time_t) strtol(piece, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
310 cookie->name = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
311 cookie->value = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
312
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
313 if (!cookie->domain || cookie->domain[0] == '\0' ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
314 !cookie->path || cookie->path[0] != '/' ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
315 !cookie->name || cookie->name[0] == '\0' ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
316 !cookie->value) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
317 MSG("Malformed line in cookies.txt file!\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
318 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
319 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
320 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
321
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
322 action = Cookies_control_check_domain(cookie->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
323 if (action == COOKIE_DENY) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
324 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
325 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
326 } else if (action == COOKIE_ACCEPT_SESSION) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
327 cookie->session_only = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
328 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
329
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
330 /* Save cookie in memory */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
331 Cookies_add_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
332 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
333 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
334 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
335
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
336 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
337 * Flush cookies to disk and free all the memory allocated.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
338 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
339 static void Cookies_save_and_free()
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
340 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
341 int i, fd;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
342 CookieNode *node;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
343 CookieData_t *cookie;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
344
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
345 #ifndef HAVE_LOCKF
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
346 struct flock lck;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
347 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
348
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
349 if (disabled)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
350 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
351
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
352 rewind(file_stream);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
353 fd = fileno(file_stream);
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
354 if (ftruncate(fd, 0) == -1)
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
355 MSG("Cookies: Truncate file stream failed: %s\n", dStrerror(errno));
454
c0d3c2ec7140 - Fixed a printf bug in cookies dpi.
jcid
parents: 445
diff changeset
356 fprintf(file_stream, "%s", cookies_txt_header_str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
357
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
358 /* Iterate cookies per domain, saving and freeing */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
359 while ((node = dList_nth_data(cookies, 0))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
360 for (i = 0; (cookie = dList_nth_data(node->dlist, i)); ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
361 if (!cookie->session_only) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
362 fprintf(file_stream, "%s\tTRUE\t%s\t%s\t%ld\t%s\t%s\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
363 cookie->domain,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
364 cookie->path,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
365 cookie->secure ? "TRUE" : "FALSE",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
366 (long)cookie->expires_at,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
367 cookie->name,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
368 cookie->value);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
369 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
370
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
371 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
372 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
373 dList_remove(cookies, node);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
374 dFree(node->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
375 dList_free(node->dlist);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
376 dFree(node);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
377 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
378
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
379 #ifdef HAVE_LOCKF
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
380 lockf(fd, F_ULOCK, 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
381 #else /* POSIX file lock */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
382 lck.l_start = 0; /* start at beginning of file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
383 lck.l_len = 0; /* lock entire file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
384 lck.l_type = F_UNLCK;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
385 lck.l_whence = SEEK_SET; /* absolute offset */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
386
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
387 fcntl(fileno(file_stream), F_SETLKW, &lck);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
388 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
389 fclose(file_stream);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
390 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
391
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
392 static char *months[] =
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
393 { "",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
394 "Jan", "Feb", "Mar",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
395 "Apr", "May", "Jun",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
396 "Jul", "Aug", "Sep",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
397 "Oct", "Nov", "Dec"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
398 };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
399
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
400 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
401 * Take a months name and return a number between 1-12.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
402 * E.g. 'April' -> 4
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
403 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
404 static int Cookies_get_month(const char *month_name)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
405 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
406 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
407
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
408 for (i = 1; i <= 12; i++) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
409 if (!dStrncasecmp(months[i], month_name, 3))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
410 return i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
411 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
412 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
413 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
414
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
415 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
416 * Return a local timestamp from a GMT date string
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
417 * Accept: RFC-1123 | RFC-850 | ANSI asctime | Old Netscape format.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
418 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
419 * Wdy, DD-Mon-YY HH:MM:SS GMT
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
420 * Wdy, DD-Mon-YYYY HH:MM:SS GMT
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
421 * Weekday, DD-Mon-YY HH:MM:SS GMT
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
422 * Weekday, DD-Mon-YYYY HH:MM:SS GMT
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
423 * Tue May 21 13:46:22 1991\n
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
424 * Tue May 21 13:46:22 1991
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
425 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
426 * (return 0 on malformed date string syntax)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
427 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
428 static time_t Cookies_create_timestamp(const char *expires)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
429 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
430 time_t ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
431 int day, month, year, hour, minutes, seconds;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
432 char *cp;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
433 char *E_msg =
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
434 "Expire date is malformed!\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
435 " (should be RFC-1123 | RFC-850 | ANSI asctime)\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
436 " Ignoring cookie: ";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
437
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
438 cp = strchr(expires, ',');
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
439 if (!cp && (strlen(expires) == 24 || strlen(expires) == 25)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
440 /* Looks like ANSI asctime format... */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
441 cp = (char *)expires;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
442 day = strtol(cp + 8, NULL, 10); /* day */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
443 month = Cookies_get_month(cp + 4); /* month */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
444 year = strtol(cp + 20, NULL, 10); /* year */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
445 hour = strtol(cp + 11, NULL, 10); /* hour */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
446 minutes = strtol(cp + 14, NULL, 10); /* minutes */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
447 seconds = strtol(cp + 17, NULL, 10); /* seconds */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
448
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
449 } else if (cp && (cp - expires == 3 || cp - expires > 5) &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
450 (strlen(cp) == 24 || strlen(cp) == 26)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
451 /* RFC-1123 | RFC-850 format | Old Netscape format */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
452 day = strtol(cp + 2, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
453 month = Cookies_get_month(cp + 5);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
454 year = strtol(cp + 9, &cp, 10);
368
2242da885677 - s/todo:/TODO:/g
jcid
parents: 235
diff changeset
455 /* TODO: tricky, because two digits for year IS ambiguous! */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
456 year += (year < 70) ? 2000 : ((year < 100) ? 1900 : 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
457 hour = strtol(cp + 1, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
458 minutes = strtol(cp + 4, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
459 seconds = strtol(cp + 7, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
460
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
461 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
462 MSG("%s%s\n", E_msg, expires);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
463 return (time_t) 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
464 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
465
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
466 /* Error checks --this may be overkill */
1127
65c7e33e4466 allow year 1970 in cookies
corvid <corvid@lavabit.com>
parents: 454
diff changeset
467 if (!(day > 0 && day < 32 && month > 0 && month < 13 && year >= 1970 &&
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
468 hour >= 0 && hour < 24 && minutes >= 0 && minutes < 60 &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
469 seconds >= 0 && seconds < 60)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
470 MSG("%s%s\n", E_msg, expires);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
471 return (time_t) 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
472 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
473
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
474 /* Calculate local timestamp.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
475 * [stolen from Lynx... (http://lynx.browser.org)] */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
476 month -= 3;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
477 if (month < 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
478 month += 12;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
479 year--;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
480 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
481
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
482 day += (year - 1968) * 1461 / 4;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
483 day += ((((month * 153) + 2) / 5) - 672);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
484 ret = (time_t)((day * 60 * 60 * 24) +
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
485 (hour * 60 * 60) +
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
486 (minutes * 60) +
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
487 seconds);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
488
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
489 MSG("Expires in %ld seconds, at %s",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
490 (long)ret - time(NULL), ctime(&ret));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
491
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
492 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
493 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
494
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
495 static void Cookies_add_cookie(CookieData_t *cookie)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
496 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
497 Dlist *domain_cookies;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
498 CookieData_t *c;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
499 CookieNode *node;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
500
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
501 node = dList_find_sorted(cookies, cookie->domain,Cookie_node_by_domain_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
502 domain_cookies = (node) ? node->dlist : NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
503
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
504 if (domain_cookies) {
1482
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
505 /* Remove any cookies with the same name and path */
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
506 while ((c = dList_find_custom(domain_cookies, cookie, Cookies_cmp))){
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
507 Cookies_remove_cookie(c);
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
508 }
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
509
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
510 /* Respect the limit of 20 cookies per domain */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
511 if (dList_length(domain_cookies) >= 20) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
512 MSG("There are too many cookies for this domain (%s)\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
513 cookie->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
514 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
515 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
516 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
517
1482
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
518 }
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
519
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
520 /* Don't add an expired cookie. Strictly speaking, max-age cookies should
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
521 * only be discarded when "the age is _greater_ than delta-seconds seconds"
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
522 * (my emphasis), but "A value of zero means the cookie SHOULD be discarded
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
523 * immediately", and there's no compelling reason to distinguish between
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
524 * these cases. */
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
525 if (cookie->expires_at <= time(NULL)) {
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
526 Cookies_free_cookie(cookie);
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
527 return;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
528 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
529
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
530 /* add the cookie into the respective domain list */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
531 node = dList_find_sorted(cookies, cookie->domain,Cookie_node_by_domain_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
532 domain_cookies = (node) ? node->dlist : NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
533 if (!domain_cookies) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
534 domain_cookies = dList_new(5);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
535 dList_append(domain_cookies, cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
536 node = dNew(CookieNode, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
537 node->domain = dStrdup(cookie->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
538 node->dlist = domain_cookies;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
539 dList_insert_sorted(cookies, node, Cookie_node_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
540 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
541 dList_append(domain_cookies, cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
542 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
543 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
544
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
545 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
546 * Remove the cookie from the domain list.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
547 * If the domain list is empty, remove the node too.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
548 * Free the cookie.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
549 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
550 static void Cookies_remove_cookie(CookieData_t *cookie)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
551 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
552 CookieNode *node;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
553
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
554 node = dList_find_sorted(cookies, cookie->domain,Cookie_node_by_domain_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
555 if (node) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
556 dList_remove(node->dlist, cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
557 if (dList_length(node->dlist) == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
558 dList_remove(cookies, node);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
559 dFree(node->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
560 dList_free(node->dlist);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
561 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
562 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
563 MSG("Attempting to remove a cookie that doesn't exist!\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
564 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
565
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
566 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
567 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
568
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
569 /*
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
570 * Return the attribute that is present at *cookie_str.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
571 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
572 static char *Cookies_parse_attr(char **cookie_str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
573 {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
574 char *str;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
575 uint_t len;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
576
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
577 while (dIsspace(**cookie_str))
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
578 (*cookie_str)++;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
579
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
580 str = *cookie_str;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
581 /* find '=' at end of attr, ';' after attr/val pair, '\0' end of string */
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
582 len = strcspn(str, "=;");
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
583 *cookie_str += len;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
584
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
585 while (len && (str[len - 1] == ' ' || str[len - 1] == '\t'))
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
586 len--;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
587 return dStrndup(str, len);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
588 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
589
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
590 /*
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
591 * Get the value in *cookie_str.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
592 */
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
593 static char *Cookies_parse_value(char **cookie_str)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
594 {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
595 uint_t len;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
596 char *str;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
597
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
598 if (**cookie_str == '=') {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
599 (*cookie_str)++;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
600 while (dIsspace(**cookie_str))
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
601 (*cookie_str)++;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
602
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
603 str = *cookie_str;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
604 /* finds ';' after attr/val pair or '\0' at end of string */
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
605 len = strcspn(str, ";");
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
606 *cookie_str += len;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
607
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
608 while (len && (str[len - 1] == ' ' || str[len - 1] == '\t'))
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
609 len--;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
610 } else {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
611 str = *cookie_str;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
612 len = 0;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
613 }
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
614 return dStrndup(str, len);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
615 }
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
616
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
617 /*
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
618 * Advance past any value
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
619 */
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
620 static void Cookies_eat_value(char **cookie_str)
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
621 {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
622 if (**cookie_str == '=')
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
623 *cookie_str += strcspn(*cookie_str, ";");
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
624 }
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
625
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
626 /*
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
627 * Parse cookie. A cookie might look something like:
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
628 * "Name=Val; Domain=example.com; Max-Age=3600; HttpOnly"
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
629 */
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
630 static CookieData_t *Cookies_parse(char *cookie_str)
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
631 {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
632 CookieData_t *cookie = NULL;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
633 char *str = cookie_str;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
634 bool_t first_attr = TRUE;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
635 bool_t max_age = FALSE;
1482
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
636 bool_t expires = FALSE;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
637
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
638 /* Iterate until there is nothing left of the string */
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
639 while (*str) {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
640 char *attr;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
641 char *value;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
642
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
643 /* Get attribute */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
644 attr = Cookies_parse_attr(&str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
645
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
646 /* Get the value for the attribute and store it */
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
647 if (first_attr) {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
648 if (!*str && !*attr) {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
649 dFree(attr);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
650 return NULL;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
651 }
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
652 cookie = dNew0(CookieData_t, 1);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
653 /* let's arbitrarily choose a year for now */
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
654 cookie->expires_at = time(NULL) + 60 * 60 * 24 * 365;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
655
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
656 if (*str != '=') {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
657 /* NOTE it seems possible that the Working Group will decide
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
658 * against allowing nameless cookies.
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
659 */
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
660 cookie->name = dStrdup("");
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
661 cookie->value = attr;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
662 } else {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
663 cookie->name = dStrdup(attr);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
664 cookie->value = Cookies_parse_value(&str);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
665 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
666 } else if (dStrcasecmp(attr, "Path") == 0) {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
667 value = Cookies_parse_value(&str);
1491
a10148c0bcab cookies don't leak extra domain/path attrs
corvid <corvid@lavabit.com>
parents: 1490
diff changeset
668 dFree(cookie->path);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
669 cookie->path = value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
670 } else if (dStrcasecmp(attr, "Domain") == 0) {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
671 value = Cookies_parse_value(&str);
1491
a10148c0bcab cookies don't leak extra domain/path attrs
corvid <corvid@lavabit.com>
parents: 1490
diff changeset
672 dFree(cookie->domain);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
673 cookie->domain = value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
674 } else if (dStrcasecmp(attr, "Max-Age") == 0) {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
675 value = Cookies_parse_value(&str);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
676 if (isdigit(*value) || *value == '-') {
1482
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
677 cookie->expires_at = time(NULL) + strtol(value, NULL, 10);
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
678 expires = max_age = TRUE;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
679 }
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
680 dFree(value);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
681 } else if (dStrcasecmp(attr, "Expires") == 0) {
1482
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
682 if (!max_age) {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
683 value = Cookies_parse_value(&str);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
684 cookie->expires_at = Cookies_create_timestamp(value);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
685 expires = TRUE;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
686 dFree(value);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
687 MSG("Expires in %ld seconds, at %s",
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
688 (long)cookie->expires_at - time(NULL),
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
689 ctime(&cookie->expires_at));
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
690
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
691 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
692 } else if (dStrcasecmp(attr, "Secure") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
693 cookie->secure = TRUE;
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
694 Cookies_eat_value(&str);
418
b7265ec32e33 - Allow login to wikipedia by ignoring the HttpOnly attribute (cookies)
jcid
parents: 379
diff changeset
695 } else if (dStrcasecmp(attr, "HttpOnly") == 0) {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
696 Cookies_eat_value(&str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
697 } else {
379
52f65d7a0e7c - minor cookie MSG improvement.
jcid
parents: 368
diff changeset
698 MSG("Cookie contains unknown attribute: '%s'\n", attr);
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
699 Cookies_eat_value(&str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
700 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
701
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
702 if (first_attr)
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
703 first_attr = FALSE;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
704 else
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
705 dFree(attr);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
706
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
707 if (*str == ';')
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
708 str++;
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
709 }
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
710 cookie->session_only = expires == FALSE;
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
711 return cookie;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
712 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
713
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
714 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
715 * Compare cookies by name and path (return 0 if equal)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
716 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
717 static int Cookies_cmp(const void *a, const void *b)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
718 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
719 const CookieData_t *ca = a, *cb = b;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
720 int ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
721
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
722 if (!(ret = strcmp(ca->name, cb->name)))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
723 ret = strcmp(ca->path, cb->path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
724 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
725 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
726
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
727 /*
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
728 * Check whether url_path path-matches cookie_path
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
729 *
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
730 * Note different user agents apparently vary in path-matching behaviour,
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
731 * but this is the recommended method at the moment.
1486
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
732 */
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
733 static bool_t Cookies_path_matches(const char *url_path,
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
734 const char *cookie_path)
1486
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
735 {
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
736 bool_t ret = TRUE;
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
737
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
738 if (!url_path || !cookie_path) {
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
739 ret = FALSE;
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
740 } else {
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
741 uint_t c_len = strlen(cookie_path);
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
742 uint_t u_len = strlen(url_path);
1486
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
743
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
744 ret = (!strncmp(cookie_path, url_path, c_len) &&
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
745 ((c_len == u_len) ||
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
746 (c_len > 0 && cookie_path[c_len - 1] == '/') ||
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
747 (u_len > c_len && url_path[c_len] == '/')));
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
748 }
1486
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
749 return ret;
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
750 }
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
751
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
752 /*
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
753 * If cookie path is not properly set, remedy that.
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
754 */
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
755 static void Cookies_validate_path(CookieData_t *cookie, const char *url_path)
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
756 {
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
757 if (!cookie->path || cookie->path[0] != '/') {
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
758 dFree(cookie->path);
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
759
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
760 if (url_path) {
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
761 uint_t len = strlen(url_path);
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
762
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
763 while (len && url_path[len] != '/')
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
764 len--;
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
765 cookie->path = dStrndup(url_path, len ? len : 1);
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
766 } else {
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
767 cookie->path = dStrdup("/");
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
768 }
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
769 }
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
770 }
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
771
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
772 /*
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
773 * Check whether host name A domain-matches host name B.
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
774 */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
775 static bool_t Cookies_domain_matches(char *A, char *B)
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
776 {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
777 int diff;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
778
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
779 if (!A || !*A || !B || !*B)
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
780 return FALSE;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
781
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
782 if (*B == '.')
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
783 B++;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
784
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
785 /* Should we concern ourselves with trailing dots in matching (here or
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
786 * elsewhere)? The HTTP State people have found that most user agents
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
787 * don't, so: No.
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
788 */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
789
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
790 if (!dStrcasecmp(A, B))
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
791 return TRUE;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
792
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
793 diff = strlen(A) - strlen(B);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
794
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
795 if (diff > 0) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
796 /* B is the tail of A, and the match is preceded by a '.' */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
797 return (dStrcasecmp(A + diff, B) == 0 && A[diff - 1] == '.');
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
798 } else {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
799 return FALSE;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
800 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
801 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
802
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
803 /*
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
804 * Based on the host, how many internal dots do we need in a cookie domain
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
805 * to make it valid? e.g., "org" is not on the list, so dillo.org is a safe
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
806 * cookie domain, but "uk" is on the list, so ac.uk is not safe.
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
807 *
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
808 * This is imperfect, but it's something. Specifically, checking for these
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
809 * TLDs is the solution that Konqueror used once upon a time, according to
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
810 * reports.
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
811 */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
812 static uint_t Cookies_internal_dots_required(const char *host)
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
813 {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
814 uint_t ret = 1;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
815
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
816 if (host) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
817 int start, after, tld_len;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
818
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
819 /* We may be able to trust the format of the host string more than
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
820 * I am here. Trailing dots and no dots are real possibilities, though.
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
821 */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
822 after = strlen(host);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
823 if (after > 0 && host[after - 1] == '.')
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
824 after--;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
825 start = after;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
826 while (start > 0 && host[start - 1] != '.')
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
827 start--;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
828 tld_len = after - start;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
829
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
830 if (tld_len > 0) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
831 const char *const tlds[] = {"ai","au","bd","bh","ck","eg","et","fk",
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
832 "il","in","kh","kr","mk","mt","na","np",
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
833 "nz","pg","pk","qa","sa","sb","sg","sv",
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
834 "ua","ug","uk","uy","vn","za","zw","name"};
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
835 uint_t i, tld_num = sizeof(tlds) / sizeof(tlds[0]);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
836
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
837 for (i = 0; i < tld_num; i++) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
838 if (strlen(tlds[i]) == (uint_t) tld_len &&
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
839 !dStrncasecmp(tlds[i], host + start, tld_len)) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
840 MSG("TLD code matched %s\n", tlds[i]);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
841 ret++;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
842 break;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
843 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
844 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
845 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
846 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
847 return ret;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
848 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
849
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
850 /*
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
851 * Is the domain an IP address?
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
852 */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
853 static bool_t Cookies_domain_is_ip(const char *domain)
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
854 {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
855 bool_t ipv4 = TRUE, ipv6 = TRUE;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
856
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
857 if (!domain)
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
858 return FALSE;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
859
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
860 while (*domain) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
861 if (*domain != '.' && !isdigit(*domain))
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
862 ipv4 = FALSE;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
863 if (*domain != ':' && !isxdigit(*domain))
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
864 ipv6 = FALSE;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
865 if (!(ipv4 || ipv6))
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
866 return FALSE;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
867 domain++;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
868 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
869 MSG("an IP address\n");
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
870 return TRUE;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
871 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
872
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
873 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
874 * Validate cookies domain against some security checks.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
875 */
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
876 static bool_t Cookies_validate_domain(CookieData_t *cookie, char *host)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
877 {
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
878 uint_t i, internal_dots;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
879
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
880 if (!cookie->domain) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
881 cookie->domain = dStrdup(host);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
882 return TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
883 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
884
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
885 if (cookie->domain[0] != '.' && !Cookies_domain_is_ip(cookie->domain)) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
886 char *d = dStrconcat(".", cookie->domain, NULL);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
887 dFree(cookie->domain);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
888 cookie->domain = d;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
889 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
890
1488
afe771c1d19f cookie domains
corvid <corvid@lavabit.com>
parents: 1487
diff changeset
891 if (!Cookies_domain_matches(host, cookie->domain))
afe771c1d19f cookie domains
corvid <corvid@lavabit.com>
parents: 1487
diff changeset
892 return FALSE;
afe771c1d19f cookie domains
corvid <corvid@lavabit.com>
parents: 1487
diff changeset
893
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
894 internal_dots = 0;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
895 for (i = 1; i < strlen(cookie->domain) - 1; i++) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
896 if (cookie->domain[i] == '.')
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
897 internal_dots++;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
898 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
899
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
900 /* All of this dots business is a weak hack.
1488
afe771c1d19f cookie domains
corvid <corvid@lavabit.com>
parents: 1487
diff changeset
901 * TODO: accept the publicsuffix.org list as an optional external file.
afe771c1d19f cookie domains
corvid <corvid@lavabit.com>
parents: 1487
diff changeset
902 */
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
903 if (internal_dots < Cookies_internal_dots_required(host)) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
904 MSG("not enough dots in %s\n", cookie->domain);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
905 return FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
906 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
907
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
908 MSG("host %s and domain %s is all right\n", host, cookie->domain);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
909 return TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
910 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
911
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
912 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
913 * Set the value corresponding to the cookie string
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
914 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
915 static void Cookies_set(char *cookie_string, char *url_host,
1496
5bec8ce33a42 cookies: rm version, comment, comment url, port, Cookie2
corvid <corvid@lavabit.com>
parents: 1495
diff changeset
916 char *url_path)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
917 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
918 CookieControlAction action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
919 CookieData_t *cookie;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
920
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
921 if (disabled)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
922 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
923
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
924 action = Cookies_control_check_domain(url_host);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
925 if (action == COOKIE_DENY) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
926 MSG("denied SET for %s\n", url_host);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
927 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
928 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
929
1482
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
930 _MSG("%s setting: %s\n", url_host, cookie_string);
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
931
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
932 if ((cookie = Cookies_parse(cookie_string))) {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
933 if (Cookies_validate_domain(cookie, url_host)) {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
934 Cookies_validate_path(cookie, url_path);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
935 if (action == COOKIE_ACCEPT_SESSION)
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
936 cookie->session_only = TRUE;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
937 Cookies_add_cookie(cookie);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
938 } else {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
939 MSG("Rejecting cookie for %s from host %s path %s\n",
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
940 cookie->domain, url_host, url_path);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
941 Cookies_free_cookie(cookie);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
942 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
943 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
944 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
945
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
946 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
947 * Compare the cookie with the supplied data to see if it matches
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
948 */
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
949 static bool_t Cookies_match(CookieData_t *cookie, const char *url_path,
1496
5bec8ce33a42 cookies: rm version, comment, comment url, port, Cookie2
corvid <corvid@lavabit.com>
parents: 1495
diff changeset
950 bool_t is_ssl)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
951 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
952 /* Insecure cookies matches both secure and insecure urls, secure
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
953 cookies matches only secure urls */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
954 if (cookie->secure && !is_ssl)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
955 return FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
956
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
957 if (!Cookies_path_matches(url_path, cookie->path))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
958 return FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
959
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
960 /* It's a match */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
961 return TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
962 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
963
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
964 static void Cookies_add_matching_cookies(const char *domain,
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
965 const char *url_path,
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
966 Dlist *matching_cookies,
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
967 bool_t is_ssl)
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
968 {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
969 CookieNode *node = dList_find_sorted(cookies, domain,
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
970 Cookie_node_by_domain_cmp);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
971 if (node) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
972 int i;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
973 CookieData_t *cookie;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
974 Dlist *domain_cookies = node->dlist;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
975
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
976 for (i = 0; (cookie = dList_nth_data(domain_cookies, i)); ++i) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
977 /* Remove expired cookie. */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
978 if (cookie->expires_at < time(NULL)) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
979 MSG("goodbye, expired cookie %s=%s d:%s p:%s\n", cookie->name,
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
980 cookie->value, cookie->domain, cookie->path);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
981 Cookies_remove_cookie(cookie);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
982 --i; continue;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
983 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
984 /* Check if the cookie matches the requesting URL */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
985 if (Cookies_match(cookie, url_path, is_ssl)) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
986 int j;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
987 CookieData_t *curr;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
988 uint_t path_length = strlen(cookie->path);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
989
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
990 /* Longest cookies go first */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
991 for (j = 0;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
992 (curr = dList_nth_data(matching_cookies, j)) &&
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
993 strlen(curr->path) >= path_length;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
994 j++) ;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
995 dList_insert_pos(matching_cookies, cookie, j);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
996 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
997 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
998 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
999 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1000
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1001 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1002 * Return a string that contains all relevant cookies as headers.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1003 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
1004 static char *Cookies_get(char *url_host, char *url_path,
1496
5bec8ce33a42 cookies: rm version, comment, comment url, port, Cookie2
corvid <corvid@lavabit.com>
parents: 1495
diff changeset
1005 char *url_scheme)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1006 {
1496
5bec8ce33a42 cookies: rm version, comment, comment url, port, Cookie2
corvid <corvid@lavabit.com>
parents: 1495
diff changeset
1007 char *domain_str, *str;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1008 CookieData_t *cookie;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1009 Dlist *matching_cookies;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1010 bool_t is_ssl;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1011 Dstr *cookie_dstring;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1012 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1013
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1014 if (disabled)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1015 return dStrdup("");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1016
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1017 matching_cookies = dList_new(8);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1018
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1019 /* Check if the protocol is secure or not */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1020 is_ssl = (!dStrcasecmp(url_scheme, "https"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1021
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1022 for (domain_str = (char *) url_host;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1023 domain_str != NULL && *domain_str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1024 domain_str = strchr(domain_str+1, '.')) {
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1025 Cookies_add_matching_cookies(domain_str, url_path, matching_cookies,
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1026 is_ssl);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1027 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1028 if (!Cookies_domain_is_ip(url_host)) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1029 domain_str = dStrconcat(".", url_host, NULL);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1030 Cookies_add_matching_cookies(domain_str, url_path, matching_cookies,
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1031 is_ssl);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1032 dFree(domain_str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1033 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1034
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1035 /* Found the cookies, now make the string */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1036 cookie_dstring = dStr_new("");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1037 if (dList_length(matching_cookies) > 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1038
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1039 dStr_sprintfa(cookie_dstring, "Cookie: ");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1040
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1041 for (i = 0; (cookie = dList_nth_data(matching_cookies, i)); ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1042 dStr_sprintfa(cookie_dstring,
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
1043 "%s%s%s",
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
1044 cookie->name, *cookie->name ? "=" : "", cookie->value);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1045 dStr_append(cookie_dstring,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1046 dList_length(matching_cookies) > i + 1 ? "; " : "\r\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1047 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1048 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1049
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1050 dList_free(matching_cookies);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1051 str = cookie_dstring->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1052 dStr_free(cookie_dstring, FALSE);
1482
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
1053 _MSG("%s gets %s\n", url_host, str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1054 return str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1055 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1056
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1057 /* -------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1058 * Access control routines
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1059 * ------------------------------------------------------------- */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1060
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1061
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1062 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1063 * Get the cookie control rules (from cookiesrc).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1064 * Return value:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1065 * 0 = Parsed OK, with cookies enabled
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1066 * 1 = Parsed OK, with cookies disabled
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1067 * 2 = Can't open the control file
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1068 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1069 static int Cookie_control_init(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1070 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1071 CookieControl cc;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1072 FILE *stream;
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1073 char *filename, *rc;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1074 char line[LINE_MAXLEN];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1075 char domain[LINE_MAXLEN];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1076 char rule[LINE_MAXLEN];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1077 int i, j;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1078 bool_t enabled = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1079
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1080 /* Get a file pointer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1081 filename = dStrconcat(dGethomedir(), "/.dillo/cookiesrc", NULL);
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
1082 stream = Cookies_fopen(filename, "r", "DEFAULT DENY\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1083 dFree(filename);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1084
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1085 if (!stream)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1086 return 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1087
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1088 /* Get all lines in the file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1089 while (!feof(stream)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1090 line[0] = '\0';
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1091 rc = fgets(line, LINE_MAXLEN, stream);
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1092 if (!rc && ferror(stream)) {
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1093 MSG("Cookies3: Error while reading rule from cookiesrc: %s\n",
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1094 dStrerror(errno));
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1095 break; /* bail out */
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1096 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1097
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1098 /* Remove leading and trailing whitespaces */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1099 dStrstrip(line);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1100
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1101 if (line[0] != '\0' && line[0] != '#') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1102 i = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1103 j = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1104
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1105 /* Get the domain */
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1201
diff changeset
1106 while (line[i] != '\0' && !dIsspace(line[i]))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1107 domain[j++] = line[i++];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1108 domain[j] = '\0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1109
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1110 /* Skip past whitespaces */
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1201
diff changeset
1111 while (dIsspace(line[i]))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1112 i++;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1113
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1114 /* Get the rule */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1115 j = 0;
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1201
diff changeset
1116 while (line[i] != '\0' && !dIsspace(line[i]))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1117 rule[j++] = line[i++];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1118 rule[j] = '\0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1119
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1120 if (dStrcasecmp(rule, "ACCEPT") == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1121 cc.action = COOKIE_ACCEPT;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1122 else if (dStrcasecmp(rule, "ACCEPT_SESSION") == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1123 cc.action = COOKIE_ACCEPT_SESSION;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1124 else if (dStrcasecmp(rule, "DENY") == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1125 cc.action = COOKIE_DENY;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1126 else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1127 MSG("Cookies: rule '%s' for domain '%s' is not recognised.\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1128 rule, domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1129 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1130 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1131
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1132 cc.domain = dStrdup(domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1133 if (dStrcasecmp(cc.domain, "DEFAULT") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1134 /* Set the default action */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1135 default_action = cc.action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1136 dFree(cc.domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1137 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1138 a_List_add(ccontrol, num_ccontrol, num_ccontrol_max);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1139 ccontrol[num_ccontrol++] = cc;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1140 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1141
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1142 if (cc.action != COOKIE_DENY)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1143 enabled = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1144 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1145 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1146
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1147 fclose(stream);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1148
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1149 return (enabled ? 0 : 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1150 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1151
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1152 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1153 * Check the rules for an appropriate action for this domain
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1154 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1155 static CookieControlAction Cookies_control_check_domain(const char *domain)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1156 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1157 int i, diff;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1158
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1159 for (i = 0; i < num_ccontrol; i++) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1160 if (ccontrol[i].domain[0] == '.') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1161 diff = strlen(domain) - strlen(ccontrol[i].domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1162 if (diff >= 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1163 if (dStrcasecmp(domain + diff, ccontrol[i].domain) != 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1164 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1165 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1166 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1167 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1168 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1169 if (dStrcasecmp(domain, ccontrol[i].domain) != 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1170 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1171 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1172
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1173 /* If we got here we have a match */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1174 return( ccontrol[i].action );
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1175 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1176
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1177 return default_action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1178 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1179
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1180 /* -- Dpi parser ----------------------------------------------------------- */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1181
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1182 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1183 * Parse a data stream (dpi protocol)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1184 * Note: Buf is a zero terminated string
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1185 * Return code: { 0:OK, 1:Abort, 2:Close }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1186 */
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1187 static int srv_parse_tok(Dsh *sh, ClientInfo *client, char *Buf)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1188 {
1496
5bec8ce33a42 cookies: rm version, comment, comment url, port, Cookie2
corvid <corvid@lavabit.com>
parents: 1495
diff changeset
1189 char *cmd, *cookie, *host, *path, *scheme;
5bec8ce33a42 cookies: rm version, comment, comment url, port, Cookie2
corvid <corvid@lavabit.com>
parents: 1495
diff changeset
1190 int ret = 1;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1191 size_t BufSize = strlen(Buf);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1192
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1193 cmd = a_Dpip_get_attr_l(Buf, BufSize, "cmd");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1194
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1195 if (!cmd) {
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1196 /* abort */
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1197 } else if (client->status == 0) {
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1198 /* authenticate */
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1199 if (a_Dpip_check_auth(Buf) == 1) {
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1200 client->status = 1;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1201 ret = 0;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1202 }
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1203 } else if (strcmp(cmd, "DpiBye") == 0) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1204 dFree(cmd);
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1205 MSG("(pid %d): Got DpiBye.\n", (int)getpid());
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1206 exit(0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1207
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1208 } else if (cmd && strcmp(cmd, "set_cookie") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1209 dFree(cmd);
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1210 cookie = a_Dpip_get_attr_l(Buf, BufSize, "cookie");
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1211 host = a_Dpip_get_attr_l(Buf, BufSize, "host");
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1212 path = a_Dpip_get_attr_l(Buf, BufSize, "path");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1213
1496
5bec8ce33a42 cookies: rm version, comment, comment url, port, Cookie2
corvid <corvid@lavabit.com>
parents: 1495
diff changeset
1214 Cookies_set(cookie, host, path);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1215
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1216 dFree(path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1217 dFree(host);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1218 dFree(cookie);
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1219 ret = 2;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1220
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1221 } else if (cmd && strcmp(cmd, "get_cookie") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1222 dFree(cmd);
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1223 scheme = a_Dpip_get_attr_l(Buf, BufSize, "scheme");
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1224 host = a_Dpip_get_attr_l(Buf, BufSize, "host");
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1225 path = a_Dpip_get_attr_l(Buf, BufSize, "path");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1226
1496
5bec8ce33a42 cookies: rm version, comment, comment url, port, Cookie2
corvid <corvid@lavabit.com>
parents: 1495
diff changeset
1227 cookie = Cookies_get(host, path, scheme);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1228 dFree(scheme);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1229 dFree(path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1230 dFree(host);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1231
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1232 cmd = a_Dpip_build_cmd("cmd=%s cookie=%s", "get_cookie_answer", cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1233
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1234 if (a_Dpip_dsh_write_str(sh, 1, cmd)) {
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1235 ret = 1;
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1236 } else {
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1237 _MSG("a_Dpip_dsh_write_str: SUCCESS cmd={%s}\n", cmd);
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1238 ret = 2;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1239 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1240 dFree(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1241 dFree(cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1242 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1243
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1244 return ret;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1245 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1246
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1247 /* -- Termination handlers ----------------------------------------------- */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1248 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1249 * (was to delete the local namespace socket),
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1250 * but this is handled by 'dpid' now.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1251 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1252 static void cleanup(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1253 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1254 Cookies_save_and_free();
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1255 MSG("cleanup\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1256 /* no more cleanup required */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1257 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1258
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1259 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1260 * Perform any necessary cleanups upon abnormal termination
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1261 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1262 static void termination_handler(int signum)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1263 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1264 exit(signum);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1265 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1266
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1267
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1268 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1269 * -- MAIN -------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1270 */
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
1271 int main(void) {
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1272 struct sockaddr_in sin;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1273 socklen_t address_size;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1274 ClientInfo *client;
1439
79fcf67eeea0 Fixed bookmarks "cancel" link (Add/Mod URL, Add Sec). Also s/tmp_fd/sock_fd.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
1275 int sock_fd, code;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1276 char *buf;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1277 Dsh *sh;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1278
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1279 /* Arrange the cleanup function for terminations via exit() */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1280 atexit(cleanup);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1281
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1282 /* Arrange the cleanup function for abnormal terminations */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1283 if (signal (SIGINT, termination_handler) == SIG_IGN)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1284 signal (SIGINT, SIG_IGN);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1285 if (signal (SIGHUP, termination_handler) == SIG_IGN)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1286 signal (SIGHUP, SIG_IGN);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1287 if (signal (SIGTERM, termination_handler) == SIG_IGN)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1288 signal (SIGTERM, SIG_IGN);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1289
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1290 Cookies_init();
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1291 MSG("(v.1) accepting connections...\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1292
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1293 if (disabled)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1294 exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1295
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1296 /* some OSes may need this... */
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1297 address_size = sizeof(struct sockaddr_in);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1298
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1299 while (1) {
1439
79fcf67eeea0 Fixed bookmarks "cancel" link (Add/Mod URL, Add Sec). Also s/tmp_fd/sock_fd.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
1300 sock_fd = accept(STDIN_FILENO, (struct sockaddr *)&sin, &address_size);
79fcf67eeea0 Fixed bookmarks "cancel" link (Add/Mod URL, Add Sec). Also s/tmp_fd/sock_fd.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
1301 if (sock_fd == -1) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1302 perror("[accept]");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1303 exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1304 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1305
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1306 /* create the Dsh structure */
1439
79fcf67eeea0 Fixed bookmarks "cancel" link (Add/Mod URL, Add Sec). Also s/tmp_fd/sock_fd.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
1307 sh = a_Dpip_dsh_new(sock_fd, sock_fd, 8*1024);
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1308 client = dNew(ClientInfo,1);
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1309 client->sh = sh;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1310 client->status = 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1311
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1312 while (1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1313 code = 1;
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1314 if ((buf = a_Dpip_dsh_read_token(sh, 1)) != NULL) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1315 /* Let's see what we fished... */
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1316 _MSG(" buf = {%s}\n", buf);
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1317 code = srv_parse_tok(sh, client, buf);
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1318 dFree(buf);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1319 }
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1320
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1321 _MSG(" code = %d %s\n", code, code == 1 ? "EXIT" : "BREAK");
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1322 if (code == 1) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1323 exit(1);
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1324 } else if (code == 2) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1325 break;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1326 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1327 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1328
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1329 _MSG("Closing Dsh\n");
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1330 a_Dpip_dsh_close(sh);
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1331 a_Dpip_dsh_free(sh);
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1332 dFree(client);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1333
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1334 }/*while*/
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1335
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1336 return 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1337 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1338
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1339 #endif /* !DISABLE_COOKIES */