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