mod_auth_external/mod_auth_external.lua
author Kim Alvefur <zash@zash.se>
Mon, 24 Aug 2015 23:17:36 +0200
changeset 1788 1656d4fd71d0
parent 1343 7dbde05b48a9
child 1593 3e4d15ae2133
child 2001 199fb0a82c0a
permissions -rw-r--r--
mod_cloud_notify: Fix syntax errors and name
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
158
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
     1
--
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
     2
-- Prosody IM
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
     3
-- Copyright (C) 2010 Waqas Hussain
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
     4
-- Copyright (C) 2010 Jeff Mitchell
1086
50ee38e95e75 Don't store password in temporary file, pipe instead
Mikael Nordfeldth <mmn@hethane.se>
parents: 902
diff changeset
     5
-- Copyright (C) 2013 Mikael Nordfeldth
1154
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
     6
-- Copyright (C) 2013 Matthew Wild, finally came to fix it all
158
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
     7
--
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
     8
-- This project is MIT/X11 licensed. Please see the
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
     9
-- COPYING file in the source package for more information.
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
    10
--
152
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    11
1154
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    12
local lpty = assert(require "lpty", "mod_auth_external requires lpty: https://code.google.com/p/prosody-modules/wiki/mod_auth_external#Installation");
1160
05685fd07395 mod_auth_external: Re-organise initialization a bit (superficial)
Matthew Wild <mwild1@gmail.com>
parents: 1159
diff changeset
    13
local usermanager = require "core.usermanager";
05685fd07395 mod_auth_external: Re-organise initialization a bit (superficial)
Matthew Wild <mwild1@gmail.com>
parents: 1159
diff changeset
    14
local new_sasl = require "util.sasl".new;
05685fd07395 mod_auth_external: Re-organise initialization a bit (superficial)
Matthew Wild <mwild1@gmail.com>
parents: 1159
diff changeset
    15
local server = require "net.server";
05685fd07395 mod_auth_external: Re-organise initialization a bit (superficial)
Matthew Wild <mwild1@gmail.com>
parents: 1159
diff changeset
    16
local have_async, async = pcall(require, "util.async");
152
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    17
168
cd8492748985 mod_auth_external: Renamed from mod_extauth. Update logging and options (external_auth_protocol, external_auth_command)
Matthew Wild <mwild1@gmail.com>
parents: 166
diff changeset
    18
local log = module._log;
158
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
    19
local host = module.host;
1160
05685fd07395 mod_auth_external: Re-organise initialization a bit (superficial)
Matthew Wild <mwild1@gmail.com>
parents: 1159
diff changeset
    20
1154
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    21
local script_type = module:get_option_string("external_auth_protocol", "generic");
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    22
local command = module:get_option_string("external_auth_command", "");
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    23
local read_timeout = module:get_option_number("external_auth_timeout", 5);
1161
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    24
local blocking = module:get_option_boolean("external_auth_blocking", not(have_async and server.event and lpty.getfd));
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    25
local auth_processes = module:get_option_number("external_auth_processes", 1);
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    26
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    27
assert(script_type == "ejabberd" or script_type == "generic", "Config error: external_auth_protocol must be 'ejabberd' or 'generic'");
1154
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    28
assert(not host:find(":"), "Invalid hostname");
152
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    29
1159
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    30
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    31
if not blocking then
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    32
	log("debug", "External auth in non-blocking mode, yay!")
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    33
	waiter, guard = async.waiter, async.guarder();
1161
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    34
elseif auth_processes > 1 then
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    35
	log("warn", "external_auth_processes is greater than 1, but we are in blocking mode - reducing to 1");
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    36
	auth_processes = 1;
1159
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    37
end
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    38
1161
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    39
local ptys = {};
846
5ddc43ce8993 mod_auth_external: Work even when the LuaProcessCall library isn't available.
Waqas Hussain <waqas20@gmail.com>
parents: 816
diff changeset
    40
1161
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    41
local pty_options = { throw_errors = false, no_local_echo = true, use_path = false };
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    42
for i = 1, auth_processes do
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    43
	ptys[i] = lpty.new(pty_options);
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    44
end
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    45
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    46
local curr_process = 0;
1154
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    47
function send_query(text)
1161
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    48
	curr_process = (curr_process%auth_processes)+1;
b9e4d935867c mod_auth_external: Support for multiple auth processes (set external_auth_processes = N) - requires non-blocking mode
Matthew Wild <mwild1@gmail.com>
parents: 1160
diff changeset
    49
	local pty = ptys[curr_process];
1159
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    50
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    51
	local finished_with_pty
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    52
	if not blocking then
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    53
		finished_with_pty = guard(pty); -- Prevent others from crossing this line while we're busy
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    54
	end
1154
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    55
	if not pty:hasproc() then
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    56
		local status, ret = pty:exitstatus();
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    57
		if status and (status ~= "exit" or ret ~= 0) then
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    58
			log("warn", "Auth process exited unexpectedly with %s %d, restarting", status, ret or 0);
846
5ddc43ce8993 mod_auth_external: Work even when the LuaProcessCall library isn't available.
Waqas Hussain <waqas20@gmail.com>
parents: 816
diff changeset
    59
			return nil;
5ddc43ce8993 mod_auth_external: Work even when the LuaProcessCall library isn't available.
Waqas Hussain <waqas20@gmail.com>
parents: 816
diff changeset
    60
		end
1154
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    61
		local ok, err = pty:startproc(command);
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    62
		if not ok then
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    63
			log("error", "Failed to start auth process '%s': %s", command, err);
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    64
			return nil;
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    65
		end
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    66
		log("debug", "Started auth process");
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    67
	end
846
5ddc43ce8993 mod_auth_external: Work even when the LuaProcessCall library isn't available.
Waqas Hussain <waqas20@gmail.com>
parents: 816
diff changeset
    68
1154
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
    69
	pty:send(text);
1159
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    70
	if blocking then
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    71
		return pty:read(read_timeout);
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    72
	else
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    73
		local response;
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    74
		local wait, done = waiter();
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    75
		server.addevent(pty:getfd(), server.event.EV_READ, function ()
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    76
			response = pty:read();
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    77
			done();
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    78
			return -1;
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    79
		end);
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    80
		wait();
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    81
		finished_with_pty();
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    82
		return response;
c56a1d449cad mod_auth_external: Add non-blocking mode (requires trunk, libevent and lpty 1.0.1). Our first complete non-blocking auth module!
Matthew Wild <mwild1@gmail.com>
parents: 1158
diff changeset
    83
	end
152
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    84
end
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    85
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    86
function do_query(kind, username, password)
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    87
	if not username then return nil, "not-acceptable"; end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1195
diff changeset
    88
152
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    89
	local query = (password and "%s:%s:%s:%s" or "%s:%s:%s"):format(kind, username, host, password);
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    90
	local len = #query
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    91
	if len > 1000 then return nil, "policy-violation"; end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1195
diff changeset
    92
158
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
    93
	if script_type == "ejabberd" then
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
    94
		local lo = len % 256;
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
    95
		local hi = (len - lo) / 256;
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
    96
		query = string.char(hi, lo)..query;
1195
f502cbffbdd4 mod_auth_external: merge two if blocks with an elseif
Matthew Wild <mwild1@gmail.com>
parents: 1161
diff changeset
    97
	elseif script_type == "generic" then
158
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
    98
		query = query..'\n';
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
    99
	end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1195
diff changeset
   100
1158
ae1767b54964 mod_auth_external: Fix logging of errors
Matthew Wild <mwild1@gmail.com>
parents: 1157
diff changeset
   101
	local response, err = send_query(query);
ae1767b54964 mod_auth_external: Fix logging of errors
Matthew Wild <mwild1@gmail.com>
parents: 1157
diff changeset
   102
	if not response then
ae1767b54964 mod_auth_external: Fix logging of errors
Matthew Wild <mwild1@gmail.com>
parents: 1157
diff changeset
   103
		log("warn", "Error while waiting for result from auth process: %s", err or "unknown error");
ae1767b54964 mod_auth_external: Fix logging of errors
Matthew Wild <mwild1@gmail.com>
parents: 1157
diff changeset
   104
	elseif (script_type == "ejabberd" and response == "\0\2\0\0") or
1154
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
   105
		(script_type == "generic" and response:gsub("\r?\n$", "") == "0") then
158
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
   106
			return nil, "not-authorized";
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
   107
	elseif (script_type == "ejabberd" and response == "\0\2\0\1") or
1154
61f95bf51b35 mod_auth_external: Switch to lpty, remove file-based fallback, improve error messages and handling. Should greatly increase compatibility with scripts.
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
   108
		(script_type == "generic" and response:gsub("\r?\n$", "") == "1") then
158
1a5d5d4f08fe Add "generic" script support to mod_extauth, as well as lpc support until waqas fixes process
Jeff Mitchell <jeff@jefferai.org>
parents: 152
diff changeset
   109
			return true;
152
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   110
	else
1158
ae1767b54964 mod_auth_external: Fix logging of errors
Matthew Wild <mwild1@gmail.com>
parents: 1157
diff changeset
   111
		log("warn", "Unable to interpret data from auth process, %s", (response:match("^error:") and response) or ("["..#response.." bytes]"));
152
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   112
		return nil, "internal-server-error";
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   113
	end
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   114
end
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   115
816
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   116
local host = module.host;
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   117
local provider = {};
152
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   118
816
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   119
function provider.test_password(username, password)
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   120
	return do_query("auth", username, password);
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   121
end
152
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   122
816
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   123
function provider.set_password(username, password)
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   124
	return do_query("setpass", username, password);
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   125
end
152
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   126
816
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   127
function provider.user_exists(username)
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   128
	return do_query("isuser", username);
152
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   129
end
4ca382e8a4c5 mod_extauth: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   130
816
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   131
function provider.create_user(username, password) return nil, "Account creation/modification not available."; end
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   132
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   133
function provider.get_sasl_handler()
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   134
	local testpass_authentication_profile = {
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   135
		plain_test = function(sasl, username, password, realm)
902
490cb9161c81 mod_auth_{external,internal_yubikey,ldap,ldap2,sql}: No need to nodeprep in SASL handler.
Waqas Hussain <waqas20@gmail.com>
parents: 846
diff changeset
   136
			return usermanager.test_password(username, realm, password), true;
816
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   137
		end,
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   138
	};
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   139
	return new_sasl(host, testpass_authentication_profile);
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   140
end
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   141
960007b0901e mod_auth_external, mod_auth_internal_yubikey: Get rid of useless wrapper function around the auth provider.
Waqas Hussain <waqas20@gmail.com>
parents: 814
diff changeset
   142
module:provides("auth", provider);