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