2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 /* $ModDesc: Allows for hashed oper passwords */
24 #include "modules/hash.h"
28 class CommandMkpasswd : public Command
31 CommandMkpasswd(Module* Creator) : Command(Creator, "MKPASSWD", 2)
33 syntax = "<hashtype> <any-text>";
37 void MakeHash(User* user, const std::string& algo, const std::string& stuff)
39 if (algo.substr(0,5) == "hmac-")
41 std::string type = algo.substr(5);
42 HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + type);
45 user->WriteNotice("Unknown hash type");
48 std::string salt = ServerInstance->GenRandomStr(6, false);
49 std::string target = hp->hmac(salt, stuff);
50 std::string str = BinToBase64(salt) + "$" + BinToBase64(target, NULL, 0);
52 user->WriteNotice(algo + " hashed password for " + stuff + " is " + str);
55 HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + algo);
58 /* Now attempt to generate a hash */
59 std::string hexsum = hp->hexsum(stuff);
60 user->WriteNotice(algo + " hashed password for " + stuff + " is " + hexsum);
64 user->WriteNotice("Unknown hash type");
68 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
70 MakeHash(user, parameters[0], parameters[1]);
76 class ModuleOperHash : public Module
81 ModuleOperHash() : cmd(this)
85 void init() CXX11_OVERRIDE
87 /* Read the config file first */
90 ServerInstance->Modules->AddService(cmd);
91 Implementation eventlist[] = { I_OnPassCompare };
92 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
95 ModResult OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype) CXX11_OVERRIDE
97 if (hashtype.substr(0,5) == "hmac-")
99 std::string type = hashtype.substr(5);
100 HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + type);
102 return MOD_RES_PASSTHRU;
103 // this is a valid hash, from here on we either accept or deny
104 std::string::size_type sep = data.find('$');
105 if (sep == std::string::npos)
107 std::string salt = Base64ToBin(data.substr(0, sep));
108 std::string target = Base64ToBin(data.substr(sep + 1));
110 if (target == hp->hmac(salt, input))
111 return MOD_RES_ALLOW;
116 HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
118 /* Is this a valid hash name? */
121 /* Compare the hash in the config to the generated hash */
122 if (data == hp->hexsum(input))
123 return MOD_RES_ALLOW;
125 /* No match, and must be hashed, forbid */
129 /* Not a hash, fall through to strcmp in core */
130 return MOD_RES_PASSTHRU;
133 Version GetVersion() CXX11_OVERRIDE
135 return Version("Allows for hashed oper passwords",VF_VENDOR);
139 MODULE_INIT(ModuleOperHash)