]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sqlauth.cpp
Allow the maximum length of a chanfilter message to be configured.
[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 SQL::Query
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                 : SQL::Query(me)
39                 , uid(u)
40                 , pendingExt(e)
41                 , verbose(v)
42         {
43         }
44
45         void OnResult(SQL::Result& res) CXX11_OVERRIDE
46         {
47                 User* user = ServerInstance->FindNick(uid);
48                 if (!user)
49                         return;
50                 if (res.Rows())
51                 {
52                         pendingExt.set(user, AUTH_STATE_NONE);
53                 }
54                 else
55                 {
56                         if (verbose)
57                                 ServerInstance->SNO->WriteGlobalSno('a', "Forbidden connection from %s (SQL query returned no matches)", user->GetFullRealHost().c_str());
58                         pendingExt.set(user, AUTH_STATE_FAIL);
59                 }
60         }
61
62         void OnError(SQL::Error& error) CXX11_OVERRIDE
63         {
64                 User* user = ServerInstance->FindNick(uid);
65                 if (!user)
66                         return;
67                 pendingExt.set(user, AUTH_STATE_FAIL);
68                 if (verbose)
69                         ServerInstance->SNO->WriteGlobalSno('a', "Forbidden connection from %s (SQL query failed: %s)", user->GetFullRealHost().c_str(), error.ToString());
70         }
71 };
72
73 class ModuleSQLAuth : public Module
74 {
75         LocalIntExt pendingExt;
76         dynamic_reference<SQL::Provider> SQL;
77
78         std::string freeformquery;
79         std::string killreason;
80         std::string allowpattern;
81         bool verbose;
82
83  public:
84         ModuleSQLAuth()
85                 : pendingExt("sqlauth-wait", ExtensionItem::EXT_USER, this)
86                 , SQL(this, "SQL")
87         {
88         }
89
90         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
91         {
92                 ConfigTag* conf = ServerInstance->Config->ConfValue("sqlauth");
93                 std::string dbid = conf->getString("dbid");
94                 if (dbid.empty())
95                         SQL.SetProvider("SQL");
96                 else
97                         SQL.SetProvider("SQL/" + dbid);
98                 freeformquery = conf->getString("query");
99                 killreason = conf->getString("killreason");
100                 allowpattern = conf->getString("allowpattern");
101                 verbose = conf->getBool("verbose");
102         }
103
104         ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
105         {
106                 // Note this is their initial (unresolved) connect block
107                 ConfigTag* tag = user->MyClass->config;
108                 if (!tag->getBool("usesqlauth", true))
109                         return MOD_RES_PASSTHRU;
110
111                 if (!allowpattern.empty() && InspIRCd::Match(user->nick,allowpattern))
112                         return MOD_RES_PASSTHRU;
113
114                 if (pendingExt.get(user))
115                         return MOD_RES_PASSTHRU;
116
117                 if (!SQL)
118                 {
119                         ServerInstance->SNO->WriteGlobalSno('a', "Forbiding connection from %s (SQL database not present)", user->GetFullRealHost().c_str());
120                         ServerInstance->Users->QuitUser(user, killreason);
121                         return MOD_RES_PASSTHRU;
122                 }
123
124                 pendingExt.set(user, AUTH_STATE_BUSY);
125
126                 SQL::ParamMap userinfo;
127                 SQL::PopulateUserInfo(user, userinfo);
128                 userinfo["pass"] = user->password;
129
130                 HashProvider* md5 = ServerInstance->Modules->FindDataService<HashProvider>("hash/md5");
131                 if (md5)
132                         userinfo["md5pass"] = md5->Generate(user->password);
133
134                 HashProvider* sha256 = ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256");
135                 if (sha256)
136                         userinfo["sha256pass"] = sha256->Generate(user->password);
137
138                 const std::string certfp = SSLClientCert::GetFingerprint(&user->eh);
139                 userinfo["certfp"] = certfp;
140
141                 SQL->Submit(new AuthQuery(this, user->uuid, pendingExt, verbose), freeformquery, userinfo);
142
143                 return MOD_RES_PASSTHRU;
144         }
145
146         ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
147         {
148                 switch (pendingExt.get(user))
149                 {
150                         case AUTH_STATE_NONE:
151                                 return MOD_RES_PASSTHRU;
152                         case AUTH_STATE_BUSY:
153                                 return MOD_RES_DENY;
154                         case AUTH_STATE_FAIL:
155                                 ServerInstance->Users->QuitUser(user, killreason);
156                                 return MOD_RES_DENY;
157                 }
158                 return MOD_RES_PASSTHRU;
159         }
160
161         Version GetVersion() CXX11_OVERRIDE
162         {
163                 return Version("Allow/Deny connections based upon an arbitrary SQL table", VF_VENDOR);
164         }
165 };
166
167 MODULE_INIT(ModuleSQLAuth)