annotate dpi/cookies.c @ 1387:16cf380cd04c

Convert dpid, file dpi and cookies dpi to dsh API cookies: convert to dsh API (with auth) file dpi: convert to dsh API (with auth) dpid: convert to dsh API (with auth) handle EINTR in connect(), authenticated DpiBye cmd
author Jorge Arellano Cid <jcid@dillo.org>
date Sun, 01 Nov 2009 16:31:59 -0300
parents eb98997886ec
children eb35203124e4
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
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 /* Handling of cookies takes place here.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 * This implementation aims to follow RFC 2965:
235
480d368c4b2f - Updated some old URLs.
jcid
parents: 219
diff changeset
18 * http://www.ietf.org/rfc/rfc2965.txt
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22 * TODO: Cleanup this code. Shorten some functions, order things,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23 * add comments, remove leaks, etc.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25
368
2242da885677 - s/todo:/TODO:/g
jcid
parents: 235
diff changeset
26 /* TODO: this server is not assembling the received packets.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 * This means it currently expects dillo to send full dpi tags
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28 * within the socket; if that fails, everything stops.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31 #ifdef DISABLE_COOKIES
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33 int main(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35 return 0; /* never called */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
36 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 #else
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 #include <sys/types.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 #include <sys/socket.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 #include <sys/stat.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 #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
45 #include <netinet/in.h>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 #include <fcntl.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 #include <unistd.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 #include <errno.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 #include <stddef.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 #include <stdlib.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 #include <stdio.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53 #include <time.h> /* for time() and time_t */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 #include <ctype.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 #include <netdb.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 #include <signal.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 #include "dpiutil.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58 #include "../dpip/dpip.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 * Debugging macros
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64 #define _MSG(...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65 #define MSG(...) printf("[cookies dpi]: " __VA_ARGS__)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69 * a_List_add()
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 * Make sure there's space for 'num_items' items within the list
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 * (First, allocate an 'alloc_step' sized chunk, after that, double the
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 * list size --to make it faster)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 #define a_List_add(list,num_items,alloc_step) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 if (!list) { \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 list = dMalloc(alloc_step * sizeof((*list))); \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 } \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 if (num_items >= alloc_step){ \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 while ( num_items >= alloc_step ) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 alloc_step <<= 1; \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 list = dRealloc(list, alloc_step * sizeof((*list))); \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 /* The maximum length of a line in the cookie file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 #define LINE_MAXLEN 4096
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 typedef enum {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 COOKIE_ACCEPT,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 COOKIE_ACCEPT_SESSION,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 COOKIE_DENY
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 } CookieControlAction;
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;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96 CookieControlAction action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 } CookieControl;
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 *domain;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 Dlist *dlist;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 } CookieNode;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 typedef struct {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 char *name;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 char *value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 char *domain;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 char *path;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 time_t expires_at;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 uint_t version;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 char *comment;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 char *comment_url;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 bool_t secure;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 bool_t session_only;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 Dlist *ports;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 } CookieData_t;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
118 typedef struct {
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
119 Dsh *sh;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
120 int status;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
121 } ClientInfo;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
122
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 * Local data
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 /* List of CookieNode. Each node holds a domain and its list of cookies */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 static Dlist *cookies;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 /* Variables for access control */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 static CookieControl *ccontrol = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 static int num_ccontrol = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 static int num_ccontrol_max = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 static CookieControlAction default_action = COOKIE_DENY;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136 static bool_t disabled;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 static FILE *file_stream;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 static char *cookies_txt_header_str =
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 "# HTTP Cookie File\n"
235
480d368c4b2f - Updated some old URLs.
jcid
parents: 219
diff changeset
140 "# http://wp.netscape.com/newsref/std/cookie_spec.html\n"
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 "# This is a generated file! Do not edit.\n\n";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 * Forward declarations
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148 static CookieControlAction Cookies_control_check_domain(const char *domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 static int Cookie_control_init(void);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 static void Cookies_parse_ports(int url_port, CookieData_t *cookie,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 const char *port_str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 static char *Cookies_build_ports_str(CookieData_t *cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153 static char *Cookies_strip_path(const char *path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 static void Cookies_add_cookie(CookieData_t *cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 static void Cookies_remove_cookie(CookieData_t *cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 static int Cookies_cmp(const void *a, const void *b);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 * Compare function for searching a cookie node
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
161 static int Cookie_node_cmp(const void *v1, const void *v2)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 const CookieNode *n1 = v1, *n2 = v2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165 return strcmp(n1->domain, n2->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
166 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
167
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
168 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
169 * Compare function for searching a cookie node by domain
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
170 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
171 static int Cookie_node_by_domain_cmp(const void *v1, const void *v2)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
172 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
173 const CookieNode *node = v1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
174 const char *domain = v2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
175
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
176 return strcmp(node->domain, domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
177 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
178
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
179 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
180 * Return a file pointer. If the file doesn't exist, try to create it,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
181 * with the optional 'init_str' as its content.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
182 */
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
183 static FILE *Cookies_fopen(const char *filename, const char *mode,
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
184 char *init_str)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
185 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
186 FILE *F_in;
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
187 int fd, rc;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
189 if ((F_in = fopen(filename, mode)) == NULL) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
190 /* Create the file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
191 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
192 if (fd != -1) {
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
193 if (init_str) {
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
194 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
195 if (rc == -1) {
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
196 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
197 filename, dStrerror(errno));
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
198 }
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
199 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
200 close(fd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
201
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
202 MSG("Created file: %s\n", filename);
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
203 F_in = fopen(filename, mode);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
204 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
205 MSG("Could not create file: %s!\n", filename);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
206 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
207 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
208
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
209 if (F_in) {
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
210 /* set close on exec */
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
211 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
212 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
213
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
214 return F_in;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
215 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
216
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
217 static void Cookies_free_cookie(CookieData_t *cookie)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
218 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
219 dFree(cookie->name);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
220 dFree(cookie->value);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
221 dFree(cookie->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
222 dFree(cookie->path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
223 dFree(cookie->comment);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
224 dFree(cookie->comment_url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
225 dList_free(cookie->ports);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
226 dFree(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
227 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
228
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
229 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
230 * Initialize the cookies module
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
231 * (The 'disabled' variable is writable only within Cookies_init)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
232 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
233 static void Cookies_init()
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
234 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
235 CookieData_t *cookie;
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
236 char *filename, *rc = NULL;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
237 char line[LINE_MAXLEN];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
238 #ifndef HAVE_LOCKF
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
239 struct flock lck;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
240 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
241 FILE *old_cookies_file_stream;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
242
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
243 /* Default setting */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
244 disabled = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
245
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
246 /* Read and parse the cookie control file (cookiesrc) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
247 if (Cookie_control_init() != 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
248 MSG("Disabling cookies.\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
249 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
250 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
251
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
252 /* Get a stream for the cookies file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
253 filename = dStrconcat(dGethomedir(), "/.dillo/cookies.txt", NULL);
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
254 file_stream = Cookies_fopen(filename, "r+", cookies_txt_header_str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
255
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
256 dFree(filename);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
257
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
258 if (!file_stream) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
259 MSG("ERROR: Can't open ~/.dillo/cookies.txt, disabling cookies\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
260 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
261 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
262
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
263 /* Try to get a lock from the file descriptor */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
264 #ifdef HAVE_LOCKF
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
265 disabled = (lockf(fileno(file_stream), F_TLOCK, 0) == -1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
266 #else /* POSIX lock */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
267 lck.l_start = 0; /* start at beginning of file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
268 lck.l_len = 0; /* lock entire file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
269 lck.l_type = F_WRLCK;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
270 lck.l_whence = SEEK_SET; /* absolute offset */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
271
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
272 disabled = (fcntl(fileno(file_stream), F_SETLK, &lck) == -1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
273 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
274 if (disabled) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
275 MSG("The cookies file has a file lock: disabling cookies!\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
276 fclose(file_stream);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
277 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
278 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
279
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
280 MSG("Enabling cookies as from cookiesrc...\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
281
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
282 cookies = dList_new(32);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
283
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
284 /* Get all lines in the file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
285 while (!feof(file_stream)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
286 line[0] = '\0';
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
287 rc = fgets(line, LINE_MAXLEN, file_stream);
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
288 if (!rc && ferror(file_stream)) {
1201
cabde55d0e05 fix cookies dpi MSGs
corvid <corvid@lavabit.com>
parents: 1200
diff changeset
289 MSG("Cookies1: Error while reading from cookies.txt: %s\n",
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
290 dStrerror(errno));
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
291 break; /* bail out */
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
292 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
293
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
294 /* Remove leading and trailing whitespaces */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
295 dStrstrip(line);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
296
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
297 if ((line[0] != '\0') && (line[0] != '#')) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
298 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
299 * Split the row into pieces using a tab as the delimiter.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
300 * pieces[0] The domain name
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
301 * pieces[1] TRUE/FALSE: is the domain a suffix, or a full domain?
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
302 * pieces[2] The path
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
303 * pieces[3] Is the cookie unsecure or secure (TRUE/FALSE)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
304 * pieces[4] Timestamp of expire date
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
305 * pieces[5] Name of the cookie
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
306 * pieces[6] Value of the cookie
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
307 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
308 CookieControlAction action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
309 char *piece;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
310 char *line_marker = line;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
311
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
312 cookie = dNew0(CookieData_t, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
313
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
314 cookie->session_only = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
315 cookie->version = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
316 cookie->domain = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
317 dStrsep(&line_marker, "\t"); /* we use domain always as sufix */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
318 cookie->path = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
319 piece = dStrsep(&line_marker, "\t");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
320 if (piece != NULL && piece[0] == 'T')
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
321 cookie->secure = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
322 piece = dStrsep(&line_marker, "\t");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
323 if (piece != NULL)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
324 cookie->expires_at = (time_t) strtol(piece, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
325 cookie->name = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
326 cookie->value = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
327
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
328 if (!cookie->domain || cookie->domain[0] == '\0' ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
329 !cookie->path || cookie->path[0] != '/' ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
330 !cookie->name || cookie->name[0] == '\0' ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
331 !cookie->value) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
332 MSG("Malformed line in cookies.txt file!\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
333 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
334 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
335 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
336
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
337 action = Cookies_control_check_domain(cookie->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
338 if (action == COOKIE_DENY) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
339 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
340 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
341 } else if (action == COOKIE_ACCEPT_SESSION) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
342 cookie->session_only = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
343 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
344
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
345 /* Save cookie in memory */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
346 Cookies_add_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
347 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
348 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
349
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
350 filename = dStrconcat(dGethomedir(), "/.dillo/cookies", NULL);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
351 if ((old_cookies_file_stream = fopen(filename, "r")) != NULL) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
352 MSG("WARNING: Reading old cookies file ~/.dillo/cookies too\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
353
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
354 /* Get all lines in the file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
355 while (!feof(old_cookies_file_stream)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
356 line[0] = '\0';
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
357 rc = fgets(line, LINE_MAXLEN, old_cookies_file_stream);
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
358 if (!rc && ferror(old_cookies_file_stream)) {
1201
cabde55d0e05 fix cookies dpi MSGs
corvid <corvid@lavabit.com>
parents: 1200
diff changeset
359 MSG("Cookies2: Error while reading from cookies file: %s\n",
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
360 dStrerror(errno));
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
361 break; /* bail out */
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
362 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
363
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
364 /* Remove leading and trailing whitespaces */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
365 dStrstrip(line);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
366
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
367 if (line[0] != '\0') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
368 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
369 * Split the row into pieces using a tab as the delimiter.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
370 * pieces[0] The version this cookie was set as (0 / 1)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
371 * pieces[1] The domain name
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
372 * pieces[2] A comma separated list of accepted ports
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
373 * pieces[3] The path
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
374 * pieces[4] Is the cookie unsecure or secure (0 / 1)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
375 * pieces[5] Timestamp of expire date
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
376 * pieces[6] Name of the cookie
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
377 * pieces[7] Value of the cookie
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
378 * pieces[8] Comment
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
379 * pieces[9] Comment url
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
380 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
381 CookieControlAction action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
382 char *piece;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
383 char *line_marker = line;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
384
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
385 cookie = dNew0(CookieData_t, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
386
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
387 cookie->session_only = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
388 piece = dStrsep(&line_marker, "\t");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
389 if (piece != NULL)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
390 cookie->version = strtol(piece, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
391 cookie->domain = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
392 Cookies_parse_ports(0, cookie, dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
393 cookie->path = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
394 piece = dStrsep(&line_marker, "\t");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
395 if (piece != NULL && piece[0] == '1')
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
396 cookie->secure = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
397 piece = dStrsep(&line_marker, "\t");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
398 if (piece != NULL)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
399 cookie->expires_at = (time_t) strtol(piece, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
400 cookie->name = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
401 cookie->value = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
402 cookie->comment = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
403 cookie->comment_url = dStrdup(dStrsep(&line_marker, "\t"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
404
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
405 if (!cookie->domain || cookie->domain[0] == '\0' ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
406 !cookie->path || cookie->path[0] != '/' ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
407 !cookie->name || cookie->name[0] == '\0' ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
408 !cookie->value) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
409 MSG("Malformed line in cookies file!\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
410 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
411 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
412 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
413
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
414 action = Cookies_control_check_domain(cookie->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
415 if (action == COOKIE_DENY) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
416 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
417 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
418 } else if (action == COOKIE_ACCEPT_SESSION) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
419 cookie->session_only = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
420 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
421
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
422 /* Save cookie in memory */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
423 Cookies_add_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
424 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
425 }
154
055164434caa - Added a couple Dstr_append_l calls (necessary for binary strings).
jcid
parents: 51
diff changeset
426 fclose(old_cookies_file_stream);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
427 }
154
055164434caa - Added a couple Dstr_append_l calls (necessary for binary strings).
jcid
parents: 51
diff changeset
428 dFree(filename);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
429 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
430
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
431 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
432 * Flush cookies to disk and free all the memory allocated.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
433 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
434 static void Cookies_save_and_free()
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
435 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
436 int i, fd;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
437 CookieNode *node;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
438 CookieData_t *cookie;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
439
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
440 #ifndef HAVE_LOCKF
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
441 struct flock lck;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
442 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
443
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
444 if (disabled)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
445 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
446
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
447 rewind(file_stream);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
448 fd = fileno(file_stream);
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
449 if (ftruncate(fd, 0) == -1)
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
450 MSG("Cookies: Truncate file stream failed: %s\n", dStrerror(errno));
454
c0d3c2ec7140 - Fixed a printf bug in cookies dpi.
jcid
parents: 445
diff changeset
451 fprintf(file_stream, "%s", cookies_txt_header_str);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
452
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
453 /* Iterate cookies per domain, saving and freeing */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
454 while ((node = dList_nth_data(cookies, 0))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
455 for (i = 0; (cookie = dList_nth_data(node->dlist, i)); ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
456 if (!cookie->session_only) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
457 /* char * ports_str = Cookies_build_ports_str(cookie); */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
458 fprintf(file_stream, "%s\tTRUE\t%s\t%s\t%ld\t%s\t%s\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
459 cookie->domain,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
460 cookie->path,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
461 cookie->secure ? "TRUE" : "FALSE",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
462 (long)cookie->expires_at,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
463 cookie->name,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
464 cookie->value);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
465 /* dFree(ports_str); */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
466 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
467
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
468 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
469 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
470 dList_remove(cookies, node);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
471 dFree(node->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
472 dList_free(node->dlist);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
473 dFree(node);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
474 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
475
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
476 #ifdef HAVE_LOCKF
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
477 lockf(fd, F_ULOCK, 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
478 #else /* POSIX file lock */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
479 lck.l_start = 0; /* start at beginning of file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
480 lck.l_len = 0; /* lock entire file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
481 lck.l_type = F_UNLCK;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
482 lck.l_whence = SEEK_SET; /* absolute offset */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
483
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
484 fcntl(fileno(file_stream), F_SETLKW, &lck);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
485 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
486 fclose(file_stream);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
487 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
488
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
489 static char *months[] =
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
490 { "",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
491 "Jan", "Feb", "Mar",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
492 "Apr", "May", "Jun",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
493 "Jul", "Aug", "Sep",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
494 "Oct", "Nov", "Dec"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
495 };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
496
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
497 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
498 * Take a months name and return a number between 1-12.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
499 * E.g. 'April' -> 4
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
500 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
501 static int Cookies_get_month(const char *month_name)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
502 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
503 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
504
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
505 for (i = 1; i <= 12; i++) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
506 if (!dStrncasecmp(months[i], month_name, 3))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
507 return i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
508 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
509 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
510 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
511
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
512 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
513 * Return a local timestamp from a GMT date string
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
514 * Accept: RFC-1123 | RFC-850 | ANSI asctime | Old Netscape format.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
515 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
516 * Wdy, DD-Mon-YY HH:MM:SS GMT
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
517 * Wdy, DD-Mon-YYYY HH:MM:SS GMT
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
518 * Weekday, DD-Mon-YY HH:MM:SS GMT
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
519 * Weekday, DD-Mon-YYYY HH:MM:SS GMT
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
520 * Tue May 21 13:46:22 1991\n
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
521 * Tue May 21 13:46:22 1991
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
522 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
523 * (return 0 on malformed date string syntax)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
524 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
525 static time_t Cookies_create_timestamp(const char *expires)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
526 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
527 time_t ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
528 int day, month, year, hour, minutes, seconds;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
529 char *cp;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
530 char *E_msg =
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
531 "Expire date is malformed!\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
532 " (should be RFC-1123 | RFC-850 | ANSI asctime)\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
533 " Ignoring cookie: ";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
534
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
535 cp = strchr(expires, ',');
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
536 if (!cp && (strlen(expires) == 24 || strlen(expires) == 25)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
537 /* Looks like ANSI asctime format... */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
538 cp = (char *)expires;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
539 day = strtol(cp + 8, NULL, 10); /* day */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
540 month = Cookies_get_month(cp + 4); /* month */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
541 year = strtol(cp + 20, NULL, 10); /* year */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
542 hour = strtol(cp + 11, NULL, 10); /* hour */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
543 minutes = strtol(cp + 14, NULL, 10); /* minutes */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
544 seconds = strtol(cp + 17, NULL, 10); /* seconds */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
545
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
546 } else if (cp && (cp - expires == 3 || cp - expires > 5) &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
547 (strlen(cp) == 24 || strlen(cp) == 26)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
548 /* RFC-1123 | RFC-850 format | Old Netscape format */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
549 day = strtol(cp + 2, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
550 month = Cookies_get_month(cp + 5);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
551 year = strtol(cp + 9, &cp, 10);
368
2242da885677 - s/todo:/TODO:/g
jcid
parents: 235
diff changeset
552 /* TODO: tricky, because two digits for year IS ambiguous! */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
553 year += (year < 70) ? 2000 : ((year < 100) ? 1900 : 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
554 hour = strtol(cp + 1, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
555 minutes = strtol(cp + 4, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
556 seconds = strtol(cp + 7, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
557
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
558 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
559 MSG("%s%s\n", E_msg, expires);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
560 return (time_t) 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
561 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
562
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
563 /* Error checks --this may be overkill */
1127
65c7e33e4466 allow year 1970 in cookies
corvid <corvid@lavabit.com>
parents: 454
diff changeset
564 if (!(day > 0 && day < 32 && month > 0 && month < 13 && year >= 1970 &&
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
565 hour >= 0 && hour < 24 && minutes >= 0 && minutes < 60 &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
566 seconds >= 0 && seconds < 60)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
567 MSG("%s%s\n", E_msg, expires);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
568 return (time_t) 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
569 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
570
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
571 /* Calculate local timestamp.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
572 * [stolen from Lynx... (http://lynx.browser.org)] */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
573 month -= 3;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
574 if (month < 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
575 month += 12;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
576 year--;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
577 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
578
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
579 day += (year - 1968) * 1461 / 4;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
580 day += ((((month * 153) + 2) / 5) - 672);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
581 ret = (time_t)((day * 60 * 60 * 24) +
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
582 (hour * 60 * 60) +
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
583 (minutes * 60) +
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
584 seconds);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
585
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
586 MSG("Expires in %ld seconds, at %s",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
587 (long)ret - time(NULL), ctime(&ret));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
588
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
589 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
590 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
591
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
592 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
593 * Parse a string containing a list of port numbers.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
594 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
595 static void Cookies_parse_ports(int url_port, CookieData_t *cookie,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
596 const char *port_str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
597 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
598 if ((!port_str || !port_str[0]) && url_port != 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
599 /* There was no list, so only the calling urls port should be allowed. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
600 if (!cookie->ports)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
601 cookie->ports = dList_new(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
602 dList_append(cookie->ports, INT2VOIDP(url_port));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
603 } else if (port_str[0] == '"' && port_str[1] != '"') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
604 char *tok, *str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
605 int port;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
606
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
607 str = dStrdup(port_str + 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
608 while ((tok = dStrsep(&str, ","))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
609 port = strtol(tok, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
610 if (port > 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
611 if (!cookie->ports)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
612 cookie->ports = dList_new(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
613 dList_append(cookie->ports, INT2VOIDP(port));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
614 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
615 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
616 dFree(str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
617 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
618 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
619
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
620 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
621 * Build a string of the ports in 'cookie'.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
622 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
623 static char *Cookies_build_ports_str(CookieData_t *cookie)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
624 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
625 Dstr *dstr;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
626 char *ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
627 void *data;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
628 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
629
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
630 dstr = dStr_new("\"");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
631 for (i = 0; (data = dList_nth_data(cookie->ports, i)); ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
632 dStr_sprintfa(dstr, "%d,", VOIDP2INT(data));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
633 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
634 /* Remove any trailing comma */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
635 if (dstr->len > 1)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
636 dStr_erase(dstr, dstr->len - 1, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
637 dStr_append(dstr, "\"");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
638
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
639 ret = dstr->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
640 dStr_free(dstr, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
641
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
642 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
643 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
644
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
645 static void Cookies_add_cookie(CookieData_t *cookie)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
646 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
647 Dlist *domain_cookies;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
648 CookieData_t *c;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
649 CookieNode *node;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
650
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
651 /* Don't add an expired cookie */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
652 if (!cookie->session_only && cookie->expires_at < time(NULL)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
653 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
654 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
655 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
656
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
657 node = dList_find_sorted(cookies, cookie->domain,Cookie_node_by_domain_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
658 domain_cookies = (node) ? node->dlist : NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
659
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
660 if (domain_cookies) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
661 /* Respect the limit of 20 cookies per domain */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
662 if (dList_length(domain_cookies) >= 20) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
663 MSG("There are too many cookies for this domain (%s)\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
664 cookie->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
665 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
666 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
667 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
668
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
669 /* Remove any cookies with the same name and path */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
670 while ((c = dList_find_custom(domain_cookies, cookie, Cookies_cmp))){
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
671 Cookies_remove_cookie(c);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
672 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
673 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
674
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
675 /* add the cookie into the respective domain list */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
676 node = dList_find_sorted(cookies, cookie->domain,Cookie_node_by_domain_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
677 domain_cookies = (node) ? node->dlist : NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
678 if (!domain_cookies) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
679 domain_cookies = dList_new(5);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
680 dList_append(domain_cookies, cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
681 node = dNew(CookieNode, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
682 node->domain = dStrdup(cookie->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
683 node->dlist = domain_cookies;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
684 dList_insert_sorted(cookies, node, Cookie_node_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
685 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
686 dList_append(domain_cookies, cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
687 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
688 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
689
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
690 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
691 * Remove the cookie from the domain list.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
692 * If the domain list is empty, remove the node too.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
693 * Free the cookie.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
694 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
695 static void Cookies_remove_cookie(CookieData_t *cookie)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
696 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
697 CookieNode *node;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
698
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
699 node = dList_find_sorted(cookies, cookie->domain,Cookie_node_by_domain_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
700 if (node) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
701 dList_remove(node->dlist, cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
702 if (dList_length(node->dlist) == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
703 dList_remove(cookies, node);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
704 dFree(node->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
705 dList_free(node->dlist);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
706 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
707 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
708 MSG("Attempting to remove a cookie that doesn't exist!\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
709 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
710
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
711 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
712 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
713
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
714 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
715 * Return the attribute that is present at *cookie_str. This function
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
716 * will also attempt to advance cookie_str past any equal-sign.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
717 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
718 static char *Cookies_parse_attr(char **cookie_str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
719 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
720 char *str = *cookie_str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
721 uint_t i, end = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
722 bool_t got_attr = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
723
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
724 for (i = 0; ; i++) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
725 switch (str[i]) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
726 case ' ':
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
727 case '\t':
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
728 case '=':
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
729 case ';':
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
730 got_attr = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
731 if (end == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
732 end = i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
733 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
734 case ',':
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
735 *cookie_str = str + i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
736 return dStrndup(str, i);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
737 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
738 case '\0':
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
739 if (!got_attr) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
740 end = i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
741 got_attr = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
742 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
743 /* fall through! */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
744 default:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
745 if (got_attr) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
746 *cookie_str = str + i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
747 return dStrndup(str, end);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
748 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
749 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
750 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
751 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
752
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
753 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
754 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
755
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
756 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
757 * Get the value starting at *cookie_str.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
758 * broken_syntax: watch out for stupid syntax (comma in unquoted string...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
759 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
760 static char *Cookies_parse_value(char **cookie_str,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
761 bool_t broken_syntax,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
762 bool_t keep_quotes)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
763 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
764 uint_t i, end;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
765 char *str = *cookie_str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
766
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
767 for (i = end = 0; !end; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
768 switch (str[i]) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
769 case ' ':
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
770 case '\t':
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
771 if (!broken_syntax && str[0] != '\'' && str[0] != '"') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
772 *cookie_str = str + i + 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
773 end = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
774 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
775 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
776 case '\'':
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
777 case '"':
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
778 if (i != 0 && str[i] == str[0]) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
779 char *tmp = str + i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
780
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
781 while (*tmp != '\0' && *tmp != ';' && *tmp != ',')
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
782 tmp++;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
783
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
784 *cookie_str = (*tmp == ';') ? tmp + 1 : tmp;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
785
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
786 if (keep_quotes)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
787 i++;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
788 end = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
789 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
790 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
791 case '\0':
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
792 *cookie_str = str + i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
793 end = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
794 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
795 case ',':
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
796 if (str[0] != '\'' && str[0] != '"' && !broken_syntax) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
797 /* A new cookie starts here! */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
798 *cookie_str = str + i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
799 end = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
800 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
801 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
802 case ';':
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
803 if (str[0] != '\'' && str[0] != '"') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
804 *cookie_str = str + i + 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
805 end = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
806 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
807 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
808 default:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
809 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
810 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
811 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
812 /* keep i as an index to the last char */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
813 --i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
814
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
815 if ((str[0] == '\'' || str[0] == '"') && !keep_quotes) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
816 return i > 1 ? dStrndup(str + 1, i - 1) : NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
817 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
818 return dStrndup(str, i);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
819 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
820 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
821
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
822 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
823 * Parse one cookie...
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
824 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
825 static CookieData_t *Cookies_parse_one(int url_port, char **cookie_str)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
826 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
827 CookieData_t *cookie;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
828 char *str = *cookie_str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
829 char *attr;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
830 char *value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
831 int num_attr = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
832 bool_t max_age = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
833 bool_t discard = FALSE;
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
834 bool_t error = FALSE;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
835
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
836 cookie = dNew0(CookieData_t, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
837 cookie->session_only = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
838
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
839 /* Iterate until there is nothing left of the string OR we come
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
840 * across a comma representing the start of another cookie */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
841 while (*str != '\0' && *str != ',') {
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
842 if (error) {
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
843 str++;
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
844 continue;
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
845 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
846 /* Skip whitespace */
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1201
diff changeset
847 while (dIsspace(*str))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
848 str++;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
849
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
850 /* Get attribute */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
851 attr = Cookies_parse_attr(&str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
852 if (!attr) {
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
853 MSG("Cannot parse cookie attribute!\n");
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
854 error = TRUE;
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
855 continue;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
856 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
857
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
858 /* Get the value for the attribute and store it */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
859 if (num_attr == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
860 /* The first attr, which always is the user supplied attr, may
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
861 * have the same name as an ordinary attr. Hence this workaround. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
862 cookie->name = dStrdup(attr);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
863 cookie->value = Cookies_parse_value(&str, FALSE, TRUE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
864 } else if (dStrcasecmp(attr, "Path") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
865 value = Cookies_parse_value(&str, FALSE, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
866 cookie->path = value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
867 } else if (dStrcasecmp(attr, "Domain") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
868 value = Cookies_parse_value(&str, FALSE, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
869 cookie->domain = value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
870 } else if (dStrcasecmp(attr, "Discard") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
871 cookie->session_only = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
872 discard = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
873 } else if (dStrcasecmp(attr, "Max-Age") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
874 if (!discard) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
875 value = Cookies_parse_value(&str, FALSE, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
876
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
877 if (value) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
878 cookie->expires_at = time(NULL) + strtol(value, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
879 cookie->session_only = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
880 max_age = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
881 dFree(value);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
882 } else {
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
883 MSG("Cannot parse cookie Max-Age value!\n");
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
884 dFree(attr);
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
885 error = TRUE;
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
886 continue;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
887 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
888 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
889 } else if (dStrcasecmp(attr, "Expires") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
890 if (!max_age && !discard) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
891 MSG("Old netscape-style cookie...\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
892 value = Cookies_parse_value(&str, TRUE, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
893 if (value) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
894 cookie->expires_at = Cookies_create_timestamp(value);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
895 cookie->session_only = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
896 dFree(value);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
897 } else {
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
898 MSG("Cannot parse cookie Expires value!\n");
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
899 dFree(attr);
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
900 error = TRUE;
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
901 continue;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
902 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
903 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
904 } else if (dStrcasecmp(attr, "Port") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
905 value = Cookies_parse_value(&str, FALSE, TRUE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
906 Cookies_parse_ports(url_port, cookie, value);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
907 dFree(value);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
908 } else if (dStrcasecmp(attr, "Comment") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
909 value = Cookies_parse_value(&str, FALSE, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
910 cookie->comment = value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
911 } else if (dStrcasecmp(attr, "CommentURL") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
912 value = Cookies_parse_value(&str, FALSE, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
913 cookie->comment_url = value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
914 } else if (dStrcasecmp(attr, "Version") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
915 value = Cookies_parse_value(&str, FALSE, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
916
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
917 if (value) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
918 cookie->version = strtol(value, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
919 dFree(value);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
920 } else {
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
921 MSG("Cannot parse cookie Version value!\n");
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
922 dFree(attr);
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
923 error = TRUE;
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
924 continue;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
925 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
926 } else if (dStrcasecmp(attr, "Secure") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
927 cookie->secure = TRUE;
418
b7265ec32e33 - Allow login to wikipedia by ignoring the HttpOnly attribute (cookies)
jcid
parents: 379
diff changeset
928 } else if (dStrcasecmp(attr, "HttpOnly") == 0) {
419
jcid
parents: 418
diff changeset
929 // this case is intentionally left blank, because we do not
jcid
parents: 418
diff changeset
930 // do client-side scripting (yet).
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
931 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
932 /* Oops! this can't be good... */
379
52f65d7a0e7c - minor cookie MSG improvement.
jcid
parents: 368
diff changeset
933 MSG("Cookie contains unknown attribute: '%s'\n", attr);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
934 dFree(attr);
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
935 error = TRUE;
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
936 continue;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
937 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
938
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
939 dFree(attr);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
940 num_attr++;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
941 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
942
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
943 *cookie_str = (*str == ',') ? str + 1 : str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
944
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
945 if (!error && (!cookie->name || !cookie->value)) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
946 MSG("Cookie missing name and/or value!\n");
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
947 error = TRUE;
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
948 }
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
949 if (error) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
950 Cookies_free_cookie(cookie);
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
951 cookie = NULL;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
952 }
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
953 return cookie;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
954 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
955
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
956 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
957 * Iterate the cookie string until we catch all cookies.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
958 * Return Value: a list with all the cookies! (or NULL upon error)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
959 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
960 static Dlist *Cookies_parse_string(int url_port, char *cookie_string)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
961 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
962 CookieData_t *cookie;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
963 Dlist *ret = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
964 char *str = cookie_string;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
965
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
966 /* The string may contain several cookies separated by comma.
159
70da40b41e3a - Fixed a bug in Cookies_parse_one. Set it to a single return potint too!
jcid
parents: 154
diff changeset
967 * We'll iterate until we've caught them all */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
968 while (*str) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
969 cookie = Cookies_parse_one(url_port, &str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
970
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
971 if (cookie) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
972 if (!ret)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
973 ret = dList_new(4);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
974 dList_append(ret, cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
975 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
976 MSG("Malformed cookie field, ignoring cookie: %s\n", cookie_string);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
977 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
978 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
979
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
980 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
981 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
982
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
983 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
984 * Compare cookies by name and path (return 0 if equal)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
985 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
986 static int Cookies_cmp(const void *a, const void *b)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
987 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
988 const CookieData_t *ca = a, *cb = b;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
989 int ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
990
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
991 if (!(ret = strcmp(ca->name, cb->name)))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
992 ret = strcmp(ca->path, cb->path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
993 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
994 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
995
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
996 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
997 * Validate cookies domain against some security checks.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
998 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
999 static bool_t Cookies_validate_domain(CookieData_t *cookie, char *host,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1000 char *url_path)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1001 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1002 int dots, diff, i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1003 bool_t is_ip;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1004
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1005 /* Make sure that the path is set to something */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1006 if (!cookie->path || cookie->path[0] != '/') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1007 dFree(cookie->path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1008 cookie->path = Cookies_strip_path(url_path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1009 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1010
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1011 /* If the server never set a domain, or set one without a leading
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1012 * dot (which isn't allowed), we use the calling URL's hostname. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1013 if (cookie->domain == NULL || cookie->domain[0] != '.') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1014 dFree(cookie->domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1015 cookie->domain = dStrdup(host);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1016 return TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1017 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1018
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1019 /* Count the number of dots and also find out if it is an IP-address */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1020 is_ip = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1021 for (i = 0, dots = 0; cookie->domain[i] != '\0'; i++) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1022 if (cookie->domain[i] == '.')
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1023 dots++;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1024 else if (!isdigit(cookie->domain[i]))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1025 is_ip = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1026 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1027
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1028 /* A valid domain must have at least two dots in it */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1029 /* NOTE: this breaks cookies on localhost... */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1030 if (dots < 2) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1031 return FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1032 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1033
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1034 /* Now see if the url matches the domain */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1035 diff = strlen(host) - i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1036 if (diff > 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1037 if (dStrcasecmp(host + diff, cookie->domain))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1038 return FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1039
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1040 if (!is_ip) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1041 /* "x.y.test.com" is not allowed to set cookies for ".test.com";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1042 * only an url of the form "y.test.com" would be. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1043 while ( diff-- )
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1044 if (host[diff] == '.')
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1045 return FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1046 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1047 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1048
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1049 return TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1050 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1051
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1052 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1053 * Strip of the filename from a full path
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1054 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1055 static char *Cookies_strip_path(const char *path)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1056 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1057 char *ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1058 uint_t len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1059
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1060 if (path) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1061 len = strlen(path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1062
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1063 while (len && path[len] != '/')
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1064 len--;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1065 ret = dStrndup(path, len + 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1066 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1067 ret = dStrdup("/");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1068 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1069
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1070 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1071 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1072
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1073 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1074 * Set the value corresponding to the cookie string
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1075 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
1076 static void Cookies_set(char *cookie_string, char *url_host,
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
1077 char *url_path, int url_port)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1078 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1079 CookieControlAction action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1080 CookieData_t *cookie;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1081 Dlist *list;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1082 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1083
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1084 if (disabled)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1085 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1086
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1087 action = Cookies_control_check_domain(url_host);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1088 if (action == COOKIE_DENY) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1089 MSG("denied SET for %s\n", url_host);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1090 return;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1091 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1092
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1093 if ((list = Cookies_parse_string(url_port, cookie_string))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1094 for (i = 0; (cookie = dList_nth_data(list, i)); ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1095 if (Cookies_validate_domain(cookie, url_host, url_path)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1096 if (action == COOKIE_ACCEPT_SESSION)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1097 cookie->session_only = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1098 Cookies_add_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1099 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1100 MSG("Rejecting cookie for %s from host %s path %s\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1101 cookie->domain, url_host, url_path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1102 Cookies_free_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1103 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1104 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1105 dList_free(list);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1106 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1107 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1108
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1109 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1110 * Compare the cookie with the supplied data to see if it matches
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1111 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1112 static bool_t Cookies_match(CookieData_t *cookie, int port,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1113 const char *path, bool_t is_ssl)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1114 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1115 void *data;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1116 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1117
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1118 /* Insecure cookies matches both secure and insecure urls, secure
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1119 cookies matches only secure urls */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1120 if (cookie->secure && !is_ssl)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1121 return FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1122
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1123 /* Check that the cookie path is a subpath of the current path */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1124 if (strncmp(cookie->path, path, strlen(cookie->path)) != 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1125 return FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1126
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1127 /* Check if the port of the request URL matches any
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1128 * of those set in the cookie */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1129 if (cookie->ports) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1130 for (i = 0; (data = dList_nth_data(cookie->ports, i)); ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1131 if (VOIDP2INT(data) == port)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1132 return TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1133 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1134 return FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1135 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1136
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1137 /* It's a match */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1138 return TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1139 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1140
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1141 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1142 * Return a string that contains all relevant cookies as headers.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1143 */
51
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
1144 static char *Cookies_get(char *url_host, char *url_path,
6ff7b6758e0c Added the "static" qualifier where missing.
jcid
parents: 35
diff changeset
1145 char *url_scheme, int url_port)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1146 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1147 char *domain_str, *q, *str, *path;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1148 CookieData_t *cookie;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1149 Dlist *matching_cookies;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1150 CookieNode *node;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1151 Dlist *domain_cookies;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1152 bool_t is_ssl;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1153 Dstr *cookie_dstring;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1154 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1155
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1156 if (disabled)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1157 return dStrdup("");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1158
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1159 matching_cookies = dList_new(8);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1160
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1161 path = Cookies_strip_path(url_path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1162
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1163 /* Check if the protocol is secure or not */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1164 is_ssl = (!dStrcasecmp(url_scheme, "https"));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1165
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1166 for (domain_str = (char *) url_host;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1167 domain_str != NULL && *domain_str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1168 domain_str = strchr(domain_str+1, '.')) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1169
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1170 node = dList_find_sorted(cookies, domain_str, Cookie_node_by_domain_cmp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1171 domain_cookies = (node) ? node->dlist : NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1172
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1173 for (i = 0; (cookie = dList_nth_data(domain_cookies, i)); ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1174 /* Remove expired cookie. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1175 if (!cookie->session_only && cookie->expires_at < time(NULL)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1176 Cookies_remove_cookie(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1177 --i; continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1178 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1179 /* Check if the cookie matches the requesting URL */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1180 if (Cookies_match(cookie, url_port, path, is_ssl)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1181 dList_append(matching_cookies, cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1182 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1183 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1184 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1185
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1186 /* Found the cookies, now make the string */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1187 cookie_dstring = dStr_new("");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1188 if (dList_length(matching_cookies) > 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1189 CookieData_t *first_cookie = dList_nth_data(matching_cookies, 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1190
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1191 dStr_sprintfa(cookie_dstring, "Cookie: ");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1192
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1193 if (first_cookie->version != 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1194 dStr_sprintfa(cookie_dstring, "$Version=\"%d\"; ",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1195 first_cookie->version);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1196
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1197
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1198 for (i = 0; (cookie = dList_nth_data(matching_cookies, i)); ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1199 q = (cookie->version == 0 ? "" : "\"");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1200 dStr_sprintfa(cookie_dstring,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1201 "%s=%s; $Path=%s%s%s; $Domain=%s%s%s",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1202 cookie->name, cookie->value,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1203 q, cookie->path, q, q, cookie->domain, q);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1204 if (cookie->ports) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1205 char *ports_str = Cookies_build_ports_str(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1206 dStr_sprintfa(cookie_dstring, "; $Port=%s", ports_str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1207 dFree(ports_str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1208 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1209
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1210 dStr_append(cookie_dstring,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1211 dList_length(matching_cookies) > i + 1 ? "; " : "\r\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1212 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1213 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1214
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1215 dList_free(matching_cookies);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1216 dFree(path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1217 str = cookie_dstring->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1218 dStr_free(cookie_dstring, FALSE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1219 return str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1220 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1221
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1222 /* -------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1223 * Access control routines
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1224 * ------------------------------------------------------------- */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1225
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1226
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1227 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1228 * Get the cookie control rules (from cookiesrc).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1229 * Return value:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1230 * 0 = Parsed OK, with cookies enabled
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1231 * 1 = Parsed OK, with cookies disabled
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1232 * 2 = Can't open the control file
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1233 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1234 static int Cookie_control_init(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1235 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1236 CookieControl cc;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1237 FILE *stream;
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1238 char *filename, *rc;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1239 char line[LINE_MAXLEN];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1240 char domain[LINE_MAXLEN];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1241 char rule[LINE_MAXLEN];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1242 int i, j;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1243 bool_t enabled = FALSE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1244
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1245 /* Get a file pointer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1246 filename = dStrconcat(dGethomedir(), "/.dillo/cookiesrc", NULL);
219
201d89d1d002 - Allowed readonly permissions for cookiesrc.
jcid
parents: 174
diff changeset
1247 stream = Cookies_fopen(filename, "r", "DEFAULT DENY\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1248 dFree(filename);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1249
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1250 if (!stream)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1251 return 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1252
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1253 /* Get all lines in the file */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1254 while (!feof(stream)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1255 line[0] = '\0';
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1256 rc = fgets(line, LINE_MAXLEN, stream);
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1257 if (!rc && ferror(stream)) {
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1258 MSG("Cookies3: Error while reading rule from cookiesrc: %s\n",
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1259 dStrerror(errno));
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1260 break; /* bail out */
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 1127
diff changeset
1261 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1262
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1263 /* Remove leading and trailing whitespaces */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1264 dStrstrip(line);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1265
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1266 if (line[0] != '\0' && line[0] != '#') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1267 i = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1268 j = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1269
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1270 /* Get the domain */
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1201
diff changeset
1271 while (line[i] != '\0' && !dIsspace(line[i]))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1272 domain[j++] = line[i++];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1273 domain[j] = '\0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1274
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1275 /* Skip past whitespaces */
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1201
diff changeset
1276 while (dIsspace(line[i]))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1277 i++;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1278
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1279 /* Get the rule */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1280 j = 0;
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1201
diff changeset
1281 while (line[i] != '\0' && !dIsspace(line[i]))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1282 rule[j++] = line[i++];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1283 rule[j] = '\0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1284
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1285 if (dStrcasecmp(rule, "ACCEPT") == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1286 cc.action = COOKIE_ACCEPT;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1287 else if (dStrcasecmp(rule, "ACCEPT_SESSION") == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1288 cc.action = COOKIE_ACCEPT_SESSION;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1289 else if (dStrcasecmp(rule, "DENY") == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1290 cc.action = COOKIE_DENY;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1291 else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1292 MSG("Cookies: rule '%s' for domain '%s' is not recognised.\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1293 rule, domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1294 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1295 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1296
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1297 cc.domain = dStrdup(domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1298 if (dStrcasecmp(cc.domain, "DEFAULT") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1299 /* Set the default action */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1300 default_action = cc.action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1301 dFree(cc.domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1302 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1303 a_List_add(ccontrol, num_ccontrol, num_ccontrol_max);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1304 ccontrol[num_ccontrol++] = cc;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1305 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1306
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1307 if (cc.action != COOKIE_DENY)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1308 enabled = TRUE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1309 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1310 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1311
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1312 fclose(stream);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1313
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1314 return (enabled ? 0 : 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1315 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1316
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1317 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1318 * Check the rules for an appropriate action for this domain
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1319 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1320 static CookieControlAction Cookies_control_check_domain(const char *domain)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1321 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1322 int i, diff;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1323
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1324 for (i = 0; i < num_ccontrol; i++) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1325 if (ccontrol[i].domain[0] == '.') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1326 diff = strlen(domain) - strlen(ccontrol[i].domain);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1327 if (diff >= 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1328 if (dStrcasecmp(domain + diff, ccontrol[i].domain) != 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1329 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1330 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1331 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1332 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1333 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1334 if (dStrcasecmp(domain, ccontrol[i].domain) != 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1335 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1336 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1337
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1338 /* If we got here we have a match */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1339 return( ccontrol[i].action );
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1340 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1341
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1342 return default_action;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1343 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1344
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1345 /* -- Dpi parser ----------------------------------------------------------- */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1346
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1347 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1348 * Parse a data stream (dpi protocol)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1349 * Note: Buf is a zero terminated string
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1350 * Return code: { 0:OK, 1:Abort, 2:Close }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1351 */
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1352 static int srv_parse_tok(Dsh *sh, ClientInfo *client, char *Buf)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1353 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1354 char *p, *cmd, *cookie, *host, *path, *scheme;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1355 int port, ret = 1;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1356 size_t BufSize = strlen(Buf);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1357
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1358 cmd = a_Dpip_get_attr_l(Buf, BufSize, "cmd");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1359
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1360 if (!cmd) {
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1361 /* abort */
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1362 } 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
1363 /* authenticate */
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1364 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
1365 client->status = 1;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1366 ret = 0;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1367 }
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1368 } else if (strcmp(cmd, "DpiBye") == 0) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1369 dFree(cmd);
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1370 MSG("(pid %d): Got DpiBye.\n", (int)getpid());
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1371 exit(0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1372
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1373 } else if (cmd && strcmp(cmd, "set_cookie") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1374 dFree(cmd);
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1375 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
1376 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
1377 path = a_Dpip_get_attr_l(Buf, BufSize, "path");
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1378 p = a_Dpip_get_attr_l(Buf, BufSize, "port");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1379 port = strtol(p, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1380 dFree(p);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1381
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1382 Cookies_set(cookie, host, path, port);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1383
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1384 dFree(path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1385 dFree(host);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1386 dFree(cookie);
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1387 ret = 2;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1388
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1389 } else if (cmd && strcmp(cmd, "get_cookie") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1390 dFree(cmd);
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1391 scheme = a_Dpip_get_attr_l(Buf, BufSize, "scheme");
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1392 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
1393 path = a_Dpip_get_attr_l(Buf, BufSize, "path");
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
1394 p = a_Dpip_get_attr_l(Buf, BufSize, "port");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1395 port = strtol(p, NULL, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1396 dFree(p);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1397
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1398 cookie = Cookies_get(host, path, scheme, port);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1399 dFree(scheme);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1400 dFree(path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1401 dFree(host);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1402
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1403 cmd = a_Dpip_build_cmd("cmd=%s cookie=%s", "get_cookie_answer", cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1404
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1405 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
1406 ret = 1;
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1407 } else {
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1408 _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
1409 ret = 2;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1410 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1411 dFree(cookie);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1412 dFree(cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1413 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1414
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1415 return ret;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1416 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1417
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1418 /* -- Termination handlers ----------------------------------------------- */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1419 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1420 * (was to delete the local namespace socket),
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1421 * but this is handled by 'dpid' now.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1422 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1423 static void cleanup(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1424 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1425 Cookies_save_and_free();
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1426 MSG("cleanup\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1427 /* no more cleanup required */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1428 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1429
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1430 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1431 * Perform any necessary cleanups upon abnormal termination
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1432 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1433 static void termination_handler(int signum)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1434 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1435 exit(signum);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1436 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1437
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1438
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1439 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1440 * -- MAIN -------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1441 */
1385
eb98997886ec Introduced the new tokenizing dsh API to DPIP
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
1442 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
1443 struct sockaddr_in sin;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1444 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
1445 ClientInfo *client;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1446 int tmp_fd,code;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1447 char *buf;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1448 Dsh *sh;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1449
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1450 /* Arrange the cleanup function for terminations via exit() */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1451 atexit(cleanup);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1452
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1453 /* Arrange the cleanup function for abnormal terminations */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1454 if (signal (SIGINT, termination_handler) == SIG_IGN)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1455 signal (SIGINT, SIG_IGN);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1456 if (signal (SIGHUP, termination_handler) == SIG_IGN)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1457 signal (SIGHUP, SIG_IGN);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1458 if (signal (SIGTERM, termination_handler) == SIG_IGN)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1459 signal (SIGTERM, SIG_IGN);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1460
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1461 Cookies_init();
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1462 MSG("(v.1) accepting connections...\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1463
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1464 if (disabled)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1465 exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1466
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1467 /* 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
1468 address_size = sizeof(struct sockaddr_in);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1469
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1470 while (1) {
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1471 tmp_fd = accept(STDIN_FILENO, (struct sockaddr *)&sin, &address_size);
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1472 if (tmp_fd == -1) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1473 perror("[accept]");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1474 exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1475 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1476
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1477 /* create the Dsh structure */
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1478 sh = a_Dpip_dsh_new(tmp_fd, tmp_fd, 8*1024);
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1479 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
1480 client->sh = sh;
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1481 client->status = 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1482
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1483 while (1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1484 code = 1;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1485 if ((buf = a_Dpip_dsh_read_token(sh)) != NULL) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1486 /* Let's see what we fished... */
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1487 _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
1488 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
1489 dFree(buf);
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1490 } else if (sh->status == DPIP_EAGAIN) {
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1491 /* may reach here when the tag size is larger than kernel buffer */
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1492 continue;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1493 }
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1494
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1495 _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
1496 if (code == 1) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1497 exit(1);
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1498 } else if (code == 2) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1499 break;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1500 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1501 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1502
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1385
diff changeset
1503 _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
1504 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
1505 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
1506 dFree(client);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1507
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1508 }/*while*/
174
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1509
67e98a1211f0 - Fixed a cookies-related dillo freeze bug happening at:
jcid
parents: 159
diff changeset
1510 return 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1511 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1512
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1513 #endif /* !DISABLE_COOKIES */