2 -- Copyright (C) 2008-2009 Tobias Markmann
4 -- All rights reserved.
6 -- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
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.
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.
14 local cyrussasl = require "cyrussasl";
15 local log = require "util.logger".init("sasl_cyrus");
16 local array = require "util.array";
18 local tostring = tostring;
19 local pairs, ipairs = pairs, ipairs;
20 local t_insert, t_concat = table.insert, table.concat;
21 local s_match = string.match;
22 local setmetatable = setmetatable
28 local s_match, s_gmatch = string.match, string.gmatch
33 method.__index = method;
34 local initialized = false;
36 local function init(service_name)
37 if not initialized then
38 local st, errmsg = pcall(cyrussasl.server_init, service_name);
42 log("error", "Failed to initialize CyrusSASL: %s", errmsg);
47 -- create a new SASL object which can be used to authenticate clients
48 function new(realm, service_name)
54 sasl_i.service_name = service_name;
55 sasl_i.cyrus = cyrussasl.server_new(service_name, nil, nil, nil, nil)
56 if sasl_i.cyrus == 0 then
57 log("error", "got NULL return value from server_new")
60 cyrussasl.setssf(sasl_i.cyrus, 0, 0xffffffff)
61 local s = setmetatable(sasl_i, method);
65 -- get a fresh clone with the same realm, profiles and forbidden mechanisms
66 function method:clean_clone()
67 return new(self.realm, self.service_name)
70 -- set the forbidden mechanisms
71 function method:forbidden( restrict )
72 log("debug", "Called method:forbidden. NOT IMPLEMENTED.")
76 -- get a list of possible SASL mechanims to use
77 function method:mechanisms()
79 local cyrus_mechs = cyrussasl.listmech(self.cyrus, nil, "", " ", "")
80 for w in s_gmatch(cyrus_mechs, "[^ ]+") do
83 self.mechs = mechanisms
84 return array.collect(keys(mechanisms));
87 -- select a mechanism to use
88 function method:select(mechanism)
89 self.mechanism = mechanism;
90 return self.mechs[mechanism];
93 -- feed new messages to process into the library
94 function method:process(message)
98 if self.mechanism then
99 err, data = cyrussasl.server_start(self.cyrus, self.mechanism, message or "")
101 err, data = cyrussasl.server_step(self.cyrus, message or "")
104 self.username = cyrussasl.get_username(self.cyrus)
106 if (err == 0) then -- SASL_OK
107 return "success", data
108 elseif (err == 1) then -- SASL_CONTINUE
109 return "challenge", data
110 elseif (err == -4) then -- SASL_NOMECH
111 log("debug", "SASL mechanism not available from remote end")
113 "undefined-condition",
114 "SASL mechanism not available"
115 elseif (err == -13) then -- SASL_BADAUTH
116 return "failure", "not-authorized"
118 log("debug", "Got SASL error condition %d", err)
120 "undefined-condition",
121 cyrussasl.get_message( self.cyrus )