mod_legacyauth: Don't allow server-generated resource identifiers, as these are not...
[prosody.git] / plugins / mod_legacyauth.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 t_concat = table.concat;
13
14 local config = require "core.configmanager";
15 local secure_auth_only = config.get(module:get_host(), "core", "require_encryption");
16
17 local sessionmanager = require "core.sessionmanager";
18 local usermanager = require "core.usermanager";
19 local nodeprep = require "util.encodings".stringprep.nodeprep;
20 local resourceprep = require "util.encodings".stringprep.resourceprep;
21
22 module:add_feature("jabber:iq:auth");
23 module:add_event_hook("stream-features", function (session, features)
24         if secure_auth_only and not session.secure then
25                 -- Sorry, not offering to insecure streams!
26                 return;
27         elseif not session.username then
28                 features:tag("auth", {xmlns='http://jabber.org/features/iq-auth'}):up();
29         end
30 end);
31
32 module:add_iq_handler("c2s_unauthed", "jabber:iq:auth", 
33                 function (session, stanza)
34                         if secure_auth_only and not session.secure then
35                                 session.send(st.error_reply(stanza, "modify", "not-acceptable", "Encryption (SSL or TLS) is required to connect to this server"));
36                                 return true;
37                         end
38                         
39                         local username = stanza.tags[1]:child_with_name("username");
40                         local password = stanza.tags[1]:child_with_name("password");
41                         local resource = stanza.tags[1]:child_with_name("resource");
42                         if not (username and password and resource) then
43                                 local reply = st.reply(stanza);
44                                 session.send(reply:query("jabber:iq:auth")
45                                         :tag("username"):up()
46                                         :tag("password"):up()
47                                         :tag("resource"):up());
48                                 return true;                    
49                         else
50                                 username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
51                                 username = nodeprep(username);
52                                 resource = resourceprep(resource)
53                                 local reply = st.reply(stanza);
54                                 require "core.usermanager"
55                                 if username and resource and usermanager.validate_credentials(session.host, username, password) then
56                                         -- Authentication successful!
57                                         local success, err = sessionmanager.make_authenticated(session, username);
58                                         if success then
59                                                 local err_type, err_msg;
60                                                 success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
61                                                 if not success then
62                                                         session.send(st.error_reply(stanza, err_type, err, err_msg));
63                                                         session.username, session.type = nil, "c2s_unauthed"; -- FIXME should this be placed in sessionmanager?
64                                                         return true;
65                                                 elseif resource ~= session.resource then -- server changed resource, not supported by legacy auth
66                                                         session.send(st.error_reply(stanza, "cancel", "conflict", "The requested resource could not be assigned to this session."));
67                                                         session:close(); -- FIXME undo resource bind and auth instead of closing the session?
68                                                         return true;
69                                                 end
70                                         end
71                                         session.send(st.reply(stanza));
72                                         return true;
73                                 else
74                                         local reply = st.reply(stanza);
75                                         reply.attr.type = "error";
76                                         reply:tag("error", { code = "401", type = "auth" })
77                                                 :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
78                                         session.send(reply);
79                                         return true;
80                                 end
81                         end
82                         
83                 end);