]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_oper_hash.cpp
Windows support. Tested and working to compile on freebsd and linux. Next step is...
[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(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                         }
108                 }
109                 else
110                 {
111                         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.");
112                 }
113
114                 mycommand = new cmd_mkpasswd(ServerInstance, this, hashers, names);
115                 ServerInstance->AddCommand(mycommand);
116         }
117         
118         virtual ~ModuleOperHash()
119         {
120                 ServerInstance->DoneWithInterface("HashRequest");
121         }
122
123         void Implements(char* List)
124         {
125                 List[I_OnRehash] = List[I_OnOperCompare] = 1;
126         }
127
128         virtual void OnRehash(userrec* user, const std::string &parameter)
129         {
130                 /* Re-read configuration file */
131                 if (Conf)
132                         delete Conf;
133
134                 Conf = new ConfigReader(ServerInstance);
135         }
136
137         virtual int OnOperCompare(const std::string &data, const std::string &input, int tagnumber)
138         {
139                 /* First, lets see what hash theyre using on this oper */
140                 std::string hashtype = Conf->ReadValue("oper", "hash", tagnumber);
141                 hashymodules::iterator x = hashers.find(hashtype.c_str());
142
143                 /* Is this a valid hash name? (case insensitive) */
144                 if (x != hashers.end())
145                 {
146                         /* Reset the hashing module */
147                         HashResetRequest(this, x->second).Send();
148                         /* Compare the hash in the config to the generated hash */
149                         if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
150                                 return 1;
151                         /* No match, and must be hashed, forbid */
152                         else return -1;
153                 }
154
155                 /* Not a hash, fall through to strcmp in core */
156                 return 0;
157         }
158
159         virtual Version GetVersion()
160         {
161                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
162         }
163 };
164
165
166 class ModuleOperHashFactory : public ModuleFactory
167 {
168  public:
169         ModuleOperHashFactory()
170         {
171         }
172         
173         ~ModuleOperHashFactory()
174         {
175         }
176         
177         virtual Module * CreateModule(InspIRCd* Me)
178         {
179                 return new ModuleOperHash(Me);
180         }
181         
182 };
183
184
185 extern "C" DllExport void * init_module( void )
186 {
187         return new ModuleOperHashFactory;
188 }