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"
23 class SSLCertExt : public ExtensionItem {
25 SSLCertExt(Module* parent)
26 : ExtensionItem("ssl_cert", ExtensionItem::EXT_USER, parent)
30 ssl_cert* get(const Extensible* item) const
32 return static_cast<ssl_cert*>(get_raw(item));
34 void set(Extensible* item, ssl_cert* value)
36 value->refcount_inc();
37 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
38 if (old && old->refcount_dec())
42 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
44 return static_cast<ssl_cert*>(item)->GetMetaLine();
47 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
49 ssl_cert* cert = new ssl_cert;
52 std::stringstream s(value);
56 cert->invalid = (v.find('v') != std::string::npos);
57 cert->trusted = (v.find('T') != std::string::npos);
58 cert->revoked = (v.find('R') != std::string::npos);
59 cert->unknownsigner = (v.find('s') != std::string::npos);
60 if (v.find('E') != std::string::npos)
62 getline(s,cert->error,'\n');
66 getline(s,cert->fingerprint,' ');
67 getline(s,cert->dn,' ');
68 getline(s,cert->issuer,'\n');
74 ssl_cert* old = static_cast<ssl_cert*>(item);
75 if (old && old->refcount_dec())
82 class CommandSSLInfo : public Command
87 CommandSSLInfo(Module* Creator) : Command(Creator, "SSLINFO", 1), CertExt(Creator)
89 this->syntax = "<nick>";
92 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
94 User* target = ServerInstance->FindNickOnly(parameters[0]);
96 if ((!target) || (target->registered != REG_ALL))
98 user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nickname", parameters[0].c_str());
101 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
102 if (operonlyfp && !user->IsOper() && target != user)
104 user->WriteNotice("*** You cannot view SSL certificate information for other users");
107 ssl_cert* cert = CertExt.get(target);
110 user->WriteNotice("*** No SSL certificate for this user");
112 else if (cert->GetError().length())
114 user->WriteNotice("*** No SSL certificate information for this user (" + cert->GetError() + ").");
118 user->WriteNotice("*** Distinguished Name: " + cert->GetDN());
119 user->WriteNotice("*** Issuer: " + cert->GetIssuer());
120 user->WriteNotice("*** Key Fingerprint: " + cert->GetFingerprint());
126 class UserCertificateAPIImpl : public UserCertificateAPIBase
131 UserCertificateAPIImpl(Module* mod, SSLCertExt& certext)
132 : UserCertificateAPIBase(mod), ext(certext)
136 ssl_cert* GetCertificate(User* user) CXX11_OVERRIDE
138 return ext.get(user);
142 class ModuleSSLInfo : public Module
145 UserCertificateAPIImpl APIImpl;
149 : cmd(this), APIImpl(this, cmd.CertExt)
153 Version GetVersion() CXX11_OVERRIDE
155 return Version("SSL Certificate Utilities", VF_VENDOR);
158 void OnWhois(User* source, User* dest) CXX11_OVERRIDE
160 ssl_cert* cert = cmd.CertExt.get(dest);
163 ServerInstance->SendWhoisLine(source, dest, 671, "%s :is using a secure connection", dest->nick.c_str());
164 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
165 if ((!operonlyfp || source == dest || source->IsOper()) && !cert->fingerprint.empty())
166 ServerInstance->SendWhoisLine(source, dest, 276, "%s :has client certificate fingerprint %s",
167 dest->nick.c_str(), cert->fingerprint.c_str());
171 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
173 if ((command == "OPER") && (validated))
175 ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
176 if (i != ServerInstance->Config->oper_blocks.end())
178 OperInfo* ifo = i->second;
179 ssl_cert* cert = cmd.CertExt.get(user);
181 if (ifo->oper_block->getBool("sslonly") && !cert)
183 user->WriteNumeric(491, ":This oper login requires an SSL connection.");
184 user->CommandFloodPenalty += 10000;
188 std::string fingerprint;
189 if (ifo->oper_block->readString("fingerprint", fingerprint) && (!cert || cert->GetFingerprint() != fingerprint))
191 user->WriteNumeric(491, ":This oper login requires a matching SSL certificate fingerprint.");
192 user->CommandFloodPenalty += 10000;
198 // Let core handle it for extra stuff
199 return MOD_RES_PASSTHRU;
202 void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
204 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
206 cmd.CertExt.set(user, cert);
209 void OnPostConnect(User* user) CXX11_OVERRIDE
211 ssl_cert *cert = cmd.CertExt.get(user);
212 if (!cert || cert->fingerprint.empty())
214 // find an auto-oper block for this user
215 for (ServerConfig::OperIndex::const_iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); ++i)
217 OperInfo* ifo = i->second;
218 std::string fp = ifo->oper_block->getString("fingerprint");
219 if (fp == cert->fingerprint && ifo->oper_block->getBool("autologin"))
224 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
226 ssl_cert* cert = SSLClientCert::GetCertificate(&user->eh);
228 if (myclass->config->getString("requiressl") == "trusted")
230 ok = (cert && cert->IsCAVerified());
232 else if (myclass->config->getBool("requiressl"))
239 return MOD_RES_PASSTHRU;
243 MODULE_INIT(ModuleSSLInfo)