2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local st = require "util.stanza";
12 local sm_bind_resource = require "core.sessionmanager".bind_resource;
13 local sm_make_authenticated = require "core.sessionmanager".make_authenticated;
14 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
15 local base64 = require "util.encodings".base64;
17 local cert_verify_identity = require "util.certverification".verify_identity;
19 local nodeprep = require "util.encodings".stringprep.nodeprep;
20 local usermanager_get_sasl_handler = require "core.usermanager".get_sasl_handler;
21 local t_concat, t_insert = table.concat, table.insert;
22 local tostring = tostring;
24 local secure_auth_only = module:get_option("c2s_require_encryption") or module:get_option("require_encryption");
25 local anonymous_login = module:get_option("anonymous_login");
26 local allow_unencrypted_plain_auth = module:get_option("allow_unencrypted_plain_auth")
28 local log = module._log;
30 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
31 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
32 local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas';
34 local new_sasl = require "util.sasl".new;
36 local anonymous_authentication_profile = {
37 anonymous = function(username, realm)
38 return true; -- for normal usage you should always return true here
42 local function build_reply(status, ret, err_msg)
43 local reply = st.stanza(status, {xmlns = xmlns_sasl});
44 if status == "challenge" then
45 --log("debug", "CHALLENGE: %s", ret or "");
46 reply:text(base64.encode(ret or ""));
47 elseif status == "failure" then
49 if err_msg then reply:tag("text"):text(err_msg); end
50 elseif status == "success" then
51 --log("debug", "SUCCESS: %s", ret or "");
52 reply:text(base64.encode(ret or ""));
54 module:log("error", "Unknown sasl status: %s", status);
59 local function handle_status(session, status, ret, err_msg)
60 if status == "failure" then
61 session.sasl_handler = session.sasl_handler:clean_clone();
62 elseif status == "success" then
63 local username = nodeprep(session.sasl_handler.username);
65 local ok, err = sm_make_authenticated(session, session.sasl_handler.username);
67 session.sasl_handler = nil;
68 session:reset_stream();
70 module:log("warn", "SASL succeeded but username was invalid");
71 session.sasl_handler = session.sasl_handler:clean_clone();
72 return "failure", "not-authorized", "User authenticated successfully, but username was invalid";
75 return status, ret, err_msg;
78 local function sasl_process_cdata(session, stanza)
79 local text = stanza[1];
81 text = base64.decode(text);
82 --log("debug", "AUTH: %s", text:gsub("[%z\001-\008\011\012\014-\031]", " "));
84 session.sasl_handler = nil;
85 session.send(build_reply("failure", "incorrect-encoding"));
89 local status, ret, err_msg = session.sasl_handler:process(text);
90 status, ret, err_msg = handle_status(session, status, ret, err_msg);
91 local s = build_reply(status, ret, err_msg);
92 log("debug", "sasl reply: %s", tostring(s));
97 module:hook_stanza(xmlns_sasl, "success", function (session, stanza)
98 if session.type ~= "s2sout_unauthed" or session.external_auth ~= "attempting" then return; end
99 module:log("debug", "SASL EXTERNAL with %s succeeded", session.to_host);
100 session.external_auth = "succeeded"
101 session:reset_stream();
103 local default_stream_attr = {xmlns = "jabber:server", ["xmlns:stream"] = "http://etherx.jabber.org/streams",
104 ["xmlns:db"] = 'jabber:server:dialback', version = "1.0", to = session.to_host, from = session.from_host};
105 session.sends2s("<?xml version='1.0'?>");
106 session.sends2s(st.stanza("stream:stream", default_stream_attr):top_tag());
108 s2s_make_authenticated(session, session.to_host);
112 module:hook_stanza(xmlns_sasl, "failure", function (session, stanza)
113 if session.type ~= "s2sout_unauthed" or session.external_auth ~= "attempting" then return; end
115 module:log("info", "SASL EXTERNAL with %s failed", session.to_host)
116 -- TODO: Log the failure reason
117 session.external_auth = "failed"
120 module:hook_stanza(xmlns_sasl, "failure", function (session, stanza)
121 -- TODO: Dialback wasn't loaded. Do something useful.
124 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza)
125 if session.type ~= "s2sout_unauthed" or not session.secure then return; end
127 local mechanisms = stanza:get_child("mechanisms", xmlns_sasl)
129 for mech in mechanisms:childtags() do
130 if mech[1] == "EXTERNAL" then
131 module:log("debug", "Initiating SASL EXTERNAL with %s", session.to_host);
132 local reply = st.stanza("auth", {xmlns = xmlns_sasl, mechanism = "EXTERNAL"});
133 reply:text(base64.encode(session.from_host))
134 session.sends2s(reply)
135 session.external_auth = "attempting"
142 local function s2s_external_auth(session, stanza)
143 local mechanism = stanza.attr.mechanism;
145 if not session.secure then
146 if mechanism == "EXTERNAL" then
147 session.sends2s(build_reply("failure", "encryption-required"))
149 session.sends2s(build_reply("failure", "invalid-mechanism"))
154 if mechanism ~= "EXTERNAL" or session.cert_chain_status ~= "valid" then
155 session.sends2s(build_reply("failure", "invalid-mechanism"))
159 local text = stanza[1]
161 session.sends2s(build_reply("failure", "malformed-request"))
165 -- Either the value is "=" and we've already verified the external
166 -- cert identity, or the value is a string and either matches the
169 text = base64.decode(text)
171 session.sends2s(build_reply("failure", "incorrect-encoding"))
175 if session.cert_identity_status == "valid" then
176 if text ~= "" and text ~= session.from_host then
177 session.sends2s(build_reply("failure", "invalid-authzid"))
182 session.sends2s(build_reply("failure", "invalid-authzid"))
186 local cert = session.conn:socket():getpeercertificate()
187 if (cert_verify_identity(text, "xmpp-server", cert)) then
188 session.cert_identity_status = "valid"
190 session.cert_identity_status = "invalid"
191 session.sends2s(build_reply("failure", "invalid-authzid"))
196 session.external_auth = "succeeded"
198 if not session.from_host then
199 session.from_host = text;
201 session.sends2s(build_reply("success"))
202 module:log("info", "Accepting SASL EXTERNAL identity from %s", text or session.from_host);
203 s2s_make_authenticated(session, text or session.from_host)
204 session:reset_stream();
208 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-sasl:auth", function(event)
209 local session, stanza = event.origin, event.stanza;
210 if session.type == "s2sin_unauthed" then
211 return s2s_external_auth(session, stanza)
214 if session.type ~= "c2s_unauthed" then return; end
216 if session.sasl_handler and session.sasl_handler.selected then
217 session.sasl_handler = nil; -- allow starting a new SASL negotiation before completing an old one
219 if not session.sasl_handler then
220 if anonymous_login then
221 session.sasl_handler = new_sasl(module.host, anonymous_authentication_profile);
223 session.sasl_handler = usermanager_get_sasl_handler(module.host);
226 local mechanism = stanza.attr.mechanism;
227 if anonymous_login then
228 if mechanism ~= "ANONYMOUS" then
229 session.send(build_reply("failure", "invalid-mechanism"));
232 elseif mechanism == "ANONYMOUS" then
233 session.send(build_reply("failure", "mechanism-too-weak"));
236 if not session.secure and (secure_auth_only or (mechanism == "PLAIN" and not allow_unencrypted_plain_auth)) then
237 session.send(build_reply("failure", "encryption-required"));
240 local valid_mechanism = session.sasl_handler:select(mechanism);
241 if not valid_mechanism then
242 session.send(build_reply("failure", "invalid-mechanism"));
245 return sasl_process_cdata(session, stanza);
247 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-sasl:response", function(event)
248 local session = event.origin;
249 if not(session.sasl_handler and session.sasl_handler.selected) then
250 session.send(build_reply("failure", "not-authorized", "Out of order SASL element"));
253 return sasl_process_cdata(session, event.stanza);
255 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-sasl:abort", function(event)
256 local session = event.origin;
257 session.sasl_handler = nil;
258 session.send(build_reply("failure", "aborted"));
262 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
263 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
264 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
265 module:hook("stream-features", function(event)
266 local origin, features = event.origin, event.features;
267 if not origin.username then
268 if secure_auth_only and not origin.secure then
271 if anonymous_login then
272 origin.sasl_handler = new_sasl(module.host, anonymous_authentication_profile);
274 origin.sasl_handler = usermanager_get_sasl_handler(module.host);
276 features:tag("mechanisms", mechanisms_attr);
277 for mechanism in pairs(origin.sasl_handler:mechanisms()) do
278 if mechanism ~= "PLAIN" or origin.secure or allow_unencrypted_plain_auth then
279 features:tag("mechanism"):text(mechanism):up();
284 features:tag("bind", bind_attr):tag("required"):up():up();
285 features:tag("session", xmpp_session_attr):tag("optional"):up():up();
289 module:hook("s2s-stream-features", function(event)
290 local origin, features = event.origin, event.features;
291 if origin.secure and origin.type == "s2sin_unauthed" then
292 -- Offer EXTERNAL if chain is valid and either we didn't validate
293 -- the identity or it passed.
294 if origin.cert_chain_status == "valid" and origin.cert_identity_status ~= "invalid" then --TODO: Configurable
295 module:log("debug", "Offering SASL EXTERNAL")
296 features:tag("mechanisms", { xmlns = xmlns_sasl })
297 :tag("mechanism"):text("EXTERNAL")
303 module:hook("iq/self/urn:ietf:params:xml:ns:xmpp-bind:bind", function(event)
304 local origin, stanza = event.origin, event.stanza;
306 if stanza.attr.type == "set" then
307 local bind = stanza.tags[1];
308 resource = bind:child_with_name("resource");
309 resource = resource and #resource.tags == 0 and resource[1] or nil;
311 local success, err_type, err, err_msg = sm_bind_resource(origin, resource);
313 origin.send(st.reply(stanza)
314 :tag("bind", { xmlns = xmlns_bind })
315 :tag("jid"):text(origin.full_jid));
316 origin.log("debug", "Resource bound: %s", origin.full_jid);
318 origin.send(st.error_reply(stanza, err_type, err, err_msg));
319 origin.log("debug", "Resource bind failed: %s", err_msg or err);
324 module:hook("iq/self/urn:ietf:params:xml:ns:xmpp-session:session", function(event)
325 event.origin.send(st.reply(event.stanza));