]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_oper_hash.cpp
036bbed1b4920677747bc7a95fa82f3c8f5da325
[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                 std::map<irc::string, Module*>::iterator x = hashers.find(algo);
48                 if (x != hashers.end())
49                 {
50                         HashResetRequest(Sender, x->second).Send();
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                         user->WriteServ("NOTICE %s :Unknown hash type, valid hash types are: %s", user->nick, irc::stringjoiner(",", names, 0, names.size() - 1).GetJoined().c_str() );
56                 }
57         }
58
59         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
60         {
61                 MakeHash(user, parameters[0], parameters[1]);
62                 /* NOTE: Don't propogate this across the network!
63                  * We dont want plaintext passes going all over the place...
64                  * To make sure it goes nowhere, return CMD_FAILURE!
65                  */
66                 return CMD_FAILURE;
67         }
68 };
69
70 class ModuleOperHash : public Module
71 {
72         
73         cmd_mkpasswd* mycommand;
74         ConfigReader* Conf;
75         std::map<irc::string, Module*> hashers;
76         std::deque<std::string> names;
77         modulelist* ml;
78
79  public:
80
81         ModuleOperHash(InspIRCd* Me)
82                 : Module::Module(Me)
83         {
84                 Conf = NULL;
85                 OnRehash("");
86
87                 modulelist* ml = ServerInstance->FindInterface("HashRequest");
88
89                 if (ml)
90                 {
91                         ServerInstance->Log(DEBUG, "Found interface 'HashRequest' containing %d modules", ml->size());
92
93                         for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
94                         {
95                                 std::string name = HashNameRequest(this, *m).Send();
96                                 hashers[name.c_str()] = *m;
97                                 names.push_back(name);
98                                 ServerInstance->Log(DEBUG, "Found HashRequest interface: '%s' -> '%08x'", name.c_str(), *m);
99                         }
100                 }
101
102                 mycommand = new cmd_mkpasswd(ServerInstance, this, hashers, names);
103                 ServerInstance->AddCommand(mycommand);
104         }
105         
106         virtual ~ModuleOperHash()
107         {
108         }
109
110         void Implements(char* List)
111         {
112                 List[I_OnRehash] = List[I_OnOperCompare] = 1;
113         }
114
115         virtual void OnRehash(const std::string &parameter)
116         {
117                 if (Conf)
118                         delete Conf;
119
120                 Conf = new ConfigReader(ServerInstance);
121         }
122
123         virtual int OnOperCompare(const std::string &data, const std::string &input, int tagnumber)
124         {
125                 std::string hashtype = Conf->ReadValue("oper", "hash", tagnumber);
126                 std::map<irc::string, Module*>::iterator x = hashers.find(hashtype.c_str());
127
128                 if (x != hashers.end())
129                 {
130                         HashResetRequest(this, x->second).Send();
131                         if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
132                                 return 1;
133                         else return -1;
134                 }
135
136                 return 0;
137         }
138
139         virtual Version GetVersion()
140         {
141                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
142         }
143 };
144
145
146 class ModuleOperHashFactory : public ModuleFactory
147 {
148  public:
149         ModuleOperHashFactory()
150         {
151         }
152         
153         ~ModuleOperHashFactory()
154         {
155         }
156         
157         virtual Module * CreateModule(InspIRCd* Me)
158         {
159                 return new ModuleOperHash(Me);
160         }
161         
162 };
163
164
165 extern "C" void * init_module( void )
166 {
167         return new ModuleOperHashFactory;
168 }