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