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';
37 if config.get(module:get_host(), "core", "cyrus_service_name") then
38 cyrus_new = require "util.sasl_cyrus".new;
39 new_sasl = function(realm)
40 return cyrus_new(realm, config.get(module:get_host(), "core", "cyrus_service_name"))
43 new_sasl = require "util.sasl".new;
46 default_authentication_profile = {
47 plain = function(username, realm)
48 local prepped_username = nodeprep(username);
49 if not prepped_username then
50 log("debug", "NODEprep failed on username: %s", username);
53 local password = usermanager_get_password(prepped_username, realm);
57 return password, true;
61 anonymous_authentication_profile = {
62 anonymous = function(username, realm)
63 return true; -- for normal usage you should always return true here
67 local function build_reply(status, ret, err_msg)
68 local reply = st.stanza(status, {xmlns = xmlns_sasl});
69 if status == "challenge" then
70 log("debug", "%s", ret or "");
71 reply:text(base64.encode(ret or ""));
72 elseif status == "failure" then
74 if err_msg then reply:tag("text"):text(err_msg); end
75 elseif status == "success" then
76 log("debug", "%s", ret or "");
77 reply:text(base64.encode(ret or ""));
79 module:log("error", "Unknown sasl status: %s", status);
84 local function handle_status(session, status)
85 if status == "failure" then
86 session.sasl_handler = session.sasl_handler:clean_clone();
87 elseif status == "success" then
88 local username = nodeprep(session.sasl_handler.username);
89 if not username then -- TODO move this to sessionmanager
90 module:log("warn", "SASL succeeded but we didn't get a username!");
91 session.sasl_handler = nil;
92 session:reset_stream();
95 sm_make_authenticated(session, session.sasl_handler.username);
96 session.sasl_handler = nil;
97 session:reset_stream();
101 local function sasl_handler(session, stanza)
102 if stanza.name == "auth" then
103 -- FIXME ignoring duplicates because ejabberd does
104 if config.get(session.host or "*", "core", "anonymous_login") then
105 if stanza.attr.mechanism ~= "ANONYMOUS" then
106 return session.send(build_reply("failure", "invalid-mechanism"));
108 elseif stanza.attr.mechanism == "ANONYMOUS" then
109 return session.send(build_reply("failure", "mechanism-too-weak"));
111 local valid_mechanism = session.sasl_handler:select(stanza.attr.mechanism);
112 if not valid_mechanism then
113 return session.send(build_reply("failure", "invalid-mechanism"));
115 if secure_auth_only and not session.secure then
116 return session.send(build_reply("failure", "encryption-required"));
118 elseif not session.sasl_handler then
119 return; -- FIXME ignoring out of order stanzas because ejabberd does
121 local text = stanza[1];
123 text = base64.decode(text);
124 log("debug", "%s", text:gsub("[%z\001-\008\011\012\014-\031]", " "));
126 session.sasl_handler = nil;
127 session.send(build_reply("failure", "incorrect-encoding"));
131 local status, ret, err_msg = session.sasl_handler:process(text);
132 handle_status(session, status);
133 local s = build_reply(status, ret, err_msg);
134 log("debug", "sasl reply: %s", tostring(s));
138 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
139 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
140 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
142 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
143 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
144 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
145 module:add_event_hook("stream-features",
146 function (session, features)
147 if not session.username then
148 if secure_auth_only and not session.secure then
151 if module:get_option("anonymous_login") then
152 session.sasl_handler = new_sasl(session.host, anonymous_authentication_profile);
154 session.sasl_handler = new_sasl(session.host, default_authentication_profile);
155 if not (module:get_option("allow_unencrypted_plain_auth")) and not session.secure then
156 session.sasl_handler:forbidden({"PLAIN"});
159 features:tag("mechanisms", mechanisms_attr);
160 for k, v in pairs(session.sasl_handler:mechanisms()) do
161 features:tag("mechanism"):text(v):up();
165 features:tag("bind", bind_attr):tag("required"):up():up();
166 features:tag("session", xmpp_session_attr):tag("optional"):up():up();
170 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
171 function (session, stanza)
172 log("debug", "Client requesting a resource bind");
174 if stanza.attr.type == "set" then
175 local bind = stanza.tags[1];
176 if bind and bind.attr.xmlns == xmlns_bind then
177 resource = bind:child_with_name("resource");
179 resource = resource[1];
183 local success, err_type, err, err_msg = sm_bind_resource(session, resource);
185 session.send(st.error_reply(stanza, err_type, err, err_msg));
187 session.send(st.reply(stanza)
188 :tag("bind", { xmlns = xmlns_bind})
189 :tag("jid"):text(session.full_jid));
193 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
194 function (session, stanza)
195 log("debug", "Client requesting a session");
196 session.send(st.reply(stanza));