prosody: Enable storage manager.
[prosody.git] / plugins / mod_proxy65.lua
1 -- Copyright (C) 2009 Thilo Cestonaro
2 -- 
3 -- This project is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information.
5 --
6 --[[
7 * to restart the proxy in the console: e.g.
8 module:unload("proxy65");
9 > server.removeserver(<proxy65_port>);
10 module:load("proxy65", <proxy65_jid>);
11 ]]--
12
13 if module:get_host_type() ~= "component" then
14         error("proxy65 should be loaded as a component, please see http://prosody.im/doc/components", 0);
15 end
16
17 local jid_split, jid_join, jid_compare = require "util.jid".split, require "util.jid".join, require "util.jid".compare;
18 local st = require "util.stanza";
19 local componentmanager = require "core.componentmanager";
20 local config_get = require "core.configmanager".get;
21 local connlisteners = require "net.connlisteners";
22 local sha1 = require "util.hashes".sha1;
23 local server = require "net.server";
24
25 local host, name = module:get_host(), "SOCKS5 Bytestreams Service";
26 local sessions, transfers, component, replies_cache = {}, {}, nil, {};
27
28 local proxy_port = config_get(host, "core", "proxy65_port") or 5000;
29 local proxy_interface = config_get(host, "core", "proxy65_interface") or "*";
30 local proxy_address = config_get(host, "core", "proxy65_address") or (proxy_interface ~= "*" and proxy_interface) or host;
31 local proxy_acl = config_get(host, "core", "proxy65_acl");
32 local max_buffer_size = 4096;
33
34 local connlistener = { default_port = proxy_port, default_interface = proxy_interface, default_mode = "*a" };
35
36 function connlistener.onincoming(conn, data)
37         local session = sessions[conn] or {};
38         
39         if session.setup == nil and data ~= nil and data:sub(1):byte() == 0x05 and data:len() > 2 then
40                 local nmethods = data:sub(2):byte();
41                 local methods = data:sub(3);
42                 local supported = false;
43                 for i=1, nmethods, 1 do
44                         if(methods:sub(i):byte() == 0x00) then -- 0x00 == method: NO AUTH
45                                 supported = true;
46                                 break;
47                         end
48                 end
49                 if(supported) then
50                         module:log("debug", "new session found ... ")
51                         session.setup = true;
52                         sessions[conn] = session;
53                         conn:write(string.char(5, 0));
54                 end
55                 return;
56         end
57         if session.setup then
58                 if session.sha ~= nil and transfers[session.sha] ~= nil then
59                         local sha = session.sha;
60                         if transfers[sha].activated == true and transfers[sha].target ~= nil then
61                                 if  transfers[sha].initiator == conn then
62                                         transfers[sha].target:write(data);
63                                 else
64                                         transfers[sha].initiator:write(data);
65                                 end
66                                 return;
67                         end
68                 end
69                 if data ~= nil and data:len() == 0x2F and  -- 40 == length of SHA1 HASH, and 7 other bytes => 47 => 0x2F
70                         data:sub(1):byte() == 0x05 and -- SOCKS5 has 5 in first byte
71                         data:sub(2):byte() == 0x01 and -- CMD must be 1
72                         data:sub(3):byte() == 0x00 and -- RSV must be 0
73                         data:sub(4):byte() == 0x03 and -- ATYP must be 3
74                         data:sub(5):byte() == 40 and -- SHA1 HASH length must be 40 (0x28)
75                         data:sub(-2):byte() == 0x00 and -- PORT must be 0, size 2 byte
76                         data:sub(-1):byte() == 0x00
77                 then
78                         local sha = data:sub(6, 45); -- second param is not count! it's the ending index (included!)
79                         if transfers[sha] == nil then
80                                 transfers[sha] = {};
81                                 transfers[sha].activated = false;
82                                 transfers[sha].target = conn;
83                                 session.sha = sha;
84                                 module:log("debug", "target connected ... ");
85                         elseif transfers[sha].target ~= nil then
86                                 transfers[sha].initiator = conn;
87                                 session.sha = sha;
88                                 module:log("debug", "initiator connected ... ");
89                                 server.link(conn, transfers[sha].target, max_buffer_size);
90                                 server.link(transfers[sha].target, conn, max_buffer_size);
91                         end
92                         conn:write(string.char(5, 0, 0, 3, sha:len()) .. sha .. string.char(0, 0)); -- VER, REP, RSV, ATYP, BND.ADDR (sha), BND.PORT (2 Byte)
93                         conn:lock_read(true)
94                 else
95                         module:log("warn", "Neither data transfer nor initial connect of a participator of a transfer.")
96                         conn:close();
97                 end
98         else
99                 if data ~= nil then
100                         module:log("warn", "unknown connection with no authentication data -> closing it");
101                         conn:close();
102                 end
103         end
104 end
105
106 function connlistener.ondisconnect(conn, err)
107         local session = sessions[conn];
108         if session then
109                 if session.sha and transfers[session.sha] then
110                         local initiator, target = transfers[session.sha].initiator, transfers[session.sha].target;
111                         if initiator == conn and target ~= nil then
112                                 target:close();
113                         elseif target == conn and initiator ~= nil then
114                                 initiator:close();
115                         end
116                         transfers[session.sha] = nil;
117                 end
118                 -- Clean up any session-related stuff here
119                 sessions[conn] = nil;
120         end
121 end
122
123 local function get_disco_info(stanza)
124         local reply = replies_cache.disco_info;
125         if reply == nil then
126                 reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#info")
127                         :tag("identity", {category='proxy', type='bytestreams', name=name}):up()
128                         :tag("feature", {var="http://jabber.org/protocol/bytestreams"});
129                 replies_cache.disco_info = reply;
130         end
131
132         reply.attr.id = stanza.attr.id;
133         reply.attr.to = stanza.attr.from;
134         return reply;
135 end
136
137 local function get_disco_items(stanza)
138         local reply = replies_cache.disco_items;
139         if reply == nil then
140                 reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#items");
141                 replies_cache.disco_items = reply;
142         end
143         
144         reply.attr.id = stanza.attr.id;
145         reply.attr.to = stanza.attr.from;
146         return reply;
147 end
148
149 local function get_stream_host(origin, stanza)
150         local reply = replies_cache.stream_host;
151         local err_reply = replies_cache.stream_host_err;
152         local sid = stanza.tags[1].attr.sid;
153         local allow = false;
154         local jid = stanza.attr.from;
155         
156         if proxy_acl and #proxy_acl > 0 then
157                 for _, acl in ipairs(proxy_acl) do
158                         if jid_compare(jid, acl) then allow = true; end
159                 end
160         else
161                 allow = true;
162         end
163         if allow == true then
164                 if reply == nil then
165                         reply = st.iq({type="result", from=host})
166                                 :query("http://jabber.org/protocol/bytestreams")
167                                 :tag("streamhost", {jid=host, host=proxy_address, port=proxy_port});
168                         replies_cache.stream_host = reply;
169                 end
170         else
171                 module:log("warn", "Denying use of proxy for %s", tostring(jid));
172                 if err_reply == nil then
173                         err_reply = st.iq({type="error", from=host})
174                                 :query("http://jabber.org/protocol/bytestreams")
175                                 :tag("error", {code='403', type='auth'})
176                                 :tag("forbidden", {xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'});
177                         replies_cache.stream_host_err = err_reply;
178                 end
179                 reply = err_reply;
180         end
181         reply.attr.id = stanza.attr.id;
182         reply.attr.to = stanza.attr.from;
183         reply.tags[1].attr.sid = sid;
184         return reply;
185 end
186
187 module.unload = function()
188         componentmanager.deregister_component(host);
189         connlisteners.deregister(module.host .. ':proxy65');
190 end
191
192 local function set_activation(stanza)
193         local from, to, sid, reply = nil;
194         from = stanza.attr.from;
195         if stanza.tags[1] ~= nil and tostring(stanza.tags[1].name) == "query" then
196                 if stanza.tags[1].attr ~= nil then
197                         sid = stanza.tags[1].attr.sid;
198                 end
199                 if stanza.tags[1].tags[1] ~= nil and tostring(stanza.tags[1].tags[1].name) == "activate" then
200                         to = stanza.tags[1].tags[1][1];
201                 end
202         end
203         if from ~= nil and to ~= nil and sid ~= nil then
204                 reply = st.iq({type="result", from=host, to=from});
205                 reply.attr.id = stanza.attr.id;
206         end
207         return reply, from, to, sid;
208 end
209
210 function handle_to_domain(origin, stanza)
211         local to_node, to_host, to_resource = jid_split(stanza.attr.to);
212         if to_node == nil then
213                 local type = stanza.attr.type;
214                 if type == "error" or type == "result" then return; end
215                 if stanza.name == "iq" and type == "get" then
216                         local xmlns = stanza.tags[1].attr.xmlns
217                         if xmlns == "http://jabber.org/protocol/disco#info" then
218                                 origin.send(get_disco_info(stanza));
219                                 return true;
220                         elseif xmlns == "http://jabber.org/protocol/disco#items" then
221                                 origin.send(get_disco_items(stanza));
222                                 return true;
223                         elseif xmlns == "http://jabber.org/protocol/bytestreams" then
224                                 origin.send(get_stream_host(origin, stanza));
225                                 return true;
226                         else
227                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
228                                 return true;
229                         end
230                 elseif stanza.name == "iq" and type == "set" then
231                         module:log("debug", "Received activation request from %s", stanza.attr.from);
232                         local reply, from, to, sid = set_activation(stanza);
233                         if reply ~= nil and from ~= nil and to ~= nil and sid ~= nil then
234                                 local sha = sha1(sid .. from .. to, true);
235                                 if transfers[sha] == nil then
236                                         module:log("error", "transfers[sha]: nil");
237                                 elseif(transfers[sha] ~= nil and transfers[sha].initiator ~= nil and transfers[sha].target ~= nil) then
238                                         origin.send(reply);
239                                         transfers[sha].activated = true;
240                                         transfers[sha].target:lock_read(false);
241                                         transfers[sha].initiator:lock_read(false);
242                                 else
243                                         module:log("debug", "Both parties were not yet connected");
244                                         local message = "Neither party is connected to the proxy";
245                                         if transfers[sha].initiator then
246                                                 message = "The recipient is not connected to the proxy";
247                                         elseif transfers[sha].target then
248                                                 message = "The sender (you) is not connected to the proxy";
249                                         end
250                                         origin.send(st.error_reply(stanza, "cancel", "not-allowed", message));
251                                 end
252                         else
253                                 module:log("error", "activation failed: sid: %s, initiator: %s, target: %s", tostring(sid), tostring(from), tostring(to));
254                         end
255                 end
256         end
257         return;
258 end
259
260 if not connlisteners.register(module.host .. ':proxy65', connlistener) then
261         module:log("error", "mod_proxy65: Could not establish a connection listener. Check your configuration please.");
262         module:log("error", "Possibly two proxy65 components are configured to share the same port.");
263 end
264
265 connlisteners.start(module.host .. ':proxy65');
266 component = componentmanager.register_component(host, handle_to_domain);