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