Mercurial > prosody-modules
annotate mod_auth_dovecot/mod_auth_dovecot.lua @ 271:05ea4abb664d
mod_auth_dovecot: Load dovecot socket path form config
author | Javier Torres <javitonino@gmail.com> |
---|---|
date | Sat, 30 Oct 2010 23:39:22 +0200 |
parents | 853ae6ae87bf |
children | 6b35c23664db |
rev | line source |
---|---|
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 -- Dovecot authentication backend for Prosody |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 -- |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 -- Copyright (C) 2010 Javier Torres |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 -- Copyright (C) 2008-2010 Matthew Wild |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 -- Copyright (C) 2008-2010 Waqas Hussain |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 -- |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local socket_unix = require "socket.unix"; |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local datamanager = require "util.datamanager"; |
270
853ae6ae87bf
mod_auth_dovecot: Use correct module name for logger
Javier Torres <javitonino@gmail.com>
parents:
269
diff
changeset
|
10 local log = require "util.logger".init("auth_dovecot"); |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local new_sasl = require "util.sasl".new; |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 local nodeprep = require "util.encodings".stringprep.nodeprep; |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local base64 = require "util.encodings".base64; |
267
76f3310ec113
mod_auth_dovecot: Use PID in handshake
Javier Torres <javitonino@gmail.com>
parents:
261
diff
changeset
|
14 local pposix = require "util.pposix"; |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local prosody = _G.prosody; |
271
05ea4abb664d
mod_auth_dovecot: Load dovecot socket path form config
Javier Torres <javitonino@gmail.com>
parents:
270
diff
changeset
|
17 local socket_path = module:get_option_string("dovecot_auth_socket", "/var/run/dovecot/auth-login"); |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 function new_default_provider(host) |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
20 local provider = { name = "dovecot", c = nil }; |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 log("debug", "initializing dovecot authentication provider for host '%s'", host); |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
22 |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
23 -- Closes the socket |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
24 function provider.close(self) |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
25 if (provider.c ~= nil) then |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
26 provider.c:close(); |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
27 end |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
28 provider.c = nil; |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
29 end |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
30 |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
31 -- The following connects to a new socket and send the handshake |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
32 function provider.connect(self) |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
33 -- Destroy old socket |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
34 provider:close(); |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
36 provider.c = socket.unix(); |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
38 -- Create a connection to dovecot socket |
271
05ea4abb664d
mod_auth_dovecot: Load dovecot socket path form config
Javier Torres <javitonino@gmail.com>
parents:
270
diff
changeset
|
39 local r, e = provider.c:connect(socket_path); |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
40 if (not r) then |
271
05ea4abb664d
mod_auth_dovecot: Load dovecot socket path form config
Javier Torres <javitonino@gmail.com>
parents:
270
diff
changeset
|
41 log("warn", "error connecting to dovecot socket at '%s'. error was '%s'. check permissions", socket_path, e); |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
42 provider:close(); |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
43 return false; |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
44 end |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
45 |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 -- Send our handshake |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
47 local pid = pposix.getpid(); |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
48 if not provider:send("VERSION\t1\t1\n") then |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
49 return false |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
50 end |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
51 if (not provider:send("CPID\t" .. pid .. "\n")) then |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
52 return false |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
53 end |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
54 |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
55 -- Parse Dovecot's handshake |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local done = false; |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 while (not done) do |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
58 local l = provider:receive(); |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
59 if (not l) then |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
60 return false; |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
61 end |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
62 |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 parts = string.gmatch(l, "[^\t]+"); |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 first = parts(); |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 if (first == "VERSION") then |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
66 -- Version should be 1.1 |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
67 local v1 = parts(); |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
68 local v2 = parts(); |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
69 |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
70 if (not (v1 == "1" and v2 == "1")) then |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
71 log("warn", "server version is not 1.1. it is %s.%s", v1, v2); |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
72 provider:close(); |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
73 return false; |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
74 end |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 elseif (first == "MECH") then |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
76 -- Mechanisms should include PLAIN |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 local ok = false; |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 for p in parts do |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 if p == "PLAIN" then |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 ok = true; |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 end |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 end |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
83 if (not ok) then |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
84 log("warn", "server doesn't support PLAIN mechanism. It supports '%s'", l); |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
85 provider:close(); |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
86 return false; |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
87 end |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 elseif (first == "DONE") then |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 done = true; |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 end |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 end |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
92 return true; |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
93 end |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
94 |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
95 -- Wrapper for send(). Handles errors |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
96 function provider.send(self, data) |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
97 local r, e = provider.c:send(data); |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
98 if (not r) then |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
99 log("warn", "error sending '%s' to dovecot. error was '%s'", data, e); |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
100 provider:close(); |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
101 return false; |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
102 end |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
103 return true; |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
104 end |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
105 |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
106 -- Wrapper for receive(). Handles errors |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
107 function provider.receive(self) |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
108 local r, e = provider.c:receive(); |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
109 if (not r) then |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
110 log("warn", "error receiving data from dovecot. error was '%s'", socket, e); |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
111 provider:close(); |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
112 return false; |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
113 end |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
114 return r; |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
115 end |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
116 |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
117 function provider.test_password(username, password) |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
118 log("debug", "test password '%s' for user %s at host %s", password, username, module.host); |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
119 |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
120 local tries = 0; |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
121 |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
122 if (provider.c == nil or tries > 0) then |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
123 if (not provider:connect()) then |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
124 return nil, "Auth failed. Dovecot communications error"; |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
125 end |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
126 end |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
127 |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 -- Send auth data |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 username = username .. "@" .. module.host; -- FIXME: this is actually a hack for my server |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 local b64 = base64.encode(username .. "\0" .. username .. "\0" .. password); |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 local id = "54321"; -- FIXME: probably can just be a fixed value if making one request per connection |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
132 if (not provider:send("AUTH\t" .. id .. "\tPLAIN\tservice=XMPP\tresp=" .. b64 .. "\n")) then |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
133 return nil, "Auth failed. Dovecot communications error"; |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
134 end |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
135 |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
136 -- Get response |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
137 local l = provider:receive(); |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
138 if (not l) then |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
139 return nil, "Auth failed. Dovecot communications error"; |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
140 end |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 local parts = string.gmatch(l, "[^\t]+"); |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
142 |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
143 -- Check response |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 if (parts() == "OK") then |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 return true; |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 else |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 return nil, "Auth failed. Invalid username or password."; |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 end |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 end |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
150 |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 function provider.get_password(username) |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 return nil, "Cannot get_password in dovecot backend."; |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 end |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 function provider.set_password(username, password) |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 return nil, "Cannot set_password in dovecot backend."; |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 end |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
158 |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 function provider.user_exists(username) |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
160 --TODO: Send an auth request. If it returns FAIL <id> user=<user> then user exists. |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
161 return nil, "user_exists not yet implemented in dovecot backend."; |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 end |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
163 |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 function provider.create_user(username, password) |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 return nil, "Cannot create_user in dovecot backend."; |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 end |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
167 |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 function provider.get_sasl_handler() |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 local realm = module:get_option("sasl_realm") or module.host; |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 local getpass_authentication_profile = { |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 plain_test = function(username, password, realm) |
268
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
172 local prepped_username = nodeprep(username); |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
173 if not prepped_username then |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
174 log("debug", "NODEprep failed on username: %s", username); |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
175 return "", nil; |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
176 end |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
177 return usermanager.test_password(prepped_username, realm, password), true; |
cfcd4efb0fa4
mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents:
267
diff
changeset
|
178 end |
269
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
179 }; |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
180 return new_sasl(realm, getpass_authentication_profile); |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
181 end |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
182 |
74846ec9c29f
mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents:
268
diff
changeset
|
183 return provider; |
261
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 end |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 |
0f46fb2dbc79
mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 module:add_item("auth-provider", new_default_provider(module.host)); |