]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_oper.cpp
2b4e3e206e907daeb44c4846c614080ae071a3c8
[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 #include <string>
18 #include <sstream>
19 #include <vector>
20 #include "inspircd_config.h"
21 #include "configreader.h"
22 #include "typedefs.h"
23 #include "users.h"
24 #include "globals.h"
25 #include "modules.h"
26 #include "dynamic.h"
27 #include "wildcard.h"
28 #include "commands.h"
29 #include "mode.h"
30 #include "xline.h"
31 #include "inspstring.h"
32 #include "helperfuncs.h"
33 #include "hashcomp.h"
34 #include "socketengine.h"
35
36 #include "command_parse.h"
37 #include "commands/cmd_oper.h"
38
39 extern InspIRCd* ServerInstance;
40 extern int MODCOUNT;
41 extern ModuleList modules;
42 extern FactoryList factory;
43 extern time_t TIME;
44
45 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
46 {
47         std::stringstream hl(hostlist);
48         std::string xhost;
49         while (hl >> xhost)
50         {
51                 log(DEBUG,"Oper: Matching host %s",xhost.c_str());
52                 if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
53                 {
54                         return true;
55                 }
56         }
57         return false;
58 }
59
60 void cmd_oper::Handle (const char** parameters, int pcnt, userrec *user)
61 {
62         char LoginName[MAXBUF];
63         char Password[MAXBUF];
64         char OperType[MAXBUF];
65         char TypeName[MAXBUF];
66         char HostName[MAXBUF];
67         char TheHost[MAXBUF];
68         char TheIP[MAXBUF];
69         int j;
70         bool found = false;
71         bool fail2 = false;
72
73         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
74         snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
75
76         for (int i = 0; i < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "oper"); i++)
77         {
78                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "name", i, LoginName, MAXBUF);
79                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "password", i, Password, MAXBUF);
80                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "type", i, OperType, MAXBUF);
81                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "host", i, HostName, MAXBUF);
82
83                 if ((!strcmp(LoginName,parameters[0])) && (!operstrcmp(Password,parameters[1])) && (OneOfMatches(TheHost,TheIP,HostName)))
84                 {
85                         fail2 = true;
86                         for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
87                         {
88                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","name", j, TypeName, MAXBUF);
89
90                                 if (!strcmp(TypeName,OperType))
91                                 {
92                                         /* found this oper's opertype */
93                                         ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
94                                         if (*HostName)
95                                                 user->ChangeDisplayedHost(HostName);
96                                         if (!ServerInstance->IsNick(TypeName))
97                                         {
98                                                 user->WriteServ("491 %s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick);
99                                                 ServerInstance->WriteOpers("*** CONFIGURATION ERROR! Oper type invalid for OperType '%s'",OperType);
100                                                 log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type erroneous.",user->nick,user->ident,user->host);
101                                                 return;
102                                         }
103                                         found = true;
104                                         fail2 = false;
105                                         break;
106                                 }
107                         }
108                 }
109                 if (found)
110                         break;
111         }
112         if (found)
113         {
114                 /* correct oper credentials */
115                 ServerInstance->WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
116                 user->WriteServ("381 %s :You are now an IRC operator of type %s",user->nick,OperType);
117                 if (!user->modes[UM_OPERATOR])
118                         user->Oper(OperType);
119         }
120         else
121         {
122                 if (!fail2)
123                 {
124                         user->WriteServ("491 %s :Invalid oper credentials",user->nick);
125                         ServerInstance->WriteOpers("*** WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
126                         log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: user, host or password did not match.",user->nick,user->ident,user->host);
127                 }
128                 else
129                 {
130                         user->WriteServ("491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
131                         ServerInstance->WriteOpers("*** CONFIGURATION ERROR! Oper block mismatch for OperType %s",OperType);
132                         log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type nonexistent.",user->nick,user->ident,user->host);
133                 }
134         }
135         return;
136 }