2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
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.
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
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/>.
21 #include "modules/ssl.h"
22 #include "modules/whois.h"
27 RPL_WHOISCERTFP = 276,
33 class SSLCertExt : public ExtensionItem {
35 SSLCertExt(Module* parent)
36 : ExtensionItem("ssl_cert", ExtensionItem::EXT_USER, parent)
40 ssl_cert* get(const Extensible* item) const
42 return static_cast<ssl_cert*>(get_raw(item));
44 void set(Extensible* item, ssl_cert* value)
46 value->refcount_inc();
47 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
48 if (old && old->refcount_dec())
52 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const CXX11_OVERRIDE
54 return static_cast<ssl_cert*>(item)->GetMetaLine();
57 void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE
59 ssl_cert* cert = new ssl_cert;
62 std::stringstream s(value);
66 cert->invalid = (v.find('v') != std::string::npos);
67 cert->trusted = (v.find('T') != std::string::npos);
68 cert->revoked = (v.find('R') != std::string::npos);
69 cert->unknownsigner = (v.find('s') != std::string::npos);
70 if (v.find('E') != std::string::npos)
72 getline(s,cert->error,'\n');
76 getline(s,cert->fingerprint,' ');
77 getline(s,cert->dn,' ');
78 getline(s,cert->issuer,'\n');
82 void free(Extensible* container, void* item) CXX11_OVERRIDE
84 ssl_cert* old = static_cast<ssl_cert*>(item);
85 if (old && old->refcount_dec())
92 class CommandSSLInfo : public Command
97 CommandSSLInfo(Module* Creator) : Command(Creator, "SSLINFO", 1), CertExt(Creator)
99 this->syntax = "<nick>";
102 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
104 User* target = ServerInstance->FindNickOnly(parameters[0]);
106 if ((!target) || (target->registered != REG_ALL))
108 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
111 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
112 if (operonlyfp && !user->IsOper() && target != user)
114 user->WriteNotice("*** You cannot view SSL certificate information for other users");
117 ssl_cert* cert = CertExt.get(target);
120 user->WriteNotice("*** No SSL certificate for this user");
122 else if (cert->GetError().length())
124 user->WriteNotice("*** No SSL certificate information for this user (" + cert->GetError() + ").");
128 user->WriteNotice("*** Distinguished Name: " + cert->GetDN());
129 user->WriteNotice("*** Issuer: " + cert->GetIssuer());
130 user->WriteNotice("*** Key Fingerprint: " + cert->GetFingerprint());
136 class UserCertificateAPIImpl : public UserCertificateAPIBase
141 UserCertificateAPIImpl(Module* mod, SSLCertExt& certext)
142 : UserCertificateAPIBase(mod), ext(certext)
146 ssl_cert* GetCertificate(User* user) CXX11_OVERRIDE
148 return ext.get(user);
152 class ModuleSSLInfo : public Module, public Whois::EventListener
155 UserCertificateAPIImpl APIImpl;
159 : Whois::EventListener(this)
161 , APIImpl(this, cmd.CertExt)
165 Version GetVersion() CXX11_OVERRIDE
167 return Version("SSL Certificate Utilities", VF_VENDOR);
170 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
172 ssl_cert* cert = cmd.CertExt.get(whois.GetTarget());
175 whois.SendLine(RPL_WHOISSECURE, "is using a secure connection");
176 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
177 if ((!operonlyfp || whois.IsSelfWhois() || whois.GetSource()->IsOper()) && !cert->fingerprint.empty())
178 whois.SendLine(RPL_WHOISCERTFP, InspIRCd::Format("has client certificate fingerprint %s", cert->fingerprint.c_str()));
182 ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
184 if ((command == "OPER") && (validated))
186 ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
187 if (i != ServerInstance->Config->oper_blocks.end())
189 OperInfo* ifo = i->second;
190 ssl_cert* cert = cmd.CertExt.get(user);
192 if (ifo->oper_block->getBool("sslonly") && !cert)
194 user->WriteNumeric(ERR_NOOPERHOST, "This oper login requires an SSL connection.");
195 user->CommandFloodPenalty += 10000;
199 std::string fingerprint;
200 if (ifo->oper_block->readString("fingerprint", fingerprint) && (!cert || cert->GetFingerprint() != fingerprint))
202 user->WriteNumeric(ERR_NOOPERHOST, "This oper login requires a matching SSL certificate fingerprint.");
203 user->CommandFloodPenalty += 10000;
209 // Let core handle it for extra stuff
210 return MOD_RES_PASSTHRU;
213 void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
215 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
217 cmd.CertExt.set(user, cert);
220 void OnPostConnect(User* user) CXX11_OVERRIDE
222 LocalUser* const localuser = IS_LOCAL(user);
226 const SSLIOHook* const ssliohook = SSLIOHook::IsSSL(&localuser->eh);
230 ssl_cert* const cert = ssliohook->GetCertificate();
233 std::string text = "*** You are connected to ";
234 if (!ssliohook->GetServerName(text))
235 text.append(ServerInstance->Config->ServerName);
236 text.append(" using SSL cipher '");
237 ssliohook->GetCiphersuite(text);
238 text.push_back('\'');
239 if ((cert) && (!cert->GetFingerprint().empty()))
240 text.append(" and your SSL certificate fingerprint is ").append(cert->GetFingerprint());
241 user->WriteNotice(text);
246 // find an auto-oper block for this user
247 for (ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); ++i)
249 OperInfo* ifo = i->second;
250 std::string fp = ifo->oper_block->getString("fingerprint");
251 if (fp == cert->fingerprint && ifo->oper_block->getBool("autologin"))
256 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
258 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
260 if (myclass->config->getString("requiressl") == "trusted")
262 ok = (cert && cert->IsCAVerified());
263 ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "Class requires a trusted SSL cert. Client %s one.", (ok ? "has" : "does not have"));
265 else if (myclass->config->getBool("requiressl"))
268 ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "Class requires any SSL cert. Client %s one.", (ok ? "has" : "does not have"));
273 return MOD_RES_PASSTHRU;
277 MODULE_INIT(ModuleSSLInfo)