]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_password_hash.cpp
Use ServiceProvider for inter-module dependencies
[user/henk/code/inspircd.git] / src / modules / m_password_hash.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $ModDesc: Allows for hashed oper passwords */
15
16 #include "inspircd.h"
17 #include "m_hash.h"
18
19 /* Handle /MKPASSWD
20  */
21 class CommandMkpasswd : public Command
22 {
23  public:
24         CommandMkpasswd(Module* Creator) : Command(Creator, "MKPASSWD", 2)
25         {
26                 syntax = "<hashtype> <any-text>";
27                 Penalty = 5;
28         }
29
30         void MakeHash(User* user, const std::string& algo, const std::string& stuff)
31         {
32                 HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + algo);
33                 if (hp)
34                 {
35                         /* Now attempt to generate a hash */
36                         user->WriteServ("NOTICE %s :%s hashed password for %s is %s",
37                                 user->nick.c_str(), algo.c_str(), stuff.c_str(), hp->hexsum(stuff).c_str());
38                 }
39                 else
40                 {
41                         /* I dont do flying, bob. */
42                         user->WriteServ("NOTICE %s :Unknown hash type", user->nick.c_str());
43                 }
44         }
45
46         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
47         {
48                 MakeHash(user, parameters[0], parameters[1]);
49
50                 return CMD_SUCCESS;
51         }
52 };
53
54 class ModuleOperHash : public Module
55 {
56         CommandMkpasswd cmd;
57  public:
58
59         ModuleOperHash() : cmd(this)
60         {
61                 /* Read the config file first */
62                 OnRehash(NULL);
63
64                 ServerInstance->AddCommand(&cmd);
65                 Implementation eventlist[] = { I_OnPassCompare };
66                 ServerInstance->Modules->Attach(eventlist, this, 1);
67         }
68
69         virtual ModResult OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
70         {
71                 HashProvider* hp = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
72
73                 /* Is this a valid hash name? */
74                 if (hp)
75                 {
76                         /* Compare the hash in the config to the generated hash */
77                         if (data == hp->hexsum(input))
78                                 return MOD_RES_ALLOW;
79                         else
80                                 /* No match, and must be hashed, forbid */
81                                 return MOD_RES_DENY;
82                 }
83
84                 /* Not a hash, fall through to strcmp in core */
85                 return MOD_RES_PASSTHRU;
86         }
87
88         virtual Version GetVersion()
89         {
90                 return Version("Allows for hashed oper passwords",VF_VENDOR);
91         }
92 };
93
94 MODULE_INIT(ModuleOperHash)