]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_oper.cpp
Extra error detection, thwap me if this comes up with false positives
[user/henk/code/inspircd.git] / src / cmd_oper.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 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include <time.h>
23 #include <string>
24 #ifdef GCC3
25 #include <ext/hash_map>
26 #else
27 #include <hash_map>
28 #endif
29 #include <map>
30 #include <sstream>
31 #include <vector>
32 #include <deque>
33 #include "users.h"
34 #include "ctables.h"
35 #include "globals.h"
36 #include "modules.h"
37 #include "dynamic.h"
38 #include "wildcard.h"
39 #include "message.h"
40 #include "commands.h"
41 #include "mode.h"
42 #include "xline.h"
43 #include "inspstring.h"
44 #include "dnsqueue.h"
45 #include "helperfuncs.h"
46 #include "hashcomp.h"
47 #include "socketengine.h"
48 #include "typedefs.h"
49 #include "command_parse.h"
50 #include "cmd_oper.h"
51
52 extern ServerConfig* Config;
53 extern InspIRCd* ServerInstance;
54 extern int MODCOUNT;
55 extern std::vector<Module*> modules;
56 extern std::vector<ircd_module*> factory;
57 extern time_t TIME;
58 extern user_hash clientlist;
59 extern chan_hash chanlist;
60 extern std::vector<userrec*> all_opers;
61 extern std::vector<userrec*> local_users;
62 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
63
64 bool OneOfMatches(const char* host, const char* hostlist)
65 {
66         std::stringstream hl(hostlist);
67         std::string xhost;
68         while (hl >> xhost)
69         {
70                 log(DEBUG,"Oper: Matching host %s",xhost.c_str());
71                 if (match(host,xhost.c_str()))
72                 {
73                         return true;
74                 }
75         }
76         return false;
77 }
78
79 void cmd_oper::Handle (char **parameters, int pcnt, userrec *user)
80 {
81         char LoginName[MAXBUF];
82         char Password[MAXBUF];
83         char OperType[MAXBUF];
84         char TypeName[MAXBUF];
85         char HostName[MAXBUF];
86         char TheHost[MAXBUF];
87         int j;
88         bool found = false;
89         bool fail2 = false;
90
91         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
92
93         for (int i = 0; i < Config->ConfValueEnum(Config->config_data, "oper"); i++)
94         {
95                 Config->ConfValue(Config->config_data, "oper", "name", i, LoginName, MAXBUF);
96                 Config->ConfValue(Config->config_data, "oper", "password", i, Password, MAXBUF);
97                 Config->ConfValue(Config->config_data, "oper", "type", i, OperType, MAXBUF);
98                 Config->ConfValue(Config->config_data, "oper", "host", i, HostName, MAXBUF);
99
100                 if ((!strcmp(LoginName,parameters[0])) && (!operstrcmp(Password,parameters[1])) && (OneOfMatches(TheHost,HostName)))
101                 {
102                         fail2 = true;
103                         for (j =0; j < Config->ConfValueEnum(Config->config_data, "type"); j++)
104                         {
105                                 Config->ConfValue(Config->config_data, "type","name", j, TypeName, MAXBUF);
106
107                                 if (!strcmp(TypeName,OperType))
108                                 {
109                                         /* found this oper's opertype */
110                                         Config->ConfValue(Config->config_data, "type","host", j, HostName, MAXBUF);
111                                         if (*HostName)
112                                                 ChangeDisplayedHost(user,HostName);
113                                         if (!isnick(TypeName))
114                                         {
115                                                 WriteServ(user->fd,"491 %s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick);
116                                                 WriteOpers("*** CONFIGURATION ERROR! Oper type invalid for OperType '%s'",OperType);
117                                                 log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type erroneous.",user->nick,user->ident,user->host);
118                                                 return;
119                                         }
120                                         strlcpy(user->oper,TypeName,NICKMAX-1);
121                                         found = true;
122                                         fail2 = false;
123                                         break;
124                                 }
125                         }
126                 }
127                 if (found)
128                         break;
129         }
130         if (found)
131         {
132                 /* correct oper credentials */
133                 WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
134                 WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,OperType);
135                 if (!strchr(user->modes,'o'))
136                 {
137                         strcat(user->modes,"o");
138                         WriteServ(user->fd,"MODE %s :+o",user->nick);
139                         FOREACH_MOD(I_OnOper,OnOper(user,OperType));
140                         log(DEFAULT,"OPER: %s!%s@%s opered as type: %s",user->nick,user->ident,user->host,OperType);
141                         AddOper(user);
142                         FOREACH_MOD(I_OnPostOper,OnPostOper(user,OperType));
143                 }
144         }
145         else
146         {
147                 if (!fail2)
148                 {
149                         WriteServ(user->fd,"491 %s :Invalid oper credentials",user->nick);
150                         WriteOpers("*** WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
151                         log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: user, host or password did not match.",user->nick,user->ident,user->host);
152                 }
153                 else
154                 {
155                         WriteServ(user->fd,"491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
156                         WriteOpers("*** CONFIGURATION ERROR! Oper block mismatch for OperType %s",OperType);
157                         log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type nonexistent.",user->nick,user->ident,user->host);
158                 }
159         }
160         return;
161 }