]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sslinfo.cpp
Improve the description of the maphide module.
[user/henk/code/inspircd.git] / src / modules / m_sslinfo.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "modules/ssl.h"
22 #include "modules/webirc.h"
23 #include "modules/whois.h"
24
25 enum
26 {
27         // From oftc-hybrid.
28         RPL_WHOISCERTFP = 276,
29
30         // From UnrealIRCd.
31         RPL_WHOISSECURE = 671
32 };
33
34 class SSLCertExt : public ExtensionItem {
35  public:
36         SSLCertExt(Module* parent)
37                 : ExtensionItem("ssl_cert", ExtensionItem::EXT_USER, parent)
38         {
39         }
40
41         ssl_cert* get(const Extensible* item) const
42         {
43                 return static_cast<ssl_cert*>(get_raw(item));
44         }
45         void set(Extensible* item, ssl_cert* value)
46         {
47                 value->refcount_inc();
48                 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
49                 if (old && old->refcount_dec())
50                         delete old;
51         }
52
53         void unset(Extensible* container)
54         {
55                 void* old = unset_raw(container);
56                 delete static_cast<std::string*>(old);
57         }
58
59         std::string serialize(SerializeFormat format, const Extensible* container, void* item) const CXX11_OVERRIDE
60         {
61                 return static_cast<ssl_cert*>(item)->GetMetaLine();
62         }
63
64         void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE
65         {
66                 ssl_cert* cert = new ssl_cert;
67                 set(container, cert);
68
69                 std::stringstream s(value);
70                 std::string v;
71                 getline(s,v,' ');
72
73                 cert->invalid = (v.find('v') != std::string::npos);
74                 cert->trusted = (v.find('T') != std::string::npos);
75                 cert->revoked = (v.find('R') != std::string::npos);
76                 cert->unknownsigner = (v.find('s') != std::string::npos);
77                 if (v.find('E') != std::string::npos)
78                 {
79                         getline(s,cert->error,'\n');
80                 }
81                 else
82                 {
83                         getline(s,cert->fingerprint,' ');
84                         getline(s,cert->dn,' ');
85                         getline(s,cert->issuer,'\n');
86                 }
87         }
88
89         void free(Extensible* container, void* item) CXX11_OVERRIDE
90         {
91                 ssl_cert* old = static_cast<ssl_cert*>(item);
92                 if (old && old->refcount_dec())
93                         delete old;
94         }
95 };
96
97 /** Handle /SSLINFO
98  */
99 class CommandSSLInfo : public Command
100 {
101  public:
102         SSLCertExt CertExt;
103
104         CommandSSLInfo(Module* Creator) : Command(Creator, "SSLINFO", 1), CertExt(Creator)
105         {
106                 this->syntax = "<nick>";
107         }
108
109         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
110         {
111                 User* target = ServerInstance->FindNickOnly(parameters[0]);
112
113                 if ((!target) || (target->registered != REG_ALL))
114                 {
115                         user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
116                         return CMD_FAILURE;
117                 }
118                 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
119                 if (operonlyfp && !user->IsOper() && target != user)
120                 {
121                         user->WriteNotice("*** You cannot view SSL certificate information for other users");
122                         return CMD_FAILURE;
123                 }
124                 ssl_cert* cert = CertExt.get(target);
125                 if (!cert)
126                 {
127                         user->WriteNotice("*** No SSL certificate for this user");
128                 }
129                 else if (cert->GetError().length())
130                 {
131                         user->WriteNotice("*** No SSL certificate information for this user (" + cert->GetError() + ").");
132                 }
133                 else
134                 {
135                         user->WriteNotice("*** Distinguished Name: " + cert->GetDN());
136                         user->WriteNotice("*** Issuer:             " + cert->GetIssuer());
137                         user->WriteNotice("*** Key Fingerprint:    " + cert->GetFingerprint());
138                 }
139                 return CMD_SUCCESS;
140         }
141 };
142
143 class UserCertificateAPIImpl : public UserCertificateAPIBase
144 {
145         SSLCertExt& ext;
146
147  public:
148         UserCertificateAPIImpl(Module* mod, SSLCertExt& certext)
149                 : UserCertificateAPIBase(mod), ext(certext)
150         {
151         }
152
153         ssl_cert* GetCertificate(User* user) CXX11_OVERRIDE
154         {
155                 return ext.get(user);
156         }
157
158         void SetCertificate(User* user, ssl_cert* cert) CXX11_OVERRIDE
159         {
160                 ext.set(user, cert);
161         }
162 };
163
164 class ModuleSSLInfo
165         : public Module
166         , public WebIRC::EventListener
167         , public Whois::EventListener
168 {
169         CommandSSLInfo cmd;
170         UserCertificateAPIImpl APIImpl;
171
172  public:
173         ModuleSSLInfo()
174                 : WebIRC::EventListener(this)
175                 , Whois::EventListener(this)
176                 , cmd(this)
177                 , APIImpl(this, cmd.CertExt)
178         {
179         }
180
181         Version GetVersion() CXX11_OVERRIDE
182         {
183                 return Version("SSL Certificate Utilities", VF_VENDOR);
184         }
185
186         void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
187         {
188                 ssl_cert* cert = cmd.CertExt.get(whois.GetTarget());
189                 if (cert)
190                 {
191                         whois.SendLine(RPL_WHOISSECURE, "is using a secure connection");
192                         bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
193                         if ((!operonlyfp || whois.IsSelfWhois() || whois.GetSource()->IsOper()) && !cert->fingerprint.empty())
194                                 whois.SendLine(RPL_WHOISCERTFP, InspIRCd::Format("has client certificate fingerprint %s", cert->fingerprint.c_str()));
195                 }
196         }
197
198         ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
199         {
200                 if ((command == "OPER") && (validated))
201                 {
202                         ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
203                         if (i != ServerInstance->Config->oper_blocks.end())
204                         {
205                                 OperInfo* ifo = i->second;
206                                 ssl_cert* cert = cmd.CertExt.get(user);
207
208                                 if (ifo->oper_block->getBool("sslonly") && !cert)
209                                 {
210                                         user->WriteNumeric(ERR_NOOPERHOST, "This oper login requires an SSL connection.");
211                                         user->CommandFloodPenalty += 10000;
212                                         return MOD_RES_DENY;
213                                 }
214
215                                 std::string fingerprint;
216                                 if (ifo->oper_block->readString("fingerprint", fingerprint) && (!cert || cert->GetFingerprint() != fingerprint))
217                                 {
218                                         user->WriteNumeric(ERR_NOOPERHOST, "This oper login requires a matching SSL certificate fingerprint.");
219                                         user->CommandFloodPenalty += 10000;
220                                         return MOD_RES_DENY;
221                                 }
222                         }
223                 }
224
225                 // Let core handle it for extra stuff
226                 return MOD_RES_PASSTHRU;
227         }
228
229         void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
230         {
231                 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
232                 if (cert)
233                         cmd.CertExt.set(user, cert);
234         }
235
236         void OnPostConnect(User* user) CXX11_OVERRIDE
237         {
238                 LocalUser* const localuser = IS_LOCAL(user);
239                 if (!localuser)
240                         return;
241
242                 const SSLIOHook* const ssliohook = SSLIOHook::IsSSL(&localuser->eh);
243                 if (!ssliohook)
244                         return;
245
246                 ssl_cert* const cert = ssliohook->GetCertificate();
247
248                 {
249                         std::string text = "*** You are connected to ";
250                         if (!ssliohook->GetServerName(text))
251                                 text.append(ServerInstance->Config->ServerName);
252                         text.append(" using SSL cipher '");
253                         ssliohook->GetCiphersuite(text);
254                         text.push_back('\'');
255                         if ((cert) && (!cert->GetFingerprint().empty()))
256                                 text.append(" and your SSL certificate fingerprint is ").append(cert->GetFingerprint());
257                         user->WriteNotice(text);
258                 }
259
260                 if (!cert)
261                         return;
262                 // find an auto-oper block for this user
263                 for (ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); ++i)
264                 {
265                         OperInfo* ifo = i->second;
266                         std::string fp = ifo->oper_block->getString("fingerprint");
267                         if (fp == cert->fingerprint && ifo->oper_block->getBool("autologin"))
268                                 user->Oper(ifo);
269                 }
270         }
271
272         ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
273         {
274                 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
275                 bool ok = true;
276                 if (myclass->config->getString("requiressl") == "trusted")
277                 {
278                         ok = (cert && cert->IsCAVerified());
279                         ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "Class requires a trusted SSL cert. Client %s one.", (ok ? "has" : "does not have"));
280                 }
281                 else if (myclass->config->getBool("requiressl"))
282                 {
283                         ok = (cert != NULL);
284                         ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "Class requires any SSL cert. Client %s one.", (ok ? "has" : "does not have"));
285                 }
286
287                 if (!ok)
288                         return MOD_RES_DENY;
289                 return MOD_RES_PASSTHRU;
290         }
291
292         void OnWebIRCAuth(LocalUser* user, const WebIRC::FlagMap* flags) CXX11_OVERRIDE
293         {
294                 // We are only interested in connection flags. If none have been
295                 // given then we have nothing to do.
296                 if (!flags)
297                         return;
298
299                 // We only care about the tls connection flag if the connection
300                 // between the gateway and the server is secure.
301                 if (!cmd.CertExt.get(user))
302                         return;
303
304                 WebIRC::FlagMap::const_iterator iter = flags->find("secure");
305                 if (iter == flags->end())
306                 {
307                         // If this is not set then the connection between the client and
308                         // the gateway is not secure.
309                         cmd.CertExt.unset(user);
310                         return;
311                 }
312
313                 // Create a fake ssl_cert for the user.
314                 ssl_cert* cert = new ssl_cert;
315                 cert->error = "WebIRC users can not specify valid certs yet";
316                 cert->invalid = true;
317                 cert->revoked = true;
318                 cert->trusted = false;
319                 cert->unknownsigner = true;
320                 cmd.CertExt.set(user, cert);
321         }
322 };
323
324 MODULE_INIT(ModuleSSLInfo)