comparison mod_extauth/mod_extauth.lua @ 152:4ca382e8a4c5

mod_extauth: Initial commit.
author Waqas Hussain <waqas20@gmail.com>
date Sat, 15 May 2010 23:05:53 +0500
parents
children 1a5d5d4f08fe
comparison
equal deleted inserted replaced
151:5abf15351b9a 152:4ca382e8a4c5
1
2
3 local nodeprep = require "util.encodings".stringprep.nodeprep;
4 local process = require "process";
5
6 local script_type = module:get_option("extauth_type");
7 assert(script_type == "ejabberd");
8 local command = module:get_option("extauth_command");
9 assert(type(command) == "string");
10 local host = module.host;
11 assert(not host:find(":"));
12
13 local proc;
14 local function send_query(text)
15 if not proc then
16 proc = process.popen(command);
17 end
18 proc:write(text);
19 proc:flush();
20 return proc:read(4); -- FIXME do properly
21 end
22
23 function do_query(kind, username, password)
24 if not username then return nil, "not-acceptable"; end
25 username = nodeprep(username);
26 if not username then return nil, "jid-malformed"; end
27
28 local query = (password and "%s:%s:%s:%s" or "%s:%s:%s"):format(kind, username, host, password);
29 local len = #query
30 if len > 1000 then return nil, "policy-violation"; end
31
32 local lo = len % 256;
33 local hi = (len - lo) / 256;
34 query = string.char(hi, lo)..query;
35
36 local response = send_query(query);
37 if response == "\0\2\0\0" then
38 return nil, "not-authorized";
39 elseif response == "\0\2\0\1" then
40 return true;
41 else
42 proc = nil; -- TODO kill proc
43 return nil, "internal-server-error";
44 end
45 end
46
47 local provider = { name = "extauth" };
48
49 function provider.test_password(username, password)
50 return do_query("auth", username, password);
51 end
52
53 function provider.set_password(username, password)
54 return do_query("setpass", username, password);
55 end
56
57 function provider.user_exists(username)
58 return do_query("isuser", username);
59 end
60
61 function provider.get_password() return nil, "Passwords not available."; end
62 function provider.create_user(username, password) return nil, "Account creation/modification not available."; end
63 function provider.get_supported_methods() return {["PLAIN"] = true}; end
64 local config = require "core.configmanager";
65 local usermanager = require "core.usermanager";
66 local jid_bare = require "util.jid".bare;
67 function provider.is_admin(jid)
68 local admins = config.get(host, "core", "admins");
69 if admins ~= config.get("*", "core", "admins") then
70 if type(admins) == "table" then
71 jid = jid_bare(jid);
72 for _,admin in ipairs(admins) do
73 if admin == jid then return true; end
74 end
75 elseif admins then
76 log("error", "Option 'admins' for host '%s' is not a table", host);
77 end
78 end
79 return usermanager.is_admin(jid); -- Test whether it's a global admin instead
80 end
81
82
83 module:add_item("auth-provider", provider);