util.sasl.plain: Fail gracefully on empty <auth/> tag
[prosody.git] / util / sasl / plain.lua
1 -- sasl.lua v0.4
2 -- Copyright (C) 2008-2009 Tobias Markmann
3 --
4 --    All rights reserved.
5 --
6 --    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 --
8 --        * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 --        * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 --        * Neither the name of Tobias Markmann nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11 --
12 --    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13
14 local s_match = string.match;
15 local saslprep = require "util.encodings".stringprep.saslprep;
16 local log = require "util.logger".init("sasl");
17
18 module "plain"
19
20 --=========================
21 --SASL PLAIN according to RFC 4616
22 local function plain(self, message)
23         local response = message
24         
25         local authorization, authentication, password;
26         if response then
27                 authorization = s_match(response, "([^%z]+)")
28                 authentication = s_match(response, "%z([^%z]+)%z")
29                 password = s_match(response, "%z[^%z]+%z([^%z]+)")
30         end
31         
32         if authentication == nil or password == nil then
33                 return "failure", "malformed-request";
34         end
35         
36         -- SASLprep password and authentication
37         authentication = saslprep(authentication);
38         password = saslprep(password);
39         
40         if (not password) or (password == "") or (not authentication) or (authentication == "") then
41                 log("debug", "Username or password violates SASLprep.");
42                 return "failure", "malformed-request", "Invalid username or password.";
43         end
44
45         local correct, state = false, false;
46         if self.profile.plain then
47                 local correct_password;
48                 correct_password, state = self.profile.plain(authentication, self.realm);
49                 if correct_password == password then correct = true; else correct = false; end
50         elseif self.profile.plain_test then
51                 correct, state = self.profile.plain_test(authentication, self.realm, password);
52         end
53
54         self.username = authentication
55         if not state then
56                 return "failure", "account-disabled";
57         end
58
59         if correct then
60                 return "success";
61         else
62                 return "failure", "not-authorized", "Unable to authorize you with the authentication credentials you've sent.";
63         end
64 end
65
66 function init(registerMechanism)
67         registerMechanism("PLAIN", {"plain", "plain_test"}, plain);
68 end
69
70 return _M;