]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_acl.cpp
Change the API of m_httpd to be dynamic_reference-based
[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 #include "protocol.h"
24
25 /* $ModDesc: Provides access control lists (passwording of resources, ip restrictions etc) to m_httpd.so dependent modules */
26
27 class HTTPACL
28 {
29  public:
30         std::string path;
31         std::string username;
32         std::string password;
33         std::string whitelist;
34         std::string blacklist;
35
36         HTTPACL(const std::string &set_path, const std::string &set_username, const std::string &set_password,
37                 const std::string &set_whitelist, const std::string &set_blacklist)
38                 : path(set_path), username(set_username), password(set_password), whitelist(set_whitelist),
39                 blacklist(set_blacklist) { }
40 };
41
42 class ModuleHTTPAccessList : public Module
43 {
44         std::string stylesheet;
45         std::vector<HTTPACL> acl_list;
46         HTTPdAPI API;
47
48  public:
49         ModuleHTTPAccessList()
50                 : API(this)
51         {
52         }
53
54         void OnRehash(User* user)
55         {
56                 acl_list.clear();
57                 ConfigTagList acls = ServerInstance->Config->ConfTags("httpdacl");
58                 for (ConfigIter i = acls.first; i != acls.second; i++)
59                 {
60                         ConfigTag* c = i->second;
61                         std::string path = c->getString("path");
62                         std::string types = c->getString("types");
63                         irc::commasepstream sep(types);
64                         std::string type;
65                         std::string username;
66                         std::string password;
67                         std::string whitelist;
68                         std::string blacklist;
69
70                         while (sep.GetToken(type))
71                         {
72                                 if (type == "password")
73                                 {
74                                         username = c->getString("username");
75                                         password = c->getString("password");
76                                 }
77                                 else if (type == "whitelist")
78                                 {
79                                         whitelist = c->getString("whitelist");
80                                 }
81                                 else if (type == "blacklist")
82                                 {
83                                         blacklist = c->getString("blacklist");
84                                 }
85                                 else
86                                 {
87                                         throw ModuleException("Invalid HTTP ACL type '" + type + "'");
88                                 }
89                         }
90
91                         ServerInstance->Logs->Log("m_httpd_acl", LOG_DEBUG, "Read ACL: path=%s pass=%s whitelist=%s blacklist=%s", path.c_str(),
92                                         password.c_str(), whitelist.c_str(), blacklist.c_str());
93
94                         acl_list.push_back(HTTPACL(path, username, password, whitelist, blacklist));
95                 }
96         }
97
98         void init() CXX11_OVERRIDE
99         {
100                 OnRehash(NULL);
101                 Implementation eventlist[] = { I_OnEvent, I_OnRehash };
102                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
103         }
104
105         void BlockAccess(HTTPRequest* http, int returnval, const std::string &extraheaderkey = "", const std::string &extraheaderval="")
106         {
107                 ServerInstance->Logs->Log("m_httpd_acl", LOG_DEBUG, "BlockAccess (%d)", returnval);
108
109                 std::stringstream data("Access to this resource is denied by an access control list. Please contact your IRC administrator.");
110                 HTTPDocumentResponse response(this, *http, &data, returnval);
111                 response.headers.SetHeader("X-Powered-By", "m_httpd_acl.so");
112                 if (!extraheaderkey.empty())
113                         response.headers.SetHeader(extraheaderkey, extraheaderval);
114                 API->SendResponse(response);
115         }
116
117         void OnEvent(Event& event) CXX11_OVERRIDE
118         {
119                 if (event.id == "httpd_acl")
120                 {
121                         ServerInstance->Logs->Log("m_http_stats", LOG_DEBUG, "Handling httpd acl event");
122                         HTTPRequest* http = (HTTPRequest*)&event;
123
124                         for (std::vector<HTTPACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
125                         {
126                                 if (InspIRCd::Match(http->GetURI(), this_acl->path, ascii_case_insensitive_map))
127                                 {
128                                         if (!this_acl->blacklist.empty())
129                                         {
130                                                 /* Blacklist */
131                                                 irc::commasepstream sep(this_acl->blacklist);
132                                                 std::string entry;
133
134                                                 while (sep.GetToken(entry))
135                                                 {
136                                                         if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
137                                                         {
138                                                                 ServerInstance->Logs->Log("m_httpd_acl", LOG_DEBUG, "Denying access to blacklisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
139                                                                                 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
140                                                                 BlockAccess(http, 403);
141                                                                 return;
142                                                         }
143                                                 }
144                                         }
145                                         if (!this_acl->whitelist.empty())
146                                         {
147                                                 /* Whitelist */
148                                                 irc::commasepstream sep(this_acl->whitelist);
149                                                 std::string entry;
150                                                 bool allow_access = false;
151
152                                                 while (sep.GetToken(entry))
153                                                 {
154                                                         if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
155                                                                 allow_access = true;
156                                                 }
157
158                                                 if (!allow_access)
159                                                 {
160                                                         ServerInstance->Logs->Log("m_httpd_acl", LOG_DEBUG, "Denying access to whitelisted resource %s (matched by pattern %s) from ip %s (Not in whitelist)",
161                                                                         http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str());
162                                                         BlockAccess(http, 403);
163                                                         return;
164                                                 }
165                                         }
166                                         if (!this_acl->password.empty() && !this_acl->username.empty())
167                                         {
168                                                 /* Password auth, first look to see if we have a basic authentication header */
169                                                 ServerInstance->Logs->Log("m_httpd_acl", LOG_DEBUG, "Checking HTTP auth password for resource %s (matched by pattern %s) from ip %s, against username %s",
170                                                                 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), this_acl->username.c_str());
171
172                                                 if (http->headers->IsSet("Authorization"))
173                                                 {
174                                                         /* Password has been given, validate it */
175                                                         std::string authorization = http->headers->GetHeader("Authorization");
176                                                         irc::spacesepstream sep(authorization);
177                                                         std::string authtype;
178                                                         std::string base64;
179
180                                                         sep.GetToken(authtype);
181                                                         if (authtype == "Basic")
182                                                         {
183                                                                 std::string user;
184                                                                 std::string pass;
185
186                                                                 sep.GetToken(base64);
187                                                                 std::string userpass = Base64ToBin(base64);
188                                                                 ServerInstance->Logs->Log("m_httpd_acl", LOG_DEBUG, "HTTP authorization: %s (%s)", userpass.c_str(), base64.c_str());
189
190                                                                 irc::sepstream userpasspair(userpass, ':');
191                                                                 if (userpasspair.GetToken(user))
192                                                                 {
193                                                                         userpasspair.GetToken(pass);
194
195                                                                         /* Access granted if username and password are correct */
196                                                                         if (user == this_acl->username && pass == this_acl->password)
197                                                                         {
198                                                                                 ServerInstance->Logs->Log("m_httpd_acl", LOG_DEBUG, "HTTP authorization: password and username match");
199                                                                                 return;
200                                                                         }
201                                                                         else
202                                                                                 /* Invalid password */
203                                                                                 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
204                                                                 }
205                                                                 else
206                                                                         /* Malformed user:pass pair */
207                                                                         BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
208                                                         }
209                                                         else
210                                                                 /* Unsupported authentication type */
211                                                                 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
212                                                 }
213                                                 else
214                                                 {
215                                                         /* No password given at all, access denied */
216                                                         BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
217                                                 }
218                                         }
219
220                                         /* A path may only match one ACL (the first it finds in the config file) */
221                                         return;
222                                 }
223                         }
224                 }
225         }
226
227         Version GetVersion() CXX11_OVERRIDE
228         {
229                 return Version("Provides access control lists (passwording of resources, ip restrictions etc) to m_httpd.so dependent modules", VF_VENDOR);
230         }
231 };
232
233 MODULE_INIT(ModuleHTTPAccessList)