]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
d0ec5d8fe87e4229fe6d400f36e8e3cda3c2e3fd
[user/henk/code/inspircd.git] / src / modules / m_override.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "m_override.h"
16
17 /* $ModDesc: Provides support for unreal-style oper-override */
18
19 typedef std::map<std::string,std::string> override_t;
20
21 class ModuleOverride : public Module
22 {
23         override_t overrides;
24         bool RequireKey;
25         bool NoisyOverride;
26         bool OverriddenMode;
27         bool OverOther;
28         int OverOps, OverDeops, OverVoices, OverDevoices, OverHalfops, OverDehalfops;
29
30  public:
31
32         ModuleOverride(InspIRCd* Me)
33                 : Module(Me)
34         {
35                 // read our config options (main config file)
36                 OnRehash(NULL);
37                 ServerInstance->SNO->EnableSnomask('G', "GODMODE");
38                 if (!ServerInstance->Modules->PublishFeature("Override", this))
39                 {
40                         throw ModuleException("m_override: Unable to publish feature 'Override'");
41                 }
42                 OverriddenMode = OverOther = false;
43                 OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
44                 Implementation eventlist[] = { I_OnRehash, I_OnAccessCheck, I_On005Numeric, I_OnUserPreJoin, I_OnUserPreKick, I_OnPostCommand, I_OnPreTopicChange, I_OnRequest };
45                 ServerInstance->Modules->Attach(eventlist, this, 8);
46         }
47
48         virtual void OnRehash(User* user)
49         {
50                 // on a rehash we delete our classes for good measure and create them again.
51                 ConfigReader Conf(ServerInstance);
52
53                 // re-read our config options on a rehash
54                 NoisyOverride = Conf.ReadFlag("override", "noisy", 0);
55                 RequireKey = Conf.ReadFlag("override", "requirekey", 0);
56
57                 overrides.clear();
58
59                 for (int j =0; j < Conf.Enumerate("type"); j++)
60                 {
61                         std::string typen = Conf.ReadValue("type","name",j);
62                         std::string tokenlist = Conf.ReadValue("type","override",j);
63                         overrides[typen] = tokenlist;
64                 }
65         }
66
67
68         virtual void OnPostCommand(const std::string &command, const std::vector<std::string> &parameters, User *user, CmdResult result, const std::string &original_line)
69         {
70                 if (OverriddenMode)
71                 {
72                         if ((irc::string(command.c_str()) == "MODE") && (result == CMD_SUCCESS) && !ServerInstance->Modes->GetLastParse().empty())
73                         {
74                                 std::string msg = std::string(user->nick)+" overriding modes: "+ServerInstance->Modes->GetLastParse()+" [Detail: ";
75                                 if (OverOps)
76                                         msg += ConvToStr(OverOps)+" op"+(OverOps != 1 ? "s" : "")+", ";
77                                 if (OverDeops)
78                                         msg += ConvToStr(OverDeops)+" deop"+(OverDeops != 1 ? "s" : "")+", ";
79                                 if (OverVoices)
80                                         msg += ConvToStr(OverVoices)+" voice"+(OverVoices != 1 ? "s" : "")+", ";
81                                 if (OverDevoices)
82                                         msg += ConvToStr(OverDevoices)+" devoice"+(OverDevoices != 1 ? "s" : "")+", ";
83                                 if (OverHalfops)
84                                         msg += ConvToStr(OverHalfops)+" halfop"+(OverHalfops != 1 ? "s" : "")+", ";
85                                 if (OverDehalfops)
86                                         msg += ConvToStr(OverDehalfops)+" dehalfop"+(OverDehalfops != 1 ? "s" : "")+", ";
87                                 if (OverOther)
88                                         msg += "others, ";
89                                 msg.replace(msg.length()-2, 2, 1, ']');
90                                 ServerInstance->SNO->WriteGlobalSno('G',msg);
91                         }
92
93                         OverriddenMode = OverOther = false;
94                         OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
95                 }
96         }
97
98         virtual void On005Numeric(std::string &output)
99         {
100                 output.append(" OVERRIDE");
101         }
102
103         virtual bool CanOverride(User* source, const char* token)
104         {
105                 // checks to see if the oper's type has <type:override>
106                 override_t::iterator j = overrides.find(source->oper);
107
108                 if (j != overrides.end())
109                 {
110                         // its defined or * is set, return its value as a boolean for if the token is set
111                         return ((j->second.find(token, 0) != std::string::npos) || (j->second.find("*", 0) != std::string::npos));
112                 }
113
114                 // its not defined at all, count as false
115                 return false;
116         }
117
118
119         virtual ModResult OnPreTopicChange(User *source, Channel *channel, const std::string &topic)
120         {
121                 if (IS_LOCAL(source) && IS_OPER(source) && CanOverride(source, "TOPIC"))
122                 {
123                         if (!channel->HasUser(source) || (channel->IsModeSet('t') && channel->GetStatus(source) < STATUS_HOP))
124                         {
125                                 ServerInstance->SNO->WriteGlobalSno('G',std::string(source->nick)+" used oper override to change a topic on "+std::string(channel->name));
126                         }
127
128                         // Explicit allow
129                         return MOD_RES_ALLOW;
130                 }
131
132                 return MOD_RES_PASSTHRU;
133         }
134
135         virtual ModResult OnUserPreKick(User* source, User* user, Channel* chan, const std::string &reason)
136         {
137                 if (IS_OPER(source) && CanOverride(source,"KICK"))
138                 {
139                         // If the kicker's status is less than the target's,                    or      the kicker's status is less than or equal to voice
140                         if ((chan->GetStatus(source) < chan->GetStatus(user))                   || (chan->GetStatus(source) <= STATUS_VOICE))
141                         {
142                                 ServerInstance->SNO->WriteGlobalSno('G',std::string(source->nick)+" used oper override to kick "+std::string(user->nick)+" on "+std::string(chan->name)+" ("+reason+")");
143                         }
144                         return MOD_RES_ALLOW;
145                 }
146                 return MOD_RES_PASSTHRU;
147         }
148
149         virtual ModResult OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
150         {
151                 if (!IS_OPER(source))
152                         return MOD_RES_PASSTHRU;
153                 if (!source || !channel)
154                         return MOD_RES_PASSTHRU;
155
156                 int mode = STATUS_NORMAL;
157                 if (channel->HasUser(source))
158                         mode = channel->GetStatus(source);
159                 bool over_this = false;
160
161                 switch (access_type)
162                 {
163                         case AC_DEOP:
164                                 if (mode < STATUS_OP && CanOverride(source,"MODEDEOP"))
165                                 {
166                                         over_this = true;
167                                         OverDeops++;
168                                 }
169                         break;
170                         case AC_OP:
171                                 if (mode < STATUS_OP && CanOverride(source,"MODEOP"))
172                                 {
173                                         over_this = true;
174                                         OverOps++;
175                                 }
176                         break;
177                         case AC_VOICE:
178                                 if (mode < STATUS_HOP && CanOverride(source,"MODEVOICE"))
179                                 {
180                                         over_this = true;
181                                         OverVoices++;
182                                 }
183                         break;
184                         case AC_DEVOICE:
185                                 if (mode < STATUS_HOP && CanOverride(source,"MODEDEVOICE"))
186                                 {
187                                         over_this = true;
188                                         OverDevoices++;
189                                 }
190                         break;
191                         case AC_HALFOP:
192                                 if (mode < STATUS_OP && CanOverride(source,"MODEHALFOP"))
193                                 {
194                                         over_this = true;
195                                         OverHalfops++;
196                                 }
197                         break;
198                         case AC_DEHALFOP:
199                                 if (mode < STATUS_OP && CanOverride(source,"MODEDEHALFOP"))
200                                 {
201                                         over_this = true;
202                                         OverDehalfops++;
203                                 }
204                         break;
205                         case AC_GENERAL_MODE:
206                         {
207                                 std::string modes = ServerInstance->Modes->GetLastParse();
208                                 bool ohv_only = (modes.find_first_not_of("+-ohv") == std::string::npos);
209
210                                 if (mode < STATUS_HOP && (ohv_only || CanOverride(source,"OTHERMODE")))
211                                 {
212                                         over_this = true;
213                                         if (!ohv_only)
214                                                 OverOther = true;
215                                 }
216                         }
217                         break;
218                 }
219
220                 if (over_this)
221                 {
222                         OverriddenMode = true;
223                         return MOD_RES_ALLOW;
224                 }
225                 else
226                 {
227                         return MOD_RES_PASSTHRU;
228                 }
229         }
230
231         virtual ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
232         {
233                 if (IS_LOCAL(user) && IS_OPER(user))
234                 {
235                         if (chan)
236                         {
237                                 if ((chan->modes[CM_INVITEONLY]) && (CanOverride(user,"INVITE")))
238                                 {
239                                         irc::string x(chan->name.c_str());
240                                         if (!user->IsInvited(x))
241                                         {
242                                                 if (RequireKey && keygiven != "override")
243                                                 {
244                                                         // Can't join normally -- must use a special key to bypass restrictions
245                                                         user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
246                                                         return MOD_RES_PASSTHRU;
247                                                 }
248
249                                                 if (NoisyOverride)
250                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass invite-only", cname, user->nick.c_str());
251                                                 ServerInstance->SNO->WriteGlobalSno('G', user->nick+" used oper override to bypass +i on "+std::string(cname));
252                                         }
253                                         return MOD_RES_ALLOW;
254                                 }
255
256                                 if ((chan->modes[CM_KEY]) && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter('k'))
257                                 {
258                                         if (RequireKey && keygiven != "override")
259                                         {
260                                                 // Can't join normally -- must use a special key to bypass restrictions
261                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
262                                                 return MOD_RES_PASSTHRU;
263                                         }
264
265                                         if (NoisyOverride)
266                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel key", cname, user->nick.c_str());
267                                         ServerInstance->SNO->WriteGlobalSno('G', user->nick+" used oper override to bypass +k on "+std::string(cname));
268                                         return MOD_RES_ALLOW;
269                                 }
270
271                                 if ((chan->modes[CM_LIMIT]) && (chan->GetUserCounter() >= atoi(chan->GetModeParameter('l').c_str())) && (CanOverride(user,"LIMIT")))
272                                 {
273                                         if (RequireKey && keygiven != "override")
274                                         {
275                                                 // Can't join normally -- must use a special key to bypass restrictions
276                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
277                                                 return MOD_RES_PASSTHRU;
278                                         }
279
280                                         if (NoisyOverride)
281                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel limit", cname, user->nick.c_str());
282                                         ServerInstance->SNO->WriteGlobalSno('G', user->nick+" used oper override to bypass +l on "+std::string(cname));
283                                         return MOD_RES_ALLOW;
284                                 }
285
286                                 if (chan->IsBanned(user) && CanOverride(user,"BANWALK"))
287                                 {
288                                         if (RequireKey && keygiven != "override")
289                                         {
290                                                 // Can't join normally -- must use a special key to bypass restrictions
291                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
292                                                 return MOD_RES_PASSTHRU;
293                                         }
294
295                                         if (NoisyOverride)
296                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass channel ban", cname, user->nick.c_str());
297                                         ServerInstance->SNO->WriteGlobalSno('G',"%s used oper override to bypass channel ban on %s", user->nick.c_str(), cname);
298                                         return MOD_RES_ALLOW;
299                                 }
300                         }
301                 }
302                 return MOD_RES_PASSTHRU;
303         }
304
305         virtual const char* OnRequest(Request* request)
306         {
307                 if(strcmp(OVRREQID, request->GetId()) == 0)
308                 {
309                         OVRrequest* req = static_cast<OVRrequest*>(request);
310                         return this->CanOverride(req->requser,req->reqtoken.c_str()) ? "yes":"";
311                 }
312                 return NULL;
313         }
314
315         virtual ~ModuleOverride()
316         {
317                 ServerInstance->Modules->UnpublishFeature("Override");
318                 ServerInstance->SNO->DisableSnomask('G');
319         }
320
321         virtual Version GetVersion()
322         {
323                 return Version("$Id$",VF_VENDOR,API_VERSION);
324         }
325 };
326
327 MODULE_INIT(ModuleOverride)