2 local st = require "util.stanza";
3 local send = require "core.sessionmanager".send_to_session;
4 local sm_bind_resource = require "core.sessionmanager".bind_resource;
6 local base64 = require "base64"
8 local usermanager_validate_credentials = require "core.usermanager".validate_credentials;
9 local t_concat, t_insert = table.concat, table.insert;
10 local tostring = tostring;
11 local jid_split = require "util.jid".split
13 local log = require "util.logger".init("mod_saslauth");
15 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
16 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
17 local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas';
19 local new_sasl = require "util.sasl".new;
21 local function build_reply(status, ret, err_msg)
22 local reply = st.stanza(status, {xmlns = xmlns_sasl});
23 if status == "challenge" then
24 reply:text(base64.encode(ret or ""));
25 elseif status == "failure" then
27 if err_msg then reply:tag("text"):text(err_msg); end
28 elseif status == "success" then
29 reply:text(base64.encode(ret or ""));
31 error("Unknown sasl status: "..status);
36 local function handle_status(session, status)
37 if status == "failure" then
38 session.sasl_handler = nil;
39 elseif status == "success" then
40 if not session.sasl_handler.username then error("SASL succeeded but we didn't get a username!"); end -- TODO move this to sessionmanager
41 sessionmanager.make_authenticated(session, session.sasl_handler.username);
42 session.sasl_handler = nil;
43 session:reset_stream();
47 local function password_callback(node, host, mechanism)
48 local password = (datamanager.load(node, host, "accounts") or {}).password; -- FIXME handle hashed passwords
49 local func = function(x) return x; end;
51 if mechanism == "PLAIN" then
52 return func, password;
53 elseif mechanism == "DIGEST-MD5" then
54 return func, require "md5".sum(node..":"..host..":"..password);
60 function sasl_handler(session, stanza)
61 if stanza.name == "auth" then
62 -- FIXME ignoring duplicates because ejabberd does
63 session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, password_callback);
64 elseif not session.sasl_handler then
65 return; -- FIXME ignoring out of order stanzas because ejabberd does
67 local text = stanza[1];
69 text = base64.decode(text);
71 session.sasl_handler = nil;
72 session.send(build_reply("failure", "incorrect-encoding"));
76 local status, ret, err_msg = session.sasl_handler:feed(text);
77 handle_status(session, status);
78 local s = build_reply(status, ret, err_msg);
79 log("debug", "sasl reply: "..tostring(s));
83 add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
84 add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
85 add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
87 add_event_hook("stream-features",
88 function (session, features)
89 if not session.username then
90 t_insert(features, "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>");
91 -- 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.
92 t_insert(features, "<mechanism>PLAIN</mechanism>");
93 t_insert(features, "<mechanism>DIGEST-MD5</mechanism>");
94 t_insert(features, "</mechanisms>");
96 t_insert(features, "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><required/></bind>");
97 t_insert(features, "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>");
99 --send [[<register xmlns="http://jabber.org/features/iq-register"/> ]]
102 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
103 function (session, stanza)
104 log("debug", "Client tried to bind to a resource");
106 if stanza.attr.type == "set" then
107 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 = sm_bind_resource(session, resource);
118 local reply = st.reply(stanza);
119 reply.attr.type = "error";
120 if err == "conflict" then
121 reply:tag("error", { type = "modify" })
122 :tag("conflict", { xmlns = xmlns_stanzas });
123 elseif err == "constraint" then
124 reply:tag("error", { type = "cancel" })
125 :tag("resource-constraint", { xmlns = xmlns_stanzas });
126 elseif err == "auth" then
127 reply:tag("error", { type = "cancel" })
128 :tag("not-allowed", { xmlns = xmlns_stanzas });
130 send(session, reply);
132 local reply = st.reply(stanza);
133 reply:tag("bind", { xmlns = xmlns_bind})
134 :tag("jid"):text(session.full_jid);
135 send(session, reply);
139 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
140 function (session, stanza)
141 log("debug", "Client tried to bind to a resource");
142 send(session, st.reply(stanza));