util/sasl.lua

Mon, 23 Aug 2010 16:54:56 +0500

author
Waqas Hussain <waqas20@gmail.com>
date
Mon, 23 Aug 2010 16:54:56 +0500
changeset 3468
d50e2c937717
parent 3442
8cfacc41099e
child 3549
395d5bb5266e
permissions
-rw-r--r--

mod_saslauth, mod_auth_cyrus, util.sasl_cyrus: Moved cyrus account provisioning check out of mod_saslauth.

894
2c0b9e3c11c3 0.3->0.4
Matthew Wild <mwild1@gmail.com>
parents: 801
diff changeset
1 -- sasl.lua v0.4
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2873
diff changeset
2 -- Copyright (C) 2008-2010 Tobias Markmann
1585
edc066730d11 Switch to using a more generic credentials_callback/handler for SASL auth.
nick@lupine.me.uk
parents: 1518
diff changeset
3 --
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 508
diff changeset
4 -- All rights reserved.
1585
edc066730d11 Switch to using a more generic credentials_callback/handler for SASL auth.
nick@lupine.me.uk
parents: 1518
diff changeset
5 --
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 508
diff changeset
6 -- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1585
edc066730d11 Switch to using a more generic credentials_callback/handler for SASL auth.
nick@lupine.me.uk
parents: 1518
diff changeset
7 --
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 508
diff changeset
8 -- * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 508
diff changeset
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.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 508
diff changeset
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.
1585
edc066730d11 Switch to using a more generic credentials_callback/handler for SASL auth.
nick@lupine.me.uk
parents: 1518
diff changeset
11 --
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 508
diff changeset
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.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 508
diff changeset
13
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
14
2172
aaf2b2df61f7 Mostly making the code run; includes fixing typos and so on.
Tobias Markmann <tm@ayena.de>
parents: 2171
diff changeset
15 local pairs, ipairs = pairs, ipairs;
3441
d4f89802cf1e util.sasl, util.sasl_cyrus: Removed a ton of unused variables.
Waqas Hussain <waqas20@gmail.com>
parents: 3427
diff changeset
16 local t_insert = table.insert;
276
30893439d5d1 Some early attempts on DIGEST-MD5.
Tobias Markmann <tm@ayena.de>
parents: 50
diff changeset
17 local type = type
2171
3ca8755581a1 Initial commit of the SASL redesign.
Tobias Markmann <tm@ayena.de>
parents: 1585
diff changeset
18 local setmetatable = setmetatable;
3ca8755581a1 Initial commit of the SASL redesign.
Tobias Markmann <tm@ayena.de>
parents: 1585
diff changeset
19 local assert = assert;
2183
f0a85d11823e Getting PLAIN mechanism work with the new API.
Tobias Markmann <tm@ayena.de>
parents: 2182
diff changeset
20 local require = require;
276
30893439d5d1 Some early attempts on DIGEST-MD5.
Tobias Markmann <tm@ayena.de>
parents: 50
diff changeset
21
38
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
22 module "sasl"
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
23
2173
8505e1da5408 Adding some docu.
Tobias Markmann <tm@ayena.de>
parents: 2172
diff changeset
24 --[[
8505e1da5408 Adding some docu.
Tobias Markmann <tm@ayena.de>
parents: 2172
diff changeset
25 Authentication Backend Prototypes:
1585
edc066730d11 Switch to using a more generic credentials_callback/handler for SASL auth.
nick@lupine.me.uk
parents: 1518
diff changeset
26
2184
1fd38975addd Add support for plain profile in digest-md5 implementation.
Tobias Markmann <tm@ayena.de>
parents: 2183
diff changeset
27 state = false : disabled
1fd38975addd Add support for plain profile in digest-md5 implementation.
Tobias Markmann <tm@ayena.de>
parents: 2183
diff changeset
28 state = true : enabled
1fd38975addd Add support for plain profile in digest-md5 implementation.
Tobias Markmann <tm@ayena.de>
parents: 2183
diff changeset
29 state = nil : non-existant
2173
8505e1da5408 Adding some docu.
Tobias Markmann <tm@ayena.de>
parents: 2172
diff changeset
30 ]]
1585
edc066730d11 Switch to using a more generic credentials_callback/handler for SASL auth.
nick@lupine.me.uk
parents: 1518
diff changeset
31
2172
aaf2b2df61f7 Mostly making the code run; includes fixing typos and so on.
Tobias Markmann <tm@ayena.de>
parents: 2171
diff changeset
32 local method = {};
aaf2b2df61f7 Mostly making the code run; includes fixing typos and so on.
Tobias Markmann <tm@ayena.de>
parents: 2171
diff changeset
33 method.__index = method;
2171
3ca8755581a1 Initial commit of the SASL redesign.
Tobias Markmann <tm@ayena.de>
parents: 1585
diff changeset
34 local mechanisms = {};
3ca8755581a1 Initial commit of the SASL redesign.
Tobias Markmann <tm@ayena.de>
parents: 1585
diff changeset
35 local backend_mechanism = {};
1585
edc066730d11 Switch to using a more generic credentials_callback/handler for SASL auth.
nick@lupine.me.uk
parents: 1518
diff changeset
36
2171
3ca8755581a1 Initial commit of the SASL redesign.
Tobias Markmann <tm@ayena.de>
parents: 1585
diff changeset
37 -- register a new SASL mechanims
3ca8755581a1 Initial commit of the SASL redesign.
Tobias Markmann <tm@ayena.de>
parents: 1585
diff changeset
38 local function registerMechanism(name, backends, f)
3ca8755581a1 Initial commit of the SASL redesign.
Tobias Markmann <tm@ayena.de>
parents: 1585
diff changeset
39 assert(type(name) == "string", "Parameter name MUST be a string.");
3ca8755581a1 Initial commit of the SASL redesign.
Tobias Markmann <tm@ayena.de>
parents: 1585
diff changeset
40 assert(type(backends) == "string" or type(backends) == "table", "Parameter backends MUST be either a string or a table.");
3ca8755581a1 Initial commit of the SASL redesign.
Tobias Markmann <tm@ayena.de>
parents: 1585
diff changeset
41 assert(type(f) == "function", "Parameter f MUST be a function.");
2172
aaf2b2df61f7 Mostly making the code run; includes fixing typos and so on.
Tobias Markmann <tm@ayena.de>
parents: 2171
diff changeset
42 mechanisms[name] = f
aaf2b2df61f7 Mostly making the code run; includes fixing typos and so on.
Tobias Markmann <tm@ayena.de>
parents: 2171
diff changeset
43 for _, backend_name in ipairs(backends) do
aaf2b2df61f7 Mostly making the code run; includes fixing typos and so on.
Tobias Markmann <tm@ayena.de>
parents: 2171
diff changeset
44 if backend_mechanism[backend_name] == nil then backend_mechanism[backend_name] = {}; end
aaf2b2df61f7 Mostly making the code run; includes fixing typos and so on.
Tobias Markmann <tm@ayena.de>
parents: 2171
diff changeset
45 t_insert(backend_mechanism[backend_name], name);
aaf2b2df61f7 Mostly making the code run; includes fixing typos and so on.
Tobias Markmann <tm@ayena.de>
parents: 2171
diff changeset
46 end
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
47 end
1585
edc066730d11 Switch to using a more generic credentials_callback/handler for SASL auth.
nick@lupine.me.uk
parents: 1518
diff changeset
48
2171
3ca8755581a1 Initial commit of the SASL redesign.
Tobias Markmann <tm@ayena.de>
parents: 1585
diff changeset
49 -- create a new SASL object which can be used to authenticate clients
3427
046a8cf304dd util.sasl: Removed method:forbidden() and its side effects.
Waqas Hussain <waqas20@gmail.com>
parents: 3373
diff changeset
50 function new(realm, profile)
3442
8cfacc41099e util.sasl: Simplified some code a bit.
Waqas Hussain <waqas20@gmail.com>
parents: 3441
diff changeset
51 return setmetatable({ profile = profile, realm = realm }, method);
2197
49e4838f9755 Enable restriction of supported mechanisms in the SASL library.
Tobias Markmann <tm@ayena.de>
parents: 2194
diff changeset
52 end
1585
edc066730d11 Switch to using a more generic credentials_callback/handler for SASL auth.
nick@lupine.me.uk
parents: 1518
diff changeset
53
3427
046a8cf304dd util.sasl: Removed method:forbidden() and its side effects.
Waqas Hussain <waqas20@gmail.com>
parents: 3373
diff changeset
54 -- get a fresh clone with the same realm and profile
2241
ac3bd7c42c8b util.sasl: Adding clean_clone() method.
Tobias Markmann <tm@ayena.de>
parents: 2208
diff changeset
55 function method:clean_clone()
3427
046a8cf304dd util.sasl: Removed method:forbidden() and its side effects.
Waqas Hussain <waqas20@gmail.com>
parents: 3373
diff changeset
56 return new(self.realm, self.profile)
276
30893439d5d1 Some early attempts on DIGEST-MD5.
Tobias Markmann <tm@ayena.de>
parents: 50
diff changeset
57 end
30893439d5d1 Some early attempts on DIGEST-MD5.
Tobias Markmann <tm@ayena.de>
parents: 50
diff changeset
58
2171
3ca8755581a1 Initial commit of the SASL redesign.
Tobias Markmann <tm@ayena.de>
parents: 1585
diff changeset
59 -- get a list of possible SASL mechanims to use
3ca8755581a1 Initial commit of the SASL redesign.
Tobias Markmann <tm@ayena.de>
parents: 1585
diff changeset
60 function method:mechanisms()
3365
32dc830d976c util.sasl, util.sasl_cyrus: Updated method:mechanisms() to cache and re-use list of mechanisms.
Waqas Hussain <waqas20@gmail.com>
parents: 3364
diff changeset
61 local mechanisms = self.mechs;
32dc830d976c util.sasl, util.sasl_cyrus: Updated method:mechanisms() to cache and re-use list of mechanisms.
Waqas Hussain <waqas20@gmail.com>
parents: 3364
diff changeset
62 if not mechanisms then
32dc830d976c util.sasl, util.sasl_cyrus: Updated method:mechanisms() to cache and re-use list of mechanisms.
Waqas Hussain <waqas20@gmail.com>
parents: 3364
diff changeset
63 mechanisms = {}
32dc830d976c util.sasl, util.sasl_cyrus: Updated method:mechanisms() to cache and re-use list of mechanisms.
Waqas Hussain <waqas20@gmail.com>
parents: 3364
diff changeset
64 for backend, f in pairs(self.profile) do
32dc830d976c util.sasl, util.sasl_cyrus: Updated method:mechanisms() to cache and re-use list of mechanisms.
Waqas Hussain <waqas20@gmail.com>
parents: 3364
diff changeset
65 if backend_mechanism[backend] then
32dc830d976c util.sasl, util.sasl_cyrus: Updated method:mechanisms() to cache and re-use list of mechanisms.
Waqas Hussain <waqas20@gmail.com>
parents: 3364
diff changeset
66 for _, mechanism in ipairs(backend_mechanism[backend]) do
3427
046a8cf304dd util.sasl: Removed method:forbidden() and its side effects.
Waqas Hussain <waqas20@gmail.com>
parents: 3373
diff changeset
67 mechanisms[mechanism] = true;
1159
f81c8cec0e71 Adding minimal support for authorization identities to workaround buggy SASL implementations.
Tobias Markmann <tm@ayena.de>
parents: 1158
diff changeset
68 end
f81c8cec0e71 Adding minimal support for authorization identities to workaround buggy SASL implementations.
Tobias Markmann <tm@ayena.de>
parents: 1158
diff changeset
69 end
294
5d861d6e5bbd Made SASL module fit the new interface.
Tobias Markmann <tm@ayena.de>
parents: 292
diff changeset
70 end
3365
32dc830d976c util.sasl, util.sasl_cyrus: Updated method:mechanisms() to cache and re-use list of mechanisms.
Waqas Hussain <waqas20@gmail.com>
parents: 3364
diff changeset
71 self.mechs = mechanisms;
294
5d861d6e5bbd Made SASL module fit the new interface.
Tobias Markmann <tm@ayena.de>
parents: 292
diff changeset
72 end
3363
a4bb658d3fcb SASL: Simplified sasl_handler:mechanisms() to return a set, and not an array.
Waqas Hussain <waqas20@gmail.com>
parents: 3168
diff changeset
73 return mechanisms;
276
30893439d5d1 Some early attempts on DIGEST-MD5.
Tobias Markmann <tm@ayena.de>
parents: 50
diff changeset
74 end
30893439d5d1 Some early attempts on DIGEST-MD5.
Tobias Markmann <tm@ayena.de>
parents: 50
diff changeset
75
2171
3ca8755581a1 Initial commit of the SASL redesign.
Tobias Markmann <tm@ayena.de>
parents: 1585
diff changeset
76 -- select a mechanism to use
2172
aaf2b2df61f7 Mostly making the code run; includes fixing typos and so on.
Tobias Markmann <tm@ayena.de>
parents: 2171
diff changeset
77 function method:select(mechanism)
2181
e92339c48ee6 Fail if mechanism has already been selected.
Tobias Markmann <tm@ayena.de>
parents: 2180
diff changeset
78 if self.mech_i then
e92339c48ee6 Fail if mechanism has already been selected.
Tobias Markmann <tm@ayena.de>
parents: 2180
diff changeset
79 return false;
e92339c48ee6 Fail if mechanism has already been selected.
Tobias Markmann <tm@ayena.de>
parents: 2180
diff changeset
80 end
e92339c48ee6 Fail if mechanism has already been selected.
Tobias Markmann <tm@ayena.de>
parents: 2180
diff changeset
81
3373
cd6836586a6a util.sasl, util.sasl_cyrus: s/self.mechanisms()/self:mechanisms()/ in method:select().
Waqas Hussain <waqas20@gmail.com>
parents: 3366
diff changeset
82 self.mech_i = mechanisms[self:mechanisms()[mechanism] and mechanism];
3168
bb4286962d69 SASL: Minor cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3116
diff changeset
83 return (self.mech_i ~= nil);
799
b7ea802f3527 Adding inital support for ANONYMOUS mechanism in SASL.
Tobias Markmann <tm@ayena.de>
parents: 760
diff changeset
84 end
b7ea802f3527 Adding inital support for ANONYMOUS mechanism in SASL.
Tobias Markmann <tm@ayena.de>
parents: 760
diff changeset
85
2172
aaf2b2df61f7 Mostly making the code run; includes fixing typos and so on.
Tobias Markmann <tm@ayena.de>
parents: 2171
diff changeset
86 -- feed new messages to process into the library
aaf2b2df61f7 Mostly making the code run; includes fixing typos and so on.
Tobias Markmann <tm@ayena.de>
parents: 2171
diff changeset
87 function method:process(message)
2184
1fd38975addd Add support for plain profile in digest-md5 implementation.
Tobias Markmann <tm@ayena.de>
parents: 2183
diff changeset
88 --if message == "" or message == nil then return "failure", "malformed-request" end
2174
28d841403a21 Adjust SASL PLAIN mechanism to the new API.
Tobias Markmann <tm@ayena.de>
parents: 2173
diff changeset
89 return self.mech_i(self, message);
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
90 end
799
b7ea802f3527 Adding inital support for ANONYMOUS mechanism in SASL.
Tobias Markmann <tm@ayena.de>
parents: 760
diff changeset
91
2182
1112871916eb Move each mechanism in an own file.
Tobias Markmann <tm@ayena.de>
parents: 2181
diff changeset
92 -- load the mechanisms
3442
8cfacc41099e util.sasl: Simplified some code a bit.
Waqas Hussain <waqas20@gmail.com>
parents: 3441
diff changeset
93 require "util.sasl.plain" .init(registerMechanism);
8cfacc41099e util.sasl: Simplified some code a bit.
Waqas Hussain <waqas20@gmail.com>
parents: 3441
diff changeset
94 require "util.sasl.digest-md5".init(registerMechanism);
8cfacc41099e util.sasl: Simplified some code a bit.
Waqas Hussain <waqas20@gmail.com>
parents: 3441
diff changeset
95 require "util.sasl.anonymous" .init(registerMechanism);
8cfacc41099e util.sasl: Simplified some code a bit.
Waqas Hussain <waqas20@gmail.com>
parents: 3441
diff changeset
96 require "util.sasl.scram" .init(registerMechanism);
15
c0d754774db2 adding SASL lib with PLAIN support, not tested yet
Tobias Markmann <tm@ayena.de>
parents:
diff changeset
97
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 508
diff changeset
98 return _M;

mercurial