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 prepped_node = nodeprep(node);
88 if not prepped_node then
91 local password = usermanager_get_password(prepped_node, domain);
94 node, realm, password = decoder(node), decoder(realm), decoder(password);
96 return func, md5(node..":"..realm..":"..password);
103 local function sasl_handler(session, stanza)
104 if stanza.name == "auth" then
105 -- FIXME ignoring duplicates because ejabberd does
106 if config.get(session.host or "*", "core", "anonymous_login") then
107 if stanza.attr.mechanism ~= "ANONYMOUS" then
108 return session.send(build_reply("failure", "invalid-mechanism"));
110 elseif stanza.attr.mechanism == "ANONYMOUS" then
111 return session.send(build_reply("failure", "mechanism-too-weak"));
113 session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, credentials_callback);
114 if not session.sasl_handler then
115 return session.send(build_reply("failure", "invalid-mechanism"));
117 elseif not session.sasl_handler then
118 return; -- FIXME ignoring out of order stanzas because ejabberd does
120 local text = stanza[1];
122 text = base64.decode(text);
123 log("debug", "%s", text);
125 session.sasl_handler = nil;
126 session.send(build_reply("failure", "incorrect-encoding"));
130 local status, ret, err_msg = session.sasl_handler:feed(text);
131 handle_status(session, status);
132 local s = build_reply(status, ret, err_msg);
133 log("debug", "sasl reply: %s", tostring(s));
137 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
138 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
139 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
141 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
142 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
143 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
144 module:add_event_hook("stream-features",
145 function (session, features)
146 if not session.username then
147 if secure_auth_only and not session.secure then
150 features:tag("mechanisms", mechanisms_attr);
151 -- 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.
152 if config.get(session.host or "*", "core", "anonymous_login") then
153 features:tag("mechanism"):text("ANONYMOUS"):up();
155 mechanisms = usermanager_get_supported_methods(session.host or "*");
156 for k, v in pairs(mechanisms) do
157 features:tag("mechanism"):text(k):up();
162 features:tag("bind", bind_attr):tag("required"):up():up();
163 features:tag("session", xmpp_session_attr):up();
167 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
168 function (session, stanza)
169 log("debug", "Client requesting a resource bind");
171 if stanza.attr.type == "set" then
172 local bind = stanza.tags[1];
173 if bind and bind.attr.xmlns == xmlns_bind then
174 resource = bind:child_with_name("resource");
176 resource = resource[1];
180 local success, err_type, err, err_msg = sm_bind_resource(session, resource);
182 session.send(st.error_reply(stanza, err_type, err, err_msg));
184 session.send(st.reply(stanza)
185 :tag("bind", { xmlns = xmlns_bind})
186 :tag("jid"):text(session.full_jid));
190 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
191 function (session, stanza)
192 log("debug", "Client requesting a session");
193 session.send(st.reply(stanza));