]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_oper.cpp
I'll give you ##TOAST, :p
[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
24 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
25 {
26         std::stringstream hl(hostlist);
27         std::string xhost;
28         while (hl >> xhost)
29         {
30                 if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
31                 {
32                         return true;
33                 }
34         }
35         return false;
36 }
37
38
39
40 extern "C" command_t* init_command(InspIRCd* Instance)
41 {
42         return new cmd_oper(Instance);
43 }
44
45 CmdResult cmd_oper::Handle (const char** parameters, int pcnt, userrec *user)
46 {
47         char LoginName[MAXBUF];
48         char Password[MAXBUF];
49         char OperType[MAXBUF];
50         char TypeName[MAXBUF];
51         char HostName[MAXBUF];
52         char TheHost[MAXBUF];
53         char TheIP[MAXBUF];
54         int j;
55         bool found = false;
56         bool fail2 = 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                 if ((!strcmp(LoginName,parameters[0])) && (!ServerInstance->OperPassCompare(Password,parameters[1])) && (OneOfMatches(TheHost,TheIP,HostName)))
69                 {
70                         fail2 = true;
71                         for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
72                         {
73                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","name", j, TypeName, MAXBUF);
74
75                                 if (!strcmp(TypeName,OperType))
76                                 {
77                                         /* found this oper's opertype */
78                                         ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
79                                         if (*HostName)
80                                                 user->ChangeDisplayedHost(HostName);
81                                         if (!ServerInstance->IsNick(TypeName))
82                                         {
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 invalid for OperType '%s'",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);
86                                                 return CMD_FAILURE;
87                                         }
88                                         found = true;
89                                         fail2 = false;
90                                         break;
91                                 }
92                         }
93                 }
94                 if (found)
95                         break;
96         }
97         if (found)
98         {
99                 /* correct oper credentials */
100                 ServerInstance->SNO->WriteToSnoMask('o',"%s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
101                 user->WriteServ("381 %s :You are now an IRC operator of type %s",user->nick,OperType);
102                 if (!user->modes[UM_OPERATOR])
103                         user->Oper(OperType);
104         }
105         else
106         {
107                 if (!fail2)
108                 {
109                         user->WriteServ("491 %s :Invalid oper credentials",user->nick);
110                         ServerInstance->SNO->WriteToSnoMask('o',"WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
111                         ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: user, host or password did not match.",user->nick,user->ident,user->host);
112                         return CMD_FAILURE;
113                 }
114                 else
115                 {
116                         user->WriteServ("491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
117                         ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper block mismatch for OperType %s",OperType);
118                         ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type nonexistent.",user->nick,user->ident,user->host);
119                         return CMD_FAILURE;
120                 }
121         }
122         return CMD_SUCCESS;
123 }
124