Comparison

mod_auth_dovecot/auth_dovecot/sasl_dovecot.lib.lua @ 700:0c130c45b7c1

mod_auth_dovecot: Old forgotten changes. Testing appreciated.
author Kim Alvefur <zash@zash.se>
date Thu, 07 Jun 2012 23:41:25 +0200
parent 474:942738953ff3
child 708:d9a4e2f11b07
comparison
equal deleted inserted replaced
699:7c88e09a07e7 700:0c130c45b7c1
42 local conn, supported_mechs, pid; 42 local conn, supported_mechs, pid;
43 43
44 local function connect(socket_info) 44 local function connect(socket_info)
45 --log("debug", "connect(%q)", socket_path); 45 --log("debug", "connect(%q)", socket_path);
46 if conn then conn:close(); pid = nil; end 46 if conn then conn:close(); pid = nil; end
47 if not pid then pid = tonumber(tostring(conn):match("0x%x*$")) end
48 47
49 local socket_type = (type(socket_info) == "string") and "UNIX" or "TCP"; 48 local socket_type = (type(socket_info) == "string") and "UNIX" or "TCP";
50 49
51 local ok, err; 50 local ok, err, socket_path;
52 if socket_type == "TCP" then 51 if socket_type == "TCP" then
53 local socket_host, socket_port = unpack(socket_info); 52 local socket_host, socket_port = unpack(socket_info);
54 conn = socket.tcp(); 53 conn = socket.tcp();
55 ok, err = conn:connect(socket_host, socket_port); 54 ok, err = conn:connect(socket_host, socket_port);
56 socket_path = ("%s:%d"):format(socket_host, socket_port); 55 socket_path = ("%s:%d"):format(socket_host, socket_port);
57 elseif socket.unix then 56 elseif socket.unix then
57 socket_path = socket_info;
58 conn = socket.unix(); 58 conn = socket.unix();
59 ok, err = conn:connect(socket_path); 59 ok, err = conn:connect(socket_path);
60 else 60 else
61 err = "luasocket was not compiled with UNIX sockets support"; 61 err = "luasocket was not compiled with UNIX sockets support";
62 end 62 end
63 63
64 if not ok then 64 if not ok then
65 log("error", "error connecting to dovecot %s socket at '%s'. error was '%s'", socket_type, socket_path, err); 65 log("error", "error connecting to dovecot %s socket at '%s'. error was '%s'", socket_type, socket_path or socket_info, err);
66 return false; 66 return false;
67 end 67 end
68 68
69 -- Send our handshake 69 -- Send our handshake
70 pid = tonumber(tostring(conn):match("0x%x*$"));
70 log("debug", "sending handshake to dovecot. version 1.1, cpid '%d'", pid); 71 log("debug", "sending handshake to dovecot. version 1.1, cpid '%d'", pid);
71 if not conn:send("VERSION\t1\t1\n") then 72 if not conn:send("VERSION\t1\t1\n") then
72 return false 73 return false
73 end 74 end
74 if not conn:send("CPID\t" .. pid .. "\n") then 75 if not conn:send("CPID\t" .. pid .. "\n") then
125 end 126 end
126 127
127 -- [[ 128 -- [[
128 function method:send(...) 129 function method:send(...)
129 local msg = t_concat({...}, "\t"); 130 local msg = t_concat({...}, "\t");
130 local ok, err = self.conn:send(authmsg.."\n"); 131 if msg:sub(-1) ~= "\n" then
132 msg = msg .. "\n"
133 end
134 module:log("debug", "sending %q", msg:sub(1,-2));
135 local ok, err = self.conn:send(msg);
131 if not ok then 136 if not ok then
132 log("error", "Could not write to socket: %s", err); 137 log("error", "Could not write to socket: %s", err);
133 return nil, err; 138 return nil, err;
134 end 139 end
135 return true; 140 return true;
136 end 141 end
137 142
138 function method:recv() 143 function method:recv()
139 local line, err = self.conn:receive();
140 --log("debug", "Sent %d bytes to socket", ok); 144 --log("debug", "Sent %d bytes to socket", ok);
141 local line, err = self.conn:receive(); 145 local line, err = self.conn:receive();
142 if not line then 146 if not line then
143 log("error", "Could not read from socket: %s", err); 147 log("error", "Could not read from socket: %s", err);
144 return nil, err; 148 return nil, err;
145 end 149 end
150 module:log("debug", "received %q", line);
146 return line; 151 return line;
147 end 152 end
148 -- ]] 153 -- ]]
149 154
150 function method:plain_test(username, password, realm) 155 function method:plain_test(username, password, realm)
181 --return "challenge"; 186 --return "challenge";
182 --return "failure", "malformed-request"; 187 --return "failure", "malformed-request";
183 --end 188 --end
184 local request_id = self.request_id; 189 local request_id = self.request_id;
185 local authmsg; 190 local authmsg;
191 local ok, err;
186 if not self.started then 192 if not self.started then
187 self.started = true; 193 self.started = true;
188 authmsg = t_concat({ 194 ok, err = self:send(
189 "AUTH", 195 "AUTH",
190 request_id, 196 request_id,
191 self.selected, 197 self.selected,
192 "service="..self.service_name, 198 "service="..self.service_name,
193 "resp="..(message and b64(message) or "=") 199 "resp="..(message and b64(message) or "=")
194 }, "\t"); 200 );
195 else 201 else
196 authmsg = t_concat({ 202 ok, err = self:send(
197 "CONT", 203 "CONT",
198 request_id, 204 request_id,
199 (message and b64(message) or "=") 205 (message and b64(message) or "=")
200 }, "\t"); 206 );
201 end 207 end
202 --log("debug", "Sending %d bytes: %q", #authmsg, authmsg); 208 --log("debug", "Sending %d bytes: %q", #authmsg, authmsg);
203 local ok, err = self.conn:send(authmsg.."\n");
204 if not ok then 209 if not ok then
205 log("error", "Could not write to socket: %s", err); 210 log("error", "Could not write to socket: %s", err);
206 return "failure", "internal-server-error", err 211 return "failure", "internal-server-error", err
207 end 212 end
208 --log("debug", "Sent %d bytes to socket", ok); 213 --log("debug", "Sent %d bytes to socket", ok);
209 local line, err = self.conn:receive(); 214 local line, err = self:recv();
210 if not line then 215 if not line then
211 log("error", "Could not read from socket: %s", err); 216 log("error", "Could not read from socket: %s", err);
212 return "failure", "internal-server-error", err 217 return "failure", "internal-server-error", err
213 end 218 end
214 --log("debug", "Received %d bytes from socket: %s", #line, line); 219 --log("debug", "Received %d bytes from socket: %s", #line, line);