5f7438d2d96841a3d052e781954dc49f0429e588
[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
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(ret or "");
24         elseif status == "failure" then
25                 reply:tag(ret):up();
26                 if err_msg then reply:tag("text"); end
27         elseif status == "success" then
28                 reply:text(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 "hashes".md5(node..":"..host..":"..password);
54                 end
55         end
56         return func, nil;
57 end
58
59 function do_sasl(session, stanza)
60         local text = stanza[1];
61         if text then
62                 text = base64.decode(text);
63                 if not text then
64                         session.sasl_handler = nil;
65                         session.send(build_reply("failure", "incorrect-encoding"));
66                         return;
67                 end
68         end
69         local status, ret, err_msg = session.sasl_handler:feed(text);
70         handle_status(session, status);
71         local s = build_reply(status, ret, err_msg); 
72         log("debug", "sasl reply: "..tostring(s));
73         session.send(s);
74 end
75
76 add_handler("c2s_unauthed", "auth", xmlns_sasl,
77                 function (session, stanza)
78                         if not session.sasl_handler then
79                                 session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, password_callback);
80                                 do_sasl(session, stanza);
81                         else
82                                 error("Client tried to negotiate SASL again", 0);
83                         end
84                 end);
85
86 add_handler("c2s_unauthed", "abort", xmlns_sasl,
87         function(session, stanza)
88                 if not session.sasl_handler then error("Attempt to abort when sasl has not started"); end
89                 do_sasl(session, stanza);
90         end);
91
92 add_handler("c2s_unauthed", "response", xmlns_sasl,
93         function(session, stanza)
94                 if not session.sasl_handler then error("Attempt to respond when sasl has not started"); end
95                 do_sasl(session, stanza);
96         end);
97
98 add_event_hook("stream-features", 
99                                         function (session, features)                                                                                            
100                                                 if not session.username then
101                                                         t_insert(features, "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>");
102                                                         -- 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.
103                                                                 t_insert(features, "<mechanism>PLAIN</mechanism>");
104                                                                 -- t_insert(features, "<mechanism>DIGEST-MD5</mechanism>");
105                                                         t_insert(features, "</mechanisms>");
106                                                 else
107                                                         t_insert(features, "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><required/></bind>");
108                                                         t_insert(features, "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>");
109                                                 end
110                                                 --send [[<register xmlns="http://jabber.org/features/iq-register"/> ]]
111                                         end);
112                                         
113 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind", 
114                 function (session, stanza)
115                         log("debug", "Client tried to bind to a resource");
116                         local resource;
117                         if stanza.attr.type == "set" then
118                                 local bind = stanza.tags[1];
119                                 
120                                 if bind and bind.attr.xmlns == xmlns_bind then
121                                         resource = bind:child_with_name("resource");
122                                         if resource then
123                                                 resource = resource[1];
124                                         end
125                                 end
126                         end
127                         local success, err = sm_bind_resource(session, resource);
128                         if not success then
129                                 local reply = st.reply(stanza);
130                                 reply.attr.type = "error";
131                                 if err == "conflict" then
132                                         reply:tag("error", { type = "modify" })
133                                                 :tag("conflict", { xmlns = xmlns_stanzas });
134                                 elseif err == "constraint" then
135                                         reply:tag("error", { type = "cancel" })
136                                                 :tag("resource-constraint", { xmlns = xmlns_stanzas });
137                                 elseif err == "auth" then
138                                         reply:tag("error", { type = "cancel" })
139                                                 :tag("not-allowed", { xmlns = xmlns_stanzas });
140                                 end
141                                 send(session, reply);
142                         else
143                                 local reply = st.reply(stanza);
144                                 reply:tag("bind", { xmlns = xmlns_bind})
145                                         :tag("jid"):text(session.full_jid);
146                                 send(session, reply);
147                         end
148                 end);
149                 
150 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session", 
151                 function (session, stanza)
152                         log("debug", "Client tried to bind to a resource");
153                         send(session, st.reply(stanza));
154                 end);