ec3857b8c2142a8d91cd1c8e84920e742e0653e8
[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 datamanager_load = require "util.datamanager".load;
17 local usermanager_validate_credentials = require "core.usermanager".validate_credentials;
18 local usermanager_get_supported_methods = require "core.usermanager".get_supported_methods;
19 local usermanager_user_exists = require "core.usermanager".user_exists;
20 local usermanager_get_password = require "core.usermanager".get_password;
21 local t_concat, t_insert = table.concat, table.insert;
22 local tostring = tostring;
23 local jid_split = require "util.jid".split
24 local md5 = require "util.hashes".md5;
25 local config = require "core.configmanager";
26
27 local secure_auth_only = config.get(module:get_host(), "core", "require_encryption");
28
29 local log = module._log;
30
31 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
32 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
33 local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas';
34
35 local new_sasl = require "util.sasl".new;
36
37 default_authentication_profile = {
38         plain = function(username, realm)
39                         return usermanager_get_password(username, realm), true;
40                 end
41 };
42
43 local function build_reply(status, ret, err_msg)
44         local reply = st.stanza(status, {xmlns = xmlns_sasl});
45         if status == "challenge" then
46                 log("debug", "%s", ret or "");
47                 reply:text(base64.encode(ret or ""));
48         elseif status == "failure" then
49                 reply:tag(ret):up();
50                 if err_msg then reply:tag("text"):text(err_msg); end
51         elseif status == "success" then
52                 log("debug", "%s", ret or "");
53                 reply:text(base64.encode(ret or ""));
54         else
55                 module:log("error", "Unknown sasl status: %s", status);
56         end
57         return reply;
58 end
59
60 local function handle_status(session, status)
61         if status == "failure" then
62                 session.sasl_handler = nil;
63         elseif status == "success" then
64                 if not session.sasl_handler.username then -- TODO move this to sessionmanager
65                         module:log("warn", "SASL succeeded but we didn't get a username!");
66                         session.sasl_handler = nil;
67                         session:reset_stream();
68                         return;
69                 end
70                 sm_make_authenticated(session, session.sasl_handler.username);
71                 session.sasl_handler = nil;
72                 session:reset_stream();
73         end
74 end
75
76 local function credentials_callback(mechanism, ...)
77         if mechanism == "PLAIN" then
78                 local username, hostname, password = ...;
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                 function func(x) return x; end
87                 local node, domain, realm, decoder = ...;
88                 local password = usermanager_get_password(node, domain);
89                 if password then
90                         if decoder then
91                                 node, realm, password = decoder(node), decoder(realm), decoder(password);
92                         end
93                         return func, md5(node..":"..realm..":"..password);
94                 else
95                         return func, nil;
96                 end
97         end
98 end
99
100 local function sasl_handler(session, stanza)
101         if stanza.name == "auth" then
102                 -- FIXME ignoring duplicates because ejabberd does
103                 if config.get(session.host or "*", "core", "anonymous_login") then
104                         if stanza.attr.mechanism ~= "ANONYMOUS" then
105                                 return session.send(build_reply("failure", "invalid-mechanism"));
106                         end
107                 elseif stanza.attr.mechanism == "ANONYMOUS" then
108                         return session.send(build_reply("failure", "mechanism-too-weak"));
109                 end
110                 local valid_mechanism = session.sasl_handler:select(stanza.attr.mechanism);
111                 if not valid_mechanism then
112                         return session.send(build_reply("failure", "invalid-mechanism"));
113                 end
114         elseif not session.sasl_handler then
115                 return; -- FIXME ignoring out of order stanzas because ejabberd does
116         end
117         local text = stanza[1];
118         if text then
119                 text = base64.decode(text);
120                 log("debug", "%s", text);
121                 if not text then
122                         session.sasl_handler = nil;
123                         session.send(build_reply("failure", "incorrect-encoding"));
124                         return;
125                 end
126         end
127         local status, ret, err_msg = session.sasl_handler:process(text);
128         handle_status(session, status);
129         local s = build_reply(status, ret, err_msg);
130         log("debug", "sasl reply: %s", tostring(s));
131         session.send(s);
132 end
133
134 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
135 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
136 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
137
138 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
139 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
140 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
141 module:add_event_hook("stream-features",
142                 function (session, features)
143                         if not session.username then
144                                 if secure_auth_only and not session.secure then
145                                         return;
146                                 end
147                                 session.sasl_handler = new_sasl(session.host, default_authentication_profile);
148                                 features:tag("mechanisms", mechanisms_attr);
149                                 -- 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.
150                                         if config.get(session.host or "*", "core", "anonymous_login") then
151                                                 features:tag("mechanism"):text("ANONYMOUS"):up();
152                                         else
153                                                 for k, v in pairs(session.sasl_handler:mechanisms()) do
154                                                         features:tag("mechanism"):text(v):up();
155                                                 end
156                                         end
157                                 features:up();
158                         else
159                                 features:tag("bind", bind_attr):tag("required"):up():up();
160                                 features:tag("session", xmpp_session_attr):up();
161                         end
162                 end);
163
164 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
165                 function (session, stanza)
166                         log("debug", "Client requesting a resource bind");
167                         local resource;
168                         if stanza.attr.type == "set" then
169                                 local bind = stanza.tags[1];
170                                 if bind and bind.attr.xmlns == xmlns_bind then
171                                         resource = bind:child_with_name("resource");
172                                         if resource then
173                                                 resource = resource[1];
174                                         end
175                                 end
176                         end
177                         local success, err_type, err, err_msg = sm_bind_resource(session, resource);
178                         if not success then
179                                 session.send(st.error_reply(stanza, err_type, err, err_msg));
180                         else
181                                 session.send(st.reply(stanza)
182                                         :tag("bind", { xmlns = xmlns_bind})
183                                         :tag("jid"):text(session.full_jid));
184                         end
185                 end);
186
187 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
188                 function (session, stanza)
189                         log("debug", "Client requesting a session");
190                         session.send(st.reply(stanza));
191                 end);