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"
26 RPL_WHOISCERTFP = 276,
32 class SSLCertExt : public ExtensionItem {
34 SSLCertExt(Module* parent)
35 : ExtensionItem("ssl_cert", ExtensionItem::EXT_USER, parent)
39 ssl_cert* get(const Extensible* item) const
41 return static_cast<ssl_cert*>(get_raw(item));
43 void set(Extensible* item, ssl_cert* value)
45 value->refcount_inc();
46 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
47 if (old && old->refcount_dec())
51 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const CXX11_OVERRIDE
53 return static_cast<ssl_cert*>(item)->GetMetaLine();
56 void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE
58 ssl_cert* cert = new ssl_cert;
61 std::stringstream s(value);
65 cert->invalid = (v.find('v') != std::string::npos);
66 cert->trusted = (v.find('T') != std::string::npos);
67 cert->revoked = (v.find('R') != std::string::npos);
68 cert->unknownsigner = (v.find('s') != std::string::npos);
69 if (v.find('E') != std::string::npos)
71 getline(s,cert->error,'\n');
75 getline(s,cert->fingerprint,' ');
76 getline(s,cert->dn,' ');
77 getline(s,cert->issuer,'\n');
81 void free(void* item) CXX11_OVERRIDE
83 ssl_cert* old = static_cast<ssl_cert*>(item);
84 if (old && old->refcount_dec())
91 class CommandSSLInfo : public Command
96 CommandSSLInfo(Module* Creator) : Command(Creator, "SSLINFO", 1), CertExt(Creator)
98 this->syntax = "<nick>";
101 CmdResult Handle(const std::vector<std::string>& parameters, User* user) CXX11_OVERRIDE
103 User* target = ServerInstance->FindNickOnly(parameters[0]);
105 if ((!target) || (target->registered != REG_ALL))
107 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
110 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
111 if (operonlyfp && !user->IsOper() && target != user)
113 user->WriteNotice("*** You cannot view SSL certificate information for other users");
116 ssl_cert* cert = CertExt.get(target);
119 user->WriteNotice("*** No SSL certificate for this user");
121 else if (cert->GetError().length())
123 user->WriteNotice("*** No SSL certificate information for this user (" + cert->GetError() + ").");
127 user->WriteNotice("*** Distinguished Name: " + cert->GetDN());
128 user->WriteNotice("*** Issuer: " + cert->GetIssuer());
129 user->WriteNotice("*** Key Fingerprint: " + cert->GetFingerprint());
135 class UserCertificateAPIImpl : public UserCertificateAPIBase
140 UserCertificateAPIImpl(Module* mod, SSLCertExt& certext)
141 : UserCertificateAPIBase(mod), ext(certext)
145 ssl_cert* GetCertificate(User* user) CXX11_OVERRIDE
147 return ext.get(user);
151 class ModuleSSLInfo : public Module, public Whois::EventListener
154 UserCertificateAPIImpl APIImpl;
158 : Whois::EventListener(this)
160 , APIImpl(this, cmd.CertExt)
164 Version GetVersion() CXX11_OVERRIDE
166 return Version("SSL Certificate Utilities", VF_VENDOR);
169 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
171 ssl_cert* cert = cmd.CertExt.get(whois.GetTarget());
174 whois.SendLine(RPL_WHOISSECURE, "is using a secure connection");
175 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
176 if ((!operonlyfp || whois.IsSelfWhois() || whois.GetSource()->IsOper()) && !cert->fingerprint.empty())
177 whois.SendLine(RPL_WHOISCERTFP, InspIRCd::Format("has client certificate fingerprint %s", cert->fingerprint.c_str()));
181 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
183 if ((command == "OPER") && (validated))
185 ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
186 if (i != ServerInstance->Config->oper_blocks.end())
188 OperInfo* ifo = i->second;
189 ssl_cert* cert = cmd.CertExt.get(user);
191 if (ifo->oper_block->getBool("sslonly") && !cert)
193 user->WriteNumeric(491, "This oper login requires an SSL connection.");
194 user->CommandFloodPenalty += 10000;
198 std::string fingerprint;
199 if (ifo->oper_block->readString("fingerprint", fingerprint) && (!cert || cert->GetFingerprint() != fingerprint))
201 user->WriteNumeric(491, "This oper login requires a matching SSL certificate fingerprint.");
202 user->CommandFloodPenalty += 10000;
208 // Let core handle it for extra stuff
209 return MOD_RES_PASSTHRU;
212 void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
214 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
216 cmd.CertExt.set(user, cert);
219 void OnPostConnect(User* user) CXX11_OVERRIDE
221 LocalUser* const localuser = IS_LOCAL(user);
225 const SSLIOHook* const ssliohook = SSLIOHook::IsSSL(&localuser->eh);
229 ssl_cert* const cert = ssliohook->GetCertificate();
232 std::string text = "*** You are connected to ";
233 if (!ssliohook->GetServerName(text))
234 text.append(ServerInstance->Config->ServerName);
235 text.append(" using SSL cipher '");
236 ssliohook->GetCiphersuite(text);
237 text.push_back('\'');
238 if ((cert) && (!cert->GetFingerprint().empty()))
239 text.append(" and your SSL certificate fingerprint is ").append(cert->GetFingerprint());
240 user->WriteNotice(text);
245 // find an auto-oper block for this user
246 for (ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); ++i)
248 OperInfo* ifo = i->second;
249 std::string fp = ifo->oper_block->getString("fingerprint");
250 if (fp == cert->fingerprint && ifo->oper_block->getBool("autologin"))
255 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
257 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
259 if (myclass->config->getString("requiressl") == "trusted")
261 ok = (cert && cert->IsCAVerified());
262 ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "Class requires a trusted SSL cert. Client %s one.", (ok ? "has" : "does not have"));
264 else if (myclass->config->getBool("requiressl"))
267 ServerInstance->Logs->Log("CONNECTCLASS", LOG_DEBUG, "Class requires any SSL cert. Client %s one.", (ok ? "has" : "does not have"));
272 return MOD_RES_PASSTHRU;
276 MODULE_INIT(ModuleSSLInfo)