]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_oper_hash.cpp
b9e246c2b4d0e0a360ca6e0d3dd68a44c6320eff
[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                 /* Find all modules which implement the interface 'HashRequest' */
95                 modulelist* ml = ServerInstance->FindInterface("HashRequest");
96
97                 /* Did we find any modules? */
98                 if (ml)
99                 {
100                         /* Yes, enumerate them all to find out the hashing algorithm name */
101                         for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
102                         {
103                                 /* Make a request to it for its name, its implementing
104                                  * HashRequest so we know its safe to do this
105                                  */
106                                 std::string name = HashNameRequest(this, *m).Send();
107                                 /* Build a map of them */
108                                 hashers[name.c_str()] = *m;
109                                 names.push_back(name);
110                                 ServerInstance->Log(DEBUG, "Found HashRequest interface: '%s' -> '%08x'", name.c_str(), *m);
111                         }
112                 }
113                 else
114                 {
115                         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.");
116                 }
117
118                 mycommand = new cmd_mkpasswd(ServerInstance, this, hashers, names);
119                 ServerInstance->AddCommand(mycommand);
120         }
121         
122         virtual ~ModuleOperHash()
123         {
124         }
125
126         void Implements(char* List)
127         {
128                 List[I_OnRehash] = List[I_OnOperCompare] = 1;
129         }
130
131         virtual void OnRehash(const std::string &parameter)
132         {
133                 /* Re-read configuration file */
134                 if (Conf)
135                         delete Conf;
136
137                 Conf = new ConfigReader(ServerInstance);
138         }
139
140         virtual int OnOperCompare(const std::string &data, const std::string &input, int tagnumber)
141         {
142                 /* First, lets see what hash theyre using on this oper */
143                 std::string hashtype = Conf->ReadValue("oper", "hash", tagnumber);
144                 hashymodules::iterator x = hashers.find(hashtype.c_str());
145
146                 /* Is this a valid hash name? (case insensitive) */
147                 if (x != hashers.end())
148                 {
149                         /* Reset the hashing module */
150                         HashResetRequest(this, x->second).Send();
151                         /* Compare the hash in the config to the generated hash */
152                         if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
153                                 return 1;
154                         /* No match, and must be hashed, forbid */
155                         else return -1;
156                 }
157
158                 /* Not a hash, fall through to strcmp in core */
159                 return 0;
160         }
161
162         virtual Version GetVersion()
163         {
164                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
165         }
166 };
167
168
169 class ModuleOperHashFactory : public ModuleFactory
170 {
171  public:
172         ModuleOperHashFactory()
173         {
174         }
175         
176         ~ModuleOperHashFactory()
177         {
178         }
179         
180         virtual Module * CreateModule(InspIRCd* Me)
181         {
182                 return new ModuleOperHash(Me);
183         }
184         
185 };
186
187
188 extern "C" void * init_module( void )
189 {
190         return new ModuleOperHashFactory;
191 }