annotate dpi/cookies.c @ 2048:5060d415a85a

clickable menu items (even those introducing submenus) MUST have callbacks I clicked on the "Panel size" item itself instead of any of the options in its submenu, and: Segfault!
author corvid <corvid@lavabit.com>
date Thu, 26 May 2011 02:51:18 +0000
parents 7cad4651d972
children
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
2030
382160be8c2f cookies comments
corvid <corvid@lavabit.com>
parents: 2025
diff changeset
16 /* The current standard for cookies is RFC 6265.
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
17 *
2030
382160be8c2f cookies comments
corvid <corvid@lavabit.com>
parents: 2025
diff changeset
18 * Info from 2009 on cookies in the wild:
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
19 * 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
20 * And dates specifically:
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
21 * http://www.ietf.org/mail-archive/web/http-state/current/msg00128.html
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 #ifdef DISABLE_COOKIES
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26 int main(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28 return 0; /* never called */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31 #else
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34 #include <sys/types.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35 #include <sys/socket.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
36 #include <sys/stat.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 #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
38 #include <netinet/in.h>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39 #include <fcntl.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 #include <unistd.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 #include <errno.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 #include <stddef.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 #include <stdlib.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 #include <stdio.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 #include <time.h> /* for time() and time_t */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 #include <ctype.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 #include <netdb.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 #include <signal.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 #include "dpiutil.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 #include "../dpip/dpip.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 * Debugging macros
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 #define _MSG(...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58 #define MSG(...) printf("[cookies dpi]: " __VA_ARGS__)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61 * a_List_add()
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63 * Make sure there's space for 'num_items' items within the list
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64 * (First, allocate an 'alloc_step' sized chunk, after that, double the
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65 * list size --to make it faster)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67 #define a_List_add(list,num_items,alloc_step) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 if (!list) { \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69 list = dMalloc(alloc_step * sizeof((*list))); \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 } \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 if (num_items >= alloc_step){ \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 while ( num_items >= alloc_step ) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 alloc_step <<= 1; \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 list = dRealloc(list, alloc_step * sizeof((*list))); \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 /* The maximum length of a line in the cookie file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 #define LINE_MAXLEN 4096
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
80 #define MAX_DOMAIN_COOKIES 20
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
81 #define MAX_TOTAL_COOKIES 1200
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
82
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 typedef enum {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 COOKIE_ACCEPT,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 COOKIE_ACCEPT_SESSION,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 COOKIE_DENY
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 } CookieControlAction;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 typedef struct {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 char *domain;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 CookieControlAction action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 } CookieControl;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 typedef struct {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 char *domain;
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
96 Dlist *cookies;
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
97 } DomainNode;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 typedef struct {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 char *name;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 char *value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 char *domain;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 char *path;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 time_t expires_at;
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
105 bool_t host_only;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 bool_t secure;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 bool_t session_only;
1504
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
108 long last_used;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 } CookieData_t;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
111 typedef struct {
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
112 Dsh *sh;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
113 int status;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
114 } ClientInfo;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
115
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 * Local data
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
120 static Dlist *all_cookies;
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
121
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
122 /* List of DomainNode. Each node holds a domain and its list of cookies */
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
123 static Dlist *domains;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 /* Variables for access control */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 static CookieControl *ccontrol = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 static int num_ccontrol = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 static int num_ccontrol_max = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 static CookieControlAction default_action = COOKIE_DENY;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130
1504
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
131 static long cookies_use_counter = 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 static bool_t disabled;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 static FILE *file_stream;
1503
f02eb59edc7d cookies: a little cleaning
corvid <corvid@lavabit.com>
parents: 1501
diff changeset
134 static const char *const cookies_txt_header_str =
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135 "# HTTP Cookie File\n"
1503
f02eb59edc7d cookies: a little cleaning
corvid <corvid@lavabit.com>
parents: 1501
diff changeset
136 "# This is a generated file! Do not edit.\n"
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
137 "# [domain subdomains path secure expiry_time name value]\n\n";
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138
1607
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
139 /* The epoch is Jan 1, 1970. When there is difficulty in representing future
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
140 * dates, use the (by far) most likely last representable time in Jan 19, 2038.
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
141 */
1605
1dd07874017e cookies: use difftime() for time_t comparisons
corvid <corvid@lavabit.com>
parents: 1603
diff changeset
142 static struct tm cookies_epoch_tm = {0, 0, 0, 1, 0, 70, 0, 0, 0, 0, 0};
1607
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
143 static time_t cookies_epoch_time, cookies_future_time;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 * Forward declarations
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 static CookieControlAction Cookies_control_check_domain(const char *domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 static int Cookie_control_init(void);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 static void Cookies_add_cookie(CookieData_t *cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 static int Cookies_cmp(const void *a, const void *b);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 /*
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
155 * Compare function for searching a domain node
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 */
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
157 static int Domain_node_cmp(const void *v1, const void *v2)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 {
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
159 const DomainNode *n1 = v1, *n2 = v2;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160
1484
29d892265da0 domain comparison not case-sensitive
corvid <corvid@lavabit.com>
parents: 1483
diff changeset
161 return dStrcasecmp(n1->domain, n2->domain);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164 /*
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
165 * Compare function for searching a domain node by domain
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
166 */
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
167 static int Domain_node_by_domain_cmp(const void *v1, const void *v2)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
168 {
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
169 const DomainNode *node = v1;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
170 const char *domain = v2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
171
1484
29d892265da0 domain comparison not case-sensitive
corvid <corvid@lavabit.com>
parents: 1483
diff changeset
172 return dStrcasecmp(node->domain, domain);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
173 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
174
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
175 /*
1625
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
176 * Delete node. This will not free any cookies that might be in node->cookies.
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
177 */
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
178 static void Cookies_delete_node(DomainNode *node)
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
179 {
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
180 dList_remove(domains, node);
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
181 dFree(node->domain);
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
182 dList_free(node->cookies);
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
183 dFree(node);
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
184 }
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
185
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
186 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
187 * Return a file pointer. If the file doesn't exist, try to create it,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188 * with the optional 'init_str' as its content.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
189 */
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
190 static FILE *Cookies_fopen(const char *filename, const char *mode,
1503
f02eb59edc7d cookies: a little cleaning
corvid <corvid@lavabit.com>
parents: 1501
diff changeset
191 const char *init_str)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
192 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
193 FILE *F_in;
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
194 int fd, rc;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
195
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
196 if ((F_in = fopen(filename, mode)) == NULL) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
197 /* Create the file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
198 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
199 if (fd != -1) {
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
200 if (init_str) {
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
201 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
202 if (rc == -1) {
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
203 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
204 filename, dStrerror(errno));
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
205 }
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
206 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
207 close(fd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
208
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
209 MSG("Created file: %s\n", filename);
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
210 F_in = fopen(filename, mode);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
211 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
212 MSG("Could not create file: %s!\n", filename);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
213 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
214 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
215
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
216 if (F_in) {
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
217 /* set close on exec */
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
218 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
219 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
220
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
221 return F_in;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
222 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
223
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
224 static void Cookies_free_cookie(CookieData_t *cookie)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
225 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
226 dFree(cookie->name);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
227 dFree(cookie->value);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
228 dFree(cookie->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
229 dFree(cookie->path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
230 dFree(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
231 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
232
1607
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
233 static void Cookies_tm_init(struct tm *tm)
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
234 {
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
235 tm->tm_sec = cookies_epoch_tm.tm_sec;
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
236 tm->tm_min = cookies_epoch_tm.tm_min;
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
237 tm->tm_hour = cookies_epoch_tm.tm_hour;
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
238 tm->tm_mday = cookies_epoch_tm.tm_mday;
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
239 tm->tm_mon = cookies_epoch_tm.tm_mon;
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
240 tm->tm_year = cookies_epoch_tm.tm_year;
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
241 tm->tm_isdst = cookies_epoch_tm.tm_isdst;
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
242 }
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
243
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
244 /*
1629
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
245 * Read in cookies from 'stream' (cookies.txt)
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
246 */
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
247 static void Cookies_load_cookies(FILE *stream)
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
248 {
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
249 char line[LINE_MAXLEN];
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
250
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
251 all_cookies = dList_new(32);
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
252 domains = dList_new(32);
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
253
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
254 /* Get all lines in the file */
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
255 while (!feof(stream)) {
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
256 line[0] = '\0';
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
257 if ((fgets(line, LINE_MAXLEN, stream) == NULL) && ferror(stream)) {
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
258 MSG("Error while reading from cookies.txt: %s\n", dStrerror(errno));
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
259 break; /* bail out */
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
260 }
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
261
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
262 /* Remove leading and trailing whitespaces */
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
263 dStrstrip(line);
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
264
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
265 if ((line[0] != '\0') && (line[0] != '#')) {
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
266 /*
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
267 * Split the row into pieces using a tab as the delimiter.
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
268 * pieces[0] The domain name
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
269 * pieces[1] TRUE/FALSE: is the domain a suffix, or a full domain?
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
270 * pieces[2] The path
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
271 * pieces[3] TRUE/FALSE: is the cookie for secure use only?
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
272 * pieces[4] Timestamp of expire date
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
273 * pieces[5] Name of the cookie
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
274 * pieces[6] Value of the cookie
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
275 */
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
276 CookieControlAction action;
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
277 char *piece;
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
278 char *line_marker = line;
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
279 CookieData_t *cookie = dNew0(CookieData_t, 1);
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
280
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
281 cookie->session_only = FALSE;
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
282 cookie->domain = dStrdup(dStrsep(&line_marker, "\t"));
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
283 piece = dStrsep(&line_marker, "\t");
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
284 if (piece != NULL && piece[0] == 'F')
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
285 cookie->host_only = TRUE;
1629
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
286 cookie->path = dStrdup(dStrsep(&line_marker, "\t"));
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
287 piece = dStrsep(&line_marker, "\t");
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
288 if (piece != NULL && piece[0] == 'T')
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
289 cookie->secure = TRUE;
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
290 piece = dStrsep(&line_marker, "\t");
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
291 if (piece != NULL) {
1631
ec05ef8ca40c fix reading maximum expiration date from cookies.txt
corvid <corvid@lavabit.com>
parents: 1630
diff changeset
292 /* There is some problem with simply putting the maximum value
ec05ef8ca40c fix reading maximum expiration date from cookies.txt
corvid <corvid@lavabit.com>
parents: 1630
diff changeset
293 * into tm.tm_sec (although a value close to it works).
ec05ef8ca40c fix reading maximum expiration date from cookies.txt
corvid <corvid@lavabit.com>
parents: 1630
diff changeset
294 */
ec05ef8ca40c fix reading maximum expiration date from cookies.txt
corvid <corvid@lavabit.com>
parents: 1630
diff changeset
295 long seconds = strtol(piece, NULL, 10);
1629
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
296 struct tm tm;
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
297 Cookies_tm_init(&tm);
1631
ec05ef8ca40c fix reading maximum expiration date from cookies.txt
corvid <corvid@lavabit.com>
parents: 1630
diff changeset
298 tm.tm_min += seconds / 60;
ec05ef8ca40c fix reading maximum expiration date from cookies.txt
corvid <corvid@lavabit.com>
parents: 1630
diff changeset
299 tm.tm_sec += seconds % 60;
1629
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
300 cookie->expires_at = mktime(&tm);
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
301 } else {
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
302 cookie->expires_at = (time_t) -1;
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
303 }
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
304 cookie->name = dStrdup(dStrsep(&line_marker, "\t"));
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
305 cookie->value = dStrdup(line_marker ? line_marker : "");
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
306
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
307 if (!cookie->domain || cookie->domain[0] == '\0' ||
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
308 !cookie->path || cookie->path[0] != '/' ||
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
309 !cookie->name || !cookie->value) {
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
310 MSG("Malformed line in cookies.txt file!\n");
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
311 Cookies_free_cookie(cookie);
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
312 continue;
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
313 }
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
314
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
315 action = Cookies_control_check_domain(cookie->domain);
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
316 if (action == COOKIE_DENY) {
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
317 Cookies_free_cookie(cookie);
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
318 continue;
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
319 } else if (action == COOKIE_ACCEPT_SESSION) {
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
320 cookie->session_only = TRUE;
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
321 }
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
322
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
323 /* Save cookie in memory */
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
324 Cookies_add_cookie(cookie);
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
325 }
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
326 }
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
327 MSG("Cookies loaded: %d.\n", dList_length(all_cookies));
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
328 }
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
329
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
330 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
331 * Initialize the cookies module
1503
f02eb59edc7d cookies: a little cleaning
corvid <corvid@lavabit.com>
parents: 1501
diff changeset
332 * (The 'disabled' variable is writeable only within Cookies_init)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
333 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
334 static void Cookies_init()
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
335 {
1629
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
336 char *filename;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
337 #ifndef HAVE_LOCKF
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
338 struct flock lck;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
339 #endif
1607
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
340 struct tm future_tm = {7, 14, 3, 19, 0, 138, 0, 0, 0, 0, 0};
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
341
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
342 /* Default setting */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
343 disabled = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
344
1605
1dd07874017e cookies: use difftime() for time_t comparisons
corvid <corvid@lavabit.com>
parents: 1603
diff changeset
345 cookies_epoch_time = mktime(&cookies_epoch_tm);
1607
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
346 cookies_future_time = mktime(&future_tm);
1605
1dd07874017e cookies: use difftime() for time_t comparisons
corvid <corvid@lavabit.com>
parents: 1603
diff changeset
347
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
348 /* Read and parse the cookie control file (cookiesrc) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
349 if (Cookie_control_init() != 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
350 MSG("Disabling cookies.\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
351 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
352 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
353
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
354 /* Get a stream for the cookies file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
355 filename = dStrconcat(dGethomedir(), "/.dillo/cookies.txt", NULL);
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
356 file_stream = Cookies_fopen(filename, "r+", cookies_txt_header_str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
357
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
358 dFree(filename);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
359
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
360 if (!file_stream) {
1495
a83b066cbcb3 rm support for ancient ~/.dillo/cookies file
corvid <corvid@lavabit.com>
parents: 1491
diff changeset
361 MSG("ERROR: Can't open ~/.dillo/cookies.txt; disabling cookies\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
362 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
363 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
364
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
365 /* Try to get a lock from the file descriptor */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
366 #ifdef HAVE_LOCKF
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
367 disabled = (lockf(fileno(file_stream), F_TLOCK, 0) == -1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
368 #else /* POSIX lock */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
369 lck.l_start = 0; /* start at beginning of file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
370 lck.l_len = 0; /* lock entire file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
371 lck.l_type = F_WRLCK;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
372 lck.l_whence = SEEK_SET; /* absolute offset */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
373
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
374 disabled = (fcntl(fileno(file_stream), F_SETLK, &lck) == -1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
375 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
376 if (disabled) {
1495
a83b066cbcb3 rm support for ancient ~/.dillo/cookies file
corvid <corvid@lavabit.com>
parents: 1491
diff changeset
377 MSG("The cookies file has a file lock; disabling cookies!\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
378 fclose(file_stream);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
379 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
380 }
1495
a83b066cbcb3 rm support for ancient ~/.dillo/cookies file
corvid <corvid@lavabit.com>
parents: 1491
diff changeset
381 MSG("Enabling cookies as per cookiesrc...\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
382
1629
c6f07a1455bc Cookies_load_cookies()
corvid <corvid@lavabit.com>
parents: 1628
diff changeset
383 Cookies_load_cookies(file_stream);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
384 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
385
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
386 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
387 * Flush cookies to disk and free all the memory allocated.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
388 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
389 static void Cookies_save_and_free()
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
390 {
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
391 int i, fd, saved = 0;
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
392 DomainNode *node;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
393 CookieData_t *cookie;
1512
bd36920f32d4 don't bother to save expired cookies
corvid <corvid@lavabit.com>
parents: 1510
diff changeset
394 time_t now;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
395
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
396 #ifndef HAVE_LOCKF
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
397 struct flock lck;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
398 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
399
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
400 if (disabled)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
401 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
402
1512
bd36920f32d4 don't bother to save expired cookies
corvid <corvid@lavabit.com>
parents: 1510
diff changeset
403 now = time(NULL);
bd36920f32d4 don't bother to save expired cookies
corvid <corvid@lavabit.com>
parents: 1510
diff changeset
404
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
405 rewind(file_stream);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
406 fd = fileno(file_stream);
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
407 if (ftruncate(fd, 0) == -1)
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
408 MSG("Cookies: Truncate file stream failed: %s\n", dStrerror(errno));
454
c0d3c2ec7140 - Fixed a printf bug in cookies dpi.
jcid
parents: 445
diff changeset
409 fprintf(file_stream, "%s", cookies_txt_header_str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
410
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
411 /* Iterate cookies per domain, saving and freeing */
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
412 while ((node = dList_nth_data(domains, 0))) {
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
413 for (i = 0; (cookie = dList_nth_data(node->cookies, i)); ++i) {
1605
1dd07874017e cookies: use difftime() for time_t comparisons
corvid <corvid@lavabit.com>
parents: 1603
diff changeset
414 if (!cookie->session_only && difftime(cookie->expires_at, now) > 0) {
2034
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
415 int len;
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
416 char buf[LINE_MAXLEN];
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
417
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
418 len = snprintf(buf, LINE_MAXLEN, "%s\t%s\t%s\t%s\t%ld\t%s\t%s\n",
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
419 cookie->domain,
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
420 cookie->host_only ? "FALSE" : "TRUE",
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
421 cookie->path,
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
422 cookie->secure ? "TRUE" : "FALSE",
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
423 (long) difftime(cookie->expires_at,
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
424 cookies_epoch_time),
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
425 cookie->name,
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
426 cookie->value);
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
427 if (len < LINE_MAXLEN) {
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
428 fprintf(file_stream, "%s", buf);
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
429 saved++;
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
430 } else {
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
431 MSG("Not saving overly long cookie for %s.\n", cookie->domain);
7cad4651d972 don't save huge cookies
corvid <corvid@lavabit.com>
parents: 2030
diff changeset
432 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
433 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
434 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
435 }
1625
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
436 Cookies_delete_node(node);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
437 }
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
438 dList_free(domains);
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
439 dList_free(all_cookies);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
440
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
441 #ifdef HAVE_LOCKF
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
442 lockf(fd, F_ULOCK, 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
443 #else /* POSIX file lock */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
444 lck.l_start = 0; /* start at beginning of file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
445 lck.l_len = 0; /* lock entire file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
446 lck.l_type = F_UNLCK;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
447 lck.l_whence = SEEK_SET; /* absolute offset */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
448
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
449 fcntl(fileno(file_stream), F_SETLKW, &lck);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
450 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
451 fclose(file_stream);
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
452
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
453 MSG("Cookies saved: %d.\n", saved);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
454 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
455
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
456 /*
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
457 * Take a month's name and return a number between 0-11.
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
458 * E.g. 'April' -> 3
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
459 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
460 static int Cookies_get_month(const char *month_name)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
461 {
1500
335f1b3692c9 timestamps a little less strict
corvid <corvid@lavabit.com>
parents: 1499
diff changeset
462 static const char *const months[] =
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
463 { "Jan", "Feb", "Mar",
1500
335f1b3692c9 timestamps a little less strict
corvid <corvid@lavabit.com>
parents: 1499
diff changeset
464 "Apr", "May", "Jun",
335f1b3692c9 timestamps a little less strict
corvid <corvid@lavabit.com>
parents: 1499
diff changeset
465 "Jul", "Aug", "Sep",
335f1b3692c9 timestamps a little less strict
corvid <corvid@lavabit.com>
parents: 1499
diff changeset
466 "Oct", "Nov", "Dec"
335f1b3692c9 timestamps a little less strict
corvid <corvid@lavabit.com>
parents: 1499
diff changeset
467 };
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
468 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
469
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
470 for (i = 0; i < 12; i++) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
471 if (!dStrncasecmp(months[i], month_name, 3))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
472 return i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
473 }
1615
6f4cc0aa0d9b invalid value on invalid month
corvid <corvid@lavabit.com>
parents: 1614
diff changeset
474 return -1;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
475 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
476
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
477 /*
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
478 * Accept: RFC-1123 | RFC-850 | ANSI asctime | Old Netscape format date string.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
479 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
480 * Wdy, DD-Mon-YY HH:MM:SS GMT
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
481 * Wdy, DD-Mon-YYYY HH:MM:SS GMT
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
482 * Weekday, DD-Mon-YY HH:MM:SS GMT
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
483 * Weekday, DD-Mon-YYYY HH:MM:SS GMT
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
484 * Tue May 21 13:46:22 1991\n
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
485 * Tue May 21 13:46:22 1991
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
486 *
1500
335f1b3692c9 timestamps a little less strict
corvid <corvid@lavabit.com>
parents: 1499
diff changeset
487 * Let's add:
335f1b3692c9 timestamps a little less strict
corvid <corvid@lavabit.com>
parents: 1499
diff changeset
488 * Mon Jan 11 08:00:00 2010 GMT
335f1b3692c9 timestamps a little less strict
corvid <corvid@lavabit.com>
parents: 1499
diff changeset
489 *
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
490 * Return a pointer to a struct tm, or NULL on error.
1500
335f1b3692c9 timestamps a little less strict
corvid <corvid@lavabit.com>
parents: 1499
diff changeset
491 *
2030
382160be8c2f cookies comments
corvid <corvid@lavabit.com>
parents: 2025
diff changeset
492 * NOTE that the RFC wants user agents to be more flexible in what
1500
335f1b3692c9 timestamps a little less strict
corvid <corvid@lavabit.com>
parents: 1499
diff changeset
493 * they accept. For now, let's hack in special cases when they're encountered.
335f1b3692c9 timestamps a little less strict
corvid <corvid@lavabit.com>
parents: 1499
diff changeset
494 * Why? Because this function is currently understandable, and I don't want to
335f1b3692c9 timestamps a little less strict
corvid <corvid@lavabit.com>
parents: 1499
diff changeset
495 * abandon that (or at best decrease that -- see section 5.1.1) until there
335f1b3692c9 timestamps a little less strict
corvid <corvid@lavabit.com>
parents: 1499
diff changeset
496 * is known to be good reason.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
497 */
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
498 static struct tm *Cookies_parse_date(const char *date)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
499 {
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
500 struct tm *tm;
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
501 char *cp = strchr(date, ',');
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
502
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
503 if (!cp && strlen(date)>20 && date[13] == ':' && date[16] == ':') {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
504 /* Looks like ANSI asctime format... */
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
505 tm = dNew0(struct tm, 1);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
506
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
507 cp = (char *)date;
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
508 tm->tm_mon = Cookies_get_month(cp + 4);
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
509 tm->tm_mday = strtol(cp + 8, NULL, 10);
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
510 tm->tm_hour = strtol(cp + 11, NULL, 10);
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
511 tm->tm_min = strtol(cp + 14, NULL, 10);
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
512 tm->tm_sec = strtol(cp + 17, NULL, 10);
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
513 tm->tm_year = strtol(cp + 20, NULL, 10) - 1900;
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
514
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
515 } else if (cp && (cp - date == 3 || cp - date > 5) &&
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
516 (strlen(cp) == 24 || strlen(cp) == 26)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
517 /* RFC-1123 | RFC-850 format | Old Netscape format */
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
518 tm = dNew0(struct tm, 1);
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
519
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
520 tm->tm_mday = strtol(cp + 2, NULL, 10);
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
521 tm->tm_mon = Cookies_get_month(cp + 5);
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
522 tm->tm_year = strtol(cp + 9, &cp, 10);
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
523 /* tm_year is the number of years since 1900 */
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
524 if (tm->tm_year < 70)
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
525 tm->tm_year += 100;
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
526 else if (tm->tm_year > 100)
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
527 tm->tm_year -= 1900;
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
528 tm->tm_hour = strtol(cp + 1, NULL, 10);
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
529 tm->tm_min = strtol(cp + 4, NULL, 10);
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
530 tm->tm_sec = strtol(cp + 7, NULL, 10);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
531
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
532 } else {
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
533 tm = NULL;
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
534 MSG("In date \"%s\", format not understood.\n", date);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
535 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
536
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
537 /* Error checks. This may be overkill. */
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
538 if (tm &&
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
539 !(tm->tm_mday > 0 && tm->tm_mday < 32 && tm->tm_mon >= 0 &&
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
540 tm->tm_mon < 12 && tm->tm_year >= 70 && tm->tm_hour >= 0 &&
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
541 tm->tm_hour < 24 && tm->tm_min >= 0 && tm->tm_min < 60 &&
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
542 tm->tm_sec >= 0 && tm->tm_sec < 60)) {
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
543 MSG("Date \"%s\" values not in range.\n", date);
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
544 dFree(tm);
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
545 tm = NULL;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
546 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
547
1608
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
548 return tm;
5f8f8ce7a913 use tm when parsing date string
corvid <corvid@lavabit.com>
parents: 1607
diff changeset
549 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
550
1504
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
551 /*
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
552 * Find the least recently used cookie among those in the provided list.
1504
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
553 */
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
554 static CookieData_t *Cookies_get_LRU(Dlist *cookies)
1504
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
555 {
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
556 int i, n = dList_length(cookies);
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
557 CookieData_t *lru = dList_nth_data(cookies, 0);
1504
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
558
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
559 for (i = 1; i < n; i++) {
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
560 CookieData_t *curr = dList_nth_data(cookies, i);
1504
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
561
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
562 if (curr->last_used < lru->last_used)
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
563 lru = curr;
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
564 }
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
565 return lru;
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
566 }
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
567
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
568 /*
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
569 * Delete expired cookies.
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
570 * If node is given, only check those cookies.
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
571 * Note that nodes can disappear if all of their cookies were expired.
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
572 *
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
573 * Return the number of cookies that were expired.
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
574 */
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
575 static int Cookies_rm_expired_cookies(DomainNode *node)
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
576 {
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
577 Dlist *cookies = node ? node->cookies : all_cookies;
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
578 int removed = 0;
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
579 int i = 0, n = dList_length(cookies);
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
580 time_t now = time(NULL);
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
581
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
582 while (i < n) {
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
583 CookieData_t *c = dList_nth_data(cookies, i);
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
584
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
585 if (difftime(c->expires_at, now) < 0) {
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
586 DomainNode *currnode = node ? node :
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
587 dList_find_sorted(domains, c->domain, Domain_node_by_domain_cmp);
1630
00e22d98bce8 my testcases aren't smart enough for remove_fast on a domain's cookies
corvid <corvid@lavabit.com>
parents: 1629
diff changeset
588 dList_remove(currnode->cookies, c);
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
589 if (dList_length(currnode->cookies) == 0)
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
590 Cookies_delete_node(currnode);
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
591 dList_remove_fast(all_cookies, c);
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
592 Cookies_free_cookie(c);
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
593 n--;
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
594 removed++;
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
595 } else {
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
596 i++;
1504
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
597 }
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
598 }
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
599 return removed;
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
600 }
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
601
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
602 /*
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
603 * There are too many cookies. Choose one to remove and delete.
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
604 * If node is given, select from among its cookies only.
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
605 */
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
606 static void Cookies_too_many(DomainNode *node)
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
607 {
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
608 CookieData_t *lru = Cookies_get_LRU(node ? node->cookies : all_cookies);
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
609
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
610 MSG("Too many cookies!\n"
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
611 "Removing LRU cookie for \'%s\': \'%s=%s\'\n", lru->domain,
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
612 lru->name, lru->value);
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
613 if (!node)
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
614 node = dList_find_sorted(domains, lru->domain,Domain_node_by_domain_cmp);
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
615
1630
00e22d98bce8 my testcases aren't smart enough for remove_fast on a domain's cookies
corvid <corvid@lavabit.com>
parents: 1629
diff changeset
616 dList_remove(node->cookies, lru);
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
617 dList_remove_fast(all_cookies, lru);
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
618 Cookies_free_cookie(lru);
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
619 if (dList_length(node->cookies) == 0)
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
620 Cookies_delete_node(node);
1504
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
621 }
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
622
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
623 static void Cookies_add_cookie(CookieData_t *cookie)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
624 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
625 Dlist *domain_cookies;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
626 CookieData_t *c;
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
627 DomainNode *node;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
628
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
629 node = dList_find_sorted(domains, cookie->domain,Domain_node_by_domain_cmp);
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
630 domain_cookies = (node) ? node->cookies : NULL;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
631
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
632 if (domain_cookies) {
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
633 /* Remove any cookies with the same name, path, and host-only values. */
1519
6fcb13a8b680 Be careful not to delete domain_cookies while still using it.
corvid <corvid@lavabit.com>
parents: 1518
diff changeset
634 while ((c = dList_find_custom(domain_cookies, cookie, Cookies_cmp))) {
1630
00e22d98bce8 my testcases aren't smart enough for remove_fast on a domain's cookies
corvid <corvid@lavabit.com>
parents: 1629
diff changeset
635 dList_remove(domain_cookies, c);
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
636 dList_remove_fast(all_cookies, c);
1519
6fcb13a8b680 Be careful not to delete domain_cookies while still using it.
corvid <corvid@lavabit.com>
parents: 1518
diff changeset
637 Cookies_free_cookie(c);
1482
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
638 }
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
639 }
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
640
1614
d6624cbf9beb make a point of using -1 for invalid time_t
corvid <corvid@lavabit.com>
parents: 1613
diff changeset
641 if ((cookie->expires_at == (time_t) -1) ||
d6624cbf9beb make a point of using -1 for invalid time_t
corvid <corvid@lavabit.com>
parents: 1613
diff changeset
642 (difftime(cookie->expires_at, time(NULL)) <= 0)) {
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
643 /*
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
644 * Don't add an expired cookie. Whether expiring now == expired, exactly,
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
645 * is arguable, but we definitely do not want to add a Max-Age=0 cookie.
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
646 */
1614
d6624cbf9beb make a point of using -1 for invalid time_t
corvid <corvid@lavabit.com>
parents: 1613
diff changeset
647 _MSG("Goodbye, cookie %s=%s d:%s p:%s\n", cookie->name,
1546
afe20c3687f2 decrease cookies chattiness
corvid <corvid@lavabit.com>
parents: 1531
diff changeset
648 cookie->value, cookie->domain, cookie->path);
1482
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
649 Cookies_free_cookie(cookie);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
650 } else {
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
651 if (domain_cookies && dList_length(domain_cookies) >=MAX_DOMAIN_COOKIES){
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
652 int removed = Cookies_rm_expired_cookies(node);
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
653
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
654 if (removed == 0) {
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
655 Cookies_too_many(node);
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
656 } else if (removed >= MAX_DOMAIN_COOKIES) {
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
657 /* So many were removed that the node might have been deleted. */
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
658 node = dList_find_sorted(domains, cookie->domain,
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
659 Domain_node_by_domain_cmp);
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
660 domain_cookies = (node) ? node->cookies : NULL;
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
661 }
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
662 }
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
663 if (dList_length(all_cookies) >= MAX_TOTAL_COOKIES) {
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
664 if (Cookies_rm_expired_cookies(NULL) == 0) {
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
665 Cookies_too_many(NULL);
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
666 } else if (domain_cookies) {
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
667 /* Our own node might have just been deleted. */
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
668 node = dList_find_sorted(domains, cookie->domain,
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
669 Domain_node_by_domain_cmp);
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
670 domain_cookies = (node) ? node->cookies : NULL;
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
671 }
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
672 }
1626
fcf42389aa4d better handling when too many cookies
corvid <corvid@lavabit.com>
parents: 1625
diff changeset
673
1519
6fcb13a8b680 Be careful not to delete domain_cookies while still using it.
corvid <corvid@lavabit.com>
parents: 1518
diff changeset
674 cookie->last_used = cookies_use_counter++;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
675
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
676 /* Actually add the cookie! */
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
677 dList_append(all_cookies, cookie);
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
678
1519
6fcb13a8b680 Be careful not to delete domain_cookies while still using it.
corvid <corvid@lavabit.com>
parents: 1518
diff changeset
679 if (!domain_cookies) {
6fcb13a8b680 Be careful not to delete domain_cookies while still using it.
corvid <corvid@lavabit.com>
parents: 1518
diff changeset
680 domain_cookies = dList_new(5);
6fcb13a8b680 Be careful not to delete domain_cookies while still using it.
corvid <corvid@lavabit.com>
parents: 1518
diff changeset
681 dList_append(domain_cookies, cookie);
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
682 node = dNew(DomainNode, 1);
1519
6fcb13a8b680 Be careful not to delete domain_cookies while still using it.
corvid <corvid@lavabit.com>
parents: 1518
diff changeset
683 node->domain = dStrdup(cookie->domain);
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
684 node->cookies = domain_cookies;
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
685 dList_insert_sorted(domains, node, Domain_node_cmp);
1519
6fcb13a8b680 Be careful not to delete domain_cookies while still using it.
corvid <corvid@lavabit.com>
parents: 1518
diff changeset
686 } else {
6fcb13a8b680 Be careful not to delete domain_cookies while still using it.
corvid <corvid@lavabit.com>
parents: 1518
diff changeset
687 dList_append(domain_cookies, cookie);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
688 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
689 }
1625
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
690 if (domain_cookies && (dList_length(domain_cookies) == 0))
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
691 Cookies_delete_node(node);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
692 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
693
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
694 /*
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
695 * Return the attribute that is present at *cookie_str.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
696 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
697 static char *Cookies_parse_attr(char **cookie_str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
698 {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
699 char *str;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
700 uint_t len;
0
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 while (dIsspace(**cookie_str))
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
703 (*cookie_str)++;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
704
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
705 str = *cookie_str;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
706 /* 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
707 len = strcspn(str, "=;");
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
708 *cookie_str += len;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
709
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
710 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
711 len--;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
712 return dStrndup(str, len);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
713 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
714
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
715 /*
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
716 * Get the value in *cookie_str.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
717 */
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
718 static char *Cookies_parse_value(char **cookie_str)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
719 {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
720 uint_t len;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
721 char *str;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
722
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
723 if (**cookie_str == '=') {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
724 (*cookie_str)++;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
725 while (dIsspace(**cookie_str))
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
726 (*cookie_str)++;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
727
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
728 str = *cookie_str;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
729 /* 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
730 len = strcspn(str, ";");
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
731 *cookie_str += len;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
732
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
733 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
734 len--;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
735 } else {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
736 str = *cookie_str;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
737 len = 0;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
738 }
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
739 return dStrndup(str, len);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
740 }
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
741
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
742 /*
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
743 * Advance past any value
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
744 */
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
745 static void Cookies_eat_value(char **cookie_str)
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
746 {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
747 if (**cookie_str == '=')
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
748 *cookie_str += strcspn(*cookie_str, ";");
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
749 }
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
750
1610
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
751 /*
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
752 * Return the number of seconds by which our clock is ahead of the server's
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
753 * clock.
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
754 */
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
755 static double Cookies_server_timediff(const char *server_date)
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
756 {
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
757 double ret = 0;
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
758
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
759 if (server_date) {
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
760 struct tm *server_tm = Cookies_parse_date(server_date);
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
761
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
762 if (server_tm) {
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
763 time_t server_time = mktime(server_tm);
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
764
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
765 if (server_time != (time_t) -1)
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
766 ret = difftime(time(NULL), server_time);
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
767 dFree(server_tm);
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
768 }
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
769 }
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
770 return ret;
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
771 }
4b55c40d4eb9 separate out server time offset
corvid <corvid@lavabit.com>
parents: 1609
diff changeset
772
1609
afbbdad5f64a separate out unquoting
corvid <corvid@lavabit.com>
parents: 1608
diff changeset
773 static void Cookies_unquote_string(char *str)
afbbdad5f64a separate out unquoting
corvid <corvid@lavabit.com>
parents: 1608
diff changeset
774 {
afbbdad5f64a separate out unquoting
corvid <corvid@lavabit.com>
parents: 1608
diff changeset
775 if (str && str[0] == '\"') {
afbbdad5f64a separate out unquoting
corvid <corvid@lavabit.com>
parents: 1608
diff changeset
776 uint_t len = strlen(str);
afbbdad5f64a separate out unquoting
corvid <corvid@lavabit.com>
parents: 1608
diff changeset
777
afbbdad5f64a separate out unquoting
corvid <corvid@lavabit.com>
parents: 1608
diff changeset
778 if (len > 1 && str[len - 1] == '\"') {
afbbdad5f64a separate out unquoting
corvid <corvid@lavabit.com>
parents: 1608
diff changeset
779 str[len - 1] = '\0';
afbbdad5f64a separate out unquoting
corvid <corvid@lavabit.com>
parents: 1608
diff changeset
780 while ((*str = str[1]))
afbbdad5f64a separate out unquoting
corvid <corvid@lavabit.com>
parents: 1608
diff changeset
781 str++;
afbbdad5f64a separate out unquoting
corvid <corvid@lavabit.com>
parents: 1608
diff changeset
782 }
afbbdad5f64a separate out unquoting
corvid <corvid@lavabit.com>
parents: 1608
diff changeset
783 }
afbbdad5f64a separate out unquoting
corvid <corvid@lavabit.com>
parents: 1608
diff changeset
784 }
afbbdad5f64a separate out unquoting
corvid <corvid@lavabit.com>
parents: 1608
diff changeset
785
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
786 /*
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
787 * Parse cookie. A cookie might look something like:
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
788 * "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
789 */
1501
6fd1c55d8022 cookies use server date to interpret Expires attr
corvid <corvid@lavabit.com>
parents: 1500
diff changeset
790 static CookieData_t *Cookies_parse(char *cookie_str, const char *server_date)
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
791 {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
792 CookieData_t *cookie = NULL;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
793 char *str = cookie_str;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
794 bool_t first_attr = TRUE;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
795 bool_t max_age = FALSE;
1482
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
796 bool_t expires = FALSE;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
797
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
798 /* 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
799 while (*str) {
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
800 char *attr;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
801 char *value;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
802
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
803 /* Get attribute */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
804 attr = Cookies_parse_attr(&str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
805
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
806 /* 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
807 if (first_attr) {
1675
20e683b693c0 disallow nameless cookies
corvid <corvid@lavabit.com>
parents: 1634
diff changeset
808 if (*str != '=' || *attr == '\0') {
20e683b693c0 disallow nameless cookies
corvid <corvid@lavabit.com>
parents: 1634
diff changeset
809 /* disregard nameless cookie */
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
810 dFree(attr);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
811 return NULL;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
812 }
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
813 cookie = dNew0(CookieData_t, 1);
1675
20e683b693c0 disallow nameless cookies
corvid <corvid@lavabit.com>
parents: 1634
diff changeset
814 cookie->name = attr;
20e683b693c0 disallow nameless cookies
corvid <corvid@lavabit.com>
parents: 1634
diff changeset
815 cookie->value = Cookies_parse_value(&str);
1607
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
816
1675
20e683b693c0 disallow nameless cookies
corvid <corvid@lavabit.com>
parents: 1634
diff changeset
817 /* let's arbitrarily initialise with a year for now */
1607
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
818 time_t now = time(NULL);
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
819 struct tm *tm = gmtime(&now);
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
820 ++tm->tm_year;
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
821 cookie->expires_at = mktime(tm);
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
822 if (cookie->expires_at == (time_t) -1)
7dc68ea220cb cookies: start using struct tm for time calculations
corvid <corvid@lavabit.com>
parents: 1606
diff changeset
823 cookie->expires_at = cookies_future_time;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
824 } else if (dStrcasecmp(attr, "Path") == 0) {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
825 value = Cookies_parse_value(&str);
1491
a10148c0bcab cookies don't leak extra domain/path attrs
corvid <corvid@lavabit.com>
parents: 1490
diff changeset
826 dFree(cookie->path);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
827 cookie->path = value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
828 } else if (dStrcasecmp(attr, "Domain") == 0) {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
829 value = Cookies_parse_value(&str);
1491
a10148c0bcab cookies don't leak extra domain/path attrs
corvid <corvid@lavabit.com>
parents: 1490
diff changeset
830 dFree(cookie->domain);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
831 cookie->domain = value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
832 } else if (dStrcasecmp(attr, "Max-Age") == 0) {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
833 value = Cookies_parse_value(&str);
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
834 if (isdigit(*value) || *value == '-') {
1508
3a82485edd3f cookie handle time overflow
corvid <corvid@lavabit.com>
parents: 1506
diff changeset
835 time_t now = time(NULL);
3a82485edd3f cookie handle time overflow
corvid <corvid@lavabit.com>
parents: 1506
diff changeset
836 long age = strtol(value, NULL, 10);
1612
3eb2cd09244d use tm for max-age
corvid <corvid@lavabit.com>
parents: 1611
diff changeset
837 struct tm *tm = gmtime(&now);
1508
3a82485edd3f cookie handle time overflow
corvid <corvid@lavabit.com>
parents: 1506
diff changeset
838
1612
3eb2cd09244d use tm for max-age
corvid <corvid@lavabit.com>
parents: 1611
diff changeset
839 tm->tm_sec += age;
3eb2cd09244d use tm for max-age
corvid <corvid@lavabit.com>
parents: 1611
diff changeset
840 cookie->expires_at = mktime(tm);
3eb2cd09244d use tm for max-age
corvid <corvid@lavabit.com>
parents: 1611
diff changeset
841 if (age > 0 && cookie->expires_at == (time_t) -1) {
3eb2cd09244d use tm for max-age
corvid <corvid@lavabit.com>
parents: 1611
diff changeset
842 cookie->expires_at = cookies_future_time;
1508
3a82485edd3f cookie handle time overflow
corvid <corvid@lavabit.com>
parents: 1506
diff changeset
843 }
1691
a0337bdbec4a turn off some cookie MSGs
corvid <corvid@lavabit.com>
parents: 1675
diff changeset
844 _MSG("Cookie to expire at %s", ctime(&cookie->expires_at));
1482
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
845 expires = max_age = TRUE;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
846 }
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
847 dFree(value);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
848 } else if (dStrcasecmp(attr, "Expires") == 0) {
1482
a749c1b10fbe clean up cookie expiration/replacement and sessions
corvid <corvid@lavabit.com>
parents: 1481
diff changeset
849 if (!max_age) {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
850 value = Cookies_parse_value(&str);
1611
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
851 Cookies_unquote_string(value);
1691
a0337bdbec4a turn off some cookie MSGs
corvid <corvid@lavabit.com>
parents: 1675
diff changeset
852 _MSG("Expires attribute gives %s\n", value);
1611
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
853 struct tm *tm = Cookies_parse_date(value);
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
854 if (tm) {
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
855 tm->tm_sec += Cookies_server_timediff(server_date);
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
856 cookie->expires_at = mktime(tm);
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
857 if (cookie->expires_at == (time_t) -1 && tm->tm_year >= 138) {
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
858 /* Just checking tm_year does not ensure that the problem was
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
859 * inability to represent a distant date...
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
860 */
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
861 cookie->expires_at = cookies_future_time;
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
862 }
1691
a0337bdbec4a turn off some cookie MSGs
corvid <corvid@lavabit.com>
parents: 1675
diff changeset
863 _MSG("Cookie to expire at %s", ctime(&cookie->expires_at));
1611
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
864 dFree(tm);
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
865 } else {
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
866 cookie->expires_at = (time_t) -1;
0c8aeadcead3 get rid of the overflow
corvid <corvid@lavabit.com>
parents: 1610
diff changeset
867 }
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
868 expires = TRUE;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
869 dFree(value);
1613
0f26b1d4ac2e if ignoring Expires attr, eat value
corvid <corvid@lavabit.com>
parents: 1612
diff changeset
870 } else {
0f26b1d4ac2e if ignoring Expires attr, eat value
corvid <corvid@lavabit.com>
parents: 1612
diff changeset
871 Cookies_eat_value(&str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
872 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
873 } else if (dStrcasecmp(attr, "Secure") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
874 cookie->secure = TRUE;
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
875 Cookies_eat_value(&str);
418
b7265ec32e33 - Allow login to wikipedia by ignoring the HttpOnly attribute (cookies)
jcid
parents: 379
diff changeset
876 } else if (dStrcasecmp(attr, "HttpOnly") == 0) {
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
877 Cookies_eat_value(&str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
878 } else {
379
52f65d7a0e7c - minor cookie MSG improvement.
jcid
parents: 368
diff changeset
879 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
880 Cookies_eat_value(&str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
881 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
882
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
883 if (first_attr)
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
884 first_attr = FALSE;
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
885 else
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
886 dFree(attr);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
887
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
888 if (*str == ';')
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
889 str++;
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
890 }
1499
1803fe44871e cookies follow draft spec's simple parsing
corvid <corvid@lavabit.com>
parents: 1498
diff changeset
891 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
892 return cookie;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
893 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
894
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
895 /*
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
896 * Compare cookies by host_only, name, and path. Return 0 if equal.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
897 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
898 static int Cookies_cmp(const void *a, const void *b)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
899 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
900 const CookieData_t *ca = a, *cb = b;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
901
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
902 return (ca->host_only != cb->host_only) ||
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
903 (strcmp(ca->name, cb->name) != 0) ||
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
904 (strcmp(ca->path, cb->path) != 0);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
905 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
906
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
907 /*
1517
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
908 * Is the domain an IP address?
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
909 */
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
910 static bool_t Cookies_domain_is_ip(const char *domain)
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
911 {
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
912 uint_t len;
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
913
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
914 if (!domain)
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
915 return FALSE;
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
916
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
917 len = strlen(domain);
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
918
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
919 if (len == strspn(domain, "0123456789.")) {
1546
afe20c3687f2 decrease cookies chattiness
corvid <corvid@lavabit.com>
parents: 1531
diff changeset
920 _MSG("an IPv4 address\n");
1517
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
921 return TRUE;
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
922 }
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
923 if (*domain == '[' &&
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
924 (len == strspn(domain, "0123456789abcdefABCDEF:.[]"))) {
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
925 /* The precise format is shown in section 3.2.2 of rfc 3986 */
1546
afe20c3687f2 decrease cookies chattiness
corvid <corvid@lavabit.com>
parents: 1531
diff changeset
926 _MSG("an IPv6 address\n");
1517
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
927 return TRUE;
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
928 }
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
929 return FALSE;
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
930 }
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
931
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
932 /*
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
933 * Check whether url_path path-matches cookie_path
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
934 *
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
935 * 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
936 * but this is the recommended method at the moment.
1486
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
937 */
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
938 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
939 const char *cookie_path)
1486
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
940 {
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
941 bool_t ret = TRUE;
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
942
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
943 if (!url_path || !cookie_path) {
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
944 ret = FALSE;
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
945 } else {
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
946 uint_t c_len = strlen(cookie_path);
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
947 uint_t u_len = strlen(url_path);
1486
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
948
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
949 ret = (!strncmp(cookie_path, url_path, c_len) &&
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
950 ((c_len == u_len) ||
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
951 (c_len > 0 && cookie_path[c_len - 1] == '/') ||
1505
4f98690a951a unnecessary test
corvid <corvid@lavabit.com>
parents: 1504
diff changeset
952 (url_path[c_len] == '/')));
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
953 }
1486
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
954 return ret;
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
955 }
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
956
4fbeadf3191b cookie paths
corvid <corvid@lavabit.com>
parents: 1485
diff changeset
957 /*
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
958 * If cookie path is not properly set, remedy that.
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
959 */
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
960 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
961 {
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
962 if (!cookie->path || cookie->path[0] != '/') {
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
963 dFree(cookie->path);
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
964
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
965 if (url_path) {
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
966 uint_t len = strlen(url_path);
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
967
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
968 while (len && url_path[len] != '/')
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
969 len--;
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
970 cookie->path = dStrndup(url_path, len ? len : 1);
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
971 } else {
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
972 cookie->path = dStrdup("/");
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
973 }
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
974 }
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
975 }
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
976
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
977 /*
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
978 * 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
979 */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
980 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
981 {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
982 int diff;
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 if (!A || !*A || !B || !*B)
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
985 return FALSE;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
986
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
987 if (*B == '.')
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
988 B++;
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 /* 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
991 * 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
992 * don't, so: No.
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
993 */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
994
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
995 if (!dStrcasecmp(A, B))
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
996 return TRUE;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
997
1517
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
998 if (Cookies_domain_is_ip(B))
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
999 return FALSE;
773b44547931 cookies: be more robust in rejecting IP addr partial matches
corvid <corvid@lavabit.com>
parents: 1516
diff changeset
1000
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1001 diff = strlen(A) - strlen(B);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1002
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1003 if (diff > 0) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1004 /* 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
1005 return (dStrcasecmp(A + diff, B) == 0 && A[diff - 1] == '.');
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1006 } else {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1007 return FALSE;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1008 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1009 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1010
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1011 /*
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1012 * 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
1013 * 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
1014 * 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
1015 *
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1016 * 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
1017 * 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
1018 * reports.
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1019 */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1020 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
1021 {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1022 uint_t ret = 1;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1023
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1024 if (host) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1025 int start, after, tld_len;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1026
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1027 /* 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
1028 * 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
1029 */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1030 after = strlen(host);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1031 if (after > 0 && host[after - 1] == '.')
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1032 after--;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1033 start = after;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1034 while (start > 0 && host[start - 1] != '.')
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1035 start--;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1036 tld_len = after - start;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1037
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1038 if (tld_len > 0) {
1510
f08bcca10105 cookies update TLDs
corvid <corvid@lavabit.com>
parents: 1508
diff changeset
1039 /* These TLDs were chosen by examining the current publicsuffix list
f08bcca10105 cookies update TLDs
corvid <corvid@lavabit.com>
parents: 1508
diff changeset
1040 * in January 2010 and picking out those where it was simplest for
f08bcca10105 cookies update TLDs
corvid <corvid@lavabit.com>
parents: 1508
diff changeset
1041 * them to describe the situation by beginning with a "*.[tld]" rule.
f08bcca10105 cookies update TLDs
corvid <corvid@lavabit.com>
parents: 1508
diff changeset
1042 */
f08bcca10105 cookies update TLDs
corvid <corvid@lavabit.com>
parents: 1508
diff changeset
1043 const char *const tlds[] = {"ar","au","bd","bn","bt","ck","cy","do",
f08bcca10105 cookies update TLDs
corvid <corvid@lavabit.com>
parents: 1508
diff changeset
1044 "eg","er","et","fj","fk","gt","gu","id",
f08bcca10105 cookies update TLDs
corvid <corvid@lavabit.com>
parents: 1508
diff changeset
1045 "il","jm","ke","kh","kw","ml","mm","mt",
f08bcca10105 cookies update TLDs
corvid <corvid@lavabit.com>
parents: 1508
diff changeset
1046 "mz","ni","np","nz","om","pg","py","qa",
f08bcca10105 cookies update TLDs
corvid <corvid@lavabit.com>
parents: 1508
diff changeset
1047 "sv","tr","uk","uy","ve","ye","yu","za",
f08bcca10105 cookies update TLDs
corvid <corvid@lavabit.com>
parents: 1508
diff changeset
1048 "zm","zw"};
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1049 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
1050
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1051 for (i = 0; i < tld_num; i++) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1052 if (strlen(tlds[i]) == (uint_t) tld_len &&
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1053 !dStrncasecmp(tlds[i], host + start, tld_len)) {
1546
afe20c3687f2 decrease cookies chattiness
corvid <corvid@lavabit.com>
parents: 1531
diff changeset
1054 _MSG("TLD code matched %s\n", tlds[i]);
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1055 ret++;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1056 break;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1057 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1058 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1059 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1060 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1061 return ret;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1062 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1063
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1064 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1065 * Validate cookies domain against some security checks.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1066 */
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
1067 static bool_t Cookies_validate_domain(CookieData_t *cookie, char *host)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1068 {
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1069 uint_t i, internal_dots;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1070
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1071 if (!cookie->domain) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1072 cookie->domain = dStrdup(host);
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1073 cookie->host_only = TRUE;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1074 return TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1075 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1076
1488
afe771c1d19f cookie domains
corvid <corvid@lavabit.com>
parents: 1487
diff changeset
1077 if (!Cookies_domain_matches(host, cookie->domain))
afe771c1d19f cookie domains
corvid <corvid@lavabit.com>
parents: 1487
diff changeset
1078 return FALSE;
afe771c1d19f cookie domains
corvid <corvid@lavabit.com>
parents: 1487
diff changeset
1079
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1080 internal_dots = 0;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1081 for (i = 1; i < strlen(cookie->domain) - 1; i++) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1082 if (cookie->domain[i] == '.')
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1083 internal_dots++;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1084 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1085
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1086 /* All of this dots business is a weak hack.
1488
afe771c1d19f cookie domains
corvid <corvid@lavabit.com>
parents: 1487
diff changeset
1087 * TODO: accept the publicsuffix.org list as an optional external file.
afe771c1d19f cookie domains
corvid <corvid@lavabit.com>
parents: 1487
diff changeset
1088 */
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1089 if (internal_dots < Cookies_internal_dots_required(host)) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1090 MSG("not enough dots in %s\n", cookie->domain);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1091 return FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1092 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1093
1546
afe20c3687f2 decrease cookies chattiness
corvid <corvid@lavabit.com>
parents: 1531
diff changeset
1094 _MSG("host %s and domain %s is all right\n", host, cookie->domain);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1095 return TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1096 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1097
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1098 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1099 * Set the value corresponding to the cookie string
1531
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1100 * Return value: 0 set OK, -1 disabled, -2 denied, -3 rejected.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1101 */
1531
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1102 static int Cookies_set(char *cookie_string, char *url_host,
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1103 char *url_path, char *server_date)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1104 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1105 CookieControlAction action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1106 CookieData_t *cookie;
1531
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1107 int ret = -1;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1108
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1109 if (disabled)
1531
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1110 return ret;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1111
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1112 action = Cookies_control_check_domain(url_host);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1113 if (action == COOKIE_DENY) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1114 MSG("denied SET for %s\n", url_host);
1531
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1115 ret = -2;
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1116
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1117 } else {
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1118 MSG("%s SETTING: %s\n", url_host, cookie_string);
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1119 ret = -3;
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1120 if ((cookie = Cookies_parse(cookie_string, server_date))) {
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1121 if (Cookies_validate_domain(cookie, url_host)) {
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1122 Cookies_validate_path(cookie, url_path);
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1123 if (action == COOKIE_ACCEPT_SESSION)
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1124 cookie->session_only = TRUE;
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1125 Cookies_add_cookie(cookie);
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1126 ret = 0;
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1127 } else {
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1128 MSG("Rejecting cookie for domain %s from host %s path %s\n",
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1129 cookie->domain, url_host, url_path);
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1130 Cookies_free_cookie(cookie);
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1131 }
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1132 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1133 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1134
1531
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1135 return ret;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1136 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1137
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1138 /*
1503
f02eb59edc7d cookies: a little cleaning
corvid <corvid@lavabit.com>
parents: 1501
diff changeset
1139 * Compare the cookie with the supplied data to see whether it matches
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1140 */
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
1141 static bool_t Cookies_match(CookieData_t *cookie, const char *url_path,
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1142 bool_t host_only_val, bool_t is_ssl)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1143 {
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1144 if (cookie->host_only != host_only_val)
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1145 return FALSE;
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1146
2030
382160be8c2f cookies comments
corvid <corvid@lavabit.com>
parents: 2025
diff changeset
1147 /* Insecure cookies match both secure and insecure urls, secure
382160be8c2f cookies comments
corvid <corvid@lavabit.com>
parents: 2025
diff changeset
1148 cookies match only secure urls */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1149 if (cookie->secure && !is_ssl)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1150 return FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1151
1497
4ecebf934b44 cookies follow draft spec for paths
corvid <corvid@lavabit.com>
parents: 1496
diff changeset
1152 if (!Cookies_path_matches(url_path, cookie->path))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1153 return FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1154
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1155 /* It's a match */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1156 return TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1157 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1158
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1159 static void Cookies_add_matching_cookies(const char *domain,
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1160 const char *url_path,
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1161 bool_t host_only_val,
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1162 Dlist *matching_cookies,
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1163 bool_t is_ssl)
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1164 {
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
1165 DomainNode *node = dList_find_sorted(domains, domain,
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
1166 Domain_node_by_domain_cmp);
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1167 if (node) {
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1168 int i;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1169 CookieData_t *cookie;
1624
d7761a69d274 cookies some data structure renaming
corvid <corvid@lavabit.com>
parents: 1615
diff changeset
1170 Dlist *domain_cookies = node->cookies;
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1171
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1172 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
1173 /* Remove expired cookie. */
1605
1dd07874017e cookies: use difftime() for time_t comparisons
corvid <corvid@lavabit.com>
parents: 1603
diff changeset
1174 if (difftime(cookie->expires_at, time(NULL)) < 0) {
1546
afe20c3687f2 decrease cookies chattiness
corvid <corvid@lavabit.com>
parents: 1531
diff changeset
1175 _MSG("Goodbye, expired cookie %s=%s d:%s p:%s\n", cookie->name,
afe20c3687f2 decrease cookies chattiness
corvid <corvid@lavabit.com>
parents: 1531
diff changeset
1176 cookie->value, cookie->domain, cookie->path);
1630
00e22d98bce8 my testcases aren't smart enough for remove_fast on a domain's cookies
corvid <corvid@lavabit.com>
parents: 1629
diff changeset
1177 dList_remove(domain_cookies, cookie);
1628
ce89b1819729 limit total number of cookies
corvid <corvid@lavabit.com>
parents: 1627
diff changeset
1178 dList_remove_fast(all_cookies, cookie);
1519
6fcb13a8b680 Be careful not to delete domain_cookies while still using it.
corvid <corvid@lavabit.com>
parents: 1518
diff changeset
1179 Cookies_free_cookie(cookie);
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1180 --i; continue;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1181 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1182 /* Check if the cookie matches the requesting URL */
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1183 if (Cookies_match(cookie, url_path, host_only_val, is_ssl)) {
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1184 int j;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1185 CookieData_t *curr;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1186 uint_t path_length = strlen(cookie->path);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1187
1504
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
1188 cookie->last_used = cookies_use_counter;
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
1189
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1190 /* Longest cookies go first */
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1191 for (j = 0;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1192 (curr = dList_nth_data(matching_cookies, j)) &&
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1193 strlen(curr->path) >= path_length;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1194 j++) ;
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1195 dList_insert_pos(matching_cookies, cookie, j);
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1196 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1197 }
1519
6fcb13a8b680 Be careful not to delete domain_cookies while still using it.
corvid <corvid@lavabit.com>
parents: 1518
diff changeset
1198
1625
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
1199 if (dList_length(domain_cookies) == 0)
803c80da9d37 Cookies_delete_node
corvid <corvid@lavabit.com>
parents: 1624
diff changeset
1200 Cookies_delete_node(node);
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1201 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1202 }
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1203
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1204 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1205 * Return a string that contains all relevant cookies as headers.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1206 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
1207 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
1208 char *url_scheme)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1209 {
1496
5bec8ce33a42 cookies: rm version, comment, comment url, port, Cookie2
corvid <corvid@lavabit.com>
parents: 1495
diff changeset
1210 char *domain_str, *str;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1211 CookieData_t *cookie;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1212 Dlist *matching_cookies;
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1213 bool_t is_ssl, is_ip_addr, host_only_val;
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1214
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1215 Dstr *cookie_dstring;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1216 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1217
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1218 if (disabled)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1219 return dStrdup("");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1220
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1221 matching_cookies = dList_new(8);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1222
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1223 /* Check if the protocol is secure or not */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1224 is_ssl = (!dStrcasecmp(url_scheme, "https"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1225
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1226 is_ip_addr = Cookies_domain_is_ip(url_host);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1227
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1228 /* If a cookie is set that lacks a Domain attribute, its domain is set to
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1229 * the server's host and the host_only flag is set for that cookie. Such a
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1230 * cookie can only be sent back to that host. Cookies with Domain attrs do
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1231 * not have the host_only flag set, and may be sent to subdomains. Domain
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1232 * attrs can have leading dots, which should be ignored for matching
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1233 * purposes.
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1234 */
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1235 host_only_val = FALSE;
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1236 if (!is_ip_addr) {
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1237 /* e.g., sub.example.com set a cookie with domain ".sub.example.com". */
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1238 domain_str = dStrconcat(".", url_host, NULL);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1239 Cookies_add_matching_cookies(domain_str, url_path, host_only_val,
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1240 matching_cookies, is_ssl);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1241 dFree(domain_str);
1498
c04b0de69b00 cookies follow draft spec for domains
corvid <corvid@lavabit.com>
parents: 1497
diff changeset
1242 }
1632
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1243 host_only_val = TRUE;
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1244 /* e.g., sub.example.com set a cookie with no domain attribute. */
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1245 Cookies_add_matching_cookies(url_host, url_path, host_only_val,
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1246 matching_cookies, is_ssl);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1247 host_only_val = FALSE;
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1248 /* e.g., sub.example.com set a cookie with domain "sub.example.com". */
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1249 Cookies_add_matching_cookies(url_host, url_path, host_only_val,
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1250 matching_cookies, is_ssl);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1251
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1252 if (!is_ip_addr) {
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1253 for (domain_str = strchr(url_host+1, '.');
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1254 domain_str != NULL && *domain_str;
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1255 domain_str = strchr(domain_str+1, '.')) {
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1256 /* e.g., sub.example.com set a cookie with domain ".example.com". */
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1257 Cookies_add_matching_cookies(domain_str, url_path, host_only_val,
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1258 matching_cookies, is_ssl);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1259 if (domain_str[1]) {
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1260 domain_str++;
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1261 /* e.g., sub.example.com set a cookie with domain "example.com".*/
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1262 Cookies_add_matching_cookies(domain_str, url_path, host_only_val,
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1263 matching_cookies, is_ssl);
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1264 }
d76eddb72e6d cookies use host-only as in draft spec
corvid <corvid@lavabit.com>
parents: 1631
diff changeset
1265 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1266 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1267
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1268 /* Found the cookies, now make the string */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1269 cookie_dstring = dStr_new("");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1270 if (dList_length(matching_cookies) > 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1271
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1272 dStr_sprintfa(cookie_dstring, "Cookie: ");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1273
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1274 for (i = 0; (cookie = dList_nth_data(matching_cookies, i)); ++i) {
1675
20e683b693c0 disallow nameless cookies
corvid <corvid@lavabit.com>
parents: 1634
diff changeset
1275 dStr_sprintfa(cookie_dstring, "%s=%s", cookie->name, cookie->value);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1276 dStr_append(cookie_dstring,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1277 dList_length(matching_cookies) > i + 1 ? "; " : "\r\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1278 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1279 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1280
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1281 dList_free(matching_cookies);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1282 str = cookie_dstring->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1283 dStr_free(cookie_dstring, FALSE);
1504
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
1284
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
1285 if (*str)
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
1286 cookies_use_counter++;
d10adebe95fb when too many cookies for domain, rm least recently used
corvid <corvid@lavabit.com>
parents: 1503
diff changeset
1287
1506
408f86fed820 make cookies very chatty for now
corvid <corvid@lavabit.com>
parents: 1505
diff changeset
1288 MSG("%s GETTING: %s\n", url_host, str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1289 return str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1290 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1291
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1292 /* -------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1293 * Access control routines
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1294 * ------------------------------------------------------------- */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1295
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1296
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1297 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1298 * Get the cookie control rules (from cookiesrc).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1299 * Return value:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1300 * 0 = Parsed OK, with cookies enabled
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1301 * 1 = Parsed OK, with cookies disabled
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1302 * 2 = Can't open the control file
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1303 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1304 static int Cookie_control_init(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1305 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1306 CookieControl cc;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1307 FILE *stream;
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1308 char *filename, *rc;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1309 char line[LINE_MAXLEN];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1310 char domain[LINE_MAXLEN];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1311 char rule[LINE_MAXLEN];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1312 bool_t enabled = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1313
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1314 /* Get a file pointer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1315 filename = dStrconcat(dGethomedir(), "/.dillo/cookiesrc", NULL);
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
1316 stream = Cookies_fopen(filename, "r", "DEFAULT DENY\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1317 dFree(filename);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1318
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1319 if (!stream)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1320 return 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1321
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1322 /* Get all lines in the file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1323 while (!feof(stream)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1324 line[0] = '\0';
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1325 rc = fgets(line, LINE_MAXLEN, stream);
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1326 if (!rc && ferror(stream)) {
1503
f02eb59edc7d cookies: a little cleaning
corvid <corvid@lavabit.com>
parents: 1501
diff changeset
1327 MSG("Error while reading rule from cookiesrc: %s\n",
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1328 dStrerror(errno));
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1329 break; /* bail out */
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1330 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1331
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1332 /* Remove leading and trailing whitespaces */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1333 dStrstrip(line);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1334
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1335 if (line[0] != '\0' && line[0] != '#') {
1634
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1336 int i = 0, j = 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1337
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1338 /* Get the domain */
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1201
diff changeset
1339 while (line[i] != '\0' && !dIsspace(line[i]))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1340 domain[j++] = line[i++];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1341 domain[j] = '\0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1342
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1343 /* Skip past whitespaces */
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1201
diff changeset
1344 while (dIsspace(line[i]))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1345 i++;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1346
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1347 /* Get the rule */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1348 j = 0;
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1201
diff changeset
1349 while (line[i] != '\0' && !dIsspace(line[i]))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1350 rule[j++] = line[i++];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1351 rule[j] = '\0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1352
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1353 if (dStrcasecmp(rule, "ACCEPT") == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1354 cc.action = COOKIE_ACCEPT;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1355 else if (dStrcasecmp(rule, "ACCEPT_SESSION") == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1356 cc.action = COOKIE_ACCEPT_SESSION;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1357 else if (dStrcasecmp(rule, "DENY") == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1358 cc.action = COOKIE_DENY;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1359 else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1360 MSG("Cookies: rule '%s' for domain '%s' is not recognised.\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1361 rule, domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1362 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1363 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1364
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1365 cc.domain = dStrdup(domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1366 if (dStrcasecmp(cc.domain, "DEFAULT") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1367 /* Set the default action */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1368 default_action = cc.action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1369 dFree(cc.domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1370 } else {
1634
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1371 int i;
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1372 uint_t len = strlen(cc.domain);
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1373
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1374 /* Insert into list such that longest rules come first. */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1375 a_List_add(ccontrol, num_ccontrol, num_ccontrol_max);
1634
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1376 for (i = num_ccontrol++;
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1377 i > 0 && (len > strlen(ccontrol[i-1].domain));
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1378 i--) {
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1379 ccontrol[i] = ccontrol[i-1];
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1380 }
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1381 ccontrol[i] = cc;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1382 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1383
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1384 if (cc.action != COOKIE_DENY)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1385 enabled = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1386 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1387 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1388
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1389 fclose(stream);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1390
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1391 return (enabled ? 0 : 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1392 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1393
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1394 /*
1634
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1395 * Check the rules for an appropriate action for this domain.
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1396 * The rules are ordered by domain length, with longest first, so the
38b254933dbb use the most specific cookiesrc rule
corvid <corvid@lavabit.com>
parents: 1632
diff changeset
1397 * first match is the most specific.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1398 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1399 static CookieControlAction Cookies_control_check_domain(const char *domain)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1400 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1401 int i, diff;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1402
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1403 for (i = 0; i < num_ccontrol; i++) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1404 if (ccontrol[i].domain[0] == '.') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1405 diff = strlen(domain) - strlen(ccontrol[i].domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1406 if (diff >= 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1407 if (dStrcasecmp(domain + diff, ccontrol[i].domain) != 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1408 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1409 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1410 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1411 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1412 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1413 if (dStrcasecmp(domain, ccontrol[i].domain) != 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1414 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1415 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1416
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1417 /* If we got here we have a match */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1418 return( ccontrol[i].action );
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1419 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1420
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1421 return default_action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1422 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1423
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1424 /* -- Dpi parser ----------------------------------------------------------- */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1425
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1426 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1427 * Parse a data stream (dpi protocol)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1428 * Note: Buf is a zero terminated string
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1429 * Return code: { 0:OK, 1:Abort, 2:Close }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1430 */
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1431 static int srv_parse_tok(Dsh *sh, ClientInfo *client, char *Buf)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1432 {
1599
2b3aee52932a fix dpip cmd leak
corvid <corvid@lavabit.com>
parents: 1546
diff changeset
1433 char *cmd, *cookie, *host, *path;
1496
5bec8ce33a42 cookies: rm version, comment, comment url, port, Cookie2
corvid <corvid@lavabit.com>
parents: 1495
diff changeset
1434 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
1435 size_t BufSize = strlen(Buf);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1436
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1437 cmd = a_Dpip_get_attr_l(Buf, BufSize, "cmd");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1438
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1439 if (!cmd) {
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1440 /* abort */
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1441 } 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
1442 /* authenticate */
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1443 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
1444 client->status = 1;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1445 ret = 0;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1446 }
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1447 } else if (strcmp(cmd, "DpiBye") == 0) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1448 dFree(cmd);
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1449 MSG("(pid %d): Got DpiBye.\n", (int)getpid());
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1450 exit(0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1451
1599
2b3aee52932a fix dpip cmd leak
corvid <corvid@lavabit.com>
parents: 1546
diff changeset
1452 } else if (strcmp(cmd, "set_cookie") == 0) {
1531
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1453 int st;
1501
6fd1c55d8022 cookies use server date to interpret Expires attr
corvid <corvid@lavabit.com>
parents: 1500
diff changeset
1454 char *date;
6fd1c55d8022 cookies use server date to interpret Expires attr
corvid <corvid@lavabit.com>
parents: 1500
diff changeset
1455
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1456 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
1457 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
1458 path = a_Dpip_get_attr_l(Buf, BufSize, "path");
1501
6fd1c55d8022 cookies use server date to interpret Expires attr
corvid <corvid@lavabit.com>
parents: 1500
diff changeset
1459 date = a_Dpip_get_attr_l(Buf, BufSize, "date");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1460
1531
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1461 st = Cookies_set(cookie, host, path, date);
1599
2b3aee52932a fix dpip cmd leak
corvid <corvid@lavabit.com>
parents: 1546
diff changeset
1462
2b3aee52932a fix dpip cmd leak
corvid <corvid@lavabit.com>
parents: 1546
diff changeset
1463 dFree(cmd);
1531
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1464 cmd = a_Dpip_build_cmd("cmd=%s msg=%s", "set_cookie_answer",
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1465 st == 0 ? "ok" : "not set");
4810e433ed3f dpi send response when setting cookies
Jorge Arellano Cid <jcid@dillo.org>
parents: 1527
diff changeset
1466 a_Dpip_dsh_write_str(sh, 1, cmd);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1467
1501
6fd1c55d8022 cookies use server date to interpret Expires attr
corvid <corvid@lavabit.com>
parents: 1500
diff changeset
1468 dFree(date);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1469 dFree(path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1470 dFree(host);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1471 dFree(cookie);
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1472 ret = 2;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1473
1599
2b3aee52932a fix dpip cmd leak
corvid <corvid@lavabit.com>
parents: 1546
diff changeset
1474 } else if (strcmp(cmd, "get_cookie") == 0) {
2b3aee52932a fix dpip cmd leak
corvid <corvid@lavabit.com>
parents: 1546
diff changeset
1475 char *scheme = a_Dpip_get_attr_l(Buf, BufSize, "scheme");
2b3aee52932a fix dpip cmd leak
corvid <corvid@lavabit.com>
parents: 1546
diff changeset
1476
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1477 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
1478 path = a_Dpip_get_attr_l(Buf, BufSize, "path");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1479
1496
5bec8ce33a42 cookies: rm version, comment, comment url, port, Cookie2
corvid <corvid@lavabit.com>
parents: 1495
diff changeset
1480 cookie = Cookies_get(host, path, scheme);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1481 dFree(scheme);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1482 dFree(path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1483 dFree(host);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1484
1599
2b3aee52932a fix dpip cmd leak
corvid <corvid@lavabit.com>
parents: 1546
diff changeset
1485 dFree(cmd);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1486 cmd = a_Dpip_build_cmd("cmd=%s cookie=%s", "get_cookie_answer", cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1487
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1488 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
1489 ret = 1;
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1490 } else {
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1491 _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
1492 ret = 2;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1493 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1494 dFree(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1495 }
1599
2b3aee52932a fix dpip cmd leak
corvid <corvid@lavabit.com>
parents: 1546
diff changeset
1496 dFree(cmd);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1497
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1498 return ret;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1499 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1500
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1501 /* -- Termination handlers ----------------------------------------------- */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1502 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1503 * (was to delete the local namespace socket),
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1504 * but this is handled by 'dpid' now.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1505 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1506 static void cleanup(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1507 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1508 Cookies_save_and_free();
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1509 MSG("cleanup\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1510 /* no more cleanup required */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1511 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1512
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1513 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1514 * Perform any necessary cleanups upon abnormal termination
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1515 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1516 static void termination_handler(int signum)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1517 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1518 exit(signum);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1519 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1520
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1521
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1522 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1523 * -- MAIN -------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1524 */
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
1525 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
1526 struct sockaddr_in sin;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1527 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
1528 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
1529 int sock_fd, code;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1530 char *buf;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1531 Dsh *sh;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1532
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1533 /* Arrange the cleanup function for terminations via exit() */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1534 atexit(cleanup);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1535
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1536 /* Arrange the cleanup function for abnormal terminations */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1537 if (signal (SIGINT, termination_handler) == SIG_IGN)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1538 signal (SIGINT, SIG_IGN);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1539 if (signal (SIGHUP, termination_handler) == SIG_IGN)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1540 signal (SIGHUP, SIG_IGN);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1541 if (signal (SIGTERM, termination_handler) == SIG_IGN)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1542 signal (SIGTERM, SIG_IGN);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1543
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1544 Cookies_init();
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1545 MSG("(v.1) accepting connections...\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1546
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1547 if (disabled)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1548 exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1549
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1550 /* 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
1551 address_size = sizeof(struct sockaddr_in);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1552
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1553 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
1554 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
1555 if (sock_fd == -1) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1556 perror("[accept]");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1557 exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1558 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1559
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1560 /* 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
1561 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
1562 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
1563 client->sh = sh;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1564 client->status = 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1565
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1566 while (1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1567 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
1568 if ((buf = a_Dpip_dsh_read_token(sh, 1)) != NULL) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1569 /* Let's see what we fished... */
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1570 _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
1571 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
1572 dFree(buf);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1573 }
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1574
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1575 _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
1576 if (code == 1) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1577 exit(1);
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1578 } else if (code == 2) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1579 break;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1580 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1581 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1582
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1583 _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
1584 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
1585 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
1586 dFree(client);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1587
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1588 }/*while*/
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1589
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1590 return 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1591 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1592
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1593 #endif /* !DISABLE_COOKIES */