1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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 */
26 std::string whitelist;
27 std::string blacklist;
29 HTTPACL(const std::string &set_path, const std::string &set_username, const std::string &set_password,
30 const std::string &set_whitelist, const std::string &set_blacklist)
31 : path(set_path), username(set_username), password(set_password), whitelist(set_whitelist),
32 blacklist(set_blacklist) { }
37 class ModuleHTTPAccessList : public Module
40 std::string stylesheet;
42 std::vector<HTTPACL> acl_list;
49 ConfigTagList acls = ServerInstance->Config->ConfTags("httpdacl");
50 for (ConfigIter i = acls.first; i != acls.second; i++)
52 ConfigTag* c = i->second;
53 std::string path = c->getString("path");
54 std::string types = c->getString("types");
55 irc::commasepstream sep(types);
59 std::string whitelist;
60 std::string blacklist;
62 while (sep.GetToken(type))
64 if (type == "password")
66 username = c->getString("username");
67 password = c->getString("password");
69 else if (type == "whitelist")
71 whitelist = c->getString("whitelist");
73 else if (type == "blacklist")
75 blacklist = c->getString("blacklist");
79 throw ModuleException("Invalid HTTP ACL type '" + type + "'");
83 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Read ACL: path=%s pass=%s whitelist=%s blacklist=%s", path.c_str(),
84 password.c_str(), whitelist.c_str(), blacklist.c_str());
86 acl_list.push_back(HTTPACL(path, username, password, whitelist, blacklist));
90 ModuleHTTPAccessList() {
92 Implementation eventlist[] = { I_OnEvent };
93 ServerInstance->Modules->Attach(eventlist, this, 1);
96 void BlockAccess(HTTPRequest* http, int returnval, const std::string &extraheaderkey = "", const std::string &extraheaderval="")
98 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "BlockAccess (%d)", returnval);
100 std::stringstream data("Access to this resource is denied by an access control list. Please contact your IRC administrator.");
101 HTTPDocumentResponse response(this, *http, &data, returnval);
102 response.headers.SetHeader("X-Powered-By", "m_httpd_acl.so");
103 if (!extraheaderkey.empty())
104 response.headers.SetHeader(extraheaderkey, extraheaderval);
108 void OnEvent(Event& event)
110 if (event.id == "httpd_acl")
112 ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd acl event");
113 HTTPRequest* http = (HTTPRequest*)&event;
115 for (std::vector<HTTPACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
117 if (InspIRCd::Match(http->GetURI(), this_acl->path, ascii_case_insensitive_map))
119 if (!this_acl->blacklist.empty())
122 irc::commasepstream sep(this_acl->blacklist);
125 while (sep.GetToken(entry))
127 if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
129 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to blacklisted resource %s (matched by pattern %s) from ip %s (matched by entry %s)",
130 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), entry.c_str());
131 BlockAccess(http, 403);
136 if (!this_acl->whitelist.empty())
139 irc::commasepstream sep(this_acl->whitelist);
141 bool allow_access = false;
143 while (sep.GetToken(entry))
145 if (InspIRCd::Match(http->GetIP(), entry, ascii_case_insensitive_map))
151 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Denying access to whitelisted resource %s (matched by pattern %s) from ip %s (Not in whitelist)",
152 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str());
153 BlockAccess(http, 403);
157 if (!this_acl->password.empty() && !this_acl->username.empty())
159 /* Password auth, first look to see if we have a basic authentication header */
160 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "Checking HTTP auth password for resource %s (matched by pattern %s) from ip %s, against username %s",
161 http->GetURI().c_str(), this_acl->path.c_str(), http->GetIP().c_str(), this_acl->username.c_str());
163 if (http->headers->IsSet("Authorization"))
165 /* Password has been given, validate it */
166 std::string authorization = http->headers->GetHeader("Authorization");
167 irc::spacesepstream sep(authorization);
168 std::string authtype;
171 sep.GetToken(authtype);
172 if (authtype == "Basic")
177 sep.GetToken(base64);
178 std::string userpass = Base64ToBin(base64);
179 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: %s (%s)", userpass.c_str(), base64.c_str());
181 irc::sepstream userpasspair(userpass, ':');
182 if (userpasspair.GetToken(user))
184 userpasspair.GetToken(pass);
186 /* Access granted if username and password are correct */
187 if (user == this_acl->username && pass == this_acl->password)
189 ServerInstance->Logs->Log("m_httpd_acl", DEBUG, "HTTP authorization: password and username match");
193 /* Invalid password */
194 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
197 /* Malformed user:pass pair */
198 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
201 /* Unsupported authentication type */
202 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
206 /* No password given at all, access denied */
207 BlockAccess(http, 401, "WWW-Authenticate", "Basic realm=\"Restricted Object\"");
211 /* A path may only match one ACL (the first it finds in the config file) */
218 virtual ~ModuleHTTPAccessList()
222 virtual Version GetVersion()
224 return Version("Provides access control lists (passwording of resources, ip restrictions etc) to m_httpd.so dependent modules", VF_VENDOR);
228 MODULE_INIT(ModuleHTTPAccessList)