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