summaryrefslogtreecommitdiff
path: root/modules/extra/m_xmlrpc.cpp
blob: 4b15499b80acd6054b1684b6868c29422fa16b69 (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
#include "module.h"
#include "ssl.h"
#include "xmlrpc.h"

class MyXMLRPCClientSocket : public XMLRPCClientSocket
{
 public:
	MyXMLRPCClientSocket(XMLRPCListenSocket *ls, int fd, const sockaddrs &addr) : XMLRPCClientSocket(ls, fd, addr)
	{
		if (ls->username.empty() && ls->password.empty())
			this->logged_in = true;
	}

	bool Read(const Anope::string &message)
	{
		this->query += message;
		Log(LOG_DEBUG) << "m_xmlrpc: " << message;

		if (message.find("</methodCall>") != Anope::string::npos)
		{
			this->HandleMessage();
			this->query.clear();
		}
		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) { }
	
	ClientSocket *OnAccept(int fd, const sockaddrs &addr)
	{
		MyXMLRPCClientSocket *socket = new MyXMLRPCClientSocket(this, fd, addr);
		if (std::find(this->allowed.begin(), this->allowed.end(), addr.addr()) != this->allowed.end())
			Log() << "m_xmlrpc: Accepted connection " << fd << " from " << addr.addr();
		else
		{
			Log() << "m_xmlrpc: Dropping disallowed connection " << fd << " from " << addr.addr();
			socket->SetFlag(SF_DEAD);
		}
		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(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("\35", ""), // italics
			special_chars("\031", ""), // underline
			special_chars("\26", ""), // 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
{
	std::vector<MyXMLRPCListenSocket *> listen_sockets;
	service_reference<SSLService> sslref;

 public:
	MyXMLRPCServiceInterface xmlrpcinterface;

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

		OnReload(false);

		ModuleManager::RegisterService(&this->xmlrpcinterface);

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

	~ModuleXMLRPC()
	{
		for (unsigned i = 0; i < this->listen_sockets.size(); ++i)
			delete this->listen_sockets[i];
		this->listen_sockets.clear();
	}

	void OnReload(bool)
	{
		ConfigReader config;

		for (unsigned i = 0; i < this->listen_sockets.size(); ++i)
			delete this->listen_sockets[i];
		this->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);
				}
				this->listen_sockets.push_back(xmls);
			}
			catch (const SocketException &ex)
			{
				Log() << "m_xmlrpc " << ex.GetReason();
			}
		}
	}
};

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

	while (this->GetData(name, 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);
	}

	if (request.name == "login" || this->logged_in)
		me->xmlrpcinterface.RunXMLRPC(this, &request);
}

MODULE_INIT(ModuleXMLRPC)