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