Anope IRC Services  Version 2.0
m_dnsbl.cpp
Go to the documentation of this file.
1 /*
2  * (C) 2003-2014 Anope Team
3  * Contact us at team@anope.org
4  *
5  * Please read COPYING and README for further details.
6  */
7 
8 #include "module.h"
9 #include "modules/dns.h"
10 
11 using namespace DNS;
12 
13 static ServiceReference<XLineManager> akills("XLineManager", "xlinemanager/sgline");
14 static ServiceReference<Manager> dnsmanager("DNS::Manager", "dns/manager");
15 
16 struct Blacklist
17 {
19  time_t bantime;
21  std::map<int, Anope::string> replies;
22 
23  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) { }
24 };
25 
26 class DNSBLResolver : public Request
27 {
31 
32  public:
33  DNSBLResolver(Module *c, User *u, const Blacklist &b, const Anope::string &host, bool add_akill) : Request(dnsmanager, c, host, QUERY_A, true), user(u), blacklist(b), add_to_akill(add_akill) { }
34 
36  {
37  if (!user || user->Quitting())
38  return;
39 
40  const ResourceRecord &ans_record = record->answers[0];
41  // Replies should be in 127.0.0.0/24
42  if (ans_record.rdata.find("127.0.0.") != 0)
43  return;
44 
45  Anope::string record_reason;
46  if (!this->blacklist.replies.empty())
47  {
48  sockaddrs sresult;
49  sresult.pton(AF_INET, ans_record.rdata);
50  int result = sresult.sa4.sin_addr.s_addr >> 24;
51 
52  if (!this->blacklist.replies.count(result))
53  return;
54  record_reason = this->blacklist.replies[result];
55  }
56 
57  Anope::string reason = this->blacklist.reason, addr = user->ip.addr();
58  reason = reason.replace_all_cs("%n", user->nick);
59  reason = reason.replace_all_cs("%u", user->GetIdent());
60  reason = reason.replace_all_cs("%g", user->realname);
61  reason = reason.replace_all_cs("%h", user->host);
62  reason = reason.replace_all_cs("%i", addr);
63  reason = reason.replace_all_cs("%r", record_reason);
64  reason = reason.replace_all_cs("%N", Config->GetBlock("networkinfo")->Get<const Anope::string>("networkname"));
65 
66  BotInfo *OperServ = Config->GetClient("OperServ");
67  Log(creator, "dnsbl", OperServ) << user->GetMask() << " (" << addr << ") appears in " << this->blacklist.name;
68  XLine *x = new XLine("*@" + addr, OperServ ? OperServ->nick : "m_dnsbl", Anope::CurTime + this->blacklist.bantime, reason, XLineManager::GenerateUID());
69  if (this->add_to_akill && akills)
70  {
71  akills->AddXLine(x);
72  akills->Send(NULL, x);
73  }
74  else
75  {
76  IRCD->SendAkill(NULL, x);
77  delete x;
78  }
79  }
80 };
81 
82 class ModuleDNSBL : public Module
83 {
84  std::vector<Blacklist> blacklists;
88 
89  public:
90  ModuleDNSBL(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR | EXTRA)
91  {
92 
93  }
94 
96  {
97  Configuration::Block *block = conf->GetModule(this);
98  this->check_on_connect = block->Get<bool>("check_on_connect");
99  this->check_on_netburst = block->Get<bool>("check_on_netburst");
100  this->add_to_akill = block->Get<bool>("add_to_akill", "yes");
101 
102  this->blacklists.clear();
103  for (int i = 0, num = block->CountBlock("blacklist"); i < num; ++i)
104  {
105  Configuration::Block *bl = block->GetBlock("blacklist", i);
106 
107  Anope::string bname = bl->Get<const Anope::string>("name");
108  if (bname.empty())
109  continue;
110  time_t bantime = bl->Get<time_t>("time", "4h");
111  Anope::string reason = bl->Get<const Anope::string>("reason");
112  std::map<int, Anope::string> replies;
113  for (int j = 0; j < 256; ++j)
114  {
115  Anope::string k = bl->Get<const Anope::string>(stringify(j));
116  if (!k.empty())
117  replies[j] = k;
118  }
119 
120  this->blacklists.push_back(Blacklist(bname, bantime, reason, replies));
121  }
122  }
123 
124  void OnUserConnect(User *user, bool &exempt) anope_override
125  {
126  if (exempt || user->Quitting() || (!this->check_on_connect && !Me->IsSynced()) || !dnsmanager)
127  return;
128 
129  if (!this->check_on_netburst && !user->server->IsSynced())
130  return;
131 
132  /* At this time we only support IPv4 */
133  if (!user->ip.valid() || user->ip.sa.sa_family != AF_INET)
134  /* User doesn't have a valid IPv4 IP (ipv6/spoof/etc) */
135  return;
136 
137  const unsigned long &ip = user->ip.sa4.sin_addr.s_addr;
138  unsigned long reverse_ip = (ip << 24) | ((ip & 0xFF00) << 8) | ((ip & 0xFF0000) >> 8) | (ip >> 24);
139 
140  sockaddrs reverse = user->ip;
141  reverse.sa4.sin_addr.s_addr = reverse_ip;
142 
143  for (unsigned i = 0; i < this->blacklists.size(); ++i)
144  {
145  const Blacklist &b = this->blacklists[i];
146 
147  Anope::string dnsbl_host = reverse.addr() + "." + b.name;
148  DNSBLResolver *res = NULL;
149  try
150  {
151  res = new DNSBLResolver(this, user, b, dnsbl_host, this->add_to_akill);
152  dnsmanager->Process(res);
153  }
154  catch (const SocketException &ex)
155  {
156  delete res;
157  Log(this) << ex.GetReason();
158  }
159  }
160  }
161 };
162 
164 
void OnReload(Configuration::Conf *conf) anope_override
Definition: m_dnsbl.cpp:95
Definition: bots.h:24
int CountBlock(const Anope::string &name)
Definition: config.cpp:35
void pton(int type, const Anope::string &address, int pport=0)
Definition: sockets.cpp:121
const Anope::string & GetIdent() const
Definition: users.cpp:264
bool IsSynced() const
Definition: servers.cpp:298
std::map< int, Anope::string > replies
Definition: m_dnsbl.cpp:21
sockaddr_in sa4
Definition: sockets.h:31
void push_back(char c)
Definition: anope.h:142
Anope::string addr() const
Definition: sockets.cpp:73
void OnLookupComplete(const Query *record) anope_override
Definition: m_dnsbl.cpp:35
Anope::string reason
Definition: m_dnsbl.cpp:20
Definition: users.h:34
virtual void SendAkill(User *, XLine *)=0
Anope::string rdata
Definition: dns.h:96
CoreExport time_t CurTime
Definition: main.cpp:41
time_t bantime
Definition: m_dnsbl.cpp:19
std::vector< Blacklist > blacklists
Definition: m_dnsbl.cpp:84
Blacklist(const Anope::string &n, time_t b, const Anope::string &r, const std::map< int, Anope::string > &re)
Definition: m_dnsbl.cpp:23
bool add_to_akill
Definition: m_dnsbl.cpp:87
bool add_to_akill
Definition: m_dnsbl.cpp:30
bool Quitting() const
Definition: users.cpp:763
Block * GetBlock(const Anope::string &name, int num=0)
Definition: config.cpp:43
Definition: Config.cs:26
string replace_all_cs(const string &_orig, const string &_repl) const
Definition: anope.h:229
Anope::string name
Definition: m_dnsbl.cpp:18
Blacklist blacklist
Definition: m_dnsbl.cpp:29
Anope::string GetMask() const
Definition: users.cpp:269
bool check_on_connect
Definition: m_dnsbl.cpp:85
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
Anope::string host
Definition: users.h:65
sockaddrs ip
Definition: users.h:75
CoreExport IRCDProto * IRCD
Definition: protocol.cpp:23
#define MODULE_INIT(x)
Definition: modules.h:45
static ServiceReference< XLineManager > akills("XLineManager","xlinemanager/sgline")
void OnUserConnect(User *user, bool &exempt) anope_override
Definition: m_dnsbl.cpp:124
Definition: defs.h:28
Anope::string stringify(const T &x)
Definition: anope.h:710
Anope::string realname
Definition: users.h:71
DNSBLResolver(Module *c, User *u, const Blacklist &b, const Anope::string &host, bool add_akill)
Definition: m_dnsbl.cpp:33
Anope::string nick
Definition: users.h:62
bool check_on_netburst
Definition: m_dnsbl.cpp:86
Definition: xline.h:18
CoreExport Server * Me
Definition: servers.cpp:24
virtual const Anope::string & GetReason() const
Definition: anope.h:672
static ServiceReference< Manager > dnsmanager("DNS::Manager","dns/manager")
Reference< User > user
Definition: m_dnsbl.cpp:28
Definition: logger.h:53
T Get(const Anope::string &tag)
Definition: config.h:44
ModuleDNSBL(const Anope::string &modname, const Anope::string &creator)
Definition: m_dnsbl.cpp:90
static Anope::string GenerateUID()
Definition: xline.cpp:234
size_type find(const string &_str, size_type pos=0) const
Definition: anope.h:192
Definition: modules.h:163