net.server_select: Allow setting the logger using server.setlogger
[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 nodeprep = require "util.encodings".stringprep.nodeprep;
17 local datamanager_load = require "util.datamanager".load;
18 local usermanager_validate_credentials = require "core.usermanager".validate_credentials;
19 local usermanager_get_supported_methods = require "core.usermanager".get_supported_methods;
20 local usermanager_user_exists = require "core.usermanager".user_exists;
21 local usermanager_get_password = require "core.usermanager".get_password;
22 local t_concat, t_insert = table.concat, table.insert;
23 local tostring = tostring;
24 local jid_split = require "util.jid".split
25 local md5 = require "util.hashes".md5;
26 local config = require "core.configmanager";
27
28 local secure_auth_only = module:get_option("c2s_require_encryption") or module:get_option("require_encryption");
29 local sasl_backend = module:get_option("sasl_backend") or "builtin";
30
31 local log = module._log;
32
33 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
34 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
35 local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas';
36
37 local new_sasl
38 if sasl_backend == "cyrus" then
39         cyrus_new = require "util.sasl_cyrus".new;
40         new_sasl = function(realm)
41                         return cyrus_new(realm, module:get_option("cyrus_service_name") or "xmpp")
42                 end
43 else
44         if sasl_backend ~= "builtin" then module:log("warn", "Unknown SASL backend %s", sasl_backend) end;
45         new_sasl = require "util.sasl".new;
46 end
47
48 default_authentication_profile = {
49         plain = function(username, realm)
50                         local prepped_username = nodeprep(username);
51                         if not prepped_username then
52                                 log("debug", "NODEprep failed on username: %s", username);
53                                 return "", nil;
54                         end
55                         local password = usermanager_get_password(prepped_username, realm);
56                         if not password then
57                                 return "", nil;
58                         end
59                         return password, true;
60                 end
61 };
62
63 anonymous_authentication_profile = {
64         anonymous = function(username, realm)
65                         return true; -- for normal usage you should always return true here
66                 end
67 }
68
69 local function build_reply(status, ret, err_msg)
70         local reply = st.stanza(status, {xmlns = xmlns_sasl});
71         if status == "challenge" then
72                 log("debug", "%s", ret or "");
73                 reply:text(base64.encode(ret or ""));
74         elseif status == "failure" then
75                 reply:tag(ret):up();
76                 if err_msg then reply:tag("text"):text(err_msg); end
77         elseif status == "success" then
78                 log("debug", "%s", ret or "");
79                 reply:text(base64.encode(ret or ""));
80         else
81                 module:log("error", "Unknown sasl status: %s", status);
82         end
83         return reply;
84 end
85
86 local function handle_status(session, status)
87         if status == "failure" then
88                 session.sasl_handler = session.sasl_handler:clean_clone();
89         elseif status == "success" then
90                 local username = nodeprep(session.sasl_handler.username);
91                 if not username then -- TODO move this to sessionmanager
92                         module:log("warn", "SASL succeeded but we didn't get a username!");
93                         session.sasl_handler = nil;
94                         session:reset_stream();
95                         return;
96                 end
97                 sm_make_authenticated(session, session.sasl_handler.username);
98                 session.sasl_handler = nil;
99                 session:reset_stream();
100         end
101 end
102
103 local function sasl_handler(session, stanza)
104         if stanza.name == "auth" then
105                 -- FIXME ignoring duplicates because ejabberd does
106                 if config.get(session.host or "*", "core", "anonymous_login") then
107                         if stanza.attr.mechanism ~= "ANONYMOUS" then
108                                 return session.send(build_reply("failure", "invalid-mechanism"));
109                         end
110                 elseif stanza.attr.mechanism == "ANONYMOUS" then
111                         return session.send(build_reply("failure", "mechanism-too-weak"));
112                 end
113                 local valid_mechanism = session.sasl_handler:select(stanza.attr.mechanism);
114                 if not valid_mechanism then
115                         return session.send(build_reply("failure", "invalid-mechanism"));
116                 end
117                 if secure_auth_only and not session.secure then
118                         return session.send(build_reply("failure", "encryption-required"));
119                 end
120         elseif not session.sasl_handler then
121                 return; -- FIXME ignoring out of order stanzas because ejabberd does
122         end
123         local text = stanza[1];
124         if text then
125                 text = base64.decode(text);
126                 log("debug", "%s", text:gsub("[%z\001-\008\011\012\014-\031]", " "));
127                 if not text then
128                         session.sasl_handler = nil;
129                         session.send(build_reply("failure", "incorrect-encoding"));
130                         return;
131                 end
132         end
133         local status, ret, err_msg = session.sasl_handler:process(text);
134         handle_status(session, status);
135         local s = build_reply(status, ret, err_msg);
136         log("debug", "sasl reply: %s", tostring(s));
137         session.send(s);
138 end
139
140 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
141 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
142 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
143
144 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
145 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
146 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
147 module:add_event_hook("stream-features",
148                 function (session, features)
149                         if not session.username then
150                                 if secure_auth_only and not session.secure then
151                                         return;
152                                 end
153                                 if module:get_option("anonymous_login") then
154                                         session.sasl_handler = new_sasl(session.host, anonymous_authentication_profile);
155                                 else
156                                         session.sasl_handler = new_sasl(session.host, default_authentication_profile);
157                                         if not (module:get_option("allow_unencrypted_plain_auth")) and not session.secure then
158                                                 session.sasl_handler:forbidden({"PLAIN"});
159                                         end
160                                 end
161                                 features:tag("mechanisms", mechanisms_attr);
162                                 for k, v in pairs(session.sasl_handler:mechanisms()) do
163                                         features:tag("mechanism"):text(v):up();
164                                 end
165                                 features:up();
166                         else
167                                 features:tag("bind", bind_attr):tag("required"):up():up();
168                                 features:tag("session", xmpp_session_attr):tag("optional"):up():up();
169                         end
170                 end);
171
172 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind",
173                 function (session, stanza)
174                         log("debug", "Client requesting a resource bind");
175                         local resource;
176                         if stanza.attr.type == "set" then
177                                 local bind = stanza.tags[1];
178                                 if bind and bind.attr.xmlns == xmlns_bind then
179                                         resource = bind:child_with_name("resource");
180                                         if resource then
181                                                 resource = resource[1];
182                                         end
183                                 end
184                         end
185                         local success, err_type, err, err_msg = sm_bind_resource(session, resource);
186                         if not success then
187                                 session.send(st.error_reply(stanza, err_type, err, err_msg));
188                         else
189                                 session.send(st.reply(stanza)
190                                         :tag("bind", { xmlns = xmlns_bind})
191                                         :tag("jid"):text(session.full_jid));
192                         end
193                 end);
194
195 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session",
196                 function (session, stanza)
197                         log("debug", "Client requesting a session");
198                         session.send(st.reply(stanza));
199                 end);