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
|
/*
* (C) 2003-2011 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*/
#include "module.h"
struct FakeAkill : public Command
{
FakeAkill() : Command("AKILL", 0, 0) { this->service = OperServ; }
CommandReturn Execute(CommandSource &, const std::vector<Anope::string> &) { return MOD_CONT; }
} fake_akill;
struct Blacklist
{
Anope::string name;
time_t bantime;
Anope::string reason;
std::map<int, Anope::string> replies;
Blacklist(const Anope::string &n, time_t b, const Anope::string &r, const std::map<int, Anope::string> &re) : name(n), bantime(b), reason(r), replies(re) { }
};
class DNSBLResolver : public DNSRequest
{
dynamic_reference<User> user;
Blacklist blacklist;
bool add_to_akill;
public:
DNSBLResolver(Module *c, User *u, const Blacklist &b, const Anope::string &host, bool add_akill) : DNSRequest(host, DNS_QUERY_A, true, c), user(u), blacklist(b), add_to_akill(add_akill) { }
void OnLookupComplete(const DNSRecord *record)
{
if (!user || user->GetExt("m_dnsbl_akilled"))
return;
Anope::string record_reason;
if (!this->blacklist.replies.empty())
{
sockaddrs sresult;
sresult.pton(AF_INET, record->result);
int result = (sresult.sa4.sin_addr.s_addr & 0xFF000000) >> 24;
if (!this->blacklist.replies.count(result))
return;
record_reason = this->blacklist.replies[result];
}
user->Extend("m_dnsbl_akilled");
Anope::string reason = this->blacklist.reason;
reason = reason.replace_all_cs("%n", user->nick);
reason = reason.replace_all_cs("%u", user->GetIdent());
reason = reason.replace_all_cs("%g", user->realname);
reason = reason.replace_all_cs("%h", user->host);
reason = reason.replace_all_cs("%i", user->ip.addr());
reason = reason.replace_all_cs("%r", record_reason);
reason = reason.replace_all_cs("%N", Config->NetworkName);
XLine *x = NULL;
if (this->add_to_akill && SGLine && (x = SGLine->Add(NULL, NULL, Anope::string("*@") + user->host, Anope::CurTime + this->blacklist.bantime, reason)))
{
Log(LOG_COMMAND, OperServ, &fake_akill) << "for " << user->GetMask() << " (Listed in " << this->blacklist.name << ")";
/* If AkillOnAdd is disabled send it anyway, noone wants bots around... */
if (!Config->AkillOnAdd)
ircdproto->SendAkill(*user, x);
}
else
{
Log(OperServ) << "DNSBL: " << user->GetMask() << " appears in " << this->blacklist.name;
XLine xline(Anope::string("*@") + user->host, OperServ ? OperServ->nick : "OperServ", Anope::CurTime + this->blacklist.bantime, reason);
ircdproto->SendAkill(*user, &xline);
}
}
};
class ModuleDNSBL : public Module
{
std::vector<Blacklist> blacklists;
bool check_on_connect;
bool check_on_netburst;
bool add_to_akill;
public:
ModuleDNSBL(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator)
{
this->SetAuthor("Anope");
this->SetType(SUPPORTED);
OnReload(false);
Implementation i[] = { I_OnReload, I_OnPreUserConnect };
ModuleManager::Attach(i, this, 2);
}
void OnReload(bool)
{
ConfigReader config;
this->check_on_connect = config.ReadFlag("m_dnsbl", "check_on_connect", "no", 0);
this->check_on_netburst = config.ReadFlag("m_dnsbl", "check_on_netburst", "no", 0);
this->add_to_akill = config.ReadFlag("m_dnsbl", "add_to_akill", "yes", 0);
this->blacklists.clear();
for (int i = 0, num = config.Enumerate("blacklist"); i < num; ++i)
{
Anope::string bname = config.ReadValue("blacklist", "name", "", i);
if (bname.empty())
continue;
Anope::string sbantime = config.ReadValue("blacklist", "time", "4h", i);
time_t bantime = dotime(sbantime);
if (bantime < 0)
bantime = 9000;
Anope::string reason = config.ReadValue("blacklist", "reason", "", i);
std::map<int, Anope::string> replies;
for (int j = 0; j < 256; ++j)
{
Anope::string k = config.ReadValue("blacklist", stringify(j), "", i);
if (!k.empty())
replies[j] = k;
}
this->blacklists.push_back(Blacklist(bname, bantime, reason, replies));
}
}
EventReturn OnPreUserConnect(User *u)
{
if (!this->check_on_connect && !Me->IsSynced())
return EVENT_CONTINUE;
if (!this->check_on_netburst && !u->server->IsSynced())
return EVENT_CONTINUE;
/* At this time we only support IPv4 */
if (u->ip.sa.sa_family != AF_INET)
return EVENT_CONTINUE;
unsigned long ip = u->ip.sa4.sin_addr.s_addr;
unsigned long reverse_ip = ((ip & 0xFF) << 24) | ((ip & 0xFF00) << 8) | ((ip & 0xFF0000) >> 8) | ((ip & 0xFF000000) >> 24);
sockaddrs user_ip;
user_ip.sa4.sin_family = AF_INET;
user_ip.sa4.sin_addr.s_addr = reverse_ip;
for (unsigned i = 0; i < this->blacklists.size(); ++i)
{
const Blacklist &b = this->blacklists[i];
try
{
Anope::string dnsbl_host = user_ip.addr() + "." + b.name;
DNSBLResolver *res = new DNSBLResolver(this, u, b, dnsbl_host, this->add_to_akill);
res->Process();
}
catch (const SocketException &ex)
{
Log() << "m_dnsbl: " << ex.GetReason();
}
}
return EVENT_CONTINUE;
}
};
MODULE_INIT(ModuleDNSBL)
|