]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
Fixed a pretty nasty bug that allowed users to +a themselves if deprotectself was...
[user/henk/code/inspircd.git] / src / modules / m_chanprotect.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                     E-mail:
7  *              <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *          the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "users.h"
18 #include "channels.h"
19 #include "modules.h"
20 #include "inspircd.h"
21
22 /* $ModDesc: Provides channel modes +a and +q */
23 /* $ModDep: ../../include/u_listmode.h */
24
25 #define PROTECT_VALUE 40000
26 #define FOUNDER_VALUE 50000
27
28 const char* fakevalue = "on";
29
30 /* When this is set to true, no restrictions apply to setting or
31  * removal of +qa. This is used while unloading so that the server
32  * can freely clear all of its users of the modes.
33  */
34 bool unload_kludge = false;
35
36 /** Handles basic operation of +qa channel modes
37  */
38 class FounderProtectBase
39 {
40  private:
41         InspIRCd* MyInstance;
42         std::string extend;
43         std::string type;
44         int list;
45         int end;
46         char* dummyptr;
47  protected:
48         bool remove_own_privs;
49  public:
50         FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e, bool remove_own) :
51                 MyInstance(Instance), extend(ext), type(mtype), list(l), end(e), remove_own_privs(remove_own)
52         {
53         }
54
55         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
56         {
57                 userrec* x = MyInstance->FindNick(parameter);
58                 if (x)
59                 {
60                         if (!channel->HasUser(x))
61                         {
62                                 return std::make_pair(false, parameter);
63                         }
64                         else
65                         {
66                                 std::string item = extend+std::string(channel->name);
67                                 if (x->GetExt(item,dummyptr))
68                                 {
69                                         return std::make_pair(true, x->nick);
70                                 }
71                                 else
72                                 {
73                                         return std::make_pair(false, parameter);
74                                 }
75                         }
76                 }
77                 return std::make_pair(false, parameter);
78         }
79
80         void RemoveMode(chanrec* channel, char mc)
81         {
82                 unload_kludge = true;
83                 CUList* cl = channel->GetUsers();
84                 std::string item = extend + std::string(channel->name);
85                 const char* mode_junk[MAXMODES+1];
86                 userrec* n = new userrec(MyInstance);
87                 n->SetFd(FD_MAGIC_NUMBER);
88                 mode_junk[0] = channel->name;
89                 irc::modestacker modestack(false);
90                 std::deque<std::string> stackresult;                            
91                 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
92                 {
93                         if (i->second->GetExt(item, dummyptr))
94                         {
95                                 modestack.Push(mc, i->second->nick);
96                         }
97                 }
98
99                 while (modestack.GetStackedLine(stackresult))
100                 {
101                         for (size_t j = 0; j < stackresult.size(); j++)
102                         {
103                                 mode_junk[j+1] = stackresult[j].c_str();
104                         }
105                         MyInstance->SendMode(mode_junk, stackresult.size() + 1, n);
106                 }
107                 
108                 delete n;
109                 unload_kludge = false;
110         }
111
112         void DisplayList(userrec* user, chanrec* channel)
113         {
114                 CUList* cl = channel->GetUsers();
115                 std::string item = extend+std::string(channel->name);
116                 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
117                 {
118                         if (i->second->GetExt(item, dummyptr))
119                         {
120                                 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->second->nick);
121                         }
122                 }
123                 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
124         }
125
126         userrec* FindAndVerify(std::string &parameter, chanrec* channel)
127         {
128                 userrec* theuser = MyInstance->FindNick(parameter);
129                 if ((!theuser) || (!channel->HasUser(theuser)))
130                 {
131                         parameter = "";
132                         return NULL;
133                 }
134                 return theuser;
135         }
136
137         ModeAction HandleChange(userrec* source, userrec* theuser, bool adding, chanrec* channel, std::string &parameter)
138         {
139                 std::string item = extend+std::string(channel->name);
140
141                 if (adding)
142                 {
143                         if (!theuser->GetExt(item, dummyptr))
144                         {
145                                 theuser->Extend(item, fakevalue);
146                                 parameter = theuser->nick;
147                                 return MODEACTION_ALLOW;
148                         }
149                 }
150                 else
151                 {
152                         if (theuser->GetExt(item, dummyptr))
153                         {
154                                 theuser->Shrink(item);
155                                 parameter = theuser->nick;
156                                 return MODEACTION_ALLOW;
157                         }
158                 }
159                 return MODEACTION_DENY;
160         }
161 };
162
163 /** Abstraction of FounderProtectBase for channel mode +q
164  */
165 class ChanFounder : public ModeHandler, public FounderProtectBase
166 {
167         char* dummyptr;
168  public:
169         ChanFounder(InspIRCd* Instance, bool using_prefixes, bool depriv_self)
170                 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
171                   FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self) { }
172
173         unsigned int GetPrefixRank()
174         {
175                 return FOUNDER_VALUE;
176         }
177
178         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
179         {
180                 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
181         }
182
183         void RemoveMode(chanrec* channel)
184         {
185                 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
186         }
187
188         void RemoveMode(userrec* user)
189         {
190         }
191
192         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
193         {
194                 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
195
196                 if (!theuser)
197                 {
198                         return MODEACTION_DENY;
199                 }
200
201                  // source is a server, or ulined, we'll let them +-q the user.
202                 if ((unload_kludge) || ((source == theuser) && (FounderProtectBase::remove_own_privs)) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
203                 {
204                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
205                 }
206                 else
207                 {
208                         // whoops, someones being naughty!
209                         source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
210                         parameter = "";
211                         return MODEACTION_DENY;
212                 }
213         }
214
215         void DisplayList(userrec* user, chanrec* channel)
216         {
217                 FounderProtectBase::DisplayList(user,channel);
218         }
219 };
220
221 /** Abstraction of FounderProtectBase for channel mode +a
222  */
223 class ChanProtect : public ModeHandler, public FounderProtectBase
224 {
225         char* dummyptr;
226  public:
227         ChanProtect(InspIRCd* Instance, bool using_prefixes, bool depriv_self)
228                 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
229                   FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self) { }
230
231         unsigned int GetPrefixRank()
232         {
233                 return PROTECT_VALUE;
234         }
235
236         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
237         {
238                 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
239         }
240
241         void RemoveMode(chanrec* channel)
242         {
243                 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
244         }
245
246         void RemoveMode(userrec* user)
247         {
248         }
249
250         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
251         {
252                 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
253
254                 if (!theuser)
255                         return MODEACTION_DENY;
256
257                 std::string founder = "cm_founder_"+std::string(channel->name);
258
259                 // source has +q, is a server, or ulined, we'll let them +-a the user.
260                 if ((unload_kludge) || ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
261                 {
262                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
263                 }
264                 else
265                 {
266                         // bzzzt, wrong answer!
267                         source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
268                         return MODEACTION_DENY;
269                 }
270         }
271
272         virtual void DisplayList(userrec* user, chanrec* channel)
273         {
274                 FounderProtectBase::DisplayList(user, channel);
275         }
276
277 };
278
279 class ModuleChanProtect : public Module
280 {
281         
282         bool FirstInGetsFounder;
283         bool QAPrefixes;
284         bool DeprivSelf;
285         bool booting;
286         ChanProtect* cp;
287         ChanFounder* cf;
288         char* dummyptr;
289         
290  public:
291  
292         ModuleChanProtect(InspIRCd* Me)
293                 : Module::Module(Me), FirstInGetsFounder(false), QAPrefixes(false), DeprivSelf(false), booting(true)
294         {       
295                 /* Load config stuff */
296                 OnRehash("");
297                 booting = false;
298
299                 /* Initialise module variables */
300
301                 cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf);
302                 cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf);
303
304                 ServerInstance->AddMode(cp, 'a');
305                 ServerInstance->AddMode(cf, 'q');
306         }
307
308         void Implements(char* List)
309         {
310                 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
311         }
312
313         virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
314         {
315                 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
316                 user->Shrink("cm_founder_"+std::string(chan->name));
317                 user->Shrink("cm_protect_"+std::string(chan->name));
318         }
319
320         virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
321         {
322                 // FIX: when someone parts a channel we must remove their Extensibles!
323                 user->Shrink("cm_founder_"+std::string(channel->name));
324                 user->Shrink("cm_protect_"+std::string(channel->name));
325         }
326
327         virtual void OnRehash(const std::string &parameter)
328         {
329                 /* Create a configreader class and read our flag,
330                  * in old versions this was heap-allocated and the
331                  * object was kept between rehashes...now we just
332                  * stack-allocate it locally.
333                  */
334                 ConfigReader Conf(ServerInstance);
335
336                 bool old_qa = QAPrefixes;
337
338                 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
339                 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
340                 DeprivSelf = Conf.ReadFlag("options","deprotectself",0);
341
342                 /* Did the user change the QA prefixes on the fly?
343                  * If so, remove all instances of the mode, and reinit
344                  * the module with prefixes enabled.
345                  */
346                 if ((old_qa != QAPrefixes) && (!booting))
347                 {
348                         ServerInstance->Modes->DelMode(cp);
349                         ServerInstance->Modes->DelMode(cf);
350                         DELETE(cp);
351                         DELETE(cf);
352                         cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf);
353                         cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf);
354                         ServerInstance->AddMode(cp, 'a');
355                         ServerInstance->AddMode(cf, 'q');
356                         ServerInstance->WriteOpers("*** WARNING: +qa prefixes were enabled or disabled via a REHASH. Clients will probably need to reconnect to pick up this change.");
357                 }
358         }
359         
360         virtual void OnUserJoin(userrec* user, chanrec* channel)
361         {
362                 // if the user is the first user into the channel, mark them as the founder, but only if
363                 // the config option for it is set
364                 if (FirstInGetsFounder)
365                 {
366                         if (channel->GetUserCounter() == 1)
367                         {
368                                 // we're using Extensible::Extend to add data into user objects.
369                                 // this way is best as it adds data thats accessible to other modules
370                                 // (so long as you document your code properly) without breaking anything
371                                 // because its encapsulated neatly in a map.
372
373                                 // Change requested by katsklaw... when the first in is set to get founder,
374                                 // to make it clearer that +q has been given, send that one user the +q notice
375                                 // so that their client's syncronization and their sanity are left intact.
376                                 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
377                                 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
378                                 {
379                                         ServerInstance->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
380                                 }
381                         }
382                 }
383         }
384         
385         virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
386         {
387                 // here we perform access checks, this is the important bit that actually stops kicking/deopping
388                 // etc of protected users. There are many types of access check, we're going to handle
389                 // a relatively small number of them relevent to our module using a switch statement.
390         
391                 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
392                 // don't allow action if:
393                 // (A) Theyre founder (no matter what)
394                 // (B) Theyre protected, and you're not
395                 // always allow the action if:
396                 // (A) The source is ulined
397                 
398                 
399                 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
400                 // without any access checks, we're not worthy :p
401                 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
402                 {
403                         ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck returns ALLOW");
404                         return ACR_ALLOW;
405                 }
406
407                 std::string founder = "cm_founder_"+std::string(channel->name);
408                 std::string protect = "cm_protect_"+std::string(channel->name);
409
410                 switch (access_type)
411                 {
412                         // a user has been deopped. Do we let them? hmmm...
413                         case AC_DEOP:
414                                 ServerInstance->Log(DEBUG,"OnAccessCheck AC_DEOP");
415                                 if (dest->GetExt(founder,dummyptr))
416                                 {
417                                         ServerInstance->Log(DEBUG,"Has %s",founder.c_str());
418                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
419                                         return ACR_DENY;
420                                 }
421                                 else
422                                 {
423                                         ServerInstance->Log(DEBUG,"Doesnt have %s",founder.c_str());
424                                 }
425                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
426                                 {
427                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
428                                         return ACR_DENY;
429                                 }
430                         break;
431
432                         // a user is being kicked. do we chop off the end of the army boot?
433                         case AC_KICK:
434                                 ServerInstance->Log(DEBUG,"OnAccessCheck AC_KICK");
435                                 if (dest->GetExt(founder,dummyptr))
436                                 {
437                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
438                                         return ACR_DENY;
439                                 }
440                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
441                                 {
442                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
443                                         return ACR_DENY;
444                                 }
445                         break;
446
447                         // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
448                         case AC_DEHALFOP:
449                                 if (dest->GetExt(founder,dummyptr))
450                                 {
451                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're a channel founder");
452                                         return ACR_DENY;
453                                 }
454                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
455                                 {
456                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
457                                         return ACR_DENY;
458                                 }
459                         break;
460
461                         // same with devoice.
462                         case AC_DEVOICE:
463                                 if (dest->GetExt(founder,dummyptr))
464                                 {
465                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
466                                         return ACR_DENY;
467                                 }
468                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
469                                 {
470                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
471                                         return ACR_DENY;
472                                 }
473                         break;
474                 }
475                 
476                 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
477                 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck returns DEFAULT");
478                 return ACR_DEFAULT;
479         }
480         
481         virtual ~ModuleChanProtect()
482         {
483                 ServerInstance->Modes->DelMode(cp);
484                 ServerInstance->Modes->DelMode(cf);
485                 DELETE(cp);
486                 DELETE(cf);
487         }
488         
489         virtual Version GetVersion()
490         {
491                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
492         }
493         
494         virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
495         {
496                 /* NOTE: If +qa prefix is on, this is propogated by the channel join,
497                  * so we dont need to propogate it manually
498                  */
499                 if (!QAPrefixes)
500                 {
501                         // this is called when the server is linking into a net and wants to sync channel data.
502                         // we should send our mode changes for the channel here to ensure that other servers
503                         // know whos +q/+a on the channel.
504                         CUList* cl = chan->GetUsers();
505                         string_list commands;
506                         std::string founder = "cm_founder_"+std::string(chan->name);
507                         std::string protect = "cm_protect_"+std::string(chan->name);
508                         irc::modestacker modestack(true);
509                         std::deque<std::string> stackresult;
510                         for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
511                         {
512                                 if (i->second->GetExt(founder,dummyptr))
513                                 {
514                                         modestack.Push('q',i->second->nick);
515                                 }
516                                 if (i->second->GetExt(protect,dummyptr))
517                                 {
518                                         modestack.Push('a',i->second->nick);
519                                 }
520                         }
521                         while (modestack.GetStackedLine(stackresult))
522                         {
523                                 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
524                                 std::string line = mode_join.GetJoined();
525                                 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan, line);
526                         }
527                 }
528         }
529
530 };
531
532
533 class ModuleChanProtectFactory : public ModuleFactory
534 {
535  public:
536         ModuleChanProtectFactory()
537         {
538         }
539         
540         ~ModuleChanProtectFactory()
541         {
542         }
543         
544         virtual Module * CreateModule(InspIRCd* Me)
545         {
546                 return new ModuleChanProtect(Me);
547         }
548         
549 };
550
551
552 extern "C" void * init_module( void )
553 {
554         return new ModuleChanProtectFactory;
555 }