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