]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_acl.cpp
Add HTTP auth
[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                 std::stringstream data("Access to this resource is denied by an access control list. Please contact your IRC administrator.");
102                 HTTPDocument response(http->sock, &data, returnval);
103                 response.headers.SetHeader("X-Powered-By", "m_httpd_acl.so");
104                 if (!extraheaderkey.empty())
105                         response.headers.SetHeader(extraheaderkey, extraheaderval);
106                 Request req((char*)&response, (Module*)this, event->GetSource());
107                 req.Send();
108         }
109
110         bool IsBase64(unsigned char c)
111         {
112                 return (isalnum(c) || (c == '+') || (c == '/'));
113         }
114
115         std::string Base64Decode(const std::string &base64)
116         {
117                 const std::string base64_chars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
118
119                 int inputlen = base64.length();
120
121                 if (inputlen == 0)
122                         return "";
123
124                 int i = 0, j = 0, input = 0;
125
126                 unsigned char longbuf[4], shortbuf[3];
127                 std::string ret;
128
129                 while (inputlen-- && ( base64[input] != '=') && IsBase64(base64[input]))
130                 {
131                         longbuf[i++] = base64[input]; 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                                         ret += 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                                 ret += shortbuf[j];
162                 }
163
164                 return ret;
165         }
166
167         void OnEvent(Event* event)
168         {
169                 std::stringstream data("");
170
171                 if (event->GetEventID() == "httpd_acl")
172                 {
173                         ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd acl event");
174                         HTTPRequest* http = (HTTPRequest*)event->GetData();
175
176                         for (std::vector<ACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
177                         {
178                                 if (match(http->GetURI(), this_acl->path))
179                                 {
180                                         if (!this_acl->blacklist.empty())
181                                         {
182                                                 /* Blacklist */
183                                                 irc::commasepstream sep(this_acl->blacklist);
184                                                 std::string entry;
185
186                                                 while (sep.GetToken(entry))
187                                                 {
188                                                         if (match(http->GetIP(), entry))
189                                                         {
190                                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to blacklisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
191                                                                                 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
192                                                                 BlockAccess(http, event, 403);
193                                                                 return;
194                                                         }
195                                                 }
196                                         }
197                                         if (!this_acl->whitelist.empty())
198                                         {
199                                                 /* Whitelist */
200                                                 irc::commasepstream sep(this_acl->whitelist);
201                                                 std::string entry;
202                                                 bool allow_access = false;
203
204                                                 while (sep.GetToken(entry))
205                                                 {
206                                                         if (match(http->GetIP(), entry))
207                                                                 allow_access = true;
208                                                 }
209
210                                                 if (!allow_access)
211                                                 {
212                                                         ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to whitelisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
213                                                                         http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
214                                                         BlockAccess(http, event, 403);
215                                                         return;
216                                                 }
217                                         }
218                                         if (!this_acl->password.empty() && !this_acl->username.empty())
219                                         {
220                                                 /* Password auth, first look to see if we have a basic authentication header */
221                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Checking HTTP auth password for resource %s (matched by pattern %s) from ip %s, against username %s",
222                                                                 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), this_acl->username.c_str());
223
224                                                 if (http->headers->IsSet("Authorization"))
225                                                 {
226                                                         /* Password has been given, validate it */
227                                                         std::string authorization = http->headers->GetHeader("Authorization");
228                                                         irc::spacesepstream sep(authorization);
229                                                         std::string authtype;
230                                                         std::string base64;
231                                                         
232                                                         sep.GetToken(authtype);
233                                                         if (authtype == "Basic")
234                                                         {
235                                                                 std::string user;
236                                                                 std::string pass;
237
238                                                                 sep.GetToken(base64);
239                                                                 std::string userpass = Base64Decode(base64);
240                                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: %s (%s)", userpass.c_str(), base64.c_str());
241
242                                                                 irc::sepstream userpasspair(userpass, ':');
243                                                                 if (userpasspair.GetToken(user))
244                                                                 {
245                                                                         userpasspair.GetToken(pass);
246
247                                                                         /* Access granted if username and password are correct */
248                                                                         if (user == this_acl->username && pass == this_acl->password)
249                                                                         {
250                                                                                 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: password and username match");
251                                                                                 return;
252                                                                         }
253                                                                         else
254                                                                                 /* Invalid password */
255                                                                                 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
256                                                                 }
257                                                                 else
258                                                                         /* Malformed user:pass pair */
259                                                                         BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
260                                                         }
261                                                         else
262                                                                 /* Unsupported authentication type */
263                                                                 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
264                                                 }
265                                                 else
266                                                 {
267                                                         /* No password given at all, access denied */
268                                                         BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
269                                                 }
270                                         }
271
272                                         /* A path may only match one ACL (the first it finds in the config file) */
273                                         return;
274                                 }
275                         }
276                 }
277         }
278
279         const char* OnRequest(Request* request)
280         {
281                 return NULL;
282         }
283
284         virtual ~ModuleHTTPAccessList()
285         {
286         }
287
288         virtual Version GetVersion()
289         {
290                 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
291         }
292 };
293
294 MODULE_INIT(ModuleHTTPAccessList)