summaryrefslogtreecommitdiff
path: root/modules/extra/m_xmlrpc.cpp
blob: 215be9d779e71f42218453a4037188de4b6bfd2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "module.h"
#include "ssl.h"
#include "xmlrpc.h"

std::vector<XMLRPCListenSocket *> listen_sockets;

class MyXMLRPCClientSocket : public XMLRPCClientSocket
{
	/* Used to skip the (optional) HTTP header,  which we really don't care about */
	bool in_query;
 public:
	MyXMLRPCClientSocket(XMLRPCListenSocket *ls, int fd, const sockaddrs &addr) : Socket(fd, ls->IsIPv6()), XMLRPCClientSocket(ls, addr), in_query(false)
	{
	}

	bool Read(const Anope::string &message)
	{
		if (message.find("xml version") != Anope::string::npos)
			this->in_query = true;
		else if (!this->in_query)
			return true;

		this->query += message;
		Log(LOG_DEBUG) << "m_xmlrpc: " << message;

		if (message.find("</methodCall>") != Anope::string::npos)
		{
			Log(LOG_DEBUG) << "m_xmlrpc: Processing message";
			this->HandleMessage();
		}

		return true;
	}

	bool GetData(Anope::string &tag, Anope::string &data)
	{
		if (this->query.empty())
			return false;

		Anope::string prev, cur;
		bool istag;

		do
		{
			prev = cur;
			cur.clear();

			int len = 0;
			istag = false;

			if (this->query[0] == '<')
			{
				len = this->query.find_first_of('>');
				istag = true;
			}
			else if (this->query[0] != '>')
			{
				len = this->query.find_first_of('<');
			}

			if (len)
			{
				if (istag)
				{
					cur = this->query.substr(1, len - 1);
					this->query.erase(0, len + 1);
					while (!this->query.empty() && this->query[0] == ' ')
						this->query.erase(this->query.begin());
				}
				else
				{
					cur = this->query.substr(0,len);
					this->query.erase(0, len);
				}
			}
		}
		while (istag && !this->query.empty());

		tag = prev;
		data = cur;
		return !istag && !data.empty();
	}
	
	void HandleMessage();
};

class MyXMLRPCListenSocket : public XMLRPCListenSocket
{
 public:
	MyXMLRPCListenSocket(const Anope::string &bindip, int port, bool ipv6, const Anope::string &u, const Anope::string &p, const std::vector<Anope::string> &a) : XMLRPCListenSocket(bindip, port, ipv6, u, p, a)
	{
		listen_sockets.push_back(this);
	}

	~MyXMLRPCListenSocket()
	{
		std::vector<XMLRPCListenSocket *>::iterator it = std::find(listen_sockets.begin(), listen_sockets.end(), this);
		if (it != listen_sockets.end())
			listen_sockets.erase(it);
	}
	
	ClientSocket *OnAccept(int fd, const sockaddrs &addr)
	{
		MyXMLRPCClientSocket *socket = new MyXMLRPCClientSocket(this, fd, addr);

		socket->SetFlag(SF_DEAD);
		for (unsigned i = 0, j = this->allowed.size(); i < j; ++i)
		{
			try
			{
				cidr cidr_mask(this->allowed[i]);
				if (cidr_mask.match(socket->clientaddr))
				{
					Log() << "m_xmlrpc: Accepted connection " << fd << " from " << addr.addr();
					socket->UnsetFlag(SF_DEAD);
					break;
				}
			}
			catch (const SocketException &ex)
			{
				Log() << "m_xmlrpc: Error checking incoming connection against mask " << this->allowed[i] << ": " << ex.GetReason();
			}
		}

		if (socket->HasFlag(SF_DEAD))
			Log() << "m_xmlrpc: Dropping disallowed connection " << fd << " from " << addr.addr();

		return socket;
	}
};

class MyXMLRPCServiceInterface : public XMLRPCServiceInterface
{
	std::deque<XMLRPCEvent *> events;

 public:
	MyXMLRPCServiceInterface(Module *creator, const Anope::string &sname) : XMLRPCServiceInterface(creator, sname) { }

	void Register(XMLRPCEvent *event)
	{
		this->events.push_back(event);
	}

	void Unregister(XMLRPCEvent *event)
	{
		std::deque<XMLRPCEvent *>::iterator it = std::find(this->events.begin(), this->events.end(), event);

		if (it != this->events.end())
			this->events.erase(it);
	}

	void Reply(XMLRPCClientSocket *source, XMLRPCRequest *request)
	{
		if (!request->id.empty())
			request->reply("id", request->id);

		Anope::string reply = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<methodCall>\n<methodName>" + request->name + "</methodName>\n<params>\n<param>\n<value>\n<struct>\n";
		for (std::map<Anope::string, Anope::string>::const_iterator it = request->get_replies().begin(); it != request->get_replies().end(); ++it)
		{
			reply += "<member>\n<name>" + it->first + "</name>\n<value>\n<string>" + this->Sanitize(it->second) + "</string>\n</value>\n</member>\n";
		}
		reply += "</struct>\n</value>\n</param>\n</params>\n</methodCall>";

		source->Write("HTTP/1.1 200 OK");
		source->Write("Connection: close");
		source->Write("Content-Type: text/xml");
		source->Write("Content-Length: " + stringify(reply.length()));
		source->Write("Server: Anope IRC Services version " + Anope::VersionShort());
		source->Write("");
		
		source->Write(reply);

		Log(LOG_DEBUG) << reply;
	}

	Anope::string Sanitize(const Anope::string &string)
	{
		static struct special_chars
		{
			Anope::string character;
			Anope::string replace;

			special_chars(const Anope::string &c, const Anope::string &r) : character(c), replace(r) { }
		}
		special[] = {
			special_chars("&", "&amp;"),
			special_chars("\"", "&quot;"),
			special_chars("<", "&lt;"),
			special_chars(">", "&qt;"),
			special_chars("'", "&#39;"),
			special_chars("\n", "&#xA;"),
			special_chars("\002", ""), // bold
			special_chars("\003", ""), // color
			special_chars("\035", ""), // italics
			special_chars("\037", ""), // underline
			special_chars("\026", ""), // reverses
			special_chars("", "")
		};

		Anope::string ret = string;
		for (int i = 0; special[i].character.empty() == false; ++i)
			ret = ret.replace_all_cs(special[i].character, special[i].replace);
		return ret;
	}

	void RunXMLRPC(XMLRPCClientSocket *source, XMLRPCRequest *request)
	{
		for (unsigned i = 0; i < this->events.size(); ++i)
		{
			XMLRPCEvent *e = this->events[i];

			e->Run(this, source, request);
			if (!request->get_replies().empty())
				this->Reply(source, request);
		}
	}
};

class ModuleXMLRPC;
static ModuleXMLRPC *me;
class ModuleXMLRPC : public Module
{
	service_reference<SSLService> sslref;

 public:
	MyXMLRPCServiceInterface xmlrpcinterface;

	ModuleXMLRPC(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, SUPPORTED), sslref("SSLService", "ssl"), xmlrpcinterface(this, "xmlrpc")
	{
		me = this;


		Implementation i[] = { I_OnReload };
		ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
		
		OnReload();
	}

	~ModuleXMLRPC()
	{
		/* Clean up our sockets and our listening sockets */
		for (std::map<int, Socket *>::const_iterator it = SocketEngine::Sockets.begin(), it_end = SocketEngine::Sockets.end(); it != it_end;)
		{
			Socket *s = it->second;
			++it;

			ClientSocket *cs = dynamic_cast<ClientSocket *>(s);
			if (cs != NULL)
			{
				for (unsigned i = 0; i < listen_sockets.size(); ++i)
					if (cs->LS == listen_sockets[i])
					{
						delete cs;
						break;
					}
			}
		}

		for (unsigned i = 0; i < listen_sockets.size(); ++i)
			delete listen_sockets[i];
		listen_sockets.clear();
	}

	void OnReload()
	{
		ConfigReader config;

		for (unsigned i = 0; i < listen_sockets.size(); ++i)
			delete listen_sockets[i];
		listen_sockets.clear();

		for (int i = 0; i < config.Enumerate("m_xmlrpc"); ++i)
		{
			Anope::string bindip = config.ReadValue("m_xmlrpc", "bindip", "0.0.0.0", i);
			int port = config.ReadInteger("m_xmlrpc", "port", 0, i);
			bool ipv6 = config.ReadFlag("m_xmlrpc", "ipv6", "no", i);
			bool ssl = config.ReadFlag("m_xmlrpc", "ssl", "no", i);
			Anope::string allowed = config.ReadValue("m_xmlrpc", "allowed", "127.0.0.1", i);
			Anope::string username = config.ReadValue("m_xmlrpc", "username", "", i);
			Anope::string password = config.ReadValue("m_xmlrpc", "password", "", i);
			std::vector<Anope::string> allowed_vector = BuildStringVector(allowed, ' ');

			if (bindip.empty() || port < 1)
				continue;

			if (ssl && !sslref)
			{
				Log() << "m_xmlrpc: Could not enable SSL, is m_ssl loaded?";
				ssl = false;
			}

			try
			{
				MyXMLRPCListenSocket *xmls = new MyXMLRPCListenSocket(bindip, port, ipv6, username, password, allowed_vector);
				if (ssl)
					sslref->Init(xmls);
			}
			catch (const SocketException &ex)
			{
				Log() << "m_xmlrpc " << ex.GetReason();
			}
		}
	}
};

void MyXMLRPCClientSocket::HandleMessage()
{
	Anope::string name, data;
	XMLRPCRequest request;

	while (this->GetData(name, data))
	{
		Log(LOG_DEBUG) << "m_xmlrpc: Tag name: " << name << ", data: " << data;
		if (name == "methodName")
			request.name = data;
		else if (name == "name" && data == "id")
		{
			this->GetData(name, data);
			request.id = data;
		}
		else if (name == "string")
			request.data.push_back(data);
	}

	me->xmlrpcinterface.RunXMLRPC(this, &request);
}

MODULE_INIT(ModuleXMLRPC)