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
27 std::string whitelist;
28 std::string blacklist;
30 ACL(const std::string &set_path, const std::string &set_password,
31 const std::string &set_whitelist, const std::string &set_blacklist)
32 : path(set_path), password(set_password), whitelist(set_whitelist),
33 blacklist(set_blacklist) { }
38 class ModuleHTTPAccessList : public Module
41 std::string stylesheet;
43 std::vector<ACL> acl_list;
50 ConfigReader c(ServerInstance);
51 int n_items = c.Enumerate("httpacl");
52 for (int i = 0; i < n_items; ++i)
54 std::string path = c.ReadValue("httpacl", "path", i);
55 std::string types = c.ReadValue("httpacl", "types", i);
56 irc::commasepstream sep(types);
59 std::string whitelist;
60 std::string blacklist;
62 while (sep.GetToken(type))
64 if (type == "password")
66 password = c.ReadValue("httpacl", "password", i);
68 else if (type == "whitelist")
70 whitelist = c.ReadValue("httpacl", "whitelist", i);
72 else if (type == "blacklist")
74 blacklist = c.ReadValue("httpacl", "blacklist", i);
78 throw ModuleException("Invalid HTTP ACL type '" + type + "'");
82 acl_list.push_back(ACL(path, password, whitelist, blacklist));
86 ModuleHTTPAccessList(InspIRCd* Me) : Module(Me)
90 Implementation eventlist[] = { I_OnEvent, I_OnRequest };
91 ServerInstance->Modules->Attach(eventlist, this, 2);
94 void BlockAccess(HTTPRequest* http, Event* event)
96 std::stringstream data("Access to this resource is denied by an access control list. Please contact your IRC administrator.");
97 HTTPDocument response(http->sock, &data, 403);
98 response.headers.SetHeader("X-Powered-By", "m_httpd_acl.so");
99 Request req((char*)&response, (Module*)this, event->GetSource());
103 void OnEvent(Event* event)
105 std::stringstream data("");
107 if (event->GetEventID() == "httpd_url")
109 ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
110 HTTPRequest* http = (HTTPRequest*)event->GetData();
112 for (std::vector<ACL>::const_iterator this_acl = acl_list.begin(); this_acl != acl_list.end(); ++this_acl)
114 if (match(http->GetURI(), this_acl->path))
116 if (!this_acl->blacklist.empty())
119 irc::commasepstream sep(this_acl->blacklist);
122 while (sep.GetToken(entry))
124 if (match(http->GetIP(), entry))
126 BlockAccess(http, event);
131 if (!this_acl->whitelist.empty())
134 irc::commasepstream sep(this_acl->whitelist);
136 bool allow_access = false;
138 while (sep.GetToken(entry))
140 if (match(http->GetIP(), entry))
146 BlockAccess(http, event);
150 if (!this_acl->password.empty())
152 /* Password auth, first look to see if we have a basic authentication header */
159 const char* OnRequest(Request* request)
164 virtual ~ModuleHTTPAccessList()
168 virtual Version GetVersion()
170 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
174 MODULE_INIT(ModuleHTTPAccessList)