Merge 0.9->0.10
[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 Channel Binding:
32
33 To enable support of channel binding in some mechanisms you need to provide appropriate callbacks in a table
34 at profile.cb.
35
36 Example:
37         profile.cb["tls-unique"] = function(self)
38                 return self.user
39         end
40
41 ]]
42
43 local method = {};
44 method.__index = method;
45 local mechanisms = {};
46 local backend_mechanism = {};
47 local mechanism_channelbindings = {};
48
49 -- register a new SASL mechanims
50 function registerMechanism(name, backends, f, cb_backends)
51         assert(type(name) == "string", "Parameter name MUST be a string.");
52         assert(type(backends) == "string" or type(backends) == "table", "Parameter backends MUST be either a string or a table.");
53         assert(type(f) == "function", "Parameter f MUST be a function.");
54         if cb_backends then assert(type(cb_backends) == "table"); end
55         mechanisms[name] = f
56         if cb_backends then
57                 mechanism_channelbindings[name] = {};
58                 for _, cb_name in ipairs(cb_backends) do
59                         mechanism_channelbindings[name][cb_name] = true;
60                 end
61         end
62         for _, backend_name in ipairs(backends) do
63                 if backend_mechanism[backend_name] == nil then backend_mechanism[backend_name] = {}; end
64                 t_insert(backend_mechanism[backend_name], name);
65         end
66 end
67
68 -- create a new SASL object which can be used to authenticate clients
69 function new(realm, profile)
70         local mechanisms = profile.mechanisms;
71         if not mechanisms then
72                 mechanisms = {};
73                 for backend, f in pairs(profile) do
74                         if backend_mechanism[backend] then
75                                 for _, mechanism in ipairs(backend_mechanism[backend]) do
76                                         mechanisms[mechanism] = true;
77                                 end
78                         end
79                 end
80                 profile.mechanisms = mechanisms;
81         end
82         return setmetatable({ profile = profile, realm = realm, mechs = mechanisms }, method);
83 end
84
85 -- add a channel binding handler
86 function method:add_cb_handler(name, f)
87         if type(self.profile.cb) ~= "table" then
88                 self.profile.cb = {};
89         end
90         self.profile.cb[name] = f;
91         return self;
92 end
93
94 -- get a fresh clone with the same realm and profile
95 function method:clean_clone()
96         return new(self.realm, self.profile)
97 end
98
99 -- get a list of possible SASL mechanims to use
100 function method:mechanisms()
101         local current_mechs = {};
102         for mech, _ in pairs(self.mechs) do
103                 if mechanism_channelbindings[mech] and self.profile.cb then
104                         local ok = false;
105                         for cb_name, _ in pairs(self.profile.cb) do
106                                 if mechanism_channelbindings[mech][cb_name] then
107                                         ok = true;
108                                 end
109                         end
110                         if ok == true then current_mechs[mech] = true; end
111                 else
112                         current_mechs[mech] = true;
113                 end
114         end
115         return current_mechs;
116 end
117
118 -- select a mechanism to use
119 function method:select(mechanism)
120         if not self.selected and self.mechs[mechanism] then
121                 self.selected = mechanism;
122                 return true;
123         end
124 end
125
126 -- feed new messages to process into the library
127 function method:process(message)
128         --if message == "" or message == nil then return "failure", "malformed-request" end
129         return mechanisms[self.selected](self, message);
130 end
131
132 -- load the mechanisms
133 require "util.sasl.plain"     .init(registerMechanism);
134 require "util.sasl.digest-md5".init(registerMechanism);
135 require "util.sasl.anonymous" .init(registerMechanism);
136 require "util.sasl.scram"     .init(registerMechanism);
137 require "util.sasl.external"  .init(registerMechanism);
138
139 return _M;