2 MiddleMan filtering proxy server
3 Copyright (C) 2002 Jason McLaughlin
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 read the <cookies> section from xml_list and load into another linked list
26 COOKIE_LIST *cookie_load(COOKIE_LIST * cookie_list, XML_LIST * xml_list)
28 COOKIE_LIST *tmp_list = cookie_list;
29 struct COOKIE_LIST_LIST *allow = NULL, *deny = NULL;
31 if (tmp_list == NULL) {
32 tmp_list = xmalloc(sizeof(COOKIE_LIST));
33 cookie_list = tmp_list;
34 tmp_list->allow = NULL;
35 tmp_list->deny = NULL;
36 tmp_list->policy = POLICY_ALLOW;
38 tmp_list->enabled = TRUE;
40 pthread_rwlock_init(&tmp_list->lock, NULL);
42 allow = tmp_list->allow;
43 deny = tmp_list->deny;
46 while ((xml_list = xml_section(xml_list, "<cookies>"))) {
47 XML_LIST_LOOP(xml_list, "<cookies>") {
48 XML_LIST_CMP(xml_list, "<allow>") {
49 allow = cookie_ll_new(allow);
50 allow->id = cookie_list->id++;
52 if (tmp_list->allow == NULL)
53 tmp_list->allow = allow;
54 XML_LIST_LOOP(xml_list, "<allow>") {
55 XML_LIST_CMP(xml_list, "<enabled>") {
56 xml_list = xml_list->next;
57 if (xml_list->type == XML_VALUE) {
58 if (!strcasecmp(xml_list->item, "false"))
59 allow->enabled = FALSE;
61 allow->enabled = TRUE;
64 XML_LIST_CMP(xml_list, "<comment>") {
65 xml_list = xml_list->next;
66 if (xml_list->type == XML_VALUE)
67 cookie_ll_insert(allow, xml_list->item, NULL, NULL);
69 XML_LIST_CMP(xml_list, "<direction>") {
70 xml_list = xml_list->next;
71 if (xml_list->type == XML_VALUE)
72 cookie_ll_insert(allow, NULL, xml_list->item, NULL);
74 XML_LIST_CMP(xml_list, "<host>") {
75 xml_list = xml_list->next;
76 if (xml_list->type == XML_VALUE)
77 cookie_ll_insert(allow, NULL, NULL, xml_list->item);
82 XML_LIST_CMP(xml_list, "<deny>") {
83 deny = cookie_ll_new(deny);
84 deny->id = cookie_list->id++;
86 if (tmp_list->deny == NULL)
87 tmp_list->deny = deny;
88 XML_LIST_LOOP(xml_list, "<deny>") {
89 XML_LIST_CMP(xml_list, "<enabled>") {
90 xml_list = xml_list->next;
91 if (xml_list->type == XML_VALUE) {
92 if (!strcasecmp(xml_list->item, "false"))
93 deny->enabled = FALSE;
98 XML_LIST_CMP(xml_list, "<comment>") {
99 xml_list = xml_list->next;
100 if (xml_list->type == XML_VALUE)
101 cookie_ll_insert(deny, xml_list->item, NULL, NULL);
103 XML_LIST_CMP(xml_list, "<direction>") {
104 xml_list = xml_list->next;
105 if (xml_list->type == XML_VALUE)
106 cookie_ll_insert(deny, NULL, xml_list->item, NULL);
108 XML_LIST_CMP(xml_list, "<host>") {
109 xml_list = xml_list->next;
110 if (xml_list->type == XML_VALUE)
111 cookie_ll_insert(deny, NULL, NULL, xml_list->item);
115 XML_LIST_CMP(xml_list, "<policy>") {
116 xml_list = xml_list->next;
117 if (xml_list->type == XML_VALUE) {
118 if (!strcasecmp(xml_list->item, "allow"))
119 tmp_list->policy = POLICY_ALLOW;
120 else if (!strcasecmp(xml_list->item, "deny"))
121 tmp_list->policy = POLICY_DENY;
124 XML_LIST_CMP(xml_list, "<enabled>") {
125 xml_list = xml_list->next;
126 if (xml_list->type == XML_VALUE) {
127 if (!strcasecmp(xml_list->item, "false"))
128 tmp_list->enabled = FALSE;
130 tmp_list->enabled = TRUE;
139 XML_LIST *cookie_xml(COOKIE_LIST * cookie_list, XML_LIST * xml_list)
143 struct COOKIE_LIST_LIST *cl = NULL;
145 if (cookie_list == NULL)
148 pthread_rwlock_rdlock(&cookie_list->lock);
150 xml_list = xml_list_add(xml_list, "<cookies>", XML_TAG);
152 xml_list = xml_list_add(xml_list, "<enabled>", XML_TAG);
153 xml_list = xml_list_add(xml_list, (cookie_list->enabled == TRUE) ? "true" : "false", XML_VALUE);
154 xml_list = xml_list_add(xml_list, "</enabled>", XML_TAG);
156 xml_list = xml_list_add(xml_list, "<policy>", XML_TAG);
157 xml_list = xml_list_add(xml_list, (cookie_list->policy == POLICY_ALLOW) ? "allow" : "deny", XML_VALUE);
158 xml_list = xml_list_add(xml_list, "</policy>", XML_TAG);
160 for (i = 0; i < 2; i++) {
163 cl = cookie_list->allow;
166 cl = cookie_list->deny;
170 for (; cl; cl = cl->next) {
171 xml_list = xml_list_add(xml_list, (i == 0) ? "<allow>" : "<deny>", XML_TAG);
173 xml_list = xml_list_add(xml_list, "<enabled>", XML_TAG);
174 xml_list = xml_list_add(xml_list, (cl->enabled == TRUE) ? "true" : "false", XML_VALUE);
175 xml_list = xml_list_add(xml_list, "</enabled>", XML_TAG);
177 if (cl->comment != NULL) {
178 xml_list = xml_list_add(xml_list, "<comment>", XML_TAG);
179 ptr = string_to_xml(cl->comment);
180 xml_list = xml_list_add(xml_list, ptr, XML_VALUE);
182 xml_list = xml_list_add(xml_list, "</comment>", XML_TAG);
185 xml_list = xml_list_add(xml_list, "<direction>", XML_TAG);
186 xml_list = xml_list_add(xml_list, (cl->direction == COOKIE_BOTH) ? "both" : (cl->direction == COOKIE_IN) ? "in" : "out", XML_VALUE);
187 xml_list = xml_list_add(xml_list, "</direction>", XML_TAG);
189 if (cl->host != NULL) {
190 xml_list = xml_list_add(xml_list, "<host>", XML_TAG);
191 ptr = string_to_xml(cl->host);
192 xml_list = xml_list_add(xml_list, ptr, XML_VALUE);
194 xml_list = xml_list_add(xml_list, "</host>", XML_TAG);
197 xml_list = xml_list_add(xml_list, (i == 0) ? "</allow>" : "</deny>", XML_TAG);
201 xml_list = xml_list_add(xml_list, "</cookies>", XML_TAG);
203 pthread_rwlock_unlock(&cookie_list->lock);
208 void cookie_ll_insert(struct COOKIE_LIST_LIST *x, char *a, char *b, char *c)
211 FREE_AND_NULL(x->comment);
214 x->comment = xstrdup(a);
217 if (!strcasecmp(b, "in"))
218 x->direction = COOKIE_IN;
219 else if (!strcasecmp(b, "out"))
220 x->direction = COOKIE_OUT;
221 else if (!strcasecmp(b, "both"))
222 x->direction = COOKIE_BOTH;
227 FREE_AND_NULL(x->host);
230 x->host = xstrdup(c);
231 x->he = reg_compile(c, REGFLAGS);
237 struct COOKIE_LIST_LIST *cookie_ll_new(struct COOKIE_LIST_LIST *x)
240 x = xmalloc(sizeof(struct COOKIE_LIST_LIST));
243 while (x->next != NULL)
245 x->next = xmalloc(sizeof(struct COOKIE_LIST_LIST));
254 x->direction = COOKIE_BOTH;
260 struct COOKIE_LIST_LIST *cookie_ll_delete(struct COOKIE_LIST_LIST *x)
262 struct COOKIE_LIST_LIST *start = x;
264 while (start->prev != NULL)
268 x->next->prev = x->prev;
270 x->prev->next = x->next;
276 FREE_AND_NULL(x->comment);
277 FREE_AND_NULL(x->host);
285 free memory used by COOKIE_LIST type structure
287 void cookie_free(COOKIE_LIST * cookie_list)
292 cookie_ll_free(cookie_list->allow);
293 cookie_ll_free(cookie_list->deny);
299 void cookie_ll_free(struct COOKIE_LIST_LIST *cl)
301 struct COOKIE_LIST_LIST *tmp;
308 FREE_AND_NULL(cl->comment);
309 FREE_AND_NULL(cl->host);
317 check if a cookie is allowed to pass in a given direction for a host
319 int cookie_check(COOKIE_LIST * cookie_list, int direction, CONNECTION * connection)
321 int action = FALSE, result = TRUE, ret, i;
322 struct COOKIE_LIST_LIST *current;
324 if (connection->bypass & FEATURE_COOKIES)
327 pthread_rwlock_rdlock(&cookie_list->lock);
329 if (cookie_list->enabled == FALSE) {
330 pthread_rwlock_unlock(&cookie_list->lock);
335 for (i = 0; i < 2; i++) {
337 if (cookie_list->policy == POLICY_ALLOW) {
338 current = cookie_list->deny;
342 current = cookie_list->allow;
346 } else if (action == result) {
347 if (cookie_list->policy == POLICY_ALLOW) {
348 current = cookie_list->allow;
351 current = cookie_list->deny;
357 for (; current != NULL; current = current->next) {
358 if (current->enabled == FALSE)
361 if (current->he != NULL && (current->direction == direction || current->direction == COOKIE_BOTH)) {
362 ret = reg_exec(current->he, connection->header->host);
372 pthread_rwlock_unlock(&cookie_list->lock);