]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sqloper.cpp
Tidy up source files:
[user/henk/code/inspircd.git] / src / modules / m_sqloper.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 /* $ModDesc: Allows storage of oper credentials in an SQL table */
25
26 static bool OneOfMatches(const char* host, const char* ip, const std::string& hostlist)
27 {
28         std::stringstream hl(hostlist);
29         std::string xhost;
30         while (hl >> xhost)
31         {
32                 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
33                 {
34                         return true;
35                 }
36         }
37         return false;
38 }
39
40 class OpMeQuery : public SQLQuery
41 {
42  public:
43         const std::string uid, username, password;
44         OpMeQuery(Module* me, const std::string& u, const std::string& un, const std::string& pw)
45                 : SQLQuery(me), uid(u), username(un), password(pw)
46         {
47         }
48
49         void OnResult(SQLResult& res)
50         {
51                 ServerInstance->Logs->Log("m_sqloper",LOG_DEBUG, "SQLOPER: result for %s", uid.c_str());
52                 User* user = ServerInstance->FindNick(uid);
53                 if (!user)
54                         return;
55
56                 // multiple rows may exist
57                 SQLEntries row;
58                 while (res.GetRow(row))
59                 {
60                         if (OperUser(user, row[0], row[1]))
61                                 return;
62                 }
63                 ServerInstance->Logs->Log("m_sqloper",LOG_DEBUG, "SQLOPER: no matches for %s (checked %d rows)", uid.c_str(), res.Rows());
64                 // nobody succeeded... fall back to OPER
65                 fallback();
66         }
67
68         void OnError(SQLerror& error)
69         {
70                 ServerInstance->Logs->Log("m_sqloper",LOG_DEFAULT, "SQLOPER: query failed (%s)", error.Str());
71                 fallback();
72         }
73
74         void fallback()
75         {
76                 User* user = ServerInstance->FindNick(uid);
77                 if (!user)
78                         return;
79
80                 Command* oper_command = ServerInstance->Parser->GetHandler("OPER");
81
82                 if (oper_command)
83                 {
84                         std::vector<std::string> params;
85                         params.push_back(username);
86                         params.push_back(password);
87                         oper_command->Handle(params, user);
88                 }
89                 else
90                 {
91                         ServerInstance->Logs->Log("m_sqloper",LOG_SPARSE, "BUG: WHAT?! Why do we have no OPER command?!");
92                 }
93         }
94
95         bool OperUser(User* user, const std::string &pattern, const std::string &type)
96         {
97                 OperIndex::iterator iter = ServerInstance->Config->oper_blocks.find(" " + type);
98                 if (iter == ServerInstance->Config->oper_blocks.end())
99                 {
100                         ServerInstance->Logs->Log("m_sqloper",LOG_DEFAULT, "SQLOPER: bad type '%s' in returned row for oper %s", type.c_str(), username.c_str());
101                         return false;
102                 }
103                 OperInfo* ifo = iter->second;
104
105                 std::string hostname(user->ident);
106
107                 hostname.append("@").append(user->host);
108
109                 if (OneOfMatches(hostname.c_str(), user->GetIPString().c_str(), pattern.c_str()))
110                 {
111                         /* Opertype and host match, looks like this is it. */
112
113                         user->Oper(ifo);
114                         return true;
115                 }
116
117                 return false;
118         }
119 };
120
121 class ModuleSQLOper : public Module
122 {
123         std::string query;
124         std::string hashtype;
125         dynamic_reference<SQLProvider> SQL;
126
127 public:
128         ModuleSQLOper() : SQL(this, "SQL") {}
129
130         void init()
131         {
132                 OnRehash(NULL);
133
134                 Implementation eventlist[] = { I_OnRehash, I_OnPreCommand };
135                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
136         }
137
138         void OnRehash(User* user)
139         {
140                 ConfigTag* tag = ServerInstance->Config->ConfValue("sqloper");
141
142                 std::string dbid = tag->getString("dbid");
143                 if (dbid.empty())
144                         SQL.SetProvider("SQL");
145                 else
146                         SQL.SetProvider("SQL/" + dbid);
147
148                 hashtype = tag->getString("hash");
149                 query = tag->getString("query", "SELECT hostname as host, type FROM ircd_opers WHERE username='$username' AND password='$password'");
150         }
151
152         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line)
153         {
154                 if (validated && command == "OPER" && parameters.size() >= 2)
155                 {
156                         if (SQL)
157                         {
158                                 LookupOper(user, parameters[0], parameters[1]);
159                                 /* Query is in progress, it will re-invoke OPER if needed */
160                                 return MOD_RES_DENY;
161                         }
162                         ServerInstance->Logs->Log("m_sqloper",LOG_DEFAULT, "SQLOPER: database not present");
163                 }
164                 return MOD_RES_PASSTHRU;
165         }
166
167         void LookupOper(User* user, const std::string &username, const std::string &password)
168         {
169                 HashProvider* hash = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
170
171                 ParamM userinfo;
172                 SQL->PopulateUserInfo(user, userinfo);
173                 userinfo["username"] = username;
174                 userinfo["password"] = hash ? hash->hexsum(password) : password;
175
176                 SQL->submit(new OpMeQuery(this, user->uuid, username, password), query, userinfo);
177         }
178
179         Version GetVersion()
180         {
181                 return Version("Allows storage of oper credentials in an SQL table", VF_VENDOR);
182         }
183 };
184
185 MODULE_INIT(ModuleSQLOper)