]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
ba8b2e1b7692a881d01140c63a4a6af453511cf9
[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://www.inspircd.org/wiki/index.php/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_OnLocalTopicChange, I_OnRequest };
45                 ServerInstance->Modules->Attach(eventlist, this, 8);
46         }
47
48         virtual void OnRehash(User* user, const std::string &parameter)
49         {
50                 // on a rehash we delete our classes for good measure and create them again.
51                 ConfigReader* Conf = new ConfigReader(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                 delete Conf;
67         }
68
69
70         virtual void OnPostCommand(const std::string &command, const std::vector<std::string> &parameters, User *user, CmdResult result, const std::string &original_line)
71         {
72                 if (OverriddenMode)
73                 {
74                         if ((irc::string(command.c_str()) == "MODE") && (result == CMD_SUCCESS) && !ServerInstance->Modes->GetLastParse().empty())
75                         {
76                                 std::string msg = std::string(user->nick)+" Overriding modes: "+ServerInstance->Modes->GetLastParse()+" [Detail: ";
77                                 if (OverOps)
78                                         msg += ConvToStr(OverOps)+" op"+(OverOps != 1 ? "s" : "")+", ";
79                                 if (OverDeops)
80                                         msg += ConvToStr(OverDeops)+" deop"+(OverDeops != 1 ? "s" : "")+", ";
81                                 if (OverVoices)
82                                         msg += ConvToStr(OverVoices)+" voice"+(OverVoices != 1 ? "s" : "")+", ";
83                                 if (OverDevoices)
84                                         msg += ConvToStr(OverDevoices)+" devoice"+(OverDevoices != 1 ? "s" : "")+", ";
85                                 if (OverHalfops)
86                                         msg += ConvToStr(OverHalfops)+" halfop"+(OverHalfops != 1 ? "s" : "")+", ";
87                                 if (OverDehalfops)
88                                         msg += ConvToStr(OverDehalfops)+" dehalfop"+(OverDehalfops != 1 ? "s" : "")+", ";
89                                 if (OverOther)
90                                         msg += "others, ";
91                                 msg.replace(msg.length()-2, 2, 1, ']');
92                                 ServerInstance->SNO->WriteToSnoMask('G',msg);
93                         }
94
95                         OverriddenMode = OverOther = false;
96                         OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
97                 }
98         }
99
100         virtual void On005Numeric(std::string &output)
101         {
102                 output.append(" OVERRIDE");
103         }
104
105         virtual bool CanOverride(User* source, const char* token)
106         {
107                 // checks to see if the oper's type has <type:override>
108                 override_t::iterator j = overrides.find(source->oper);
109
110                 if (j != overrides.end())
111                 {
112                         // its defined or * is set, return its value as a boolean for if the token is set
113                         return ((j->second.find(token, 0) != std::string::npos) || (j->second.find("*", 0) != std::string::npos));
114                 }
115
116                 // its not defined at all, count as false
117                 return false;
118         }
119
120
121         virtual int OnLocalTopicChange(User *source, Channel *channel, const std::string &topic)
122         {
123                 if (IS_OPER(source) && CanOverride(source, "TOPIC"))
124                 {
125                         if (!channel->HasUser(source) || (channel->IsModeSet('t') && channel->GetStatus(source) < STATUS_HOP))
126                         {
127                                 ServerInstance->SNO->WriteToSnoMask('G',std::string(source->nick)+" used oper override to change a topic on "+std::string(channel->name));
128                         }
129
130                         // Explicit allow
131                         return -1;
132                 }
133
134                 return 0;
135         }
136
137         virtual int OnUserPreKick(User* source, User* user, Channel* chan, const std::string &reason)
138         {
139                 if (IS_OPER(source) && CanOverride(source,"KICK"))
140                 {
141                         // If the kicker's status is less than the target's,                    or      the kicker's status is less than or equal to voice
142                         if ((chan->GetStatus(source) < chan->GetStatus(user))                   || (chan->GetStatus(source) <= STATUS_VOICE))
143                         {
144                                 ServerInstance->SNO->WriteToSnoMask('G',std::string(source->nick)+" used oper override to kick "+std::string(user->nick)+" on "+std::string(chan->name)+" ("+reason+")");
145                         }
146                         /* Returning -1 explicitly allows the kick */
147                         return -1;
148                 }
149                 return 0;
150         }
151
152         virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
153         {
154                 if (!IS_OPER(source))
155                         return ACR_DEFAULT;
156                 if (!source || !channel)
157                         return ACR_DEFAULT;
158
159                 int mode = STATUS_NORMAL;
160                 if (channel->HasUser(source))
161                         mode = channel->GetStatus(source);
162                 bool over_this = false;
163
164                 switch (access_type)
165                 {
166                         case AC_DEOP:
167                                 if (mode < STATUS_OP && CanOverride(source,"MODEDEOP"))
168                                 {
169                                         over_this = true;
170                                         OverDeops++;
171                                 }
172                         break;
173                         case AC_OP:
174                                 if (mode < STATUS_OP && CanOverride(source,"MODEOP"))
175                                 {
176                                         over_this = true;
177                                         OverOps++;
178                                 }
179                         break;
180                         case AC_VOICE:
181                                 if (mode < STATUS_HOP && CanOverride(source,"MODEVOICE"))
182                                 {
183                                         over_this = true;
184                                         OverVoices++;
185                                 }
186                         break;
187                         case AC_DEVOICE:
188                                 if (mode < STATUS_HOP && CanOverride(source,"MODEDEVOICE"))
189                                 {
190                                         over_this = true;
191                                         OverDevoices++;
192                                 }
193                         break;
194                         case AC_HALFOP:
195                                 if (mode < STATUS_OP && CanOverride(source,"MODEHALFOP"))
196                                 {
197                                         over_this = true;
198                                         OverHalfops++;
199                                 }
200                         break;
201                         case AC_DEHALFOP:
202                                 if (mode < STATUS_OP && CanOverride(source,"MODEDEHALFOP"))
203                                 {
204                                         over_this = true;
205                                         OverDehalfops++;
206                                 }
207                         break;
208                         case AC_GENERAL_MODE:
209                         {
210                                 std::string modes = ServerInstance->Modes->GetLastParse();
211                                 bool ohv_only = (modes.find_first_not_of("+-ohv") == std::string::npos);
212
213                                 if (mode < STATUS_HOP && (ohv_only || CanOverride(source,"OTHERMODE")))
214                                 {
215                                         over_this = true;
216                                         if (!ohv_only)
217                                                 OverOther = true;
218                                 }
219                         }
220                         break;
221                 }
222
223                 if (over_this)
224                 {
225                         OverriddenMode = true;
226                         return ACR_ALLOW;
227                 }
228                 else
229                 {
230                         return ACR_DEFAULT;
231                 }
232         }
233
234         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
235         {
236                 if (IS_LOCAL(user) && IS_OPER(user))
237                 {
238                         if (chan)
239                         {
240                                 if ((chan->modes[CM_INVITEONLY]) && (CanOverride(user,"INVITE")))
241                                 {
242                                         irc::string x(chan->name.c_str());
243                                         if (!user->IsInvited(x))
244                                         {
245                                                 if (RequireKey && keygiven != "override")
246                                                 {
247                                                         // Can't join normally -- must use a special key to bypass restrictions
248                                                         user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
249                                                         return 1;
250                                                 }
251
252                                                 if (NoisyOverride)
253                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass invite-only", cname, user->nick.c_str());
254                                                 ServerInstance->SNO->WriteToSnoMask('G', user->nick+" used oper override to bypass +i on "+std::string(cname));
255                                         }
256                                         return -1;
257                                 }
258
259                                 if ((chan->modes[CM_KEY]) && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter('k'))
260                                 {
261                                         if (RequireKey && keygiven != "override")
262                                         {
263                                                 // Can't join normally -- must use a special key to bypass restrictions
264                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
265                                                 return 1;
266                                         }
267
268                                         if (NoisyOverride)
269                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel key", cname, user->nick.c_str());
270                                         ServerInstance->SNO->WriteToSnoMask('G', user->nick+" used oper override to bypass +k on "+std::string(cname));
271                                         return -1;
272                                 }
273
274                                 if ((chan->modes[CM_LIMIT]) && (chan->GetUserCounter() >= atoi(chan->GetModeParameter('l').c_str())) && (CanOverride(user,"LIMIT")))
275                                 {
276                                         if (RequireKey && keygiven != "override")
277                                         {
278                                                 // Can't join normally -- must use a special key to bypass restrictions
279                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
280                                                 return 1;
281                                         }
282
283                                         if (NoisyOverride)
284                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel limit", cname, user->nick.c_str());
285                                         ServerInstance->SNO->WriteToSnoMask('G', user->nick+" used oper override to bypass +l on "+std::string(cname));
286                                         return -1;
287                                 }
288
289                                 if (chan->IsBanned(user) && CanOverride(user,"BANWALK"))
290                                 {
291                                         if (RequireKey && keygiven != "override")
292                                         {
293                                                 // Can't join normally -- must use a special key to bypass restrictions
294                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
295                                                 return 1;
296                                         }
297
298                                         if (NoisyOverride)
299                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass channel ban", cname, user->nick.c_str());
300                                         ServerInstance->SNO->WriteToSnoMask('G',"%s used oper override to bypass channel ban on %s", user->nick.c_str(), cname);
301                                         return -1;
302                                 }
303                         }
304                 }
305                 return 0;
306         }
307
308         virtual const char* OnRequest(Request* request)
309         {
310                 if(strcmp(OVRREQID, request->GetId()) == 0)
311                 {
312                         OVRrequest* req = static_cast<OVRrequest*>(request);
313                         return this->CanOverride(req->requser,req->reqtoken.c_str()) ? "yes":"";
314                 }
315                 return NULL;
316         }
317
318         virtual ~ModuleOverride()
319         {
320                 ServerInstance->Modules->UnpublishFeature("Override");
321                 ServerInstance->SNO->DisableSnomask('G');
322         }
323
324         virtual Version GetVersion()
325         {
326                 return Version("$Id$",VF_VENDOR,API_VERSION);
327         }
328 };
329
330 MODULE_INIT(ModuleOverride)