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