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