Use a stanza for c2s stream features instead of an array of strings. Removes a FIXME.
[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 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
87 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
88 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
89 add_event_hook("stream-features", 
90                                         function (session, features)                                                                                            
91                                                 if not session.username then
92                                                         features:tag("mechanisms", mechanisms_attr);
93                                                         -- 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.
94                                                                 features:tag("mechanism"):text("PLAIN"):up();
95                                                                 features:tag("mechanism"):text("DIGEST-MD5"):up();
96                                                         features:up();
97                                                 else
98                                                         features:tag("bind", bind_attr):tag("required"):up():up();
99                                                         features:tag("session", xmpp_session_attr):up();
100                                                 end
101                                         end);
102                                         
103 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind", 
104                 function (session, stanza)
105                         log("debug", "Client tried to bind to a resource");
106                         local resource;
107                         if stanza.attr.type == "set" then
108                                 local bind = stanza.tags[1];
109                                 if bind and bind.attr.xmlns == xmlns_bind then
110                                         resource = bind:child_with_name("resource");
111                                         if resource then
112                                                 resource = resource[1];
113                                         end
114                                 end
115                         end
116                         local success, err_type, err, err_msg = sm_bind_resource(session, resource);
117                         if not success then
118                                 session.send(st.error_reply(stanza, err_type, err, err_msg));
119                         else
120                                 session.send(st.reply(stanza)
121                                         :tag("bind", { xmlns = xmlns_bind})
122                                         :tag("jid"):text(session.full_jid));
123                         end
124                 end);
125                 
126 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session", 
127                 function (session, stanza)
128                         log("debug", "Client tried to bind to a resource");
129                         session.send(st.reply(stanza));
130                 end);