util.sasl, util.sasl_cyrus: Removed a ton of unused variables.
[prosody.git] / util / sasl.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
15 local pairs, ipairs = pairs, ipairs;
16 local t_insert = table.insert;
17 local type = type
18 local setmetatable = setmetatable;
19 local assert = assert;
20 local require = require;
21
22 module "sasl"
23
24 --[[
25 Authentication Backend Prototypes:
26
27 state = false : disabled
28 state = true : enabled
29 state = nil : non-existant
30 ]]
31
32 local method = {};
33 method.__index = method;
34 local mechanisms = {};
35 local backend_mechanism = {};
36
37 -- register a new SASL mechanims
38 local function registerMechanism(name, backends, f)
39         assert(type(name) == "string", "Parameter name MUST be a string.");
40         assert(type(backends) == "string" or type(backends) == "table", "Parameter backends MUST be either a string or a table.");
41         assert(type(f) == "function", "Parameter f MUST be a function.");
42         mechanisms[name] = f
43         for _, backend_name in ipairs(backends) do
44                 if backend_mechanism[backend_name] == nil then backend_mechanism[backend_name] = {}; end
45                 t_insert(backend_mechanism[backend_name], name);
46         end
47 end
48
49 -- create a new SASL object which can be used to authenticate clients
50 function new(realm, profile)
51         local sasl_i = {profile = profile};
52         sasl_i.realm = realm;
53         return setmetatable(sasl_i, method);
54 end
55
56 -- get a fresh clone with the same realm and profile
57 function method:clean_clone()
58         return new(self.realm, self.profile)
59 end
60
61 -- get a list of possible SASL mechanims to use
62 function method:mechanisms()
63         local mechanisms = self.mechs;
64         if not mechanisms then
65                 mechanisms = {}
66                 for backend, f in pairs(self.profile) do
67                         if backend_mechanism[backend] then
68                                 for _, mechanism in ipairs(backend_mechanism[backend]) do
69                                         mechanisms[mechanism] = true;
70                                 end
71                         end
72                 end
73                 self.mechs = mechanisms;
74         end
75         return mechanisms;
76 end
77
78 -- select a mechanism to use
79 function method:select(mechanism)
80         if self.mech_i then
81                 return false;
82         end
83         
84         self.mech_i = mechanisms[self:mechanisms()[mechanism] and mechanism];
85         return (self.mech_i ~= nil);
86 end
87
88 -- feed new messages to process into the library
89 function method:process(message)
90         --if message == "" or message == nil then return "failure", "malformed-request" end
91         return self.mech_i(self, message);
92 end
93
94 -- load the mechanisms
95 local load_mechs = {"plain", "digest-md5", "anonymous", "scram"}
96 for _, mech in ipairs(load_mechs) do
97         local name = "util.sasl."..mech;
98         local m = require(name);
99         m.init(registerMechanism)
100 end
101
102 return _M;