mod_ping: Convert from Windows line endings
[prosody.git] / plugins / mod_saslauth.lua
1 -- Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10
11 local st = require "util.stanza";
12 local sm_bind_resource = require "core.sessionmanager".bind_resource;
13 local sm_make_authenticated = require "core.sessionmanager".make_authenticated;
14 local base64 = require "util.encodings".base64;
15
16 local datamanager_load = require "util.datamanager".load;
17 local usermanager_validate_credentials = require "core.usermanager".validate_credentials;
18 local t_concat, t_insert = table.concat, table.insert;
19 local tostring = tostring;
20 local jid_split = require "util.jid".split
21 local md5 = require "util.hashes".md5;
22 local config = require "core.configmanager";
23
24 local secure_auth_only = config.get(module:get_host(), "core", "require_encryption");
25
26 local log = module._log;
27
28 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
29 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
30 local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas';
31
32 local new_sasl = require "util.sasl".new;
33
34 local function build_reply(status, ret, err_msg)
35         local reply = st.stanza(status, {xmlns = xmlns_sasl});
36         if status == "challenge" then
37                 log("debug", "%s", ret or "");
38                 reply:text(base64.encode(ret or ""));
39         elseif status == "failure" then
40                 reply:tag(ret):up();
41                 if err_msg then reply:tag("text"):text(err_msg); end
42         elseif status == "success" then
43                 log("debug", "%s", ret or "");
44                 reply:text(base64.encode(ret or ""));
45         else
46                 module:log("error", "Unknown sasl status: %s", status);
47         end
48         return reply;
49 end
50
51 local function handle_status(session, status)
52         if status == "failure" then
53                 session.sasl_handler = nil;
54         elseif status == "success" then
55                 if not session.sasl_handler.username then -- TODO move this to sessionmanager
56                         module:log("warn", "SASL succeeded but we didn't get a username!");
57                         session.sasl_handler = nil;
58                         session:reset_stream();
59                         return;
60                 end 
61                 sm_make_authenticated(session, session.sasl_handler.username);
62                 session.sasl_handler = nil;
63                 session:reset_stream();
64         end
65 end
66
67 local function password_callback(node, hostname, realm, mechanism, decoder)
68         local password = (datamanager_load(node, hostname, "accounts") or {}).password; -- FIXME handle hashed passwords
69         local func = function(x) return x; end;
70         if password then
71                 if mechanism == "PLAIN" then
72                         return func, password;
73                 elseif mechanism == "DIGEST-MD5" then
74                         if decoder then node, realm, password = decoder(node), decoder(realm), decoder(password); end
75                         return func, md5(node..":"..realm..":"..password);
76                 end
77         end
78         return func, nil;
79 end
80
81 local function sasl_handler(session, stanza)
82         if stanza.name == "auth" then
83                 -- FIXME ignoring duplicates because ejabberd does
84                 if config.get(session.host or "*", "core", "anonymous_login") then
85                         if stanza.attr.mechanism ~= "ANONYMOUS" then
86                                 return session.send(build_reply("failure", "invalid-mechanism"));
87                         end
88                 elseif stanza.attr.mechanism == "ANONYMOUS" then
89                         return session.send(build_reply("failure", "mechanism-too-weak"));
90                 end
91                 session.sasl_handler = new_sasl(stanza.attr.mechanism, session.host, password_callback);
92                 if not session.sasl_handler then
93                         return session.send(build_reply("failure", "invalid-mechanism"));
94                 end
95         elseif not session.sasl_handler then
96                 return; -- FIXME ignoring out of order stanzas because ejabberd does
97         end
98         local text = stanza[1];
99         if text then
100                 text = base64.decode(text);
101                 log("debug", "%s", text);
102                 if not text then
103                         session.sasl_handler = nil;
104                         session.send(build_reply("failure", "incorrect-encoding"));
105                         return;
106                 end
107         end
108         local status, ret, err_msg = session.sasl_handler:feed(text);
109         handle_status(session, status);
110         local s = build_reply(status, ret, err_msg); 
111         log("debug", "sasl reply: %s", tostring(s));
112         session.send(s);
113 end
114
115 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
116 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
117 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
118
119 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
120 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
121 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
122 module:add_event_hook("stream-features", 
123                 function (session, features)                                                                                            
124                         if not session.username then
125                                 if secure_auth_only and not session.secure then
126                                         return;
127                                 end
128                                 features:tag("mechanisms", mechanisms_attr);
129                                 -- 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.
130                                         if config.get(session.host or "*", "core", "anonymous_login") then
131                                                 features:tag("mechanism"):text("ANONYMOUS"):up();
132                                         else
133                                                 features:tag("mechanism"):text("DIGEST-MD5"):up();
134                                                 features:tag("mechanism"):text("PLAIN"):up();
135                                         end
136                                 features:up();
137                         else
138                                 features:tag("bind", bind_attr):tag("required"):up():up();
139                                 features:tag("session", xmpp_session_attr):up();
140                         end
141                 end);
142                                         
143 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind", 
144                 function (session, stanza)
145                         log("debug", "Client requesting a resource bind");
146                         local resource;
147                         if stanza.attr.type == "set" then
148                                 local bind = stanza.tags[1];
149                                 if bind and bind.attr.xmlns == xmlns_bind then
150                                         resource = bind:child_with_name("resource");
151                                         if resource then
152                                                 resource = resource[1];
153                                         end
154                                 end
155                         end
156                         local success, err_type, err, err_msg = sm_bind_resource(session, resource);
157                         if not success then
158                                 session.send(st.error_reply(stanza, err_type, err, err_msg));
159                         else
160                                 session.send(st.reply(stanza)
161                                         :tag("bind", { xmlns = xmlns_bind})
162                                         :tag("jid"):text(session.full_jid));
163                         end
164                 end);
165                 
166 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session", 
167                 function (session, stanza)
168                         log("debug", "Client requesting a session");
169                         session.send(st.reply(stanza));
170                 end);