Merge 0.10->trunk
[prosody.git] / plugins / mod_legacyauth.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 t_concat = table.concat;
13
14 local secure_auth_only = module:get_option("c2s_require_encryption",
15         module:get_option("require_encryption"))
16         or not(module:get_option("allow_unencrypted_plain_auth"));
17
18 local sessionmanager = require "core.sessionmanager";
19 local usermanager = require "core.usermanager";
20 local nodeprep = require "util.encodings".stringprep.nodeprep;
21 local resourceprep = require "util.encodings".stringprep.resourceprep;
22
23 module:add_feature("jabber:iq:auth");
24 module:hook("stream-features", function(event)
25         local origin, features = event.origin, event.features;
26         if secure_auth_only and not origin.secure then
27                 -- Sorry, not offering to insecure streams!
28                 return;
29         elseif not origin.username then
30                 features:tag("auth", {xmlns='http://jabber.org/features/iq-auth'}):up();
31         end
32 end);
33
34 module:hook("stanza/iq/jabber:iq:auth:query", function(event)
35         local session, stanza = event.origin, event.stanza;
36
37         if session.type ~= "c2s_unauthed" then
38                 (session.sends2s or session.send)(st.error_reply(stanza, "cancel", "service-unavailable", "Legacy authentication is only allowed for unauthenticated client connections."));
39                 return true;
40         end
41
42         if secure_auth_only and not session.secure then
43                 session.send(st.error_reply(stanza, "modify", "not-acceptable", "Encryption (SSL or TLS) is required to connect to this server"));
44                 return true;
45         end
46
47         local query = stanza.tags[1];
48         local username = query:get_child("username");
49         local password = query:get_child("password");
50         local resource = query:get_child("resource");
51         if not (username and password and resource) then
52                 local reply = st.reply(stanza);
53                 session.send(reply:query("jabber:iq:auth")
54                         :tag("username"):up()
55                         :tag("password"):up()
56                         :tag("resource"):up());
57         else
58                 username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
59                 username = nodeprep(username);
60                 resource = resourceprep(resource)
61                 if not (username and resource) then
62                         session.send(st.error_reply(stanza, "modify", "bad-request"));
63                         return true;
64                 end
65                 if usermanager.test_password(username, session.host, password) then
66                         -- Authentication successful!
67                         local success, err = sessionmanager.make_authenticated(session, username);
68                         if success then
69                                 local err_type, err_msg;
70                                 success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
71                                 if not success then
72                                         session.send(st.error_reply(stanza, err_type, err, err_msg));
73                                         session.username, session.type = nil, "c2s_unauthed"; -- FIXME should this be placed in sessionmanager?
74                                         return true;
75                                 elseif resource ~= session.resource then -- server changed resource, not supported by legacy auth
76                                         session.send(st.error_reply(stanza, "cancel", "conflict", "The requested resource could not be assigned to this session."));
77                                         session:close(); -- FIXME undo resource bind and auth instead of closing the session?
78                                         return true;
79                                 end
80                         end
81                         session.send(st.reply(stanza));
82                 else
83                         session.send(st.error_reply(stanza, "auth", "not-authorized"));
84                 end
85         end
86         return true;
87 end);