]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ssl_oper_cert.cpp
Add move-match-to-head-of-queue stuff to pcre filter
[user/henk/code/inspircd.git] / src / modules / extra / m_ssl_oper_cert.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 /* $ModDesc: Allows for MD5 encrypted oper passwords */
15 /* $ModDep: transport.h */
16
17 #include <stdio.h>
18 #include "inspircd_config.h"
19 #include "users.h"
20 #include "channels.h"
21 #include "modules.h"
22 #include "inspircd.h"
23 #include "transport.h"
24 #include "wildcard.h"
25
26 /** Handle /FINGERPRINT
27  */
28 class cmd_fingerprint : public command_t
29 {
30  public:
31         cmd_fingerprint (InspIRCd* Instance) : command_t(Instance,"FINGERPRINT", 0, 1)
32         {
33                 this->source = "m_ssl_oper_cert.so";
34                 syntax = "<nickname>";
35         }       
36                   
37         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
38         {
39                 userrec* target = ServerInstance->FindNick(parameters[0]);
40                 if (target)
41                 {
42                         ssl_cert* cert;
43                         if (target->GetExt("ssl_cert",cert))
44                         {
45                                 if (cert->GetFingerprint().length())
46                                 {
47                                         user->WriteServ("NOTICE %s :Certificate fingerprint for %s is %s",user->nick,target->nick,cert->GetFingerprint().c_str());
48                                         return CMD_SUCCESS;
49                                 }
50                                 else
51                                 {
52                                         user->WriteServ("NOTICE %s :Certificate fingerprint for %s does not exist!", user->nick,target->nick);
53                                         return CMD_FAILURE;
54                                 }
55                         }
56                         else
57                         {
58                                 user->WriteServ("NOTICE %s :Certificate fingerprint for %s does not exist!", user->nick, target->nick);
59                                 return CMD_FAILURE;
60                         }
61                 }
62                 else
63                 {
64                         user->WriteServ("401 %s %s :No such nickname", user->nick, parameters[0]);
65                         return CMD_FAILURE;
66                 }
67         }
68 };
69
70
71
72 class ModuleOperSSLCert : public Module
73 {
74         ssl_cert* cert;
75         bool HasCert;
76         cmd_fingerprint* mycommand;
77         ConfigReader* cf;
78  public:
79
80         ModuleOperSSLCert(InspIRCd* Me)
81                 : Module::Module(Me)
82         {
83                 mycommand = new cmd_fingerprint(ServerInstance);
84                 ServerInstance->AddCommand(mycommand);
85                 cf = new ConfigReader(ServerInstance);
86         }
87
88         virtual ~ModuleOperSSLCert()
89         {
90                 delete cf;
91         }
92
93         void Implements(char* List)
94         {
95                 List[I_OnPreCommand] = List[I_OnRehash] = 1;
96         }
97
98         virtual void OnRehash(userrec* user, const std::string &parameter)
99         {
100                 delete cf;
101                 cf = new ConfigReader(ServerInstance);
102         }
103
104         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
105         {
106                 std::stringstream hl(hostlist);
107                 std::string xhost;
108                 while (hl >> xhost)
109                 {
110                         if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
111                         {
112                                 return true;
113                         }
114                 }
115                 return false;
116         }
117
118
119         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
120         {
121                 irc::string cmd = command.c_str();
122                 
123                 if ((cmd == "OPER") && (validated))
124                 {
125                         char TheHost[MAXBUF];
126                         char TheIP[MAXBUF];
127                         std::string LoginName;
128                         std::string Password;
129                         std::string OperType;
130                         std::string HostName;
131                         std::string FingerPrint;
132                         bool SSLOnly;
133                         char* dummy;
134
135                         snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
136                         snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
137
138                         HasCert = user->GetExt("ssl_cert",cert);
139
140                         for (int i = 0; i < cf->Enumerate("oper"); i++)
141                         {
142                                 LoginName = cf->ReadValue("oper", "name", i);
143                                 Password = cf->ReadValue("oper", "password", i);
144                                 OperType = cf->ReadValue("oper", "type", i);
145                                 HostName = cf->ReadValue("oper", "host", i);
146                                 FingerPrint = cf->ReadValue("oper", "fingerprint", i);
147                                 SSLOnly = cf->ReadFlag("oper", "sslonly", i);
148
149                                 if (SSLOnly || !FingerPrint.empty())
150                                 {
151                                         if ((!strcmp(LoginName.c_str(),parameters[0])) && (!ServerInstance->OperPassCompare(Password.c_str(),parameters[1],i)) && (OneOfMatches(TheHost,TheIP,HostName.c_str())))
152                                         {
153                                                 if (SSLOnly && !user->GetExt("ssl", dummy))
154                                                 {
155                                                         user->WriteServ("491 %s :This oper login name requires an SSL connection.", user->nick);
156                                                         return 1;
157                                                 }
158
159                                                 /* This oper would match */
160                                                 if ((!cert) || (cert->GetFingerprint() != FingerPrint))
161                                                 {
162                                                         user->WriteServ("491 %s :This oper login name requires a matching key fingerprint.",user->nick);
163                                                         ServerInstance->SNO->WriteToSnoMask('o',"'%s' cannot oper, does not match fingerprint", user->nick);
164                                                         ServerInstance->Log(DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but wrong fingerprint.",user->nick,user->ident,user->host);
165                                                         return 1;
166                                                 }
167                                         }
168                                 }
169                         }
170                 }
171                 return 0;
172         }
173
174         virtual Version GetVersion()
175         {
176                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
177         }
178 };
179
180 class ModuleOperSSLCertFactory : public ModuleFactory
181 {
182  public:
183         ModuleOperSSLCertFactory()
184         {
185         }
186         
187         ~ModuleOperSSLCertFactory()
188         {
189         }
190         
191         virtual Module * CreateModule(InspIRCd* Me)
192         {
193                 return new ModuleOperSSLCert(Me);
194         }
195         
196 };
197
198
199 extern "C" void * init_module( void )
200 {
201         return new ModuleOperSSLCertFactory;
202 }