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/>.
23 /* $ModDesc: Provides SSL metadata, including /WHOIS information and /SSLINFO command */
25 class SSLCertExt : public ExtensionItem {
27 SSLCertExt(Module* parent) : ExtensionItem("ssl_cert", parent) {}
28 ssl_cert* get(const Extensible* item) const
30 return static_cast<ssl_cert*>(get_raw(item));
32 void set(Extensible* item, ssl_cert* value)
34 value->refcount_inc();
35 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
36 if (old && old->refcount_dec())
40 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
42 return static_cast<ssl_cert*>(item)->GetMetaLine();
45 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
47 ssl_cert* cert = new ssl_cert;
50 std::stringstream s(value);
54 cert->invalid = (v.find('v') != std::string::npos);
55 cert->trusted = (v.find('T') != std::string::npos);
56 cert->revoked = (v.find('R') != std::string::npos);
57 cert->unknownsigner = (v.find('s') != std::string::npos);
58 if (v.find('E') != std::string::npos)
60 getline(s,cert->error,'\n');
64 getline(s,cert->fingerprint,' ');
65 getline(s,cert->dn,' ');
66 getline(s,cert->issuer,'\n');
72 ssl_cert* old = static_cast<ssl_cert*>(item);
73 if (old && old->refcount_dec())
80 class CommandSSLInfo : public Command
85 CommandSSLInfo(Module* Creator) : Command(Creator, "SSLINFO", 1), CertExt(Creator)
87 this->syntax = "<nick>";
90 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
92 User* target = ServerInstance->FindNickOnly(parameters[0]);
96 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
99 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
100 if (operonlyfp && !IS_OPER(user) && target != user)
102 user->WriteServ("NOTICE %s :*** You cannot view SSL certificate information for other users", user->nick.c_str());
105 ssl_cert* cert = CertExt.get(target);
108 user->WriteServ("NOTICE %s :*** No SSL certificate for this user", user->nick.c_str());
110 else if (cert->GetError().length())
112 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user (%s).", user->nick.c_str(), cert->GetError().c_str());
116 user->WriteServ("NOTICE %s :*** Distinguished Name: %s", user->nick.c_str(), cert->GetDN().c_str());
117 user->WriteServ("NOTICE %s :*** Issuer: %s", user->nick.c_str(), cert->GetIssuer().c_str());
118 user->WriteServ("NOTICE %s :*** Key Fingerprint: %s", user->nick.c_str(), cert->GetFingerprint().c_str());
124 class ModuleSSLInfo : public Module
129 ModuleSSLInfo() : cmd(this)
135 ServerInstance->AddCommand(&cmd);
137 ServerInstance->Extensions.Register(&cmd.CertExt);
139 Implementation eventlist[] = { I_OnWhois, I_OnPreCommand, I_OnSetConnectClass, I_OnUserConnect, I_OnPostConnect };
140 ServerInstance->Modules->Attach(eventlist, this, 5);
145 return Version("SSL Certificate Utilities", VF_VENDOR);
148 void OnWhois(User* source, User* dest)
150 ssl_cert* cert = cmd.CertExt.get(dest);
153 ServerInstance->SendWhoisLine(source, dest, 671, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
154 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
155 if ((!operonlyfp || source == dest || IS_OPER(source)) && !cert->fingerprint.empty())
156 ServerInstance->SendWhoisLine(source, dest, 276, "%s %s :has client certificate fingerprint %s",
157 source->nick.c_str(), dest->nick.c_str(), cert->fingerprint.c_str());
161 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
163 std::stringstream hl(hostlist);
167 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
175 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line)
177 irc::string pcmd = command.c_str();
179 if ((pcmd == "OPER") && (validated))
181 OperIndex::iterator i = ServerInstance->Config->oper_blocks.find(parameters[0]);
182 if (i != ServerInstance->Config->oper_blocks.end())
184 OperInfo* ifo = i->second;
185 ssl_cert* cert = cmd.CertExt.get(user);
187 if (ifo->oper_block->getBool("sslonly") && !cert)
189 user->WriteNumeric(491, "%s :This oper login requires an SSL connection.", user->nick.c_str());
190 user->CommandFloodPenalty += 10000;
194 std::string fingerprint;
195 if (ifo->oper_block->readString("fingerprint", fingerprint) && (!cert || cert->GetFingerprint() != fingerprint))
197 user->WriteNumeric(491, "%s :This oper login requires a matching SSL fingerprint.",user->nick.c_str());
198 user->CommandFloodPenalty += 10000;
204 // Let core handle it for extra stuff
205 return MOD_RES_PASSTHRU;
208 void OnUserConnect(LocalUser* user)
210 SocketCertificateRequest req(&user->eh, this);
213 cmd.CertExt.set(user, req.cert);
216 void OnPostConnect(User* user)
218 ssl_cert *cert = cmd.CertExt.get(user);
219 if (!cert || cert->fingerprint.empty())
221 // find an auto-oper block for this user
222 for(OperIndex::iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); i++)
224 OperInfo* ifo = i->second;
225 std::string fp = ifo->oper_block->getString("fingerprint");
226 if (fp == cert->fingerprint && ifo->oper_block->getBool("autologin"))
231 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
233 SocketCertificateRequest req(&user->eh, this);
235 if (myclass->config->getString("requiressl") == "trusted")
237 ok = (req.cert && req.cert->IsCAVerified());
239 else if (myclass->config->getBool("requiressl"))
241 ok = (req.cert != NULL);
246 return MOD_RES_PASSTHRU;
249 void OnRequest(Request& request)
251 if (strcmp("GET_USER_CERT", request.id) == 0)
253 UserCertificateRequest& req = static_cast<UserCertificateRequest&>(request);
254 req.cert = cmd.CertExt.get(req.user);
259 MODULE_INIT(ModuleSSLInfo)