]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_password_hash.cpp
Remove InspIRCd* parameters and fields
[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 /* $ModDep: m_hash.h */
16
17 #include "inspircd.h"
18 #include "m_hash.h"
19
20 typedef std::map<irc::string, Module*> hashymodules;
21
22 /* Handle /MKPASSWD
23  */
24 class CommandMkpasswd : public Command
25 {
26         hashymodules &hashers;
27         std::deque<std::string> &names;
28  public:
29         CommandMkpasswd(Module* Creator, hashymodules &h, std::deque<std::string> &n) : Command(Creator, "MKPASSWD", 2), hashers(h), names(n)
30         {
31                 syntax = "<hashtype> <any-text>";
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                         /* Yup, reset it first (Always ALWAYS do this) */
41                         HashResetRequest(creator, x->second).Send();
42                         /* Now attempt to generate a hash */
43                         user->WriteServ("NOTICE %s :%s hashed password for %s is %s",user->nick.c_str(), algo, stuff, HashSumRequest(creator, x->second, stuff).Send() );
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                 // this hashing could take some time, increasing server load.
61                 // Slow down the user if they are trying to flood mkpasswd requests
62                 user->IncreasePenalty(5);
63
64                 return CMD_SUCCESS;
65         }
66 };
67
68 class ModuleOperHash : public Module
69 {
70
71         CommandMkpasswd cmd;
72         hashymodules hashers; /* List of modules which implement HashRequest */
73         std::deque<std::string> names; /* Module names which implement HashRequest */
74
75         bool diduseiface; /* If we've called UseInterface yet. */
76  public:
77
78         ModuleOperHash()
79                 : cmd(this, hashers, names)
80         {
81                 diduseiface = false;
82
83                 /* Read the config file first */
84 //              Conf = NULL;
85                 OnRehash(NULL);
86
87                 /* Find all modules which implement the interface 'HashRequest' */
88                 modulelist* ml = ServerInstance->Modules->FindInterface("HashRequest");
89
90                 /* Did we find any modules? */
91                 if (ml)
92                 {
93                         /* Yes, enumerate them all to find out the hashing algorithm name */
94                         for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
95                         {
96                                 /* Make a request to it for its name, its implementing
97                                  * HashRequest so we know its safe to do this
98                                  */
99                                 std::string name = HashNameRequest(this, *m).Send();
100                                 /* Build a map of them */
101                                 hashers[name.c_str()] = *m;
102                                 names.push_back(name);
103                         }
104                         /* 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. */
105                         ServerInstance->Modules->UseInterface("HashRequest");
106                         diduseiface = true;
107                 }
108
109                 ServerInstance->AddCommand(&cmd);
110                 Implementation eventlist[] = { I_OnPassCompare, I_OnLoadModule };
111                 ServerInstance->Modules->Attach(eventlist, this, 2);
112         }
113
114         virtual ~ModuleOperHash()
115         {
116                 if (diduseiface) ServerInstance->Modules->DoneWithInterface("HashRequest");
117         }
118
119
120         virtual void OnLoadModule(Module* mod, const std::string& name)
121         {
122                 if (ServerInstance->Modules->ModuleHasInterface(mod, "HashRequest"))
123                 {
124                         ServerInstance->Logs->Log("m_password-hash",DEBUG, "Post-load registering hasher: %s", name.c_str());
125                         std::string sname = HashNameRequest(this, mod).Send();
126                         hashers[sname.c_str()] = mod;
127                         names.push_back(sname);
128                         if (!diduseiface)
129                         {
130                                 ServerInstance->Modules->UseInterface("HashRequest");
131                                 diduseiface = true;
132                         }
133                 }
134         }
135
136         virtual ModResult OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
137         {
138                 /* First, lets see what hash theyre using on this oper */
139                 hashymodules::iterator x = hashers.find(hashtype.c_str());
140
141                 /* Is this a valid hash name? (case insensitive) */
142                 if (x != hashers.end())
143                 {
144                         /* Reset the hashing module */
145                         HashResetRequest(this, x->second).Send();
146                         /* Compare the hash in the config to the generated hash */
147                         if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
148                                 return MOD_RES_ALLOW;
149                         /* No match, and must be hashed, forbid */
150                         else
151                                 return MOD_RES_DENY;
152                 }
153
154                 /* Not a hash, fall through to strcmp in core */
155                 return MOD_RES_PASSTHRU;
156         }
157
158         virtual Version GetVersion()
159         {
160                 return Version("Allows for hashed oper passwords",VF_VENDOR,API_VERSION);
161         }
162 };
163
164 MODULE_INIT(ModuleOperHash)