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 HTTPACL : public Extensible
28 std::string whitelist;
29 std::string blacklist;
31 HTTPACL(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<HTTPACL> 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(HTTPACL(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 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "BlockAccess (%d)", returnval);
103 std::stringstream data("Access to this resource is denied by an access control list. Please contact your IRC administrator.");
104 HTTPDocument response(http->sock, &data, returnval);
105 response.headers.SetHeader("X-Powered-By", "m_httpd_acl.so");
106 if (!extraheaderkey.empty())
107 response.headers.SetHeader(extraheaderkey, extraheaderval);
108 Request req((char*)&response, (Module*)this, event->GetSource());
112 bool IsBase64(unsigned char c)
114 return (isalnum(c) || (c == '+') || (c == '/'));
117 std::string Base64Decode(const std::string &base64)
119 const std::string base64_chars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
120 int inputlen = base64.length();
121 int i = 0, j = 0, input = 0;
122 unsigned char longbuf[4], shortbuf[3];
128 while (inputlen-- && (base64[input] != '=') && IsBase64(base64[input]))
130 longbuf[i++] = base64[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)
142 retval += shortbuf[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)
161 retval += shortbuf[j];
167 void OnEvent(Event* event)
169 if (event->GetEventID() == "httpd_acl")
171 ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd acl event");
172 HTTPRequest* http = (HTTPRequest*)event->GetData();
174 for (std::vector<HTTPACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
176 if (match(http->GetURI(), this_acl->path))
178 if (!this_acl->blacklist.empty())
181 irc::commasepstream sep(this_acl->blacklist);
184 while (sep.GetToken(entry))
186 if (match(http->GetIP(), entry))
188 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to blacklisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
189 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
190 BlockAccess(http, event, 403);
195 if (!this_acl->whitelist.empty())
198 irc::commasepstream sep(this_acl->whitelist);
200 bool allow_access = false;
202 while (sep.GetToken(entry))
204 if (match(http->GetIP(), entry))
210 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to whitelisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
211 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
212 BlockAccess(http, event, 403);
216 if (!this_acl->password.empty() && !this_acl->username.empty())
218 /* Password auth, first look to see if we have a basic authentication header */
219 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Checking HTTP auth password for resource %s (matched by pattern %s) from ip %s, against username %s",
220 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), this_acl->username.c_str());
222 if (http->headers->IsSet("Authorization"))
224 /* Password has been given, validate it */
225 std::string authorization = http->headers->GetHeader("Authorization");
226 irc::spacesepstream sep(authorization);
227 std::string authtype;
230 sep.GetToken(authtype);
231 if (authtype == "Basic")
236 sep.GetToken(base64);
237 std::string userpass = Base64Decode(base64);
238 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: %s (%s)", userpass.c_str(), base64.c_str());
240 irc::sepstream userpasspair(userpass, ':');
241 if (userpasspair.GetToken(user))
243 userpasspair.GetToken(pass);
245 /* Access granted if username and password are correct */
246 if (user == this_acl->username && pass == this_acl->password)
248 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: password and username match");
252 /* Invalid password */
253 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
256 /* Malformed user:pass pair */
257 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
260 /* Unsupported authentication type */
261 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
265 /* No password given at all, access denied */
266 BlockAccess(http, event, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
270 /* A path may only match one ACL (the first it finds in the config file) */
277 const char* OnRequest(Request* request)
282 virtual ~ModuleHTTPAccessList()
286 virtual Version GetVersion()
288 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
292 MODULE_INIT(ModuleHTTPAccessList)