]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_oper.cpp
Initial UUID generation code. Generates a TS6 compatible UUID.
[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 "inspircd.h"
15 #include "configreader.h"
16 #include "typedefs.h"
17 #include "users.h"
18 #include "modules.h"
19 #include "wildcard.h"
20 #include "commands/cmd_oper.h"
21 #include "hashcomp.h"
22
23 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
24 {
25         std::stringstream hl(hostlist);
26         std::string xhost;
27         while (hl >> xhost)
28         {
29                 if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
30                 {
31                         return true;
32                 }
33         }
34         return false;
35 }
36
37 extern "C" DllExport command_t* init_command(InspIRCd* Instance)
38 {
39         return new cmd_oper(Instance);
40 }
41
42 CmdResult cmd_oper::Handle (const char** parameters, int pcnt, userrec *user)
43 {
44         char LoginName[MAXBUF];
45         char Password[MAXBUF];
46         char OperType[MAXBUF];
47         char TypeName[MAXBUF];
48         char HostName[MAXBUF];
49         char ClassName[MAXBUF];
50         char TheHost[MAXBUF];
51         char TheIP[MAXBUF];
52         int j;
53         bool found = false;
54         bool type_invalid = false;
55
56         bool match_login = false;
57         bool match_pass = false;
58         bool match_hosts = false;
59
60         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
61         snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
62
63         for (int i = 0; i < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "oper"); i++)
64         {
65                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "name", i, LoginName, MAXBUF);
66                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "password", i, Password, MAXBUF);
67                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "type", i, OperType, MAXBUF);
68                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "host", i, HostName, MAXBUF);
69
70                 match_login = !strcmp(LoginName,parameters[0]);
71                 match_pass = !ServerInstance->OperPassCompare(Password,parameters[1], i);
72                 match_hosts = OneOfMatches(TheHost,TheIP,HostName);
73
74                 if (match_login && match_pass && match_hosts)
75                 {
76                         type_invalid = true;
77                         for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
78                         {
79                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type", "name", j, TypeName, MAXBUF);
80                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type", "class", j, ClassName, MAXBUF);
81
82                                 if (!strcmp(TypeName,OperType))
83                                 {
84                                         /* found this oper's opertype */
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                                         ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
93                                         if (*HostName)
94                                                 user->ChangeDisplayedHost(HostName);
95                                         if (*ClassName)
96                                                 user->CheckClass(ClassName);
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,irc::Spacify(OperType),parameters[0]);
110                 user->WriteServ("381 %s :You are now an IRC operator of type %s",user->nick,irc::Spacify(OperType));
111                 if (!user->IsModeSet('o'))
112                         user->Oper(OperType);
113         }
114         else
115         {
116                 std::deque<std::string> n;
117                 n.push_back("o");
118                 char broadcast[MAXBUF];
119
120                 if (!type_invalid)
121                 {
122                         std::string fields;
123                         if (!match_login)
124                                 fields.append("login ");
125                         if (!match_pass)
126                                 fields.append("password ");
127                         if (!match_hosts)
128                                 fields.append("hosts");
129                         user->WriteServ("491 %s :Invalid oper credentials",user->nick);
130                         
131                         snprintf(broadcast, MAXBUF, "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());
132                         ServerInstance->SNO->WriteToSnoMask('o',std::string(broadcast));
133                         n.push_back(broadcast);
134                         Event rmode2((char *)&n, NULL, "send_snoset");
135                         rmode2.Send(ServerInstance);
136
137                         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());
138                         return CMD_FAILURE;
139                 }
140                 else
141                 {
142                         user->WriteServ("491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
143
144                         snprintf(broadcast, MAXBUF, "CONFIGURATION ERROR! Oper block '%s': missing OperType %s",parameters[0],OperType);
145
146                         ServerInstance->SNO->WriteToSnoMask('o', std::string(broadcast));
147                         n.push_back(broadcast);
148                         Event rmode2((char *)&n, NULL, "send_snoset");
149                         rmode2.Send(ServerInstance);
150
151                         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]);
152                         return CMD_FAILURE;
153                 }
154         }
155         return CMD_SUCCESS;
156 }
157