mod_httpserver: Rename to mod_http_files
[prosody.git] / plugins / mod_proxy65.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2011 Matthew Wild
3 -- Copyright (C) 2008-2011 Waqas Hussain
4 -- Copyright (C) 2009 Thilo Cestonaro
5 -- 
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9 --[[
10 * to restart the proxy in the console: e.g.
11 module:unload("proxy65");
12 > server.removeserver(<proxy65_port>);
13 module:load("proxy65", <proxy65_jid>);
14 ]]--
15
16
17 local module = module;
18 local tostring = tostring;
19 local jid_compare, jid_prep = require "util.jid".compare, require "util.jid".prep;
20 local st = require "util.stanza";
21 local connlisteners = require "net.connlisteners";
22 local sha1 = require "util.hashes".sha1;
23 local server = require "net.server";
24 local b64 = require "util.encodings".base64.encode;
25
26 local host, name = module:get_host(), "SOCKS5 Bytestreams Service";
27 local sessions, transfers = {}, {};
28
29 local proxy_port = module:get_option("proxy65_port") or 5000;
30 local proxy_interface = module:get_option("proxy65_interface") or "*";
31 local proxy_address = module:get_option("proxy65_address") or (proxy_interface ~= "*" and proxy_interface) or host;
32 local proxy_acl = module:get_option("proxy65_acl");
33 local max_buffer_size = 4096;
34
35 local connlistener = { default_port = proxy_port, default_interface = proxy_interface, default_mode = "*a" };
36
37 function connlistener.onincoming(conn, data)
38         local session = sessions[conn] or {};
39
40         local transfer = transfers[session.sha];
41         if transfer and transfer.activated then -- copy data between initiator and target
42                 local initiator, target = transfer.initiator, transfer.target;
43                 (conn == initiator and target or initiator):write(data);
44                 return;
45         end -- FIXME server.link should be doing this?
46         
47         if not session.greeting_done then
48                 local nmethods = data:byte(2) or 0;
49                 if data:byte(1) == 0x05 and nmethods > 0 and #data == 2 + nmethods then -- check if we have all the data
50                         if data:find("%z") then -- 0x00 = 'No authentication' is supported
51                                 session.greeting_done = true;
52                                 sessions[conn] = session;
53                                 conn:write("\5\0"); -- send (SOCKS version 5, No authentication)
54                                 module:log("debug", "SOCKS5 greeting complete");
55                                 return;
56                         end
57                 end -- else error, unexpected input
58                 conn:write("\5\255"); -- send (SOCKS version 5, no acceptable method)
59                 conn:close();
60                 module:log("debug", "Invalid SOCKS5 greeting recieved: '%s'", b64(data));
61         else -- connection request
62                 --local head = string.char( 0x05, 0x01, 0x00, 0x03, 40 ); -- ( VER=5=SOCKS5, CMD=1=CONNECT, RSV=0=RESERVED, ATYP=3=DOMAIMNAME, SHA-1 size )
63                 if #data == 47 and data:sub(1,5) == "\5\1\0\3\40" and data:sub(-2) == "\0\0" then
64                         local sha = data:sub(6, 45);
65                         conn:pause();
66                         conn:write("\5\0\0\3\40" .. sha .. "\0\0"); -- VER, REP, RSV, ATYP, BND.ADDR (sha), BND.PORT (2 Byte)
67                         if not transfers[sha] then
68                                 transfers[sha] = {};
69                                 transfers[sha].target = conn;
70                                 session.sha = sha;
71                                 module:log("debug", "SOCKS5 target connected for session %s", sha);
72                         else -- transfers[sha].target ~= nil
73                                 transfers[sha].initiator = conn;
74                                 session.sha = sha;
75                                 module:log("debug", "SOCKS5 initiator connected for session %s", sha);
76                                 server.link(conn, transfers[sha].target, max_buffer_size);
77                                 server.link(transfers[sha].target, conn, max_buffer_size);
78                         end
79                 else -- error, unexpected input
80                         conn:write("\5\1\0\3\0\0\0"); -- VER, REP, RSV, ATYP, BND.ADDR (sha), BND.PORT (2 Byte)
81                         conn:close();
82                         module:log("debug", "Invalid SOCKS5 negotiation recieved: '%s'", b64(data));
83                 end
84         end
85 end
86
87 function connlistener.ondisconnect(conn, err)
88         local session = sessions[conn];
89         if session then
90                 if transfers[session.sha] then
91                         local initiator, target = transfers[session.sha].initiator, transfers[session.sha].target;
92                         if initiator == conn and target ~= nil then
93                                 target:close();
94                         elseif target == conn and initiator ~= nil then
95                                 initiator:close();
96                         end
97                         transfers[session.sha] = nil;
98                 end
99                 -- Clean up any session-related stuff here
100                 sessions[conn] = nil;
101         end
102 end
103
104 module:add_identity("proxy", "bytestreams", name);
105 module:add_feature("http://jabber.org/protocol/bytestreams");
106
107 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function(event)
108         local origin, stanza = event.origin, event.stanza;
109         origin.send(st.reply(stanza):query("http://jabber.org/protocol/disco#info")
110                 :tag("identity", {category='proxy', type='bytestreams', name=name}):up()
111                 :tag("feature", {var="http://jabber.org/protocol/bytestreams"}) );
112         return true;
113 end, -1);
114
115 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function(event)
116         local origin, stanza = event.origin, event.stanza;
117         origin.send(st.reply(stanza):query("http://jabber.org/protocol/disco#items"));
118         return true;
119 end, -1);
120
121 module:hook("iq-get/host/http://jabber.org/protocol/bytestreams:query", function(event)
122         local origin, stanza = event.origin, event.stanza;
123         
124         -- check ACL
125         while proxy_acl and #proxy_acl > 0 do -- using 'while' instead of 'if' so we can break out of it
126                 local jid = stanza.attr.from;
127                 for _, acl in ipairs(proxy_acl) do
128                         if jid_compare(jid, acl) then break; end
129                 end
130                 module:log("warn", "Denying use of proxy for %s", tostring(stanza.attr.from));
131                 origin.send(st.error_reply(stanza, "auth", "forbidden"));
132                 return true;
133         end
134
135         local sid = stanza.tags[1].attr.sid;
136         origin.send(st.reply(stanza):tag("query", {xmlns="http://jabber.org/protocol/bytestreams", sid=sid})
137                 :tag("streamhost", {jid=host, host=proxy_address, port=proxy_port}));
138         return true;
139 end);
140
141 module.unload = function()
142         connlisteners.deregister(module.host .. ':proxy65');
143 end
144
145 module:hook("iq-set/host/http://jabber.org/protocol/bytestreams:query", function(event)
146         local origin, stanza = event.origin, event.stanza;
147
148         local query = stanza.tags[1];
149         local sid = query.attr.sid;
150         local from = stanza.attr.from;
151         local to = query:get_child_text("activate");
152         local prepped_to = jid_prep(to);
153
154         local info = "sid: "..tostring(sid)..", initiator: "..tostring(from)..", target: "..tostring(prepped_to or to);
155         if prepped_to and sid then
156                 local sha = sha1(sid .. from .. prepped_to, true);
157                 if not transfers[sha] then
158                         module:log("debug", "Activation request has unknown session id; activation failed (%s)", info);
159                         origin.send(st.error_reply(stanza, "modify", "item-not-found"));
160                 elseif not transfers[sha].initiator then
161                         module:log("debug", "The sender was not connected to the proxy; activation failed (%s)", info);
162                         origin.send(st.error_reply(stanza, "cancel", "not-allowed", "The sender (you) is not connected to the proxy"));
163                 --elseif not transfers[sha].target then -- can't happen, as target is set when a transfer object is created
164                 --      module:log("debug", "The recipient was not connected to the proxy; activation failed (%s)", info);
165                 --      origin.send(st.error_reply(stanza, "cancel", "not-allowed", "The recipient is not connected to the proxy"));
166                 else -- if transfers[sha].initiator ~= nil and transfers[sha].target ~= nil then
167                         module:log("debug", "Transfer activated (%s)", info);
168                         transfers[sha].activated = true;
169                         transfers[sha].target:resume();
170                         transfers[sha].initiator:resume();
171                         origin.send(st.reply(stanza));
172                 end
173         elseif to and sid then
174                 module:log("debug", "Malformed activation jid; activation failed (%s)", info);
175                 origin.send(st.error_reply(stanza, "modify", "jid-malformed"));
176         else
177                 module:log("debug", "Bad request; activation failed (%s)", info);
178                 origin.send(st.error_reply(stanza, "modify", "bad-request"));
179         end
180         return true;
181 end);
182
183 if not connlisteners.register(module.host .. ':proxy65', connlistener) then
184         module:log("error", "mod_proxy65: Could not establish a connection listener. Check your configuration please.");
185         module:log("error", "Possibly two proxy65 components are configured to share the same port.");
186 end
187
188 connlisteners.start(module.host .. ':proxy65');