f27d806039034a958ee477d4ccd87ad1fccded43
[prosody.git] / plugins / mod_saslauth.lua
1 -- Prosody IM v0.4
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 t_concat, t_insert = table.concat, table.insert;
19 local tostring = tostring;
20 local jid_split = require "util.jid".split
21 local md5 = require "util.hashes".md5;
22 local config = require "core.configmanager";
23
24 local log = module._log;
25
26 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
27 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
28 local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas';
29
30 local new_sasl = require "util.sasl".new;
31
32 local function build_reply(status, ret, err_msg)
33         local reply = st.stanza(status, {xmlns = xmlns_sasl});
34         if status == "challenge" then
35                 log("debug", "%s", ret or "");
36                 reply:text(base64.encode(ret or ""));
37         elseif status == "failure" then
38                 reply:tag(ret):up();
39                 if err_msg then reply:tag("text"):text(err_msg); end
40         elseif status == "success" then
41                 log("debug", "%s", ret or "");
42                 reply:text(base64.encode(ret or ""));
43         else
44                 module:log("error", "Unknown sasl status: %s", status);
45         end
46         return reply;
47 end
48
49 local function handle_status(session, status)
50         if status == "failure" then
51                 session.sasl_handler = nil;
52         elseif status == "success" then
53                 if not session.sasl_handler.username then -- TODO move this to sessionmanager
54                         module:log("warn", "SASL succeeded but we didn't get a username!");
55                         session.sasl_handler = nil;
56                         session:reset_stream();
57                         return;
58                 end 
59                 sm_make_authenticated(session, session.sasl_handler.username);
60                 session.sasl_handler = nil;
61                 session:reset_stream();
62         end
63 end
64
65 local function password_callback(node, host, mechanism, decoder)
66         local password = (datamanager_load(node, host, "accounts") or {}).password; -- FIXME handle hashed passwords
67         local func = function(x) return x; end;
68         if password then
69                 if mechanism == "PLAIN" then
70                         return func, password;
71                 elseif mechanism == "DIGEST-MD5" then
72                         if decoder then node, host, password = decoder(node), decoder(host), decoder(password); end
73                         return func, md5(node..":"..host..":"..password);
74                 end
75         end
76         return func, nil;
77 end
78
79 local function sasl_handler(session, stanza)
80         if stanza.name == "auth" then
81                 -- FIXME ignoring duplicates because ejabberd does
82                 if config.get(session.host or "*", "core", "anonymous_login") and stanza.attr.mechanism ~= "ANONYMOUS" then
83                         return session.send(build_reply("failure", "invalid-mechanism"));
84                 elseif stanza.attr.mechanism == "ANONYMOUS" then
85                         return session.send(build_reply("failure", "mechanism-too-weak"));
86                 end
87                 session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, password_callback);
88                 if not session.sasl_handler then
89                         return session.send(build_reply("failure", "invalid-mechanism"));
90                 end
91         elseif not session.sasl_handler then
92                 return; -- FIXME ignoring out of order stanzas because ejabberd does
93         end
94         local text = stanza[1];
95         if text then
96                 text = base64.decode(text);
97                 log("debug", "%s", text);
98                 if not text then
99                         session.sasl_handler = nil;
100                         session.send(build_reply("failure", "incorrect-encoding"));
101                         return;
102                 end
103         end
104         local status, ret, err_msg = session.sasl_handler:feed(text);
105         handle_status(session, status);
106         local s = build_reply(status, ret, err_msg); 
107         log("debug", "sasl reply: %s", tostring(s));
108         session.send(s);
109 end
110
111 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
112 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
113 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
114
115 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
116 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
117 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
118 module:add_event_hook("stream-features", 
119                 function (session, features)                                                                                            
120                         if not session.username then
121                                 features:tag("mechanisms", mechanisms_attr);
122                                 -- 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.
123                                         if config.get(session.host or "*", "core", "anonymous_login") then
124                                                 features:tag("mechanism"):text("ANONYMOUS"):up();
125                                         else
126                                                 features:tag("mechanism"):text("DIGEST-MD5"):up();
127                                                 features:tag("mechanism"):text("PLAIN"):up();
128                                         end
129                                 features:up();
130                         else
131                                 features:tag("bind", bind_attr):tag("required"):up():up();
132                                 features:tag("session", xmpp_session_attr):up();
133                         end
134                 end);
135                                         
136 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind", 
137                 function (session, stanza)
138                         log("debug", "Client requesting a resource bind");
139                         local resource;
140                         if stanza.attr.type == "set" then
141                                 local bind = stanza.tags[1];
142                                 if bind and bind.attr.xmlns == xmlns_bind then
143                                         resource = bind:child_with_name("resource");
144                                         if resource then
145                                                 resource = resource[1];
146                                         end
147                                 end
148                         end
149                         local success, err_type, err, err_msg = sm_bind_resource(session, resource);
150                         if not success then
151                                 session.send(st.error_reply(stanza, err_type, err, err_msg));
152                         else
153                                 session.send(st.reply(stanza)
154                                         :tag("bind", { xmlns = xmlns_bind})
155                                         :tag("jid"):text(session.full_jid));
156                         end
157                 end);
158                 
159 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session", 
160                 function (session, stanza)
161                         log("debug", "Client requesting a session");
162                         session.send(st.reply(stanza));
163                 end);