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
66 CommandSSLInfo* newcommand;
68 ModuleSSLInfo(InspIRCd* Me)
71 newcommand = new CommandSSLInfo(ServerInstance);
72 ServerInstance->AddCommand(newcommand);
74 Implementation eventlist[] = { I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnWhois, I_OnPreCommand };
75 ServerInstance->Modules->Attach(eventlist, this, 4);
79 virtual ~ModuleSSLInfo()
83 virtual Version GetVersion()
85 return Version("$Id$", VF_VENDOR, API_VERSION);
88 virtual void OnWhois(User* source, User* dest)
90 if(dest->GetExt("ssl"))
92 ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
96 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
98 // check if the linking module wants to know about OUR metadata
101 // check if this user has an ssl field to send
102 if (!user->GetExt(extname))
105 // call this function in the linking module, let it format the data how it
106 // sees fit, and send it on its way. We dont need or want to know how.
107 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, displayable ? "Enabled" : "ON");
109 else if (extname == "ssl_cert")
112 if (!user->GetExt("ssl_cert", cert))
115 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, cert->GetMetaLine().c_str());
119 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
121 // check if its our metadata key, and its associated with a user
122 if ((target_type == TYPE_USER) && (extname == "ssl"))
124 User* dest = static_cast<User*>(target);
125 // if they dont already have an ssl flag, accept the remote server's
126 if (!dest->GetExt(extname))
128 dest->Extend(extname);
131 else if ((target_type == TYPE_USER) && (extname == "ssl_cert"))
133 User* dest = static_cast<User*>(target);
134 if (dest->GetExt(extname))
137 ssl_cert* cert = new ssl_cert;
138 dest->Extend(extname, cert);
140 std::stringstream s(extdata);
144 cert->invalid = (v.find('v') != std::string::npos);
145 cert->trusted = (v.find('T') != std::string::npos);
146 cert->revoked = (v.find('R') != std::string::npos);
147 cert->unknownsigner = (v.find('s') != std::string::npos);
148 if (v.find('E') != std::string::npos)
150 getline(s,cert->error,'\n');
154 getline(s,cert->fingerprint,' ');
155 getline(s,cert->dn,' ');
156 getline(s,cert->issuer,'\n');
161 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
163 std::stringstream hl(hostlist);
167 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
175 virtual int OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
177 irc::string cmd = command.c_str();
179 if ((cmd == "OPER") && (validated))
181 ConfigReader cf(ServerInstance);
182 char TheHost[MAXBUF];
184 std::string LoginName;
185 std::string Password;
186 std::string OperType;
187 std::string HostName;
188 std::string HashType;
189 std::string FingerPrint;
191 ssl_cert* cert = NULL;
193 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
194 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
196 user->GetExt("ssl_cert",cert);
198 for (int i = 0; i < cf.Enumerate("oper"); i++)
200 LoginName = cf.ReadValue("oper", "name", i);
201 Password = cf.ReadValue("oper", "password", i);
202 OperType = cf.ReadValue("oper", "type", i);
203 HostName = cf.ReadValue("oper", "host", i);
204 HashType = cf.ReadValue("oper", "hash", i);
205 FingerPrint = cf.ReadValue("oper", "fingerprint", i);
206 SSLOnly = cf.ReadFlag("oper", "sslonly", i);
208 if (FingerPrint.empty() && !SSLOnly)
211 if (LoginName != parameters[0])
214 if (!OneOfMatches(TheHost, TheIP, HostName.c_str()))
217 if (Password.length() && ServerInstance->PassCompare(user, Password.c_str(),parameters[1].c_str(), HashType.c_str()))
220 if (SSLOnly && !user->GetExt("ssl"))
222 user->WriteNumeric(491, "%s :This oper login name requires an SSL connection.", user->nick.c_str());
227 * No cert found or the fingerprint doesn't match
229 if ((!cert) || (cert->GetFingerprint() != FingerPrint))
231 user->WriteNumeric(491, "%s :This oper login name requires a matching key fingerprint.",user->nick.c_str());
232 ServerInstance->SNO->WriteToSnoMask('o',"'%s' cannot oper, does not match fingerprint", user->nick.c_str());
233 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());
239 // Let core handle it for extra stuff
246 MODULE_INIT(ModuleSSLInfo)