]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_oper.cpp
More tweaks
[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 <string>
18 #include <sstream>
19 #include <vector>
20 #include "inspircd_config.h"
21 #include "configreader.h"
22 #include "typedefs.h"
23 #include "users.h"
24 #include "globals.h"
25 #include "modules.h"
26 #include "dynamic.h"
27 #include "wildcard.h"
28 #include "commands.h"
29 #include "mode.h"
30 #include "xline.h"
31 #include "inspstring.h"
32
33 #include "hashcomp.h"
34 #include "socketengine.h"
35
36 #include "command_parse.h"
37 #include "commands/cmd_oper.h"
38
39 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
40 {
41         std::stringstream hl(hostlist);
42         std::string xhost;
43         while (hl >> xhost)
44         {
45                 if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
46                 {
47                         return true;
48                 }
49         }
50         return false;
51 }
52
53 void cmd_oper::Handle (const char** parameters, int pcnt, userrec *user)
54 {
55         char LoginName[MAXBUF];
56         char Password[MAXBUF];
57         char OperType[MAXBUF];
58         char TypeName[MAXBUF];
59         char HostName[MAXBUF];
60         char TheHost[MAXBUF];
61         char TheIP[MAXBUF];
62         int j;
63         bool found = false;
64         bool fail2 = false;
65
66         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
67         snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
68
69         for (int i = 0; i < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "oper"); i++)
70         {
71                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "name", i, LoginName, MAXBUF);
72                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "password", i, Password, MAXBUF);
73                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "type", i, OperType, MAXBUF);
74                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "host", i, HostName, MAXBUF);
75
76                 if ((!strcmp(LoginName,parameters[0])) && (!ServerInstance->OperPassCompare(Password,parameters[1])) && (OneOfMatches(TheHost,TheIP,HostName)))
77                 {
78                         fail2 = true;
79                         for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
80                         {
81                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","name", j, TypeName, MAXBUF);
82
83                                 if (!strcmp(TypeName,OperType))
84                                 {
85                                         /* found this oper's opertype */
86                                         ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
87                                         if (*HostName)
88                                                 user->ChangeDisplayedHost(HostName);
89                                         if (!ServerInstance->IsNick(TypeName))
90                                         {
91                                                 user->WriteServ("491 %s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick);
92                                                 ServerInstance->WriteOpers("*** CONFIGURATION ERROR! Oper type invalid for OperType '%s'",OperType);
93                                                 ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type erroneous.",user->nick,user->ident,user->host);
94                                                 return;
95                                         }
96                                         found = true;
97                                         fail2 = false;
98                                         break;
99                                 }
100                         }
101                 }
102                 if (found)
103                         break;
104         }
105         if (found)
106         {
107                 /* correct oper credentials */
108                 ServerInstance->WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
109                 user->WriteServ("381 %s :You are now an IRC operator of type %s",user->nick,OperType);
110                 if (!user->modes[UM_OPERATOR])
111                         user->Oper(OperType);
112         }
113         else
114         {
115                 if (!fail2)
116                 {
117                         user->WriteServ("491 %s :Invalid oper credentials",user->nick);
118                         ServerInstance->WriteOpers("*** WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
119                         ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: user, host or password did not match.",user->nick,user->ident,user->host);
120                 }
121                 else
122                 {
123                         user->WriteServ("491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
124                         ServerInstance->WriteOpers("*** CONFIGURATION ERROR! Oper block mismatch for OperType %s",OperType);
125                         ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type nonexistent.",user->nick,user->ident,user->host);
126                 }
127         }
128         return;
129 }