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