2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018-2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2013, 2017-2018, 2020 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
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;
107 data << "<html><head></head><body style='font-family: sans-serif; text-align: center'>"
108 << "<h1 style='font-size: 48pt'>Error " << returnval << "</h1>"
109 << "<h2 style='font-size: 24pt'>Access to this resource is denied by an access control list.</h2>"
110 << "<h2 style='font-size: 24pt'>Please contact your IRC administrator.</h2><hr>"
111 << "<small>Powered by <a href='https://www.inspircd.org'>InspIRCd</a></small></body></html>";
113 HTTPDocumentResponse response(this, *http, &data, returnval);
114 response.headers.SetHeader("X-Powered-By", MODNAME);
115 if (!extraheaderkey.empty())
116 response.headers.SetHeader(extraheaderkey, extraheaderval);
117 API->SendResponse(response);
120 bool IsAccessAllowed(HTTPRequest* http)
123 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd acl event");
125 for (std::vector<HTTPACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
127 if (InspIRCd::Match(http->GetPath(), this_acl->path, ascii_case_insensitive_map))
129 if (!this_acl->blacklist.empty())
132 irc::commasepstream sep(this_acl->blacklist);
135 while (sep.GetToken(entry))
137 if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
139 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Denying access to blacklisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
140 http->GetPath().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
141 BlockAccess(http, 403);
146 if (!this_acl->whitelist.empty())
149 irc::commasepstream sep(this_acl->whitelist);
151 bool allow_access = false;
153 while (sep.GetToken(entry))
155 if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
161 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Denying access to whitelisted resource %s (matched by pattern %s) from ip %s (Not in whitelist)",
162 http->GetPath().c_str(), this_acl->path.c_str(), http->GetIP().c_str());
163 BlockAccess(http, 403);
167 if (!this_acl->password.empty() && !this_acl->username.empty())
169 /* Password auth, first look to see if we have a basic authentication header */
170 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Checking HTTP auth password for resource %s (matched by pattern %s) from ip %s, against username %s",
171 http->GetPath().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), this_acl->username.c_str());
173 if (http->headers->IsSet("Authorization"))
175 /* Password has been given, validate it */
176 std::string authorization = http->headers->GetHeader("Authorization");
177 irc::spacesepstream sep(authorization);
178 std::string authtype;
181 sep.GetToken(authtype);
182 if (authtype == "Basic")
187 sep.GetToken(base64);
188 std::string userpass = Base64ToBin(base64);
189 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: %s (%s)", userpass.c_str(), base64.c_str());
191 irc::sepstream userpasspair(userpass, ':');
192 if (userpasspair.GetToken(user))
194 userpasspair.GetToken(pass);
196 /* Access granted if username and password are correct */
197 if (user == this_acl->username && pass == this_acl->password)
199 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: password and username match");
203 /* Invalid password */
204 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
207 /* Malformed user:pass pair */
208 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
211 /* Unsupported authentication type */
212 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
216 /* No password given at all, access denied */
217 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
222 /* A path may only match one ACL (the first it finds in the config file) */
230 ModResult OnHTTPACLCheck(HTTPRequest& req) CXX11_OVERRIDE
232 if (IsAccessAllowed(&req))
233 return MOD_RES_PASSTHRU;
237 Version GetVersion() CXX11_OVERRIDE
239 return Version("Allows the server administrator to control who can access resources served over HTTP with the httpd module.", VF_VENDOR);
243 MODULE_INIT(ModuleHTTPAccessList)