]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_oper_hash.cpp
d74939a5dc7f90d67def2527dc25d6934186ad21
[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_md5.h m_sha256.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_md5.h"
29 #include "m_sha256.h"
30
31 enum ProviderTypes
32 {
33         PROV_MD5 = 1,
34         PROV_SHA = 2
35 };
36
37 /* Handle /MKPASSWD
38  */
39 class cmd_mkpasswd : public command_t
40 {
41         Module* MD5Provider;
42         Module* SHAProvider;
43         Module* Sender;
44         int Prov;
45  public:
46         cmd_mkpasswd (InspIRCd* Instance, Module* Sender, Module* MD5Hasher, Module* SHAHasher, int P)
47                 : command_t(Instance,"MKPASSWD", 'o', 2), MD5Provider(MD5Hasher), SHAProvider(SHAHasher), Prov(P)
48         {
49                 this->source = "m_oper_hash.so";
50                 syntax = "<hashtype> <any-text>";
51         }
52
53         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
54         {
55                 if (!strcasecmp(parameters[0], "md5"))
56                 {
57                         if ((Prov & PROV_MD5) > 0)
58                         {
59                                 MD5ResetRequest(Sender, MD5Provider).Send();
60                                 user->WriteServ("NOTICE %s :MD5 hashed password for %s is %s",user->nick, parameters[1], MD5SumRequest(Sender, MD5Provider, parameters[1]).Send() );
61                         }
62                         else
63                         {
64                                 user->WriteServ("NOTICE %s :MD5 hashing is not available (m_md5.so not loaded)");
65                         }
66                 }
67                 else if (!strcasecmp(parameters[0], "sha256"))
68                 {
69                         if ((Prov & PROV_SHA) > 0)
70                         {
71                                 SHA256ResetRequest(Sender, SHAProvider).Send();
72                                 user->WriteServ("NOTICE %s :SHA256 hashed password for %s is %s",user->nick, parameters[1], SHA256SumRequest(Sender, SHAProvider, parameters[1]).Send() );
73                         }
74                         else
75                         {
76                                 user->WriteServ("NOTICE %s :SHA256 hashing is not available (m_sha256.so not loaded)");
77                         }
78                 }
79                 else
80                 {
81                         user->WriteServ("NOTICE %s :Unknown hash type, valid hash types are:%s%s", (Prov & PROV_MD5) > 0 ? " MD5" : "", (Prov & PROV_SHA) > 0 ? " SHA256" : "");
82                 }
83
84                 /* NOTE: Don't propogate this across the network!
85                  * We dont want plaintext passes going all over the place...
86                  * To make sure it goes nowhere, return CMD_FAILURE!
87                  */
88                 return CMD_FAILURE;
89         }
90 };
91
92 class ModuleOperHash : public Module
93 {
94         
95         cmd_mkpasswd* mycommand;
96         Module* MD5Provider;
97         Module* SHAProvider;
98         std::string providername;
99         int ID;
100         ConfigReader* Conf;
101
102  public:
103
104         ModuleOperHash(InspIRCd* Me)
105                 : Module::Module(Me)
106         {
107                 Conf = NULL;
108                 OnRehash("");
109
110                 /* Try to find the md5 service provider, bail if it can't be found */
111                 MD5Provider = ServerInstance->FindModule("m_md5.so");
112                 if (MD5Provider)
113                         ID |= PROV_MD5;
114
115                 SHAProvider = ServerInstance->FindModule("m_sha256.so");
116                 if (SHAProvider)
117                         ID |= PROV_SHA;
118
119                 mycommand = new cmd_mkpasswd(ServerInstance, this, MD5Provider, SHAProvider, ID);
120                 ServerInstance->AddCommand(mycommand);
121         }
122         
123         virtual ~ModuleOperHash()
124         {
125         }
126
127         void Implements(char* List)
128         {
129                 List[I_OnRehash] = List[I_OnOperCompare] = 1;
130         }
131
132         virtual void OnRehash(const std::string &parameter)
133         {
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                 std::string hashtype = Conf->ReadValue("oper", "hash", tagnumber);
143                 if ((hashtype == "sha256") && (data.length() == SHA256_BLOCK_SIZE) && ((ID & PROV_SHA) > 0))
144                 {
145                         SHA256ResetRequest(this, SHAProvider).Send();
146                         if (!strcasecmp(data.c_str(), SHA256SumRequest(this, SHAProvider, input.c_str()).Send()))
147                                 return 1;
148                         else return -1;
149                 }
150                 else if ((hashtype == "md5") && (data.length() == 32) && ((ID & PROV_MD5) > 0))
151                 {
152                         MD5ResetRequest(this, MD5Provider).Send();
153                         if (!strcasecmp(data.c_str(), MD5SumRequest(this, MD5Provider, input.c_str()).Send()))
154                                 return 1;
155                         else return -1;
156                 }
157                 return 0;
158         }
159         
160         virtual Version GetVersion()
161         {
162                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
163         }
164 };
165
166
167 class ModuleOperHashFactory : public ModuleFactory
168 {
169  public:
170         ModuleOperHashFactory()
171         {
172         }
173         
174         ~ModuleOperHashFactory()
175         {
176         }
177         
178         virtual Module * CreateModule(InspIRCd* Me)
179         {
180                 return new ModuleOperHash(Me);
181         }
182         
183 };
184
185
186 extern "C" void * init_module( void )
187 {
188         return new ModuleOperHashFactory;
189 }