2 local st = require "util.stanza";
3 local sm_bind_resource = require "core.sessionmanager".bind_resource;
5 local base64 = require "base64"
7 local usermanager_validate_credentials = require "core.usermanager".validate_credentials;
8 local t_concat, t_insert = table.concat, table.insert;
9 local tostring = tostring;
10 local jid_split = require "util.jid".split
12 local log = require "util.logger".init("mod_saslauth");
14 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
15 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
16 local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas';
18 local new_sasl = require "util.sasl".new;
20 local function build_reply(status, ret, err_msg)
21 local reply = st.stanza(status, {xmlns = xmlns_sasl});
22 if status == "challenge" then
23 reply:text(base64.encode(ret or ""));
24 elseif status == "failure" then
26 if err_msg then reply:tag("text"):text(err_msg); end
27 elseif status == "success" then
28 reply:text(base64.encode(ret or ""));
30 error("Unknown sasl status: "..status);
35 local function handle_status(session, status)
36 if status == "failure" then
37 session.sasl_handler = nil;
38 elseif status == "success" then
39 if not session.sasl_handler.username then error("SASL succeeded but we didn't get a username!"); end -- TODO move this to sessionmanager
40 sessionmanager.make_authenticated(session, session.sasl_handler.username);
41 session.sasl_handler = nil;
42 session:reset_stream();
46 local function password_callback(node, host, mechanism)
47 local password = (datamanager.load(node, host, "accounts") or {}).password; -- FIXME handle hashed passwords
48 local func = function(x) return x; end;
50 if mechanism == "PLAIN" then
51 return func, password;
52 elseif mechanism == "DIGEST-MD5" then
53 return func, require "md5".sum(node..":"..host..":"..password);
59 function sasl_handler(session, stanza)
60 if stanza.name == "auth" then
61 -- FIXME ignoring duplicates because ejabberd does
62 session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, password_callback);
63 elseif not session.sasl_handler then
64 return; -- FIXME ignoring out of order stanzas because ejabberd does
66 local text = stanza[1];
68 text = base64.decode(text);
70 session.sasl_handler = nil;
71 session.send(build_reply("failure", "incorrect-encoding"));
75 local status, ret, err_msg = session.sasl_handler:feed(text);
76 handle_status(session, status);
77 local s = build_reply(status, ret, err_msg);
78 log("debug", "sasl reply: "..tostring(s));
82 add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
83 add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
84 add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
86 add_event_hook("stream-features",
87 function (session, features)
88 if not session.username then
89 t_insert(features, "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>");
90 -- TODO: Provide PLAIN only if TLS is active, this is a SHOULD from the introduction of RFC 4616. This behavior could be overridden via configuration but will issuing a warning or so.
91 t_insert(features, "<mechanism>PLAIN</mechanism>");
92 t_insert(features, "<mechanism>DIGEST-MD5</mechanism>");
93 t_insert(features, "</mechanisms>");
95 t_insert(features, "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><required/></bind>");
96 t_insert(features, "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>");
98 --send [[<register xmlns="http://jabber.org/features/iq-register"/> ]]
101 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
102 function (session, stanza)
103 log("debug", "Client tried to bind to a resource");
105 if stanza.attr.type == "set" then
106 local bind = stanza.tags[1];
107 if bind and bind.attr.xmlns == xmlns_bind then
108 resource = bind:child_with_name("resource");
110 resource = resource[1];
114 local success, err_type, err, err_msg = sm_bind_resource(session, resource);
116 session.send(st.error_reply(stanza, err_type, err, err_msg));
118 session.send(st.reply(stanza)
119 :tag("bind", { xmlns = xmlns_bind})
120 :tag("jid"):text(session.full_jid));
124 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
125 function (session, stanza)
126 log("debug", "Client tried to bind to a resource");
127 session.send(st.reply(stanza));