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 */
21 class CommandSSLInfo : public Command
24 CommandSSLInfo (InspIRCd* Instance) : Command(Instance,"SSLINFO", 0, 1)
26 this->source = "m_sslinfo.so";
27 this->syntax = "<nick>";
30 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
32 User* target = ServerInstance->FindNick(parameters[0]);
37 if (target->GetExt("ssl_cert", cert))
39 if (cert->GetError().length())
41 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user (%s).", user->nick.c_str(), cert->GetError().c_str());
45 user->WriteServ("NOTICE %s :*** Distinguised Name: %s", user->nick.c_str(), cert->GetDN().c_str());
46 user->WriteServ("NOTICE %s :*** Issuer: %s", user->nick.c_str(), cert->GetIssuer().c_str());
47 user->WriteServ("NOTICE %s :*** Key Fingerprint: %s", user->nick.c_str(), cert->GetFingerprint().c_str());
53 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user.", user->nick.c_str());
58 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
64 class ModuleSSLInfo : public Module
68 ModuleSSLInfo(InspIRCd* Me)
71 ServerInstance->AddCommand(&cmd);
73 Implementation eventlist[] = { I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnWhois, I_OnPreCommand };
74 ServerInstance->Modules->Attach(eventlist, this, 4);
78 virtual ~ModuleSSLInfo()
82 virtual Version GetVersion()
84 return Version("$Id$", VF_VENDOR, API_VERSION);
87 virtual void OnWhois(User* source, User* dest)
89 if(dest->GetExt("ssl"))
91 ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
95 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
97 // check if the linking module wants to know about OUR metadata
100 // check if this user has an ssl field to send
101 if (!user->GetExt(extname))
104 // call this function in the linking module, let it format the data how it
105 // sees fit, and send it on its way. We dont need or want to know how.
106 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, displayable ? "Enabled" : "ON");
108 else if (extname == "ssl_cert")
111 if (!user->GetExt("ssl_cert", cert))
114 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, cert->GetMetaLine().c_str());
118 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
120 // check if its our metadata key, and its associated with a user
121 if ((target_type == TYPE_USER) && (extname == "ssl"))
123 User* dest = static_cast<User*>(target);
124 // if they dont already have an ssl flag, accept the remote server's
125 if (!dest->GetExt(extname))
127 dest->Extend(extname);
130 else if ((target_type == TYPE_USER) && (extname == "ssl_cert"))
132 User* dest = static_cast<User*>(target);
133 if (dest->GetExt(extname))
136 ssl_cert* cert = new ssl_cert;
137 dest->Extend(extname, cert);
139 std::stringstream s(extdata);
143 cert->invalid = (v.find('v') != std::string::npos);
144 cert->trusted = (v.find('T') != std::string::npos);
145 cert->revoked = (v.find('R') != std::string::npos);
146 cert->unknownsigner = (v.find('s') != std::string::npos);
147 if (v.find('E') != std::string::npos)
149 getline(s,cert->error,'\n');
153 getline(s,cert->fingerprint,' ');
154 getline(s,cert->dn,' ');
155 getline(s,cert->issuer,'\n');
160 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
162 std::stringstream hl(hostlist);
166 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
174 virtual int OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
176 irc::string cmd = command.c_str();
178 if ((cmd == "OPER") && (validated))
180 ConfigReader cf(ServerInstance);
181 char TheHost[MAXBUF];
183 std::string LoginName;
184 std::string Password;
185 std::string OperType;
186 std::string HostName;
187 std::string HashType;
188 std::string FingerPrint;
190 ssl_cert* cert = NULL;
192 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
193 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
195 user->GetExt("ssl_cert",cert);
197 for (int i = 0; i < cf.Enumerate("oper"); i++)
199 LoginName = cf.ReadValue("oper", "name", i);
200 Password = cf.ReadValue("oper", "password", i);
201 OperType = cf.ReadValue("oper", "type", i);
202 HostName = cf.ReadValue("oper", "host", i);
203 HashType = cf.ReadValue("oper", "hash", i);
204 FingerPrint = cf.ReadValue("oper", "fingerprint", i);
205 SSLOnly = cf.ReadFlag("oper", "sslonly", i);
207 if (FingerPrint.empty() && !SSLOnly)
210 if (LoginName != parameters[0])
213 if (!OneOfMatches(TheHost, TheIP, HostName.c_str()))
216 if (Password.length() && ServerInstance->PassCompare(user, Password.c_str(),parameters[1].c_str(), HashType.c_str()))
219 if (SSLOnly && !user->GetExt("ssl"))
221 user->WriteNumeric(491, "%s :This oper login name requires an SSL connection.", user->nick.c_str());
226 * No cert found or the fingerprint doesn't match
228 if ((!cert) || (cert->GetFingerprint() != FingerPrint))
230 user->WriteNumeric(491, "%s :This oper login name requires a matching key fingerprint.",user->nick.c_str());
231 ServerInstance->SNO->WriteToSnoMask('o',"'%s' cannot oper, does not match fingerprint", user->nick.c_str());
232 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());
238 // Let core handle it for extra stuff
245 MODULE_INIT(ModuleSSLInfo)