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