]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
Allow commands to optionally route themselves using ENCAP
[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_OnLocalTopicChange, 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 int OnLocalTopicChange(User *source, Channel *channel, const std::string &topic)
120         {
121                 if (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 -1;
130                 }
131
132                 return 0;
133         }
134
135         virtual int 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                         /* Returning -1 explicitly allows the kick */
145                         return -1;
146                 }
147                 return 0;
148         }
149
150         virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
151         {
152                 if (!IS_OPER(source))
153                         return ACR_DEFAULT;
154                 if (!source || !channel)
155                         return ACR_DEFAULT;
156
157                 int mode = STATUS_NORMAL;
158                 if (channel->HasUser(source))
159                         mode = channel->GetStatus(source);
160                 bool over_this = false;
161
162                 switch (access_type)
163                 {
164                         case AC_DEOP:
165                                 if (mode < STATUS_OP && CanOverride(source,"MODEDEOP"))
166                                 {
167                                         over_this = true;
168                                         OverDeops++;
169                                 }
170                         break;
171                         case AC_OP:
172                                 if (mode < STATUS_OP && CanOverride(source,"MODEOP"))
173                                 {
174                                         over_this = true;
175                                         OverOps++;
176                                 }
177                         break;
178                         case AC_VOICE:
179                                 if (mode < STATUS_HOP && CanOverride(source,"MODEVOICE"))
180                                 {
181                                         over_this = true;
182                                         OverVoices++;
183                                 }
184                         break;
185                         case AC_DEVOICE:
186                                 if (mode < STATUS_HOP && CanOverride(source,"MODEDEVOICE"))
187                                 {
188                                         over_this = true;
189                                         OverDevoices++;
190                                 }
191                         break;
192                         case AC_HALFOP:
193                                 if (mode < STATUS_OP && CanOverride(source,"MODEHALFOP"))
194                                 {
195                                         over_this = true;
196                                         OverHalfops++;
197                                 }
198                         break;
199                         case AC_DEHALFOP:
200                                 if (mode < STATUS_OP && CanOverride(source,"MODEDEHALFOP"))
201                                 {
202                                         over_this = true;
203                                         OverDehalfops++;
204                                 }
205                         break;
206                         case AC_GENERAL_MODE:
207                         {
208                                 std::string modes = ServerInstance->Modes->GetLastParse();
209                                 bool ohv_only = (modes.find_first_not_of("+-ohv") == std::string::npos);
210
211                                 if (mode < STATUS_HOP && (ohv_only || CanOverride(source,"OTHERMODE")))
212                                 {
213                                         over_this = true;
214                                         if (!ohv_only)
215                                                 OverOther = true;
216                                 }
217                         }
218                         break;
219                 }
220
221                 if (over_this)
222                 {
223                         OverriddenMode = true;
224                         return ACR_ALLOW;
225                 }
226                 else
227                 {
228                         return ACR_DEFAULT;
229                 }
230         }
231
232         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
233         {
234                 if (IS_LOCAL(user) && IS_OPER(user))
235                 {
236                         if (chan)
237                         {
238                                 if ((chan->modes[CM_INVITEONLY]) && (CanOverride(user,"INVITE")))
239                                 {
240                                         irc::string x(chan->name.c_str());
241                                         if (!user->IsInvited(x))
242                                         {
243                                                 if (RequireKey && keygiven != "override")
244                                                 {
245                                                         // Can't join normally -- must use a special key to bypass restrictions
246                                                         user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
247                                                         return 0;
248                                                 }
249
250                                                 if (NoisyOverride)
251                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass invite-only", cname, user->nick.c_str());
252                                                 ServerInstance->SNO->WriteGlobalSno('G', user->nick+" used oper override to bypass +i on "+std::string(cname));
253                                         }
254                                         return -1;
255                                 }
256
257                                 if ((chan->modes[CM_KEY]) && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter('k'))
258                                 {
259                                         if (RequireKey && keygiven != "override")
260                                         {
261                                                 // Can't join normally -- must use a special key to bypass restrictions
262                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
263                                                 return 0;
264                                         }
265
266                                         if (NoisyOverride)
267                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel key", cname, user->nick.c_str());
268                                         ServerInstance->SNO->WriteGlobalSno('G', user->nick+" used oper override to bypass +k on "+std::string(cname));
269                                         return -1;
270                                 }
271
272                                 if ((chan->modes[CM_LIMIT]) && (chan->GetUserCounter() >= atoi(chan->GetModeParameter('l').c_str())) && (CanOverride(user,"LIMIT")))
273                                 {
274                                         if (RequireKey && keygiven != "override")
275                                         {
276                                                 // Can't join normally -- must use a special key to bypass restrictions
277                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
278                                                 return 0;
279                                         }
280
281                                         if (NoisyOverride)
282                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel limit", cname, user->nick.c_str());
283                                         ServerInstance->SNO->WriteGlobalSno('G', user->nick+" used oper override to bypass +l on "+std::string(cname));
284                                         return -1;
285                                 }
286
287                                 if (chan->IsBanned(user) && CanOverride(user,"BANWALK"))
288                                 {
289                                         if (RequireKey && keygiven != "override")
290                                         {
291                                                 // Can't join normally -- must use a special key to bypass restrictions
292                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
293                                                 return 0;
294                                         }
295
296                                         if (NoisyOverride)
297                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass channel ban", cname, user->nick.c_str());
298                                         ServerInstance->SNO->WriteGlobalSno('G',"%s used oper override to bypass channel ban on %s", user->nick.c_str(), cname);
299                                         return -1;
300                                 }
301                         }
302                 }
303                 return 0;
304         }
305
306         virtual const char* OnRequest(Request* request)
307         {
308                 if(strcmp(OVRREQID, request->GetId()) == 0)
309                 {
310                         OVRrequest* req = static_cast<OVRrequest*>(request);
311                         return this->CanOverride(req->requser,req->reqtoken.c_str()) ? "yes":"";
312                 }
313                 return NULL;
314         }
315
316         virtual ~ModuleOverride()
317         {
318                 ServerInstance->Modules->UnpublishFeature("Override");
319                 ServerInstance->SNO->DisableSnomask('G');
320         }
321
322         virtual Version GetVersion()
323         {
324                 return Version("$Id$",VF_VENDOR,API_VERSION);
325         }
326 };
327
328 MODULE_INIT(ModuleOverride)