]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_oper.cpp
Wheee, mass commit! this adds const stafety, throwing a compile error if anyone does...
[user/henk/code/inspircd.git] / src / commands / cmd_oper.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 "wildcard.h"
16 #include "commands/cmd_oper.h"
17 #include "hashcomp.h"
18
19 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
20 {
21         std::stringstream hl(hostlist);
22         std::string xhost;
23         while (hl >> xhost)
24         {
25                 if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
26                 {
27                         return true;
28                 }
29         }
30         return false;
31 }
32
33 extern "C" DllExport Command* init_command(InspIRCd* Instance)
34 {
35         return new CommandOper(Instance);
36 }
37
38 CmdResult CommandOper::Handle (const char* const* parameters, int, User *user)
39 {
40         char LoginName[MAXBUF];
41         char Password[MAXBUF];
42         char OperType[MAXBUF];
43         char TypeName[MAXBUF];
44         char HostName[MAXBUF];
45         char ClassName[MAXBUF];
46         char TheHost[MAXBUF];
47         char TheIP[MAXBUF];
48         char HashType[MAXBUF];
49         int j;
50         bool found = false;
51         bool type_invalid = false;
52
53         bool match_login = false;
54         bool match_pass = false;
55         bool match_hosts = false;
56
57         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
58         snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
59
60         for (int i = 0; i < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "oper"); i++)
61         {
62                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "name", i, LoginName, MAXBUF);
63                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "password", i, Password, MAXBUF);
64                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "type", i, OperType, MAXBUF);
65                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "host", i, HostName, MAXBUF);
66                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "hash", i, HashType, MAXBUF);
67
68                 match_login = !strcmp(LoginName,parameters[0]);
69                 match_pass = !ServerInstance->PassCompare(user, Password,parameters[1], HashType);
70                 match_hosts = OneOfMatches(TheHost,TheIP,HostName);
71
72                 if (match_login && match_pass && match_hosts)
73                 {
74                         type_invalid = true;
75                         for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
76                         {
77                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type", "name", j, TypeName, MAXBUF);
78                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type", "class", j, ClassName, MAXBUF);
79
80                                 if (!strcmp(TypeName,OperType))
81                                 {
82                                         /* found this oper's opertype */
83                                         if (!ServerInstance->IsNick(TypeName))
84                                         {
85                                                 user->WriteServ("491 %s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick);
86                                                 ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper type '%s' contains invalid characters",OperType);
87                                                 ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type erroneous.",user->nick,user->ident,user->host);
88                                                 return CMD_FAILURE;
89                                         }
90                                         ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
91                                         if (*HostName)
92                                                 user->ChangeDisplayedHost(HostName);
93                                         if (*ClassName)
94                                         {
95                                                 user->SetClass(ClassName);
96                                                 user->CheckClass();
97                                         }
98                                         found = true;
99                                         type_invalid = false;
100                                         break;
101                                 }
102                         }
103                 }
104                 if (match_login || found)
105                         break;
106         }
107         if (found)
108         {
109                 /* correct oper credentials */
110                 ServerInstance->SNO->WriteToSnoMask('o',"%s (%s@%s) is now an IRC operator of type %s (using oper '%s')",user->nick,user->ident,user->host,irc::Spacify(OperType),parameters[0]);
111                 user->WriteServ("381 %s :You are now %s %s",user->nick, strchr("aeiouAEIOU", *OperType) ? "an" : "a", irc::Spacify(OperType));
112                 if (!user->IsModeSet('o'))
113                         user->Oper(OperType, LoginName);
114         }
115         else
116         {
117                 std::deque<std::string> n;
118                 n.push_back("o");
119                 char broadcast[MAXBUF];
120
121                 if (!type_invalid)
122                 {
123                         std::string fields;
124                         if (!match_login)
125                                 fields.append("login ");
126                         else
127                         {
128                                 if (!match_pass)
129                                         fields.append("password ");
130                                 if (!match_hosts)
131                                         fields.append("hosts");
132                         }
133
134                         // tell them they suck, and lag them up to help prevent brute-force attacks
135                         user->WriteServ("491 %s :Invalid oper credentials",user->nick);
136                         user->IncreasePenalty(10);
137                         
138                         snprintf(broadcast, MAXBUF, "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());
139                         ServerInstance->SNO->WriteToSnoMask('o',std::string(broadcast));
140                         n.push_back(broadcast);
141                         Event rmode2((char *)&n, NULL, "send_snoset");
142                         rmode2.Send(ServerInstance);
143
144                         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());
145                         return CMD_FAILURE;
146                 }
147                 else
148                 {
149                         user->WriteServ("491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
150
151                         snprintf(broadcast, MAXBUF, "CONFIGURATION ERROR! Oper block '%s': missing OperType %s",parameters[0],OperType);
152
153                         ServerInstance->SNO->WriteToSnoMask('o', std::string(broadcast));
154                         n.push_back(broadcast);
155                         Event rmode2((char *)&n, NULL, "send_snoset");
156                         rmode2.Send(ServerInstance);
157
158                         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]);
159                         return CMD_FAILURE;
160                 }
161         }
162         return CMD_SUCCESS;
163 }
164