2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
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.
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
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/>.
22 #include "modules/httpd.h"
25 /* $ModDesc: Provides access control lists (passwording of resources, ip restrictions etc) to m_httpd.so dependent modules */
33 std::string whitelist;
34 std::string blacklist;
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) { }
42 class ModuleHTTPAccessList : public Module
44 std::string stylesheet;
45 std::vector<HTTPACL> acl_list;
51 ConfigTagList acls = ServerInstance->Config->ConfTags("httpdacl");
52 for (ConfigIter i = acls.first; i != acls.second; i++)
54 ConfigTag* c = i->second;
55 std::string path = c->getString("path");
56 std::string types = c->getString("types");
57 irc::commasepstream sep(types);
61 std::string whitelist;
62 std::string blacklist;
64 while (sep.GetToken(type))
66 if (type == "password")
68 username = c->getString("username");
69 password = c->getString("password");
71 else if (type == "whitelist")
73 whitelist = c->getString("whitelist");
75 else if (type == "blacklist")
77 blacklist = c->getString("blacklist");
81 throw ModuleException("Invalid HTTP ACL type '" + type + "'");
85 ServerInstance->Logs->Log("m_httpd_acl", LOG_DEBUG, "Read ACL: path=%s pass=%s whitelist=%s blacklist=%s", path.c_str(),
86 password.c_str(), whitelist.c_str(), blacklist.c_str());
88 acl_list.push_back(HTTPACL(path, username, password, whitelist, blacklist));
92 void init() CXX11_OVERRIDE
95 Implementation eventlist[] = { I_OnEvent };
96 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
99 void BlockAccess(HTTPRequest* http, int returnval, const std::string &extraheaderkey = "", const std::string &extraheaderval="")
101 ServerInstance->Logs->Log("m_httpd_acl", LOG_DEBUG, "BlockAccess (%d)", returnval);
103 std::stringstream data("Access to this resource is denied by an access control list. Please contact your IRC administrator.");
104 HTTPDocumentResponse response(this, *http, &data, returnval);
105 response.headers.SetHeader("X-Powered-By", "m_httpd_acl.so");
106 if (!extraheaderkey.empty())
107 response.headers.SetHeader(extraheaderkey, extraheaderval);
111 void OnEvent(Event& event) CXX11_OVERRIDE
113 if (event.id == "httpd_acl")
115 ServerInstance->Logs->Log("m_http_stats", LOG_DEBUG, "Handling httpd acl event");
116 HTTPRequest* http = (HTTPRequest*)&event;
118 for (std::vector<HTTPACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
120 if (InspIRCd::Match(http->GetURI(), this_acl->path, ascii_case_insensitive_map))
122 if (!this_acl->blacklist.empty())
125 irc::commasepstream sep(this_acl->blacklist);
128 while (sep.GetToken(entry))
130 if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
132 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)",
133 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
134 BlockAccess(http, 403);
139 if (!this_acl->whitelist.empty())
142 irc::commasepstream sep(this_acl->whitelist);
144 bool allow_access = false;
146 while (sep.GetToken(entry))
148 if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
154 ServerInstance->Logs->Log("m_httpd_acl", LOG_DEBUG, "Denying access to whitelisted resource %s (matched by pattern %s) from ip %s (Not in whitelist)",
155 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str());
156 BlockAccess(http, 403);
160 if (!this_acl->password.empty() && !this_acl->username.empty())
162 /* Password auth, first look to see if we have a basic authentication header */
163 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",
164 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), this_acl->username.c_str());
166 if (http->headers->IsSet("Authorization"))
168 /* Password has been given, validate it */
169 std::string authorization = http->headers->GetHeader("Authorization");
170 irc::spacesepstream sep(authorization);
171 std::string authtype;
174 sep.GetToken(authtype);
175 if (authtype == "Basic")
180 sep.GetToken(base64);
181 std::string userpass = Base64ToBin(base64);
182 ServerInstance->Logs->Log("m_httpd_acl", LOG_DEBUG, "HTTP authorization: %s (%s)", userpass.c_str(), base64.c_str());
184 irc::sepstream userpasspair(userpass, ':');
185 if (userpasspair.GetToken(user))
187 userpasspair.GetToken(pass);
189 /* Access granted if username and password are correct */
190 if (user == this_acl->username && pass == this_acl->password)
192 ServerInstance->Logs->Log("m_httpd_acl", LOG_DEBUG, "HTTP authorization: password and username match");
196 /* Invalid password */
197 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
200 /* Malformed user:pass pair */
201 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
204 /* Unsupported authentication type */
205 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
209 /* No password given at all, access denied */
210 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
214 /* A path may only match one ACL (the first it finds in the config file) */
221 Version GetVersion() CXX11_OVERRIDE
223 return Version("Provides access control lists (passwording of resources, ip restrictions etc) to m_httpd.so dependent modules", VF_VENDOR);
227 MODULE_INIT(ModuleHTTPAccessList)