1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 /* $ModDesc: Provides access control lists (passwording of resources, ip restrictions etc) to m_httpd.so dependent modules */
19 /* $ModDep: httpd.h */
21 class HTTPACL : public Extensible
27 std::string whitelist;
28 std::string blacklist;
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) { }
38 class ModuleHTTPAccessList : public Module
41 std::string stylesheet;
43 std::vector<HTTPACL> acl_list;
51 int n_items = c.Enumerate("httpdacl");
52 for (int i = 0; i < n_items; ++i)
54 std::string path = c.ReadValue("httpdacl", "path", i);
55 std::string types = c.ReadValue("httpdacl", "types", i);
56 irc::commasepstream sep(types);
60 std::string whitelist;
61 std::string blacklist;
63 while (sep.GetToken(type))
65 if (type == "password")
67 username = c.ReadValue("httpdacl", "username", i);
68 password = c.ReadValue("httpdacl", "password", i);
70 else if (type == "whitelist")
72 whitelist = c.ReadValue("httpdacl", "whitelist", i);
74 else if (type == "blacklist")
76 blacklist = c.ReadValue("httpdacl", "blacklist", i);
80 throw ModuleException("Invalid HTTP ACL type '" + type + "'");
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());
87 acl_list.push_back(HTTPACL(path, username, password, whitelist, blacklist));
91 ModuleHTTPAccessList() {
93 Implementation eventlist[] = { I_OnEvent, I_OnRequest };
94 ServerInstance->Modules->Attach(eventlist, this, 2);
97 void BlockAccess(HTTPRequest* http, Event* event, int returnval, const std::string &extraheaderkey = "", const std::string &extraheaderval="")
99 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "BlockAccess (%d)", returnval);
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+/");
118 int inputlen = base64.length();
119 int i = 0, j = 0, input = 0;
120 unsigned char longbuf[4], shortbuf[3];
126 while (inputlen-- && (base64[input] != '=') && IsBase64(base64[input]))
128 longbuf[i++] = base64[input];
132 for (i = 0; i < 4; ++i)
133 longbuf[i] = base64_chars.find(longbuf[i]);
135 shortbuf[0] = (longbuf[0] << 2) + ((longbuf[1] & 0x30) >> 4);
136 shortbuf[1] = ((longbuf[1] & 0xf) << 4) + ((longbuf[2] & 0x3c) >> 2);
137 shortbuf[2] = ((longbuf[2] & 0x3) << 6) + longbuf[3];
139 for (i = 0; i < 3; ++i)
140 retval += shortbuf[i];
148 for (j = i; j < 4; ++j)
151 for (j = 0; j < 4; ++j)
152 longbuf[j] = base64_chars.find(longbuf[j]);
154 shortbuf[0] = (longbuf[0] << 2) + ((longbuf[1] & 0x30) >> 4);
155 shortbuf[1] = ((longbuf[1] & 0xf) << 4) + ((longbuf[2] & 0x3c) >> 2);
156 shortbuf[2] = ((longbuf[2] & 0x3) << 6) + longbuf[3];
158 for (j = 0; j < i - 1; ++j)
159 retval += shortbuf[j];
165 void OnEvent(Event* event)
167 if (event->GetEventID() == "httpd_acl")
169 ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd acl event");
170 HTTPRequest* http = (HTTPRequest*)event->GetData();
172 for (std::vector<HTTPACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
174 if (InspIRCd::Match(http->GetURI(), this_acl->path, ascii_case_insensitive_map))
176 if (!this_acl->blacklist.empty())
179 irc::commasepstream sep(this_acl->blacklist);
182 while (sep.GetToken(entry))
184 if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
186 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to blacklisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
187 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
188 BlockAccess(http, event, 403);
193 if (!this_acl->whitelist.empty())
196 irc::commasepstream sep(this_acl->whitelist);
198 bool allow_access = false;
200 while (sep.GetToken(entry))
202 if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
208 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to whitelisted resource %s (matched by pattern %s) from ip %s (Not in whitelist)",
209 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str());
210 BlockAccess(http, event, 403);
214 if (!this_acl->password.empty() && !this_acl->username.empty())
216 /* Password auth, first look to see if we have a basic authentication header */
217 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Checking HTTP auth password for resource %s (matched by pattern %s) from ip %s, against username %s",
218 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), this_acl->username.c_str());
220 if (http->headers->IsSet("Authorization"))
222 /* Password has been given, validate it */
223 std::string authorization = http->headers->GetHeader("Authorization");
224 irc::spacesepstream sep(authorization);
225 std::string authtype;
228 sep.GetToken(authtype);
229 if (authtype == "Basic")
234 sep.GetToken(base64);
235 std::string userpass = Base64Decode(base64);
236 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: %s (%s)", userpass.c_str(), base64.c_str());
238 irc::sepstream userpasspair(userpass, ':');
239 if (userpasspair.GetToken(user))
241 userpasspair.GetToken(pass);
243 /* Access granted if username and password are correct */
244 if (user == this_acl->username && pass == this_acl->password)
246 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: password and username match");
250 /* Invalid password */
251 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
254 /* Malformed user:pass pair */
255 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
258 /* Unsupported authentication type */
259 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
263 /* No password given at all, access denied */
264 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
268 /* A path may only match one ACL (the first it finds in the config file) */
275 const char* OnRequest(Request* request)
280 virtual ~ModuleHTTPAccessList()
284 virtual Version GetVersion()
286 return Version("Provides access control lists (passwording of resources, ip restrictions etc) to m_httpd.so dependent modules", VF_VENDOR, API_VERSION);
290 MODULE_INIT(ModuleHTTPAccessList)