]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_oper.cpp
Add fine-grained command flood controls
[user/henk/code/inspircd.git] / src / commands / cmd_oper.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 "hashcomp.h"
16
17 bool OneOfMatches(const char* host, const char* ip, const char* hostlist);
18
19 /** Handle /OPER. These command handlers can be reloaded by the core,
20  * and handle basic RFC1459 commands. Commands within modules work
21  * the same way, however, they can be fully unloaded, where these
22  * may not.
23  */
24 class CommandOper : public SplitCommand
25 {
26  public:
27         /** Constructor for oper.
28          */
29         CommandOper ( Module* parent) : SplitCommand(parent,"OPER",2,2) { syntax = "<username> <password>"; }
30         /** Handle command.
31          * @param parameters The parameters to the comamnd
32          * @param pcnt The number of parameters passed to teh command
33          * @param user The user issuing the command
34          * @return A value from CmdResult to indicate command success or failure.
35          */
36         CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser *user);
37 };
38
39 bool OneOfMatches(const char* host, const char* ip, const std::string& hostlist)
40 {
41         std::stringstream hl(hostlist);
42         std::string xhost;
43         while (hl >> xhost)
44         {
45                 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
46                 {
47                         return true;
48                 }
49         }
50         return false;
51 }
52
53 CmdResult CommandOper::HandleLocal(const std::vector<std::string>& parameters, LocalUser *user)
54 {
55         char TheHost[MAXBUF];
56         char TheIP[MAXBUF];
57         bool match_login = false;
58         bool match_pass = false;
59         bool match_hosts = false;
60
61         snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
62         snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
63
64         OperIndex::iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
65         if (i != ServerInstance->Config->oper_blocks.end())
66         {
67                 OperInfo* ifo = i->second;
68                 ConfigTag* tag = ifo->oper_block;
69                 match_login = true;
70                 match_pass = !ServerInstance->PassCompare(user, tag->getString("password"), parameters[1], tag->getString("hash"));
71                 match_hosts = OneOfMatches(TheHost,TheIP,tag->getString("host"));
72
73                 if (match_pass && match_hosts)
74                 {
75                         /* found this oper's opertype */
76                         user->Oper(ifo);
77                         return CMD_SUCCESS;
78                 }
79         }
80         char broadcast[MAXBUF];
81
82         std::string fields;
83         if (!match_login)
84                 fields.append("login ");
85         if (!match_pass)
86                 fields.append("password ");
87         if (!match_hosts)
88                 fields.append("hosts");
89
90         // tell them they suck, and lag them up to help prevent brute-force attacks
91         user->WriteNumeric(491, "%s :Invalid oper credentials",user->nick.c_str());
92         user->CommandFloodPenalty += 10000;
93
94         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());
95         ServerInstance->SNO->WriteToSnoMask('o',std::string(broadcast));
96         ServerInstance->PI->SendSNONotice("o", std::string("OPER: ") + broadcast);
97
98         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());
99         return CMD_FAILURE;
100 }
101
102 COMMAND_INIT(CommandOper)