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