Adjust SASL PLAIN mechanism to the new API.
[prosody.git] / util / sasl.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
15 local md5 = require "util.hashes".md5;
16 local log = require "util.logger".init("sasl");
17 local tostring = tostring;
18 local st = require "util.stanza";
19 local generate_uuid = require "util.uuid".generate;
20 local pairs, ipairs = pairs, ipairs;
21 local t_insert, t_concat = table.insert, table.concat;
22 local to_byte, to_char = string.byte, string.char;
23 local to_unicode = require "util.encodings".idna.to_unicode;
24 local s_match = string.match;
25 local gmatch = string.gmatch
26 local string = string
27 local math = require "math"
28 local type = type
29 local error = error
30 local print = print
31 local setmetatable = setmetatable;
32 local assert = assert;
33
34 require "util.iterators"
35 local keys = keys
36
37 local array = require "util.array"
38 module "sasl"
39
40 --[[
41 Authentication Backend Prototypes:
42
43 plain:
44         function(username, realm)
45                 return password, state;
46         end
47
48 plain-test:
49         function(username, realm, password)
50                 return true or false, state;
51         end
52
53 digest-md5:
54         function(username, realm, encoding)
55                 return digesthash, state;
56         end
57
58 digest-md5-test:
59         function(username, realm, encoding, digesthash)
60                 return true or false, state;
61         end
62 ]]
63
64 local method = {};
65 method.__index = method;
66 local mechanisms = {};
67 local backend_mechanism = {};
68
69 -- register a new SASL mechanims
70 local function registerMechanism(name, backends, f)
71         assert(type(name) == "string", "Parameter name MUST be a string.");
72         assert(type(backends) == "string" or type(backends) == "table", "Parameter backends MUST be either a string or a table.");
73         assert(type(f) == "function", "Parameter f MUST be a function.");
74         mechanisms[name] = f
75         for _, backend_name in ipairs(backends) do
76                 if backend_mechanism[backend_name] == nil then backend_mechanism[backend_name] = {}; end
77                 t_insert(backend_mechanism[backend_name], name);
78         end
79 end
80
81 -- create a new SASL object which can be used to authenticate clients
82 function new(realm, profile)
83         sasl_i = {profile = profile};
84         return setmetatable(sasl_i, method);
85 end
86
87 -- get a list of possible SASL mechanims to use
88 function method:mechanisms()
89         local mechanisms = {}
90         for backend, f in pairs(self.profile) do
91                 print(backend)
92                 if backend_mechanism[backend] then
93                         for _, mechanism in ipairs(backend_mechanism[backend]) do
94                                 mechanisms[mechanism] = true;
95                                 end
96                 end
97         end
98         self["possible_mechanisms"] = mechanisms;
99         return array.collect(keys(mechanisms));
100 end
101
102 -- select a mechanism to use
103 function method:select(mechanism)
104         self.mech_i = mechanisms[mechanism]
105         if self.mech_i == nil then return false; end
106         return true;
107 end
108
109 -- feed new messages to process into the library
110 function method:process(message)
111         if message == "" or message == nil then return "failure", "malformed-request" end
112         return self.mech_i(self, message);
113 end
114
115 --=========================
116 --SASL PLAIN
117 local function sasl_mechanism_plain(self, message)
118         local response = message
119         local authorization = s_match(response, "([^&%z]+)")
120         local authentication = s_match(response, "%z([^&%z]+)%z")
121         local password = s_match(response, "%z[^&%z]+%z([^&%z]+)")
122
123         if authentication == nil or password == nil then return "failure", "malformed-request" end
124
125         local correct, state = false, false, false;
126         if self.profile.plain then
127                 local correct_password, state = self.profile.plain(authentication, self.realm);
128                 if correct_password == password then correct = true; else correct = false; end
129         else if self.profile.plain_test then
130                 correct, state = self.profile.plain_test(authentication, self.realm, password);
131         end
132
133         self.username = authentication
134         if not state then
135                 return "failure", "account-disabled";
136         end
137
138         if correct then
139                 return "success";
140         else
141                 return "failure", "not-authorized";
142         end
143 end
144 registerMechanism("PLAIN", {"plain", "plain_test"}, sasl_mechanism_plain);
145
146 return _M;