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