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