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", "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 if not session.sasl_handler.username then -- TODO move this to sessionmanager
60 module:log("warn", "SASL succeeded but we didn't get a username!");
61 session.sasl_handler = nil;
62 session:reset_stream();
65 sm_make_authenticated(session, session.sasl_handler.username);
66 session.sasl_handler = nil;
67 session:reset_stream();
71 local function credentials_callback(mechanism, ...)
72 if mechanism == "PLAIN" then
73 local username, hostname, password = ...;
74 username = nodeprep(username);
78 local response = usermanager_validate_credentials(hostname, username, password, mechanism);
79 if response == nil then
84 elseif mechanism == "DIGEST-MD5" then
85 function func(x) return x; end
86 local node, domain, realm, decoder = ...;
87 local password = usermanager_get_password(node, domain);
90 node, realm, password = decoder(node), decoder(realm), decoder(password);
92 return func, md5(node..":"..realm..":"..password);
99 local function sasl_handler(session, stanza)
100 if stanza.name == "auth" then
101 -- FIXME ignoring duplicates because ejabberd does
102 if config.get(session.host or "*", "core", "anonymous_login") then
103 if stanza.attr.mechanism ~= "ANONYMOUS" then
104 return session.send(build_reply("failure", "invalid-mechanism"));
106 elseif stanza.attr.mechanism == "ANONYMOUS" then
107 return session.send(build_reply("failure", "mechanism-too-weak"));
109 session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, credentials_callback);
110 if not session.sasl_handler then
111 return session.send(build_reply("failure", "invalid-mechanism"));
113 elseif not session.sasl_handler then
114 return; -- FIXME ignoring out of order stanzas because ejabberd does
116 local text = stanza[1];
118 text = base64.decode(text);
119 log("debug", "%s", text);
121 session.sasl_handler = nil;
122 session.send(build_reply("failure", "incorrect-encoding"));
126 local status, ret, err_msg = session.sasl_handler:feed(text);
127 handle_status(session, status);
128 local s = build_reply(status, ret, err_msg);
129 log("debug", "sasl reply: %s", tostring(s));
133 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
134 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
135 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
137 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
138 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
139 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
140 module:add_event_hook("stream-features",
141 function (session, features)
142 if not session.username then
143 if secure_auth_only and not session.secure then
146 features:tag("mechanisms", mechanisms_attr);
147 -- 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.
148 if config.get(session.host or "*", "core", "anonymous_login") then
149 features:tag("mechanism"):text("ANONYMOUS"):up();
151 mechanisms = usermanager_get_supported_methods(session.host or "*");
152 for k, v in pairs(mechanisms) do
153 features:tag("mechanism"):text(k):up();
158 features:tag("bind", bind_attr):tag("required"):up():up();
159 features:tag("session", xmpp_session_attr):up();
163 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
164 function (session, stanza)
165 log("debug", "Client requesting a resource bind");
167 if stanza.attr.type == "set" then
168 local bind = stanza.tags[1];
169 if bind and bind.attr.xmlns == xmlns_bind then
170 resource = bind:child_with_name("resource");
172 resource = resource[1];
176 local success, err_type, err, err_msg = sm_bind_resource(session, resource);
178 session.send(st.error_reply(stanza, err_type, err, err_msg));
180 session.send(st.reply(stanza)
181 :tag("bind", { xmlns = xmlns_bind})
182 :tag("jid"):text(session.full_jid));
186 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
187 function (session, stanza)
188 log("debug", "Client requesting a session");
189 session.send(st.reply(stanza));