-
Notifications
You must be signed in to change notification settings - Fork 1
/
client-tags.c
711 lines (621 loc) · 20.2 KB
/
client-tags.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/client-tags.c,v $
*
* Purpose : Functions related to client-specific tags.
*
* Copyright : Copyright (C) 2016-2017 Fabian Keil <[email protected]>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* The GNU General Public License should be included with
* this file. If not, you can view it at
* http://www.gnu.org/copyleft/gpl.html
* or write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
**********************************************************************/
#include "config.h"
#ifdef FEATURE_CLIENT_TAGS
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include "project.h"
#include "list.h"
#include "jcc.h"
#include "miscutil.h"
#include "errlog.h"
#include "parsers.h"
struct client_specific_tag
{
char *name;
time_t end_of_life;
struct client_specific_tag *next;
struct client_specific_tag *prev;
};
/**
* This struct represents tags that have been requested by clients
*/
struct requested_tags
{
char *client; /**< The IP address of the client that requested the tag */
/**< List of tags the client requested .... */
struct client_specific_tag *tags;
struct requested_tags *next;
struct requested_tags *prev;
};
struct requested_tags *requested_tags;
static void remove_tag_for_client(const char *client_address, const char *tag);
/*********************************************************************
*
* Function : validate_tag_list
*
* Description : Validates the given tag list
*
* Parameters :
* 1 : enabled_tags = The tags to validate
*
* Returns : void
*
*********************************************************************/
static void validate_tag_list(struct client_specific_tag *enabled_tags)
{
while (enabled_tags != NULL)
{
if (enabled_tags->name == NULL)
{
assert(enabled_tags->name != NULL);
log_error(LOG_LEVEL_FATAL, "validate_tag_list(): Tag without name detected");
}
if (enabled_tags->next != NULL)
{
if (enabled_tags->next->prev != enabled_tags)
{
assert(enabled_tags->next->prev == enabled_tags);
log_error(LOG_LEVEL_FATAL, "validate_tag_list(): Invalid backlink detected");
}
}
enabled_tags = enabled_tags->next;
}
}
/*********************************************************************
*
* Function : validate_requested_tags
*
* Description : Validates the requested_tags list
*
* Parameters : N/A
*
* Returns : void
*
*********************************************************************/
static jb_err validate_requested_tags()
{
struct requested_tags *requested_tag;
for (requested_tag = requested_tags; requested_tag != NULL;
requested_tag = requested_tag->next)
{
if (requested_tag->client == NULL)
{
assert(requested_tag->client != NULL);
log_error(LOG_LEVEL_FATAL, "validate_tag_list(): Client not registered");
}
validate_tag_list(requested_tag->tags);
if (requested_tag->next != NULL)
{
if (requested_tag->next->prev != requested_tag)
{
assert(requested_tag->next->prev == requested_tag);
log_error(LOG_LEVEL_FATAL, "validate_requested_tags(): Invalid backlink detected");
}
}
}
return TRUE;
}
/*********************************************************************
*
* Function : get_client_specific_tag
*
* Description : Returns the data for a client-specific-tag specified
* by name.
*
* Parameters :
* 1 : tag_list = The tag list to check
* 2 : name = The tag name to look up
*
* Returns : Pointer to tag structure or NULL on error.
*
*********************************************************************/
static struct client_tag_spec *get_client_specific_tag(
struct client_tag_spec *tag_list, const char *name)
{
struct client_tag_spec *tag;
for (tag = tag_list; tag != NULL; tag = tag->next)
{
if (tag->name != NULL && !strcmp(tag->name, name))
{
return tag;
}
}
log_error(LOG_LEVEL_ERROR, "No such tag: '%s'", name);
return NULL;
}
/*********************************************************************
*
* Function : get_tags_for_client
*
* Description : Returns the list of tags the client opted-in.
*
* Parameters :
* 1 : client_address = Address of the client
*
* Returns : Pointer to tag structure or NULL on error.
*
*********************************************************************/
static struct client_specific_tag *get_tags_for_client(const char *client_address)
{
struct requested_tags *requested_tag;
for (requested_tag = requested_tags; requested_tag != NULL;
requested_tag = requested_tag->next)
{
if (!strcmp(requested_tag->client, client_address))
{
return requested_tag->tags;
}
}
return NULL;
}
/*********************************************************************
*
* Function : get_tag_list_for_client
*
* Description : Provides a list of tag names the client opted-in.
* Other tag attributes are not part of the list.
*
* Parameters :
* 1 : tag_list = The list to fill in.
* 2 : client_address = Address of the client
*
* Returns : Pointer to tag list.
*
*********************************************************************/
void get_tag_list_for_client(struct list *tag_list,
const char *client_address)
{
struct client_specific_tag *enabled_tags;
const time_t now = time(NULL);
privoxy_mutex_lock(&client_tags_mutex);
enabled_tags = get_tags_for_client(client_address);
while (enabled_tags != NULL)
{
if (enabled_tags->end_of_life && (enabled_tags->end_of_life < now))
{
struct client_specific_tag *next_tag = enabled_tags->next;
log_error(LOG_LEVEL_TAGGING,
"Tag '%s' for client %s expired %ld seconds ago. Deleting it.",
enabled_tags->name, client_address,
(now - enabled_tags->end_of_life));
remove_tag_for_client(client_address, enabled_tags->name);
enabled_tags = next_tag;
continue;
}
else
{
log_error(LOG_LEVEL_TAGGING, "Enlisting tag '%s' for client %s.",
enabled_tags->name, client_address);
enlist(tag_list, enabled_tags->name);
}
enabled_tags = enabled_tags->next;
}
privoxy_mutex_unlock(&client_tags_mutex);
}
/*********************************************************************
*
* Function : get_next_tag_timeout_for_client
*
* Description : Figures out when the next temporarily enabled tag
* for the client will have timed out.
*
* Parameters :
* 1 : client_address = Address of the client
*
* Returns : Lowest timeout in seconds
*
*********************************************************************/
time_t get_next_tag_timeout_for_client(const char *client_address)
{
struct client_specific_tag *enabled_tags;
time_t next_timeout = 0;
const time_t now = time(NULL);
privoxy_mutex_lock(&client_tags_mutex);
enabled_tags = get_tags_for_client(client_address);
while (enabled_tags != NULL)
{
log_error(LOG_LEVEL_TAGGING,
"Evaluating tag '%s' for client %s. End of life %ld.",
enabled_tags->name, client_address, enabled_tags->end_of_life);
if (enabled_tags->end_of_life)
{
time_t time_left = enabled_tags->end_of_life - now;
/* Add a second to make sure the tag will have expired */
time_left++;
log_error(LOG_LEVEL_CGI, "%ld > %ld?", next_timeout, time_left);
if (next_timeout == 0 || next_timeout > time_left)
{
next_timeout = time_left;
}
}
enabled_tags = enabled_tags->next;
}
privoxy_mutex_unlock(&client_tags_mutex);
log_error(LOG_LEVEL_CGI, "Next timeout in %ld seconds", next_timeout);
return next_timeout;
}
/*********************************************************************
*
* Function : create_client_specific_tag
*
* Description : Allocates memory for a client specific tag
* and populates it.
*
* Parameters :
* 1 : name = The name of the tag to create.
* 2 : time_to_live = 0, or the number of seconds
* the tag remains activated.
*
* Returns : Pointer to populated tag
*
*********************************************************************/
static struct client_specific_tag *create_client_specific_tag(const char *name,
const time_t time_to_live)
{
struct client_specific_tag *tag;
tag = zalloc_or_die(sizeof(struct client_specific_tag));
tag->name = strdup_or_die(name);
tag->end_of_life = time_to_live ? (time(NULL) + time_to_live) : 0;
return tag;
}
/*********************************************************************
*
* Function : add_tag_for_client
*
* Description : Adds the tag for the client.
*
* Parameters :
* 1 : client_address = Address of the client
* 2 : tag = The tag to add.
* 3 : time_to_live = 0, or the number of seconds
* the tag remains activated.
*
* Returns : void
*
*********************************************************************/
static void add_tag_for_client(const char *client_address,
const char *tag, const time_t time_to_live)
{
struct requested_tags *clients_with_tags;
struct client_specific_tag *enabled_tags;
validate_requested_tags();
if (requested_tags == NULL)
{
/* XXX: Code duplication. */
requested_tags = zalloc_or_die(sizeof(struct requested_tags));
requested_tags->client = strdup_or_die(client_address);
requested_tags->tags = create_client_specific_tag(tag, time_to_live);
validate_requested_tags();
return;
}
else
{
clients_with_tags = requested_tags;
while (clients_with_tags->next != NULL)
{
if (!strcmp(clients_with_tags->client, client_address))
{
break;
}
clients_with_tags = clients_with_tags->next;
}
if (strcmp(clients_with_tags->client, client_address))
{
/* Client does not have tags yet, add new structure */
clients_with_tags->next = zalloc_or_die(sizeof(struct requested_tags));
clients_with_tags->next->prev = clients_with_tags;
clients_with_tags = clients_with_tags->next;
clients_with_tags->client = strdup_or_die(client_address);
clients_with_tags->tags = create_client_specific_tag(tag, time_to_live);
validate_requested_tags();
return;
}
}
enabled_tags = clients_with_tags->tags;
while (enabled_tags != NULL)
{
if (enabled_tags->next == NULL)
{
enabled_tags->next = create_client_specific_tag(tag, time_to_live);
enabled_tags->next->prev = enabled_tags;
break;
}
enabled_tags = enabled_tags->next;
}
validate_requested_tags();
}
/*********************************************************************
*
* Function : remove_tag_for_client
*
* Description : Removes the tag for the client.
*
* Parameters :
* 1 : client_address = Address of the client
* 2 : tag = The tag to remove.
*
* Returns : void
*
*********************************************************************/
static void remove_tag_for_client(const char *client_address, const char *tag)
{
struct requested_tags *clients_with_tags;
struct client_specific_tag *enabled_tags;
validate_requested_tags();
clients_with_tags = requested_tags;
while (clients_with_tags != NULL && clients_with_tags->client != NULL)
{
if (!strcmp(clients_with_tags->client, client_address))
{
break;
}
clients_with_tags = clients_with_tags->next;
}
assert(clients_with_tags != NULL);
if (clients_with_tags == NULL)
{
log_error(LOG_LEVEL_ERROR,
"Tried to remove tag %s for tag-less client %s",
tag, client_address);
}
enabled_tags = clients_with_tags->tags;
while (enabled_tags != NULL)
{
if (!strcmp(enabled_tags->name, tag))
{
if (enabled_tags->next != NULL)
{
enabled_tags->next->prev = enabled_tags->prev;
if (enabled_tags == clients_with_tags->tags)
{
/* Tag is first in line */
clients_with_tags->tags = enabled_tags->next;
}
}
if (enabled_tags->prev != NULL)
{
/* Tag has preceding tag */
enabled_tags->prev->next = enabled_tags->next;
}
if (enabled_tags->prev == NULL && enabled_tags->next == NULL)
{
/* Tag is the only one */
if (clients_with_tags->next != NULL)
{
/* Client has following client */
clients_with_tags->next->prev = clients_with_tags->prev;
}
if (clients_with_tags->prev != NULL)
{
/* Client has preceding client */
clients_with_tags->prev->next = clients_with_tags->next;
}
if (clients_with_tags == requested_tags)
{
/*
* We're in the process of removing the last tag,
* mark the global list as empty.
*/
requested_tags = NULL;
}
freez(clients_with_tags->client);
freez(clients_with_tags);
}
freez(enabled_tags->name);
freez(enabled_tags);
break;
}
enabled_tags = enabled_tags->next;
}
validate_requested_tags();
}
/*********************************************************************
*
* Function : client_has_requested_tag
*
* Description : Checks whether or not the given client requested
* the tag.
*
* Parameters :
* 1 : client_address = Address of the client
* 2 : tag = Tag to check.
*
* Returns : TRUE or FALSE.
*
*********************************************************************/
int client_has_requested_tag(const char *client_address, const char *tag)
{
struct client_specific_tag *enabled_tags;
enabled_tags = get_tags_for_client(client_address);
while (enabled_tags != NULL)
{
if (!strcmp(enabled_tags->name, tag))
{
return TRUE;
}
enabled_tags = enabled_tags->next;
}
return FALSE;
}
/*********************************************************************
*
* Function : enable_client_specific_tag
*
* Description : Enables a client-specific-tag for the client
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : tag_name = The name of the tag to enable
* 3 : time_to_live = If not 0, the number of seconds the
* tag should stay enabled.
*
* Returns : JB_ERR_OK on success, JB_ERR_MEMORY or JB_ERR_PARSE.
*
*********************************************************************/
jb_err enable_client_specific_tag(struct client_state *csp,
const char *tag_name, const time_t time_to_live)
{
struct client_tag_spec *tag;
privoxy_mutex_lock(&client_tags_mutex);
tag = get_client_specific_tag(csp->config->client_tags, tag_name);
if (tag == NULL)
{
privoxy_mutex_unlock(&client_tags_mutex);
return JB_ERR_PARSE;
}
if (client_has_requested_tag(csp->client_address, tag_name))
{
log_error(LOG_LEVEL_TAGGING,
"Tag '%s' already enabled for client '%s'.",
tag->name, csp->client_address);
}
else
{
add_tag_for_client(csp->client_address, tag_name, time_to_live);
log_error(LOG_LEVEL_TAGGING,
"Tag '%s' enabled for client '%s'. TTL: %ld.",
tag->name, csp->client_address, time_to_live);
}
privoxy_mutex_unlock(&client_tags_mutex);
return JB_ERR_OK;
}
/*********************************************************************
*
* Function : disable_client_specific_tag
*
* Description : Disables a client-specific-tag for the client
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : tag_name = The name of the tag to disable
*
* Returns : JB_ERR_OK on success, JB_ERR_MEMORY or JB_ERR_PARSE.
*
*********************************************************************/
jb_err disable_client_specific_tag(struct client_state *csp, const char *tag_name)
{
struct client_tag_spec *tag;
privoxy_mutex_lock(&client_tags_mutex);
tag = get_client_specific_tag(csp->config->client_tags, tag_name);
if (tag == NULL)
{
privoxy_mutex_unlock(&client_tags_mutex);
return JB_ERR_PARSE;
}
if (client_has_requested_tag(csp->client_address, tag_name))
{
remove_tag_for_client(csp->client_address, tag_name);
log_error(LOG_LEVEL_TAGGING,
"Tag '%s' disabled for client '%s'.", tag->name, csp->client_address);
}
else
{
log_error(LOG_LEVEL_TAGGING,
"Tag '%s' currently not set for client '%s'.",
tag->name, csp->client_address);
}
privoxy_mutex_unlock(&client_tags_mutex);
return JB_ERR_OK;
}
/*********************************************************************
*
* Function : client_tag_match
*
* Description : Compare a client tag against a client tag pattern.
*
* Parameters :
* 1 : pattern = a TAG pattern
* 2 : tag = Client tag to match
*
* Returns : Nonzero if the tag matches the pattern, else 0.
*
*********************************************************************/
int client_tag_match(const struct pattern_spec *pattern,
const struct list *tags)
{
struct list_entry *tag;
if (!(pattern->flags & PATTERN_SPEC_CLIENT_TAG_PATTERN))
{
/*
* It's not a client pattern and thus shouldn't
* be matched against client tags.
*/
return 0;
}
assert(tags);
for (tag = tags->first; tag != NULL; tag = tag->next)
{
if (0 == regexec(pattern->pattern.tag_regex, tag->str, 0, NULL, 0))
{
log_error(LOG_LEVEL_TAGGING, "Client tag '%s' matches.", tag->str);
return 1;
}
}
return 0;
}
/*********************************************************************
*
* Function : set_client_address
*
* Description : Sets the client address that will be used to enable,
* disable, or apply client tags.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
* 2 : headers = Client headers
*
* Returns : void.
*
*********************************************************************/
void set_client_address(struct client_state *csp, const struct list *headers)
{
if (csp->config->trust_x_forwarded_for)
{
const char *client_address;
client_address = get_header_value(headers, "X-Forwarded-For:");
if (client_address != NULL)
{
csp->client_address = strdup_or_die(client_address);
log_error(LOG_LEVEL_HEADER,
"Got client address %s from X-Forwarded-For header",
csp->client_address);
}
}
if (csp->client_address == NULL)
{
csp->client_address = strdup_or_die(csp->ip_addr_str);
}
}
#else
#error Compiling client-tags.c without FEATURE_CLIENT_TAGS
#endif /* def FEATURE_CLIENT_TAGS */