]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_oper.cpp
Change this a LOT.
[user/henk/code/inspircd.git] / src / cmd_oper.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "configreader.h"
18 #include "typedefs.h"
19 #include "users.h"
20 #include "modules.h"
21 #include "wildcard.h"
22 #include "commands/cmd_oper.h"
23 #include "hashcomp.h"
24
25 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
26 {
27         std::stringstream hl(hostlist);
28         std::string xhost;
29         while (hl >> xhost)
30         {
31                 if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
32                 {
33                         return true;
34                 }
35         }
36         return false;
37 }
38
39 extern "C" command_t* init_command(InspIRCd* Instance)
40 {
41         return new cmd_oper(Instance);
42 }
43
44 CmdResult cmd_oper::Handle (const char** parameters, int pcnt, userrec *user)
45 {
46         char LoginName[MAXBUF];
47         char Password[MAXBUF];
48         char OperType[MAXBUF];
49         char TypeName[MAXBUF];
50         char HostName[MAXBUF];
51         char TheHost[MAXBUF];
52         char TheIP[MAXBUF];
53         int j;
54         bool found = false;
55         bool type_invalid = false;
56
57         bool match_login = false;
58         bool match_pass = false;
59         bool match_hosts = false;
60
61         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
62         snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
63
64         for (int i = 0; i < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "oper"); i++)
65         {
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);
70
71                 match_login = !strcmp(LoginName,parameters[0]);
72                 match_pass = !ServerInstance->OperPassCompare(Password,parameters[1], i);
73                 match_hosts = OneOfMatches(TheHost,TheIP,HostName);
74
75                 if (match_login && match_pass && match_hosts)
76                 {
77                         type_invalid = true;
78                         for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
79                         {
80                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","name", j, TypeName, MAXBUF);
81
82                                 if (!strcmp(TypeName,OperType))
83                                 {
84                                         /* found this oper's opertype */
85                                         ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
86                                         if (*HostName)
87                                                 user->ChangeDisplayedHost(HostName);
88                                         if (!ServerInstance->IsNick(TypeName))
89                                         {
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);
93                                                 return CMD_FAILURE;
94                                         }
95                                         found = true;
96                                         type_invalid = false;
97                                         break;
98                                 }
99                         }
100                 }
101                 if (match_login || found)
102                         break;
103         }
104         if (found)
105         {
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);
111         }
112         else
113         {
114                 if (!type_invalid)
115                 {
116                         std::string fields = "";
117                         if (!match_login)
118                                 fields.append("login ");
119                         if (!match_pass)
120                                 fields.append("password ");
121                         if (!match_hosts)
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());
126                         return CMD_FAILURE;
127                 }
128                 else
129                 {
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]);
133                         return CMD_FAILURE;
134                 }
135         }
136         return CMD_SUCCESS;
137 }
138