cbf9eeb7dbd2a77971cb2b540a42b2831ff4c7d4
[prosody.git] / plugins / mod_saslauth.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 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_get_provider = require "core.usermanager".get_provider;
19 local usermanager_get_sasl_handler = require "core.usermanager".get_sasl_handler;
20 local usermanager_user_exists = require "core.usermanager".user_exists;
21 local usermanager_get_password = require "core.usermanager".get_password;
22 local usermanager_test_password = require "core.usermanager".test_password;
23 local t_concat, t_insert = table.concat, table.insert;
24 local tostring = tostring;
25 local jid_split = require "util.jid".split;
26 local md5 = require "util.hashes".md5;
27 local config = require "core.configmanager";
28
29 local secure_auth_only = module:get_option("c2s_require_encryption") or module:get_option("require_encryption");
30 local sasl_backend = module:get_option("sasl_backend") or "builtin";
31 local anonymous_login = module:get_option("anonymous_login");
32
33 -- Cyrus config options
34 local require_provisioning = module:get_option("cyrus_require_provisioning") or false;
35 local cyrus_service_realm = module:get_option("cyrus_service_realm");
36 local cyrus_service_name = module:get_option("cyrus_service_name");
37 local cyrus_application_name = module:get_option("cyrus_application_name");
38
39 local log = module._log;
40
41 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
42 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
43 local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas';
44
45 local new_sasl;
46 if sasl_backend == "builtin" then
47         new_sasl = require "util.sasl".new;
48 elseif sasl_backend == "cyrus" then
49         prosody.unlock_globals(); --FIXME: Figure out why this is needed and
50                                   -- why cyrussasl isn't caught by the sandbox
51         local ok, cyrus = pcall(require, "util.sasl_cyrus");
52         prosody.lock_globals();
53         if ok then
54                 local cyrus_new = cyrus.new;
55                 new_sasl = function(realm)
56                         return cyrus_new(
57                                 cyrus_service_realm or realm,
58                                 cyrus_service_name or "xmpp",
59                                 cyrus_application_name or "prosody"
60                         );
61                 end
62         else
63                 module:log("error", "Failed to load Cyrus SASL because: %s", cyrus);
64                 error("Failed to load Cyrus SASL");
65         end
66 else
67         module:log("error", "Unknown SASL backend: %s", sasl_backend);
68         error("Unknown SASL backend");
69 end
70
71 local anonymous_authentication_profile = {
72         anonymous = function(username, realm)
73                 return true; -- for normal usage you should always return true here
74         end
75 };
76
77 local function build_reply(status, ret, err_msg)
78         local reply = st.stanza(status, {xmlns = xmlns_sasl});
79         if status == "challenge" then
80                 --log("debug", "CHALLENGE: %s", ret or "");
81                 reply:text(base64.encode(ret or ""));
82         elseif status == "failure" then
83                 reply:tag(ret):up();
84                 if err_msg then reply:tag("text"):text(err_msg); end
85         elseif status == "success" then
86                 --log("debug", "SUCCESS: %s", ret or "");
87                 reply:text(base64.encode(ret or ""));
88         else
89                 module:log("error", "Unknown sasl status: %s", status);
90         end
91         return reply;
92 end
93
94 local function handle_status(session, status, ret, err_msg)
95         if status == "failure" then
96                 session.sasl_handler = session.sasl_handler:clean_clone();
97         elseif status == "success" then
98                 local username = nodeprep(session.sasl_handler.username);
99
100                 if not(require_provisioning) or usermanager_user_exists(username, session.host) then
101                         local ok, err = sm_make_authenticated(session, session.sasl_handler.username);
102                         if ok then
103                                 session.sasl_handler = nil;
104                                 session:reset_stream();
105                         else
106                                 module:log("warn", "SASL succeeded but username was invalid");
107                                 session.sasl_handler = session.sasl_handler:clean_clone();
108                                 return "failure", "not-authorized", "User authenticated successfully, but username was invalid";
109                         end
110                 else
111                         module:log("warn", "SASL succeeded but we don't have an account provisioned for %s", username);
112                         session.sasl_handler = session.sasl_handler:clean_clone();
113                         return "failure", "not-authorized", "User authenticated successfully, but not provisioned for XMPP";
114                 end
115         end
116         return status, ret, err_msg;
117 end
118
119 local function sasl_handler(session, stanza)
120         if stanza.name == "auth" then
121                 -- FIXME ignoring duplicates because ejabberd does
122                 if anonymous_login then
123                         if stanza.attr.mechanism ~= "ANONYMOUS" then
124                                 return session.send(build_reply("failure", "invalid-mechanism"));
125                         end
126                 elseif stanza.attr.mechanism == "ANONYMOUS" then
127                         return session.send(build_reply("failure", "mechanism-too-weak"));
128                 end
129                 local valid_mechanism = session.sasl_handler:select(stanza.attr.mechanism);
130                 if not valid_mechanism then
131                         return session.send(build_reply("failure", "invalid-mechanism"));
132                 end
133                 if secure_auth_only and not session.secure then
134                         return session.send(build_reply("failure", "encryption-required"));
135                 end
136         elseif not session.sasl_handler then
137                 return; -- FIXME ignoring out of order stanzas because ejabberd does
138         end
139         local text = stanza[1];
140         if text then
141                 text = base64.decode(text);
142                 --log("debug", "AUTH: %s", text:gsub("[%z\001-\008\011\012\014-\031]", " "));
143                 if not text then
144                         session.sasl_handler = nil;
145                         session.send(build_reply("failure", "incorrect-encoding"));
146                         return;
147                 end
148         end
149         local status, ret, err_msg = session.sasl_handler:process(text);
150         status, ret, err_msg = handle_status(session, status, ret, err_msg);
151         local s = build_reply(status, ret, err_msg);
152         log("debug", "sasl reply: %s", tostring(s));
153         session.send(s);
154 end
155
156 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
157 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
158 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
159
160 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
161 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
162 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
163 module:hook("stream-features", function(event)
164         local origin, features = event.origin, event.features;
165         if not origin.username then
166                 if secure_auth_only and not origin.secure then
167                         return;
168                 end
169                 local realm = module:get_option("sasl_realm") or origin.host;
170                 if anonymous_login then
171                         origin.sasl_handler = new_sasl(realm, anonymous_authentication_profile);
172                 else
173                         origin.sasl_handler = usermanager_get_sasl_handler(module.host);
174                         if not (module:get_option("allow_unencrypted_plain_auth")) and not origin.secure then
175                                 origin.sasl_handler:forbidden({"PLAIN"});
176                         end
177                 end
178                 features:tag("mechanisms", mechanisms_attr);
179                 for k in pairs(origin.sasl_handler:mechanisms()) do
180                         features:tag("mechanism"):text(k):up();
181                 end
182                 features:up();
183         else
184                 features:tag("bind", bind_attr):tag("required"):up():up();
185                 features:tag("session", xmpp_session_attr):tag("optional"):up():up();
186         end
187 end);
188
189 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind", function(session, stanza)
190         log("debug", "Client requesting a resource bind");
191         local resource;
192         if stanza.attr.type == "set" then
193                 local bind = stanza.tags[1];
194                 if bind and bind.attr.xmlns == xmlns_bind then
195                         resource = bind:child_with_name("resource");
196                         if resource then
197                                 resource = resource[1];
198                         end
199                 end
200         end
201         local success, err_type, err, err_msg = sm_bind_resource(session, resource);
202         if not success then
203                 session.send(st.error_reply(stanza, err_type, err, err_msg));
204         else
205                 session.send(st.reply(stanza)
206                         :tag("bind", { xmlns = xmlns_bind})
207                         :tag("jid"):text(session.full_jid));
208         end
209 end);
210
211 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session", function(session, stanza)
212         log("debug", "Client requesting a session");
213         session.send(st.reply(stanza));
214 end);