util.sasl: Improved a log message.
[prosody.git] / plugins / mod_saslauth.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10
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;
15
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";
27
28 local secure_auth_only = config.get(module:get_host(), "core", "c2s_require_encryption") or config.get(module:get_host(), "core", "require_encryption");
29
30 local log = module._log;
31
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';
35
36 local new_sasl = require "util.sasl".new;
37
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
44                 reply:tag(ret):up();
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 ""));
49         else
50                 module:log("error", "Unknown sasl status: %s", status);
51         end
52         return reply;
53 end
54
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();
65                         return;
66                 end 
67                 sm_make_authenticated(session, username);
68                 session:reset_stream();
69         end
70 end
71
72 local function credentials_callback(mechanism, ...)
73         if mechanism == "PLAIN" then
74                 local username, hostname, password = ...;
75                 username = nodeprep(username);
76                 if not username then
77                         return false;
78                 end
79                 local response = usermanager_validate_credentials(hostname, username, password, mechanism);
80                 if response == nil then
81                         return false;
82                 else
83                         return response;
84                 end
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
90                         return func, nil;
91                 end
92                 local password = usermanager_get_password(prepped_node, domain);
93                 if password then
94                         if decoder then
95                                 node, realm, password = decoder(node), decoder(realm), decoder(password);
96                         end
97                         return func, md5(node..":"..realm..":"..password);
98                 else
99                         return func, nil;
100                 end
101         end
102 end
103
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"));
110                         end
111                 elseif stanza.attr.mechanism == "ANONYMOUS" then
112                         return session.send(build_reply("failure", "mechanism-too-weak"));
113                 end
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"));
117                 end
118         elseif not session.sasl_handler then
119                 return; -- FIXME ignoring out of order stanzas because ejabberd does
120         end
121         local text = stanza[1];
122         if text then
123                 text = base64.decode(text);
124                 log("debug", "%s", text);
125                 if not text then
126                         session.sasl_handler = nil;
127                         session.send(build_reply("failure", "incorrect-encoding"));
128                         return;
129                 end
130         end
131         local status, ret, err_msg = session.sasl_handler:feed(text);
132         handle_status(session, status);
133         local s = build_reply(status, ret, err_msg);
134         log("debug", "sasl reply: %s", tostring(s));
135         session.send(s);
136 end
137
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);
141
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
149                                         return;
150                                 end
151                                 features:tag("mechanisms", mechanisms_attr);
152                                 -- 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.
153                                         if config.get(session.host or "*", "core", "anonymous_login") then
154                                                 features:tag("mechanism"):text("ANONYMOUS"):up();
155                                         else
156                                                 local mechanisms = usermanager_get_supported_methods(session.host or "*");
157                                                 for k, v in pairs(mechanisms) do
158                                                         features:tag("mechanism"):text(k):up();
159                                                 end
160                                         end
161                                 features:up();
162                         else
163                                 features:tag("bind", bind_attr):tag("required"):up():up();
164                                 features:tag("session", xmpp_session_attr):tag("optional"):up():up();
165                         end
166                 end);
167
168 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
169                 function (session, stanza)
170                         log("debug", "Client requesting a resource bind");
171                         local resource;
172                         if stanza.attr.type == "set" then
173                                 local bind = stanza.tags[1];
174                                 if bind and bind.attr.xmlns == xmlns_bind then
175                                         resource = bind:child_with_name("resource");
176                                         if resource then
177                                                 resource = resource[1];
178                                         end
179                                 end
180                         end
181                         local success, err_type, err, err_msg = sm_bind_resource(session, resource);
182                         if not success then
183                                 session.send(st.error_reply(stanza, err_type, err, err_msg));
184                         else
185                                 session.send(st.reply(stanza)
186                                         :tag("bind", { xmlns = xmlns_bind})
187                                         :tag("jid"):text(session.full_jid));
188                         end
189                 end);
190
191 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
192                 function (session, stanza)
193                         log("debug", "Client requesting a session");
194                         session.send(st.reply(stanza));
195                 end);