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