/
sgDNSBL.c
122 lines (97 loc) · 2.82 KB
/
sgDNSBL.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
/*
* By accepting this notice, you agree to be bound by the following
* agreements:
*
* This software product, squidGuard, is copyrighted
* (C) 2012, Andreas Hofmeister, Collax GmbH,
* (C) 1998-2009 by Christine Kronberg, Shalla Secure Services.
* 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. It 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 (GPL) for more details.
*
* You should have received a copy of the GNU General Public License
* (GPL) along with this program.
*/
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <netdb.h>
#include "sgDNSBL.h"
#include "sgMemory.h"
#include "sgLog.h"
static char *strip_fqdn(const char *domain)
{
char *result;
result = strstr(domain, ".");
if (result == NULL)
return NULL;
return result + 1;
}
static int is_blacklisted(const char *domain, const char *suffix)
{
char *target = NULL;
struct addrinfo *res = NULL;
int result;
asprintf(&target, "%s%s.", domain, suffix);
result = getaddrinfo(target, NULL, NULL, &res);
sgFree(target);
if (result == 0) { //Result is defined
freeaddrinfo(res);
return 1;
}
if (res)
freeaddrinfo(res);
sgFree(target);
//If anything fails (DNS server not reachable, any problem in the resolution,
//let's not block anything.
return 0;
}
static int blocked_by_dnsbl(const char *domain, const char *suffix)
{
const char *dn = domain;
while ((dn != NULL) && (strchr(dn, '.') != NULL)) { //No need to lookup "com.black.uribl.com"
sgLogDebug("check %s agains dnsbl %s", dn, suffix);
if (is_blacklisted(dn, suffix)) {
sgLogDebug(" %s found on dnsbl", dn);
return 1;
}
dn = strip_fqdn(dn);
}
return 0;
}
static int dnsbl_match(void *priv, const struct SquidInfo *req)
{
return blocked_by_dnsbl(req->domain, (char *)priv);
}
static void free_suffix(void *priv)
{
sgFree(priv);
}
#define DEFAULT_SUFFIX ".black.uribl.com"
struct DestMatch *newDNSBLMatch(const char *suffix)
{
struct DestMatch *dm = NULL;
char *priv = NULL;
if ((suffix == NULL) || (*suffix == '\0')) { //Config does not define which dns domain to use
priv = sgStrdup(DEFAULT_SUFFIX);
} else {
if (strspn(suffix, ".-abcdefghijklmnopqrstuvwxyz0123456789") != strlen(suffix))
sgLogFatal("provided dnsbl \"%s\" doesn't look like a valid domain suffix", suffix);
priv = sgMalloc(strlen(suffix) + 1);
*priv = 0;
if (*suffix != '.')
strcpy(priv, ".");
strcat(priv, suffix);
}
if ((dm = sgNewDestMatch(dnsbl_match, free_suffix)) == NULL) {
sgFree(priv);
return NULL;
}
dm->priv = priv;
return dm;
}