]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_acl.cpp
Fix a bunch of weird indentation and spacing issues.
[user/henk/code/inspircd.git] / src / modules / m_httpd_acl.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
11  *
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.
15  *
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
19  * details.
20  *
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/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "modules/httpd.h"
28
29 class HTTPACL
30 {
31  public:
32         std::string path;
33         std::string username;
34         std::string password;
35         std::string whitelist;
36         std::string blacklist;
37
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) { }
42 };
43
44 class ModuleHTTPAccessList : public Module, public HTTPACLEventListener
45 {
46  private:
47         std::vector<HTTPACL> acl_list;
48         HTTPdAPI API;
49
50  public:
51         ModuleHTTPAccessList()
52                 : HTTPACLEventListener(this)
53                 , API(this)
54         {
55         }
56
57         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
58         {
59                 std::vector<HTTPACL> new_acls;
60                 ConfigTagList acls = ServerInstance->Config->ConfTags("httpdacl");
61                 for (ConfigIter i = acls.first; i != acls.second; i++)
62                 {
63                         ConfigTag* c = i->second;
64                         std::string path = c->getString("path");
65                         std::string types = c->getString("types");
66                         irc::commasepstream sep(types);
67                         std::string type;
68                         std::string username;
69                         std::string password;
70                         std::string whitelist;
71                         std::string blacklist;
72
73                         while (sep.GetToken(type))
74                         {
75                                 if (stdalgo::string::equalsci(type, "password"))
76                                 {
77                                         username = c->getString("username");
78                                         password = c->getString("password");
79                                 }
80                                 else if (stdalgo::string::equalsci(type, "whitelist"))
81                                 {
82                                         whitelist = c->getString("whitelist");
83                                 }
84                                 else if (stdalgo::string::equalsci(type, "blacklist"))
85                                 {
86                                         blacklist = c->getString("blacklist");
87                                 }
88                                 else
89                                 {
90                                         throw ModuleException("Invalid HTTP ACL type '" + type + "'");
91                                 }
92                         }
93
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());
96
97                         new_acls.push_back(HTTPACL(path, username, password, whitelist, blacklist));
98                 }
99                 acl_list.swap(new_acls);
100         }
101
102         void BlockAccess(HTTPRequest* http, unsigned int returnval, const std::string &extraheaderkey = "", const std::string &extraheaderval="")
103         {
104                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "BlockAccess (%u)", returnval);
105
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>";
112
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);
118         }
119
120         bool IsAccessAllowed(HTTPRequest* http)
121         {
122                 {
123                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd acl event");
124
125                         for (std::vector<HTTPACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
126                         {
127                                 if (InspIRCd::Match(http->GetPath(), this_acl->path, ascii_case_insensitive_map))
128                                 {
129                                         if (!this_acl->blacklist.empty())
130                                         {
131                                                 /* Blacklist */
132                                                 irc::commasepstream sep(this_acl->blacklist);
133                                                 std::string entry;
134
135                                                 while (sep.GetToken(entry))
136                                                 {
137                                                         if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
138                                                         {
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);
142                                                                 return false;
143                                                         }
144                                                 }
145                                         }
146                                         if (!this_acl->whitelist.empty())
147                                         {
148                                                 /* Whitelist */
149                                                 irc::commasepstream sep(this_acl->whitelist);
150                                                 std::string entry;
151                                                 bool allow_access = false;
152
153                                                 while (sep.GetToken(entry))
154                                                 {
155                                                         if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
156                                                                 allow_access = true;
157                                                 }
158
159                                                 if (!allow_access)
160                                                 {
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);
164                                                         return false;
165                                                 }
166                                         }
167                                         if (!this_acl->password.empty() && !this_acl->username.empty())
168                                         {
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());
172
173                                                 if (http->headers->IsSet("Authorization"))
174                                                 {
175                                                         /* Password has been given, validate it */
176                                                         std::string authorization = http->headers->GetHeader("Authorization");
177                                                         irc::spacesepstream sep(authorization);
178                                                         std::string authtype;
179                                                         std::string base64;
180
181                                                         sep.GetToken(authtype);
182                                                         if (authtype == "Basic")
183                                                         {
184                                                                 std::string user;
185                                                                 std::string pass;
186
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());
190
191                                                                 irc::sepstream userpasspair(userpass, ':');
192                                                                 if (userpasspair.GetToken(user))
193                                                                 {
194                                                                         userpasspair.GetToken(pass);
195
196                                                                         /* Access granted if username and password are correct */
197                                                                         if (user == this_acl->username && pass == this_acl->password)
198                                                                         {
199                                                                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: password and username match");
200                                                                                 return true;
201                                                                         }
202                                                                         else
203                                                                         {
204                                                                                 /* Invalid password */
205                                                                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: password and username do not match");
206                                                                                 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
207                                                                         }
208                                                                 }
209                                                                 else
210                                                                 {
211                                                                         /* Malformed user:pass pair */
212                                                                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: password and username malformed");
213                                                                         BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
214                                                                 }
215                                                         }
216                                                         else
217                                                         {
218                                                                 /* Unsupported authentication type */
219                                                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: unsupported auth type: %s", authtype.c_str());
220                                                                 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
221                                                         }
222                                                 }
223                                                 else
224                                                 {
225                                                         /* No password given at all, access denied */
226                                                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: password and username not sent");
227                                                         BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
228                                                 }
229                                                 return false;
230                                         }
231
232                                         /* A path may only match one ACL (the first it finds in the config file) */
233                                         break;
234                                 }
235                         }
236                 }
237                 return true;
238         }
239
240         ModResult OnHTTPACLCheck(HTTPRequest& req) CXX11_OVERRIDE
241         {
242                 if (IsAccessAllowed(&req))
243                         return MOD_RES_PASSTHRU;
244                 return MOD_RES_DENY;
245         }
246
247         Version GetVersion() CXX11_OVERRIDE
248         {
249                 return Version("Allows the server administrator to control who can access resources served over HTTP with the httpd module.", VF_VENDOR);
250         }
251 };
252
253 MODULE_INIT(ModuleHTTPAccessList)