]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sqlauth.cpp
Remove dummy API_VERSION from Version constructor
[user/henk/code/inspircd.git] / src / modules / m_sqlauth.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "m_sqlv2.h"
16 #include "m_sqlutils.h"
17 #include "m_hash.h"
18
19 /* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
20 /* $ModDep: m_sqlv2.h m_sqlutils.h m_hash.h */
21
22 class ModuleSQLAuth : public Module
23 {
24         LocalIntExt sqlAuthed;
25         Module* SQLutils;
26         Module* SQLprovider;
27
28         std::string freeformquery;
29         std::string killreason;
30         std::string allowpattern;
31         std::string databaseid;
32
33         bool verbose;
34
35 public:
36         ModuleSQLAuth() : sqlAuthed("sqlauth", this)
37         {
38                 ServerInstance->Modules->UseInterface("SQLutils");
39                 ServerInstance->Modules->UseInterface("SQL");
40
41                 SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
42                 if (!SQLutils)
43                         throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
44
45                 SQLprovider = ServerInstance->Modules->FindFeature("SQL");
46                 if (!SQLprovider)
47                         throw ModuleException("Can't find an SQL provider module. Please load one before attempting to load m_sqlauth.");
48
49                 OnRehash(NULL);
50                 Implementation eventlist[] = { I_OnUserDisconnect, I_OnCheckReady, I_OnRehash, I_OnUserRegister };
51                 ServerInstance->Modules->Attach(eventlist, this, 4);
52         }
53
54         virtual ~ModuleSQLAuth()
55         {
56                 ServerInstance->Modules->DoneWithInterface("SQL");
57                 ServerInstance->Modules->DoneWithInterface("SQLutils");
58         }
59
60
61         void OnRehash(User* user)
62         {
63                 ConfigReader Conf;
64
65                 databaseid      = Conf.ReadValue("sqlauth", "dbid", 0);                 /* Database ID, given to the SQL service provider */
66                 freeformquery   = Conf.ReadValue("sqlauth", "query", 0);        /* Field name where username can be found */
67                 killreason      = Conf.ReadValue("sqlauth", "killreason", 0);   /* Reason to give when access is denied to a user (put your reg details here) */
68                 allowpattern    = Conf.ReadValue("sqlauth", "allowpattern",0 ); /* Allow nicks matching this pattern without requiring auth */
69                 verbose         = Conf.ReadFlag("sqlauth", "verbose", 0);               /* Set to true if failed connects should be reported to operators */
70         }
71
72         ModResult OnUserRegister(User* user)
73         {
74                 if ((!allowpattern.empty()) && (InspIRCd::Match(user->nick,allowpattern)))
75                 {
76                         sqlAuthed.set(user, 1);
77                         return MOD_RES_PASSTHRU;
78                 }
79
80                 if (!CheckCredentials(user))
81                 {
82                         ServerInstance->Users->QuitUser(user, killreason);
83                         return MOD_RES_DENY;
84                 }
85                 return MOD_RES_PASSTHRU;
86         }
87
88         bool CheckCredentials(User* user)
89         {
90                 std::string thisquery = freeformquery;
91                 std::string safepass = user->password;
92                 std::string safegecos = user->fullname;
93
94                 /* Search and replace the escaped nick and escaped pass into the query */
95
96                 SearchAndReplace(safepass, std::string("\""), std::string("\\\""));
97                 SearchAndReplace(safegecos, std::string("\""), std::string("\\\""));
98
99                 SearchAndReplace(thisquery, std::string("$nick"), user->nick);
100                 SearchAndReplace(thisquery, std::string("$pass"), safepass);
101                 SearchAndReplace(thisquery, std::string("$host"), user->host);
102                 SearchAndReplace(thisquery, std::string("$ip"), std::string(user->GetIPString()));
103                 SearchAndReplace(thisquery, std::string("$gecos"), safegecos);
104                 SearchAndReplace(thisquery, std::string("$ident"), user->ident);
105                 SearchAndReplace(thisquery, std::string("$server"), std::string(user->server));
106                 SearchAndReplace(thisquery, std::string("$uuid"), user->uuid);
107
108                 Module* HashMod = ServerInstance->Modules->Find("m_md5.so");
109
110                 if (HashMod)
111                 {
112                         SearchAndReplace(thisquery, std::string("$md5pass"), HashRequest(this, HashMod, user->password).hex());
113                 }
114
115                 HashMod = ServerInstance->Modules->Find("m_sha256.so");
116
117                 if (HashMod)
118                 {
119                         SearchAndReplace(thisquery, std::string("$sha256pass"), HashRequest(this, HashMod, user->password).hex());
120                 }
121
122                 /* Build the query */
123                 SQLrequest req = SQLrequest(this, SQLprovider, databaseid, SQLquery(thisquery));
124
125                 req.Send();
126                 /* When we get the query response from the service provider we will be given an ID to play with,
127                  * just an ID number which is unique to this query. We need a way of associating that ID with a User
128                  * so we insert it into a map mapping the IDs to users.
129                  * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
130                  * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
131                  * us to discard the query.
132                  */
133                 AssociateUser(this, SQLutils, req.id, user).Send();
134
135                 return true;
136         }
137
138         void OnRequest(Request& request)
139         {
140                 if(strcmp(SQLRESID, request.id) == 0)
141                 {
142                         SQLresult* res = static_cast<SQLresult*>(&request);
143
144                         User* user = GetAssocUser(this, SQLutils, res->id).S().user;
145                         UnAssociate(this, SQLutils, res->id).S();
146
147                         if(user)
148                         {
149                                 if(res->error.Id() == SQL_NO_ERROR)
150                                 {
151                                         if(res->Rows())
152                                         {
153                                                 /* We got a row in the result, this is enough really */
154                                                 sqlAuthed.set(user, 1);
155                                         }
156                                         else if (verbose)
157                                         {
158                                                 /* No rows in result, this means there was no record matching the user */
159                                                 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());
160                                         }
161                                 }
162                                 else if (verbose)
163                                 {
164                                         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(), res->error.Str());
165                                 }
166                         }
167                         else
168                         {
169                                 return;
170                         }
171
172                         if (!sqlAuthed.get(user))
173                         {
174                                 ServerInstance->Users->QuitUser(user, killreason);
175                         }
176                 }
177         }
178
179         ModResult OnCheckReady(User* user)
180         {
181                 return sqlAuthed.get(user) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
182         }
183
184         Version GetVersion()
185         {
186                 return Version("Allow/Deny connections based upon an arbitary SQL table", VF_VENDOR);
187         }
188
189 };
190
191 MODULE_INIT(ModuleSQLAuth)