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