]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_acl.cpp
Expand searching in m_httpd_stats, add global handling of GET parameters (#1566)
[user/henk/code/inspircd.git] / src / modules / m_httpd_acl.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "modules/httpd.h"
23
24 class HTTPACL
25 {
26  public:
27         std::string path;
28         std::string username;
29         std::string password;
30         std::string whitelist;
31         std::string blacklist;
32
33         HTTPACL(const std::string &set_path, const std::string &set_username, const std::string &set_password,
34                 const std::string &set_whitelist, const std::string &set_blacklist)
35                 : path(set_path), username(set_username), password(set_password), whitelist(set_whitelist),
36                 blacklist(set_blacklist) { }
37 };
38
39 class ModuleHTTPAccessList : public Module, public HTTPACLEventListener
40 {
41         std::string stylesheet;
42         std::vector<HTTPACL> acl_list;
43         HTTPdAPI API;
44
45  public:
46         ModuleHTTPAccessList()
47                 : HTTPACLEventListener(this)
48                 , API(this)
49         {
50         }
51
52         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
53         {
54                 std::vector<HTTPACL> new_acls;
55                 ConfigTagList acls = ServerInstance->Config->ConfTags("httpdacl");
56                 for (ConfigIter i = acls.first; i != acls.second; i++)
57                 {
58                         ConfigTag* c = i->second;
59                         std::string path = c->getString("path");
60                         std::string types = c->getString("types");
61                         irc::commasepstream sep(types);
62                         std::string type;
63                         std::string username;
64                         std::string password;
65                         std::string whitelist;
66                         std::string blacklist;
67
68                         while (sep.GetToken(type))
69                         {
70                                 if (stdalgo::string::equalsci(type, "password"))
71                                 {
72                                         username = c->getString("username");
73                                         password = c->getString("password");
74                                 }
75                                 else if (stdalgo::string::equalsci(type, "whitelist"))
76                                 {
77                                         whitelist = c->getString("whitelist");
78                                 }
79                                 else if (stdalgo::string::equalsci(type, "blacklist"))
80                                 {
81                                         blacklist = c->getString("blacklist");
82                                 }
83                                 else
84                                 {
85                                         throw ModuleException("Invalid HTTP ACL type '" + type + "'");
86                                 }
87                         }
88
89                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Read ACL: path=%s pass=%s whitelist=%s blacklist=%s", path.c_str(),
90                                         password.c_str(), whitelist.c_str(), blacklist.c_str());
91
92                         new_acls.push_back(HTTPACL(path, username, password, whitelist, blacklist));
93                 }
94                 acl_list.swap(new_acls);
95         }
96
97         void BlockAccess(HTTPRequest* http, unsigned int returnval, const std::string &extraheaderkey = "", const std::string &extraheaderval="")
98         {
99                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "BlockAccess (%u)", returnval);
100
101                 std::stringstream data("Access to this resource is denied by an access control list. Please contact your IRC administrator.");
102                 HTTPDocumentResponse response(this, *http, &data, returnval);
103                 response.headers.SetHeader("X-Powered-By", MODNAME);
104                 if (!extraheaderkey.empty())
105                         response.headers.SetHeader(extraheaderkey, extraheaderval);
106                 API->SendResponse(response);
107         }
108
109         bool IsAccessAllowed(HTTPRequest* http)
110         {
111                 {
112                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd acl event");
113
114                         for (std::vector<HTTPACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
115                         {
116                                 if (InspIRCd::Match(http->GetPath(), this_acl->path, ascii_case_insensitive_map))
117                                 {
118                                         if (!this_acl->blacklist.empty())
119                                         {
120                                                 /* Blacklist */
121                                                 irc::commasepstream sep(this_acl->blacklist);
122                                                 std::string entry;
123
124                                                 while (sep.GetToken(entry))
125                                                 {
126                                                         if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
127                                                         {
128                                                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Denying access to blacklisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
129                                                                                 http->GetPath().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
130                                                                 BlockAccess(http, 403);
131                                                                 return false;
132                                                         }
133                                                 }
134                                         }
135                                         if (!this_acl->whitelist.empty())
136                                         {
137                                                 /* Whitelist */
138                                                 irc::commasepstream sep(this_acl->whitelist);
139                                                 std::string entry;
140                                                 bool allow_access = false;
141
142                                                 while (sep.GetToken(entry))
143                                                 {
144                                                         if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
145                                                                 allow_access = true;
146                                                 }
147
148                                                 if (!allow_access)
149                                                 {
150                                                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Denying access to whitelisted resource %s (matched by pattern %s) from ip %s (Not in whitelist)",
151                                                                         http->GetPath().c_str(), this_acl->path.c_str(), http->GetIP().c_str());
152                                                         BlockAccess(http, 403);
153                                                         return false;
154                                                 }
155                                         }
156                                         if (!this_acl->password.empty() && !this_acl->username.empty())
157                                         {
158                                                 /* Password auth, first look to see if we have a basic authentication header */
159                                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Checking HTTP auth password for resource %s (matched by pattern %s) from ip %s, against username %s",
160                                                                 http->GetPath().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), this_acl->username.c_str());
161
162                                                 if (http->headers->IsSet("Authorization"))
163                                                 {
164                                                         /* Password has been given, validate it */
165                                                         std::string authorization = http->headers->GetHeader("Authorization");
166                                                         irc::spacesepstream sep(authorization);
167                                                         std::string authtype;
168                                                         std::string base64;
169
170                                                         sep.GetToken(authtype);
171                                                         if (authtype == "Basic")
172                                                         {
173                                                                 std::string user;
174                                                                 std::string pass;
175
176                                                                 sep.GetToken(base64);
177                                                                 std::string userpass = Base64ToBin(base64);
178                                                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: %s (%s)", userpass.c_str(), base64.c_str());
179
180                                                                 irc::sepstream userpasspair(userpass, ':');
181                                                                 if (userpasspair.GetToken(user))
182                                                                 {
183                                                                         userpasspair.GetToken(pass);
184
185                                                                         /* Access granted if username and password are correct */
186                                                                         if (user == this_acl->username && pass == this_acl->password)
187                                                                         {
188                                                                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: password and username match");
189                                                                                 return true;
190                                                                         }
191                                                                         else
192                                                                                 /* Invalid password */
193                                                                                 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
194                                                                 }
195                                                                 else
196                                                                         /* Malformed user:pass pair */
197                                                                         BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
198                                                         }
199                                                         else
200                                                                 /* Unsupported authentication type */
201                                                                 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
202                                                 }
203                                                 else
204                                                 {
205                                                         /* No password given at all, access denied */
206                                                         BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
207                                                 }
208                                                 return false;
209                                         }
210
211                                         /* A path may only match one ACL (the first it finds in the config file) */
212                                         break;
213                                 }
214                         }
215                 }
216                 return true;
217         }
218
219         ModResult OnHTTPACLCheck(HTTPRequest& req) CXX11_OVERRIDE
220         {
221                 if (IsAccessAllowed(&req))
222                         return MOD_RES_PASSTHRU;
223                 return MOD_RES_DENY;
224         }
225
226         Version GetVersion() CXX11_OVERRIDE
227         {
228                 return Version("Provides access control lists (passwording of resources, ip restrictions etc) to m_httpd dependent modules", VF_VENDOR);
229         }
230 };
231
232 MODULE_INIT(ModuleHTTPAccessList)