]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslinfo.cpp
Remove InspIRCd* parameters and fields
[user/henk/code/inspircd.git] / src / modules / m_sslinfo.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "transport.h"
16
17 /* $ModDesc: Provides SSL metadata, including /WHOIS information and /SSLINFO command */
18
19 class SSLCertExt : public ExtensionItem {
20  public:
21         SSLCertExt(Module* parent) : ExtensionItem("ssl_cert", parent) {}
22         ssl_cert* get(const Extensible* item)
23         {
24                 return static_cast<ssl_cert*>(get_raw(item));
25         }
26         void set(Extensible* item, ssl_cert* value)
27         {
28                 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
29                 delete old;
30         }
31
32         std::string serialize(SerializeFormat format, const Extensible* container, void* item)
33         {
34                 return static_cast<ssl_cert*>(item)->GetMetaLine();
35         }
36
37         void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
38         {
39                 ssl_cert* cert = new ssl_cert;
40                 set(container, cert);
41
42                 std::stringstream s(value);
43                 std::string v;
44                 getline(s,v,' ');
45
46                 cert->invalid = (v.find('v') != std::string::npos);
47                 cert->trusted = (v.find('T') != std::string::npos);
48                 cert->revoked = (v.find('R') != std::string::npos);
49                 cert->unknownsigner = (v.find('s') != std::string::npos);
50                 if (v.find('E') != std::string::npos)
51                 {
52                         getline(s,cert->error,'\n');
53                 }
54                 else
55                 {
56                         getline(s,cert->fingerprint,' ');
57                         getline(s,cert->dn,' ');
58                         getline(s,cert->issuer,'\n');
59                 }
60         }
61
62         void free(void* item)
63         {
64                 delete static_cast<ssl_cert*>(item);
65         }
66 };
67
68 /** Handle /SSLINFO
69  */
70 class CommandSSLInfo : public Command
71 {
72  public:
73         SSLCertExt CertExt;
74
75         CommandSSLInfo(Module* Creator) : Command(Creator, "SSLINFO", 1), CertExt(Creator)
76         {
77                 this->syntax = "<nick>";
78         }
79
80         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
81         {
82                 User* target = ServerInstance->FindNick(parameters[0]);
83
84                 if (target)
85                 {
86                         ssl_cert* cert = CertExt.get(target);
87                         if (cert)
88                         {
89                                 if (cert->GetError().length())
90                                 {
91                                         user->WriteServ("NOTICE %s :*** No SSL certificate information for this user (%s).", user->nick.c_str(), cert->GetError().c_str());
92                                 }
93                                 else
94                                 {
95                                         user->WriteServ("NOTICE %s :*** Distinguised Name: %s", user->nick.c_str(), cert->GetDN().c_str());
96                                         user->WriteServ("NOTICE %s :*** Issuer:            %s", user->nick.c_str(), cert->GetIssuer().c_str());
97                                         user->WriteServ("NOTICE %s :*** Key Fingerprint:   %s", user->nick.c_str(), cert->GetFingerprint().c_str());
98                                 }
99                                 return CMD_SUCCESS;
100                         }
101                         else
102                         {
103                                 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user.", user->nick.c_str());
104                                 return CMD_FAILURE;
105                         }
106                 }
107                 else
108                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
109
110                 return CMD_FAILURE;
111         }
112 };
113
114 class ModuleSSLInfo : public Module
115 {
116         CommandSSLInfo cmd;
117
118  public:
119         ModuleSSLInfo()
120                 : cmd(this)
121         {
122                 ServerInstance->AddCommand(&cmd);
123
124                 Extensible::Register(&cmd.CertExt);
125
126                 Implementation eventlist[] = { I_OnWhois, I_OnPreCommand };
127                 ServerInstance->Modules->Attach(eventlist, this, 2);
128         }
129
130         ~ModuleSSLInfo()
131         {
132         }
133
134         Version GetVersion()
135         {
136                 return Version("SSL Certificate Utilities", VF_VENDOR);
137         }
138
139         void OnWhois(User* source, User* dest)
140         {
141                 if (cmd.CertExt.get(dest))
142                 {
143                         ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
144                 }
145         }
146
147         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
148         {
149                 std::stringstream hl(hostlist);
150                 std::string xhost;
151                 while (hl >> xhost)
152                 {
153                         if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
154                         {
155                                 return true;
156                         }
157                 }
158                 return false;
159         }
160
161         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
162         {
163                 irc::string pcmd = command.c_str();
164
165                 if ((pcmd == "OPER") && (validated))
166                 {
167                         ConfigReader cf;
168                         char TheHost[MAXBUF];
169                         char TheIP[MAXBUF];
170                         std::string LoginName;
171                         std::string Password;
172                         std::string OperType;
173                         std::string HostName;
174                         std::string HashType;
175                         std::string FingerPrint;
176                         bool SSLOnly;
177                         ssl_cert* cert = cmd.CertExt.get(user);
178
179                         snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
180                         snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
181
182                         for (int i = 0; i < cf.Enumerate("oper"); i++)
183                         {
184                                 LoginName = cf.ReadValue("oper", "name", i);
185                                 Password = cf.ReadValue("oper", "password", i);
186                                 OperType = cf.ReadValue("oper", "type", i);
187                                 HostName = cf.ReadValue("oper", "host", i);
188                                 HashType = cf.ReadValue("oper", "hash", i);
189                                 FingerPrint = cf.ReadValue("oper", "fingerprint", i);
190                                 SSLOnly = cf.ReadFlag("oper", "sslonly", i);
191
192                                 if (FingerPrint.empty() && !SSLOnly)
193                                         continue;
194
195                                 if (LoginName != parameters[0])
196                                         continue;
197
198                                 if (!OneOfMatches(TheHost, TheIP, HostName.c_str()))
199                                         continue;
200
201                                 if (Password.length() && ServerInstance->PassCompare(user, Password.c_str(),parameters[1].c_str(), HashType.c_str()))
202                                         continue;
203
204                                 if (SSLOnly && !cert)
205                                 {
206                                         user->WriteNumeric(491, "%s :This oper login name requires an SSL connection.", user->nick.c_str());
207                                         return MOD_RES_DENY;
208                                 }
209
210                                 /*
211                                  * No cert found or the fingerprint doesn't match
212                                  */
213                                 if ((!cert) || (cert->GetFingerprint() != FingerPrint))
214                                 {
215                                         user->WriteNumeric(491, "%s :This oper login name requires a matching key fingerprint.",user->nick.c_str());
216                                         ServerInstance->SNO->WriteToSnoMask('o',"'%s' cannot oper, does not match fingerprint", user->nick.c_str());
217                                         ServerInstance->Logs->Log("m_ssl_oper_cert",DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but wrong fingerprint.", user->nick.c_str(), user->ident.c_str(), user->host.c_str());
218                                         return MOD_RES_DENY;
219                                 }
220                         }
221                 }
222
223                 // Let core handle it for extra stuff
224                 return MOD_RES_PASSTHRU;
225         }
226
227         const char* OnRequest(Request* request)
228         {
229                 if (strcmp("GET_CERT", request->GetId()) == 0)
230                 {
231                         BufferedSocketCertificateRequest* req = static_cast<BufferedSocketCertificateRequest*>(request);
232                         req->cert = cmd.CertExt.get(req->item);
233                 }
234                 else if (strcmp("SET_CERT", request->GetId()) == 0)
235                 {
236                         BufferedSocketFingerprintSubmission* req = static_cast<BufferedSocketFingerprintSubmission*>(request);
237                         cmd.CertExt.set(req->item, req->cert);
238                 }
239                 return NULL;
240         }
241 };
242
243 MODULE_INIT(ModuleSSLInfo)
244