Software /
code /
prosody
Annotate
plugins/mod_hashpassauth.lua @ 3164:db9def53fe9c
Check in mod_hashpassauth -- works!
author | Jeff Mitchell <jeff@jefferai.org> |
---|---|
date | Wed, 26 May 2010 18:16:58 -0400 |
child | 3166:3c46cb94caed |
rev | line source |
---|---|
3164
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
1 -- Prosody IM |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
4 -- Copyright (C) 2010 Jeff Mitchell |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
5 -- |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
6 -- This project is MIT/X11 licensed. Please see the |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
7 -- COPYING file in the source package for more information. |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
8 -- |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
9 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
10 local datamanager = require "util.datamanager"; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
11 local log = require "util.logger".init("usermanager"); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
12 local type = type; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
13 local error = error; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
14 local ipairs = ipairs; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
15 local hashes = require "util.hashes"; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
16 local jid_bare = require "util.jid".bare; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
17 local saltedPasswordSHA1 = require "util.sasl.scram".saltedPasswordSHA1; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
18 local config = require "core.configmanager"; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
19 local usermanager = require "core.usermanager"; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
20 local generate_uuid = require "util.uuid".generate; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
21 local hosts = hosts; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
22 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
23 local prosody = _G.prosody; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
24 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
25 local is_cyrus = usermanager.is_cyrus; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
26 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
27 -- Default; can be set per-user |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
28 local iteration_count = 4096; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
29 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
30 function new_hashpass_provider(host) |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
31 local provider = { name = "hashpass" }; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
32 log("debug", "initializing hashpass authentication provider for host '%s'", host); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
33 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
34 function provider.test_password(username, password) |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
35 log("debug", "test password for user %s at host %s", username, module.host); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
36 if is_cyrus(host) then return nil, "Legacy auth not supported with Cyrus SASL."; end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
37 local credentials = datamanager.load(username, host, "accounts") or {}; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
38 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
39 if credentials.hashpass == nil or credentials.iteration_count == nil or credentials.salt == nil then |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
40 return nil, "Auth failed. Stored credential information is not complete."; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
41 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
42 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
43 local valid, binpass = saltedPasswordSHA1(password, credentials.salt, credentials.iteration_count); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
44 local hexpass = binpass:gsub(".", function (c) return ("%02x"):format(c:byte()); end); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
45 if valid then |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
46 log("debug", "salted password returned valid"); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
47 else |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
48 log("debug", "salted password returned not valid"); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
49 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
50 log("debug", "hexpass is '%s', stored pass is '%s'", hexpass, credentials.hashpass); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
51 if valid and hexpass == credentials.hashpass then |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
52 return true; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
53 else |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
54 return nil, "Auth failed. Invalid username, password, or password hash information."; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
55 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
56 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
57 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
58 function provider.get_password(username) |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
59 log("debug", "get_password for username '%s' at host '%s'", username, module.host); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
60 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
61 return (datamanager.load(username, host, "accounts") or {}).hashpass; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
62 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
63 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
64 function provider.set_password(username, password) |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
65 if is_cyrus(host) then return nil, "Passwords unavailable for Cyrus SASL."; end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
66 local account = datamanager.load(username, host, "accounts"); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
67 if account then |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
68 if account.iteration_count == nil then |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
69 account.iteration_count = iteration_count; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
70 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
71 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
72 if account.salt == nil then |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
73 account.salt = generate_uuid(); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
74 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
75 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
76 local valid, binpass = saltedPasswordSHA1(password, account.salt, account.iteration_count); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
77 local hexpass = binpass:gsub(".", function (c) return ("%02x"):format(c:byte()); end); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
78 account.hashpass = hexpass; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
79 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
80 return datamanager.store(username, host, "accounts", account); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
81 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
82 return nil, "Account not available."; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
83 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
84 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
85 function provider.user_exists(username) |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
86 if is_cyrus(host) then return true; end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
87 local account = datamanager.load(username, host, "accounts"); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
88 if not account then |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
89 log("debug", "account not found for username '%s' at host '%s'", username, module.host); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
90 return nil, "Auth failed. Invalid username"; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
91 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
92 if account.hashpass == nil or string.len(account.hashpass) == 0 then |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
93 log("debug", "account password not set or zero-length for username '%s' at host '%s'", username, module.host); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
94 return nil, "Auth failed. Password invalid."; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
95 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
96 return true; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
97 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
98 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
99 function provider.create_user(username, password) |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
100 if is_cyrus(host) then return nil, "Account creation/modification not available with Cyrus SASL."; end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
101 local salt = generate_uuid(); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
102 local valid, binpass = saltedPasswordSHA1(password, salt, iteration_count); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
103 local hexpass = binpass:gsub(".", function (c) return ("%02x"):format(c:byte()); end); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
104 return datamanager.store(username, host, "accounts", {hashpass = hexpass, salt = salt, iteration_count = iteration_count}); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
105 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
106 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
107 function provider.get_supported_methods() |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
108 return {["PLAIN"] = true}; -- TODO this should be taken from the config |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
109 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
110 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
111 function provider.is_admin(jid) |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
112 local admins = config.get(host, "core", "admins"); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
113 if admins ~= config.get("*", "core", "admins") and type(admins) == "table" then |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
114 jid = jid_bare(jid); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
115 for _,admin in ipairs(admins) do |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
116 if admin == jid then return true; end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
117 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
118 elseif admins then |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
119 log("error", "Option 'admins' for host '%s' is not a table", host); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
120 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
121 return is_admin(jid); -- Test whether it's a global admin instead |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
122 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
123 return provider; |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
124 end |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
125 |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
126 module:add_item("auth-provider", new_hashpass_provider(module.host)); |
db9def53fe9c
Check in mod_hashpassauth -- works!
Jeff Mitchell <jeff@jefferai.org>
parents:
diff
changeset
|
127 |