]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_acl.cpp
Remove dummy API_VERSION from Version constructor
[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;
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()  {
92                 ReadConfig();
93                 Implementation eventlist[] = { I_OnEvent };
94                 ServerInstance->Modules->Attach(eventlist, this, 1);
95         }
96
97         void BlockAccess(HTTPRequest* http, int returnval, const std::string &extraheaderkey = "", const std::string &extraheaderval="")
98         {
99                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "BlockAccess (%d)", returnval);
100
101                 std::stringstream data("Access to this resource is denied by an access control list. Please contact your IRC administrator.");
102                 HTTPDocumentResponse response(this, *http, &data, returnval);
103                 response.headers.SetHeader("X-Powered-By", "m_httpd_acl.so");
104                 if (!extraheaderkey.empty())
105                         response.headers.SetHeader(extraheaderkey, extraheaderval);
106                 response.Send();
107         }
108
109         bool IsBase64(unsigned char c)
110         {
111                 return (isalnum(c) || (c == '+') || (c == '/'));
112         }
113
114         std::string Base64Decode(const std::string &base64)
115         {
116                 const std::string base64_chars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
117                 int inputlen = base64.length();
118                 int i = 0, j = 0, input = 0;
119                 unsigned char longbuf[4], shortbuf[3];
120                 std::string retval;
121
122                 if (inputlen == 0)
123                         return "";
124
125                 while (inputlen-- && (base64[input] != '=') && IsBase64(base64[input]))
126                 {
127                         longbuf[i++] = base64[input];
128                         input++;
129                         if (i == 4)
130                         {
131                                 for (i = 0; i < 4; ++i)
132                                         longbuf[i] = base64_chars.find(longbuf[i]);
133
134                                 shortbuf[0] = (longbuf[0] << 2)         + ((longbuf[1] & 0x30) >> 4);
135                                 shortbuf[1] = ((longbuf[1] & 0xf) << 4) + ((longbuf[2] & 0x3c) >> 2);
136                                 shortbuf[2] = ((longbuf[2] & 0x3) << 6) + longbuf[3];
137
138                                 for (i = 0; i < 3; ++i)
139                                         retval += shortbuf[i];
140
141                                 i = 0;
142                         }
143                 }
144
145                 if (i)
146                 {
147                         for (j = i; j < 4; ++j)
148                                 longbuf[j] = 0;
149
150                         for (j = 0; j < 4; ++j)
151                                 longbuf[j] = base64_chars.find(longbuf[j]);
152
153                         shortbuf[0] = (longbuf[0] << 2)         + ((longbuf[1] & 0x30) >> 4);
154                         shortbuf[1] = ((longbuf[1] & 0xf) << 4) + ((longbuf[2] & 0x3c) >> 2);
155                         shortbuf[2] = ((longbuf[2] & 0x3) << 6) + longbuf[3];
156
157                         for (j = 0; j < i - 1; ++j)
158                                 retval += shortbuf[j];
159                 }
160
161                 return retval;
162         }
163
164         void OnEvent(Event& event)
165         {
166                 if (event.id == "httpd_acl")
167                 {
168                         ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd acl event");
169                         HTTPRequest* http = (HTTPRequest*)&event;
170
171                         for (std::vector<HTTPACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
172                         {
173                                 if (InspIRCd::Match(http->GetURI(), this_acl->path, ascii_case_insensitive_map))
174                                 {
175                                         if (!this_acl->blacklist.empty())
176                                         {
177                                                 /* Blacklist */
178                                                 irc::commasepstream sep(this_acl->blacklist);
179                                                 std::string entry;
180
181                                                 while (sep.GetToken(entry))
182                                                 {
183                                                         if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
184                                                         {
185                                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to blacklisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
186                                                                                 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
187                                                                 BlockAccess(http, 403);
188                                                                 return;
189                                                         }
190                                                 }
191                                         }
192                                         if (!this_acl->whitelist.empty())
193                                         {
194                                                 /* Whitelist */
195                                                 irc::commasepstream sep(this_acl->whitelist);
196                                                 std::string entry;
197                                                 bool allow_access = false;
198
199                                                 while (sep.GetToken(entry))
200                                                 {
201                                                         if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
202                                                                 allow_access = true;
203                                                 }
204
205                                                 if (!allow_access)
206                                                 {
207                                                         ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to whitelisted resource %s (matched by pattern %s) from ip %s (Not in whitelist)",
208                                                                         http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str());
209                                                         BlockAccess(http, 403);
210                                                         return;
211                                                 }
212                                         }
213                                         if (!this_acl->password.empty() && !this_acl->username.empty())
214                                         {
215                                                 /* Password auth, first look to see if we have a basic authentication header */
216                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Checking HTTP auth password for resource %s (matched by pattern %s) from ip %s, against username %s",
217                                                                 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), this_acl->username.c_str());
218
219                                                 if (http->headers->IsSet("Authorization"))
220                                                 {
221                                                         /* Password has been given, validate it */
222                                                         std::string authorization = http->headers->GetHeader("Authorization");
223                                                         irc::spacesepstream sep(authorization);
224                                                         std::string authtype;
225                                                         std::string base64;
226
227                                                         sep.GetToken(authtype);
228                                                         if (authtype == "Basic")
229                                                         {
230                                                                 std::string user;
231                                                                 std::string pass;
232
233                                                                 sep.GetToken(base64);
234                                                                 std::string userpass = Base64Decode(base64);
235                                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: %s (%s)", userpass.c_str(), base64.c_str());
236
237                                                                 irc::sepstream userpasspair(userpass, ':');
238                                                                 if (userpasspair.GetToken(user))
239                                                                 {
240                                                                         userpasspair.GetToken(pass);
241
242                                                                         /* Access granted if username and password are correct */
243                                                                         if (user == this_acl->username && pass == this_acl->password)
244                                                                         {
245                                                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: password and username match");
246                                                                                 return;
247                                                                         }
248                                                                         else
249                                                                                 /* Invalid password */
250                                                                                 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
251                                                                 }
252                                                                 else
253                                                                         /* Malformed user:pass pair */
254                                                                         BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
255                                                         }
256                                                         else
257                                                                 /* Unsupported authentication type */
258                                                                 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
259                                                 }
260                                                 else
261                                                 {
262                                                         /* No password given at all, access denied */
263                                                         BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
264                                                 }
265                                         }
266
267                                         /* A path may only match one ACL (the first it finds in the config file) */
268                                         return;
269                                 }
270                         }
271                 }
272         }
273
274         virtual ~ModuleHTTPAccessList()
275         {
276         }
277
278         virtual Version GetVersion()
279         {
280                 return Version("Provides access control lists (passwording of resources, ip restrictions etc) to m_httpd.so dependent modules", VF_VENDOR);
281         }
282 };
283
284 MODULE_INIT(ModuleHTTPAccessList)