1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 * ---------------------------------------------------
16 #include "commands/cmd_oper.h"
19 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
21 std::stringstream hl(hostlist);
25 if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
33 extern "C" DllExport command_t* init_command(InspIRCd* Instance)
35 return new cmd_oper(Instance);
38 CmdResult cmd_oper::Handle (const char** parameters, int pcnt, userrec *user)
40 char LoginName[MAXBUF];
41 char Password[MAXBUF];
42 char OperType[MAXBUF];
43 char TypeName[MAXBUF];
44 char HostName[MAXBUF];
45 char ClassName[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,user->host);
57 snprintf(TheIP, MAXBUF,"%s@%s",user->ident,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);
66 match_login = !strcmp(LoginName,parameters[0]);
67 match_pass = !ServerInstance->OperPassCompare(Password,parameters[1], i);
68 match_hosts = OneOfMatches(TheHost,TheIP,HostName);
70 if (match_login && match_pass && match_hosts)
73 for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
75 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type", "name", j, TypeName, MAXBUF);
76 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type", "class", j, ClassName, MAXBUF);
78 if (!strcmp(TypeName,OperType))
80 /* found this oper's opertype */
81 if (!ServerInstance->IsNick(TypeName))
83 user->WriteServ("491 %s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick);
84 ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper type '%s' contains invalid characters",OperType);
85 ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type erroneous.",user->nick,user->ident,user->host);
88 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
90 user->ChangeDisplayedHost(HostName);
92 user->CheckClass(ClassName);
99 if (match_login || found)
104 /* correct oper credentials */
105 ServerInstance->SNO->WriteToSnoMask('o',"%s (%s@%s) is now an IRC operator of type %s (using oper '%s')",user->nick,user->ident,user->host,irc::Spacify(OperType),parameters[0]);
106 user->WriteServ("381 %s :You are now %s %s",user->nick, strchr("aeiouAEIOU", *OperType) ? "an" : "a", irc::Spacify(OperType));
107 if (!user->IsModeSet('o'))
108 user->Oper(OperType);
112 std::deque<std::string> n;
114 char broadcast[MAXBUF];
120 fields.append("login ");
124 fields.append("password ");
126 fields.append("hosts");
128 user->WriteServ("491 %s :Invalid oper credentials",user->nick);
130 snprintf(broadcast, MAXBUF, "WARNING! Failed oper attempt by %s!%s@%s using login '%s': The following fields do not match: %s",user->nick,user->ident,user->host, parameters[0], fields.c_str());
131 ServerInstance->SNO->WriteToSnoMask('o',std::string(broadcast));
132 n.push_back(broadcast);
133 Event rmode2((char *)&n, NULL, "send_snoset");
134 rmode2.Send(ServerInstance);
136 ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s using login '%s': The following fields did not match: %s",user->nick,user->ident,user->host,parameters[0],fields.c_str());
141 user->WriteServ("491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
143 snprintf(broadcast, MAXBUF, "CONFIGURATION ERROR! Oper block '%s': missing OperType %s",parameters[0],OperType);
145 ServerInstance->SNO->WriteToSnoMask('o', std::string(broadcast));
146 n.push_back(broadcast);
147 Event rmode2((char *)&n, NULL, "send_snoset");
148 rmode2.Send(ServerInstance);
150 ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s using login '%s': credentials valid, but oper type nonexistent.",user->nick,user->ident,user->host,parameters[0]);