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.x509".verify_identity;
19 local usermanager_get_sasl_handler = require "core.usermanager".get_sasl_handler;
20 local tostring = tostring;
22 local secure_auth_only = module:get_option("c2s_require_encryption") or module:get_option("require_encryption");
23 local allow_unencrypted_plain_auth = module:get_option("allow_unencrypted_plain_auth")
25 local log = module._log;
27 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
28 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
30 local function build_reply(status, ret, err_msg)
31 local reply = st.stanza(status, {xmlns = xmlns_sasl});
32 if status == "challenge" then
33 --log("debug", "CHALLENGE: %s", ret or "");
34 reply:text(base64.encode(ret or ""));
35 elseif status == "failure" then
37 if err_msg then reply:tag("text"):text(err_msg); end
38 elseif status == "success" then
39 --log("debug", "SUCCESS: %s", ret or "");
40 reply:text(base64.encode(ret or ""));
42 module:log("error", "Unknown sasl status: %s", status);
47 local function handle_status(session, status, ret, err_msg)
48 if status == "failure" then
49 module:fire_event("authentication-failure", { session = session, condition = ret, text = err_msg });
50 session.sasl_handler = session.sasl_handler:clean_clone();
51 elseif status == "success" then
52 local ok, err = sm_make_authenticated(session, session.sasl_handler.username);
54 module:fire_event("authentication-success", { session = session });
55 session.sasl_handler = nil;
56 session:reset_stream();
58 module:log("warn", "SASL succeeded but username was invalid");
59 module:fire_event("authentication-failure", { session = session, condition = "not-authorized", text = err });
60 session.sasl_handler = session.sasl_handler:clean_clone();
61 return "failure", "not-authorized", "User authenticated successfully, but username was invalid";
64 return status, ret, err_msg;
67 local function sasl_process_cdata(session, stanza)
68 local text = stanza[1];
70 text = base64.decode(text);
71 --log("debug", "AUTH: %s", text:gsub("[%z\001-\008\011\012\014-\031]", " "));
73 session.sasl_handler = nil;
74 session.send(build_reply("failure", "incorrect-encoding"));
78 local status, ret, err_msg = session.sasl_handler:process(text);
79 status, ret, err_msg = handle_status(session, status, ret, err_msg);
80 local s = build_reply(status, ret, err_msg);
81 log("debug", "sasl reply: %s", tostring(s));
86 module:hook_stanza(xmlns_sasl, "success", function (session, stanza)
87 if session.type ~= "s2sout_unauthed" or session.external_auth ~= "attempting" then return; end
88 module:log("debug", "SASL EXTERNAL with %s succeeded", session.to_host);
89 session.external_auth = "succeeded"
90 session:reset_stream();
92 local default_stream_attr = {xmlns = "jabber:server", ["xmlns:stream"] = "http://etherx.jabber.org/streams",
93 ["xmlns:db"] = 'jabber:server:dialback', version = "1.0", to = session.to_host, from = session.from_host};
94 session.sends2s("<?xml version='1.0'?>");
95 session.sends2s(st.stanza("stream:stream", default_stream_attr):top_tag());
97 s2s_make_authenticated(session, session.to_host);
101 module:hook_stanza(xmlns_sasl, "failure", function (session, stanza)
102 if session.type ~= "s2sout_unauthed" or session.external_auth ~= "attempting" then return; end
104 module:log("info", "SASL EXTERNAL with %s failed", session.to_host)
105 -- TODO: Log the failure reason
106 session.external_auth = "failed"
109 module:hook_stanza(xmlns_sasl, "failure", function (session, stanza)
110 -- TODO: Dialback wasn't loaded. Do something useful.
113 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza)
114 if session.type ~= "s2sout_unauthed" or not session.secure then return; end
116 local mechanisms = stanza:get_child("mechanisms", xmlns_sasl)
118 for mech in mechanisms:childtags() do
119 if mech[1] == "EXTERNAL" then
120 module:log("debug", "Initiating SASL EXTERNAL with %s", session.to_host);
121 local reply = st.stanza("auth", {xmlns = xmlns_sasl, mechanism = "EXTERNAL"});
122 reply:text(base64.encode(session.from_host))
123 session.sends2s(reply)
124 session.external_auth = "attempting"
131 local function s2s_external_auth(session, stanza)
132 local mechanism = stanza.attr.mechanism;
134 if not session.secure then
135 if mechanism == "EXTERNAL" then
136 session.sends2s(build_reply("failure", "encryption-required"))
138 session.sends2s(build_reply("failure", "invalid-mechanism"))
143 if mechanism ~= "EXTERNAL" or session.cert_chain_status ~= "valid" then
144 session.sends2s(build_reply("failure", "invalid-mechanism"))
148 local text = stanza[1]
150 session.sends2s(build_reply("failure", "malformed-request"))
154 -- Either the value is "=" and we've already verified the external
155 -- cert identity, or the value is a string and either matches the
158 text = base64.decode(text)
160 session.sends2s(build_reply("failure", "incorrect-encoding"))
164 if session.cert_identity_status == "valid" then
165 if text ~= "" and text ~= session.from_host then
166 session.sends2s(build_reply("failure", "invalid-authzid"))
171 session.sends2s(build_reply("failure", "invalid-authzid"))
175 local cert = session.conn:socket():getpeercertificate()
176 if (cert_verify_identity(text, "xmpp-server", cert)) then
177 session.cert_identity_status = "valid"
179 session.cert_identity_status = "invalid"
180 session.sends2s(build_reply("failure", "invalid-authzid"))
185 session.external_auth = "succeeded"
187 if not session.from_host then
188 session.from_host = text;
190 session.sends2s(build_reply("success"))
192 local domain = text ~= "" and text or session.from_host;
193 module:log("info", "Accepting SASL EXTERNAL identity from %s", domain);
194 s2s_make_authenticated(session, domain);
195 session:reset_stream();
199 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-sasl:auth", function(event)
200 local session, stanza = event.origin, event.stanza;
201 if session.type == "s2sin_unauthed" then
202 return s2s_external_auth(session, stanza)
205 if session.type ~= "c2s_unauthed" then return; end
207 if session.sasl_handler and session.sasl_handler.selected then
208 session.sasl_handler = nil; -- allow starting a new SASL negotiation before completing an old one
210 if not session.sasl_handler then
211 session.sasl_handler = usermanager_get_sasl_handler(module.host);
213 local mechanism = stanza.attr.mechanism;
214 if not session.secure and (secure_auth_only or (mechanism == "PLAIN" and not allow_unencrypted_plain_auth)) then
215 session.send(build_reply("failure", "encryption-required"));
218 local valid_mechanism = session.sasl_handler:select(mechanism);
219 if not valid_mechanism then
220 session.send(build_reply("failure", "invalid-mechanism"));
223 return sasl_process_cdata(session, stanza);
225 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-sasl:response", function(event)
226 local session = event.origin;
227 if not(session.sasl_handler and session.sasl_handler.selected) then
228 session.send(build_reply("failure", "not-authorized", "Out of order SASL element"));
231 return sasl_process_cdata(session, event.stanza);
233 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-sasl:abort", function(event)
234 local session = event.origin;
235 session.sasl_handler = nil;
236 session.send(build_reply("failure", "aborted"));
240 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
241 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
242 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
243 module:hook("stream-features", function(event)
244 local origin, features = event.origin, event.features;
245 if not origin.username then
246 if secure_auth_only and not origin.secure then
249 origin.sasl_handler = usermanager_get_sasl_handler(module.host);
250 local mechanisms = st.stanza("mechanisms", mechanisms_attr);
251 for mechanism in pairs(origin.sasl_handler:mechanisms()) do
252 if mechanism ~= "PLAIN" or origin.secure or allow_unencrypted_plain_auth then
253 mechanisms:tag("mechanism"):text(mechanism):up();
256 if mechanisms[1] then features:add_child(mechanisms); end
258 features:tag("bind", bind_attr):tag("required"):up():up();
259 features:tag("session", xmpp_session_attr):tag("optional"):up():up();
263 module:hook("s2s-stream-features", function(event)
264 local origin, features = event.origin, event.features;
265 if origin.secure and origin.type == "s2sin_unauthed" then
266 -- Offer EXTERNAL if chain is valid and either we didn't validate
267 -- the identity or it passed.
268 if origin.cert_chain_status == "valid" and origin.cert_identity_status ~= "invalid" then --TODO: Configurable
269 module:log("debug", "Offering SASL EXTERNAL")
270 features:tag("mechanisms", { xmlns = xmlns_sasl })
271 :tag("mechanism"):text("EXTERNAL")
277 module:hook("iq/self/urn:ietf:params:xml:ns:xmpp-bind:bind", function(event)
278 local origin, stanza = event.origin, event.stanza;
280 if stanza.attr.type == "set" then
281 local bind = stanza.tags[1];
282 resource = bind:child_with_name("resource");
283 resource = resource and #resource.tags == 0 and resource[1] or nil;
285 local success, err_type, err, err_msg = sm_bind_resource(origin, resource);
287 origin.send(st.reply(stanza)
288 :tag("bind", { xmlns = xmlns_bind })
289 :tag("jid"):text(origin.full_jid));
290 origin.log("debug", "Resource bound: %s", origin.full_jid);
292 origin.send(st.error_reply(stanza, err_type, err, err_msg));
293 origin.log("debug", "Resource bind failed: %s", err_msg or err);
298 local function handle_legacy_session(event)
299 event.origin.send(st.reply(event.stanza));
303 module:hook("iq/self/urn:ietf:params:xml:ns:xmpp-session:session", handle_legacy_session);
304 module:hook("iq/host/urn:ietf:params:xml:ns:xmpp-session:session", handle_legacy_session);