annotate mod_auth_phpbb3/mod_auth_phpbb3.lua @ 373:81c7b36e6cdd

mod_auth_phpbb3: Initial commit.
author Waqas Hussain <waqas20@gmail.com>
date Fri, 01 Jul 2011 06:40:33 +0500
parents
children 2dd6dfda94d6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
373
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 -- phpbb3 authentication backend for Prosody
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 --
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2011 Waqas Hussain
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 --
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 local log = require "util.logger".init("auth_sql");
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 local new_sasl = require "util.sasl".new;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 local nodeprep = require "util.encodings".stringprep.nodeprep;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local DBI = require "DBI"
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local md5 = require "util.hashes".md5;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local connection;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local params = module:get_option("sql");
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local resolve_relative_path = require "core.configmanager".resolve_relative_path;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local function test_connection()
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 if not connection then return nil; end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 if connection:ping() then
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 return true;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 else
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 module:log("debug", "Database connection closed");
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 connection = nil;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 local function connect()
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 if not test_connection() then
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 prosody.unlock_globals();
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 local dbh, err = DBI.Connect(
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 params.driver, params.database,
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 params.username, params.password,
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 params.host, params.port
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 );
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 prosody.lock_globals();
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 if not dbh then
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 module:log("debug", "Database connection failed: %s", tostring(err));
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 return nil, err;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 module:log("debug", "Successfully connected to database");
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 dbh:autocommit(true); -- don't run in transaction
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 connection = dbh;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 return connection;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 do -- process options to get a db connection
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 params = params or { driver = "SQLite3" };
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 if params.driver == "SQLite3" then
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite");
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 assert(params.driver and params.database, "Both the SQL driver and the database need to be specified");
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 assert(connect());
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 local function getsql(sql, ...)
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 if params.driver == "PostgreSQL" then
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 sql = sql:gsub("`", "\"");
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 if not test_connection() then connect(); end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 -- do prepared statement stuff
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 local stmt, err = connection:prepare(sql);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 if not stmt and not test_connection() then error("connection failed"); end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 -- run query
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 local ok, err = stmt:execute(...);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 if not ok and not test_connection() then error("connection failed"); end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 if not ok then return nil, err; end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 return stmt;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 local function get_password(username)
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 local stmt, err = getsql("SELECT `user_password` FROM `phpbb_users` WHERE `username`=?", username);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 if stmt then
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 for row in stmt:rows(true) do
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 return row.user_password;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 local itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 local function hashEncode64(input)
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 local count = 16;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 local output = "";
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 local i, value = 0, 0;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 while true do
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 value = input:byte(i+1)
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 i = i+1;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 local idx = value % 0x40 + 1;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 output = output .. itoa64:sub(idx, idx);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 if i < count then
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 value = value + input:byte(i+1) * 256;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 local _ = value % (2^6);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 local idx = ((value - _) / (2^6)) % 0x40 + 1
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 output = output .. itoa64:sub(idx, idx);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 if i >= count then break; end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 i = i+1;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 if i < count then
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 value = value + input:byte(i+1) * 256 * 256;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 local _ = value % (2^12);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 local idx = ((value - _) / (2^12)) % 0x40 + 1
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 output = output .. itoa64:sub(idx, idx);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 if i >= count then break; end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 i = i+1;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 local _ = value % (2^18);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 local idx = ((value - _) / (2^18)) % 0x40 + 1
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 output = output .. itoa64:sub(idx, idx);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 if not(i < count) then break; end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 return output;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 local function hashCryptPrivate(password, genSalt, itoa64)
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 local output = "*";
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 if not genSalt:match("^%$H%$") then return output; end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 local count_log2 = itoa64:find(genSalt:sub(4,4)) - 1;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 if count_log2 < 7 or count_log2 > 30 then return output; end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 local count = 2 ^ count_log2;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 local salt = genSalt:sub(5, 12);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 if #salt ~= 8 then return output; end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 local hash = md5(salt..password);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 while true do
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 hash = md5(hash..password);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 if not(count > 1) then break; end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 count = count-1;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 output = genSalt:sub(1, 12);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 output = output .. hashEncode64(hash);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 return output;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 local function phpbbCheckHash(password, hash)
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 return #hash == 34 and hashCryptPrivate(password, hash, itoa64) == hash;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 provider = { name = "phpbb3" };
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 function provider.test_password(username, password)
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 module:log("debug", "test_password '%s' for user %s", password, username);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 local hash = get_password(username);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 return phpbbCheckHash(password, hash);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 function provider.user_exists(username)
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 module:log("debug", "test user %s existence", username);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 return get_password(username) and true;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 function provider.get_password(username)
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 return nil, "Getting password is not supported.";
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 function provider.set_password(username, password)
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 return nil, "Setting password is not supported.";
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 function provider.create_user(username, password)
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
174 return nil, "Account creation/modification not supported.";
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 function provider.get_sasl_handler()
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 local profile = {
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 plain_test = function(username, password, realm)
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 -- TODO stringprep
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 return provider.test_password(username, password), true;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182 end;
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 };
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184 return new_sasl(module.host, profile);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 end
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187 module:add_item("auth-provider", provider);
81c7b36e6cdd mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188