Comparison

plugins/mod_component.lua @ 5776:bd0ff8ae98a8

Remove all trailing whitespace
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 09 Aug 2013 17:48:21 +0200
parent 5728:2ecf400b194a
child 5954:6dc73be95213
comparison
equal deleted inserted replaced
5775:a6c2b8933507 5776:bd0ff8ae98a8
1 -- Prosody IM 1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild 2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain 3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 module:set_global(); 9 module:set_global();
29 29
30 function module.add_host(module) 30 function module.add_host(module)
31 if module:get_host_type() ~= "component" then 31 if module:get_host_type() ~= "component" then
32 error("Don't load mod_component manually, it should be for a component, please see http://prosody.im/doc/components", 0); 32 error("Don't load mod_component manually, it should be for a component, please see http://prosody.im/doc/components", 0);
33 end 33 end
34 34
35 local env = module.environment; 35 local env = module.environment;
36 env.connected = false; 36 env.connected = false;
37 37
38 local send; 38 local send;
39 39
40 local function on_destroy(session, err) 40 local function on_destroy(session, err)
41 env.connected = false; 41 env.connected = false;
42 send = nil; 42 send = nil;
43 session.on_destroy = nil; 43 session.on_destroy = nil;
44 end 44 end
45 45
46 -- Handle authentication attempts by component 46 -- Handle authentication attempts by component
47 local function handle_component_auth(event) 47 local function handle_component_auth(event)
48 local session, stanza = event.origin, event.stanza; 48 local session, stanza = event.origin, event.stanza;
49 49
50 if session.type ~= "component_unauthed" then return; end 50 if session.type ~= "component_unauthed" then return; end
51 51
52 if (not session.host) or #stanza.tags > 0 then 52 if (not session.host) or #stanza.tags > 0 then
53 (session.log or log)("warn", "Invalid component handshake for host: %s", session.host); 53 (session.log or log)("warn", "Invalid component handshake for host: %s", session.host);
54 session:close("not-authorized"); 54 session:close("not-authorized");
55 return true; 55 return true;
56 end 56 end
57 57
58 local secret = module:get_option("component_secret"); 58 local secret = module:get_option("component_secret");
59 if not secret then 59 if not secret then
60 (session.log or log)("warn", "Component attempted to identify as %s, but component_secret is not set", session.host); 60 (session.log or log)("warn", "Component attempted to identify as %s, but component_secret is not set", session.host);
61 session:close("not-authorized"); 61 session:close("not-authorized");
62 return true; 62 return true;
63 end 63 end
64 64
65 local supplied_token = t_concat(stanza); 65 local supplied_token = t_concat(stanza);
66 local calculated_token = sha1(session.streamid..secret, true); 66 local calculated_token = sha1(session.streamid..secret, true);
67 if supplied_token:lower() ~= calculated_token:lower() then 67 if supplied_token:lower() ~= calculated_token:lower() then
68 module:log("info", "Component authentication failed for %s", session.host); 68 module:log("info", "Component authentication failed for %s", session.host);
69 session:close{ condition = "not-authorized", text = "Given token does not match calculated token" }; 69 session:close{ condition = "not-authorized", text = "Given token does not match calculated token" };
70 return true; 70 return true;
71 end 71 end
72 72
73 if env.connected then 73 if env.connected then
74 module:log("error", "Second component attempted to connect, denying connection"); 74 module:log("error", "Second component attempted to connect, denying connection");
75 session:close{ condition = "conflict", text = "Component already connected" }; 75 session:close{ condition = "conflict", text = "Component already connected" };
76 return true; 76 return true;
77 end 77 end
78 78
79 env.connected = true; 79 env.connected = true;
80 send = session.send; 80 send = session.send;
81 session.on_destroy = on_destroy; 81 session.on_destroy = on_destroy;
82 session.component_validate_from = module:get_option_boolean("validate_from_addresses", true); 82 session.component_validate_from = module:get_option_boolean("validate_from_addresses", true);
83 session.type = "component"; 83 session.type = "component";
84 module:log("info", "External component successfully authenticated"); 84 module:log("info", "External component successfully authenticated");
85 session.send(st.stanza("handshake")); 85 session.send(st.stanza("handshake"));
86 86
87 return true; 87 return true;
88 end 88 end
89 module:hook("stanza/jabber:component:accept:handshake", handle_component_auth); 89 module:hook("stanza/jabber:component:accept:handshake", handle_component_auth);
90 90
91 -- Handle stanzas addressed to this component 91 -- Handle stanzas addressed to this component
112 event.origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable")); 112 event.origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
113 end 113 end
114 end 114 end
115 return true; 115 return true;
116 end 116 end
117 117
118 module:hook("iq/bare", handle_stanza, -1); 118 module:hook("iq/bare", handle_stanza, -1);
119 module:hook("message/bare", handle_stanza, -1); 119 module:hook("message/bare", handle_stanza, -1);
120 module:hook("presence/bare", handle_stanza, -1); 120 module:hook("presence/bare", handle_stanza, -1);
121 module:hook("iq/full", handle_stanza, -1); 121 module:hook("iq/full", handle_stanza, -1);
122 module:hook("message/full", handle_stanza, -1); 122 module:hook("message/full", handle_stanza, -1);
267 267
268 -- Logging functions -- 268 -- Logging functions --
269 local conn_name = "jcp"..tostring(session):match("[a-f0-9]+$"); 269 local conn_name = "jcp"..tostring(session):match("[a-f0-9]+$");
270 session.log = logger.init(conn_name); 270 session.log = logger.init(conn_name);
271 session.close = session_close; 271 session.close = session_close;
272 272
273 session.log("info", "Incoming Jabber component connection"); 273 session.log("info", "Incoming Jabber component connection");
274 274
275 local stream = new_xmpp_stream(session, stream_callbacks); 275 local stream = new_xmpp_stream(session, stream_callbacks);
276 session.stream = stream; 276 session.stream = stream;
277 277
278 session.notopen = true; 278 session.notopen = true;
279 279
280 function session.reset_stream() 280 function session.reset_stream()
281 session.notopen = true; 281 session.notopen = true;
282 session.stream:reset(); 282 session.stream:reset();
283 end 283 end
284 284
286 local ok, err = stream:feed(data); 286 local ok, err = stream:feed(data);
287 if ok then return; end 287 if ok then return; end
288 module:log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_")); 288 module:log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
289 session:close("not-well-formed"); 289 session:close("not-well-formed");
290 end 290 end
291 291
292 session.dispatch_stanza = stream_callbacks.handlestanza; 292 session.dispatch_stanza = stream_callbacks.handlestanza;
293 293
294 sessions[conn] = session; 294 sessions[conn] = session;
295 end 295 end
296 function listener.onincoming(conn, data) 296 function listener.onincoming(conn, data)