1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 #ifndef __CMD_OPER_H__
17 #define __CMD_OPER_H__
19 // include the common header files
24 bool OneOfMatches(const char* host, const char* ip, const char* hostlist);
26 /** Handle /OPER. These command handlers can be reloaded by the core,
27 * and handle basic RFC1459 commands. Commands within modules work
28 * the same way, however, they can be fully unloaded, where these
31 class CommandOper : public Command
34 /** Constructor for oper.
36 CommandOper (InspIRCd* Instance, Module* parent) : Command(Instance,parent,"OPER",0,2,false,2) { syntax = "<username> <password>"; }
38 * @param parameters The parameters to the comamnd
39 * @param pcnt The number of parameters passed to teh command
40 * @param user The user issuing the command
41 * @return A value from CmdResult to indicate command success or failure.
43 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
50 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
52 std::stringstream hl(hostlist);
56 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
64 CmdResult CommandOper::Handle (const std::vector<std::string>& parameters, User *user)
66 char LoginName[MAXBUF];
67 char Password[MAXBUF];
68 char OperType[MAXBUF];
69 char TypeName[MAXBUF];
70 char HostName[MAXBUF];
71 char ClassName[MAXBUF];
74 char HashType[MAXBUF];
77 bool type_invalid = false;
79 bool match_login = false;
80 bool match_pass = false;
81 bool match_hosts = false;
83 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
84 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
86 for (int i = 0; i < ServerInstance->Config->ConfValueEnum("oper"); i++)
88 ServerInstance->Config->ConfValue("oper", "name", i, LoginName, MAXBUF);
89 ServerInstance->Config->ConfValue("oper", "password", i, Password, MAXBUF);
90 ServerInstance->Config->ConfValue("oper", "type", i, OperType, MAXBUF);
91 ServerInstance->Config->ConfValue("oper", "host", i, HostName, MAXBUF);
92 ServerInstance->Config->ConfValue("oper", "hash", i, HashType, MAXBUF);
94 match_login = (LoginName == parameters[0]);
95 match_pass = !ServerInstance->PassCompare(user, Password, parameters[1], HashType);
96 match_hosts = OneOfMatches(TheHost,TheIP,HostName);
98 if (match_login && match_pass && match_hosts)
101 for (j =0; j < ServerInstance->Config->ConfValueEnum("type"); j++)
103 ServerInstance->Config->ConfValue("type", "name", j, TypeName, MAXBUF);
104 ServerInstance->Config->ConfValue("type", "class", j, ClassName, MAXBUF);
106 if (!strcmp(TypeName,OperType))
108 /* found this oper's opertype */
109 if (!ServerInstance->IsNick(TypeName, ServerInstance->Config->Limits.NickMax))
111 user->WriteNumeric(491, "%s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick.c_str());
112 ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper type '%s' contains invalid characters",OperType);
113 ServerInstance->Logs->Log("OPER",DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type erroneous.", user->nick.c_str(), user->ident.c_str(), user->host.c_str());
116 ServerInstance->Config->ConfValue("type","host", j, HostName, MAXBUF);
118 user->ChangeDisplayedHost(HostName);
121 user->SetClass(ClassName);
125 type_invalid = false;
130 if (match_login || found)
135 /* correct oper credentials */
136 user->Oper(OperType, LoginName);
140 char broadcast[MAXBUF];
146 fields.append("login ");
150 fields.append("password ");
152 fields.append("hosts");
155 // tell them they suck, and lag them up to help prevent brute-force attacks
156 user->WriteNumeric(491, "%s :Invalid oper credentials",user->nick.c_str());
157 user->IncreasePenalty(10);
159 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());
160 ServerInstance->SNO->WriteToSnoMask('o',std::string(broadcast));
161 ServerInstance->PI->SendSNONotice("o", std::string("OPER: ") + broadcast);
163 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());
168 user->WriteNumeric(491, "%s :Your oper block does not have a valid opertype associated with it",user->nick.c_str());
170 snprintf(broadcast, MAXBUF, "CONFIGURATION ERROR! Oper block '%s': missing OperType %s",parameters[0].c_str(),OperType);
172 ServerInstance->SNO->WriteToSnoMask('o', std::string(broadcast));
174 ServerInstance->Logs->Log("OPER",DEFAULT,"OPER: Failed oper attempt by %s!%s@%s using login '%s': credentials valid, but oper type nonexistent.", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), parameters[0].c_str());
181 COMMAND_INIT(CommandOper)