]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_oper.cpp
Allow nick!ident@ and ident@ portions in a CIDR mask if given, use match() without...
[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 "message.h"
29 #include "commands.h"
30 #include "mode.h"
31 #include "xline.h"
32 #include "inspstring.h"
33 #include "helperfuncs.h"
34 #include "hashcomp.h"
35 #include "socketengine.h"
36
37 #include "command_parse.h"
38 #include "commands/cmd_oper.h"
39
40 extern ServerConfig* Config;
41 extern int MODCOUNT;
42 extern ModuleList modules;
43 extern FactoryList factory;
44 extern time_t TIME;
45
46 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
47 {
48         std::stringstream hl(hostlist);
49         std::string xhost;
50         while (hl >> xhost)
51         {
52                 log(DEBUG,"Oper: Matching host %s",xhost.c_str());
53                 if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
54                 {
55                         return true;
56                 }
57         }
58         return false;
59 }
60
61 void cmd_oper::Handle (const char** parameters, int pcnt, userrec *user)
62 {
63         char LoginName[MAXBUF];
64         char Password[MAXBUF];
65         char OperType[MAXBUF];
66         char TypeName[MAXBUF];
67         char HostName[MAXBUF];
68         char TheHost[MAXBUF];
69         char TheIP[MAXBUF];
70         int j;
71         bool found = false;
72         bool fail2 = false;
73
74         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
75         snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
76
77         for (int i = 0; i < Config->ConfValueEnum(Config->config_data, "oper"); i++)
78         {
79                 Config->ConfValue(Config->config_data, "oper", "name", i, LoginName, MAXBUF);
80                 Config->ConfValue(Config->config_data, "oper", "password", i, Password, MAXBUF);
81                 Config->ConfValue(Config->config_data, "oper", "type", i, OperType, MAXBUF);
82                 Config->ConfValue(Config->config_data, "oper", "host", i, HostName, MAXBUF);
83
84                 if ((!strcmp(LoginName,parameters[0])) && (!operstrcmp(Password,parameters[1])) && (OneOfMatches(TheHost,TheIP,HostName)))
85                 {
86                         fail2 = true;
87                         for (j =0; j < Config->ConfValueEnum(Config->config_data, "type"); j++)
88                         {
89                                 Config->ConfValue(Config->config_data, "type","name", j, TypeName, MAXBUF);
90
91                                 if (!strcmp(TypeName,OperType))
92                                 {
93                                         /* found this oper's opertype */
94                                         Config->ConfValue(Config->config_data, "type","host", j, HostName, MAXBUF);
95                                         if (*HostName)
96                                                 ChangeDisplayedHost(user,HostName);
97                                         if (!isnick(TypeName))
98                                         {
99                                                 WriteServ(user->fd,"491 %s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick);
100                                                 WriteOpers("*** CONFIGURATION ERROR! Oper type invalid for OperType '%s'",OperType);
101                                                 log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type erroneous.",user->nick,user->ident,user->host);
102                                                 return;
103                                         }
104                                         strlcpy(user->oper,TypeName,NICKMAX-1);
105                                         found = true;
106                                         fail2 = false;
107                                         break;
108                                 }
109                         }
110                 }
111                 if (found)
112                         break;
113         }
114         if (found)
115         {
116                 /* correct oper credentials */
117                 WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
118                 WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,OperType);
119                 if (!user->modes[UM_OPERATOR])
120                 {
121                         user->modes[UM_OPERATOR] = 1;
122                         WriteServ(user->fd,"MODE %s :+o",user->nick);
123                         FOREACH_MOD(I_OnOper,OnOper(user,OperType));
124                         log(DEFAULT,"OPER: %s!%s@%s opered as type: %s",user->nick,user->ident,user->host,OperType);
125                         AddOper(user);
126                         FOREACH_MOD(I_OnPostOper,OnPostOper(user,OperType));
127                 }
128         }
129         else
130         {
131                 if (!fail2)
132                 {
133                         WriteServ(user->fd,"491 %s :Invalid oper credentials",user->nick);
134                         WriteOpers("*** WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
135                         log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: user, host or password did not match.",user->nick,user->ident,user->host);
136                 }
137                 else
138                 {
139                         WriteServ(user->fd,"491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
140                         WriteOpers("*** CONFIGURATION ERROR! Oper block mismatch for OperType %s",OperType);
141                         log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type nonexistent.",user->nick,user->ident,user->host);
142                 }
143         }
144         return;
145 }