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, Module* Creator) : Command(Instance, Creator, "SSLINFO", 0, 1)
26 this->syntax = "<nick>";
29 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
31 User* target = ServerInstance->FindNick(parameters[0]);
36 if (target->GetExt("ssl_cert", cert))
38 if (cert->GetError().length())
40 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user (%s).", user->nick.c_str(), cert->GetError().c_str());
44 user->WriteServ("NOTICE %s :*** Distinguised Name: %s", user->nick.c_str(), cert->GetDN().c_str());
45 user->WriteServ("NOTICE %s :*** Issuer: %s", user->nick.c_str(), cert->GetIssuer().c_str());
46 user->WriteServ("NOTICE %s :*** Key Fingerprint: %s", user->nick.c_str(), cert->GetFingerprint().c_str());
52 user->WriteServ("NOTICE %s :*** No SSL certificate information for this user.", user->nick.c_str());
57 user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
63 class ModuleSSLInfo : public Module
67 ModuleSSLInfo(InspIRCd* Me)
68 : Module(Me), cmd(Me, this)
70 ServerInstance->AddCommand(&cmd);
72 Implementation eventlist[] = { I_OnSyncUser, I_OnDecodeMetaData, I_OnWhois, I_OnPreCommand };
73 ServerInstance->Modules->Attach(eventlist, this, 4);
77 virtual ~ModuleSSLInfo()
81 virtual Version GetVersion()
83 return Version("$Id$", VF_VENDOR, API_VERSION);
86 virtual void OnWhois(User* source, User* dest)
88 if(dest->GetExt("ssl"))
90 ServerInstance->SendWhoisLine(source, dest, 320, "%s %s :is using a secure connection", source->nick.c_str(), dest->nick.c_str());
94 virtual void OnSyncUser(User* user, Module* proto, void* opaque)
96 if (user->GetExt("ssl"))
97 proto->ProtoSendMetaData(opaque, user, "ssl", "ON");
100 if (user->GetExt("ssl_cert", cert))
101 proto->ProtoSendMetaData(opaque, user, "ssl_cert", cert->GetMetaLine().c_str());
104 virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
106 User* dest = dynamic_cast<User*>(target);
107 // check if its our metadata key, and its associated with a user
108 if (dest && (extname == "ssl"))
110 // if they dont already have an ssl flag, accept the remote server's
111 if (!dest->GetExt(extname))
113 dest->Extend(extname);
116 else if (dest && (extname == "ssl_cert"))
118 if (dest->GetExt(extname))
121 ssl_cert* cert = new ssl_cert;
122 dest->Extend(extname, cert);
124 std::stringstream s(extdata);
128 cert->invalid = (v.find('v') != std::string::npos);
129 cert->trusted = (v.find('T') != std::string::npos);
130 cert->revoked = (v.find('R') != std::string::npos);
131 cert->unknownsigner = (v.find('s') != std::string::npos);
132 if (v.find('E') != std::string::npos)
134 getline(s,cert->error,'\n');
138 getline(s,cert->fingerprint,' ');
139 getline(s,cert->dn,' ');
140 getline(s,cert->issuer,'\n');
145 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
147 std::stringstream hl(hostlist);
151 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
159 virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
161 irc::string pcmd = command.c_str();
163 if ((pcmd == "OPER") && (validated))
165 ConfigReader cf(ServerInstance);
166 char TheHost[MAXBUF];
168 std::string LoginName;
169 std::string Password;
170 std::string OperType;
171 std::string HostName;
172 std::string HashType;
173 std::string FingerPrint;
175 ssl_cert* cert = NULL;
177 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
178 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
180 user->GetExt("ssl_cert",cert);
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 && !user->GetExt("ssl"))
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;
230 MODULE_INIT(ModuleSSLInfo)