]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_oper_hash.cpp
Fix new millisec /map to compile on windows, by ifndef gettimeofday out reverting...
[user/henk/code/inspircd.git] / src / modules / m_oper_hash.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $ModDesc: Allows for hashed oper passwords */
15 /* $ModDep: m_hash.h */
16
17 #include "inspircd.h"
18 #include "users.h"
19 #include "channels.h"
20 #include "modules.h"
21 #include "m_hash.h"
22
23 typedef std::map<irc::string, Module*> hashymodules;
24
25 /* Handle /MKPASSWD
26  */
27 class cmd_mkpasswd : public command_t
28 {
29         Module* Sender;
30         hashymodules &hashers;
31         std::deque<std::string> &names;
32  public:
33         cmd_mkpasswd (InspIRCd* Instance, Module* S, hashymodules &h, std::deque<std::string> &n)
34                 : command_t(Instance,"MKPASSWD", 'o', 2), Sender(S), hashers(h), names(n)
35         {
36                 this->source = "m_oper_hash.so";
37                 syntax = "<hashtype> <any-text>";
38         }
39
40         void MakeHash(userrec* user, const char* algo, const char* stuff)
41         {
42                 /* Lets see if they gave us an algorithm which has been implemented */
43                 hashymodules::iterator x = hashers.find(algo);
44                 if (x != hashers.end())
45                 {
46                         /* Yup, reset it first (Always ALWAYS do this) */
47                         HashResetRequest(Sender, x->second).Send();
48                         /* Now attempt to generate a hash */
49                         user->WriteServ("NOTICE %s :%s hashed password for %s is %s",user->nick, algo, stuff, HashSumRequest(Sender, x->second, stuff).Send() );
50                 }
51                 else
52                 {
53                         /* I dont do flying, bob. */
54                         user->WriteServ("NOTICE %s :Unknown hash type, valid hash types are: %s", user->nick, irc::stringjoiner(", ", names, 0, names.size() - 1).GetJoined().c_str() );
55                 }
56         }
57
58         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
59         {
60                 MakeHash(user, parameters[0], parameters[1]);
61                 /* NOTE: Don't propogate this across the network!
62                  * We dont want plaintext passes going all over the place...
63                  * To make sure it goes nowhere, return CMD_FAILURE!
64                  */
65                 return CMD_FAILURE;
66         }
67 };
68
69 class ModuleOperHash : public Module
70 {
71         
72         cmd_mkpasswd* mycommand;
73         ConfigReader* Conf;
74         hashymodules hashers; /* List of modules which implement HashRequest */
75         std::deque<std::string> names; /* Module names which implement HashRequest */
76
77  public:
78
79         ModuleOperHash(InspIRCd* Me)
80                 : Module(Me)
81         {
82
83                 /* Read the config file first */
84                 Conf = NULL;
85                 OnRehash(NULL,"");
86
87                 ServerInstance->UseInterface("HashRequest");
88
89                 /* Find all modules which implement the interface 'HashRequest' */
90                 modulelist* ml = ServerInstance->FindInterface("HashRequest");
91
92                 /* Did we find any modules? */
93                 if (ml)
94                 {
95                         /* Yes, enumerate them all to find out the hashing algorithm name */
96                         for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
97                         {
98                                 /* Make a request to it for its name, its implementing
99                                  * HashRequest so we know its safe to do this
100                                  */
101                                 std::string name = HashNameRequest(this, *m).Send();
102                                 /* Build a map of them */
103                                 hashers[name.c_str()] = *m;
104                                 names.push_back(name);
105                         }
106                 }
107                 else
108                 {
109                         throw ModuleException("I can't find any modules loaded which implement the HashRequest interface! You probably forgot to load a hashing module such as m_md5.so or m_sha256.so.");
110                 }
111
112                 mycommand = new cmd_mkpasswd(ServerInstance, this, hashers, names);
113                 ServerInstance->AddCommand(mycommand);
114         }
115         
116         virtual ~ModuleOperHash()
117         {
118                 ServerInstance->DoneWithInterface("HashRequest");
119         }
120
121         void Implements(char* List)
122         {
123                 List[I_OnRehash] = List[I_OnOperCompare] = 1;
124         }
125
126         virtual void OnRehash(userrec* user, const std::string &parameter)
127         {
128                 /* Re-read configuration file */
129                 if (Conf)
130                         delete Conf;
131
132                 Conf = new ConfigReader(ServerInstance);
133         }
134
135         virtual int OnOperCompare(const std::string &data, const std::string &input, int tagnumber)
136         {
137                 /* First, lets see what hash theyre using on this oper */
138                 std::string hashtype = Conf->ReadValue("oper", "hash", tagnumber);
139                 hashymodules::iterator x = hashers.find(hashtype.c_str());
140
141                 /* Is this a valid hash name? (case insensitive) */
142                 if (x != hashers.end())
143                 {
144                         /* Reset the hashing module */
145                         HashResetRequest(this, x->second).Send();
146                         /* Compare the hash in the config to the generated hash */
147                         if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
148                                 return 1;
149                         /* No match, and must be hashed, forbid */
150                         else return -1;
151                 }
152
153                 /* Not a hash, fall through to strcmp in core */
154                 return 0;
155         }
156
157         virtual Version GetVersion()
158         {
159                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
160         }
161 };
162
163 MODULE_INIT(ModuleOperHash)