4934523f5ec6e9a2f7e533e5f69f001e158d5f1b
[prosody.git] / plugins / mod_saslauth.lua
1 -- Prosody IM v0.2
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21
22 local st = require "util.stanza";
23 local sm_bind_resource = require "core.sessionmanager".bind_resource;
24 local jid
25 local base64 = require "util.encodings".base64;
26
27 local usermanager_validate_credentials = require "core.usermanager".validate_credentials;
28 local t_concat, t_insert = table.concat, table.insert;
29 local tostring = tostring;
30 local jid_split = require "util.jid".split
31 local md5 = require "util.hashes".md5;
32
33 local log = require "util.logger".init("mod_saslauth");
34
35 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
36 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
37 local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas';
38
39 local new_sasl = require "util.sasl".new;
40
41 local function build_reply(status, ret, err_msg)
42         local reply = st.stanza(status, {xmlns = xmlns_sasl});
43         if status == "challenge" then
44                 log("challenge", ret or "");
45                 reply:text(base64.encode(ret or ""));
46         elseif status == "failure" then
47                 reply:tag(ret):up();
48                 if err_msg then reply:tag("text"):text(err_msg); end
49         elseif status == "success" then
50                 log("success", ret or "");
51                 reply:text(base64.encode(ret or ""));
52         else
53                 error("Unknown sasl status: "..status);
54         end
55         return reply;
56 end
57
58 local function handle_status(session, status)
59         if status == "failure" then
60                 session.sasl_handler = nil;
61         elseif status == "success" then
62                 if not session.sasl_handler.username then error("SASL succeeded but we didn't get a username!"); end -- TODO move this to sessionmanager
63                 sessionmanager.make_authenticated(session, session.sasl_handler.username);
64                 session.sasl_handler = nil;
65                 session:reset_stream();
66         end
67 end
68
69 local function password_callback(node, host, mechanism, decoder)
70         local password = (datamanager.load(node, host, "accounts") or {}).password; -- FIXME handle hashed passwords
71         local func = function(x) return x; end;
72         if password then
73                 if mechanism == "PLAIN" then
74                         return func, password;
75                 elseif mechanism == "DIGEST-MD5" then
76                         if decoder then node, host, password = decoder(node), decoder(host), decoder(password); end
77                         return func, md5(node..":"..host..":"..password);
78                 end
79         end
80         return func, nil;
81 end
82
83 function sasl_handler(session, stanza)
84         if stanza.name == "auth" then
85                 -- FIXME ignoring duplicates because ejabberd does
86                 session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, password_callback);
87         elseif not session.sasl_handler then
88                 return; -- FIXME ignoring out of order stanzas because ejabberd does
89         end
90         local text = stanza[1];
91         if text then
92                 text = base64.decode(text);
93                 log("recieved", text);
94                 if not text then
95                         session.sasl_handler = nil;
96                         session.send(build_reply("failure", "incorrect-encoding"));
97                         return;
98                 end
99         end
100         local status, ret, err_msg = session.sasl_handler:feed(text);
101         handle_status(session, status);
102         local s = build_reply(status, ret, err_msg); 
103         log("debug", "sasl reply: "..tostring(s));
104         session.send(s);
105 end
106
107 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
108 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
109 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
110
111 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
112 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
113 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
114 module:add_event_hook("stream-features", 
115                                         function (session, features)                                                                                            
116                                                 if not session.username then
117                                                         features:tag("mechanisms", mechanisms_attr);
118                                                         -- 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.
119                                                                 features:tag("mechanism"):text("PLAIN"):up();
120                                                                 features:tag("mechanism"):text("DIGEST-MD5"):up();
121                                                         features:up();
122                                                 else
123                                                         features:tag("bind", bind_attr):tag("required"):up():up();
124                                                         features:tag("session", xmpp_session_attr):up();
125                                                 end
126                                         end);
127                                         
128 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind", 
129                 function (session, stanza)
130                         log("debug", "Client tried to bind to a resource");
131                         local resource;
132                         if stanza.attr.type == "set" then
133                                 local bind = stanza.tags[1];
134                                 if bind and bind.attr.xmlns == xmlns_bind then
135                                         resource = bind:child_with_name("resource");
136                                         if resource then
137                                                 resource = resource[1];
138                                         end
139                                 end
140                         end
141                         local success, err_type, err, err_msg = sm_bind_resource(session, resource);
142                         if not success then
143                                 session.send(st.error_reply(stanza, err_type, err, err_msg));
144                         else
145                                 session.send(st.reply(stanza)
146                                         :tag("bind", { xmlns = xmlns_bind})
147                                         :tag("jid"):text(session.full_jid));
148                         end
149                 end);
150                 
151 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session", 
152                 function (session, stanza)
153                         log("debug", "Client tried to bind to a resource");
154                         session.send(st.reply(stanza));
155                 end);