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"
32 class cmd_mkpasswd : public command_t
35 std::map<irc::string, Module*> &hashers;
36 std::deque<std::string> &names;
38 cmd_mkpasswd (InspIRCd* Instance, Module* S, std::map<irc::string, Module*> &h, std::deque<std::string> &n)
39 : command_t(Instance,"MKPASSWD", 'o', 2), Sender(S), hashers(h), names(n)
41 this->source = "m_oper_hash.so";
42 syntax = "<hashtype> <any-text>";
45 void MakeHash(userrec* user, const char* algo, const char* stuff)
47 /* Lets see if they gave us an algorithm which has been implemented */
48 std::map<irc::string, Module*>::iterator x = hashers.find(algo);
49 if (x != hashers.end())
51 /* Yup, reset it first (Always ALWAYS do this) */
52 HashResetRequest(Sender, x->second).Send();
53 /* Now attempt to generate a hash */
54 user->WriteServ("NOTICE %s :%s hashed password for %s is %s",user->nick, algo, stuff, HashSumRequest(Sender, x->second, stuff).Send() );
58 /* I dont do flying, bob. */
59 user->WriteServ("NOTICE %s :Unknown hash type, valid hash types are: %s", user->nick, irc::stringjoiner(", ", names, 0, names.size() - 1).GetJoined().c_str() );
63 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
65 MakeHash(user, parameters[0], parameters[1]);
66 /* NOTE: Don't propogate this across the network!
67 * We dont want plaintext passes going all over the place...
68 * To make sure it goes nowhere, return CMD_FAILURE!
74 class ModuleOperHash : public Module
77 cmd_mkpasswd* mycommand;
79 std::map<irc::string, Module*> hashers; /* List of modules which implement HashRequest */
80 std::deque<std::string> names; /* Module names which implement HashRequest */
84 ModuleOperHash(InspIRCd* Me)
88 /* Read the config file first */
92 /* Find all modules which implement the interface 'HashRequest' */
93 modulelist* ml = ServerInstance->FindInterface("HashRequest");
95 /* Did we find any modules? */
98 /* Yes, enumerate them all to find out the hashing algorithm name */
99 for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
101 /* Make a request to it for its name, its implementing
102 * HashRequest so we know its safe to do this
104 std::string name = HashNameRequest(this, *m).Send();
105 /* Build a map of them */
106 hashers[name.c_str()] = *m;
107 names.push_back(name);
108 ServerInstance->Log(DEBUG, "Found HashRequest interface: '%s' -> '%08x'", name.c_str(), *m);
112 mycommand = new cmd_mkpasswd(ServerInstance, this, hashers, names);
113 ServerInstance->AddCommand(mycommand);
116 virtual ~ModuleOperHash()
120 void Implements(char* List)
122 List[I_OnRehash] = List[I_OnOperCompare] = 1;
125 virtual void OnRehash(const std::string ¶meter)
127 /* Re-read configuration file */
131 Conf = new ConfigReader(ServerInstance);
134 virtual int OnOperCompare(const std::string &data, const std::string &input, int tagnumber)
136 /* First, lets see what hash theyre using on this oper */
137 std::string hashtype = Conf->ReadValue("oper", "hash", tagnumber);
138 std::map<irc::string, Module*>::iterator x = hashers.find(hashtype.c_str());
140 /* Is this a valid hash name? (case insensitive) */
141 if (x != hashers.end())
143 /* Reset the hashing module */
144 HashResetRequest(this, x->second).Send();
145 /* Compare the hash in the config to the generated hash */
146 if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
148 /* No match, and must be hashed, forbid */
152 /* Not a hash, fall through to strcmp in core */
156 virtual Version GetVersion()
158 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
163 class ModuleOperHashFactory : public ModuleFactory
166 ModuleOperHashFactory()
170 ~ModuleOperHashFactory()
174 virtual Module * CreateModule(InspIRCd* Me)
176 return new ModuleOperHash(Me);
182 extern "C" void * init_module( void )
184 return new ModuleOperHashFactory;