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