]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_password_hash.cpp
Fix more undefined behavior caused by referencing the returned buffer by std::string...
[user/henk/code/inspircd.git] / src / modules / m_password_hash.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 /* $ModDesc: Allows for hashed oper passwords */
22
23 #include "inspircd.h"
24 #include "hash.h"
25
26 /* Handle /MKPASSWD
27  */
28 class CommandMkpasswd : public Command
29 {
30  public:
31         CommandMkpasswd(Module* Creator) : Command(Creator, "MKPASSWD", 2)
32         {
33                 syntax = "<hashtype> <any-text>";
34                 Penalty = 5;
35         }
36
37         void MakeHash(User* user, const std::string& algo, const std::string& stuff)
38         {
39                 if (algo.substr(0,5) == "hmac-")
40                 {
41                         std::string type = algo.substr(5);
42                         HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + type);
43                         if (!hp)
44                         {
45                                 user->WriteServ("NOTICE %s :Unknown hash type", user->nick.c_str());
46                                 return;
47                         }
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);
51
52                         user->WriteServ("NOTICE %s :%s hashed password for %s is %s",
53                                 user->nick.c_str(), algo.c_str(), stuff.c_str(), str.c_str());
54                         return;
55                 }
56                 HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + algo);
57                 if (hp)
58                 {
59                         /* Now attempt to generate a hash */
60                         std::string hexsum = hp->hexsum(stuff);
61                         user->WriteServ("NOTICE %s :%s hashed password for %s is %s",
62                                 user->nick.c_str(), algo.c_str(), stuff.c_str(), hexsum.c_str());
63                 }
64                 else
65                 {
66                         user->WriteServ("NOTICE %s :Unknown hash type", user->nick.c_str());
67                 }
68         }
69
70         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
71         {
72                 MakeHash(user, parameters[0], parameters[1]);
73
74                 return CMD_SUCCESS;
75         }
76 };
77
78 class ModuleOperHash : public Module
79 {
80         CommandMkpasswd cmd;
81  public:
82
83         ModuleOperHash() : cmd(this)
84         {
85                 /* Read the config file first */
86                 OnRehash(NULL);
87
88                 ServerInstance->AddCommand(&cmd);
89                 Implementation eventlist[] = { I_OnPassCompare };
90                 ServerInstance->Modules->Attach(eventlist, this, 1);
91         }
92
93         virtual ModResult OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
94         {
95                 if (hashtype.substr(0,5) == "hmac-")
96                 {
97                         std::string type = hashtype.substr(5);
98                         HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + type);
99                         if (!hp)
100                                 return MOD_RES_PASSTHRU;
101                         // this is a valid hash, from here on we either accept or deny
102                         std::string::size_type sep = data.find('$');
103                         if (sep == std::string::npos)
104                                 return MOD_RES_DENY;
105                         std::string salt = Base64ToBin(data.substr(0, sep));
106                         std::string target = Base64ToBin(data.substr(sep + 1));
107
108                         if (target == hp->hmac(salt, input))
109                                 return MOD_RES_ALLOW;
110                         else
111                                 return MOD_RES_DENY;
112                 }
113
114                 HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
115
116                 /* Is this a valid hash name? */
117                 if (hp)
118                 {
119                         /* Compare the hash in the config to the generated hash */
120                         if (data == hp->hexsum(input))
121                                 return MOD_RES_ALLOW;
122                         else
123                                 /* No match, and must be hashed, forbid */
124                                 return MOD_RES_DENY;
125                 }
126
127                 /* Not a hash, fall through to strcmp in core */
128                 return MOD_RES_PASSTHRU;
129         }
130
131         virtual Version GetVersion()
132         {
133                 return Version("Allows for hashed oper passwords",VF_VENDOR);
134         }
135 };
136
137 MODULE_INIT(ModuleOperHash)