dd268555d793ae52ac87afb1556654ba01eb4089
[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
11 local log = require "util.logger".init("mod_saslauth");
12
13 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
14 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
15 local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas';
16
17 local new_sasl = require "util.sasl".new;
18
19 local function build_reply(status, ret)
20         local reply = st.stanza(status, {xmlns = xmlns_sasl});
21         if status == "challenge" then
22                 reply:text(ret or "");
23         elseif status == "failure" then
24                 reply:tag(ret):up();
25         elseif status == "success" then
26                 reply:text(ret or "");
27         else
28                 error("Unknown sasl status: "..status);
29         end
30         return reply;
31 end
32
33 local function handle_status(session, status)
34         if status == "failure" then
35                 session.sasl_handler = nil;
36         elseif status == "success" then
37                 session.sasl_handler = nil;
38                 session:reset_stream();
39         end
40 end
41
42 local function password_callback(jid, mechanism)
43         local node, host = jid_split(jid);
44         local password = (datamanager.load(node, host, "accounts") or {}).password; -- FIXME handle hashed passwords
45         local func = function(x) return x; end;
46         if password then
47                 if mechanism == "PLAIN" then
48                         return func, password;
49                 elseif mechanism == "DIGEST-MD5" then
50                         return func, require "hashes".md5(node.."::"..password);
51                 end
52         end
53         return func, nil;
54 end
55
56 add_handler("c2s_unauthed", "auth", xmlns_sasl,
57                 function (session, stanza)
58                         if not session.sasl_handler then
59                                 session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, password_callback);
60                                 local status, ret = session.sasl_handler:feed(stanza[1]);
61                                 handle_status(session, status);
62                                 session.send(build_reply(status, ret));
63                                 --[[session.sasl_handler = new_sasl(stanza.attr.mechanism, 
64                                         function (username, password)
65                                                 -- onAuth
66                                                 require "core.usermanager"
67                                                 if usermanager_validate_credentials(session.host, username, password) then
68                                                         return true;
69                                                 end
70                                                 return false;
71                                         end,
72                                         function (username)
73                                                 -- onSuccess
74                                                 local success, err = sessionmanager.make_authenticated(session, username);
75                                                 if not success then
76                                                         sessionmanager.destroy_session(session);
77                                                         return;
78                                                 end
79                                                 session.sasl_handler = nil;
80                                                 session:reset_stream();
81                                         end,
82                                         function (reason)
83                                                 -- onFail
84                                                 log("debug", "SASL failure, reason: %s", reason);
85                                         end,
86                                         function (stanza)
87                                                 -- onWrite
88                                                 log("debug", "SASL writes: %s", tostring(stanza));
89                                                 send(session, stanza);
90                                         end
91                                 );
92                                 session.sasl_handler:feed(stanza);      ]]
93                         else
94                                 error("Client tried to negotiate SASL again", 0);
95                         end
96                 end);
97
98 add_handler("c2s_unauthed", "abort", xmlns_sasl,
99         function(session, stanza)
100                 if not session.sasl_handler then error("Attempt to abort when sasl has not started"); end
101                 local status, ret = session.sasl_handler:feed(stanza[1]);
102                 handle_status(session, status);
103                 session.send(build_reply(status, ret));
104         end);
105
106 add_handler("c2s_unauthed", "response", xmlns_sasl,
107         function(session, stanza)
108                 if not session.sasl_handler then error("Attempt to respond when sasl has not started"); end
109                 local status, ret = session.sasl_handler:feed(stanza[1]);
110                 handle_status(session, status);
111                 session.send(build_reply(status, ret));
112         end);
113                 
114 add_event_hook("stream-features", 
115                                         function (session, features)                                                                                            
116                                                 if not session.username then
117                                                         t_insert(features, "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>");
118                                                                 t_insert(features, "<mechanism>PLAIN</mechanism>");
119                                                                 t_insert(features, "<mechanism>DIGEST-MD5</mechanism>");
120                                                         t_insert(features, "</mechanisms>");
121                                                 else
122                                                         t_insert(features, "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><required/></bind>");
123                                                         t_insert(features, "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>");
124                                                 end
125                                                 --send [[<register xmlns="http://jabber.org/features/iq-register"/> ]]
126                                         end);
127                                         
128 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                                 
135                                 if bind and bind.attr.xmlns == xmlns_bind then
136                                         resource = bind:child_with_name("resource");
137                                         if resource then
138                                                 resource = resource[1];
139                                         end
140                                 end
141                         end
142                         local success, err = sm_bind_resource(session, resource);
143                         if not success then
144                                 local reply = st.reply(stanza);
145                                 reply.attr.type = "error";
146                                 if err == "conflict" then
147                                         reply:tag("error", { type = "modify" })
148                                                 :tag("conflict", { xmlns = xmlns_stanzas });
149                                 elseif err == "constraint" then
150                                         reply:tag("error", { type = "cancel" })
151                                                 :tag("resource-constraint", { xmlns = xmlns_stanzas });
152                                 elseif err == "auth" then
153                                         reply:tag("error", { type = "cancel" })
154                                                 :tag("not-allowed", { xmlns = xmlns_stanzas });
155                                 end
156                                 send(session, reply);
157                         else
158                                 local reply = st.reply(stanza);
159                                 reply:tag("bind", { xmlns = xmlns_bind})
160                                         :tag("jid"):text(session.full_jid);
161                                 send(session, reply);
162                         end
163                 end);
164                 
165 add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session", 
166                 function (session, stanza)
167                         log("debug", "Client tried to bind to a resource");
168                         send(session, st.reply(stanza));
169                 end);