1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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 * ---------------------------------------------------
15 #include "transport.h"
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)
24 return static_cast<ssl_cert*>(get_raw(item));
26 void set(Extensible* item, ssl_cert* value)
28 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
32 std::string serialize(SerializeFormat format, const Extensible* container, void* item)
34 return static_cast<ssl_cert*>(item)->GetMetaLine();
37 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
39 ssl_cert* cert = new ssl_cert;
42 std::stringstream s(value);
46 cert->invalid = (v.find('v') != std::string::npos);
47 cert->trusted = (v.find('T') != std::string::npos);
48 cert->revoked = (v.find('R') != std::string::npos);
49 cert->unknownsigner = (v.find('s') != std::string::npos);
50 if (v.find('E') != std::string::npos)
52 getline(s,cert->error,'\n');
56 getline(s,cert->fingerprint,' ');
57 getline(s,cert->dn,' ');
58 getline(s,cert->issuer,'\n');
64 delete static_cast<ssl_cert*>(item);
70 class CommandSSLInfo : public Command
75 CommandSSLInfo(Module* Creator) : Command(Creator, "SSLINFO", 1), CertExt(Creator)
77 this->syntax = "<nick>";
80 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
82 User* target = ServerInstance->FindNick(parameters[0]);
86 ssl_cert* cert = CertExt.get(target);
89 if (cert->GetError().length())
91 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user (%s).", user->nick.c_str(), cert->GetError().c_str());
95 user->WriteServ("NOTICE %s :*** Distinguised Name: %s", user->nick.c_str(), cert->GetDN().c_str());
96 user->WriteServ("NOTICE %s :*** Issuer: %s", user->nick.c_str(), cert->GetIssuer().c_str());
97 user->WriteServ("NOTICE %s :*** Key Fingerprint: %s", user->nick.c_str(), cert->GetFingerprint().c_str());
103 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user.", user->nick.c_str());
108 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
114 class ModuleSSLInfo : public Module
119 ModuleSSLInfo(InspIRCd* Me)
120 : Module(Me), cmd(this)
122 ServerInstance->AddCommand(&cmd);
124 Extensible::Register(&cmd.CertExt);
126 Implementation eventlist[] = { I_OnWhois, I_OnPreCommand };
127 ServerInstance->Modules->Attach(eventlist, this, 2);
136 return Version("SSL Certificate Utilities", VF_VENDOR);
139 void OnWhois(User* source, User* dest)
141 if (cmd.CertExt.get(dest))
143 ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
147 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
149 std::stringstream hl(hostlist);
153 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
161 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
163 irc::string pcmd = command.c_str();
165 if ((pcmd == "OPER") && (validated))
167 ConfigReader cf(ServerInstance);
168 char TheHost[MAXBUF];
170 std::string LoginName;
171 std::string Password;
172 std::string OperType;
173 std::string HostName;
174 std::string HashType;
175 std::string FingerPrint;
177 ssl_cert* cert = cmd.CertExt.get(user);
179 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
180 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
182 for (int i = 0; i < cf.Enumerate("oper"); i++)
184 LoginName = cf.ReadValue("oper", "name", i);
185 Password = cf.ReadValue("oper", "password", i);
186 OperType = cf.ReadValue("oper", "type", i);
187 HostName = cf.ReadValue("oper", "host", i);
188 HashType = cf.ReadValue("oper", "hash", i);
189 FingerPrint = cf.ReadValue("oper", "fingerprint", i);
190 SSLOnly = cf.ReadFlag("oper", "sslonly", i);
192 if (FingerPrint.empty() && !SSLOnly)
195 if (LoginName != parameters[0])
198 if (!OneOfMatches(TheHost, TheIP, HostName.c_str()))
201 if (Password.length() && ServerInstance->PassCompare(user, Password.c_str(),parameters[1].c_str(), HashType.c_str()))
204 if (SSLOnly && !cert)
206 user->WriteNumeric(491, "%s :This oper login name requires an SSL connection.", user->nick.c_str());
211 * No cert found or the fingerprint doesn't match
213 if ((!cert) || (cert->GetFingerprint() != FingerPrint))
215 user->WriteNumeric(491, "%s :This oper login name requires a matching key fingerprint.",user->nick.c_str());
216 ServerInstance->SNO->WriteToSnoMask('o',"'%s' cannot oper, does not match fingerprint", user->nick.c_str());
217 ServerInstance->Logs->Log("m_ssl_oper_cert",DEFAULT,"OPER: Failed oper attempt by %s!%s@%s: credentials valid, but wrong fingerprint.", user->nick.c_str(), user->ident.c_str(), user->host.c_str());
223 // Let core handle it for extra stuff
224 return MOD_RES_PASSTHRU;
227 const char* OnRequest(Request* request)
229 if (strcmp("GET_CERT", request->GetId()) == 0)
231 BufferedSocketCertificateRequest* req = static_cast<BufferedSocketCertificateRequest*>(request);
232 req->cert = cmd.CertExt.get(req->item);
234 else if (strcmp("SET_CERT", request->GetId()) == 0)
236 BufferedSocketFingerprintSubmission* req = static_cast<BufferedSocketFingerprintSubmission*>(request);
237 cmd.CertExt.set(req->item, req->cert);
243 MODULE_INIT(ModuleSSLInfo)