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