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