]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_oper.cpp
Change to use std::string::iterator rather than making a copy of the pointer and...
[user/henk/code/inspircd.git] / src / cmd_oper.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "configreader.h"
15 #include "typedefs.h"
16 #include "users.h"
17 #include "modules.h"
18 #include "wildcard.h"
19 #include "commands/cmd_oper.h"
20 #include "hashcomp.h"
21
22 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
23 {
24         std::stringstream hl(hostlist);
25         std::string xhost;
26         while (hl >> xhost)
27         {
28                 if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
29                 {
30                         return true;
31                 }
32         }
33         return false;
34 }
35
36 extern "C" command_t* init_command(InspIRCd* Instance)
37 {
38         return new cmd_oper(Instance);
39 }
40
41 CmdResult cmd_oper::Handle (const char** parameters, int pcnt, userrec *user)
42 {
43         char LoginName[MAXBUF];
44         char Password[MAXBUF];
45         char OperType[MAXBUF];
46         char TypeName[MAXBUF];
47         char HostName[MAXBUF];
48         char TheHost[MAXBUF];
49         char TheIP[MAXBUF];
50         int j;
51         bool found = false;
52         bool type_invalid = false;
53
54         bool match_login = false;
55         bool match_pass = false;
56         bool match_hosts = false;
57
58         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
59         snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
60
61         for (int i = 0; i < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "oper"); i++)
62         {
63                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "name", i, LoginName, MAXBUF);
64                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "password", i, Password, MAXBUF);
65                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "type", i, OperType, MAXBUF);
66                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "host", i, HostName, MAXBUF);
67
68                 match_login = !strcmp(LoginName,parameters[0]);
69                 match_pass = !ServerInstance->OperPassCompare(Password,parameters[1], i);
70                 match_hosts = OneOfMatches(TheHost,TheIP,HostName);
71
72                 if (match_login && match_pass && match_hosts)
73                 {
74                         type_invalid = true;
75                         for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
76                         {
77                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","name", j, TypeName, MAXBUF);
78
79                                 if (!strcmp(TypeName,OperType))
80                                 {
81                                         /* found this oper's opertype */
82                                         ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
83                                         if (*HostName)
84                                                 user->ChangeDisplayedHost(HostName);
85                                         if (!ServerInstance->IsNick(TypeName))
86                                         {
87                                                 user->WriteServ("491 %s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick);
88                                                 ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper type '%s' contains invalid characters",OperType);
89                                                 ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type erroneous.",user->nick,user->ident,user->host);
90                                                 return CMD_FAILURE;
91                                         }
92                                         found = true;
93                                         type_invalid = false;
94                                         break;
95                                 }
96                         }
97                 }
98                 if (match_login || found)
99                         break;
100         }
101         if (found)
102         {
103                 /* correct oper credentials */
104                 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]);
105                 user->WriteServ("381 %s :You are now an IRC operator of type %s",user->nick,irc::Spacify(OperType));
106                 if (!user->modes[UM_OPERATOR])
107                         user->Oper(OperType);
108         }
109         else
110         {
111                 if (!type_invalid)
112                 {
113                         std::string fields = "";
114                         if (!match_login)
115                                 fields.append("login ");
116                         if (!match_pass)
117                                 fields.append("password ");
118                         if (!match_hosts)
119                                 fields.append("hosts");
120                         user->WriteServ("491 %s :Invalid oper credentials",user->nick);
121                         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());
122                         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());
123                         return CMD_FAILURE;
124                 }
125                 else
126                 {
127                         user->WriteServ("491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
128                         ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper block '%s': missing OperType %s",parameters[0],OperType);
129                         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]);
130                         return CMD_FAILURE;
131                 }
132         }
133         return CMD_SUCCESS;
134 }
135