]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_acl.cpp
Update copyright headers.
[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 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         std::string stylesheet;
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("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);
112         }
113
114         bool IsAccessAllowed(HTTPRequest* http)
115         {
116                 {
117                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd acl event");
118
119                         for (std::vector<HTTPACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
120                         {
121                                 if (InspIRCd::Match(http->GetPath(), this_acl->path, ascii_case_insensitive_map))
122                                 {
123                                         if (!this_acl->blacklist.empty())
124                                         {
125                                                 /* Blacklist */
126                                                 irc::commasepstream sep(this_acl->blacklist);
127                                                 std::string entry;
128
129                                                 while (sep.GetToken(entry))
130                                                 {
131                                                         if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
132                                                         {
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);
136                                                                 return false;
137                                                         }
138                                                 }
139                                         }
140                                         if (!this_acl->whitelist.empty())
141                                         {
142                                                 /* Whitelist */
143                                                 irc::commasepstream sep(this_acl->whitelist);
144                                                 std::string entry;
145                                                 bool allow_access = false;
146
147                                                 while (sep.GetToken(entry))
148                                                 {
149                                                         if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
150                                                                 allow_access = true;
151                                                 }
152
153                                                 if (!allow_access)
154                                                 {
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);
158                                                         return false;
159                                                 }
160                                         }
161                                         if (!this_acl->password.empty() && !this_acl->username.empty())
162                                         {
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());
166
167                                                 if (http->headers->IsSet("Authorization"))
168                                                 {
169                                                         /* Password has been given, validate it */
170                                                         std::string authorization = http->headers->GetHeader("Authorization");
171                                                         irc::spacesepstream sep(authorization);
172                                                         std::string authtype;
173                                                         std::string base64;
174
175                                                         sep.GetToken(authtype);
176                                                         if (authtype == "Basic")
177                                                         {
178                                                                 std::string user;
179                                                                 std::string pass;
180
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());
184
185                                                                 irc::sepstream userpasspair(userpass, ':');
186                                                                 if (userpasspair.GetToken(user))
187                                                                 {
188                                                                         userpasspair.GetToken(pass);
189
190                                                                         /* Access granted if username and password are correct */
191                                                                         if (user == this_acl->username && pass == this_acl->password)
192                                                                         {
193                                                                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: password and username match");
194                                                                                 return true;
195                                                                         }
196                                                                         else
197                                                                                 /* Invalid password */
198                                                                                 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
199                                                                 }
200                                                                 else
201                                                                         /* Malformed user:pass pair */
202                                                                         BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
203                                                         }
204                                                         else
205                                                                 /* Unsupported authentication type */
206                                                                 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
207                                                 }
208                                                 else
209                                                 {
210                                                         /* No password given at all, access denied */
211                                                         BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
212                                                 }
213                                                 return false;
214                                         }
215
216                                         /* A path may only match one ACL (the first it finds in the config file) */
217                                         break;
218                                 }
219                         }
220                 }
221                 return true;
222         }
223
224         ModResult OnHTTPACLCheck(HTTPRequest& req) CXX11_OVERRIDE
225         {
226                 if (IsAccessAllowed(&req))
227                         return MOD_RES_PASSTHRU;
228                 return MOD_RES_DENY;
229         }
230
231         Version GetVersion() CXX11_OVERRIDE
232         {
233                 return Version("Provides access control lists (passwording of resources, IP restrictions, etc) to m_httpd dependent modules", VF_VENDOR);
234         }
235 };
236
237 MODULE_INIT(ModuleHTTPAccessList)