# HG changeset patch # User Javier Torres # Date 1288477676 -7200 # Node ID cda4855863afd2236a5ae68c33729130cd94dc79 # Parent 8d283ae7f29d7299335aa1f54ec8cef72e22fd3d mod_auth_dovecot: Implement user_exists diff -r 8d283ae7f29d -r cda4855863af mod_auth_dovecot/mod_auth_dovecot.lua --- a/mod_auth_dovecot/mod_auth_dovecot.lua Sun Oct 31 00:27:17 2010 +0200 +++ b/mod_auth_dovecot/mod_auth_dovecot.lua Sun Oct 31 00:27:56 2010 +0200 @@ -117,12 +117,8 @@ return r; end - function provider.test_password(username, password) - log("debug", "test password '%s' for user %s at host %s", password, username, module.host); - - local tries = 0; - - if (provider.c == nil or tries > 0) then + function provider.send_auth_request(self, username, password) + if (provider.c == nil) then if (not provider:connect()) then return nil, "Auth failed. Dovecot communications error"; end @@ -151,12 +147,21 @@ -- Check response local status = parts(); local resp_id = tonumber(parts()); + if (resp_id ~= provider.request_id) then log("warn", "dovecot response_id(%s) doesn't match request_id(%s)", resp_id, provider.request_id); provider:close(); return nil, "Auth failed. Dovecot communications error"; end + return status, parts; + end + + function provider.test_password(username, password) + log("debug", "test password '%s' for user %s at host %s", password, username, module.host); + + local status, extra = provider:send_auth_request(username, password); + if (status == "OK") then log("info", "login ok for '%s'", username); return true; @@ -175,8 +180,27 @@ end function provider.user_exists(username) - --TODO: Send an auth request. If it returns FAIL user= then user exists. - return nil, "user_exists not yet implemented in dovecot backend."; + log("debug", "user_exists for user %s at host %s", username, module.host); + + -- Send a request. If the response (FAIL) contains an extra + -- parameter like user= then it exists. + local status, extra = provider:send_auth_request(username, ""); + + local param = extra(); + while (param) do + parts = string.gmatch(param, "[^=]+"); + name = parts(); + value = parts(); + if (name == "user") then + log("info", "user '%s' exists", username); + return true; + end + + param = extra(); + end + + log("info", "user '%s' does not exists (or dovecot didn't send user= parameter)", username); + return false; end function provider.create_user(username, password)