1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 /* $ModDesc: Provides access control lists (passwording of resources, ip restrictions etc) to m_httpd.so dependent modules */
20 /* $ModDep: httpd.h */
22 class ACL : public Extensible
28 std::string whitelist;
29 std::string blacklist;
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) { }
39 class ModuleHTTPAccessList : public Module
42 std::string stylesheet;
44 std::vector<ACL> acl_list;
51 ConfigReader c(ServerInstance);
52 int n_items = c.Enumerate("httpdacl");
53 for (int i = 0; i < n_items; ++i)
55 std::string path = c.ReadValue("httpdacl", "path", i);
56 std::string types = c.ReadValue("httpdacl", "types", i);
57 irc::commasepstream sep(types);
61 std::string whitelist;
62 std::string blacklist;
64 while (sep.GetToken(type))
66 if (type == "password")
68 username = c.ReadValue("httpdacl", "username", i);
69 password = c.ReadValue("httpdacl", "password", i);
71 else if (type == "whitelist")
73 whitelist = c.ReadValue("httpdacl", "whitelist", i);
75 else if (type == "blacklist")
77 blacklist = c.ReadValue("httpdacl", "blacklist", i);
81 throw ModuleException("Invalid HTTP ACL type '" + type + "'");
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());
88 acl_list.push_back(ACL(path, username, password, whitelist, blacklist));
92 ModuleHTTPAccessList(InspIRCd* Me) : Module(Me)
95 Implementation eventlist[] = { I_OnEvent, I_OnRequest };
96 ServerInstance->Modules->Attach(eventlist, this, 2);
99 void BlockAccess(HTTPRequest* http, Event* event, int returnval, const std::string &extraheaderkey = "", const std::string &extraheaderval="")
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());
110 bool IsBase64(unsigned char c)
112 return (isalnum(c) || (c == '+') || (c == '/'));
115 std::string Base64Decode(const std::string &base64)
117 const std::string base64_chars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
119 int inputlen = base64.length();
124 int i = 0, j = 0, input = 0;
126 unsigned char longbuf[4], shortbuf[3];
129 while (inputlen-- && ( base64[input] != '=') && IsBase64(base64[input]))
131 longbuf[i++] = base64[input]; input++;
134 for (i = 0; i <4; i++)
135 longbuf[i] = base64_chars.find(longbuf[i]);
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];
141 for (i = 0; (i < 3); i++)
150 for (j = i; j <4; j++)
153 for (j = 0; j <4; j++)
154 longbuf[j] = base64_chars.find(longbuf[j]);
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];
160 for (j = 0; (j < i - 1); j++)
167 void OnEvent(Event* event)
169 std::stringstream data("");
171 if (event->GetEventID() == "httpd_acl")
173 ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd acl event");
174 HTTPRequest* http = (HTTPRequest*)event->GetData();
176 for (std::vector<ACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
178 if (match(http->GetURI(), this_acl->path))
180 if (!this_acl->blacklist.empty())
183 irc::commasepstream sep(this_acl->blacklist);
186 while (sep.GetToken(entry))
188 if (match(http->GetIP(), entry))
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);
197 if (!this_acl->whitelist.empty())
200 irc::commasepstream sep(this_acl->whitelist);
202 bool allow_access = false;
204 while (sep.GetToken(entry))
206 if (match(http->GetIP(), entry))
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);
218 if (!this_acl->password.empty() && !this_acl->username.empty())
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());
224 if (http->headers->IsSet("Authorization"))
226 /* Password has been given, validate it */
227 std::string authorization = http->headers->GetHeader("Authorization");
228 irc::spacesepstream sep(authorization);
229 std::string authtype;
232 sep.GetToken(authtype);
233 if (authtype == "Basic")
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());
242 irc::sepstream userpasspair(userpass, ':');
243 if (userpasspair.GetToken(user))
245 userpasspair.GetToken(pass);
247 /* Access granted if username and password are correct */
248 if (user == this_acl->username && pass == this_acl->password)
250 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: password and username match");
254 /* Invalid password */
255 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
258 /* Malformed user:pass pair */
259 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
262 /* Unsupported authentication type */
263 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
267 /* No password given at all, access denied */
268 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
272 /* A path may only match one ACL (the first it finds in the config file) */
279 const char* OnRequest(Request* request)
284 virtual ~ModuleHTTPAccessList()
288 virtual Version GetVersion()
290 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
294 MODULE_INIT(ModuleHTTPAccessList)