Enable restriction of supported mechanisms in the SASL library.
[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 set = require "util.set";
20 local array = require "util.array";
21 local pairs, ipairs = pairs, ipairs;
22 local t_insert, t_concat = table.insert, table.concat;
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 local dofile = dofile;
34 local require = require;
35
36 require "util.iterators"
37 local keys = keys
38
39 local array = require "util.array"
40 module "sasl"
41
42 --[[
43 Authentication Backend Prototypes:
44
45 state = false : disabled
46 state = true : enabled
47 state = nil : non-existant
48
49 plain:
50         function(username, realm)
51                 return password, state;
52         end
53
54 plain-test:
55         function(username, realm, password)
56                 return true or false, state;
57         end
58
59 digest-md5:
60         function(username, domain, realm, encoding) -- domain and realm are usually the same; for some broken
61                                                                                                 -- implementations it's not
62                 return digesthash, state;
63         end
64
65 digest-md5-test:
66         function(username, domain, realm, encoding, digesthash)
67                 return true or false, state;
68         end
69 ]]
70
71 local method = {};
72 method.__index = method;
73 local mechanisms = {};
74 local backend_mechanism = {};
75
76 -- register a new SASL mechanims
77 local function registerMechanism(name, backends, f)
78         assert(type(name) == "string", "Parameter name MUST be a string.");
79         assert(type(backends) == "string" or type(backends) == "table", "Parameter backends MUST be either a string or a table.");
80         assert(type(f) == "function", "Parameter f MUST be a function.");
81         mechanisms[name] = f
82         for _, backend_name in ipairs(backends) do
83                 if backend_mechanism[backend_name] == nil then backend_mechanism[backend_name] = {}; end
84                 t_insert(backend_mechanism[backend_name], name);
85         end
86 end
87
88 -- create a new SASL object which can be used to authenticate clients
89 function new(realm, profile, forbidden)
90         sasl_i = {profile = profile};
91         sasl_i.realm = realm;
92         s = setmetatable(sasl_i, method);
93         s:forbidden(sasl_i, forbidden)
94         return s;
95 end
96
97 -- set the forbidden mechanisms
98 function method:forbidden( forbidden )
99         if forbidden then
100                 -- set forbidden
101                 self.forbidden = set.new(forbidden);
102         else
103                 -- get forbidden
104                 return array.collect(self.forbidden:items());
105         end
106 end
107
108 -- get a list of possible SASL mechanims to use
109 function method:mechanisms()
110         local mechanisms = {}
111         for backend, f in pairs(self.profile) do
112                 if backend_mechanism[backend] then
113                         for _, mechanism in ipairs(backend_mechanism[backend]) do
114                                 if not sasl_i.forbidden:contains(mechanism) then
115                                         mechanisms[mechanism] = true;
116                                 end
117                         end
118                 end
119         end
120         self["possible_mechanisms"] = mechanisms;
121         return array.collect(keys(mechanisms));
122 end
123
124 -- select a mechanism to use
125 function method:select(mechanism)
126         if self.mech_i then
127                 return false;
128         end
129         
130         self.mech_i = mechanisms[mechanism]
131         if self.mech_i == nil then 
132                 return false;
133         end
134         return true;
135 end
136
137 -- feed new messages to process into the library
138 function method:process(message)
139         --if message == "" or message == nil then return "failure", "malformed-request" end
140         return self.mech_i(self, message);
141 end
142
143 -- load the mechanisms
144 load_mechs = {"plain", "digest-md5", "anonymous", "scram"}
145 for _, mech in ipairs(load_mechs) do
146         local name = "util.sasl."..mech;
147         local m = require(name);
148         m.init(registerMechanism)
149 end
150
151 return _M;