2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018-2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2013, 2017-2018 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2013, 2015 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
8 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9 * Copyright (C) 2008, 2010 Craig Edwards <brain@inspircd.org>
10 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "modules/httpd.h"
35 std::string whitelist;
36 std::string blacklist;
38 HTTPACL(const std::string &set_path, const std::string &set_username, const std::string &set_password,
39 const std::string &set_whitelist, const std::string &set_blacklist)
40 : path(set_path), username(set_username), password(set_password), whitelist(set_whitelist),
41 blacklist(set_blacklist) { }
44 class ModuleHTTPAccessList : public Module, public HTTPACLEventListener
46 std::string stylesheet;
47 std::vector<HTTPACL> acl_list;
51 ModuleHTTPAccessList()
52 : HTTPACLEventListener(this)
57 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
59 std::vector<HTTPACL> new_acls;
60 ConfigTagList acls = ServerInstance->Config->ConfTags("httpdacl");
61 for (ConfigIter i = acls.first; i != acls.second; i++)
63 ConfigTag* c = i->second;
64 std::string path = c->getString("path");
65 std::string types = c->getString("types");
66 irc::commasepstream sep(types);
70 std::string whitelist;
71 std::string blacklist;
73 while (sep.GetToken(type))
75 if (stdalgo::string::equalsci(type, "password"))
77 username = c->getString("username");
78 password = c->getString("password");
80 else if (stdalgo::string::equalsci(type, "whitelist"))
82 whitelist = c->getString("whitelist");
84 else if (stdalgo::string::equalsci(type, "blacklist"))
86 blacklist = c->getString("blacklist");
90 throw ModuleException("Invalid HTTP ACL type '" + type + "'");
94 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Read ACL: path=%s pass=%s whitelist=%s blacklist=%s", path.c_str(),
95 password.c_str(), whitelist.c_str(), blacklist.c_str());
97 new_acls.push_back(HTTPACL(path, username, password, whitelist, blacklist));
99 acl_list.swap(new_acls);
102 void BlockAccess(HTTPRequest* http, unsigned int returnval, const std::string &extraheaderkey = "", const std::string &extraheaderval="")
104 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "BlockAccess (%u)", returnval);
106 std::stringstream data("Access to this resource is denied by an access control list. Please contact your IRC administrator.");
107 HTTPDocumentResponse response(this, *http, &data, returnval);
108 response.headers.SetHeader("X-Powered-By", MODNAME);
109 if (!extraheaderkey.empty())
110 response.headers.SetHeader(extraheaderkey, extraheaderval);
111 API->SendResponse(response);
114 bool IsAccessAllowed(HTTPRequest* http)
117 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd acl event");
119 for (std::vector<HTTPACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
121 if (InspIRCd::Match(http->GetPath(), this_acl->path, ascii_case_insensitive_map))
123 if (!this_acl->blacklist.empty())
126 irc::commasepstream sep(this_acl->blacklist);
129 while (sep.GetToken(entry))
131 if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
133 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Denying access to blacklisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
134 http->GetPath().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
135 BlockAccess(http, 403);
140 if (!this_acl->whitelist.empty())
143 irc::commasepstream sep(this_acl->whitelist);
145 bool allow_access = false;
147 while (sep.GetToken(entry))
149 if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
155 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Denying access to whitelisted resource %s (matched by pattern %s) from ip %s (Not in whitelist)",
156 http->GetPath().c_str(), this_acl->path.c_str(), http->GetIP().c_str());
157 BlockAccess(http, 403);
161 if (!this_acl->password.empty() && !this_acl->username.empty())
163 /* Password auth, first look to see if we have a basic authentication header */
164 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Checking HTTP auth password for resource %s (matched by pattern %s) from ip %s, against username %s",
165 http->GetPath().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), this_acl->username.c_str());
167 if (http->headers->IsSet("Authorization"))
169 /* Password has been given, validate it */
170 std::string authorization = http->headers->GetHeader("Authorization");
171 irc::spacesepstream sep(authorization);
172 std::string authtype;
175 sep.GetToken(authtype);
176 if (authtype == "Basic")
181 sep.GetToken(base64);
182 std::string userpass = Base64ToBin(base64);
183 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: %s (%s)", userpass.c_str(), base64.c_str());
185 irc::sepstream userpasspair(userpass, ':');
186 if (userpasspair.GetToken(user))
188 userpasspair.GetToken(pass);
190 /* Access granted if username and password are correct */
191 if (user == this_acl->username && pass == this_acl->password)
193 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: password and username match");
197 /* Invalid password */
198 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
201 /* Malformed user:pass pair */
202 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
205 /* Unsupported authentication type */
206 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
210 /* No password given at all, access denied */
211 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
216 /* A path may only match one ACL (the first it finds in the config file) */
224 ModResult OnHTTPACLCheck(HTTPRequest& req) CXX11_OVERRIDE
226 if (IsAccessAllowed(&req))
227 return MOD_RES_PASSTHRU;
231 Version GetVersion() CXX11_OVERRIDE
233 return Version("Provides access control lists (passwording of resources, IP restrictions, etc) to m_httpd dependent modules", VF_VENDOR);
237 MODULE_INIT(ModuleHTTPAccessList)