]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_oper.cpp
Replace copyright headers with headers granting specific authors copyright
[user/henk/code/inspircd.git] / src / commands / cmd_oper.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23 #include "hashcomp.h"
24
25 bool OneOfMatches(const char* host, const char* ip, const char* hostlist);
26
27 /** Handle /OPER. These command handlers can be reloaded by the core,
28  * and handle basic RFC1459 commands. Commands within modules work
29  * the same way, however, they can be fully unloaded, where these
30  * may not.
31  */
32 class CommandOper : public SplitCommand
33 {
34  public:
35         /** Constructor for oper.
36          */
37         CommandOper ( Module* parent) : SplitCommand(parent,"OPER",2,2) { syntax = "<username> <password>"; }
38         /** Handle command.
39          * @param parameters The parameters to the comamnd
40          * @param pcnt The number of parameters passed to teh command
41          * @param user The user issuing the command
42          * @return A value from CmdResult to indicate command success or failure.
43          */
44         CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser *user);
45 };
46
47 bool OneOfMatches(const char* host, const char* ip, const std::string& hostlist)
48 {
49         std::stringstream hl(hostlist);
50         std::string xhost;
51         while (hl >> xhost)
52         {
53                 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
54                 {
55                         return true;
56                 }
57         }
58         return false;
59 }
60
61 CmdResult CommandOper::HandleLocal(const std::vector<std::string>& parameters, LocalUser *user)
62 {
63         char TheHost[MAXBUF];
64         char TheIP[MAXBUF];
65         bool match_login = false;
66         bool match_pass = false;
67         bool match_hosts = false;
68
69         snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
70         snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
71
72         OperIndex::iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
73         if (i != ServerInstance->Config->oper_blocks.end())
74         {
75                 OperInfo* ifo = i->second;
76                 ConfigTag* tag = ifo->oper_block;
77                 match_login = true;
78                 match_pass = !ServerInstance->PassCompare(user, tag->getString("password"), parameters[1], tag->getString("hash"));
79                 match_hosts = OneOfMatches(TheHost,TheIP,tag->getString("host"));
80
81                 if (match_pass && match_hosts)
82                 {
83                         /* found this oper's opertype */
84                         user->Oper(ifo);
85                         return CMD_SUCCESS;
86                 }
87         }
88         char broadcast[MAXBUF];
89
90         std::string fields;
91         if (!match_login)
92                 fields.append("login ");
93         if (!match_pass)
94                 fields.append("password ");
95         if (!match_hosts)
96                 fields.append("hosts");
97
98         // tell them they suck, and lag them up to help prevent brute-force attacks
99         user->WriteNumeric(491, "%s :Invalid oper credentials",user->nick.c_str());
100         user->CommandFloodPenalty += 10000;
101
102         snprintf(broadcast, MAXBUF, "WARNING! Failed oper attempt by %s!%s@%s using login '%s': The following fields do not match: %s", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), parameters[0].c_str(), fields.c_str());
103         ServerInstance->SNO->WriteGlobalSno('o',std::string(broadcast));
104
105         ServerInstance->Logs->Log("OPER",DEFAULT,"OPER: Failed oper attempt by %s!%s@%s using login '%s': The following fields did not match: %s", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), parameters[0].c_str(), fields.c_str());
106         return CMD_FAILURE;
107 }
108
109 COMMAND_INIT(CommandOper)