]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_oper.cpp
8d4eae8cb94f62cbac349936af8a5e52951306ea
[user/henk/code/inspircd.git] / src / commands / cmd_oper.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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** 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         int j;
49         bool found = false;
50         bool type_invalid = false;
51
52         bool match_login = false;
53         bool match_pass = false;
54         bool match_hosts = false;
55
56         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
57         snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
58
59         for (int i = 0; i < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "oper"); i++)
60         {
61                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "name", i, LoginName, MAXBUF);
62                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "password", i, Password, MAXBUF);
63                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "type", i, OperType, MAXBUF);
64                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "host", i, HostName, MAXBUF);
65
66                 match_login = !strcmp(LoginName,parameters[0]);
67                 match_pass = !ServerInstance->OperPassCompare(Password,parameters[1], i);
68                 match_hosts = OneOfMatches(TheHost,TheIP,HostName);
69
70                 if (match_login && match_pass && match_hosts)
71                 {
72                         type_invalid = true;
73                         for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
74                         {
75                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type", "name", j, TypeName, MAXBUF);
76                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type", "class", j, ClassName, MAXBUF);
77
78                                 if (!strcmp(TypeName,OperType))
79                                 {
80                                         /* found this oper's opertype */
81                                         if (!ServerInstance->IsNick(TypeName))
82                                         {
83                                                 user->WriteServ("491 %s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick);
84                                                 ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper type '%s' contains invalid characters",OperType);
85                                                 ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type erroneous.",user->nick,user->ident,user->host);
86                                                 return CMD_FAILURE;
87                                         }
88                                         ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
89                                         if (*HostName)
90                                                 user->ChangeDisplayedHost(HostName);
91                                         if (*ClassName)
92                                         {
93                                                 user->SetClass(ClassName);
94                                                 user->CheckClass();
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,irc::Spacify(OperType),parameters[0]);
109                 user->WriteServ("381 %s :You are now %s %s",user->nick, strchr("aeiouAEIOU", *OperType) ? "an" : "a", irc::Spacify(OperType));
110                 if (!user->IsModeSet('o'))
111                         user->Oper(OperType, LoginName);
112         }
113         else
114         {
115                 std::deque<std::string> n;
116                 n.push_back("o");
117                 char broadcast[MAXBUF];
118
119                 if (!type_invalid)
120                 {
121                         std::string fields;
122                         if (!match_login)
123                                 fields.append("login ");
124                         else
125                         {
126                                 if (!match_pass)
127                                         fields.append("password ");
128                                 if (!match_hosts)
129                                         fields.append("hosts");
130                         }
131
132                         // tell them they suck, and lag them up to help prevent brute-force attacks
133                         user->WriteServ("491 %s :Invalid oper credentials",user->nick);
134                         user->IncreasePenalty(10);
135                         
136                         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());
137                         ServerInstance->SNO->WriteToSnoMask('o',std::string(broadcast));
138                         n.push_back(broadcast);
139                         Event rmode2((char *)&n, NULL, "send_snoset");
140                         rmode2.Send(ServerInstance);
141
142                         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());
143                         return CMD_FAILURE;
144                 }
145                 else
146                 {
147                         user->WriteServ("491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
148
149                         snprintf(broadcast, MAXBUF, "CONFIGURATION ERROR! Oper block '%s': missing OperType %s",parameters[0],OperType);
150
151                         ServerInstance->SNO->WriteToSnoMask('o', std::string(broadcast));
152                         n.push_back(broadcast);
153                         Event rmode2((char *)&n, NULL, "send_snoset");
154                         rmode2.Send(ServerInstance);
155
156                         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]);
157                         return CMD_FAILURE;
158                 }
159         }
160         return CMD_SUCCESS;
161 }
162