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