Software /
code /
prosody
Comparison
plugins/mod_component.lua @ 902:00daf63c129e
Add initial mod_component for XEP-0114 support. Albert, where are you?
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 21:47:09 +0000 |
child | 909:505a2cbb823d |
comparison
equal
deleted
inserted
replaced
901:0e8934d3c4cb | 902:00daf63c129e |
---|---|
1 -- Prosody IM v0.4 | |
2 -- Copyright (C) 2008-2009 Matthew Wild | |
3 -- Copyright (C) 2008-2009 Waqas Hussain | |
4 -- | |
5 -- This project is MIT/X11 licensed. Please see the | |
6 -- COPYING file in the source package for more information. | |
7 -- | |
8 | |
9 if module:get_host_type() ~= "component" then | |
10 error("Don't load mod_component manually, it should be for a component, please see http://prosody.im/doc/components", 0); | |
11 end | |
12 | |
13 local t_concat = table.concat; | |
14 | |
15 local connlisteners = require "net.connlisteners"; | |
16 local cm_register_component = require "core.componentmanager".register_component; | |
17 local uuid_gen = require "util.uuid".generate; | |
18 local sha1 = require "util.hashes".sha1; | |
19 local st = stanza; | |
20 local init_xmlhandlers = require "core.xmlhandlers"; | |
21 | |
22 local sessions = {}; | |
23 | |
24 local log = module._log; | |
25 | |
26 local component_listener = { default_port = 5347; default_mode = "*a"; }; | |
27 | |
28 local xmlns_component = 'jabber:component:accept'; | |
29 | |
30 --- Callbacks/data for xmlhandlers to handle streams for us --- | |
31 | |
32 local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", default_ns = xmlns_component }; | |
33 | |
34 function stream_callbacks.error(session, error, data, data2) | |
35 log("warn", "Error processing component stream: "..tostring(error)); | |
36 if error == "no-stream" then | |
37 session:close("invalid-namespace"); | |
38 elseif error == "xml-parse-error" and data == "unexpected-element-close" then | |
39 session.log("debug", "Unexpected close of '%s' tag", data2); | |
40 session:close("xml-not-well-formed"); | |
41 else | |
42 session.log("debug", "External component %s XML parse error: %s", tostring(session.host), tostring(error)); | |
43 print(data, debug.traceback()) | |
44 session:close("xml-not-well-formed"); | |
45 end | |
46 end | |
47 | |
48 function stream_callbacks.streamopened(session, attr) | |
49 if config.get(attr.to, "core", "component_module") ~= "component" then | |
50 -- Trying to act as a component domain which | |
51 -- hasn't been configured | |
52 session:close{ condition = "host-unknown", text = tostring(attr.to).." does not match any configured external components" }; | |
53 return; | |
54 end | |
55 | |
56 -- Store the original host (this is used for config, etc.) | |
57 session.user = attr.to; | |
58 -- Set the host for future reference | |
59 session.host = config.get(attr.to, "core", "component_address") or attr.to; | |
60 -- Note that we don't create the internal component | |
61 -- until after the external component auths successfully | |
62 | |
63 session.streamid = uuid_gen(); | |
64 session.notopen = nil; | |
65 | |
66 session.send(st.stanza("stream:stream", { xmlns=xmlns_component, | |
67 ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.host }):top_tag()); | |
68 | |
69 end | |
70 | |
71 function stream_callbacks.streamclosed(session) | |
72 end | |
73 | |
74 local core_process_stanza = core_process_stanza; | |
75 | |
76 function stream_callbacks.handlestanza(session, stanza) | |
77 -- Namespaces are icky. | |
78 log("warn", "Handing stanza with name %s", stanza.name); | |
79 if stanza.name ~= "handshake" then | |
80 return core_process_stanza(session, stanza); | |
81 else | |
82 handle_component_auth(session, stanza); | |
83 end | |
84 end | |
85 | |
86 --- Handle authentication attempts by components | |
87 function handle_component_auth(session, stanza) | |
88 if (not session.host) or #stanza.tags > 0 then | |
89 session:close("not-authorized"); | |
90 return; | |
91 end | |
92 | |
93 local secret = config.get(session.user, "core", "component_secret"); | |
94 if not secret then | |
95 log("warn", "Component attempted to identify as %s, but component_password is not set", session.user); | |
96 session:close("not-authorized"); | |
97 return; | |
98 end | |
99 | |
100 local supplied_token = t_concat(stanza); | |
101 local calculated_token = sha1(session.streamid..secret, true); | |
102 if supplied_token:lower() ~= calculated_token:lower() then | |
103 session:close{ condition = "not-authorized", text = "Given token does not match calculated token" }; | |
104 return; | |
105 end | |
106 | |
107 | |
108 -- Authenticated now | |
109 | |
110 -- If component not already created for this host, create one now | |
111 if not hosts[session.host].connected then | |
112 local send = session.send; | |
113 session.component_session = cm_register_component(session.host, function (_, data) return send(data); end); | |
114 hosts[session.host].connected = true; | |
115 else | |
116 log("error", "Multiple components bound to the same address, first one wins (TODO: Implement stanza distribution)"); | |
117 end | |
118 | |
119 -- Signal successful authentication | |
120 session.send(st.stanza("handshake")); | |
121 end | |
122 | |
123 module:add_handler("component", "handshake", xmlns_component, handle_component_auth); | |
124 | |
125 --[[ | |
126 -- Helper function to deal with errors processing component stanzas | |
127 local function handleerr(err) log("error", "Traceback[component]: %s: %s", tostring(err), debug.traceback()); end | |
128 function stream_callbacks.handlestanza(a, b) | |
129 xpcall(function () core_process_stanza(a, b) end, handleerr); | |
130 end | |
131 ]] | |
132 | |
133 --- Closing a component connection | |
134 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; | |
135 local function session_close(session, reason) | |
136 local log = session.log or log; | |
137 if session.conn then | |
138 if reason then | |
139 if type(reason) == "string" then -- assume stream error | |
140 log("info", "Disconnecting component, <stream:error> is: %s", reason); | |
141 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' })); | |
142 elseif type(reason) == "table" then | |
143 if reason.condition then | |
144 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up(); | |
145 if reason.text then | |
146 stanza:tag("text", stream_xmlns_attr):text(reason.text):up(); | |
147 end | |
148 if reason.extra then | |
149 stanza:add_child(reason.extra); | |
150 end | |
151 log("info", "Disconnecting component, <stream:error> is: %s", tostring(stanza)); | |
152 session.send(stanza); | |
153 elseif reason.name then -- a stanza | |
154 log("info", "Disconnecting component, <stream:error> is: %s", tostring(reason)); | |
155 session.send(reason); | |
156 end | |
157 end | |
158 end | |
159 session.send("</stream:stream>"); | |
160 session.conn.close(); | |
161 component_listener.disconnect(session.conn, "stream error"); | |
162 end | |
163 end | |
164 | |
165 --- Component connlistener | |
166 function component_listener.listener(conn, data) | |
167 local session = sessions[conn]; | |
168 if not session then | |
169 local _send = conn.write; | |
170 session = { type = "component", conn = conn, send = function (data) return _send(tostring(data)); end }; | |
171 sessions[conn] = session; | |
172 | |
173 -- Logging functions -- | |
174 | |
175 local conn_name = "xep114-"..tostring(conn):match("[a-f0-9]+$"); | |
176 session.log = logger.init(conn_name); | |
177 session.close = session_close; | |
178 | |
179 session.log("info", "Incoming XEP-0114 connection"); | |
180 | |
181 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|"); | |
182 session.parser = parser; | |
183 | |
184 session.notopen = true; | |
185 | |
186 function session.data(conn, data) | |
187 local ok, err = parser:parse(data); | |
188 if ok then return; end | |
189 session:close("xml-not-well-formed"); | |
190 end | |
191 | |
192 session.dispatch_stanza = stream_callbacks.handlestanza; | |
193 | |
194 end | |
195 if data then | |
196 session.data(conn, data); | |
197 end | |
198 end | |
199 | |
200 function component_listener.disconnect(conn, err) | |
201 local session = sessions[conn]; | |
202 if session then | |
203 (session.log or log)("info", "component disconnected: %s (%s)", tostring(session.host), tostring(err)); | |
204 if session.host then | |
205 if session.component then | |
206 deregister_component(session.host); | |
207 end | |
208 end | |
209 sessions[conn] = nil; | |
210 session = nil; | |
211 collectgarbage("collect"); | |
212 end | |
213 end | |
214 | |
215 connlisteners.register('component', component_listener); | |
216 | |
217 module:add_event_hook("server-starting", function () connlisteners.start("component"); end); |