]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_acl.cpp
httpd connection close fixes (these need to be backported to 1.1 at some point?)...
[user/henk/code/inspircd.git] / src / modules / m_httpd_acl.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "httpd.h"
16 #include "protocol.h"
17 #include "wildcard.h"
18
19 /* $ModDesc: Provides access control lists (passwording of resources, ip restrictions etc) to m_httpd.so dependent modules */
20 /* $ModDep: httpd.h */
21
22 class ACL : public Extensible
23 {
24  public:
25         std::string path;
26         std::string username;
27         std::string password;
28         std::string whitelist;
29         std::string blacklist;
30
31         ACL(const std::string &set_path, const std::string &set_username, const std::string &set_password,
32                 const std::string &set_whitelist, const std::string &set_blacklist)
33                 : path(set_path), username(set_username), password(set_password), whitelist(set_whitelist),
34                 blacklist(set_blacklist) { }
35
36         ~ACL() { }
37 };
38
39 class ModuleHTTPAccessList : public Module
40 {
41         
42         std::string stylesheet;
43         bool changed;
44         std::vector<ACL> acl_list;
45
46  public:
47
48         void ReadConfig()
49         {
50                 acl_list.clear();
51                 ConfigReader c(ServerInstance);
52                 int n_items = c.Enumerate("httpdacl");
53                 for (int i = 0; i < n_items; ++i)
54                 {
55                         std::string path = c.ReadValue("httpdacl", "path", i);
56                         std::string types = c.ReadValue("httpdacl", "types", i);
57                         irc::commasepstream sep(types);
58                         std::string type;
59                         std::string username;
60                         std::string password;
61                         std::string whitelist;
62                         std::string blacklist;
63
64                         while (sep.GetToken(type))
65                         {
66                                 if (type == "password")
67                                 {
68                                         username = c.ReadValue("httpdacl", "username", i);
69                                         password = c.ReadValue("httpdacl", "password", i);
70                                 }
71                                 else if (type == "whitelist")
72                                 {
73                                         whitelist = c.ReadValue("httpdacl", "whitelist", i);
74                                 }
75                                 else if (type == "blacklist")
76                                 {
77                                         blacklist = c.ReadValue("httpdacl", "blacklist", i);
78                                 }
79                                 else
80                                 {
81                                         throw ModuleException("Invalid HTTP ACL type '" + type + "'");
82                                 }
83                         }
84
85                         ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Read ACL: path=%s pass=%s whitelist=%s blacklist=%s", path.c_str(),
86                                         password.c_str(), whitelist.c_str(), blacklist.c_str());
87
88                         acl_list.push_back(ACL(path, username, password, whitelist, blacklist));
89                 }
90         }
91
92         ModuleHTTPAccessList(InspIRCd* Me) : Module(Me)
93         {
94                 ReadConfig();
95                 Implementation eventlist[] = { I_OnEvent, I_OnRequest };
96                 ServerInstance->Modules->Attach(eventlist, this, 2);
97         }
98
99         void BlockAccess(HTTPRequest* http, Event* event, int returnval, const std::string &extraheaderkey = "", const std::string &extraheaderval="")
100         {
101                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "BlockAccess (%d)", returnval);
102
103                 std::stringstream data("Access to this resource is denied by an access control list. Please contact your IRC administrator.");
104                 HTTPDocument response(http->sock, &data, returnval);
105                 response.headers.SetHeader("X-Powered-By", "m_httpd_acl.so");
106                 if (!extraheaderkey.empty())
107                         response.headers.SetHeader(extraheaderkey, extraheaderval);
108                 Request req((char*)&response, (Module*)this, event->GetSource());
109                 req.Send();
110         }
111
112         bool IsBase64(unsigned char c)
113         {
114                 return (isalnum(c) || (c == '+') || (c == '/'));
115         }
116
117         std::string Base64Decode(const std::string &base64)
118         {
119                 const std::string base64_chars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
120
121                 int inputlen = base64.length();
122
123                 if (inputlen == 0)
124                         return "";
125
126                 int i = 0, j = 0, input = 0;
127
128                 unsigned char longbuf[4], shortbuf[3];
129                 std::string ret;
130
131                 while (inputlen-- && ( base64[input] != '=') && IsBase64(base64[input]))
132                 {
133                         longbuf[i++] = base64[input]; input++;
134                         if (i ==4)
135                         {
136                                 for (i = 0; i <4; i++)
137                                         longbuf[i] = base64_chars.find(longbuf[i]);
138
139                                 shortbuf[0] = (longbuf[0] << 2) + ((longbuf[1] & 0x30) >> 4);
140                                 shortbuf[1] = ((longbuf[1] & 0xf) << 4) + ((longbuf[2] & 0x3c) >> 2);
141                                 shortbuf[2] = ((longbuf[2] & 0x3) << 6) + longbuf[3];
142
143                                 for (i = 0; (i < 3); i++)
144                                         ret += shortbuf[i];
145
146                                 i = 0;
147                         }
148                 }
149
150                 if (i)
151                 {
152                         for (j = i; j <4; j++)
153                                 longbuf[j] = 0;
154
155                         for (j = 0; j <4; j++)
156                                 longbuf[j] = base64_chars.find(longbuf[j]);
157
158                         shortbuf[0] = (longbuf[0] << 2) + ((longbuf[1] & 0x30) >> 4);
159                         shortbuf[1] = ((longbuf[1] & 0xf) << 4) + ((longbuf[2] & 0x3c) >> 2);
160                         shortbuf[2] = ((longbuf[2] & 0x3) << 6) + longbuf[3];
161
162                         for (j = 0; (j < i - 1); j++)
163                                 ret += shortbuf[j];
164                 }
165
166                 return ret;
167         }
168
169         void OnEvent(Event* event)
170         {
171                 std::stringstream data("");
172
173                 if (event->GetEventID() == "httpd_acl")
174                 {
175                         ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd acl event");
176                         HTTPRequest* http = (HTTPRequest*)event->GetData();
177
178                         for (std::vector<ACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
179                         {
180                                 if (match(http->GetURI(), this_acl->path))
181                                 {
182                                         if (!this_acl->blacklist.empty())
183                                         {
184                                                 /* Blacklist */
185                                                 irc::commasepstream sep(this_acl->blacklist);
186                                                 std::string entry;
187
188                                                 while (sep.GetToken(entry))
189                                                 {
190                                                         if (match(http->GetIP(), entry))
191                                                         {
192                                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to blacklisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
193                                                                                 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
194                                                                 BlockAccess(http, event, 403);
195                                                                 return;
196                                                         }
197                                                 }
198                                         }
199                                         if (!this_acl->whitelist.empty())
200                                         {
201                                                 /* Whitelist */
202                                                 irc::commasepstream sep(this_acl->whitelist);
203                                                 std::string entry;
204                                                 bool allow_access = false;
205
206                                                 while (sep.GetToken(entry))
207                                                 {
208                                                         if (match(http->GetIP(), entry))
209                                                                 allow_access = true;
210                                                 }
211
212                                                 if (!allow_access)
213                                                 {
214                                                         ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to whitelisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
215                                                                         http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
216                                                         BlockAccess(http, event, 403);
217                                                         return;
218                                                 }
219                                         }
220                                         if (!this_acl->password.empty() && !this_acl->username.empty())
221                                         {
222                                                 /* Password auth, first look to see if we have a basic authentication header */
223                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Checking HTTP auth password for resource %s (matched by pattern %s) from ip %s, against username %s",
224                                                                 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), this_acl->username.c_str());
225
226                                                 if (http->headers->IsSet("Authorization"))
227                                                 {
228                                                         /* Password has been given, validate it */
229                                                         std::string authorization = http->headers->GetHeader("Authorization");
230                                                         irc::spacesepstream sep(authorization);
231                                                         std::string authtype;
232                                                         std::string base64;
233                                                         
234                                                         sep.GetToken(authtype);
235                                                         if (authtype == "Basic")
236                                                         {
237                                                                 std::string user;
238                                                                 std::string pass;
239
240                                                                 sep.GetToken(base64);
241                                                                 std::string userpass = Base64Decode(base64);
242                                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: %s (%s)", userpass.c_str(), base64.c_str());
243
244                                                                 irc::sepstream userpasspair(userpass, ':');
245                                                                 if (userpasspair.GetToken(user))
246                                                                 {
247                                                                         userpasspair.GetToken(pass);
248
249                                                                         /* Access granted if username and password are correct */
250                                                                         if (user == this_acl->username && pass == this_acl->password)
251                                                                         {
252                                                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: password and username match");
253                                                                                 return;
254                                                                         }
255                                                                         else
256                                                                                 /* Invalid password */
257                                                                                 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
258                                                                 }
259                                                                 else
260                                                                         /* Malformed user:pass pair */
261                                                                         BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
262                                                         }
263                                                         else
264                                                                 /* Unsupported authentication type */
265                                                                 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
266                                                 }
267                                                 else
268                                                 {
269                                                         /* No password given at all, access denied */
270                                                         BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
271                                                 }
272                                         }
273
274                                         /* A path may only match one ACL (the first it finds in the config file) */
275                                         return;
276                                 }
277                         }
278                 }
279         }
280
281         const char* OnRequest(Request* request)
282         {
283                 return NULL;
284         }
285
286         virtual ~ModuleHTTPAccessList()
287         {
288         }
289
290         virtual Version GetVersion()
291         {
292                 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
293         }
294 };
295
296 MODULE_INIT(ModuleHTTPAccessList)