]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_oper_hash.cpp
812228ca5879fba5e027aeaa3c9aa5c44fbe6b6e
[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 ProviderType
32 {
33         PROV_MD5,
34         PROV_SHA
35 };
36
37 /* Handle /MKPASSWD
38  */
39 class cmd_mkpasswd : public command_t
40 {
41         Module* Provider;
42         Module* Sender;
43         ProviderType Prov;
44  public:
45         cmd_mkpasswd (InspIRCd* Instance, Module* Sender, Module* Hasher, ProviderType P) : command_t(Instance,"MKPASSWD", 'o', 1), Provider(Hasher), Prov(P)
46         {
47                 this->source = "m_oper_hash.so";
48                 syntax = "<any-text>";
49         }
50
51         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
52         {
53                 if (Prov == PROV_MD5)
54                 {
55                         MD5ResetRequest(Sender, Provider).Send();
56                         user->WriteServ("NOTICE %s :MD5 hashed password for %s is %s",user->nick,parameters[0], MD5SumRequest(Sender, Provider, parameters[0]).Send() );
57                 }
58                 else
59                 {
60                         SHA256ResetRequest(Sender, Provider).Send();
61                         user->WriteServ("NOTICE %s :SHA256 hashed password for %s is %s",user->nick,parameters[0], SHA256SumRequest(Sender, Provider, parameters[0]).Send() );
62                 }
63
64                 return CMD_SUCCESS;
65         }
66 };
67
68 class ModuleOperHash : public Module
69 {
70         
71         cmd_mkpasswd* mycommand;
72         Module* Provider;
73         std::string providername;
74         ProviderType ID;
75
76  public:
77
78         ModuleOperHash(InspIRCd* Me)
79                 : Module::Module(Me)
80         {
81                 ConfigReader Conf(ServerInstance);
82                 providername = Conf.ReadValue("operhash","algorithm",0);
83
84                 if (providername.empty())
85                         providername = "md5";
86
87                 if (providername == "md5")
88                         ID = PROV_MD5;
89                 else
90                         ID = PROV_SHA;
91
92                 /* Try to find the md5 service provider, bail if it can't be found */
93                 Provider = ServerInstance->FindModule(std::string("m_") + providername + ".so");
94                 if (!Provider)
95                         throw ModuleException(std::string("Can't find m_") + providername + ".so. Please load m_" + providername + ".so before m_oper_hash.so.");
96
97                 mycommand = new cmd_mkpasswd(ServerInstance, this, Provider, ID);
98                 ServerInstance->AddCommand(mycommand);
99         }
100         
101         virtual ~ModuleOperHash()
102         {
103         }
104
105         void Implements(char* List)
106         {
107                 List[I_OnOperCompare] = 1;
108         }
109
110         virtual int OnOperCompare(const std::string &data, const std::string &input)
111         {
112                 /* always always reset first */
113                 if (ID == PROV_MD5)
114                 {
115                         MD5ResetRequest(this, Provider).Send();
116                         if (data.length() == 32) // if its 32 chars long, try it as an md5
117                         {
118                                 /* Does it match the md5 sum? */
119                                 if (!strcasecmp(data.c_str(), MD5SumRequest(this, Provider, input.c_str()).Send()))
120                                 {
121                                         return 1;
122                                 }
123                                 else return 0;
124                         }
125                 }
126                 else
127                 {
128                         SHA256ResetRequest(this, Provider).Send();
129                         if (data.length() == SHA256_BLOCK_SIZE)
130                         {
131                                 if (!strcasecmp(data.c_str(), SHA256SumRequest(this, Provider, input.c_str()).Send()))
132                                 {
133                                         return 1;
134                                 }
135                                 else return 0;
136                         }
137                 }
138                 return 0;
139         }
140         
141         virtual Version GetVersion()
142         {
143                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
144         }
145 };
146
147
148 class ModuleOperHashFactory : public ModuleFactory
149 {
150  public:
151         ModuleOperHashFactory()
152         {
153         }
154         
155         ~ModuleOperHashFactory()
156         {
157         }
158         
159         virtual Module * CreateModule(InspIRCd* Me)
160         {
161                 return new ModuleOperHash(Me);
162         }
163         
164 };
165
166
167 extern "C" void * init_module( void )
168 {
169         return new ModuleOperHashFactory;
170 }