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