]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_oper.cpp
We were already sending FMODE +nt after each channel creation to keep services happy...
[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 TheHost[MAXBUF];
50         char TheIP[MAXBUF];
51         int j;
52         bool found = false;
53         bool type_invalid = false;
54
55         bool match_login = false;
56         bool match_pass = false;
57         bool match_hosts = false;
58
59         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
60         snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
61
62         for (int i = 0; i < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "oper"); i++)
63         {
64                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "name", i, LoginName, MAXBUF);
65                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "password", i, Password, MAXBUF);
66                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "type", i, OperType, MAXBUF);
67                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "oper", "host", i, HostName, MAXBUF);
68
69                 match_login = !strcmp(LoginName,parameters[0]);
70                 match_pass = !ServerInstance->OperPassCompare(Password,parameters[1], i);
71                 match_hosts = OneOfMatches(TheHost,TheIP,HostName);
72
73                 if (match_login && match_pass && match_hosts)
74                 {
75                         type_invalid = true;
76                         for (j =0; j < ServerInstance->Config->ConfValueEnum(ServerInstance->Config->config_data, "type"); j++)
77                         {
78                                 ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","name", j, TypeName, MAXBUF);
79
80                                 if (!strcmp(TypeName,OperType))
81                                 {
82                                         /* found this oper's opertype */
83                                         if (!ServerInstance->IsNick(TypeName))
84                                         {
85                                                 user->WriteServ("491 %s :Invalid oper type (oper types must follow the same syntax as nicknames)",user->nick);
86                                                 ServerInstance->SNO->WriteToSnoMask('o',"CONFIGURATION ERROR! Oper type '%s' contains invalid characters",OperType);
87                                                 ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but oper type erroneous.",user->nick,user->ident,user->host);
88                                                 return CMD_FAILURE;
89                                         }
90                                         ServerInstance->Config->ConfValue(ServerInstance->Config->config_data, "type","host", j, HostName, MAXBUF);
91                                         if (*HostName)
92                                                 user->ChangeDisplayedHost(HostName);
93                                         found = true;
94                                         type_invalid = false;
95                                         break;
96                                 }
97                         }
98                 }
99                 if (match_login || found)
100                         break;
101         }
102         if (found)
103         {
104                 /* correct oper credentials */
105                 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]);
106                 user->WriteServ("381 %s :You are now an IRC operator of type %s",user->nick,irc::Spacify(OperType));
107                 if (!user->modes[UM_OPERATOR])
108                         user->Oper(OperType);
109         }
110         else
111         {
112                 std::deque<std::string> n;
113                 n.push_back("o");
114                 char broadcast[MAXBUF];
115
116                 if (!type_invalid)
117                 {
118                         std::string fields = "";
119                         if (!match_login)
120                                 fields.append("login ");
121                         if (!match_pass)
122                                 fields.append("password ");
123                         if (!match_hosts)
124                                 fields.append("hosts");
125                         user->WriteServ("491 %s :Invalid oper credentials",user->nick);
126                         
127                         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());
128                         ServerInstance->SNO->WriteToSnoMask('o',std::string(broadcast));
129                         n.push_back(broadcast);
130                         Event rmode2((char *)&n, NULL, "send_snoset");
131                         rmode2.Send(ServerInstance);
132
133                         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());
134                         return CMD_FAILURE;
135                 }
136                 else
137                 {
138                         user->WriteServ("491 %s :Your oper block does not have a valid opertype associated with it",user->nick);
139
140                         snprintf(broadcast, MAXBUF, "CONFIGURATION ERROR! Oper block '%s': missing OperType %s",parameters[0],OperType);
141
142                         ServerInstance->SNO->WriteToSnoMask('o', std::string(broadcast));
143                         n.push_back(broadcast);
144                         Event rmode2((char *)&n, NULL, "send_snoset");
145                         rmode2.Send(ServerInstance);
146
147                         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]);
148                         return CMD_FAILURE;
149                 }
150         }
151         return CMD_SUCCESS;
152 }
153