]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_dccallow.cpp
45a2304651bf29e4ecb45fc08c60a7d899b3d272
[user/henk/code/inspircd.git] / src / modules / m_dccallow.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 John Brooks <john.brooks@dereferenced.net>
6  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
7  *   Copyright (C) 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10  *   Copyright (C) 2006 Jamie ??? <???@???>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27
28 static const char* const helptext[] =
29 {
30         "DCCALLOW [(+|-)<nick> [<time>]]|[LIST|HELP]",
31         "You may allow DCCs from specific users by specifying a",
32         "DCC allow for the user you want to receive DCCs from.",
33         "For example, to allow the user Brain to send you inspircd.exe",
34         "you would type:",
35         "/DCCALLOW +Brain",
36         "Brain would then be able to send you files. They would have to",
37         "resend the file again if the server gave them an error message",
38         "before you added them to your DCCALLOW list.",
39         "DCCALLOW entries will be temporary by default, if you want to add",
40         "them to your DCCALLOW list until you leave IRC, type:",
41         "/DCCALLOW +Brain 0",
42         "To remove the user from your DCCALLOW list, type:",
43         "/DCCALLOW -Brain",
44         "To see the users in your DCCALLOW list, type:",
45         "/DCCALLOW LIST",
46         "NOTE: If the user leaves IRC or changes their nickname",
47         "  they will be removed from your DCCALLOW list.",
48         "  your DCCALLOW list will be deleted when you leave IRC."
49 };
50
51 class BannedFileList
52 {
53  public:
54         std::string filemask;
55         std::string action;
56 };
57
58 class DCCAllow
59 {
60  public:
61         std::string nickname;
62         std::string hostmask;
63         time_t set_on;
64         long length;
65
66         DCCAllow() { }
67
68         DCCAllow(const std::string &nick, const std::string &hm, const time_t so, const long ln) : nickname(nick), hostmask(hm), set_on(so), length(ln) { }
69 };
70
71 typedef std::vector<User *> userlist;
72 userlist ul;
73 typedef std::vector<DCCAllow> dccallowlist;
74 dccallowlist* dl;
75 typedef std::vector<BannedFileList> bannedfilelist;
76 bannedfilelist bfl;
77 typedef SimpleExtItem<dccallowlist> DCCAllowExt;
78
79 class CommandDccallow : public Command
80 {
81         DCCAllowExt& ext;
82
83  public:
84         unsigned int maxentries;
85         CommandDccallow(Module* parent, DCCAllowExt& Ext)
86                 : Command(parent, "DCCALLOW", 0)
87                 , ext(Ext)
88         {
89                 syntax = "[(+|-)<nick> [<time>]]|[LIST|HELP]";
90                 /* XXX we need to fix this so it can work with translation stuff (i.e. move +- into a seperate param */
91         }
92
93         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
94         {
95                 /* syntax: DCCALLOW [+|-]<nick> (<time>) */
96                 if (!parameters.size())
97                 {
98                         // display current DCCALLOW list
99                         DisplayDCCAllowList(user);
100                         return CMD_FAILURE;
101                 }
102                 else if (parameters.size() > 0)
103                 {
104                         char action = *parameters[0].c_str();
105
106                         // if they didn't specify an action, this is probably a command
107                         if (action != '+' && action != '-')
108                         {
109                                 if (!strcasecmp(parameters[0].c_str(), "LIST"))
110                                 {
111                                         // list current DCCALLOW list
112                                         DisplayDCCAllowList(user);
113                                         return CMD_FAILURE;
114                                 }
115                                 else if (!strcasecmp(parameters[0].c_str(), "HELP"))
116                                 {
117                                         // display help
118                                         DisplayHelp(user);
119                                         return CMD_FAILURE;
120                                 }
121                                 else
122                                 {
123                                         user->WriteNumeric(998, ":DCCALLOW command not understood. For help on DCCALLOW, type /DCCALLOW HELP");
124                                         return CMD_FAILURE;
125                                 }
126                         }
127
128                         std::string nick(parameters[0], 1);
129                         User *target = ServerInstance->FindNickOnly(nick);
130
131                         if ((target) && (!target->quitting) && (target->registered == REG_ALL))
132                         {
133
134                                 if (action == '-')
135                                 {
136                                         // check if it contains any entries
137                                         dl = ext.get(user);
138                                         if (dl)
139                                         {
140                                                 for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
141                                                 {
142                                                         // search through list
143                                                         if (i->nickname == target->nick)
144                                                         {
145                                                                 dl->erase(i);
146                                                                 user->WriteNumeric(995, "%s :Removed %s from your DCCALLOW list", user->nick.c_str(), target->nick.c_str());
147                                                                 break;
148                                                         }
149                                                 }
150                                         }
151                                 }
152                                 else if (action == '+')
153                                 {
154                                         if (target == user)
155                                         {
156                                                 user->WriteNumeric(996, "%s :You cannot add yourself to your own DCCALLOW list!", user->nick.c_str());
157                                                 return CMD_FAILURE;
158                                         }
159
160                                         dl = ext.get(user);
161                                         if (!dl)
162                                         {
163                                                 dl = new dccallowlist;
164                                                 ext.set(user, dl);
165                                                 // add this user to the userlist
166                                                 ul.push_back(user);
167                                         }
168
169                                         if (dl->size() >= maxentries)
170                                         {
171                                                 user->WriteNumeric(996, "%s :Too many nicks on DCCALLOW list", user->nick.c_str());
172                                                 return CMD_FAILURE;
173                                         }
174
175                                         for (dccallowlist::const_iterator k = dl->begin(); k != dl->end(); ++k)
176                                         {
177                                                 if (k->nickname == target->nick)
178                                                 {
179                                                         user->WriteNumeric(996, "%s :%s is already on your DCCALLOW list", user->nick.c_str(), target->nick.c_str());
180                                                         return CMD_FAILURE;
181                                                 }
182                                         }
183
184                                         std::string mask = target->nick+"!"+target->ident+"@"+target->dhost;
185                                         std::string default_length = ServerInstance->Config->ConfValue("dccallow")->getString("length");
186
187                                         unsigned long length;
188                                         if (parameters.size() < 2)
189                                         {
190                                                 length = InspIRCd::Duration(default_length);
191                                         }
192                                         else if (!atoi(parameters[1].c_str()))
193                                         {
194                                                 length = 0;
195                                         }
196                                         else
197                                         {
198                                                 length = InspIRCd::Duration(parameters[1]);
199                                         }
200
201                                         if (!InspIRCd::IsValidMask(mask))
202                                         {
203                                                 return CMD_FAILURE;
204                                         }
205
206                                         dl->push_back(DCCAllow(target->nick, mask, ServerInstance->Time(), length));
207
208                                         if (length > 0)
209                                         {
210                                                 user->WriteNumeric(993, "%s :Added %s to DCCALLOW list for %ld seconds", user->nick.c_str(), target->nick.c_str(), length);
211                                         }
212                                         else
213                                         {
214                                                 user->WriteNumeric(994, "%s :Added %s to DCCALLOW list for this session", user->nick.c_str(), target->nick.c_str());
215                                         }
216
217                                         /* route it. */
218                                         return CMD_SUCCESS;
219                                 }
220                         }
221                         else
222                         {
223                                 // nick doesn't exist
224                                 user->WriteNumeric(401, "%s :No such nick/channel", nick.c_str());
225                                 return CMD_FAILURE;
226                         }
227                 }
228                 return CMD_FAILURE;
229         }
230
231         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
232         {
233                 return ROUTE_BROADCAST;
234         }
235
236         void DisplayHelp(User* user)
237         {
238                 for (size_t i = 0; i < sizeof(helptext)/sizeof(helptext[0]); i++)
239                         user->WriteNumeric(998, ":%s", helptext[i]);
240                 user->WriteNumeric(999, ":End of DCCALLOW HELP");
241
242                 LocalUser* localuser = IS_LOCAL(user);
243                 if (localuser)
244                         localuser->CommandFloodPenalty += 4000;
245         }
246
247         void DisplayDCCAllowList(User* user)
248         {
249                  // display current DCCALLOW list
250                 user->WriteNumeric(990, ":Users on your DCCALLOW list:");
251
252                 dl = ext.get(user);
253                 if (dl)
254                 {
255                         for (dccallowlist::const_iterator c = dl->begin(); c != dl->end(); ++c)
256                         {
257                                 user->WriteNumeric(991, "%s :%s (%s)", user->nick.c_str(), c->nickname.c_str(), c->hostmask.c_str());
258                         }
259                 }
260
261                 user->WriteNumeric(992, ":End of DCCALLOW list");
262         }
263
264 };
265
266 class ModuleDCCAllow : public Module
267 {
268         DCCAllowExt ext;
269         CommandDccallow cmd;
270
271  public:
272         ModuleDCCAllow()
273                 : ext("dccallow", ExtensionItem::EXT_USER, this)
274                 , cmd(this, ext)
275         {
276         }
277
278         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message) CXX11_OVERRIDE
279         {
280                 dccallowlist* udl = ext.get(user);
281
282                 // remove their DCCALLOW list if they have one
283                 if (udl)
284                         stdalgo::erase(ul, user);
285
286                 // remove them from any DCCALLOW lists
287                 // they are currently on
288                 RemoveNick(user);
289         }
290
291         void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
292         {
293                 RemoveNick(user);
294         }
295
296         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
297         {
298                 if (!IS_LOCAL(user))
299                         return MOD_RES_PASSTHRU;
300
301                 if (target_type == TYPE_USER)
302                 {
303                         User* u = (User*)dest;
304
305                         /* Always allow a user to dcc themselves (although... why?) */
306                         if (user == u)
307                                 return MOD_RES_PASSTHRU;
308
309                         if ((text.length()) && (text[0] == '\1'))
310                         {
311                                 Expire();
312
313                                 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :DCC SEND m_dnsbl.cpp 3232235786 52650 9676
314                                 // :jamie!jamie@test-D4457903BA652E0F.silverdream.org PRIVMSG eimaj :VERSION
315
316                                 if (strncmp(text.c_str(), "\1DCC ", 5) == 0)
317                                 {
318                                         dl = ext.get(u);
319                                         if (dl && dl->size())
320                                         {
321                                                 for (dccallowlist::const_iterator iter = dl->begin(); iter != dl->end(); ++iter)
322                                                         if (InspIRCd::Match(user->GetFullHost(), iter->hostmask))
323                                                                 return MOD_RES_PASSTHRU;
324                                         }
325
326                                         // tokenize
327                                         std::stringstream ss(text);
328                                         std::string buf;
329                                         std::vector<std::string> tokens;
330
331                                         while (ss >> buf)
332                                                 tokens.push_back(buf);
333
334                                         if (tokens.size() < 2)
335                                                 return MOD_RES_PASSTHRU;
336
337                                         irc::string type = tokens[1].c_str();
338
339                                         ConfigTag* conftag = ServerInstance->Config->ConfValue("dccallow");
340                                         bool blockchat = conftag->getBool("blockchat");
341
342                                         if (type == "SEND")
343                                         {
344                                                 if (tokens.size() < 3)
345                                                         return MOD_RES_PASSTHRU;
346
347                                                 std::string defaultaction = conftag->getString("action");
348                                                 std::string filename = tokens[2];
349
350                                                 bool found = false;
351                                                 for (unsigned int i = 0; i < bfl.size(); i++)
352                                                 {
353                                                         if (InspIRCd::Match(filename, bfl[i].filemask, ascii_case_insensitive_map))
354                                                         {
355                                                                 /* We have a matching badfile entry, override whatever the default action is */
356                                                                 if (bfl[i].action == "allow")
357                                                                         return MOD_RES_PASSTHRU;
358                                                                 else
359                                                                 {
360                                                                         found = true;
361                                                                         break;
362                                                                 }
363                                                         }
364                                                 }
365
366                                                 /* only follow the default action if no badfile matches were found above */
367                                                 if ((!found) && (defaultaction == "allow"))
368                                                         return MOD_RES_PASSTHRU;
369
370                                                 user->WriteNotice("The user " + u->nick + " is not accepting DCC SENDs from you. Your file " + filename + " was not sent.");
371                                                 u->WriteNotice(user->nick + " (" + user->ident + "@" + user->dhost + ") attempted to send you a file named " + filename + ", which was blocked.");
372                                                 u->WriteNotice("If you trust " + user->nick + " and were expecting this, you can type /DCCALLOW HELP for information on the DCCALLOW system.");
373                                                 return MOD_RES_DENY;
374                                         }
375                                         else if ((type == "CHAT") && (blockchat))
376                                         {
377                                                 user->WriteNotice("The user " + u->nick + " is not accepting DCC CHAT requests from you.");
378                                                 u->WriteNotice(user->nick + " (" + user->ident + "@" + user->dhost + ") attempted to initiate a DCC CHAT session, which was blocked.");
379                                                 u->WriteNotice("If you trust " + user->nick + " and were expecting this, you can type /DCCALLOW HELP for information on the DCCALLOW system.");
380                                                 return MOD_RES_DENY;
381                                         }
382                                 }
383                         }
384                 }
385                 return MOD_RES_PASSTHRU;
386         }
387
388         void Expire()
389         {
390                 for (userlist::iterator iter = ul.begin(); iter != ul.end();)
391                 {
392                         User* u = (User*)(*iter);
393                         dl = ext.get(u);
394                         if (dl)
395                         {
396                                 if (dl->size())
397                                 {
398                                         dccallowlist::iterator iter2 = dl->begin();
399                                         while (iter2 != dl->end())
400                                         {
401                                                 if (iter2->length != 0 && (iter2->set_on + iter2->length) <= ServerInstance->Time())
402                                                 {
403                                                         u->WriteNumeric(997, "%s :DCCALLOW entry for %s has expired", u->nick.c_str(), iter2->nickname.c_str());
404                                                         iter2 = dl->erase(iter2);
405                                                 }
406                                                 else
407                                                 {
408                                                         ++iter2;
409                                                 }
410                                         }
411                                 }
412                                 ++iter;
413                         }
414                         else
415                         {
416                                 iter = ul.erase(iter);
417                         }
418                 }
419         }
420
421         void RemoveNick(User* user)
422         {
423                 /* Iterate through all DCCALLOW lists and remove user */
424                 for (userlist::iterator iter = ul.begin(); iter != ul.end();)
425                 {
426                         User *u = (User*)(*iter);
427                         dl = ext.get(u);
428                         if (dl)
429                         {
430                                 if (dl->size())
431                                 {
432                                         for (dccallowlist::iterator i = dl->begin(); i != dl->end(); ++i)
433                                         {
434                                                 if (i->nickname == user->nick)
435                                                 {
436
437                                                         u->WriteNotice(i->nickname + " left the network or changed their nickname and has been removed from your DCCALLOW list");
438                                                         u->WriteNumeric(995, "%s :Removed %s from your DCCALLOW list", u->nick.c_str(), i->nickname.c_str());
439                                                         dl->erase(i);
440                                                         break;
441                                                 }
442                                         }
443                                 }
444                                 ++iter;
445                         }
446                         else
447                         {
448                                 iter = ul.erase(iter);
449                         }
450                 }
451         }
452
453         void RemoveFromUserlist(User *user)
454         {
455                 // remove user from userlist
456                 for (userlist::iterator j = ul.begin(); j != ul.end(); ++j)
457                 {
458                         User* u = (User*)(*j);
459                         if (u == user)
460                         {
461                                 ul.erase(j);
462                                 break;
463                         }
464                 }
465         }
466
467         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
468         {
469                 ConfigTag* tag = ServerInstance->Config->ConfValue("dccallow");
470                 cmd.maxentries = tag->getInt("maxentries", 20);
471
472                 bfl.clear();
473                 ConfigTagList tags = ServerInstance->Config->ConfTags("banfile");
474                 for (ConfigIter i = tags.first; i != tags.second; ++i)
475                 {
476                         BannedFileList bf;
477                         bf.filemask = i->second->getString("pattern");
478                         bf.action = i->second->getString("action");
479                         bfl.push_back(bf);
480                 }
481         }
482
483         Version GetVersion() CXX11_OVERRIDE
484         {
485                 return Version("Provides support for the /DCCALLOW command", VF_COMMON | VF_VENDOR);
486         }
487 };
488
489 MODULE_INIT(ModuleDCCAllow)