]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_password_hash.cpp
3e94e9db125b64204ba978c62289a31a9ca93b60
[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://www.inspircd.org/wiki/index.php/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         Module* Sender;
27         hashymodules &hashers;
28         std::deque<std::string> &names;
29  public:
30         CommandMkpasswd (InspIRCd* Instance, Module* S, hashymodules &h, std::deque<std::string> &n)
31                 : Command(Instance,"MKPASSWD", 0, 2), Sender(S), hashers(h), names(n)
32         {
33                 this->source = "m_password_hash.so";
34                 syntax = "<hashtype> <any-text>";
35         }
36
37         void MakeHash(User* user, const char* algo, const char* stuff)
38         {
39                 /* Lets see if they gave us an algorithm which has been implemented */
40                 hashymodules::iterator x = hashers.find(algo);
41                 if (x != hashers.end())
42                 {
43                         /* Yup, reset it first (Always ALWAYS do this) */
44                         HashResetRequest(Sender, x->second).Send();
45                         /* Now attempt to generate a hash */
46                         user->WriteServ("NOTICE %s :%s hashed password for %s is %s",user->nick.c_str(), algo, stuff, HashSumRequest(Sender, x->second, stuff).Send() );
47                 }
48                 else if (names.empty())
49                 {
50                         /* same idea as bug #569 */
51                         user->WriteServ("NOTICE %s :No hash provider modules are loaded", user->nick.c_str());
52                 }
53                 else
54                 {
55                         /* I dont do flying, bob. */
56                         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() );
57                 }
58         }
59
60         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
61         {
62                 MakeHash(user, parameters[0].c_str(), parameters[1].c_str());
63                 // this hashing could take some time, increasing server load.
64                 // Slow down the user if they are trying to flood mkpasswd requests
65                 user->IncreasePenalty(5);
66
67                 return CMD_LOCALONLY;
68         }
69 };
70
71 class ModuleOperHash : public Module
72 {
73
74         CommandMkpasswd* mycommand;
75         hashymodules hashers; /* List of modules which implement HashRequest */
76         std::deque<std::string> names; /* Module names which implement HashRequest */
77
78         bool diduseiface; /* If we've called UseInterface yet. */
79  public:
80
81         ModuleOperHash(InspIRCd* Me)
82                 : Module(Me)
83         {
84                 diduseiface = false;
85
86                 /* Read the config file first */
87 //              Conf = NULL;
88                 OnRehash(NULL,"");
89
90                 /* Find all modules which implement the interface 'HashRequest' */
91                 modulelist* ml = ServerInstance->Modules->FindInterface("HashRequest");
92
93                 /* Did we find any modules? */
94                 if (ml)
95                 {
96                         /* Yes, enumerate them all to find out the hashing algorithm name */
97                         for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
98                         {
99                                 /* Make a request to it for its name, its implementing
100                                  * HashRequest so we know its safe to do this
101                                  */
102                                 std::string name = HashNameRequest(this, *m).Send();
103                                 /* Build a map of them */
104                                 hashers[name.c_str()] = *m;
105                                 names.push_back(name);
106                         }
107                         /* 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. */
108                         ServerInstance->Modules->UseInterface("HashRequest");
109                         diduseiface = true;
110                 }
111
112                 mycommand = new CommandMkpasswd(ServerInstance, this, hashers, names);
113                 ServerInstance->AddCommand(mycommand);
114                 Implementation eventlist[] = { I_OnPassCompare, I_OnLoadModule };
115                 ServerInstance->Modules->Attach(eventlist, this, 2);
116         }
117
118         virtual ~ModuleOperHash()
119         {
120                 if (diduseiface) ServerInstance->Modules->DoneWithInterface("HashRequest");
121         }
122
123
124         virtual void OnLoadModule(Module* mod, const std::string& name)
125         {
126                 if (ServerInstance->Modules->ModuleHasInterface(mod, "HashRequest"))
127                 {
128                         ServerInstance->Logs->Log("m_password-hash",DEBUG, "Post-load registering hasher: %s", name.c_str());
129                         std::string sname = HashNameRequest(this, mod).Send();
130                         hashers[sname.c_str()] = mod;
131                         names.push_back(sname);
132                         if (!diduseiface)
133                         {
134                                 ServerInstance->Modules->UseInterface("HashRequest");
135                                 diduseiface = true;
136                         }
137                 }
138         }
139
140         virtual int OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
141         {
142                 /* First, lets see what hash theyre using on this oper */
143                 hashymodules::iterator x = hashers.find(hashtype.c_str());
144
145                 /* Is this a valid hash name? (case insensitive) */
146                 if (x != hashers.end())
147                 {
148                         /* Reset the hashing module */
149                         HashResetRequest(this, x->second).Send();
150                         /* Compare the hash in the config to the generated hash */
151                         if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
152                                 return 1;
153                         /* No match, and must be hashed, forbid */
154                         else return -1;
155                 }
156
157                 /* Not a hash, fall through to strcmp in core */
158                 return 0;
159         }
160
161         virtual Version GetVersion()
162         {
163                 return Version("$Id$",VF_VENDOR,API_VERSION);
164         }
165 };
166
167 MODULE_INIT(ModuleOperHash)