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