1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "commands/cmd_oper.h"
18 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
20 std::stringstream hl(hostlist);
24 if (InspIRCd::MatchCIDR(host, xhost, NULL) || InspIRCd::MatchCIDR(ip, xhost, NULL))
32 extern "C" DllExport Command* init_command(InspIRCd* Instance)
34 return new CommandOper(Instance);
37 CmdResult CommandOper::Handle (const std::vector<std::string>& parameters, User *user)
39 char LoginName[MAXBUF];
40 char Password[MAXBUF];
41 char OperType[MAXBUF];
42 char TypeName[MAXBUF];
43 char HostName[MAXBUF];
44 char ClassName[MAXBUF];
47 char HashType[MAXBUF];
50 bool type_invalid = false;
52 bool match_login = false;
53 bool match_pass = false;
54 bool match_hosts = false;
56 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
57 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
59 for (int i = 0; i < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "oper"); i++)
61 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "name", i, LoginName, MAXBUF);
62 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "password", i, Password, MAXBUF);
63 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "type", i, OperType, MAXBUF);
64 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "host", i, HostName, MAXBUF);
65 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "hash", i, HashType, MAXBUF);
67 match_login = (LoginName == parameters[0]);
68 match_pass = !ServerInstance->PassCompare(user, Password, parameters[1], HashType);
69 match_hosts = OneOfMatches(TheHost,TheIP,HostName);
71 if (match_login && match_pass && match_hosts)
74 for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
76 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type", "name", j, TypeName, MAXBUF);
77 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type", "class", j, ClassName, MAXBUF);
79 if (!strcmp(TypeName,OperType))
81 /* found this oper's opertype */
82 if (!ServerInstance->IsNick(TypeName, ServerInstance->Config->Limits.NickMax))
84 user->WriteNumeric(491, "%s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick.c_str());
85 ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper type '%s' contains invalid characters",OperType);
86 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());
89 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
91 user->ChangeDisplayedHost(HostName);
94 user->SetClass(ClassName);
103 if (match_login || found)
108 /* correct oper credentials */
109 ServerInstance->SNO->WriteToSnoMask('o',"%s (%s@%s) is now an IRC operator of type %s (using oper '%s')", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), irc::Spacify(OperType), parameters[0].c_str());
110 user->WriteNumeric(381, "%s :You are now %s %s",user->nick.c_str(), strchr("aeiouAEIOU", *OperType) ? "an" : "a", irc::Spacify(OperType));
111 if (!user->IsModeSet('o'))
112 user->Oper(OperType, LoginName);
116 char broadcast[MAXBUF];
122 fields.append("login ");
126 fields.append("password ");
128 fields.append("hosts");
131 // tell them they suck, and lag them up to help prevent brute-force attacks
132 user->WriteNumeric(491, "%s :Invalid oper credentials",user->nick.c_str());
133 user->IncreasePenalty(10);
135 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());
136 ServerInstance->SNO->WriteToSnoMask('o',std::string(broadcast));
137 ServerInstance->PI->SendSNONotice("o", std::string("OPER: ") + broadcast);
139 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());
144 user->WriteNumeric(491, "%s :Your oper block does not have a valid opertype associated with it",user->nick.c_str());
146 snprintf(broadcast, MAXBUF, "CONFIGURATION ERROR! Oper block '%s': missing OperType %s",parameters[0].c_str(),OperType);
148 ServerInstance->SNO->WriteToSnoMask('o', std::string(broadcast));
150 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());