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