mod_auth_external/mod_auth_external.lua
changeset 1161 b9e4d935867c
parent 1160 05685fd07395
child 1195 f502cbffbdd4
equal deleted inserted replaced
1160:05685fd07395 1161:b9e4d935867c
    17 
    17 
    18 local log = module._log;
    18 local log = module._log;
    19 local host = module.host;
    19 local host = module.host;
    20 
    20 
    21 local script_type = module:get_option_string("external_auth_protocol", "generic");
    21 local script_type = module:get_option_string("external_auth_protocol", "generic");
    22 assert(script_type == "ejabberd" or script_type == "generic", "Config error: external_auth_protocol must be 'ejabberd' or 'generic'");
       
    23 local command = module:get_option_string("external_auth_command", "");
    22 local command = module:get_option_string("external_auth_command", "");
    24 local read_timeout = module:get_option_number("external_auth_timeout", 5);
    23 local read_timeout = module:get_option_number("external_auth_timeout", 5);
       
    24 local blocking = module:get_option_boolean("external_auth_blocking", not(have_async and server.event and lpty.getfd));
       
    25 local auth_processes = module:get_option_number("external_auth_processes", 1);
       
    26 
       
    27 assert(script_type == "ejabberd" or script_type == "generic", "Config error: external_auth_protocol must be 'ejabberd' or 'generic'");
    25 assert(not host:find(":"), "Invalid hostname");
    28 assert(not host:find(":"), "Invalid hostname");
    26 
    29 
    27 local blocking = module:get_option_boolean("external_auth_blocking", not(have_async and server.event and lpty.getfd));
       
    28 
    30 
    29 if not blocking then
    31 if not blocking then
    30 	log("debug", "External auth in non-blocking mode, yay!")
    32 	log("debug", "External auth in non-blocking mode, yay!")
    31 	waiter, guard = async.waiter, async.guarder();
    33 	waiter, guard = async.waiter, async.guarder();
       
    34 elseif auth_processes > 1 then
       
    35 	log("warn", "external_auth_processes is greater than 1, but we are in blocking mode - reducing to 1");
       
    36 	auth_processes = 1;
    32 end
    37 end
    33 
    38 
    34 local ptys = { lpty.new({ throw_errors = false, no_local_echo = true, use_path = false }) };
    39 local ptys = {};
    35 
    40 
       
    41 local pty_options = { throw_errors = false, no_local_echo = true, use_path = false };
       
    42 for i = 1, auth_processes do
       
    43 	ptys[i] = lpty.new(pty_options);
       
    44 end
       
    45 
       
    46 local curr_process = 0;
    36 function send_query(text)
    47 function send_query(text)
    37 	local pty = ptys[1];
    48 	curr_process = (curr_process%auth_processes)+1;
       
    49 	local pty = ptys[curr_process];
    38 
    50 
    39 	local finished_with_pty
    51 	local finished_with_pty
    40 	if not blocking then
    52 	if not blocking then
    41 		finished_with_pty = guard(pty); -- Prevent others from crossing this line while we're busy
    53 		finished_with_pty = guard(pty); -- Prevent others from crossing this line while we're busy
    42 	end
    54 	end