1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 hashed oper passwords */
15 /* $ModDep: m_hash.h */
20 typedef std::map<irc::string, Module*> hashymodules;
24 class cmd_mkpasswd : public command_t
27 hashymodules &hashers;
28 std::deque<std::string> &names;
30 cmd_mkpasswd (InspIRCd* Instance, Module* S, hashymodules &h, std::deque<std::string> &n)
31 : command_t(Instance,"MKPASSWD", 'o', 2), Sender(S), hashers(h), names(n)
33 this->source = "m_oper_hash.so";
34 syntax = "<hashtype> <any-text>";
37 void MakeHash(userrec* user, const char* algo, const char* stuff)
39 /* Lets see if they gave us an algorithm which has been implemented */
40 hashymodules::iterator x = hashers.find(algo);
41 if (x != hashers.end())
43 /* Yup, reset it first (Always ALWAYS do this) */
44 HashResetRequest(Sender, x->second).Send();
45 /* Now attempt to generate a hash */
46 user->WriteServ("NOTICE %s :%s hashed password for %s is %s",user->nick, algo, stuff, HashSumRequest(Sender, x->second, stuff).Send() );
50 /* I dont do flying, bob. */
51 user->WriteServ("NOTICE %s :Unknown hash type, valid hash types are: %s", user->nick, irc::stringjoiner(", ", names, 0, names.size() - 1).GetJoined().c_str() );
55 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
57 MakeHash(user, parameters[0], parameters[1]);
58 /* NOTE: Don't propogate this across the network!
59 * We dont want plaintext passes going all over the place...
60 * To make sure it goes nowhere, return CMD_FAILURE!
66 class ModuleOperHash : public Module
69 cmd_mkpasswd* mycommand;
71 hashymodules hashers; /* List of modules which implement HashRequest */
72 std::deque<std::string> names; /* Module names which implement HashRequest */
76 ModuleOperHash(InspIRCd* Me)
80 /* Read the config file first */
84 ServerInstance->UseInterface("HashRequest");
86 /* Find all modules which implement the interface 'HashRequest' */
87 modulelist* ml = ServerInstance->FindInterface("HashRequest");
89 /* Did we find any modules? */
92 /* Yes, enumerate them all to find out the hashing algorithm name */
93 for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
95 /* Make a request to it for its name, its implementing
96 * HashRequest so we know its safe to do this
98 std::string name = HashNameRequest(this, *m).Send();
99 /* Build a map of them */
100 hashers[name.c_str()] = *m;
101 names.push_back(name);
106 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.");
109 mycommand = new cmd_mkpasswd(ServerInstance, this, hashers, names);
110 ServerInstance->AddCommand(mycommand);
113 virtual ~ModuleOperHash()
115 ServerInstance->DoneWithInterface("HashRequest");
118 void Implements(char* List)
120 List[I_OnRehash] = List[I_OnOperCompare] = 1;
123 virtual void OnRehash(userrec* user, const std::string ¶meter)
125 /* Re-read configuration file */
129 Conf = new ConfigReader(ServerInstance);
132 virtual int OnOperCompare(const std::string &data, const std::string &input, int tagnumber)
134 /* First, lets see what hash theyre using on this oper */
135 std::string hashtype = Conf->ReadValue("oper", "hash", tagnumber);
136 hashymodules::iterator x = hashers.find(hashtype.c_str());
138 /* Is this a valid hash name? (case insensitive) */
139 if (x != hashers.end())
141 /* Reset the hashing module */
142 HashResetRequest(this, x->second).Send();
143 /* Compare the hash in the config to the generated hash */
144 if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
146 /* No match, and must be hashed, forbid */
150 /* Not a hash, fall through to strcmp in core */
154 virtual Version GetVersion()
156 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
160 MODULE_INIT(ModuleOperHash)