plugins/mod_legacyauth.lua
author Matthew Wild <mwild1@gmail.com>
Tue, 23 Apr 2013 15:13:51 +0100
changeset 5509 76744bda82be
parent 5126 a0673b644f05
child 5776 bd0ff8ae98a8
permissions -rw-r--r--
mod_auth_internal_plain: Don't log passwords, even at debug level
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1218
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1912
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1912
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     4
-- 
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     7
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     9
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local st = require "util.stanza";
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
local t_concat = table.concat;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
4258
ee445e658848 mod_legacyauth: Disallow on unencrypted connections by default, heed allow_unencrypted_plain_auth config option (thanks Maranda/Zash)
Matthew Wild <mwild1@gmail.com>
parents: 3528
diff changeset
    14
local secure_auth_only = module:get_option("c2s_require_encryption")
ee445e658848 mod_legacyauth: Disallow on unencrypted connections by default, heed allow_unencrypted_plain_auth config option (thanks Maranda/Zash)
Matthew Wild <mwild1@gmail.com>
parents: 3528
diff changeset
    15
	or module:get_option("require_encryption")
ee445e658848 mod_legacyauth: Disallow on unencrypted connections by default, heed allow_unencrypted_plain_auth config option (thanks Maranda/Zash)
Matthew Wild <mwild1@gmail.com>
parents: 3528
diff changeset
    16
	or not(module:get_option("allow_unencrypted_plain_auth"));
1216
fd8ce71bc72b mod_saslauth, mod_legacyauth: Deny logins to unsecure sessions when require_encryption config option is true
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
    17
1042
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    18
local sessionmanager = require "core.sessionmanager";
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    19
local usermanager = require "core.usermanager";
1831
ced7a6b8bcd0 mod_legacyauth: Added node and resource prepping.
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
    20
local nodeprep = require "util.encodings".stringprep.nodeprep;
ced7a6b8bcd0 mod_legacyauth: Added node and resource prepping.
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
    21
local resourceprep = require "util.encodings".stringprep.resourceprep;
1042
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    22
541
3521e0851c9e Change modules to use the new add_feature module API method.
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
    23
module:add_feature("jabber:iq:auth");
2610
c9ed79940b2e mod_legacyauth: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 1912
diff changeset
    24
module:hook("stream-features", function(event)
c9ed79940b2e mod_legacyauth: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 1912
diff changeset
    25
	local origin, features = event.origin, event.features;
c9ed79940b2e mod_legacyauth: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 1912
diff changeset
    26
	if secure_auth_only and not origin.secure then
1218
8e02c10c9e60 mod_legacyauth: Hide stream feature when secure auth is enabled, and session isn't secure
Matthew Wild <mwild1@gmail.com>
parents: 1216
diff changeset
    27
		-- Sorry, not offering to insecure streams!
8e02c10c9e60 mod_legacyauth: Hide stream feature when secure auth is enabled, and session isn't secure
Matthew Wild <mwild1@gmail.com>
parents: 1216
diff changeset
    28
		return;
2610
c9ed79940b2e mod_legacyauth: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 1912
diff changeset
    29
	elseif not origin.username then
1218
8e02c10c9e60 mod_legacyauth: Hide stream feature when secure auth is enabled, and session isn't secure
Matthew Wild <mwild1@gmail.com>
parents: 1216
diff changeset
    30
		features:tag("auth", {xmlns='http://jabber.org/features/iq-auth'}):up();
8e02c10c9e60 mod_legacyauth: Hide stream feature when secure auth is enabled, and session isn't secure
Matthew Wild <mwild1@gmail.com>
parents: 1216
diff changeset
    31
	end
891
236d1ce9fa99 mod_legacyauth: Added stream feature: <auth xmlns='http://jabber.org/features/iq-auth'/>
Waqas Hussain <waqas20@gmail.com>
parents: 760
diff changeset
    32
end);
421
63be85693710 Modules now sending disco replies
Waqas Hussain <waqas20@gmail.com>
parents: 308
diff changeset
    33
3527
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    34
module:hook("stanza/iq/jabber:iq:auth:query", function(event)
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    35
	local session, stanza = event.origin, event.stanza;
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    36
3528
5cdcd7ee6ef5 mod_legacyauth: Limit authentication to unauthenticated client connections.
Waqas Hussain <waqas20@gmail.com>
parents: 3527
diff changeset
    37
	if session.type ~= "c2s_unauthed" then
5126
a0673b644f05 mod_legacyauth: Fix for sending error response to unauthed s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 5084
diff changeset
    38
		(session.sends2s or session.send)(st.error_reply(stanza, "cancel", "service-unavailable", "Legacy authentication is only allowed for unauthenticated client connections."));
3528
5cdcd7ee6ef5 mod_legacyauth: Limit authentication to unauthenticated client connections.
Waqas Hussain <waqas20@gmail.com>
parents: 3527
diff changeset
    39
		return true;
5cdcd7ee6ef5 mod_legacyauth: Limit authentication to unauthenticated client connections.
Waqas Hussain <waqas20@gmail.com>
parents: 3527
diff changeset
    40
	end
5cdcd7ee6ef5 mod_legacyauth: Limit authentication to unauthenticated client connections.
Waqas Hussain <waqas20@gmail.com>
parents: 3527
diff changeset
    41
3527
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    42
	if secure_auth_only and not session.secure then
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    43
		session.send(st.error_reply(stanza, "modify", "not-acceptable", "Encryption (SSL or TLS) is required to connect to this server"));
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    44
		return true;
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    45
	end
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    46
	
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    47
	local username = stanza.tags[1]:child_with_name("username");
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    48
	local password = stanza.tags[1]:child_with_name("password");
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    49
	local resource = stanza.tags[1]:child_with_name("resource");
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    50
	if not (username and password and resource) then
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    51
		local reply = st.reply(stanza);
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    52
		session.send(reply:query("jabber:iq:auth")
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    53
			:tag("username"):up()
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    54
			:tag("password"):up()
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    55
			:tag("resource"):up());
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    56
	else
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    57
		username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    58
		username = nodeprep(username);
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    59
		resource = resourceprep(resource)
5083
4629c60a303b mod_legacyauth: Return an error if username or resource fails stringprep (thanks iron)
Kim Alvefur <zash@zash.se>
parents: 4258
diff changeset
    60
		if not (username and resource) then
4629c60a303b mod_legacyauth: Return an error if username or resource fails stringprep (thanks iron)
Kim Alvefur <zash@zash.se>
parents: 4258
diff changeset
    61
			session.send(st.error_reply(stanza, "modify", "bad-request"));
4629c60a303b mod_legacyauth: Return an error if username or resource fails stringprep (thanks iron)
Kim Alvefur <zash@zash.se>
parents: 4258
diff changeset
    62
			return true;
4629c60a303b mod_legacyauth: Return an error if username or resource fails stringprep (thanks iron)
Kim Alvefur <zash@zash.se>
parents: 4258
diff changeset
    63
		end
3527
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    64
		if usermanager.test_password(username, session.host, password) then
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    65
			-- Authentication successful!
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    66
			local success, err = sessionmanager.make_authenticated(session, username);
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    67
			if success then
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    68
				local err_type, err_msg;
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    69
				success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    70
				if not success then
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    71
					session.send(st.error_reply(stanza, err_type, err, err_msg));
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    72
					session.username, session.type = nil, "c2s_unauthed"; -- FIXME should this be placed in sessionmanager?
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    73
					return true;
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    74
				elseif resource ~= session.resource then -- server changed resource, not supported by legacy auth
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    75
					session.send(st.error_reply(stanza, "cancel", "conflict", "The requested resource could not be assigned to this session."));
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    76
					session:close(); -- FIXME undo resource bind and auth instead of closing the session?
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    77
					return true;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
				end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
			end
3527
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    80
			session.send(st.reply(stanza));
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    81
		else
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    82
			session.send(st.error_reply(stanza, "auth", "not-authorized"));
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    83
		end
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    84
	end
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    85
	return true;
59cdb9166bd0 mod_legacyauth: Updated to use the new events API.
Waqas Hussain <waqas20@gmail.com>
parents: 3395
diff changeset
    86
end);