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 */
23 typedef std::map<irc::string, Module*> hashymodules;
27 class cmd_mkpasswd : public command_t
30 hashymodules &hashers;
31 std::deque<std::string> &names;
33 cmd_mkpasswd (InspIRCd* Instance, Module* S, hashymodules &h, std::deque<std::string> &n)
34 : command_t(Instance,"MKPASSWD", 'o', 2), Sender(S), hashers(h), names(n)
36 this->source = "m_oper_hash.so";
37 syntax = "<hashtype> <any-text>";
40 void MakeHash(userrec* user, const char* algo, const char* stuff)
42 /* Lets see if they gave us an algorithm which has been implemented */
43 hashymodules::iterator x = hashers.find(algo);
44 if (x != hashers.end())
46 /* Yup, reset it first (Always ALWAYS do this) */
47 HashResetRequest(Sender, x->second).Send();
48 /* Now attempt to generate a hash */
49 user->WriteServ("NOTICE %s :%s hashed password for %s is %s",user->nick, algo, stuff, HashSumRequest(Sender, x->second, stuff).Send() );
53 /* I dont do flying, bob. */
54 user->WriteServ("NOTICE %s :Unknown hash type, valid hash types are: %s", user->nick, irc::stringjoiner(", ", names, 0, names.size() - 1).GetJoined().c_str() );
58 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
60 MakeHash(user, parameters[0], parameters[1]);
61 /* NOTE: Don't propogate this across the network!
62 * We dont want plaintext passes going all over the place...
63 * To make sure it goes nowhere, return CMD_FAILURE!
69 class ModuleOperHash : public Module
72 cmd_mkpasswd* mycommand;
74 hashymodules hashers; /* List of modules which implement HashRequest */
75 std::deque<std::string> names; /* Module names which implement HashRequest */
79 ModuleOperHash(InspIRCd* Me)
83 /* Read the config file first */
87 ServerInstance->UseInterface("HashRequest");
89 /* Find all modules which implement the interface 'HashRequest' */
90 modulelist* ml = ServerInstance->FindInterface("HashRequest");
92 /* Did we find any modules? */
95 /* Yes, enumerate them all to find out the hashing algorithm name */
96 for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
98 /* Make a request to it for its name, its implementing
99 * HashRequest so we know its safe to do this
101 std::string name = HashNameRequest(this, *m).Send();
102 /* Build a map of them */
103 hashers[name.c_str()] = *m;
104 names.push_back(name);
109 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.");
112 mycommand = new cmd_mkpasswd(ServerInstance, this, hashers, names);
113 ServerInstance->AddCommand(mycommand);
116 virtual ~ModuleOperHash()
118 ServerInstance->DoneWithInterface("HashRequest");
121 void Implements(char* List)
123 List[I_OnRehash] = List[I_OnOperCompare] = 1;
126 virtual void OnRehash(userrec* user, const std::string ¶meter)
128 /* Re-read configuration file */
132 Conf = new ConfigReader(ServerInstance);
135 virtual int OnOperCompare(const std::string &data, const std::string &input, int tagnumber)
137 /* First, lets see what hash theyre using on this oper */
138 std::string hashtype = Conf->ReadValue("oper", "hash", tagnumber);
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()))
149 /* No match, and must be hashed, forbid */
153 /* Not a hash, fall through to strcmp in core */
157 virtual Version GetVersion()
159 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
163 MODULE_INIT(ModuleOperHash)