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 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
83 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
84 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
86 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
87 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
88 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
89 module:add_event_hook("stream-features",
90 function (session, features)
91 if not session.username then
92 features:tag("mechanisms", mechanisms_attr);
93 -- 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.
94 features:tag("mechanism"):text("PLAIN"):up();
95 features:tag("mechanism"):text("DIGEST-MD5"):up();
98 features:tag("bind", bind_attr):tag("required"):up():up();
99 features:tag("session", xmpp_session_attr):up();
103 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
104 function (session, stanza)
105 log("debug", "Client tried to bind to a resource");
107 if stanza.attr.type == "set" then
108 local bind = stanza.tags[1];
109 if bind and bind.attr.xmlns == xmlns_bind then
110 resource = bind:child_with_name("resource");
112 resource = resource[1];
116 local success, err_type, err, err_msg = sm_bind_resource(session, resource);
118 session.send(st.error_reply(stanza, err_type, err, err_msg));
120 session.send(st.reply(stanza)
121 :tag("bind", { xmlns = xmlns_bind})
122 :tag("jid"):text(session.full_jid));
126 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
127 function (session, stanza)
128 log("debug", "Client tried to bind to a resource");
129 session.send(st.reply(stanza));