]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_acl.cpp
fixed some indentation and spacing in modules
[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 HTTPACL : 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         HTTPACL(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         ~HTTPACL() { }
37 };
38
39 class ModuleHTTPAccessList : public Module
40 {
41
42         std::string stylesheet;
43         bool changed;
44         std::vector<HTTPACL> 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(HTTPACL(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                 int inputlen = base64.length();
121                 int i = 0, j = 0, input = 0;
122                 unsigned char longbuf[4], shortbuf[3];
123                 std::string retval;
124
125                 if (inputlen == 0)
126                         return "";
127
128                 while (inputlen-- && (base64[input] != '=') && IsBase64(base64[input]))
129                 {
130                         longbuf[i++] = base64[input];
131                         input++;
132                         if (i == 4)
133                         {
134                                 for (i = 0; i < 4; ++i)
135                                         longbuf[i] = base64_chars.find(longbuf[i]);
136
137                                 shortbuf[0] = (longbuf[0] << 2)         + ((longbuf[1] & 0x30) >> 4);
138                                 shortbuf[1] = ((longbuf[1] & 0xf) << 4) + ((longbuf[2] & 0x3c) >> 2);
139                                 shortbuf[2] = ((longbuf[2] & 0x3) << 6) + longbuf[3];
140
141                                 for (i = 0; i < 3; ++i)
142                                         retval += shortbuf[i];
143
144                                 i = 0;
145                         }
146                 }
147
148                 if (i)
149                 {
150                         for (j = i; j < 4; ++j)
151                                 longbuf[j] = 0;
152
153                         for (j = 0; j < 4; ++j)
154                                 longbuf[j] = base64_chars.find(longbuf[j]);
155
156                         shortbuf[0] = (longbuf[0] << 2)         + ((longbuf[1] & 0x30) >> 4);
157                         shortbuf[1] = ((longbuf[1] & 0xf) << 4) + ((longbuf[2] & 0x3c) >> 2);
158                         shortbuf[2] = ((longbuf[2] & 0x3) << 6) + longbuf[3];
159
160                         for (j = 0; j < i - 1; ++j)
161                                 retval += shortbuf[j];
162                 }
163
164                 return retval;
165         }
166
167         void OnEvent(Event* event)
168         {
169                 if (event->GetEventID() == "httpd_acl")
170                 {
171                         ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd acl event");
172                         HTTPRequest* http = (HTTPRequest*)event->GetData();
173
174                         for (std::vector<HTTPACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
175                         {
176                                 if (match(http->GetURI(), this_acl->path))
177                                 {
178                                         if (!this_acl->blacklist.empty())
179                                         {
180                                                 /* Blacklist */
181                                                 irc::commasepstream sep(this_acl->blacklist);
182                                                 std::string entry;
183
184                                                 while (sep.GetToken(entry))
185                                                 {
186                                                         if (match(http->GetIP(), entry))
187                                                         {
188                                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to blacklisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
189                                                                                 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
190                                                                 BlockAccess(http, event, 403);
191                                                                 return;
192                                                         }
193                                                 }
194                                         }
195                                         if (!this_acl->whitelist.empty())
196                                         {
197                                                 /* Whitelist */
198                                                 irc::commasepstream sep(this_acl->whitelist);
199                                                 std::string entry;
200                                                 bool allow_access = false;
201
202                                                 while (sep.GetToken(entry))
203                                                 {
204                                                         if (match(http->GetIP(), entry))
205                                                                 allow_access = true;
206                                                 }
207
208                                                 if (!allow_access)
209                                                 {
210                                                         ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to whitelisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
211                                                                         http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
212                                                         BlockAccess(http, event, 403);
213                                                         return;
214                                                 }
215                                         }
216                                         if (!this_acl->password.empty() && !this_acl->username.empty())
217                                         {
218                                                 /* Password auth, first look to see if we have a basic authentication header */
219                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Checking HTTP auth password for resource %s (matched by pattern %s) from ip %s, against username %s",
220                                                                 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), this_acl->username.c_str());
221
222                                                 if (http->headers->IsSet("Authorization"))
223                                                 {
224                                                         /* Password has been given, validate it */
225                                                         std::string authorization = http->headers->GetHeader("Authorization");
226                                                         irc::spacesepstream sep(authorization);
227                                                         std::string authtype;
228                                                         std::string base64;
229
230                                                         sep.GetToken(authtype);
231                                                         if (authtype == "Basic")
232                                                         {
233                                                                 std::string user;
234                                                                 std::string pass;
235
236                                                                 sep.GetToken(base64);
237                                                                 std::string userpass = Base64Decode(base64);
238                                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: %s (%s)", userpass.c_str(), base64.c_str());
239
240                                                                 irc::sepstream userpasspair(userpass, ':');
241                                                                 if (userpasspair.GetToken(user))
242                                                                 {
243                                                                         userpasspair.GetToken(pass);
244
245                                                                         /* Access granted if username and password are correct */
246                                                                         if (user == this_acl->username && pass == this_acl->password)
247                                                                         {
248                                                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: password and username match");
249                                                                                 return;
250                                                                         }
251                                                                         else
252                                                                                 /* Invalid password */
253                                                                                 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
254                                                                 }
255                                                                 else
256                                                                         /* Malformed user:pass pair */
257                                                                         BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
258                                                         }
259                                                         else
260                                                                 /* Unsupported authentication type */
261                                                                 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
262                                                 }
263                                                 else
264                                                 {
265                                                         /* No password given at all, access denied */
266                                                         BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
267                                                 }
268                                         }
269
270                                         /* A path may only match one ACL (the first it finds in the config file) */
271                                         return;
272                                 }
273                         }
274                 }
275         }
276
277         const char* OnRequest(Request* request)
278         {
279                 return NULL;
280         }
281
282         virtual ~ModuleHTTPAccessList()
283         {
284         }
285
286         virtual Version GetVersion()
287         {
288                 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
289         }
290 };
291
292 MODULE_INIT(ModuleHTTPAccessList)