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 default_authentication_profile = {
39 plain = function(username, realm)
40 local prepped_username = nodeprep(username);
41 if not prepped_username then
42 log("debug", "NODEprep failed on username: %s", username);
45 local password = usermanager_get_password(prepped_username, realm);
49 return password, true;
53 anonymous_authentication_profile = {
54 anonymous = function(username, realm)
55 return true; -- for normal usage you should always return true here
59 local function build_reply(status, ret, err_msg)
60 local reply = st.stanza(status, {xmlns = xmlns_sasl});
61 if status == "challenge" then
62 log("debug", "%s", ret or "");
63 reply:text(base64.encode(ret or ""));
64 elseif status == "failure" then
66 if err_msg then reply:tag("text"):text(err_msg); end
67 elseif status == "success" then
68 log("debug", "%s", ret or "");
69 reply:text(base64.encode(ret or ""));
71 module:log("error", "Unknown sasl status: %s", status);
76 local function handle_status(session, status)
77 if status == "failure" then
78 session.sasl_handler = sesion.sasl_handler:clean_clone();
79 elseif status == "success" then
80 local username = nodeprep(session.sasl_handler.username);
81 if not username then -- TODO move this to sessionmanager
82 module:log("warn", "SASL succeeded but we didn't get a username!");
83 session.sasl_handler = nil;
84 session:reset_stream();
87 sm_make_authenticated(session, session.sasl_handler.username);
88 session.sasl_handler = nil;
89 session:reset_stream();
93 local function sasl_handler(session, stanza)
94 if stanza.name == "auth" then
95 -- FIXME ignoring duplicates because ejabberd does
96 if config.get(session.host or "*", "core", "anonymous_login") then
97 if stanza.attr.mechanism ~= "ANONYMOUS" then
98 return session.send(build_reply("failure", "invalid-mechanism"));
100 elseif stanza.attr.mechanism == "ANONYMOUS" then
101 return session.send(build_reply("failure", "mechanism-too-weak"));
103 local valid_mechanism = session.sasl_handler:select(stanza.attr.mechanism);
104 if not valid_mechanism then
105 return session.send(build_reply("failure", "invalid-mechanism"));
107 elseif not session.sasl_handler then
108 return; -- FIXME ignoring out of order stanzas because ejabberd does
110 local text = stanza[1];
112 text = base64.decode(text);
113 log("debug", "%s", text);
115 session.sasl_handler = nil;
116 session.send(build_reply("failure", "incorrect-encoding"));
120 local status, ret, err_msg = session.sasl_handler:process(text);
121 handle_status(session, status);
122 local s = build_reply(status, ret, err_msg);
123 log("debug", "sasl reply: %s", tostring(s));
127 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
128 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
129 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
131 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
132 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
133 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
134 module:add_event_hook("stream-features",
135 function (session, features)
136 if not session.username then
137 if secure_auth_only and not session.secure then
140 if module:get_option("anonymous_login") then
141 session.sasl_handler = new_sasl(session.host, anonymous_authentication_profile);
143 session.sasl_handler = new_sasl(session.host, default_authentication_profile);
144 if not (module:get_option("allow_unencrypted_plain_auth")) and not session.secure then
145 session.sasl_handler:forbidden({"PLAIN"});
148 features:tag("mechanisms", mechanisms_attr);
149 for k, v in pairs(session.sasl_handler:mechanisms()) do
150 features:tag("mechanism"):text(v):up();
154 features:tag("bind", bind_attr):tag("required"):up():up();
155 features:tag("session", xmpp_session_attr):tag("optional"):up():up();
159 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
160 function (session, stanza)
161 log("debug", "Client requesting a resource bind");
163 if stanza.attr.type == "set" then
164 local bind = stanza.tags[1];
165 if bind and bind.attr.xmlns == xmlns_bind then
166 resource = bind:child_with_name("resource");
168 resource = resource[1];
172 local success, err_type, err, err_msg = sm_bind_resource(session, resource);
174 session.send(st.error_reply(stanza, err_type, err, err_msg));
176 session.send(st.reply(stanza)
177 :tag("bind", { xmlns = xmlns_bind})
178 :tag("jid"):text(session.full_jid));
182 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
183 function (session, stanza)
184 log("debug", "Client requesting a session");
185 session.send(st.reply(stanza));