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