1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 #include "configreader.h"
22 #include "commands/cmd_oper.h"
25 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
27 std::stringstream hl(hostlist);
31 if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
39 extern "C" command_t* init_command(InspIRCd* Instance)
41 return new cmd_oper(Instance);
44 CmdResult cmd_oper::Handle (const char** parameters, int pcnt, userrec *user)
46 char LoginName[MAXBUF];
47 char Password[MAXBUF];
48 char OperType[MAXBUF];
49 char TypeName[MAXBUF];
50 char HostName[MAXBUF];
55 bool type_invalid = false;
57 bool match_login = false;
58 bool match_pass = false;
59 bool match_hosts = false;
61 snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
62 snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
64 for (int i = 0; i < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "oper"); i++)
66 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "name", i, LoginName, MAXBUF);
67 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "password", i, Password, MAXBUF);
68 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "type", i, OperType, MAXBUF);
69 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "host", i, HostName, MAXBUF);
71 match_login = !strcmp(LoginName,parameters[0]);
72 match_pass = !ServerInstance->OperPassCompare(Password,parameters[1]);
73 match_hosts = OneOfMatches(TheHost,TheIP,HostName);
75 if (match_login && match_pass && match_hosts)
78 for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
80 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","name", j, TypeName, MAXBUF);
82 if (!strcmp(TypeName,OperType))
84 /* found this oper's opertype */
85 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
87 user->ChangeDisplayedHost(HostName);
88 if (!ServerInstance->IsNick(TypeName))
90 user->WriteServ("491 %s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick);
91 ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper type '%s' contains invalid characters",OperType);
92 ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type erroneous.",user->nick,user->ident,user->host);
101 if (match_login || found)
106 /* correct oper credentials */
107 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]);
108 user->WriteServ("381 %s :You are now an IRC operator of type %s",user->nick,irc::Spacify(OperType));
109 if (!user->modes[UM_OPERATOR])
110 user->Oper(OperType);
116 std::string fields = "";
118 fields.append("login ");
120 fields.append("password ");
122 fields.append("hosts");
123 user->WriteServ("491 %s :Invalid oper credentials",user->nick);
124 ServerInstance->SNO->WriteToSnoMask('o',"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());
125 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());
130 user->WriteServ("491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
131 ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper block '%s': missing OperType %s",parameters[0],OperType);
132 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]);