The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

m_password_hash.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *            the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 /* $ModDesc: Allows for hashed oper passwords */
00015 /* $ModDep: m_hash.h */
00016 
00017 #include "inspircd.h"
00018 #include "m_hash.h"
00019 
00020 typedef std::map<irc::string, Module*> hashymodules;
00021 
00022 /* Handle /MKPASSWD
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                 /* Lets see if they gave us an algorithm which has been implemented */
00040                 hashymodules::iterator x = hashers.find(algo);
00041                 if (x != hashers.end())
00042                 {
00043                         /* Yup, reset it first (Always ALWAYS do this) */
00044                         HashResetRequest(Sender, x->second).Send();
00045                         /* Now attempt to generate a hash */
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                         /* same idea as bug #569 */
00051                         user->WriteServ("NOTICE %s :No hash provider modules are loaded", user->nick.c_str());
00052                 }
00053                 else
00054                 {
00055                         /* I dont do flying, bob. */
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                 /* NOTE: Don't propagate this across the network!
00064                  * We dont want plaintext passes going all over the place...
00065                  * To make sure it goes nowhere, return CMD_FAILURE!
00066                  */
00067                 return CMD_FAILURE;
00068         }
00069 };
00070 
00071 class ModuleOperHash : public Module
00072 {
00073 
00074         CommandMkpasswd* mycommand;
00075         hashymodules hashers; /* List of modules which implement HashRequest */
00076         std::deque<std::string> names; /* Module names which implement HashRequest */
00077 
00078         bool diduseiface; /* If we've called UseInterface yet. */
00079  public:
00080 
00081         ModuleOperHash(InspIRCd* Me)
00082                 : Module(Me)
00083         {
00084                 diduseiface = false;
00085 
00086                 /* Read the config file first */
00087 //              Conf = NULL;
00088                 OnRehash(NULL,"");
00089 
00090                 /* Find all modules which implement the interface 'HashRequest' */
00091                 modulelist* ml = ServerInstance->Modules->FindInterface("HashRequest");
00092 
00093                 /* Did we find any modules? */
00094                 if (ml)
00095                 {
00096                         /* Yes, enumerate them all to find out the hashing algorithm name */
00097                         for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
00098                         {
00099                                 /* Make a request to it for its name, its implementing
00100                                  * HashRequest so we know its safe to do this
00101                                  */
00102                                 std::string name = HashNameRequest(this, *m).Send();
00103                                 /* Build a map of them */
00104                                 hashers[name.c_str()] = *m;
00105                                 names.push_back(name);
00106                         }
00107                         /* 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. */
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                 /* First, lets see what hash theyre using on this oper */
00143                 hashymodules::iterator x = hashers.find(hashtype.c_str());
00144 
00145                 /* Is this a valid hash name? (case insensitive) */
00146                 if (x != hashers.end())
00147                 {
00148                         /* Reset the hashing module */
00149                         HashResetRequest(this, x->second).Send();
00150                         /* Compare the hash in the config to the generated hash */
00151                         if (!strcasecmp(data.c_str(), HashSumRequest(this, x->second, input.c_str()).Send()))
00152                                 return 1;
00153                         /* No match, and must be hashed, forbid */
00154                         else return -1;
00155                 }
00156 
00157                 /* Not a hash, fall through to strcmp in core */
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)