1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* $ModDesc: Allows for MD5 encrypted oper passwords */
15 /* $ModDep: transport.h */
18 #include "inspircd_config.h"
22 #include "transport.h"
24 /** Handle /FINGERPRINT
26 class cmd_fingerprint : public Command
29 cmd_fingerprint (InspIRCd* Instance) : Command(Instance,"FINGERPRINT", 0, 1)
31 this->source = "m_ssl_oper_cert.so";
32 syntax = "<nickname>";
35 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
37 User* target = ServerInstance->FindNick(parameters[0]);
41 if (target->GetExt("ssl_cert",cert))
43 if (cert->GetFingerprint().length())
45 user->WriteServ("NOTICE %s :Certificate fingerprint for %s is %s",user->nick.c_str(),target->nick.c_str(),cert->GetFingerprint().c_str());
50 user->WriteServ("NOTICE %s :Certificate fingerprint for %s does not exist!", user->nick.c_str(),target->nick.c_str());
56 user->WriteServ("NOTICE %s :Certificate fingerprint for %s does not exist!", user->nick.c_str(), target->nick.c_str());
62 user->WriteNumeric(401, "%s %s :No such nickname", user->nick.c_str(), parameters[0].c_str());
70 class ModuleOperSSLCert : public Module
74 cmd_fingerprint* mycommand;
78 ModuleOperSSLCert(InspIRCd* Me)
81 mycommand = new cmd_fingerprint(ServerInstance);
82 ServerInstance->AddCommand(mycommand);
83 cf = new ConfigReader(ServerInstance);
84 Implementation eventlist[] = { I_OnPreCommand, I_OnRehash };
85 ServerInstance->Modules->Attach(eventlist, this, 2);
88 virtual ~ModuleOperSSLCert()
94 virtual void OnRehash(User* user, const std::string ¶meter)
97 cf = new ConfigReader(ServerInstance);
100 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
102 std::stringstream hl(hostlist);
106 if (InspIRCd::Match(host, xhost) || InspIRCd::MatchCIDR(ip, xhost))
114 virtual int OnPreCommand(std::string &command, std::vector<std::string> ¶meters, User *user, bool validated, const std::string &original_line)
116 irc::string cmd = command.c_str();
118 if ((cmd == "OPER") && (validated))
120 char TheHost[MAXBUF];
122 std::string LoginName;
123 std::string Password;
124 std::string OperType;
125 std::string HostName;
126 std::string HashType;
127 std::string FingerPrint;
131 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(),user->host.c_str());
132 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(),user->GetIPString());
134 HasCert = user->GetExt("ssl_cert",cert);
136 for (int i = 0; i < cf->Enumerate("oper"); i++)
138 LoginName = cf->ReadValue("oper", "name", i);
139 Password = cf->ReadValue("oper", "password", i);
140 OperType = cf->ReadValue("oper", "type", i);
141 HostName = cf->ReadValue("oper", "host", i);
142 HashType = cf->ReadValue("oper", "hash", i);
143 FingerPrint = cf->ReadValue("oper", "fingerprint", i);
144 SSLOnly = cf->ReadFlag("oper", "sslonly", i);
146 if (FingerPrint.empty() && !SSLOnly)
149 if (LoginName != parameters[0])
152 if (!OneOfMatches(TheHost, TheIP, HostName.c_str()))
155 if (Password.length() && !ServerInstance->PassCompare(user, Password.c_str(),parameters[1].c_str(), HashType.c_str()))
158 if (SSLOnly && !user->GetExt("ssl", dummy))
160 user->WriteNumeric(491, "%s :This oper login name requires an SSL connection.", user->nick.c_str());
165 * No cert found or the fingerprint doesn't match
167 if ((!cert) || (cert->GetFingerprint() != FingerPrint))
169 user->WriteNumeric(491, "%s :This oper login name requires a matching key fingerprint.",user->nick.c_str());
170 ServerInstance->SNO->WriteToSnoMask('o',"'%s' cannot oper, does not match fingerprint", user->nick.c_str());
171 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());
177 // Let core handle it for extra stuff
181 virtual Version GetVersion()
183 return Version("$Id$", VF_VENDOR, API_VERSION);
187 MODULE_INIT(ModuleOperSSLCert)