1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 /* $ModDesc: Allows for hashed oper passwords */
18 /* $ModDep: m_hash.h */
22 #include "inspircd_config.h"
30 typedef std::map<irc::string, Module*> hashymodules;
34 class cmd_mkpasswd : public command_t
37 hashymodules &hashers;
38 std::deque<std::string> &names;
40 cmd_mkpasswd (InspIRCd* Instance, Module* S, hashymodules &h, std::deque<std::string> &n)
41 : command_t(Instance,"MKPASSWD", 'o', 2), Sender(S), hashers(h), names(n)
43 this->source = "m_oper_hash.so";
44 syntax = "<hashtype> <any-text>";
47 void MakeHash(userrec* user, const char* algo, const char* stuff)
49 /* Lets see if they gave us an algorithm which has been implemented */
50 hashymodules::iterator x = hashers.find(algo);
51 if (x != hashers.end())
53 /* Yup, reset it first (Always ALWAYS do this) */
54 HashResetRequest(Sender, x->second).Send();
55 /* Now attempt to generate a hash */
56 user->WriteServ("NOTICE %s :%s hashed password for %s is %s",user->nick, algo, stuff, HashSumRequest(Sender, x->second, stuff).Send() );
60 /* I dont do flying, bob. */
61 user->WriteServ("NOTICE %s :Unknown hash type, valid hash types are: %s", user->nick, irc::stringjoiner(", ", names, 0, names.size() - 1).GetJoined().c_str() );
65 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
67 MakeHash(user, parameters[0], parameters[1]);
68 /* NOTE: Don't propogate this across the network!
69 * We dont want plaintext passes going all over the place...
70 * To make sure it goes nowhere, return CMD_FAILURE!
76 class ModuleOperHash : public Module
79 cmd_mkpasswd* mycommand;
81 hashymodules hashers; /* List of modules which implement HashRequest */
82 std::deque<std::string> names; /* Module names which implement HashRequest */
86 ModuleOperHash(InspIRCd* Me)
90 /* Read the config file first */
94 /* Find all modules which implement the interface 'HashRequest' */
95 modulelist* ml = ServerInstance->FindInterface("HashRequest");
97 /* Did we find any modules? */
100 /* Yes, enumerate them all to find out the hashing algorithm name */
101 for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
103 /* Make a request to it for its name, its implementing
104 * HashRequest so we know its safe to do this
106 std::string name = HashNameRequest(this, *m).Send();
107 /* Build a map of them */
108 hashers[name.c_str()] = *m;
109 names.push_back(name);
110 ServerInstance->Log(DEBUG, "Found HashRequest interface: '%s' -> '%08x'", name.c_str(), *m);
115 throw ModuleException("I can't find any modules loaded which implement the HashRequest interface! You probably forgot to load a hashing module such as m_md5.so or m_sha256.so.");
118 mycommand = new cmd_mkpasswd(ServerInstance, this, hashers, names);
119 ServerInstance->AddCommand(mycommand);
122 virtual ~ModuleOperHash()
126 void Implements(char* List)
128 List[I_OnRehash] = List[I_OnOperCompare] = 1;
131 virtual void OnRehash(const std::string ¶meter)
133 /* Re-read configuration file */
137 Conf = new ConfigReader(ServerInstance);
140 virtual int OnOperCompare(const std::string &data, const std::string &input, int tagnumber)
142 /* First, lets see what hash theyre using on this oper */
143 std::string hashtype = Conf->ReadValue("oper", "hash", tagnumber);
144 hashymodules::iterator x = hashers.find(hashtype.c_str());
146 /* Is this a valid hash name? (case insensitive) */
147 if (x != hashers.end())
149 /* Reset the hashing module */
150 HashResetRequest(this, x->second).Send();
151 /* Compare the hash in the config to the generated hash */
152 if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
154 /* No match, and must be hashed, forbid */
158 /* Not a hash, fall through to strcmp in core */
162 virtual Version GetVersion()
164 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
169 class ModuleOperHashFactory : public ModuleFactory
172 ModuleOperHashFactory()
176 ~ModuleOperHashFactory()
180 virtual Module * CreateModule(InspIRCd* Me)
182 return new ModuleOperHash(Me);
188 extern "C" void * init_module( void )
190 return new ModuleOperHashFactory;