65053ffabd51eabae234b5d9bb3208469cea96f3
[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 t_concat, t_insert = table.concat, table.insert;
19 local tostring = tostring;
20
21 local secure_auth_only = module:get_option("c2s_require_encryption") or module:get_option("require_encryption");
22 local anonymous_login = module:get_option("anonymous_login");
23 local allow_unencrypted_plain_auth = module:get_option("allow_unencrypted_plain_auth")
24
25 local log = module._log;
26
27 local xmlns_sasl ='urn:ietf:params:xml:ns:xmpp-sasl';
28 local xmlns_bind ='urn:ietf:params:xml:ns:xmpp-bind';
29 local xmlns_stanzas ='urn:ietf:params:xml:ns:xmpp-stanzas';
30
31 local new_sasl = require "util.sasl".new;
32
33 local anonymous_authentication_profile = {
34         anonymous = function(sasl, username, realm)
35                 return true; -- for normal usage you should always return true here
36         end
37 };
38
39 local function build_reply(status, ret, err_msg)
40         local reply = st.stanza(status, {xmlns = xmlns_sasl});
41         if status == "challenge" then
42                 --log("debug", "CHALLENGE: %s", ret or "");
43                 reply:text(base64.encode(ret or ""));
44         elseif status == "failure" then
45                 reply:tag(ret):up();
46                 if err_msg then reply:tag("text"):text(err_msg); end
47         elseif status == "success" then
48                 --log("debug", "SUCCESS: %s", ret or "");
49                 reply:text(base64.encode(ret or ""));
50         else
51                 module:log("error", "Unknown sasl status: %s", status);
52         end
53         return reply;
54 end
55
56 local function handle_status(session, status, ret, err_msg)
57         if status == "failure" then
58                 session.sasl_handler = session.sasl_handler:clean_clone();
59         elseif status == "success" then
60                 local username = nodeprep(session.sasl_handler.username);
61
62                 local ok, err = sm_make_authenticated(session, session.sasl_handler.username);
63                 if ok then
64                         session.sasl_handler = nil;
65                         session:reset_stream();
66                 else
67                         module:log("warn", "SASL succeeded but username was invalid");
68                         session.sasl_handler = session.sasl_handler:clean_clone();
69                         return "failure", "not-authorized", "User authenticated successfully, but username was invalid";
70                 end
71         end
72         return status, ret, err_msg;
73 end
74
75 local function sasl_process_cdata(session, stanza)
76         local text = stanza[1];
77         if text then
78                 text = base64.decode(text);
79                 --log("debug", "AUTH: %s", text:gsub("[%z\001-\008\011\012\014-\031]", " "));
80                 if not text then
81                         session.sasl_handler = nil;
82                         session.send(build_reply("failure", "incorrect-encoding"));
83                         return true;
84                 end
85         end
86         local status, ret, err_msg = session.sasl_handler:process(text);
87         status, ret, err_msg = handle_status(session, status, ret, err_msg);
88         local s = build_reply(status, ret, err_msg);
89         log("debug", "sasl reply: %s", tostring(s));
90         session.send(s);
91         return true;
92 end
93
94 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-sasl:auth", function(event)
95         local session, stanza = event.origin, event.stanza;
96         if session.type ~= "c2s_unauthed" then return; end
97
98         if session.sasl_handler and session.sasl_handler.selected then
99                 session.sasl_handler = nil; -- allow starting a new SASL negotiation before completing an old one
100         end
101         if not session.sasl_handler then
102                 if anonymous_login then
103                         session.sasl_handler = new_sasl(module.host, anonymous_authentication_profile);
104                 else
105                         session.sasl_handler = usermanager_get_sasl_handler(module.host);
106                 end
107         end
108         local mechanism = stanza.attr.mechanism;
109         if anonymous_login then
110                 if mechanism ~= "ANONYMOUS" then
111                         session.send(build_reply("failure", "invalid-mechanism"));
112                         return true;
113                 end
114         elseif mechanism == "ANONYMOUS" then
115                 session.send(build_reply("failure", "mechanism-too-weak"));
116                 return true;
117         end
118         if not session.secure and (secure_auth_only or (mechanism == "PLAIN" and not allow_unencrypted_plain_auth)) then
119                 session.send(build_reply("failure", "encryption-required"));
120                 return true;
121         end
122         local valid_mechanism = session.sasl_handler:select(mechanism);
123         if not valid_mechanism then
124                 session.send(build_reply("failure", "invalid-mechanism"));
125                 return true;
126         end
127         return sasl_process_cdata(session, stanza);
128 end);
129 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-sasl:response", function(event)
130         local session = event.origin;
131         if not(session.sasl_handler and session.sasl_handler.selected) then
132                 session.send(build_reply("failure", "not-authorized", "Out of order SASL element"));
133                 return true;
134         end
135         return sasl_process_cdata(session, event.stanza);
136 end);
137 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-sasl:abort", function(event)
138         local session = event.origin;
139         session.sasl_handler = nil;
140         session.send(build_reply("failure", "aborted"));
141         return true;
142 end);
143
144 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
145 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
146 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
147 module:hook("stream-features", function(event)
148         local origin, features = event.origin, event.features;
149         if not origin.username then
150                 if secure_auth_only and not origin.secure then
151                         return;
152                 end
153                 if anonymous_login then
154                         origin.sasl_handler = new_sasl(module.host, anonymous_authentication_profile);
155                 else
156                         origin.sasl_handler = usermanager_get_sasl_handler(module.host);
157                 end
158                 features:tag("mechanisms", mechanisms_attr);
159                 for mechanism in pairs(origin.sasl_handler:mechanisms()) do
160                         if mechanism ~= "PLAIN" or origin.secure or allow_unencrypted_plain_auth then
161                                 features:tag("mechanism"):text(mechanism):up();
162                         end
163                 end
164                 features:up();
165         else
166                 features:tag("bind", bind_attr):tag("required"):up():up();
167                 features:tag("session", xmpp_session_attr):tag("optional"):up():up();
168         end
169 end);
170
171 module:hook("iq/self/urn:ietf:params:xml:ns:xmpp-bind:bind", function(event)
172         local origin, stanza = event.origin, event.stanza;
173         local resource;
174         if stanza.attr.type == "set" then
175                 local bind = stanza.tags[1];
176                 resource = bind:child_with_name("resource");
177                 resource = resource and #resource.tags == 0 and resource[1] or nil;
178         end
179         local success, err_type, err, err_msg = sm_bind_resource(origin, resource);
180         if success then
181                 origin.send(st.reply(stanza)
182                         :tag("bind", { xmlns = xmlns_bind })
183                         :tag("jid"):text(origin.full_jid));
184                 origin.log("debug", "Resource bound: %s", origin.full_jid);
185         else
186                 origin.send(st.error_reply(stanza, err_type, err, err_msg));
187                 origin.log("debug", "Resource bind failed: %s", err_msg or err);
188         end
189         return true;
190 end);
191
192 local function handle_legacy_session(event)
193         event.origin.send(st.reply(event.stanza));
194         return true;
195 end
196
197 module:hook("iq/self/urn:ietf:params:xml:ns:xmpp-session:session", handle_legacy_session);
198 module:hook("iq/host/urn:ietf:params:xml:ns:xmpp-session:session", handle_legacy_session);