mod_auth_dovecot/mod_auth_dovecot.lua
author Marco Cirillo <maranda@lightwitch.org>
Tue, 20 Sep 2011 22:42:03 +0000
changeset 435 b6abe463b4fc
parent 431 fb7898826026
permissions -rw-r--r--
mod_stanza_counter: "*" doesn't handle stanzas.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
418
e840b4ce538d mod_auth_dovecot: Fix years in (c)
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
     4
-- Copyright (C) 2010-2011 Matthew Wild
e840b4ce538d mod_auth_dovecot: Fix years in (c)
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
     5
-- Copyright (C) 2010-2011 Waqas Hussain
e840b4ce538d mod_auth_dovecot: Fix years in (c)
Kim Alvefur <zash@zash.se>
parents: 411
diff changeset
     6
-- Copyright (C) 2011 Kim Alvefur
261
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
431
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
     9
pcall(require, "socket.unix");
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
local datamanager = require "util.datamanager";
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
    11
local usermanager = require "core.usermanager";
270
853ae6ae87bf mod_auth_dovecot: Use correct module name for logger
Javier Torres <javitonino@gmail.com>
parents: 269
diff changeset
    12
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
    13
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
    14
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
    15
local base64 = require "util.encodings".base64;
340
5d306466f3f6 mod_auth_dovecot: Use hash of vhost as PID to pass to dovecot - the ID must be unique per process, whereas we make a connection per vhost.
Matthew Wild <mwild1@gmail.com>
parents: 339
diff changeset
    16
local sha1 = require "util.hashes".sha1;
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
    18
local prosody = prosody;
271
05ea4abb664d mod_auth_dovecot: Load dovecot socket path form config
Javier Torres <javitonino@gmail.com>
parents: 270
diff changeset
    19
local socket_path = module:get_option_string("dovecot_auth_socket", "/var/run/dovecot/auth-login");
431
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    20
local socket_host = module:get_option_string("dovecot_auth_host", "127.0.0.1");
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    21
local socket_port = module:get_option_string("dovecot_auth_port");
410
abac17cb5032 mod_auth_dovecot: Make appending the domain name configurable. Warning: If you depend on this behaviour, add auth_append_host = true to your config
Kim Alvefur <zash@zash.se>
parents: 342
diff changeset
    22
local append_host = module:get_option_boolean("auth_append_host", false);
431
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    23
if not socket_port and not socket.unix then
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    24
	error("LuaSocket was not compiled with UNIX socket support. Try using Dovecot 2.x with inet_listener support, or recompile LuaSocket with UNIX socket support.");
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    25
end
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
    27
function new_provider(host)
310
b3bcd1913c85 mod_auth_dovecot: Switch to using upvalue 'conn' instead of provider.c throughout (thanks Adrien Clerc)
Matthew Wild <mwild1@gmail.com>
parents: 305
diff changeset
    28
	local provider = { name = "dovecot", request_id = 0 };
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
	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
    30
	
310
b3bcd1913c85 mod_auth_dovecot: Switch to using upvalue 'conn' instead of provider.c throughout (thanks Adrien Clerc)
Matthew Wild <mwild1@gmail.com>
parents: 305
diff changeset
    31
	local conn;
340
5d306466f3f6 mod_auth_dovecot: Use hash of vhost as PID to pass to dovecot - the ID must be unique per process, whereas we make a connection per vhost.
Matthew Wild <mwild1@gmail.com>
parents: 339
diff changeset
    32
	-- Generate an id for this connection (must be a 31-bit number, unique per process)
5d306466f3f6 mod_auth_dovecot: Use hash of vhost as PID to pass to dovecot - the ID must be unique per process, whereas we make a connection per vhost.
Matthew Wild <mwild1@gmail.com>
parents: 339
diff changeset
    33
	local pid = tonumber(sha1(host, true):sub(1, 6), 16);
310
b3bcd1913c85 mod_auth_dovecot: Switch to using upvalue 'conn' instead of provider.c throughout (thanks Adrien Clerc)
Matthew Wild <mwild1@gmail.com>
parents: 305
diff changeset
    34
	
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
    35
	-- Closes the socket
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
    36
	function provider.close(self)
310
b3bcd1913c85 mod_auth_dovecot: Switch to using upvalue 'conn' instead of provider.c throughout (thanks Adrien Clerc)
Matthew Wild <mwild1@gmail.com>
parents: 305
diff changeset
    37
		if conn then
b3bcd1913c85 mod_auth_dovecot: Switch to using upvalue 'conn' instead of provider.c throughout (thanks Adrien Clerc)
Matthew Wild <mwild1@gmail.com>
parents: 305
diff changeset
    38
			conn:close();
b3bcd1913c85 mod_auth_dovecot: Switch to using upvalue 'conn' instead of provider.c throughout (thanks Adrien Clerc)
Matthew Wild <mwild1@gmail.com>
parents: 305
diff changeset
    39
			conn = nil;
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
    40
		end
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
    41
	end
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
    42
	
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    43
	-- 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
    44
	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
    45
		-- Destroy old socket
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
    46
		provider:close();
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
		
431
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    48
		local ok, err;
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    49
		if socket_port then
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    50
			log("debug", "connecting to dovecot TCP socket at '%s':'%s'", socket_host, socket_port);
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    51
			conn = socket.tcp();
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    52
			ok, err = conn:connect(socket_host, socket_port);
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    53
		elseif socket.unix then
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    54
			log("debug", "connecting to dovecot UNIX socket at '%s'", socket_path);
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    55
			conn = socket.unix();
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    56
			ok, err = conn:connect(socket_path);
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    57
		else
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    58
			err = "luasocket was not compiled with UNIX sockets support";
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    59
		end
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
    60
		if not ok then
431
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    61
			if socket_port then
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    62
				log("error", "error connecting to dovecot TCP socket at '%s':'%s'. error was '%s'. check permissions", socket_host, socket_port, err);
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    63
			else
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    64
				log("error", "error connecting to dovecot UNIX socket at '%s'. error was '%s'. check permissions", socket_path, err);
fb7898826026 mod_auth_dovecot: Added support for TCP sockets, and checks for UNIX socket availability.
Waqas Hussain <waqas20@gmail.com>
parents: 418
diff changeset
    65
			end
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
    66
			provider:close();
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    67
			return false;
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    68
		end
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    69
		
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
		-- Send our handshake
273
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
    71
		log("debug", "sending handshake to dovecot. version 1.1, cpid '%d'", pid);
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    72
		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
    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
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
    75
		if not provider:send("CPID\t" .. pid .. "\n") then
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    76
			return false
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    77
		end
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    78
		
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    79
		-- 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
    80
		local done = false;
339
5aa8229e8815 mod_auth_dovecot: Add line missing from previous commit.
Kim Alvefur <zash@zash.se>
parents: 338
diff changeset
    81
		local supported_mechs = {};
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
		while (not done) do
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
    83
			local line = provider:receive();
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
    84
			if not line then
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    85
				return false;
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    86
			end
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    87
			
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
    88
			log("debug", "dovecot handshake: '%s'", line);
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
    89
			local parts = line:gmatch("[^\t]+");
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
    90
			local first = parts();
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
    91
			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
    92
				-- Version should be 1.1
311
f663ea45436f mod_auth_dovecot: Only check Dovecot major version. Bump log level from 'warn' to 'error'. (thanks Adrien Clerc)
Matthew Wild <mwild1@gmail.com>
parents: 310
diff changeset
    93
				local major_version = parts();
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    94
				
311
f663ea45436f mod_auth_dovecot: Only check Dovecot major version. Bump log level from 'warn' to 'error'. (thanks Adrien Clerc)
Matthew Wild <mwild1@gmail.com>
parents: 310
diff changeset
    95
				if major_version ~= "1" then
f663ea45436f mod_auth_dovecot: Only check Dovecot major version. Bump log level from 'warn' to 'error'. (thanks Adrien Clerc)
Matthew Wild <mwild1@gmail.com>
parents: 310
diff changeset
    96
					log("error", "dovecot server version is not 1.x. it is %s.x", major_version);
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
    97
					provider:close();
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    98
					return false;
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
    99
				end
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   100
			elseif first == "MECH" then
338
6560fd0b77f5 mod_auth_dovecot: One mechanism per MECH command.
Kim Alvefur <zash@zash.se>
parents: 312
diff changeset
   101
				local mech = parts();
6560fd0b77f5 mod_auth_dovecot: One mechanism per MECH command.
Kim Alvefur <zash@zash.se>
parents: 312
diff changeset
   102
				supported_mechs[mech] = true;
6560fd0b77f5 mod_auth_dovecot: One mechanism per MECH command.
Kim Alvefur <zash@zash.se>
parents: 312
diff changeset
   103
			elseif first == "DONE" then
6560fd0b77f5 mod_auth_dovecot: One mechanism per MECH command.
Kim Alvefur <zash@zash.se>
parents: 312
diff changeset
   104
				-- We need PLAIN
6560fd0b77f5 mod_auth_dovecot: One mechanism per MECH command.
Kim Alvefur <zash@zash.se>
parents: 312
diff changeset
   105
				if not supported_mechs.PLAIN then
6560fd0b77f5 mod_auth_dovecot: One mechanism per MECH command.
Kim Alvefur <zash@zash.se>
parents: 312
diff changeset
   106
					log("warn", "server doesn't support PLAIN mechanism.");
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   107
					provider:close();
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   108
					return false;
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   109
				end
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
				done = true;
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
			end
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   112
		end
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   113
		return true;
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   114
	end
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   115
	
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   116
	-- 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
   117
	function provider.send(self, data)
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   118
		local ok, err = conn:send(data);
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   119
		if not ok then
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   120
			log("error", "error sending '%s' to dovecot. error was '%s'", data, err);
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   121
			provider:close();
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   122
			return false;
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   123
		end
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   124
		return true;
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   125
	end
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   126
	
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   127
	-- 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
   128
	function provider.receive(self)
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   129
		local line, err = conn:receive();
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   130
		if not line then
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   131
			log("error", "error receiving data from dovecot. error was '%s'", err);
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   132
			provider:close();
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   133
			return false;
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   134
		end
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   135
		return line;
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   136
	end
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   137
	
274
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   138
	function provider.send_auth_request(self, username, password)
310
b3bcd1913c85 mod_auth_dovecot: Switch to using upvalue 'conn' instead of provider.c throughout (thanks Adrien Clerc)
Matthew Wild <mwild1@gmail.com>
parents: 305
diff changeset
   139
		if not conn then
b3bcd1913c85 mod_auth_dovecot: Switch to using upvalue 'conn' instead of provider.c throughout (thanks Adrien Clerc)
Matthew Wild <mwild1@gmail.com>
parents: 305
diff changeset
   140
			if not provider:connect() then
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   141
				return nil, "Auth failed. Dovecot communications error";
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   142
			end
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   143
		end
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   144
		
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
		-- Send auth data
410
abac17cb5032 mod_auth_dovecot: Make appending the domain name configurable. Warning: If you depend on this behaviour, add auth_append_host = true to your config
Kim Alvefur <zash@zash.se>
parents: 342
diff changeset
   146
		if append_host then
abac17cb5032 mod_auth_dovecot: Make appending the domain name configurable. Warning: If you depend on this behaviour, add auth_append_host = true to your config
Kim Alvefur <zash@zash.se>
parents: 342
diff changeset
   147
			username = username .. "@" .. module.host;
abac17cb5032 mod_auth_dovecot: Make appending the domain name configurable. Warning: If you depend on this behaviour, add auth_append_host = true to your config
Kim Alvefur <zash@zash.se>
parents: 342
diff changeset
   148
		end
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
		local b64 = base64.encode(username .. "\0" .. username .. "\0" .. password);
273
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
   150
		provider.request_id = provider.request_id + 1 % 4294967296
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
   151
		
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
   152
		local msg = "AUTH\t" .. provider.request_id .. "\tPLAIN\tservice=XMPP\tresp=" .. b64;
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
   153
		log("debug", "sending auth request for '%s' with password '%s': '%s'", username, password, msg);
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   154
		if not provider:send(msg .. "\n") then
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   155
			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
   156
		end
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   157
		
272
6b35c23664db mod_auth_dovecot: Use sequential (instead of fixed) id for messages
Javier Torres <javitonino@gmail.com>
parents: 271
diff changeset
   158
		
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   159
		-- Get response
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   160
		local line = provider:receive();
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   161
		log("debug", "got auth response: '%s'", line);
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   162
		if not line then
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   163
			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
   164
		end
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   165
		local parts = line:gmatch("[^\t]+");
268
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   166
		
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   167
		-- Check response
273
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
   168
		local status = parts();
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
   169
		local resp_id = tonumber(parts());
274
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   170
		
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   171
		if resp_id  ~= provider.request_id then
273
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
   172
			log("warn", "dovecot response_id(%s) doesn't match request_id(%s)", resp_id, provider.request_id);
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
   173
			provider:close();
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
   174
			return nil, "Auth failed. Dovecot communications error";
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
   175
		end
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
   176
		
274
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   177
		return status, parts;
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   178
	end
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   179
	
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   180
	function provider.test_password(username, password)
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   181
		log("debug", "test password '%s' for user %s at host %s", password, username, module.host);
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   182
		
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   183
		local status, extra = provider:send_auth_request(username, password);
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   184
		
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   185
		if status == "OK" then
273
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
   186
			log("info", "login ok for '%s'", username);
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   187
			return true;
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   188
		else
273
8d283ae7f29d mod_auth_dovecot: More debug messages
Javier Torres <javitonino@gmail.com>
parents: 272
diff changeset
   189
			log("info", "login failed for '%s'", username);
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   190
			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
   191
		end
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   192
	end
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   193
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   194
	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
   195
		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
   196
	end
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   197
	
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   198
	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
   199
		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
   200
	end
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   201
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   202
	function provider.user_exists(username)
274
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   203
		log("debug", "user_exists for user %s at host %s", username, module.host);
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   204
		
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   205
		-- Send a request. If the response (FAIL) contains an extra
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   206
		-- parameter like user=<username> then it exists.
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   207
		local status, extra = provider:send_auth_request(username, "");
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   208
		
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   209
		local param = extra();
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   210
		while param do
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   211
			local parts = param:gmatch("[^=]+");
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   212
			local name = parts();
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   213
			local value = parts();
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   214
			if name == "user" then
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   215
				log("debug", "user '%s' exists", username);
274
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   216
				return true;
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   217
			end
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   218
			
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   219
			param = extra();
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   220
		end
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   221
		
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   222
		log("debug", "user '%s' does not exists (or dovecot didn't send user=<username> parameter)", username);
274
cda4855863af mod_auth_dovecot: Implement user_exists
Javier Torres <javitonino@gmail.com>
parents: 273
diff changeset
   223
		return false;
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   224
	end
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   225
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   226
	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
   227
		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
   228
	end
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   229
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   230
	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
   231
		local getpass_authentication_profile = {
305
4c3abf1a9b5a mod_auth_*, mod_saslauth_muc: Update SASL callbacks to take SASL handler as first argument.
Waqas Hussain <waqas20@gmail.com>
parents: 274
diff changeset
   232
			plain_test = function(sasl, 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
   233
			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
   234
			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
   235
				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
   236
				return "", nil;
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   237
			end
cfcd4efb0fa4 mod_auth_dovecot: Remove asserts (use logger) and refactor socket code
Javier Torres <javitonino@gmail.com>
parents: 267
diff changeset
   238
			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
   239
		end
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   240
		};
342
8e9e5c7d97ff mod_auth_*: Get rid of undocumented and broken 'sasl_realm' config option.
Waqas Hussain <waqas20@gmail.com>
parents: 340
diff changeset
   241
		return new_sasl(module.host, getpass_authentication_profile);
269
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   242
	end
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   243
	
74846ec9c29f mod_auth_dovecot: Close socket on error
Javier Torres <javitonino@gmail.com>
parents: 268
diff changeset
   244
	return provider;
261
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   245
end
0f46fb2dbc79 mod_auth_dovecot: Initial commit of Dovecot authentication backend by Javier Torres
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   246
312
31deafcbf623 mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
Matthew Wild <mwild1@gmail.com>
parents: 311
diff changeset
   247
module:add_item("auth-provider", new_provider(module.host));