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