732d5d91cd1453cb4f9eb330a8e709cd264e5cd9
[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(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_handler(session, stanza)
76         if stanza.name == "auth" then
77                 -- FIXME ignoring duplicates because ejabberd does
78                 local mechanism = stanza.attr.mechanism;
79                 if anonymous_login then
80                         if mechanism ~= "ANONYMOUS" then
81                                 return session.send(build_reply("failure", "invalid-mechanism"));
82                         end
83                 elseif mechanism == "ANONYMOUS" then
84                         return session.send(build_reply("failure", "mechanism-too-weak"));
85                 end
86                 if not session.secure and (secure_auth_only or (mechanism == "PLAIN" and not allow_unencrypted_plain_auth)) then
87                         return session.send(build_reply("failure", "encryption-required"));
88                 end
89                 local valid_mechanism = session.sasl_handler:select(mechanism);
90                 if not valid_mechanism then
91                         return session.send(build_reply("failure", "invalid-mechanism"));
92                 end
93         elseif not session.sasl_handler then
94                 return; -- FIXME ignoring out of order stanzas because ejabberd does
95         end
96         local text = stanza[1];
97         if text then
98                 text = base64.decode(text);
99                 --log("debug", "AUTH: %s", text:gsub("[%z\001-\008\011\012\014-\031]", " "));
100                 if not text then
101                         session.sasl_handler = nil;
102                         session.send(build_reply("failure", "incorrect-encoding"));
103                         return;
104                 end
105         end
106         local status, ret, err_msg = session.sasl_handler:process(text);
107         status, ret, err_msg = handle_status(session, status, ret, err_msg);
108         local s = build_reply(status, ret, err_msg);
109         log("debug", "sasl reply: %s", tostring(s));
110         session.send(s);
111 end
112
113 module:add_handler("c2s_unauthed", "auth", xmlns_sasl, sasl_handler);
114 module:add_handler("c2s_unauthed", "abort", xmlns_sasl, sasl_handler);
115 module:add_handler("c2s_unauthed", "response", xmlns_sasl, sasl_handler);
116
117 local mechanisms_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-sasl' };
118 local bind_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-bind' };
119 local xmpp_session_attr = { xmlns='urn:ietf:params:xml:ns:xmpp-session' };
120 module:hook("stream-features", function(event)
121         local origin, features = event.origin, event.features;
122         if not origin.username then
123                 if secure_auth_only and not origin.secure then
124                         return;
125                 end
126                 if anonymous_login then
127                         origin.sasl_handler = new_sasl(module.host, anonymous_authentication_profile);
128                 else
129                         origin.sasl_handler = usermanager_get_sasl_handler(module.host);
130                 end
131                 features:tag("mechanisms", mechanisms_attr);
132                 for mechanism in pairs(origin.sasl_handler:mechanisms()) do
133                         if mechanism ~= "PLAIN" or origin.secure or allow_unencrypted_plain_auth then
134                                 features:tag("mechanism"):text(mechanism):up();
135                         end
136                 end
137                 features:up();
138         else
139                 features:tag("bind", bind_attr):tag("required"):up():up();
140                 features:tag("session", xmpp_session_attr):tag("optional"):up():up();
141         end
142 end);
143
144 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind", function(session, stanza)
145         log("debug", "Client requesting a resource bind");
146         local resource;
147         if stanza.attr.type == "set" then
148                 local bind = stanza.tags[1];
149                 if bind and bind.attr.xmlns == xmlns_bind then
150                         resource = bind:child_with_name("resource");
151                         if resource then
152                                 resource = resource[1];
153                         end
154                 end
155         end
156         local success, err_type, err, err_msg = sm_bind_resource(session, resource);
157         if not success then
158                 session.send(st.error_reply(stanza, err_type, err, err_msg));
159         else
160                 session.send(st.reply(stanza)
161                         :tag("bind", { xmlns = xmlns_bind})
162                         :tag("jid"):text(session.full_jid));
163         end
164 end);
165
166 module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session", function(session, stanza)
167         log("debug", "Client requesting a session");
168         session.send(st.reply(stanza));
169 end);