Add support for plain profile in digest-md5 implementation.
[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 pairs, ipairs = pairs, ipairs;
20 local t_insert, t_concat = table.insert, table.concat;
21 local to_unicode = require "util.encodings".idna.to_unicode;
22 local s_match = string.match;
23 local gmatch = string.gmatch
24 local string = string
25 local math = require "math"
26 local type = type
27 local error = error
28 local print = print
29 local setmetatable = setmetatable;
30 local assert = assert;
31 local dofile = dofile;
32 local require = require;
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 state = false : disabled
44 state = true : enabled
45 state = nil : non-existant
46
47 plain:
48         function(username, realm)
49                 return password, state;
50         end
51
52 plain-test:
53         function(username, realm, password)
54                 return true or false, state;
55         end
56
57 digest-md5:
58         function(username, realm, encoding)
59                 return digesthash, state;
60         end
61
62 digest-md5-test:
63         function(username, realm, encoding, digesthash)
64                 return true or false, state;
65         end
66 ]]
67
68 local method = {};
69 method.__index = method;
70 local mechanisms = {};
71 local backend_mechanism = {};
72
73 -- register a new SASL mechanims
74 local function registerMechanism(name, backends, f)
75         assert(type(name) == "string", "Parameter name MUST be a string.");
76         assert(type(backends) == "string" or type(backends) == "table", "Parameter backends MUST be either a string or a table.");
77         assert(type(f) == "function", "Parameter f MUST be a function.");
78         mechanisms[name] = f
79         for _, backend_name in ipairs(backends) do
80                 if backend_mechanism[backend_name] == nil then backend_mechanism[backend_name] = {}; end
81                 t_insert(backend_mechanism[backend_name], name);
82         end
83 end
84
85 -- create a new SASL object which can be used to authenticate clients
86 function new(realm, profile)
87         sasl_i = {profile = profile};
88         sasl_i.realm = realm;
89         return setmetatable(sasl_i, method);
90 end
91
92 -- get a list of possible SASL mechanims to use
93 function method:mechanisms()
94         local mechanisms = {}
95         for backend, f in pairs(self.profile) do
96                 print(backend)
97                 if backend_mechanism[backend] then
98                         for _, mechanism in ipairs(backend_mechanism[backend]) do
99                                 mechanisms[mechanism] = true;
100                         end
101                 end
102         end
103         self["possible_mechanisms"] = mechanisms;
104         return array.collect(keys(mechanisms));
105 end
106
107 -- select a mechanism to use
108 function method:select(mechanism)
109         if self.mech_i then
110                 return false;
111         end
112         
113         self.mech_i = mechanisms[mechanism]
114         if self.mech_i == nil then 
115                 return false;
116         end
117         return true;
118 end
119
120 -- feed new messages to process into the library
121 function method:process(message)
122         --if message == "" or message == nil then return "failure", "malformed-request" end
123         return self.mech_i(self, message);
124 end
125
126 -- load the mechanisms
127 load_mechs = {"plain", "digest-md5"}
128 for _, mech in ipairs(load_mechs) do
129         local name = "util.sasl."..mech;
130         local m = require(name);
131         m.init(registerMechanism)
132 end
133
134 return _M;