1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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 */
21 class CommandMkpasswd : public Command
24 CommandMkpasswd(Module* Creator) : Command(Creator, "MKPASSWD", 2)
26 syntax = "<hashtype> <any-text>";
30 void MakeHash(User* user, const std::string& algo, const std::string& stuff)
32 if (algo.substr(0,5) == "hmac-")
34 std::string type = algo.substr(5);
35 HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + type);
38 user->WriteServ("NOTICE %s :Unknown hash type", user->nick.c_str());
41 std::string salt = ServerInstance->GenRandomStr(6, false);
42 std::string target = hp->hmac(salt, stuff);
43 std::string str = BinToBase64(salt) + "$" + BinToBase64(target, NULL, 0);
45 user->WriteServ("NOTICE %s :%s hashed password for %s is %s",
46 user->nick.c_str(), algo.c_str(), stuff.c_str(), str.c_str());
49 HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + algo);
52 /* Now attempt to generate a hash */
53 user->WriteServ("NOTICE %s :%s hashed password for %s is %s",
54 user->nick.c_str(), algo.c_str(), stuff.c_str(), hp->hexsum(stuff).c_str());
58 user->WriteServ("NOTICE %s :Unknown hash type", user->nick.c_str());
62 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
64 MakeHash(user, parameters[0], parameters[1]);
70 class ModuleOperHash : public Module
75 ModuleOperHash() : cmd(this)
77 /* Read the config file first */
80 ServerInstance->AddCommand(&cmd);
81 Implementation eventlist[] = { I_OnPassCompare };
82 ServerInstance->Modules->Attach(eventlist, this, 1);
85 virtual ModResult OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
87 if (hashtype.substr(0,5) == "hmac-")
89 std::string type = hashtype.substr(5);
90 HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + type);
92 return MOD_RES_PASSTHRU;
93 // this is a valid hash, from here on we either accept or deny
94 std::string::size_type sep = data.find('$');
95 if (sep == std::string::npos)
97 std::string salt = Base64ToBin(data.substr(0, sep));
98 std::string target = Base64ToBin(data.substr(sep + 1));
100 if (target == hp->hmac(salt, input))
101 return MOD_RES_ALLOW;
106 HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
108 /* Is this a valid hash name? */
111 /* Compare the hash in the config to the generated hash */
112 if (data == hp->hexsum(input))
113 return MOD_RES_ALLOW;
115 /* No match, and must be hashed, forbid */
119 /* Not a hash, fall through to strcmp in core */
120 return MOD_RES_PASSTHRU;
123 virtual Version GetVersion()
125 return Version("Allows for hashed oper passwords",VF_VENDOR);
129 MODULE_INIT(ModuleOperHash)