]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_oper.cpp
Tons more useful detail when failing to oper (and when successfully opering too)...
[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 "configreader.h"
18 #include "typedefs.h"
19 #include "users.h"
20 #include "modules.h"
21 #include "wildcard.h"
22 #include "commands/cmd_oper.h"
23
24 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
25 {
26         std::stringstream hl(hostlist);
27         std::string xhost;
28         while (hl >> xhost)
29         {
30                 if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
31                 {
32                         return true;
33                 }
34         }
35         return false;
36 }
37
38
39
40 extern "C" command_t* init_command(InspIRCd* Instance)
41 {
42         return new cmd_oper(Instance);
43 }
44
45 CmdResult cmd_oper::Handle (const char** parameters, int pcnt, userrec *user)
46 {
47         char LoginName[MAXBUF];
48         char Password[MAXBUF];
49         char OperType[MAXBUF];
50         char TypeName[MAXBUF];
51         char HostName[MAXBUF];
52         char TheHost[MAXBUF];
53         char TheIP[MAXBUF];
54         int j;
55         bool found = false;
56         bool type_invalid = false;
57
58         bool match_login = false;
59         bool match_pass = false;
60         bool match_hosts = false;
61
62         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
63         snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
64
65         for (int i = 0; i < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "oper"); i++)
66         {
67                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "name", i, LoginName, MAXBUF);
68                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "password", i, Password, MAXBUF);
69                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "type", i, OperType, MAXBUF);
70                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "host", i, HostName, MAXBUF);
71
72                 match_login = !strcmp(LoginName,parameters[0]);
73                 match_pass = !ServerInstance->OperPassCompare(Password,parameters[1]);
74                 match_hosts = OneOfMatches(TheHost,TheIP,HostName);
75
76                 if (match_login && match_pass && match_hosts)
77                 {
78                         type_invalid = true;
79                         for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
80                         {
81                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","name", j, TypeName, MAXBUF);
82
83                                 if (!strcmp(TypeName,OperType))
84                                 {
85                                         /* found this oper's opertype */
86                                         ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
87                                         if (*HostName)
88                                                 user->ChangeDisplayedHost(HostName);
89                                         if (!ServerInstance->IsNick(TypeName))
90                                         {
91                                                 user->WriteServ("491 %s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick);
92                                                 ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper type '%s' contains invalid characters",OperType);
93                                                 ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type erroneous.",user->nick,user->ident,user->host);
94                                                 return CMD_FAILURE;
95                                         }
96                                         found = true;
97                                         type_invalid = false;
98                                         break;
99                                 }
100                         }
101                 }
102                 if (match_login || found)
103                         break;
104         }
105         if (found)
106         {
107                 /* correct oper credentials */
108                 ServerInstance->SNO->WriteToSnoMask('o',"%s (%s@%s) is now an IRC operator of type %s (using oper '%s')",user->nick,user->ident,user->host,OperType,parameters[0]);
109                 user->WriteServ("381 %s :You are now an IRC operator of type %s",user->nick,OperType);
110                 if (!user->modes[UM_OPERATOR])
111                         user->Oper(OperType);
112         }
113         else
114         {
115                 if (!type_invalid)
116                 {
117                         std::string fields = "";
118                         if (!match_login)
119                                 fields.append("login ");
120                         if (!match_pass)
121                                 fields.append("password ");
122                         if (!match_hosts)
123                                 fields.append("hosts");
124                         user->WriteServ("491 %s :Invalid oper credentials",user->nick);
125                         ServerInstance->SNO->WriteToSnoMask('o',"WARNING! Failed oper attempt by %s!%s@%s using login '%s': The following fields do not match: %s",user->nick,user->ident,user->host, parameters[0], fields.c_str());
126                         ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s using login '%s': The following fields did not match: %s",user->nick,user->ident,user->host,parameters[0],fields.c_str());
127                         return CMD_FAILURE;
128                 }
129                 else
130                 {
131                         user->WriteServ("491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
132                         ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper block '%s': missing OperType %s",parameters[0],OperType);
133                         ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s using login '%s': credentials valid, but oper type nonexistent.",user->nick,user->ident,user->host,parameters[0]);
134                         return CMD_FAILURE;
135                 }
136         }
137         return CMD_SUCCESS;
138 }
139