]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_acl.cpp
Change modules to use the MODNAME constant when logging.
[user/henk/code/inspircd.git] / src / modules / m_httpd_acl.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "modules/httpd.h"
23 #include "protocol.h"
24
25 class HTTPACL
26 {
27  public:
28         std::string path;
29         std::string username;
30         std::string password;
31         std::string whitelist;
32         std::string blacklist;
33
34         HTTPACL(const std::string &set_path, const std::string &set_username, const std::string &set_password,
35                 const std::string &set_whitelist, const std::string &set_blacklist)
36                 : path(set_path), username(set_username), password(set_password), whitelist(set_whitelist),
37                 blacklist(set_blacklist) { }
38 };
39
40 class ModuleHTTPAccessList : public Module
41 {
42         std::string stylesheet;
43         std::vector<HTTPACL> acl_list;
44         HTTPdAPI API;
45
46  public:
47         ModuleHTTPAccessList()
48                 : API(this)
49         {
50         }
51
52         void OnRehash(User* user)
53         {
54                 acl_list.clear();
55                 ConfigTagList acls = ServerInstance->Config->ConfTags("httpdacl");
56                 for (ConfigIter i = acls.first; i != acls.second; i++)
57                 {
58                         ConfigTag* c = i->second;
59                         std::string path = c->getString("path");
60                         std::string types = c->getString("types");
61                         irc::commasepstream sep(types);
62                         std::string type;
63                         std::string username;
64                         std::string password;
65                         std::string whitelist;
66                         std::string blacklist;
67
68                         while (sep.GetToken(type))
69                         {
70                                 if (type == "password")
71                                 {
72                                         username = c->getString("username");
73                                         password = c->getString("password");
74                                 }
75                                 else if (type == "whitelist")
76                                 {
77                                         whitelist = c->getString("whitelist");
78                                 }
79                                 else if (type == "blacklist")
80                                 {
81                                         blacklist = c->getString("blacklist");
82                                 }
83                                 else
84                                 {
85                                         throw ModuleException("Invalid HTTP ACL type '" + type + "'");
86                                 }
87                         }
88
89                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Read ACL: path=%s pass=%s whitelist=%s blacklist=%s", path.c_str(),
90                                         password.c_str(), whitelist.c_str(), blacklist.c_str());
91
92                         acl_list.push_back(HTTPACL(path, username, password, whitelist, blacklist));
93                 }
94         }
95
96         void init() CXX11_OVERRIDE
97         {
98                 OnRehash(NULL);
99                 Implementation eventlist[] = { I_OnEvent, I_OnRehash };
100                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
101         }
102
103         void BlockAccess(HTTPRequest* http, int returnval, const std::string &extraheaderkey = "", const std::string &extraheaderval="")
104         {
105                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "BlockAccess (%d)", returnval);
106
107                 std::stringstream data("Access to this resource is denied by an access control list. Please contact your IRC administrator.");
108                 HTTPDocumentResponse response(this, *http, &data, returnval);
109                 response.headers.SetHeader("X-Powered-By", "m_httpd_acl.so");
110                 if (!extraheaderkey.empty())
111                         response.headers.SetHeader(extraheaderkey, extraheaderval);
112                 API->SendResponse(response);
113         }
114
115         void OnEvent(Event& event) CXX11_OVERRIDE
116         {
117                 if (event.id == "httpd_acl")
118                 {
119                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd acl event");
120                         HTTPRequest* http = (HTTPRequest*)&event;
121
122                         for (std::vector<HTTPACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
123                         {
124                                 if (InspIRCd::Match(http->GetURI(), this_acl->path, ascii_case_insensitive_map))
125                                 {
126                                         if (!this_acl->blacklist.empty())
127                                         {
128                                                 /* Blacklist */
129                                                 irc::commasepstream sep(this_acl->blacklist);
130                                                 std::string entry;
131
132                                                 while (sep.GetToken(entry))
133                                                 {
134                                                         if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
135                                                         {
136                                                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Denying access to blacklisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
137                                                                                 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
138                                                                 BlockAccess(http, 403);
139                                                                 return;
140                                                         }
141                                                 }
142                                         }
143                                         if (!this_acl->whitelist.empty())
144                                         {
145                                                 /* Whitelist */
146                                                 irc::commasepstream sep(this_acl->whitelist);
147                                                 std::string entry;
148                                                 bool allow_access = false;
149
150                                                 while (sep.GetToken(entry))
151                                                 {
152                                                         if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
153                                                                 allow_access = true;
154                                                 }
155
156                                                 if (!allow_access)
157                                                 {
158                                                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Denying access to whitelisted resource %s (matched by pattern %s) from ip %s (Not in whitelist)",
159                                                                         http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str());
160                                                         BlockAccess(http, 403);
161                                                         return;
162                                                 }
163                                         }
164                                         if (!this_acl->password.empty() && !this_acl->username.empty())
165                                         {
166                                                 /* Password auth, first look to see if we have a basic authentication header */
167                                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Checking HTTP auth password for resource %s (matched by pattern %s) from ip %s, against username %s",
168                                                                 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), this_acl->username.c_str());
169
170                                                 if (http->headers->IsSet("Authorization"))
171                                                 {
172                                                         /* Password has been given, validate it */
173                                                         std::string authorization = http->headers->GetHeader("Authorization");
174                                                         irc::spacesepstream sep(authorization);
175                                                         std::string authtype;
176                                                         std::string base64;
177
178                                                         sep.GetToken(authtype);
179                                                         if (authtype == "Basic")
180                                                         {
181                                                                 std::string user;
182                                                                 std::string pass;
183
184                                                                 sep.GetToken(base64);
185                                                                 std::string userpass = Base64ToBin(base64);
186                                                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: %s (%s)", userpass.c_str(), base64.c_str());
187
188                                                                 irc::sepstream userpasspair(userpass, ':');
189                                                                 if (userpasspair.GetToken(user))
190                                                                 {
191                                                                         userpasspair.GetToken(pass);
192
193                                                                         /* Access granted if username and password are correct */
194                                                                         if (user == this_acl->username && pass == this_acl->password)
195                                                                         {
196                                                                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "HTTP authorization: password and username match");
197                                                                                 return;
198                                                                         }
199                                                                         else
200                                                                                 /* Invalid password */
201                                                                                 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
202                                                                 }
203                                                                 else
204                                                                         /* Malformed user:pass pair */
205                                                                         BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
206                                                         }
207                                                         else
208                                                                 /* Unsupported authentication type */
209                                                                 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
210                                                 }
211                                                 else
212                                                 {
213                                                         /* No password given at all, access denied */
214                                                         BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
215                                                 }
216                                         }
217
218                                         /* A path may only match one ACL (the first it finds in the config file) */
219                                         return;
220                                 }
221                         }
222                 }
223         }
224
225         Version GetVersion() CXX11_OVERRIDE
226         {
227                 return Version("Provides access control lists (passwording of resources, ip restrictions etc) to m_httpd.so dependent modules", VF_VENDOR);
228         }
229 };
230
231 MODULE_INIT(ModuleHTTPAccessList)