2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 base64 = require "util.encodings".base64;
16 local nodeprep = require "util.encodings".stringprep.nodeprep;
17 local datamanager_load = require "util.datamanager".load;
18 local usermanager_validate_credentials = require "core.usermanager".validate_credentials;
19 local usermanager_get_supported_methods = require "core.usermanager".get_supported_methods;
20 local usermanager_user_exists = require "core.usermanager".user_exists;
21 local usermanager_get_password = require "core.usermanager".get_password;
22 local t_concat, t_insert = table.concat, table.insert;
23 local tostring = tostring;
24 local jid_split = require "util.jid".split
25 local md5 = require "util.hashes".md5;
26 local config = require "core.configmanager";
28 local secure_auth_only = config.get(module:get_host(), "core", "c2s_require_encryption") or config.get(module:get_host(), "core", "require_encryption");
30 local log = module._log;
32 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
33 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
34 local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas';
36 local new_sasl = require "util.sasl".new;
38 local function build_reply(status, ret, err_msg)
39 local reply = st.stanza(status, {xmlns = xmlns_sasl});
40 if status == "challenge" then
41 log("debug", "%s", ret or "");
42 reply:text(base64.encode(ret or ""));
43 elseif status == "failure" then
45 if err_msg then reply:tag("text"):text(err_msg); end
46 elseif status == "success" then
47 log("debug", "%s", ret or "");
48 reply:text(base64.encode(ret or ""));
50 module:log("error", "Unknown sasl status: %s", status);
55 local function handle_status(session, status)
56 if status == "failure" then
57 session.sasl_handler = nil;
58 elseif status == "success" then
59 local username = nodeprep(session.sasl_handler.username);
60 session.sasl_handler = nil;
61 if not username then -- TODO move this to sessionmanager
62 module:log("warn", "SASL succeeded but we didn't get a username!");
63 session.sasl_handler = nil;
64 session:reset_stream();
67 sm_make_authenticated(session, username);
68 session:reset_stream();
72 local function credentials_callback(mechanism, ...)
73 if mechanism == "PLAIN" then
74 local username, hostname, password = ...;
75 username = nodeprep(username);
79 local response = usermanager_validate_credentials(hostname, username, password, mechanism);
80 if response == nil then
85 elseif mechanism == "DIGEST-MD5" then
86 local function func(x) return x; end
87 local node, domain, realm, decoder = ...;
88 local prepped_node = nodeprep(node);
89 if not prepped_node then
92 local password = usermanager_get_password(prepped_node, domain);
95 node, realm, password = decoder(node), decoder(realm), decoder(password);
97 return func, md5(node..":"..realm..":"..password);
104 local function sasl_handler(session, stanza)
105 if stanza.name == "auth" then
106 -- FIXME ignoring duplicates because ejabberd does
107 if config.get(session.host or "*", "core", "anonymous_login") then
108 if stanza.attr.mechanism ~= "ANONYMOUS" then
109 return session.send(build_reply("failure", "invalid-mechanism"));
111 elseif stanza.attr.mechanism == "ANONYMOUS" then
112 return session.send(build_reply("failure", "mechanism-too-weak"));
114 session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, credentials_callback);
115 if not session.sasl_handler then
116 return session.send(build_reply("failure", "invalid-mechanism"));
118 if secure_auth_only and not session.secure then
119 return session.send(build_reply("failure", "encryption-required"));
121 elseif not session.sasl_handler then
122 return; -- FIXME ignoring out of order stanzas because ejabberd does
124 local text = stanza[1];
126 text = base64.decode(text);
127 log("debug", "%s", text);
129 session.sasl_handler = nil;
130 session.send(build_reply("failure", "incorrect-encoding"));
134 local status, ret, err_msg = session.sasl_handler:feed(text);
135 handle_status(session, status);
136 local s = build_reply(status, ret, err_msg);
137 log("debug", "sasl reply: %s", tostring(s));
141 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
142 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
143 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
145 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
146 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
147 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
148 module:add_event_hook("stream-features",
149 function (session, features)
150 if not session.username then
151 if secure_auth_only and not session.secure then
154 features:tag("mechanisms", mechanisms_attr);
155 -- 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.
156 if config.get(session.host or "*", "core", "anonymous_login") then
157 features:tag("mechanism"):text("ANONYMOUS"):up();
159 local mechanisms = usermanager_get_supported_methods(session.host or "*");
160 for k, v in pairs(mechanisms) do
161 features:tag("mechanism"):text(k):up();
166 features:tag("bind", bind_attr):tag("required"):up():up();
167 features:tag("session", xmpp_session_attr):tag("optional"):up():up();
171 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
172 function (session, stanza)
173 log("debug", "Client requesting a resource bind");
175 if stanza.attr.type == "set" then
176 local bind = stanza.tags[1];
177 if bind and bind.attr.xmlns == xmlns_bind then
178 resource = bind:child_with_name("resource");
180 resource = resource[1];
184 local success, err_type, err, err_msg = sm_bind_resource(session, resource);
186 session.send(st.error_reply(stanza, err_type, err, err_msg));
188 session.send(st.reply(stanza)
189 :tag("bind", { xmlns = xmlns_bind})
190 :tag("jid"):text(session.full_jid));
194 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
195 function (session, stanza)
196 log("debug", "Client requesting a session");
197 session.send(st.reply(stanza));