]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_oper.cpp
Change User::oper to an OperInfo reference
[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                         std::string host = ifo->getConfig("host");
77                         if (!host.empty())
78                                 user->ChangeDisplayedHost(host.c_str());
79                         std::string opClass = ifo->getConfig("class");
80                         if (!opClass.empty())
81                         {
82                                 user->SetClass(opClass);
83                                 user->CheckClass();
84                         }
85                         user->Oper(ifo);
86                         return CMD_SUCCESS;
87                 }
88         }
89         char broadcast[MAXBUF];
90
91         std::string fields;
92         if (!match_login)
93                 fields.append("login ");
94         if (!match_pass)
95                 fields.append("password ");
96         if (!match_hosts)
97                 fields.append("hosts");
98
99         // tell them they suck, and lag them up to help prevent brute-force attacks
100         user->WriteNumeric(491, "%s :Invalid oper credentials",user->nick.c_str());
101         user->Penalty += 10;
102
103         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());
104         ServerInstance->SNO->WriteToSnoMask('o',std::string(broadcast));
105         ServerInstance->PI->SendSNONotice("o", std::string("OPER: ") + broadcast);
106
107         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());
108         return CMD_FAILURE;
109 }
110
111 COMMAND_INIT(CommandOper)