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 */
19 typedef std::map<irc::string, Module*> hashymodules;
23 class CommandMkpasswd : public Command
25 hashymodules &hashers;
26 std::deque<std::string> &names;
28 CommandMkpasswd(Module* Creator, hashymodules &h, std::deque<std::string> &n) : Command(Creator, "MKPASSWD", 2), hashers(h), names(n)
30 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 HashRequest hash(creator, x->second, stuff);
41 /* Now attempt to generate a hash */
42 user->WriteServ("NOTICE %s :%s hashed password for %s is %s",
43 user->nick.c_str(), algo, stuff, hash.hex().c_str());
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());
65 class ModuleOperHash : public Module
69 hashymodules hashers; /* List of modules which implement HashRequest */
70 std::deque<std::string> names; /* Module names which implement HashRequest */
72 bool diduseiface; /* If we've called UseInterface yet. */
76 : cmd(this, hashers, names)
80 /* Read the config file first */
84 /* Find all modules which implement the interface 'HashRequest' */
85 modulelist* ml = ServerInstance->Modules->FindInterface("HashRequest");
87 /* Did we find any modules? */
90 /* Yes, enumerate them all to find out the hashing algorithm name */
91 for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
93 /* Make a request to it for its name, its implementing
94 * HashRequest so we know its safe to do this
96 std::string name = HashNameRequest(this, *m).response;
97 /* Build a map of them */
98 hashers[name.c_str()] = *m;
99 names.push_back(name);
101 /* 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. */
102 ServerInstance->Modules->UseInterface("HashRequest");
106 ServerInstance->AddCommand(&cmd);
107 Implementation eventlist[] = { I_OnPassCompare, I_OnLoadModule };
108 ServerInstance->Modules->Attach(eventlist, this, 2);
111 virtual ~ModuleOperHash()
113 if (diduseiface) ServerInstance->Modules->DoneWithInterface("HashRequest");
117 virtual void OnLoadModule(Module* mod)
119 if (ServerInstance->Modules->ModuleHasInterface(mod, "HashRequest"))
121 std::string sname = HashNameRequest(this, mod).response;
122 hashers[sname.c_str()] = mod;
123 names.push_back(sname);
126 ServerInstance->Modules->UseInterface("HashRequest");
132 virtual ModResult OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
134 /* First, lets see what hash theyre using on this oper */
135 hashymodules::iterator x = hashers.find(hashtype.c_str());
137 /* Is this a valid hash name? (case insensitive) */
138 if (x != hashers.end())
140 /* Compare the hash in the config to the generated hash */
141 if (!strcasecmp(data.c_str(), HashRequest(this, x->second, input).hex().c_str()))
142 return MOD_RES_ALLOW;
143 /* No match, and must be hashed, forbid */
148 /* Not a hash, fall through to strcmp in core */
149 return MOD_RES_PASSTHRU;
152 virtual Version GetVersion()
154 return Version("Allows for hashed oper passwords",VF_VENDOR);
158 MODULE_INIT(ModuleOperHash)