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