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 * ---------------------------------------------------
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 ssl_cert* old = static_cast<ssl_cert*>(set_raw(item, value));
32 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
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
122 ServerInstance->AddCommand(&cmd);
124 ServerInstance->Extensions.Register(&cmd.CertExt);
126 Implementation eventlist[] = { I_OnWhois, I_OnPreCommand };
127 ServerInstance->Modules->Attach(eventlist, this, 2);
128 ServerInstance->Modules->PublishInterface("SSLCertInfo", this);
133 ServerInstance->Modules->UnpublishInterface("SSLCertInfo", this);
138 return Version("SSL Certificate Utilities", VF_VENDOR);
141 void OnWhois(User* source, User* dest)
143 if (cmd.CertExt.get(dest))
145 ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
149 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
151 std::stringstream hl(hostlist);
155 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
163 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
165 irc::string pcmd = command.c_str();
167 if ((pcmd == "OPER") && (validated))
170 char TheHost[MAXBUF];
172 std::string LoginName;
173 std::string Password;
174 std::string OperType;
175 std::string HostName;
176 std::string HashType;
177 std::string FingerPrint;
179 ssl_cert* cert = cmd.CertExt.get(user);
181 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
182 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
184 for (int i = 0; i < cf.Enumerate("oper"); i++)
186 LoginName = cf.ReadValue("oper", "name", i);
187 Password = cf.ReadValue("oper", "password", i);
188 OperType = cf.ReadValue("oper", "type", i);
189 HostName = cf.ReadValue("oper", "host", i);
190 HashType = cf.ReadValue("oper", "hash", i);
191 FingerPrint = cf.ReadValue("oper", "fingerprint", i);
192 SSLOnly = cf.ReadFlag("oper", "sslonly", i);
194 if (FingerPrint.empty() && !SSLOnly)
197 if (LoginName != parameters[0])
200 if (!OneOfMatches(TheHost, TheIP, HostName.c_str()))
203 if (Password.length() && ServerInstance->PassCompare(user, Password.c_str(),parameters[1].c_str(), HashType.c_str()))
206 if (SSLOnly && !cert)
208 user->WriteNumeric(491, "%s :This oper login name requires an SSL connection.", user->nick.c_str());
213 * No cert found or the fingerprint doesn't match
215 if ((!cert) || (cert->GetFingerprint() != FingerPrint))
217 user->WriteNumeric(491, "%s :This oper login name requires a matching key fingerprint.",user->nick.c_str());
218 ServerInstance->SNO->WriteToSnoMask('o',"'%s' cannot oper, does not match fingerprint", user->nick.c_str());
219 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());
225 // Let core handle it for extra stuff
226 return MOD_RES_PASSTHRU;
229 void OnRequest(Request& request)
231 if (strcmp("GET_CERT", request.id) == 0)
233 SSLCertificateRequest& req = static_cast<SSLCertificateRequest&>(request);
234 req.cert = cmd.CertExt.get(req.item);
236 else if (strcmp("SET_CERT", request.id) == 0)
238 SSLCertSubmission& req = static_cast<SSLCertSubmission&>(request);
239 cmd.CertExt.set(req.item, req.cert);
244 MODULE_INIT(ModuleSSLInfo)