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