]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_oper.cpp
4f4c0d2844fe8a34d111d6aa333d78203028b016
[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 "commands/cmd_whois.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
40
41 extern "C" command_t* init_command(InspIRCd* Instance)
42 {
43         return new cmd_oper(Instance);
44 }
45
46 CmdResult cmd_oper::Handle (const char** parameters, int pcnt, userrec *user)
47 {
48         char LoginName[MAXBUF];
49         char Password[MAXBUF];
50         char OperType[MAXBUF];
51         char TypeName[MAXBUF];
52         char HostName[MAXBUF];
53         char TheHost[MAXBUF];
54         char TheIP[MAXBUF];
55         int j;
56         bool found = false;
57         bool type_invalid = false;
58
59         bool match_login = false;
60         bool match_pass = false;
61         bool match_hosts = false;
62
63         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
64         snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
65
66         for (int i = 0; i < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "oper"); i++)
67         {
68                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "name", i, LoginName, MAXBUF);
69                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "password", i, Password, MAXBUF);
70                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "type", i, OperType, MAXBUF);
71                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "host", i, HostName, MAXBUF);
72
73                 match_login = !strcmp(LoginName,parameters[0]);
74                 match_pass = !ServerInstance->OperPassCompare(Password,parameters[1]);
75                 match_hosts = OneOfMatches(TheHost,TheIP,HostName);
76
77                 if (match_login && match_pass && match_hosts)
78                 {
79                         type_invalid = true;
80                         for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
81                         {
82                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","name", j, TypeName, MAXBUF);
83
84                                 if (!strcmp(TypeName,OperType))
85                                 {
86                                         /* found this oper's opertype */
87                                         ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
88                                         if (*HostName)
89                                                 user->ChangeDisplayedHost(HostName);
90                                         if (!ServerInstance->IsNick(TypeName))
91                                         {
92                                                 user->WriteServ("491 %s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick);
93                                                 ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper type '%s' contains invalid characters",OperType);
94                                                 ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type erroneous.",user->nick,user->ident,user->host);
95                                                 return CMD_FAILURE;
96                                         }
97                                         found = true;
98                                         type_invalid = false;
99                                         break;
100                                 }
101                         }
102                 }
103                 if (match_login || found)
104                         break;
105         }
106         if (found)
107         {
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,user->ident,user->host,Spacify(OperType),parameters[0]);
110                 user->WriteServ("381 %s :You are now an IRC operator of type %s",user->nick,Spacify(OperType));
111                 if (!user->modes[UM_OPERATOR])
112                         user->Oper(OperType);
113         }
114         else
115         {
116                 if (!type_invalid)
117                 {
118                         std::string fields = "";
119                         if (!match_login)
120                                 fields.append("login ");
121                         if (!match_pass)
122                                 fields.append("password ");
123                         if (!match_hosts)
124                                 fields.append("hosts");
125                         user->WriteServ("491 %s :Invalid oper credentials",user->nick);
126                         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());
127                         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());
128                         return CMD_FAILURE;
129                 }
130                 else
131                 {
132                         user->WriteServ("491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
133                         ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper block '%s': missing OperType %s",parameters[0],OperType);
134                         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]);
135                         return CMD_FAILURE;
136                 }
137         }
138         return CMD_SUCCESS;
139 }
140