Merge 0.7->trunk
[prosody.git] / util / sasl / plain.lua
1 -- sasl.lua v0.4
2 -- Copyright (C) 2008-2010 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
23 --[[
24 Supported Authentication Backends
25
26 plain:
27         function(username, realm)
28                 return password, state;
29         end
30
31 plain-test:
32         function(username, realm, password)
33                 return true or false, state;
34         end
35         
36 plain-hashed:
37         function(username, realm)
38                 return hashed_password, hash_function, state;
39         end
40 ]]
41
42 local function plain(self, message)
43         if not message then
44                 return "failure", "malformed-request";
45         end
46
47         local authorization, authentication, password = s_match(message, "^([^%z]*)%z([^%z]+)%z([^%z]+)");
48
49         if not authorization then
50                 return "failure", "malformed-request";
51         end
52
53         -- SASLprep password and authentication
54         authentication = saslprep(authentication);
55         password = saslprep(password);
56
57         if (not password) or (password == "") or (not authentication) or (authentication == "") then
58                 log("debug", "Username or password violates SASLprep.");
59                 return "failure", "malformed-request", "Invalid username or password.";
60         end
61
62         local correct, state = false, false;
63         if self.profile.plain then
64                 local correct_password;
65                 correct_password, state = self.profile.plain(authentication, self.realm);
66                 if correct_password == password then correct = true; else correct = false; end
67         elseif self.profile.plain_test then
68                 correct, state = self.profile.plain_test(authentication, self.realm, password);
69         elseif self.profile.plain_hashed then
70                 local hashed_password, hash_f;
71                 hashed_password, hash_f, state = self.profile.plain_hashed(authentication, self.realm);
72                 if hashed_password == hash_f(password) then correct = true; else correct = false; end
73         end
74
75         self.username = authentication
76         if not state then
77                 return "failure", "account-disabled";
78         end
79
80         if correct then
81                 return "success";
82         else
83                 return "failure", "not-authorized", "Unable to authorize you with the authentication credentials you've sent.";
84         end
85 end
86
87 function init(registerMechanism)
88         registerMechanism("PLAIN", {"plain", "plain_test", "plain_hashed"}, plain);
89 end
90
91 return _M;