2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 std::set<FlashPDSocket*> sockets;
27 std::string policy_reply;
28 const std::string expected_request("<policy-file-request/>\0", 23);
31 class FlashPDSocket : public BufferedSocket
36 FlashPDSocket(int newfd)
37 : BufferedSocket(newfd)
38 , created(ServerInstance->Time())
47 void OnError(BufferedSocketError) CXX11_OVERRIDE
52 void OnDataReady() CXX11_OVERRIDE
54 if (recvq == expected_request)
55 WriteData(policy_reply);
66 ServerInstance->GlobalCulls.AddItem(this);
70 class ModuleFlashPD : public Module
75 void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
77 for (std::set<FlashPDSocket*>::const_iterator i = sockets.begin(); i != sockets.end(); ++i)
79 FlashPDSocket* sock = *i;
80 if ((sock->created + timeout <= curtime) && (sock->created != 0))
85 ModResult OnAcceptConnection(int nfd, ListenSocket* from, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) CXX11_OVERRIDE
87 if (from->bind_tag->getString("type") != "flashpolicyd")
88 return MOD_RES_PASSTHRU;
90 if (policy_reply.empty())
93 sockets.insert(new FlashPDSocket(nfd));
97 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
99 ConfigTag* tag = ServerInstance->Config->ConfValue("flashpolicyd");
100 timeout = tag->getInt("timeout", 5, 1);
101 std::string file = tag->getString("file");
107 FileReader reader(file);
108 policy_reply = reader.GetString();
110 catch (CoreException&)
112 const std::string error_message = "A file was specified for FlashPD, but it could not be loaded.";
113 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, error_message);
114 ServerInstance->SNO->WriteGlobalSno('a', error_message);
115 policy_reply.clear();
120 // A file was not specified. Set the default setting.
121 // We allow access to all client ports by default
122 std::string to_ports;
123 for (std::vector<ListenSocket*>::const_iterator i = ServerInstance->ports.begin(); i != ServerInstance->ports.end(); ++i)
125 ListenSocket* ls = *i;
126 if (ls->bind_tag->getString("type", "clients") != "clients" || ls->bind_tag->getString("ssl", "plaintext") != "plaintext")
129 to_ports.append(ConvToStr(ls->bind_port)).push_back(',');
131 to_ports.erase(to_ports.size() - 1);
134 "<?xml version=\"1.0\"?>\
135 <!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">\
136 <cross-domain-policy>\
137 <site-control permitted-cross-domain-policies=\"master-only\"/>\
138 <allow-access-from domain=\"*\" to-ports=\"" + to_ports + "\" />\
139 </cross-domain-policy>";
144 for (std::set<FlashPDSocket*>::const_iterator i = sockets.begin(); i != sockets.end(); ++i)
146 FlashPDSocket* sock = *i;
149 return Module::cull();
152 Version GetVersion() CXX11_OVERRIDE
154 return Version("Flash Policy Daemon. Allows Flash IRC clients to connect", VF_VENDOR);
158 MODULE_INIT(ModuleFlashPD)