1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 /* $ModDesc: Provides SSL metadata, including /WHOIS information and /SSLINFO command */
19 class SSLCertExt : public ExtensionItem {
21 SSLCertExt(Module* parent) : ExtensionItem("ssl_cert", parent) {}
22 ssl_cert* get(const Extensible* item) const
24 return static_cast<ssl_cert*>(get_raw(item));
26 void set(Extensible* item, ssl_cert* value)
28 value->refcount_inc();
29 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
30 if (old && old->refcount_dec())
34 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
36 return static_cast<ssl_cert*>(item)->GetMetaLine();
39 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
41 ssl_cert* cert = new ssl_cert;
44 std::stringstream s(value);
48 cert->invalid = (v.find('v') != std::string::npos);
49 cert->trusted = (v.find('T') != std::string::npos);
50 cert->revoked = (v.find('R') != std::string::npos);
51 cert->unknownsigner = (v.find('s') != std::string::npos);
52 if (v.find('E') != std::string::npos)
54 getline(s,cert->error,'\n');
58 getline(s,cert->fingerprint,' ');
59 getline(s,cert->dn,' ');
60 getline(s,cert->issuer,'\n');
66 ssl_cert* old = static_cast<ssl_cert*>(item);
67 if (old && old->refcount_dec())
74 class CommandSSLInfo : public Command
79 CommandSSLInfo(Module* Creator) : Command(Creator, "SSLINFO", 1), CertExt(Creator)
81 this->syntax = "<nick>";
84 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
86 User* target = ServerInstance->FindNickOnly(parameters[0]);
90 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
93 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
94 if (operonlyfp && !IS_OPER(user) && target != user)
96 user->WriteServ("NOTICE %s :*** You cannot view SSL certificate information for other users", user->nick.c_str());
99 ssl_cert* cert = CertExt.get(target);
102 user->WriteServ("NOTICE %s :*** No SSL certificate for this user", user->nick.c_str());
104 else if (cert->GetError().length())
106 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user (%s).", user->nick.c_str(), cert->GetError().c_str());
110 user->WriteServ("NOTICE %s :*** Distinguished Name: %s", user->nick.c_str(), cert->GetDN().c_str());
111 user->WriteServ("NOTICE %s :*** Issuer: %s", user->nick.c_str(), cert->GetIssuer().c_str());
112 user->WriteServ("NOTICE %s :*** Key Fingerprint: %s", user->nick.c_str(), cert->GetFingerprint().c_str());
118 class ModuleSSLInfo : public Module
123 ModuleSSLInfo() : cmd(this)
129 ServerInstance->AddCommand(&cmd);
131 ServerInstance->Extensions.Register(&cmd.CertExt);
133 Implementation eventlist[] = { I_OnWhois, I_OnPreCommand, I_OnSetConnectClass, I_OnUserConnect, I_OnPostConnect };
134 ServerInstance->Modules->Attach(eventlist, this, 5);
139 return Version("SSL Certificate Utilities", VF_VENDOR);
142 void OnWhois(User* source, User* dest)
144 ssl_cert* cert = cmd.CertExt.get(dest);
147 ServerInstance->SendWhoisLine(source, dest, 671, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
148 bool operonlyfp = ServerInstance->Config->ConfValue("sslinfo")->getBool("operonly");
149 if ((!operonlyfp || source == dest || IS_OPER(source)) && !cert->fingerprint.empty())
150 ServerInstance->SendWhoisLine(source, dest, 276, "%s %s :has client certificate fingerprint %s",
151 source->nick.c_str(), dest->nick.c_str(), cert->fingerprint.c_str());
155 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
157 std::stringstream hl(hostlist);
161 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
169 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line)
171 irc::string pcmd = command.c_str();
173 if ((pcmd == "OPER") && (validated))
175 OperIndex::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, "%s :This oper login requires an SSL connection.", user->nick.c_str());
184 user->CommandFloodPenalty += 10000;
188 std::string fingerprint;
189 if (ifo->oper_block->readString("fingerprint", fingerprint) && (!cert || cert->GetFingerprint() != fingerprint))
191 user->WriteNumeric(491, "%s :This oper login requires a matching SSL fingerprint.",user->nick.c_str());
192 user->CommandFloodPenalty += 10000;
198 // Let core handle it for extra stuff
199 return MOD_RES_PASSTHRU;
202 void OnUserConnect(LocalUser* user)
204 SocketCertificateRequest req(&user->eh, this);
207 cmd.CertExt.set(user, req.cert);
210 void OnPostConnect(User* user)
212 ssl_cert *cert = cmd.CertExt.get(user);
213 if (!cert || cert->fingerprint.empty())
215 // find an auto-oper block for this user
216 for(OperIndex::iterator i = ServerInstance->Config->oper_blocks.begin(); i != ServerInstance->Config->oper_blocks.end(); i++)
218 OperInfo* ifo = i->second;
219 std::string fp = ifo->oper_block->getString("fingerprint");
220 if (fp == cert->fingerprint && ifo->oper_block->getBool("autologin"))
225 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
227 SocketCertificateRequest req(&user->eh, this);
229 if (myclass->config->getString("requiressl") == "trusted")
231 ok = (req.cert && req.cert->IsCAVerified());
233 else if (myclass->config->getBool("requiressl"))
235 ok = (req.cert != NULL);
240 return MOD_RES_PASSTHRU;
243 void OnRequest(Request& request)
245 if (strcmp("GET_USER_CERT", request.id) == 0)
247 UserCertificateRequest& req = static_cast<UserCertificateRequest&>(request);
248 req.cert = cmd.CertExt.get(req.user);
253 MODULE_INIT(ModuleSSLInfo)