summaryrefslogtreecommitdiffstats
path: root/src/squark-filter.c
blob: 22c8800924bc0974f3b5aa916933fad62b780266 (plain)
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
/* squark-filter.c - Squid User Authentication and Rating Kit
 *   An external redirector for Squid which analyzes the URL according
 *   to a database and can redirect to a block page.
 *
 * Copyright (C) 2010 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation. See http://www.gnu.org/ for details.
 */

#include <time.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>

#include <cmph.h>

#include "config.h"
#include "blob.h"
#include "addr.h"
#include "filterdb.h"
#include "authdb.h"

#define FILTER_OVERRIDE_TIMEOUT		(15*60)

static struct sqdb db;
static struct authdb adb;
static struct authdb_config adbc;

static int running = 1;
static const blob_t dash = BLOB_STR_INIT("-");
static const blob_t space = BLOB_STR_INIT(" ");
static const blob_t slash = BLOB_STR_INIT("/");
static const blob_t lf = BLOB_STR_INIT("\n");
static struct authdb adb;
static time_t now;

struct url_info {
	blob_t protocol;
	blob_t username;
	blob_t password;
	blob_t host;
	blob_t significant_host;
	blob_t path;
	blob_t query;
	blob_t fragment;
	int port;
	int is_ipv4;
	int num_dots;
};

struct url_dns_part_data {
	blob_t word;
	int num_dots;
	int numeric;
};

void blob_pull_url_dns_part(blob_t *b, struct url_dns_part_data *udp)
{
	blob_t t = *b;
	int c, i, dots = 0, numeric = 1;

	for (i = 0; i < t.len; i++) {
		c = (unsigned char) t.ptr[i];
		switch (c) {
		case '.':
			dots++;
			break;
		case ':': case '@': case '/': case '?':
			*b = BLOB_PTR_LEN(t.ptr + i, t.len - i);
			udp->word = BLOB_PTR_LEN(t.ptr, i);
			udp->num_dots = dots;
			udp->numeric = numeric;
			return;
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			break;
		default:
			numeric = 0;
			break;
		}
	}

	*b = BLOB_NULL;
	udp->word = t;
	udp->num_dots = dots;
	udp->numeric = numeric;
}

/* URI is generalized as:
 * [proto://][user[:password]@]domain.name[:port][/[path/to][?p=a&q=b;r=c][#fragment]]
 * Character literals used as separators are:
 *  : / @ ? & ; #
 * Also URI escaping says to treat %XX as encoded hex value.
 */

static int url_parse(blob_t uri, struct url_info *nfo)
{
	struct url_dns_part_data prev, cur;

	memset(&prev, 0, sizeof(prev));
	memset(nfo, 0, sizeof(*nfo));

	/* parse protocol, username/password and domain name/port */
	do {
		blob_pull_url_dns_part(&uri, &cur);

		switch (uri.len ? uri.ptr[0] : '/') {
		case ':':
			blob_pull_skip(&uri, 1);
			if (blob_is_null(nfo->protocol) &&
			    blob_pull_matching(&uri, BLOB_STR("//")))
				nfo->protocol = cur.word;
			else
				prev = cur;
			break;
		case '@':
			blob_pull_skip(&uri, 1);
			if (!blob_is_null(nfo->username) ||
			    !blob_is_null(nfo->password))
				goto error;
			if (!blob_is_null(prev.word)) {
				nfo->username = prev.word;
				nfo->password = cur.word;
			} else
				nfo->username = cur.word;
			memset(&prev, 0, sizeof(prev));
			break;
		case '/':
		case '?':
			if (!blob_is_null(prev.word)) {
				nfo->host = prev.word;
				nfo->num_dots = prev.num_dots;
				nfo->is_ipv4 = prev.numeric && prev.num_dots == 3;
				nfo->port = blob_pull_uint(&cur.word, 10);
			} else {
				nfo->host = cur.word;
				nfo->num_dots = cur.num_dots;
				nfo->is_ipv4 = cur.numeric && cur.num_dots == 3;
			}
			if (blob_is_null(nfo->host))
				nfo->host = BLOB_STR("localhost");
			break;
		}
	} while (blob_is_null(nfo->host) && !blob_is_null(uri));

	/* rest of the components */
	nfo->path = blob_pull_cspn(&uri, BLOB_STR("?&;#"));
	nfo->query = blob_pull_cspn(&uri, BLOB_STR("#"));
	nfo->fragment = uri;

	/* fill in defaults if needed */
	if (blob_is_null(nfo->protocol)) {
		if (nfo->port == 443)
			nfo->protocol = BLOB_STR("https");
		else
			nfo->protocol = BLOB_STR("http");
		if (nfo->port == 0)
			nfo->port = 80;
	} else if (nfo->port == 0) {
		if (blob_cmp(nfo->protocol, BLOB_STR("https")) == 0)
			nfo->port = 443;
		else
			nfo->port = 80;
	}
	if (blob_is_null(nfo->path))
		nfo->path = BLOB_STR("/");

	/* significant host name */
	nfo->significant_host = nfo->host;
	if (nfo->num_dots > 1) {
		blob_t b = nfo->significant_host;
		if (blob_pull_matching(&b, BLOB_STR("www")) &&
		    (blob_pull_uint(&b, 10), 1) &&
		    blob_pull_matching(&b, BLOB_STR(".")))
			nfo->significant_host = b;
	}
	return 1;

error:
	return 0;
}

#if 0
static void url_print(struct url_info *nfo)
{
#define print_field(nfo, x) if (!blob_is_null(nfo->x)) printf(" %s{%.*s}", #x, nfo->x.len, nfo->x.ptr)
	print_field(nfo, protocol);
	print_field(nfo, username);
	print_field(nfo, password);
	print_field(nfo, host);
	printf(" port{%d}", nfo->port);
	print_field(nfo, path);
	print_field(nfo, query);
	print_field(nfo, fragment);
#undef print_field
	printf("\n");
	fflush(stdout);
}
#endif

static int url_classify(struct url_info *url, struct sqdb *db)
{
	unsigned char buffer[512];
	blob_t key, got, tld, keybuf, keylimits;
	void *cmph;
	struct sqdb_index_entry *indx;
	cmph_uint32 i = SQDB_PARENT_ROOT, previ = SQDB_PARENT_ROOT;
	int dots_done = 1;

	cmph = sqdb_section_get(db, SQDB_SECTION_INDEX_MPH, NULL);
	indx = sqdb_section_get(db, SQDB_SECTION_INDEX, NULL);

	keybuf = BLOB_BUF(buffer);
	blob_push_lower(&keybuf, url->significant_host);
	key = keylimits = blob_pushed(BLOB_BUF(buffer), keybuf);

	/* search for most qualified domain match; do first lookup
	 * with two domain components */
	if (url->is_ipv4) {
		i = cmph_search_packed(cmph, key.ptr, key.len);

		if (indx[i].parent != SQDB_PARENT_IPV4 ||
		    indx[i].component != blob_inet_addr(url->host)) {
			i = previ;
			goto parent_dns_match;
		}
	} else {
		key = BLOB_PTR_LEN(key.ptr + key.len, 0);
		tld = blob_expand_head(&key, keylimits, '.');

		do {
			/* add one more domain component */
			got = blob_expand_head(&key, keylimits, '.');
			if (blob_is_null(got))
				break;

			previ = i;
			i = cmph_search_packed(cmph, key.ptr, key.len);
			if (!blob_is_null(tld)) {
				int p = indx[i].parent;

				if (p == SQDB_PARENT_ROOT ||
				    p == SQDB_PARENT_IPV4 ||
				    indx[p].parent != SQDB_PARENT_ROOT ||
				    blob_cmp(tld, sqdb_get_string_literal(db, indx[p].component)) != 0) {
					/* top level domain did not match */
					i = -1;
					goto parent_dns_match;
				}
				tld = BLOB_NULL;
				previ = p;
			}
			if (indx[i].parent != previ ||
			    blob_cmp(got, sqdb_get_string_literal(db, indx[i].component)) != 0) {
				/* the subdomain did no longer match, use
				 * parents classification */
				i = previ;
				goto parent_dns_match;
			}
			dots_done++;
		} while (indx[i].has_subdomains);
	}

	/* No paths to match for */
	if (i == SQDB_PARENT_ROOT || !indx[i].has_paths || key.ptr != keylimits.ptr)
		goto parent_dns_match;

	/* and then search for path matches -- construct hashing
	 * string of url decoded path */
	blob_push_urldecode(&keybuf, url->path);
	key = keylimits = blob_pushed(BLOB_BUF(buffer), keybuf);

	while (indx[i].has_paths) {
		/* add one more path component */
		got = blob_expand_tail(&key, keylimits, '/');
		if (blob_is_null(got))
			break;
		previ = i;
		i = cmph_search_packed(cmph, key.ptr, key.len);
		tld = sqdb_get_string_literal(db, indx[i].component);
		if (blob_cmp(got, sqdb_get_string_literal(db, indx[i].component)) != 0) {
			/* the subdomain did no longer match, use 
			* parents classification */
			i = previ;
			goto parent_dns_match;
		}
	}

parent_dns_match:
	if (i == SQDB_PARENT_ROOT)
		return 0; /* no category */

	return indx[i].category;
}

static blob_t get_category_name(struct sqdb *db, int id)
{
	uint32_t *c, clen;

	c = sqdb_section_get(db, SQDB_SECTION_CATEGORIES, &clen);
	if (c == NULL || id < 0 || id * sizeof(uint32_t) >= clen)
		return BLOB_NULL;

	return sqdb_get_string_literal(db, c[id]);
}

static void send_ok(blob_t tag, blob_t categ, int override)
{
	static char buffer[64];
	blob_t b = BLOB_BUF(buffer);

	blob_push(&b, tag);
	blob_push(&b, BLOB_STR(" !"));
	blob_push(&b, categ);
	if (override)
		blob_push(&b, BLOB_STR(",overridden"));
	blob_push(&b, BLOB_STR("!"));
	blob_push(&b, lf);
	b = blob_pushed(BLOB_BUF(buffer), b);

	write(STDOUT_FILENO, b.ptr, b.len);
}

static void send_redirect(blob_t redirect_page, blob_t tag, blob_t url, blob_t categ, blob_t username)
{
	static char buffer[8*1024];
	blob_t b = BLOB_BUF(buffer);

	blob_push(&b, tag);
	blob_push(&b, BLOB_STR(" !"));
	blob_push(&b, categ);
	blob_push(&b, BLOB_STR(",blocked!302:"));
	blob_push(&b, adbc.redirect_url_base);
	blob_push(&b, redirect_page);
	blob_push(&b, BLOB_STR("?REASON="));
	blob_push_urlencode(&b, categ);
	blob_push(&b, BLOB_STR("&USER="));
	blob_push_urlencode(&b, username);
	blob_push(&b, BLOB_STR("&DENIEDURL="));
	blob_push_urlencode(&b, url);
	blob_push(&b, lf);
	b = blob_pushed(BLOB_BUF(buffer), b);

	write(STDOUT_FILENO, b.ptr, b.len);
}

static void read_input(struct sqdb *db)
{
	static char buffer[8 * 1024];
	static blob_t left;

	blob_t b, line, id, ipaddr, url, username;
	struct url_info nfo;
	int r, category, auth_ok;
	sockaddr_any addr;
	struct authdb_entry entry;
	void *token;

	if (blob_is_null(left))
		left = BLOB_BUF(buffer);

	r = read(STDIN_FILENO, left.ptr, left.len);
	if (r < 0)
		return;
	if (r == 0) {
		running = 0;
		return;
	}
	left.ptr += r;
	left.len -= r;

	now = time(NULL);

	b = blob_pushed(BLOB_BUF(buffer), left);
	do {
		line = blob_pull_cspn(&b, lf);
		if (!blob_pull_matching(&b, lf))
			return;

		id = blob_pull_cspn(&line, space);
		blob_pull_spn(&line, space);
		url = blob_pull_cspn(&line, space);
		blob_pull_spn(&line, space);
		ipaddr = blob_pull_cspn(&line, slash); /* client addr */
		blob_pull_cspn(&line, space); /* fqdn */
		blob_pull_spn(&line, space);
		username = blob_pull_cspn(&line, space);
		/* http method */
		/* urlgroup */
		/* myaddr=xxx myport=xxx etc */

		if (!blob_is_null(url) &&
		    addr_parse(ipaddr, &addr)) {
			/* valid request, handle it */
			if (url_parse(url, &nfo))
				category = url_classify(&nfo, db);
			else
				category = 0;

			token = authdb_get(&adb, &addr, &entry, 1);
			if (authdb_check_login(token, &entry, username, now, &adbc)) {
				auth_ok = 1;
				username = BLOB_STRLEN(entry.p.login_name);
			} else {
				auth_ok = 0;
			}

			if (!auth_ok) {
				send_redirect(BLOB_STR("login.cgi"), id, url, BLOB_STR("auth"), username);
			} else if (((1ULL << category) & entry.p.block_categories) &&
				   (now < entry.override_time ||
				    now > entry.override_time + FILTER_OVERRIDE_TIMEOUT ||
				   ((1ULL << category) & entry.p.hard_block_categories))) {
				send_redirect(BLOB_STR("warning.cgi"), id, url, get_category_name(db, category), username);
			} else
				send_ok(id, get_category_name(db, category),
					!!((1ULL << category) & entry.p.block_categories));
		}

		if (b.len) {
			memcpy(buffer, b.ptr, b.len);
			b.ptr = buffer;
		}
		left = BLOB_PTR_LEN(buffer + b.len, sizeof(buffer) - b.len);
	} while (b.len);
}

int main(int argc, char **argv)
{
	int rc = 1;

	if (sqdb_open(&db, squark_dbname) < 0) {
		fprintf(stderr, "%s: failed to open squarkdb\n",
			squark_dbname);
		goto err_sqdb;
	}
	if (authdb_open(&adb, &adbc, &db) < 0) {
		fprintf(stderr, "Failed to initialize authdb\n");
		goto err_adb;
	}

	while (running)
		read_input(&db);
	rc = 0;

	authdb_close(&adb);
err_adb:
	sqdb_close(&db);
err_sqdb:
        return rc;
}