comparison mod_saslauth_muc/mod_saslauth_muc.lua @ 1426:249c5447fed1

mod_saslauth_muc: Update to use new MUC API.
author Waqas Hussain <waqas20@gmail.com>
date Fri, 30 May 2014 19:07:18 -0400
parents 7dbde05b48a9
children 926db29176f5
comparison
equal deleted inserted replaced
1425:9c894b56b4e4 1426:249c5447fed1
22 local hosts = hosts; 22 local hosts = hosts;
23 local module = module; 23 local module = module;
24 local pairs, next = pairs, next; 24 local pairs, next = pairs, next;
25 local os_time = os.time; 25 local os_time = os.time;
26 26
27 local muc_password = module:require("muc/password");
28
27 -- SASL sessions management 29 -- SASL sessions management
28 local _rooms = {}; -- SASL data 30 local _rooms = {}; -- SASL data
29 local function get_handler_for(room, jid) return _rooms[room] and _rooms[room][jid]; end 31 local function get_handler_for(room, jid) return _rooms[room] and _rooms[room][jid]; end
30 local function remove_handler_for(room, jid) if _rooms[room] then _rooms[room][jid] = nil; end end 32 local function remove_handler_for(room, jid) if _rooms[room] then _rooms[room][jid] = nil; end end
31 local function create_handler_for(room_jid, jid) 33 local function create_handler_for(room_jid, jid)
32 _rooms[room_jid] = _rooms[room_jid] or {}; 34 _rooms[room_jid] = _rooms[room_jid] or {};
33 _rooms[room_jid][jid] = new_sasl(module.host, { plain = function(sasl, username, realm) 35 _rooms[room_jid][jid] = new_sasl(module.host, { plain = function(sasl, username, realm)
34 local muc = hosts[module.host].modules.muc; 36 local muc = hosts[module.host].modules.muc;
35 local room = muc and muc.rooms[room_jid]; 37 local room = muc and muc.get_room_by_jid(room_jid);
36 local password = room and room:get_password(); 38 local password = room and muc_password.get(room);
37 local ret = password and true or nil; 39 local ret = password and true or nil;
38 return password or "", ret; 40 return password or "", ret;
39 end }); 41 end });
40 _rooms[room_jid][jid].timeout = os_time() + timeout; 42 _rooms[room_jid][jid].timeout = os_time() + timeout;
41 return _rooms[room_jid][jid]; 43 return _rooms[room_jid][jid];
54 function module.unload() 56 function module.unload()
55 timeout = nil; -- stop timer on unload 57 timeout = nil; -- stop timer on unload
56 end 58 end
57 59
58 -- Stanza handlers 60 -- Stanza handlers
59 module:hook("presence/full", function(event) 61 -- Don't allow anyone to join room unless they provide the password
60 local origin, stanza = event.origin, event.stanza; 62 module:hook("muc-occupant-pre-join", function(event)
61 63 local room, stanza = event.room, event.stanza;
62 if not stanza.attr.type then -- available presence 64 local room_password = muc_password.get(room);
63 local room_jid = jid_bare(stanza.attr.to); 65 if room_password then -- room has a password
64 local room = hosts[module.host].modules.muc.rooms[room_jid]; 66 local x = stanza:get_child("x", "http://jabber.org/protocol/muc");
65 67 local password = x and x:get_child_text("password", "http://jabber.org/protocol/muc");
66 if room and not room:get_role(stanza.attr.from) then -- this is a room join 68 if not password then -- no password sent
67 if room:get_password() then -- room has a password 69 local sasl_handler = get_handler_for(jid_bare(stanza.attr.to), stanza.attr.from);
68 local x = stanza:get_child("x", "http://jabber.org/protocol/muc"); 70 if x and sasl_handler and sasl_handler.authorized then -- if already passed SASL
69 local password = x and x:get_child("password"); 71 x:reset():tag("password", { xmlns = "http://jabber.org/protocol/muc" }):text(room_password);
70 if not password then -- no password sent 72 else
71 local sasl_handler = get_handler_for(jid_bare(stanza.attr.to), stanza.attr.from); 73 event.origin.send(st.error_reply(stanza, "auth", "not-authorized")
72 if x and sasl_handler and sasl_handler.authorized then -- if already passed SASL 74 :tag("sasl-required", { xmlns = "urn:xmpp:errors" }));
73 x:reset():tag("password", { xmlns = "http://jabber.org/protocol/muc" }):text(room:get_password()); 75 return true;
74 else
75 origin.send(st.error_reply(stanza, "auth", "not-authorized")
76 :tag("sasl-required", { xmlns = "urn:xmpp:errors" }));
77 return true;
78 end
79 end
80 end 76 end
81 end 77 end
82 end 78 end
83 end, 10); 79 end, -18);
84 80
85 module:hook("iq-get/bare/urn:ietf:params:xml:ns:xmpp-sasl:mechanisms", function(event) 81 module:hook("iq-get/bare/urn:ietf:params:xml:ns:xmpp-sasl:mechanisms", function(event)
86 local origin, stanza = event.origin, event.stanza; 82 local origin, stanza = event.origin, event.stanza;
87 83
88 local reply = st.reply(stanza):tag("mechanisms", { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' }); 84 local reply = st.reply(stanza):tag("mechanisms", { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' });