Add support for plain profile in digest-md5 implementation.
authorTobias Markmann <tm@ayena.de>
Fri, 13 Nov 2009 09:54:17 +0000 (10:54 +0100)
committerTobias Markmann <tm@ayena.de>
Fri, 13 Nov 2009 09:54:17 +0000 (10:54 +0100)
util/sasl.lua
util/sasl/digest-md5.lua

index e9b466ee48f1b8decaaa3f21272ffff0f2c5f424..8fe4727eccf71dd51c20d84080462632870a83f8 100644 (file)
@@ -16,10 +16,8 @@ local md5 = require "util.hashes".md5;
 local log = require "util.logger".init("sasl");
 local tostring = tostring;
 local st = require "util.stanza";
-local generate_uuid = require "util.uuid".generate;
 local pairs, ipairs = pairs, ipairs;
 local t_insert, t_concat = table.insert, table.concat;
-local to_byte, to_char = string.byte, string.char;
 local to_unicode = require "util.encodings".idna.to_unicode;
 local s_match = string.match;
 local gmatch = string.gmatch
@@ -42,6 +40,10 @@ module "sasl"
 --[[
 Authentication Backend Prototypes:
 
+state = false : disabled
+state = true : enabled
+state = nil : non-existant
+
 plain:
        function(username, realm)
                return password, state;
@@ -117,14 +119,16 @@ end
 
 -- feed new messages to process into the library
 function method:process(message)
-       if message == "" or message == nil then return "failure", "malformed-request" end
+       --if message == "" or message == nil then return "failure", "malformed-request" end
        return self.mech_i(self, message);
 end
 
 -- load the mechanisms
-m = require "util.sasl.plain"
-m.init(registerMechanism)
---dofile "util/sasl/digest-md5.lua"
---dofile "util/sasl/scram.lua"
+load_mechs = {"plain", "digest-md5"}
+for _, mech in ipairs(load_mechs) do
+       local name = "util.sasl."..mech;
+       local m = require(name);
+       m.init(registerMechanism)
+end
 
 return _M;
index 8acbce4b4ddd905c06f8bc5f37aeae6f2b8c6962..1ff0f62ba73a72a50ded7876641077ea8e457ab3 100644 (file)
 --
 --    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.
 
+local tostring = tostring;
+local type = type;
+
+local s_gmatch = string.gmatch;
+local s_match = string.match;
+local t_concat = table.concat;
+local t_insert = table.insert;
+local to_byte, to_char = string.byte, string.char;
+
+local md5 = require "util.hashes".md5;
+local log = require "util.logger".init("sasl");
+local generate_uuid = require "util.uuid".generate;
+
+module "plain"
 
 --=========================
 --SASL DIGEST-MD5 according to RFC 2831
-local function sasl_mechanism_digest_md5(self, message)
+local function digest_response()
+       
+       return response, A1, A2
+end
+
+local function digest(self, message)
        --TODO complete support for authzid
 
        local function serialize(message)
@@ -68,7 +87,7 @@ local function sasl_mechanism_digest_md5(self, message)
        end
        local function latin1toutf8(str)
                local p = {};
-               for ch in gmatch(str, ".") do
+               for ch in s_gmatch(str, ".") do
                        ch = to_byte(ch);
                        if (ch < 0x80) then
                                t_insert(p, to_char(ch));
@@ -82,7 +101,7 @@ local function sasl_mechanism_digest_md5(self, message)
        end
        local function parse(data)
                local message = {}
-               for k, v in gmatch(data, [[([%w%-]+)="?([^",]*)"?,?]]) do -- FIXME The hacky regex makes me shudder
+               for k, v in s_gmatch(data, [[([%w%-]+)="?([^",]*)"?,?]]) do -- FIXME The hacky regex makes me shudder
                        message[k] = v;
                end
                return message;
@@ -96,7 +115,7 @@ local function sasl_mechanism_digest_md5(self, message)
 
        self.step = self.step + 1;
        if (self.step == 1) then
-               local challenge = serialize({   nonce = object.nonce,
+               local challenge = serialize({   nonce = self.nonce,
                                                                                qop = "auth",
                                                                                charset = "utf-8",
                                                                                algorithm = "md5-sess",
@@ -150,9 +169,19 @@ local function sasl_mechanism_digest_md5(self, message)
 
                --TODO maybe realm support
                self.username = response["username"];
-               local password_encoding, Y = self.credentials_handler("DIGEST-MD5", response["username"], self.realm, response["realm"], decoder);
-               if Y == nil then return "failure", "not-authorized"
-               elseif Y == false then return "failure", "account-disabled" end
+               if self.profile.plain then
+                       local password, state = self.profile.plain(response["username"], self.realm)
+                       if state == nil then return "failure", "not-authorized"
+                       elseif state == false then return "failure", "account-disabled" end
+                       Y = md5(response["username"]..":"..self.realm..":"..password);
+               elseif self.profile["digest-md5"] then
+                       --local Y, state = self.profile["digest-md5"](response["username"], self.realm, response["charset"])
+               elseif self.profile["digest-md5-test"] then
+               
+               end
+               --local password_encoding, Y = self.credentials_handler("DIGEST-MD5", response["username"], self.realm, response["realm"], decoder);
+               --if Y == nil then return "failure", "not-authorized"
+               --elseif Y == false then return "failure", "account-disabled" end
                local A1 = "";
                if response.authzid then
                        if response.authzid == self.username.."@"..self.realm then
@@ -191,4 +220,10 @@ local function sasl_mechanism_digest_md5(self, message)
                if self.authenticated ~= nil then return "success"
                else return "failure", "malformed-request" end
        end
-end
\ No newline at end of file
+end
+
+function init(registerMechanism)
+       registerMechanism("DIGEST-MD5", {"plain"}, digest);
+end
+
+return _M;
\ No newline at end of file