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