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 * ---------------------------------------------------
14 /* $ModDesc: Allows for hashed oper passwords */
15 /* $ModDep: m_hash.h */
20 typedef std::map<irc::string, Module*> hashymodules;
24 class CommandMkpasswd : public Command
26 hashymodules &hashers;
27 std::deque<std::string> &names;
29 CommandMkpasswd(Module* Creator, hashymodules &h, std::deque<std::string> &n) : Command(Creator, "MKPASSWD", 2), hashers(h), names(n)
31 syntax = "<hashtype> <any-text>";
34 void MakeHash(User* user, const char* algo, const char* stuff)
36 /* Lets see if they gave us an algorithm which has been implemented */
37 hashymodules::iterator x = hashers.find(algo);
38 if (x != hashers.end())
40 /* Yup, reset it first (Always ALWAYS do this) */
41 HashResetRequest(creator, x->second).Send();
42 /* Now attempt to generate a hash */
43 user->WriteServ("NOTICE %s :%s hashed password for %s is %s",user->nick.c_str(), algo, stuff, HashSumRequest(creator, x->second, stuff).Send() );
45 else if (names.empty())
47 /* same idea as bug #569 */
48 user->WriteServ("NOTICE %s :No hash provider modules are loaded", user->nick.c_str());
52 /* I dont do flying, bob. */
53 user->WriteServ("NOTICE %s :Unknown hash type, valid hash types are: %s", user->nick.c_str(), irc::stringjoiner(", ", names, 0, names.size() - 1).GetJoined().c_str() );
57 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
59 MakeHash(user, parameters[0].c_str(), parameters[1].c_str());
60 // this hashing could take some time, increasing server load.
61 // Slow down the user if they are trying to flood mkpasswd requests
62 user->IncreasePenalty(5);
68 class ModuleOperHash : public Module
72 hashymodules hashers; /* List of modules which implement HashRequest */
73 std::deque<std::string> names; /* Module names which implement HashRequest */
75 bool diduseiface; /* If we've called UseInterface yet. */
78 ModuleOperHash(InspIRCd* Me)
79 : Module(Me), cmd(this, hashers, names)
83 /* Read the config file first */
87 /* Find all modules which implement the interface 'HashRequest' */
88 modulelist* ml = ServerInstance->Modules->FindInterface("HashRequest");
90 /* Did we find any modules? */
93 /* Yes, enumerate them all to find out the hashing algorithm name */
94 for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
96 /* Make a request to it for its name, its implementing
97 * HashRequest so we know its safe to do this
99 std::string name = HashNameRequest(this, *m).Send();
100 /* Build a map of them */
101 hashers[name.c_str()] = *m;
102 names.push_back(name);
104 /* UseInterface doesn't do anything if there are no providers, so we'll have to call it later if a module gets loaded later on. */
105 ServerInstance->Modules->UseInterface("HashRequest");
109 ServerInstance->AddCommand(&cmd);
110 Implementation eventlist[] = { I_OnPassCompare, I_OnLoadModule };
111 ServerInstance->Modules->Attach(eventlist, this, 2);
114 virtual ~ModuleOperHash()
116 if (diduseiface) ServerInstance->Modules->DoneWithInterface("HashRequest");
120 virtual void OnLoadModule(Module* mod, const std::string& name)
122 if (ServerInstance->Modules->ModuleHasInterface(mod, "HashRequest"))
124 ServerInstance->Logs->Log("m_password-hash",DEBUG, "Post-load registering hasher: %s", name.c_str());
125 std::string sname = HashNameRequest(this, mod).Send();
126 hashers[sname.c_str()] = mod;
127 names.push_back(sname);
130 ServerInstance->Modules->UseInterface("HashRequest");
136 virtual ModResult OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
138 /* First, lets see what hash theyre using on this oper */
139 hashymodules::iterator x = hashers.find(hashtype.c_str());
141 /* Is this a valid hash name? (case insensitive) */
142 if (x != hashers.end())
144 /* Reset the hashing module */
145 HashResetRequest(this, x->second).Send();
146 /* Compare the hash in the config to the generated hash */
147 if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
148 return MOD_RES_ALLOW;
149 /* No match, and must be hashed, forbid */
154 /* Not a hash, fall through to strcmp in core */
155 return MOD_RES_PASSTHRU;
158 virtual Version GetVersion()
160 return Version("$Id$",VF_VENDOR,API_VERSION);
164 MODULE_INIT(ModuleOperHash)