]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_password_hash.cpp
Fix module unmapping with culled Module objects
[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 typedef std::map<irc::string, Module*> hashymodules;
20
21 /* Handle /MKPASSWD
22  */
23 class CommandMkpasswd : public Command
24 {
25         hashymodules &hashers;
26         std::deque<std::string> &names;
27  public:
28         CommandMkpasswd(Module* Creator, hashymodules &h, std::deque<std::string> &n) : Command(Creator, "MKPASSWD", 2), hashers(h), names(n)
29         {
30                 syntax = "<hashtype> <any-text>";
31                 Penalty = 5;
32         }
33
34         void MakeHash(User* user, const char* algo, const char* stuff)
35         {
36                 /* Lets see if they gave us an algorithm which has been implemented */
37                 hashymodules::iterator x = hashers.find(algo);
38                 if (x != hashers.end())
39                 {
40                         HashRequest hash(creator, x->second, stuff);
41                         /* Now attempt to generate a hash */
42                         user->WriteServ("NOTICE %s :%s hashed password for %s is %s",
43                                 user->nick.c_str(), algo, stuff, hash.hex().c_str());
44                 }
45                 else if (names.empty())
46                 {
47                         /* same idea as bug #569 */
48                         user->WriteServ("NOTICE %s :No hash provider modules are loaded", user->nick.c_str());
49                 }
50                 else
51                 {
52                         /* I dont do flying, bob. */
53                         user->WriteServ("NOTICE %s :Unknown hash type, valid hash types are: %s", user->nick.c_str(), irc::stringjoiner(", ", names, 0, names.size() - 1).GetJoined().c_str() );
54                 }
55         }
56
57         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
58         {
59                 MakeHash(user, parameters[0].c_str(), parameters[1].c_str());
60
61                 return CMD_SUCCESS;
62         }
63 };
64
65 class ModuleOperHash : public Module
66 {
67
68         CommandMkpasswd cmd;
69         hashymodules hashers; /* List of modules which implement HashRequest */
70         std::deque<std::string> names; /* Module names which implement HashRequest */
71
72         bool diduseiface; /* If we've called UseInterface yet. */
73  public:
74
75         ModuleOperHash()
76                 : cmd(this, hashers, names)
77         {
78                 diduseiface = false;
79
80                 /* Read the config file first */
81 //              Conf = NULL;
82                 OnRehash(NULL);
83
84                 /* Find all modules which implement the interface 'HashRequest' */
85                 modulelist* ml = ServerInstance->Modules->FindInterface("HashRequest");
86
87                 /* Did we find any modules? */
88                 if (ml)
89                 {
90                         /* Yes, enumerate them all to find out the hashing algorithm name */
91                         for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
92                         {
93                                 /* Make a request to it for its name, its implementing
94                                  * HashRequest so we know its safe to do this
95                                  */
96                                 std::string name = HashNameRequest(this, *m).response;
97                                 /* Build a map of them */
98                                 hashers[name.c_str()] = *m;
99                                 names.push_back(name);
100                         }
101                         /* UseInterface doesn't do anything if there are no providers, so we'll have to call it later if a module gets loaded later on. */
102                         ServerInstance->Modules->UseInterface("HashRequest");
103                         diduseiface = true;
104                 }
105
106                 ServerInstance->AddCommand(&cmd);
107                 Implementation eventlist[] = { I_OnPassCompare, I_OnLoadModule };
108                 ServerInstance->Modules->Attach(eventlist, this, 2);
109         }
110
111         virtual ~ModuleOperHash()
112         {
113                 if (diduseiface) ServerInstance->Modules->DoneWithInterface("HashRequest");
114         }
115
116
117         virtual void OnLoadModule(Module* mod)
118         {
119                 if (ServerInstance->Modules->ModuleHasInterface(mod, "HashRequest"))
120                 {
121                         std::string sname = HashNameRequest(this, mod).response;
122                         hashers[sname.c_str()] = mod;
123                         names.push_back(sname);
124                         if (!diduseiface)
125                         {
126                                 ServerInstance->Modules->UseInterface("HashRequest");
127                                 diduseiface = true;
128                         }
129                 }
130         }
131
132         virtual ModResult OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
133         {
134                 /* First, lets see what hash theyre using on this oper */
135                 hashymodules::iterator x = hashers.find(hashtype.c_str());
136
137                 /* Is this a valid hash name? (case insensitive) */
138                 if (x != hashers.end())
139                 {
140                         /* Compare the hash in the config to the generated hash */
141                         if (!strcasecmp(data.c_str(), HashRequest(this, x->second, input).hex().c_str()))
142                                 return MOD_RES_ALLOW;
143                         /* No match, and must be hashed, forbid */
144                         else
145                                 return MOD_RES_DENY;
146                 }
147
148                 /* Not a hash, fall through to strcmp in core */
149                 return MOD_RES_PASSTHRU;
150         }
151
152         virtual Version GetVersion()
153         {
154                 return Version("Allows for hashed oper passwords",VF_VENDOR,API_VERSION);
155         }
156 };
157
158 MODULE_INIT(ModuleOperHash)