]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_password_hash.cpp
Make m_password_hash able to pick up hasher modules after it's loaded, meaning m_md5...
[user/henk/code/inspircd.git] / src / modules / m_password_hash.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 "m_hash.h"
19
20 typedef std::map<irc::string, Module*> hashymodules;
21
22 /* Handle /MKPASSWD
23  */
24 class CommandMkpasswd : public Command
25 {
26         Module* Sender;
27         hashymodules &hashers;
28         std::deque<std::string> &names;
29  public:
30         CommandMkpasswd (InspIRCd* Instance, Module* S, hashymodules &h, std::deque<std::string> &n)
31                 : Command(Instance,"MKPASSWD", 'o', 2), Sender(S), hashers(h), names(n)
32         {
33                 this->source = "m_password_hash.so";
34                 syntax = "<hashtype> <any-text>";
35         }
36
37         void MakeHash(User* user, const char* algo, const char* stuff)
38         {
39                 /* Lets see if they gave us an algorithm which has been implemented */
40                 hashymodules::iterator x = hashers.find(algo);
41                 if (x != hashers.end())
42                 {
43                         /* Yup, reset it first (Always ALWAYS do this) */
44                         HashResetRequest(Sender, x->second).Send();
45                         /* Now attempt to generate a hash */
46                         user->WriteServ("NOTICE %s :%s hashed password for %s is %s",user->nick, algo, stuff, HashSumRequest(Sender, x->second, stuff).Send() );
47                 }
48                 else
49                 {
50                         /* I dont do flying, bob. */
51                         user->WriteServ("NOTICE %s :Unknown hash type, valid hash types are: %s", user->nick, irc::stringjoiner(", ", names, 0, names.size() - 1).GetJoined().c_str() );
52                 }
53         }
54
55         CmdResult Handle (const char** parameters, int pcnt, User *user)
56         {
57                 MakeHash(user, parameters[0], parameters[1]);
58                 /* NOTE: Don't propagate this across the network!
59                  * We dont want plaintext passes going all over the place...
60                  * To make sure it goes nowhere, return CMD_FAILURE!
61                  */
62                 return CMD_FAILURE;
63         }
64 };
65
66 class ModuleOperHash : public Module
67 {
68         
69         CommandMkpasswd* mycommand;
70         hashymodules hashers; /* List of modules which implement HashRequest */
71         std::deque<std::string> names; /* Module names which implement HashRequest */
72
73         bool diduseiface; /* If we've called UseInterface yet. */
74  public:
75
76         ModuleOperHash(InspIRCd* Me)
77                 : Module(Me)
78         {
79                 diduseiface = false;
80
81                 /* Read the config file first */
82 //              Conf = NULL;
83                 OnRehash(NULL,"");
84
85                 /* Find all modules which implement the interface 'HashRequest' */
86                 modulelist* ml = ServerInstance->Modules->FindInterface("HashRequest");
87
88                 /* Did we find any modules? */
89                 if (ml)
90                 {
91                         /* Yes, enumerate them all to find out the hashing algorithm name */
92                         for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
93                         {
94                                 /* Make a request to it for its name, its implementing
95                                  * HashRequest so we know its safe to do this
96                                  */
97                                 std::string name = HashNameRequest(this, *m).Send();
98                                 /* Build a map of them */
99                                 hashers[name.c_str()] = *m;
100                                 names.push_back(name);
101                         }
102                         /* UseInterface doesn't do anything if there are no providers, so we'll have to call it later if a module gets loaded later on. */
103                         ServerInstance->Modules->UseInterface("HashRequest");
104                         diduseiface = true;
105                 }
106
107                 mycommand = new CommandMkpasswd(ServerInstance, this, hashers, names);
108                 ServerInstance->AddCommand(mycommand);
109                 Implementation eventlist[] = { I_OnPassCompare, I_OnLoadModule };
110                 ServerInstance->Modules->Attach(eventlist, this, 2);
111         }
112         
113         virtual ~ModuleOperHash()
114         {
115                 if (diduseiface) ServerInstance->Modules->DoneWithInterface("HashRequest");
116         }
117
118
119         virtual void OnLoadModule(Module* mod, const std::string& name)
120         {
121                 if (ServerInstance->Modules->ModuleHasInterface(mod, "HashRequest"))
122                 {
123                         ServerInstance->Log(DEBUG, "Post-load registering hasher: %s", name.c_str());
124                         std::string name = HashNameRequest(this, mod).Send();
125                         hashers[name.c_str()] = mod;
126                         names.push_back(name);
127                         if (!diduseiface)
128                         {
129                                 ServerInstance->Modules->UseInterface("HashRequest");
130                                 diduseiface = true;
131                         }
132                 }
133         }
134
135         virtual int OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
136         {
137                 /* First, lets see what hash theyre using on this oper */
138                 hashymodules::iterator x = hashers.find(hashtype.c_str());
139
140                 /* Is this a valid hash name? (case insensitive) */
141                 if (x != hashers.end())
142                 {
143                         /* Reset the hashing module */
144                         HashResetRequest(this, x->second).Send();
145                         /* Compare the hash in the config to the generated hash */
146                         if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
147                                 return 1;
148                         /* No match, and must be hashed, forbid */
149                         else return -1;
150                 }
151
152                 /* Not a hash, fall through to strcmp in core */
153                 return 0;
154         }
155
156         virtual Version GetVersion()
157         {
158                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
159         }
160 };
161
162 MODULE_INIT(ModuleOperHash)