]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_oper_hash.cpp
Remove some debug (im on a crusade to make debug mode useful, but at the same time...
[user/henk/code/inspircd.git] / src / modules / m_oper_hash.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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_config.h"
18 #include "users.h"
19 #include "channels.h"
20 #include "modules.h"
21 #include "inspircd.h"
22
23 #include "m_hash.h"
24
25 typedef std::map<irc::string, Module*> hashymodules;
26
27 /* Handle /MKPASSWD
28  */
29 class cmd_mkpasswd : public command_t
30 {
31         Module* Sender;
32         hashymodules &hashers;
33         std::deque<std::string> &names;
34  public:
35         cmd_mkpasswd (InspIRCd* Instance, Module* S, hashymodules &h, std::deque<std::string> &n)
36                 : command_t(Instance,"MKPASSWD", 'o', 2), Sender(S), hashers(h), names(n)
37         {
38                 this->source = "m_oper_hash.so";
39                 syntax = "<hashtype> <any-text>";
40         }
41
42         void MakeHash(userrec* user, const char* algo, const char* stuff)
43         {
44                 /* Lets see if they gave us an algorithm which has been implemented */
45                 hashymodules::iterator x = hashers.find(algo);
46                 if (x != hashers.end())
47                 {
48                         /* Yup, reset it first (Always ALWAYS do this) */
49                         HashResetRequest(Sender, x->second).Send();
50                         /* Now attempt to generate a hash */
51                         user->WriteServ("NOTICE %s :%s hashed password for %s is %s",user->nick, algo, stuff, HashSumRequest(Sender, x->second, stuff).Send() );
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, irc::stringjoiner(", ", names, 0, names.size() - 1).GetJoined().c_str() );
57                 }
58         }
59
60         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
61         {
62                 MakeHash(user, parameters[0], parameters[1]);
63                 /* NOTE: Don't propogate this across the network!
64                  * We dont want plaintext passes going all over the place...
65                  * To make sure it goes nowhere, return CMD_FAILURE!
66                  */
67                 return CMD_FAILURE;
68         }
69 };
70
71 class ModuleOperHash : public Module
72 {
73         
74         cmd_mkpasswd* mycommand;
75         ConfigReader* Conf;
76         hashymodules hashers; /* List of modules which implement HashRequest */
77         std::deque<std::string> names; /* Module names which implement HashRequest */
78
79  public:
80
81         ModuleOperHash(InspIRCd* Me)
82                 : Module::Module(Me)
83         {
84
85                 /* Read the config file first */
86                 Conf = NULL;
87                 OnRehash(NULL,"");
88
89                 ServerInstance->UseInterface("HashRequest");
90
91                 /* Find all modules which implement the interface 'HashRequest' */
92                 modulelist* ml = ServerInstance->FindInterface("HashRequest");
93
94                 /* Did we find any modules? */
95                 if (ml)
96                 {
97                         /* Yes, enumerate them all to find out the hashing algorithm name */
98                         for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
99                         {
100                                 /* Make a request to it for its name, its implementing
101                                  * HashRequest so we know its safe to do this
102                                  */
103                                 std::string name = HashNameRequest(this, *m).Send();
104                                 /* Build a map of them */
105                                 hashers[name.c_str()] = *m;
106                                 names.push_back(name);
107                                 ServerInstance->Log(DEBUG, "Found HashRequest interface: '%s' -> '%08x'", name.c_str(), *m);
108                         }
109                 }
110                 else
111                 {
112                         throw ModuleException("I can't find any modules loaded which implement the HashRequest interface! You probably forgot to load a hashing module such as m_md5.so or m_sha256.so.");
113                 }
114
115                 mycommand = new cmd_mkpasswd(ServerInstance, this, hashers, names);
116                 ServerInstance->AddCommand(mycommand);
117         }
118         
119         virtual ~ModuleOperHash()
120         {
121                 ServerInstance->DoneWithInterface("HashRequest");
122         }
123
124         void Implements(char* List)
125         {
126                 List[I_OnRehash] = List[I_OnOperCompare] = 1;
127         }
128
129         virtual void OnRehash(userrec* user, const std::string &parameter)
130         {
131                 /* Re-read configuration file */
132                 if (Conf)
133                         delete Conf;
134
135                 Conf = new ConfigReader(ServerInstance);
136         }
137
138         virtual int OnOperCompare(const std::string &data, const std::string &input, int tagnumber)
139         {
140                 /* First, lets see what hash theyre using on this oper */
141                 std::string hashtype = Conf->ReadValue("oper", "hash", tagnumber);
142                 hashymodules::iterator x = hashers.find(hashtype.c_str());
143
144                 /* Is this a valid hash name? (case insensitive) */
145                 if (x != hashers.end())
146                 {
147                         /* Reset the hashing module */
148                         HashResetRequest(this, x->second).Send();
149                         /* Compare the hash in the config to the generated hash */
150                         if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
151                                 return 1;
152                         /* No match, and must be hashed, forbid */
153                         else return -1;
154                 }
155
156                 /* Not a hash, fall through to strcmp in core */
157                 return 0;
158         }
159
160         virtual Version GetVersion()
161         {
162                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
163         }
164 };
165
166
167 class ModuleOperHashFactory : public ModuleFactory
168 {
169  public:
170         ModuleOperHashFactory()
171         {
172         }
173         
174         ~ModuleOperHashFactory()
175         {
176         }
177         
178         virtual Module * CreateModule(InspIRCd* Me)
179         {
180                 return new ModuleOperHash(Me);
181         }
182         
183 };
184
185
186 extern "C" void * init_module( void )
187 {
188         return new ModuleOperHashFactory;
189 }