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