1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 /* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
26 class AuthQuery : public SQLQuery
29 const std::string uid;
30 LocalIntExt& pendingExt;
32 AuthQuery(Module* me, const std::string& q, const std::string& u, LocalIntExt& e, bool v)
33 : SQLQuery(me, q), uid(u), pendingExt(e), verbose(v)
35 ServerInstance->Logs->Log("m_sqlauth",DEBUG, "SQLAUTH: query=\"%s\"", q.c_str());
38 void OnResult(SQLResult& res)
40 User* user = ServerInstance->FindNick(uid);
45 pendingExt.set(user, AUTH_STATE_NONE);
50 ServerInstance->SNO->WriteGlobalSno('a', "Forbidden connection from %s!%s@%s (SQL query returned no matches)", user->nick.c_str(), user->ident.c_str(), user->host.c_str());
51 pendingExt.set(user, AUTH_STATE_FAIL);
55 void OnError(SQLerror& error)
57 User* user = ServerInstance->FindNick(uid);
60 pendingExt.set(user, AUTH_STATE_FAIL);
62 ServerInstance->SNO->WriteGlobalSno('a', "Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), error.Str());
66 class ModuleSQLAuth : public Module
68 LocalIntExt pendingExt;
69 dynamic_reference<SQLProvider> SQL;
71 std::string freeformquery;
72 std::string killreason;
73 std::string allowpattern;
77 ModuleSQLAuth() : pendingExt("sqlauth-wait", this), SQL(this, "SQL")
83 ServerInstance->Modules->AddService(pendingExt);
85 Implementation eventlist[] = { I_OnUserDisconnect, I_OnCheckReady, I_OnRehash, I_OnUserRegister };
86 ServerInstance->Modules->Attach(eventlist, this, 4);
89 void OnRehash(User* user)
93 SQL.SetProvider("SQL/" + Conf.ReadValue("sqlauth", "dbid", 0)); /* Database ID, given to the SQL service provider */
94 freeformquery = Conf.ReadValue("sqlauth", "query", 0); /* Field name where username can be found */
95 killreason = Conf.ReadValue("sqlauth", "killreason", 0); /* Reason to give when access is denied to a user (put your reg details here) */
96 allowpattern = Conf.ReadValue("sqlauth", "allowpattern",0 ); /* Allow nicks matching this pattern without requiring auth */
97 verbose = Conf.ReadFlag("sqlauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
101 ModResult OnUserRegister(LocalUser* user)
103 // Note this is their initial (unresolved) connect block
104 ConfigTag* tag = user->MyClass->config;
105 if (!tag->getBool("usesqlauth", true))
106 return MOD_RES_PASSTHRU;
108 if (!allowpattern.empty() && InspIRCd::Match(user->nick,allowpattern))
109 return MOD_RES_PASSTHRU;
111 if (pendingExt.get(user))
112 return MOD_RES_PASSTHRU;
114 pendingExt.set(user, AUTH_STATE_BUSY);
117 SQL->PopulateUserInfo(user, userinfo);
118 userinfo["pass"] = user->password;
120 HashProvider* md5 = ServerInstance->Modules->FindDataService<HashProvider>("hash/md5");
122 userinfo["md5pass"] = md5->hexsum(user->password);
124 HashProvider* sha256 = ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256");
126 userinfo["sha256pass"] = sha256->hexsum(user->password);
128 SQL->submit(new AuthQuery(this, SQL->FormatQuery(freeformquery, userinfo), user->uuid, pendingExt, verbose));
130 return MOD_RES_PASSTHRU;
133 ModResult OnCheckReady(LocalUser* user)
135 switch (pendingExt.get(user))
137 case AUTH_STATE_NONE:
138 return MOD_RES_PASSTHRU;
139 case AUTH_STATE_BUSY:
141 case AUTH_STATE_FAIL:
142 ServerInstance->Users->QuitUser(user, killreason);
145 return MOD_RES_PASSTHRU;
150 return Version("Allow/Deny connections based upon an arbitary SQL table", VF_VENDOR);
154 MODULE_INIT(ModuleSQLAuth)