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;
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(ret or "");
24 elseif status == "failure" then
26 if err_msg then reply:tag("text"); end
27 elseif status == "success" then
28 reply:text(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 "hashes".md5(node..":"..host..":"..password);
59 function do_sasl(session, stanza)
60 local text = stanza[1];
62 text = base64.decode(text);
64 session.sasl_handler = nil;
65 session.send(build_reply("failure", "incorrect-encoding"));
69 local status, ret, err_msg = session.sasl_handler:feed(text);
70 handle_status(session, status);
71 local s = build_reply(status, ret, err_msg);
72 log("debug", "sasl reply: "..tostring(s));
76 add_handler("c2s_unauthed", "auth", xmlns_sasl,
77 function (session, stanza)
78 if not session.sasl_handler then
79 session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, password_callback);
80 do_sasl(session, stanza);
82 error("Client tried to negotiate SASL again", 0);
86 add_handler("c2s_unauthed", "abort", xmlns_sasl,
87 function(session, stanza)
88 if not session.sasl_handler then error("Attempt to abort when sasl has not started"); end
89 do_sasl(session, stanza);
92 add_handler("c2s_unauthed", "response", xmlns_sasl,
93 function(session, stanza)
94 if not session.sasl_handler then error("Attempt to respond when sasl has not started"); end
95 do_sasl(session, stanza);
98 add_event_hook("stream-features",
99 function (session, features)
100 if not session.username then
101 t_insert(features, "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>");
102 -- 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.
103 t_insert(features, "<mechanism>PLAIN</mechanism>");
104 -- t_insert(features, "<mechanism>DIGEST-MD5</mechanism>");
105 t_insert(features, "</mechanisms>");
107 t_insert(features, "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><required/></bind>");
108 t_insert(features, "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>");
110 --send [[<register xmlns="http://jabber.org/features/iq-register"/> ]]
113 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
114 function (session, stanza)
115 log("debug", "Client tried to bind to a resource");
117 if stanza.attr.type == "set" then
118 local bind = stanza.tags[1];
120 if bind and bind.attr.xmlns == xmlns_bind then
121 resource = bind:child_with_name("resource");
123 resource = resource[1];
127 local success, err = sm_bind_resource(session, resource);
129 local reply = st.reply(stanza);
130 reply.attr.type = "error";
131 if err == "conflict" then
132 reply:tag("error", { type = "modify" })
133 :tag("conflict", { xmlns = xmlns_stanzas });
134 elseif err == "constraint" then
135 reply:tag("error", { type = "cancel" })
136 :tag("resource-constraint", { xmlns = xmlns_stanzas });
137 elseif err == "auth" then
138 reply:tag("error", { type = "cancel" })
139 :tag("not-allowed", { xmlns = xmlns_stanzas });
141 send(session, reply);
143 local reply = st.reply(stanza);
144 reply:tag("bind", { xmlns = xmlns_bind})
145 :tag("jid"):text(session.full_jid);
146 send(session, reply);
150 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
151 function (session, stanza)
152 log("debug", "Client tried to bind to a resource");
153 send(session, st.reply(stanza));