]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sqlauth.cpp
Merge v2.0.23 and v2.0.24 into master.
[user/henk/code/inspircd.git] / src / modules / m_sqlauth.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "modules/sql.h"
22 #include "modules/hash.h"
23 #include "modules/ssl.h"
24
25 enum AuthState {
26         AUTH_STATE_NONE = 0,
27         AUTH_STATE_BUSY = 1,
28         AUTH_STATE_FAIL = 2
29 };
30
31 class AuthQuery : public SQLQuery
32 {
33  public:
34         const std::string uid;
35         LocalIntExt& pendingExt;
36         bool verbose;
37         AuthQuery(Module* me, const std::string& u, LocalIntExt& e, bool v)
38                 : SQLQuery(me), uid(u), pendingExt(e), verbose(v)
39         {
40         }
41
42         void OnResult(SQLResult& res) CXX11_OVERRIDE
43         {
44                 User* user = ServerInstance->FindNick(uid);
45                 if (!user)
46                         return;
47                 if (res.Rows())
48                 {
49                         pendingExt.set(user, AUTH_STATE_NONE);
50                 }
51                 else
52                 {
53                         if (verbose)
54                                 ServerInstance->SNO->WriteGlobalSno('a', "Forbidden connection from %s (SQL query returned no matches)", user->GetFullRealHost().c_str());
55                         pendingExt.set(user, AUTH_STATE_FAIL);
56                 }
57         }
58
59         void OnError(SQLerror& error) CXX11_OVERRIDE
60         {
61                 User* user = ServerInstance->FindNick(uid);
62                 if (!user)
63                         return;
64                 pendingExt.set(user, AUTH_STATE_FAIL);
65                 if (verbose)
66                         ServerInstance->SNO->WriteGlobalSno('a', "Forbidden connection from %s (SQL query failed: %s)", user->GetFullRealHost().c_str(), error.Str());
67         }
68 };
69
70 class ModuleSQLAuth : public Module
71 {
72         LocalIntExt pendingExt;
73         dynamic_reference<SQLProvider> SQL;
74
75         std::string freeformquery;
76         std::string killreason;
77         std::string allowpattern;
78         bool verbose;
79
80  public:
81         ModuleSQLAuth()
82                 : pendingExt("sqlauth-wait", ExtensionItem::EXT_USER, this)
83                 , SQL(this, "SQL")
84         {
85         }
86
87         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
88         {
89                 ConfigTag* conf = ServerInstance->Config->ConfValue("sqlauth");
90                 std::string dbid = conf->getString("dbid");
91                 if (dbid.empty())
92                         SQL.SetProvider("SQL");
93                 else
94                         SQL.SetProvider("SQL/" + dbid);
95                 freeformquery = conf->getString("query");
96                 killreason = conf->getString("killreason");
97                 allowpattern = conf->getString("allowpattern");
98                 verbose = conf->getBool("verbose");
99         }
100
101         ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
102         {
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;
107
108                 if (!allowpattern.empty() && InspIRCd::Match(user->nick,allowpattern))
109                         return MOD_RES_PASSTHRU;
110
111                 if (pendingExt.get(user))
112                         return MOD_RES_PASSTHRU;
113
114                 if (!SQL)
115                 {
116                         ServerInstance->SNO->WriteGlobalSno('a', "Forbiding connection from %s (SQL database not present)", user->GetFullRealHost().c_str());
117                         ServerInstance->Users->QuitUser(user, killreason);
118                         return MOD_RES_PASSTHRU;
119                 }
120
121                 pendingExt.set(user, AUTH_STATE_BUSY);
122
123                 ParamM userinfo;
124                 SQL->PopulateUserInfo(user, userinfo);
125                 userinfo["pass"] = user->password;
126
127                 HashProvider* md5 = ServerInstance->Modules->FindDataService<HashProvider>("hash/md5");
128                 if (md5)
129                         userinfo["md5pass"] = md5->Generate(user->password);
130
131                 HashProvider* sha256 = ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256");
132                 if (sha256)
133                         userinfo["sha256pass"] = sha256->Generate(user->password);
134
135                 const std::string certfp = SSLClientCert::GetFingerprint(&user->eh);
136                 userinfo["certfp"] = certfp;
137
138                 SQL->submit(new AuthQuery(this, user->uuid, pendingExt, verbose), freeformquery, userinfo);
139
140                 return MOD_RES_PASSTHRU;
141         }
142
143         ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
144         {
145                 switch (pendingExt.get(user))
146                 {
147                         case AUTH_STATE_NONE:
148                                 return MOD_RES_PASSTHRU;
149                         case AUTH_STATE_BUSY:
150                                 return MOD_RES_DENY;
151                         case AUTH_STATE_FAIL:
152                                 ServerInstance->Users->QuitUser(user, killreason);
153                                 return MOD_RES_DENY;
154                 }
155                 return MOD_RES_PASSTHRU;
156         }
157
158         Version GetVersion() CXX11_OVERRIDE
159         {
160                 return Version("Allow/Deny connections based upon an arbitrary SQL table", VF_VENDOR);
161         }
162 };
163
164 MODULE_INIT(ModuleSQLAuth)