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");
17 local setmetatable = setmetatable
20 local s_match, s_gmatch = string.match, string.gmatch
22 local sasl_errstring = {
23 -- SASL result codes --
24 [1] = "another step is needed in authentication";
25 [0] = "successful result";
26 [-1] = "generic failure";
27 [-2] = "memory shortage failure";
28 [-3] = "overflowed buffer";
29 [-4] = "mechanism not supported";
30 [-5] = "bad protocol / cancel";
31 [-6] = "can't request info until later in exchange";
32 [-7] = "invalid parameter supplied";
33 [-8] = "transient failure (e.g., weak key)";
34 [-9] = "integrity check failed";
35 [-12] = "SASL library not initialized";
37 -- client only codes --
38 [2] = "needs user interaction";
39 [-10] = "server failed mutual authentication step";
40 [-11] = "mechanism doesn't support requested feature";
42 -- server only codes --
43 [-13] = "authentication failure";
44 [-14] = "authorization failure";
45 [-15] = "mechanism too weak for this user";
46 [-16] = "encryption needed to use mechanism";
47 [-17] = "One time use of a plaintext password will enable requested mechanism for user";
48 [-18] = "passphrase expired, has to be reset";
49 [-19] = "account disabled";
50 [-20] = "user not found";
51 [-23] = "version mismatch with plug-in";
52 [-24] = "remote authentication server unavailable";
53 [-26] = "user exists, but no verifier for user";
55 -- codes for password setting --
56 [-21] = "passphrase locked";
57 [-22] = "requested change was not needed";
58 [-27] = "passphrase is too weak for security policy";
59 [-28] = "user supplied passwords not permitted";
61 setmetatable(sasl_errstring, { __index = function() return "undefined error!" end });
66 method.__index = method;
67 local initialized = false;
69 local function init(service_name)
70 if not initialized then
71 local st, errmsg = pcall(cyrussasl.server_init, service_name);
75 log("error", "Failed to initialize Cyrus SASL: %s", errmsg);
80 -- create a new SASL object which can be used to authenticate clients
81 function new(realm, service_name, app_name)
83 init(app_name or service_name);
85 local st, ret = pcall(cyrussasl.server_new, service_name, nil, realm, nil, nil)
87 log("error", "Creating SASL server connection failed: %s", ret);
91 local sasl_i = { realm = realm, service_name = service_name, cyrus = ret };
93 if cyrussasl.set_canon_cb then
94 local c14n_cb = function (user)
95 local node = s_match(user, "^([^@]+)");
96 log("debug", "Canonicalizing username %s to %s", user, node)
99 cyrussasl.set_canon_cb(sasl_i.cyrus, c14n_cb);
102 cyrussasl.setssf(sasl_i.cyrus, 0, 0xffffffff)
103 return setmetatable(sasl_i, method);
106 -- get a fresh clone with the same realm and service name
107 function method:clean_clone()
108 return new(self.realm, self.service_name)
111 -- get a list of possible SASL mechanims to use
112 function method:mechanisms()
113 local mechanisms = self.mechs;
114 if not mechanisms then
116 local cyrus_mechs = cyrussasl.listmech(self.cyrus, nil, "", " ", "")
117 for w in s_gmatch(cyrus_mechs, "[^ ]+") do
118 mechanisms[w] = true;
120 self.mechs = mechanisms
125 -- select a mechanism to use
126 function method:select(mechanism)
127 self.mechanism = mechanism;
128 return self:mechanisms()[mechanism];
131 -- feed new messages to process into the library
132 function method:process(message)
136 if not self.first_step_done then
137 err, data = cyrussasl.server_start(self.cyrus, self.mechanism, message or "")
138 self.first_step_done = true;
140 err, data = cyrussasl.server_step(self.cyrus, message or "")
143 self.username = cyrussasl.get_username(self.cyrus)
145 if (err == 0) then -- SASL_OK
146 if self.require_provisioning and not self.require_provisioning(self.username) then
147 return "failure", "not-authorized", "User authenticated successfully, but not provisioned for XMPP";
149 return "success", data
150 elseif (err == 1) then -- SASL_CONTINUE
151 return "challenge", data
152 elseif (err == -4) then -- SASL_NOMECH
153 log("debug", "SASL mechanism not available from remote end")
154 return "failure", "invalid-mechanism", "SASL mechanism not available"
155 elseif (err == -13) then -- SASL_BADAUTH
156 return "failure", "not-authorized", sasl_errstring[err];
158 log("debug", "Got SASL error condition %d: %s", err, sasl_errstring[err]);
159 return "failure", "undefined-condition", sasl_errstring[err];