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