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->FindNick(parameters[0]);
90 ssl_cert* cert = CertExt.get(target);
93 if (cert->GetError().length())
95 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user (%s).", user->nick.c_str(), cert->GetError().c_str());
99 user->WriteServ("NOTICE %s :*** Distinguised Name: %s", user->nick.c_str(), cert->GetDN().c_str());
100 user->WriteServ("NOTICE %s :*** Issuer: %s", user->nick.c_str(), cert->GetIssuer().c_str());
101 user->WriteServ("NOTICE %s :*** Key Fingerprint: %s", user->nick.c_str(), cert->GetFingerprint().c_str());
107 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user.", user->nick.c_str());
112 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
118 class ModuleSSLInfo : public Module
126 ServerInstance->AddCommand(&cmd);
128 ServerInstance->Extensions.Register(&cmd.CertExt);
130 Implementation eventlist[] = { I_OnWhois, I_OnPreCommand };
131 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))
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 void OnRequest(Request& request)
229 if (strcmp("GET_USER_CERT", request.id) == 0)
231 UserCertificateRequest& req = static_cast<UserCertificateRequest&>(request);
232 req.cert = cmd.CertExt.get(req.user);
234 else if (strcmp("SET_CERT", request.id) == 0)
236 SSLCertSubmission& req = static_cast<SSLCertSubmission&>(request);
237 cmd.CertExt.set(req.item, req.cert);
242 MODULE_INIT(ModuleSSLInfo)