mod_auth_dovecot/mod_auth_dovecot.lua
changeset 274 cda4855863af
parent 273 8d283ae7f29d
child 305 4c3abf1a9b5a
equal deleted inserted replaced
273:8d283ae7f29d 274:cda4855863af
   115 			return false;
   115 			return false;
   116 		end
   116 		end
   117 		return r;
   117 		return r;
   118 	end
   118 	end
   119 	
   119 	
   120 	function provider.test_password(username, password)
   120 	function provider.send_auth_request(self, username, password)
   121 		log("debug", "test password '%s' for user %s at host %s", password, username, module.host);
   121 		if (provider.c == nil) then
   122 		
       
   123 		local tries = 0;
       
   124 		
       
   125 		if (provider.c == nil or tries > 0) then
       
   126 			if (not provider:connect()) then
   122 			if (not provider:connect()) then
   127 				return nil, "Auth failed. Dovecot communications error";
   123 				return nil, "Auth failed. Dovecot communications error";
   128 			end
   124 			end
   129 		end
   125 		end
   130 		
   126 		
   149 		local parts = string.gmatch(l, "[^\t]+");
   145 		local parts = string.gmatch(l, "[^\t]+");
   150 		
   146 		
   151 		-- Check response
   147 		-- Check response
   152 		local status = parts();
   148 		local status = parts();
   153 		local resp_id = tonumber(parts());
   149 		local resp_id = tonumber(parts());
       
   150 		
   154 		if (resp_id  ~= provider.request_id) then
   151 		if (resp_id  ~= provider.request_id) then
   155 			log("warn", "dovecot response_id(%s) doesn't match request_id(%s)", resp_id, provider.request_id);
   152 			log("warn", "dovecot response_id(%s) doesn't match request_id(%s)", resp_id, provider.request_id);
   156 			provider:close();
   153 			provider:close();
   157 			return nil, "Auth failed. Dovecot communications error";
   154 			return nil, "Auth failed. Dovecot communications error";
   158 		end
   155 		end
       
   156 		
       
   157 		return status, parts;
       
   158 	end
       
   159 	
       
   160 	function provider.test_password(username, password)
       
   161 		log("debug", "test password '%s' for user %s at host %s", password, username, module.host);
       
   162 		
       
   163 		local status, extra = provider:send_auth_request(username, password);
   159 		
   164 		
   160 		if (status == "OK") then
   165 		if (status == "OK") then
   161 			log("info", "login ok for '%s'", username);
   166 			log("info", "login ok for '%s'", username);
   162 			return true;
   167 			return true;
   163 		else
   168 		else
   173 	function provider.set_password(username, password)
   178 	function provider.set_password(username, password)
   174 		return nil, "Cannot set_password in dovecot backend.";
   179 		return nil, "Cannot set_password in dovecot backend.";
   175 	end
   180 	end
   176 
   181 
   177 	function provider.user_exists(username)
   182 	function provider.user_exists(username)
   178 		--TODO: Send an auth request. If it returns FAIL <id> user=<user> then user exists.
   183 		log("debug", "user_exists for user %s at host %s", username, module.host);
   179 		return nil, "user_exists not yet implemented in dovecot backend.";
   184 		
       
   185 		-- Send a request. If the response (FAIL) contains an extra
       
   186 		-- parameter like user=<username> then it exists.
       
   187 		local status, extra = provider:send_auth_request(username, "");
       
   188 		
       
   189 		local param = extra();
       
   190 		while (param) do
       
   191 			parts = string.gmatch(param, "[^=]+");
       
   192 			name = parts();
       
   193 			value = parts();
       
   194 			if (name == "user") then
       
   195 				log("info", "user '%s' exists", username);
       
   196 				return true;
       
   197 			end
       
   198 			
       
   199 			param = extra();
       
   200 		end
       
   201 		
       
   202 		log("info", "user '%s' does not exists (or dovecot didn't send user=<username> parameter)", username);
       
   203 		return false;
   180 	end
   204 	end
   181 
   205 
   182 	function provider.create_user(username, password)
   206 	function provider.create_user(username, password)
   183 		return nil, "Cannot create_user in dovecot backend.";
   207 		return nil, "Cannot create_user in dovecot backend.";
   184 	end
   208 	end