util.sasl_cyrus: Further fixing and cleanup.
[prosody.git] / util / sasl_cyrus.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 local cyrussasl = require "cyrussasl";
15 local log = require "util.logger".init("sasl_cyrus");
16 local array = require "util.array";
17
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
23
24 local keys = keys;
25
26 local print = print
27 local pcall = pcall
28
29 module "sasl_cyrus"
30
31 local method = {};
32 method.__index = method;
33
34 pcall(cyrussasl.server_init, "prosody")
35
36 -- create a new SASL object which can be used to authenticate clients
37 function new(realm, service_name)
38         local sasl_i = {};
39         sasl_i.realm = realm;
40         sasl_i.service_name = service_name;
41         sasl_i.cyrus = cyrussasl.server_new(service_name, realm, realm, nil, nil)
42         if sasl_i.cyrus == 0 then
43                 log("error", "got NULL return value from server_new")
44                 return nil;
45         end
46         cyrussasl.setssf(sasl_i.cyrus, 0, 0xffffffff)
47         local s = setmetatable(sasl_i, method);
48         return s;
49 end
50
51 -- get a fresh clone with the same realm, profiles and forbidden mechanisms
52 function method:clean_clone()
53         return new(self.realm, self.service_name)
54 end
55
56 -- set the forbidden mechanisms
57 function method:forbidden( restrict )
58         log("debug", "Called method:forbidden. NOT IMPLEMENTED.")
59         return {}
60 end
61
62 -- get a list of possible SASL mechanims to use
63 function method:mechanisms()
64         local mechanisms = {}
65         local cyrus_mechs = cyrussasl.listmech(self.cyrus)
66         for w in s_gmatch(cyrus_mechs, "%a+") do
67                 mechanisms[w] = true;
68         end
69         self.mechanisms = mechanisms
70         return array.collect(keys(mechanisms));
71 end
72
73 -- select a mechanism to use
74 function method:select(mechanism)
75         self.mechanism = mechanism;
76         return not self.mechanisms[mechanisms];
77 end
78
79 -- feed new messages to process into the library
80 function method:process(message)
81         local err;
82         local data;
83         if self.mechanism then
84                 err, data = cyrussasl.server_start(self.cyrus, self.mechanism, message)
85         else
86                 err, data = cyrussasl.server_step(self.cyrus, message)
87         end
88
89         self.username = cyrussasl.get_username(self.cyrus)
90
91         if (err == 0) then -- SASL_OK
92            return "success", data
93         elseif (err == 1) then -- SASL_CONTINUE
94            return "challenge", data
95         elseif (err == -4) then -- SASL_NOMECH
96            log("debug", "SASL mechanism not available from remote end")
97            return "failure", 
98              "undefined-condition",
99              "SASL mechanism not available"
100         elseif (err == -13) then -- SASL_BADAUTH
101            return "failure", "not-authorized"
102         else
103            log("debug", "Got SASL error condition %d", err)
104            return "failure", 
105              "undefined-condition",
106              cyrussasl.get_message( self.cyrus )
107         end
108 end
109
110 return _M;