da66717cf2e48a0e744709f4c60acfe749fe1d1a
[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", "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                 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();
63                         return;
64                 end
65                 sm_make_authenticated(session, session.sasl_handler.username);
66                 session.sasl_handler = nil;
67                 session:reset_stream();
68         end
69 end
70
71 local function credentials_callback(mechanism, ...)
72         if mechanism == "PLAIN" then
73                 local username, hostname, password = ...;
74                 username = nodeprep(username);
75                 if not username then
76                         return false;
77                 end
78                 local response = usermanager_validate_credentials(hostname, username, password, mechanism);
79                 if response == nil then
80                         return false;
81                 else
82                         return response;
83                 end
84         elseif mechanism == "DIGEST-MD5" then
85                 function func(x) return x; end
86                 local node, domain, realm, decoder = ...;
87                 local password = usermanager_get_password(node, domain);
88                 if password then
89                         if decoder then
90                                 node, realm, password = decoder(node), decoder(realm), decoder(password);
91                         end
92                         return func, md5(node..":"..realm..":"..password);
93                 else
94                         return func, nil;
95                 end
96         end
97 end
98
99 local function sasl_handler(session, stanza)
100         if stanza.name == "auth" then
101                 -- FIXME ignoring duplicates because ejabberd does
102                 if config.get(session.host or "*", "core", "anonymous_login") then
103                         if stanza.attr.mechanism ~= "ANONYMOUS" then
104                                 return session.send(build_reply("failure", "invalid-mechanism"));
105                         end
106                 elseif stanza.attr.mechanism == "ANONYMOUS" then
107                         return session.send(build_reply("failure", "mechanism-too-weak"));
108                 end
109                 session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, credentials_callback);
110                 if not session.sasl_handler then
111                         return session.send(build_reply("failure", "invalid-mechanism"));
112                 end
113         elseif not session.sasl_handler then
114                 return; -- FIXME ignoring out of order stanzas because ejabberd does
115         end
116         local text = stanza[1];
117         if text then
118                 text = base64.decode(text);
119                 log("debug", "%s", text);
120                 if not text then
121                         session.sasl_handler = nil;
122                         session.send(build_reply("failure", "incorrect-encoding"));
123                         return;
124                 end
125         end
126         local status, ret, err_msg = session.sasl_handler:feed(text);
127         handle_status(session, status);
128         local s = build_reply(status, ret, err_msg);
129         log("debug", "sasl reply: %s", tostring(s));
130         session.send(s);
131 end
132
133 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
134 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
135 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
136
137 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
138 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
139 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
140 module:add_event_hook("stream-features",
141                 function (session, features)
142                         if not session.username then
143                                 if secure_auth_only and not session.secure then
144                                         return;
145                                 end
146                                 features:tag("mechanisms", mechanisms_attr);
147                                 -- 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.
148                                         if config.get(session.host or "*", "core", "anonymous_login") then
149                                                 features:tag("mechanism"):text("ANONYMOUS"):up();
150                                         else
151                                                 mechanisms = usermanager_get_supported_methods(session.host or "*");
152                                                 for k, v in pairs(mechanisms) do
153                                                         features:tag("mechanism"):text(k):up();
154                                                 end
155                                         end
156                                 features:up();
157                         else
158                                 features:tag("bind", bind_attr):tag("required"):up():up();
159                                 features:tag("session", xmpp_session_attr):up();
160                         end
161                 end);
162
163 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
164                 function (session, stanza)
165                         log("debug", "Client requesting a resource bind");
166                         local resource;
167                         if stanza.attr.type == "set" then
168                                 local bind = stanza.tags[1];
169                                 if bind and bind.attr.xmlns == xmlns_bind then
170                                         resource = bind:child_with_name("resource");
171                                         if resource then
172                                                 resource = resource[1];
173                                         end
174                                 end
175                         end
176                         local success, err_type, err, err_msg = sm_bind_resource(session, resource);
177                         if not success then
178                                 session.send(st.error_reply(stanza, err_type, err, err_msg));
179                         else
180                                 session.send(st.reply(stanza)
181                                         :tag("bind", { xmlns = xmlns_bind})
182                                         :tag("jid"):text(session.full_jid));
183                         end
184                 end);
185
186 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
187                 function (session, stanza)
188                         log("debug", "Client requesting a session");
189                         session.send(st.reply(stanza));
190                 end);