00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "inspircd.h"
00018 #include "m_hash.h"
00019
00020 typedef std::map<irc::string, Module*> hashymodules;
00021
00022
00023
00024 class CommandMkpasswd : public Command
00025 {
00026 Module* Sender;
00027 hashymodules &hashers;
00028 std::deque<std::string> &names;
00029 public:
00030 CommandMkpasswd (InspIRCd* Instance, Module* S, hashymodules &h, std::deque<std::string> &n)
00031 : Command(Instance,"MKPASSWD", "o", 2), Sender(S), hashers(h), names(n)
00032 {
00033 this->source = "m_password_hash.so";
00034 syntax = "<hashtype> <any-text>";
00035 }
00036
00037 void MakeHash(User* user, const char* algo, const char* stuff)
00038 {
00039
00040 hashymodules::iterator x = hashers.find(algo);
00041 if (x != hashers.end())
00042 {
00043
00044 HashResetRequest(Sender, x->second).Send();
00045
00046 user->WriteServ("NOTICE %s :%s hashed password for %s is %s",user->nick.c_str(), algo, stuff, HashSumRequest(Sender, x->second, stuff).Send() );
00047 }
00048 else if (names.empty())
00049 {
00050
00051 user->WriteServ("NOTICE %s :No hash provider modules are loaded", user->nick.c_str());
00052 }
00053 else
00054 {
00055
00056 user->WriteServ("NOTICE %s :Unknown hash type, valid hash types are: %s", user->nick.c_str(), irc::stringjoiner(", ", names, 0, names.size() - 1).GetJoined().c_str() );
00057 }
00058 }
00059
00060 CmdResult Handle (const std::vector<std::string>& parameters, User *user)
00061 {
00062 MakeHash(user, parameters[0].c_str(), parameters[1].c_str());
00063
00064
00065
00066
00067 return CMD_FAILURE;
00068 }
00069 };
00070
00071 class ModuleOperHash : public Module
00072 {
00073
00074 CommandMkpasswd* mycommand;
00075 hashymodules hashers;
00076 std::deque<std::string> names;
00077
00078 bool diduseiface;
00079 public:
00080
00081 ModuleOperHash(InspIRCd* Me)
00082 : Module(Me)
00083 {
00084 diduseiface = false;
00085
00086
00087
00088 OnRehash(NULL,"");
00089
00090
00091 modulelist* ml = ServerInstance->Modules->FindInterface("HashRequest");
00092
00093
00094 if (ml)
00095 {
00096
00097 for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
00098 {
00099
00100
00101
00102 std::string name = HashNameRequest(this, *m).Send();
00103
00104 hashers[name.c_str()] = *m;
00105 names.push_back(name);
00106 }
00107
00108 ServerInstance->Modules->UseInterface("HashRequest");
00109 diduseiface = true;
00110 }
00111
00112 mycommand = new CommandMkpasswd(ServerInstance, this, hashers, names);
00113 ServerInstance->AddCommand(mycommand);
00114 Implementation eventlist[] = { I_OnPassCompare, I_OnLoadModule };
00115 ServerInstance->Modules->Attach(eventlist, this, 2);
00116 }
00117
00118 virtual ~ModuleOperHash()
00119 {
00120 if (diduseiface) ServerInstance->Modules->DoneWithInterface("HashRequest");
00121 }
00122
00123
00124 virtual void OnLoadModule(Module* mod, const std::string& name)
00125 {
00126 if (ServerInstance->Modules->ModuleHasInterface(mod, "HashRequest"))
00127 {
00128 ServerInstance->Logs->Log("m_password-hash",DEBUG, "Post-load registering hasher: %s", name.c_str());
00129 std::string sname = HashNameRequest(this, mod).Send();
00130 hashers[sname.c_str()] = mod;
00131 names.push_back(sname);
00132 if (!diduseiface)
00133 {
00134 ServerInstance->Modules->UseInterface("HashRequest");
00135 diduseiface = true;
00136 }
00137 }
00138 }
00139
00140 virtual int OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
00141 {
00142
00143 hashymodules::iterator x = hashers.find(hashtype.c_str());
00144
00145
00146 if (x != hashers.end())
00147 {
00148
00149 HashResetRequest(this, x->second).Send();
00150
00151 if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
00152 return 1;
00153
00154 else return -1;
00155 }
00156
00157
00158 return 0;
00159 }
00160
00161 virtual Version GetVersion()
00162 {
00163 return Version("$Id: m_password_hash.cpp 10291 2008-08-25 20:35:51Z w00t $",VF_VENDOR,API_VERSION);
00164 }
00165 };
00166
00167 MODULE_INIT(ModuleOperHash)