]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ssl_oper_cert.cpp
YAY! A module which allows a user to oper via their ssl key fingerprint.
[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
29 class cmd_fingerprint : public command_t
30 {
31  public:
32         cmd_fingerprint (InspIRCd* Instance) : command_t(Instance,"FINGERPRINT", 0, 1)
33         {
34                 this->source = "m_ssl_oper_cert.so";
35                 syntax = "<nickname>";
36         }       
37                   
38         void Handle (const char** parameters, int pcnt, userrec *user)
39         {
40                 userrec* target = ServerInstance->FindNick(parameters[0]);
41                 if (target)
42                 {
43                         ssl_cert* cert;
44                         if (target->GetExt("ssl_cert",cert))
45                         {
46                                 if (cert->GetFingerprint().length())
47                                         user->WriteServ("NOTICE %s :Certificate fingerprint for %s is %s",user->nick,target->nick,cert->GetFingerprint().c_str());
48                                 else
49                                         user->WriteServ("NOTICE %s :Certificate fingerprint for %s does not exist!", user->nick,target->nick);
50                         }
51                         else
52                         {
53                                 user->WriteServ("NOTICE %s :Certificate fingerprint for %s does not exist!", user->nick, target->nick);
54                         }
55                 }
56                 else
57                 {
58                         user->WriteServ("401 %s %s :No such nickname", user->nick, parameters[0]);
59                 }
60         }
61 };
62
63
64 class ModuleOperSSLCert : public Module
65 {
66         ssl_cert* cert;
67         bool HasCert;
68         cmd_fingerprint* mycommand;
69  public:
70
71         ModuleOperSSLCert(InspIRCd* Me)
72                 : Module::Module(Me)
73         {
74                 
75                 mycommand = new cmd_fingerprint(ServerInstance);
76                 ServerInstance->AddCommand(mycommand);
77         }
78         
79         virtual ~ModuleOperSSLCert()
80         {
81         }
82
83         void Implements(char* List)
84         {
85                 List[I_OnOperCompare] = List[I_OnPreCommand] = 1;
86         }
87
88         virtual int OnOperCompare(const std::string &data, const std::string &input)
89         {
90                 ServerInstance->Log(DEBUG,"HasCert=%d, data='%s' input='%s'",HasCert,data.c_str(), input.c_str());
91                 if (((data.length()) && (data.length() == cert->GetFingerprint().length())))
92                 {
93                         ServerInstance->Log(DEBUG,"Lengths match, cert='%s'",cert->GetFingerprint().c_str());
94                         if (data == cert->GetFingerprint())
95                         {
96                                 ServerInstance->Log(DEBUG,"Return 1");
97                                 return 1;
98                         }
99                         else
100                         {
101                                 ServerInstance->Log(DEBUG,"'%s' != '%s'",data.c_str(), cert->GetFingerprint().c_str());
102                                 return 0;
103                         }
104                 }
105                 else
106                 {
107                         ServerInstance->Log(DEBUG,"Lengths dont match");
108                         return 0;
109                 }
110         }
111
112         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated)
113         {
114                 irc::string cmd = command.c_str();
115                 
116                 if ((cmd == "OPER") && (validated == 1))
117                 {
118                         HasCert = user->GetExt("ssl_cert",cert);
119                         ServerInstance->Log(DEBUG,"HasCert=%d",HasCert);
120                 }
121                 return 0;
122         }
123
124         virtual Version GetVersion()
125         {
126                 return Version(1,1,0,0,VF_VENDOR);
127         }
128 };
129
130 class ModuleOperSSLCertFactory : public ModuleFactory
131 {
132  public:
133         ModuleOperSSLCertFactory()
134         {
135         }
136         
137         ~ModuleOperSSLCertFactory()
138         {
139         }
140         
141         virtual Module * CreateModule(InspIRCd* Me)
142         {
143                 return new ModuleOperSSLCert(Me);
144         }
145         
146 };
147
148
149 extern "C" void * init_module( void )
150 {
151         return new ModuleOperSSLCertFactory;
152 }