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 * ---------------------------------------------------
17 bool OneOfMatches(const char* host, const char* ip, const char* hostlist);
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
24 class CommandOper : public Command
27 /** Constructor for oper.
29 CommandOper ( Module* parent) : Command(parent,"OPER",2,2) { syntax = "<username> <password>"; }
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.
36 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
39 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
41 std::stringstream hl(hostlist);
45 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
53 CmdResult CommandOper::Handle (const std::vector<std::string>& parameters, User *user)
55 char LoginName[MAXBUF];
56 char Password[MAXBUF];
57 char OperType[MAXBUF];
58 char TypeName[MAXBUF];
59 char HostName[MAXBUF];
60 char ClassName[MAXBUF];
63 char HashType[MAXBUF];
66 bool type_invalid = false;
68 bool match_login = false;
69 bool match_pass = false;
70 bool match_hosts = false;
72 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
73 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
75 for (int i = 0; i < ServerInstance->Config->ConfValueEnum("oper"); i++)
77 ServerInstance->Config->ConfValue("oper", "name", i, LoginName, MAXBUF);
78 ServerInstance->Config->ConfValue("oper", "password", i, Password, MAXBUF);
79 ServerInstance->Config->ConfValue("oper", "type", i, OperType, MAXBUF);
80 ServerInstance->Config->ConfValue("oper", "host", i, HostName, MAXBUF);
81 ServerInstance->Config->ConfValue("oper", "hash", i, HashType, MAXBUF);
83 match_login = (LoginName == parameters[0]);
84 match_pass = !ServerInstance->PassCompare(user, Password, parameters[1], HashType);
85 match_hosts = OneOfMatches(TheHost,TheIP,HostName);
87 if (match_login && match_pass && match_hosts)
90 for (j =0; j < ServerInstance->Config->ConfValueEnum("type"); j++)
92 ServerInstance->Config->ConfValue("type", "name", j, TypeName, MAXBUF);
93 ServerInstance->Config->ConfValue("type", "class", j, ClassName, MAXBUF);
95 if (!strcmp(TypeName,OperType))
97 /* found this oper's opertype */
98 if (!ServerInstance->IsNick(TypeName, ServerInstance->Config->Limits.NickMax))
100 user->WriteNumeric(491, "%s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick.c_str());
101 ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper type '%s' contains invalid characters",OperType);
102 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());
105 ServerInstance->Config->ConfValue("type","host", j, HostName, MAXBUF);
107 user->ChangeDisplayedHost(HostName);
110 user->SetClass(ClassName);
114 type_invalid = false;
119 if (match_login || found)
124 /* correct oper credentials */
125 user->Oper(OperType, LoginName);
129 char broadcast[MAXBUF];
135 fields.append("login ");
139 fields.append("password ");
141 fields.append("hosts");
144 // tell them they suck, and lag them up to help prevent brute-force attacks
145 user->WriteNumeric(491, "%s :Invalid oper credentials",user->nick.c_str());
146 user->IncreasePenalty(10);
148 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());
149 ServerInstance->SNO->WriteToSnoMask('o',std::string(broadcast));
150 ServerInstance->PI->SendSNONotice("o", std::string("OPER: ") + broadcast);
152 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());
157 user->WriteNumeric(491, "%s :Your oper block does not have a valid opertype associated with it",user->nick.c_str());
159 snprintf(broadcast, MAXBUF, "CONFIGURATION ERROR! Oper block '%s': missing OperType %s",parameters[0].c_str(),OperType);
161 ServerInstance->SNO->WriteToSnoMask('o', std::string(broadcast));
163 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());
170 COMMAND_INIT(CommandOper)