Annotate

plugins/mod_iq.lua @ 13652:a08065207ef0

net.server_epoll: Call :shutdown() on TLS sockets when supported Comment from Matthew: This fixes a potential issue where the Prosody process gets blocked on sockets waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends layer 7 data, and this can cause problems for sockets which are in the process of being cleaned up. This depends on LuaSec changes which are not yet upstream. From Martijn's original email: So first my analysis of luasec. in ssl.c the socket is put into blocking mode right before calling SSL_shutdown() inside meth_destroy(). My best guess to why this is is because meth_destroy is linked to the __close and __gc methods, which can't exactly be called multiple times and luasec does want to make sure that a tls session is shutdown as clean as possible. I can't say I disagree with this reasoning and don't want to change this behaviour. My solution to this without changing the current behaviour is to introduce a shutdown() method. I am aware that this overlaps in a conflicting way with tcp's shutdown method, but it stays close to the OpenSSL name. This method calls SSL_shutdown() in the current (non)blocking mode of the underlying socket and returns a boolean whether or not the shutdown is completed (matching SSL_shutdown()'s 0 or 1 return values), and returns the familiar ssl_ioerror() strings on error with a false for completion. This error can then be used to determine if we have wantread/wantwrite to finalize things. Once meth_shutdown() has been called once a shutdown flag will be set, which indicates to meth_destroy() that the SSL_shutdown() has been handled by the application and it shouldn't be needed to set the socket to blocking mode. I've left the SSL_shutdown() call in the LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a timeout for the shutdown code, which might allow SSL_shutdown() to clean up anyway at the last possible moment. Another thing I've changed to luasec is the call to socket_setblocking() right before calling close(2) in socket_destroy() in usocket.c. According to the latest POSIX[0]: Note that the requirement for close() on a socket to block for up to the current linger interval is not conditional on the O_NONBLOCK setting. Which I read to mean that removing O_NONBLOCK on the socket before close doesn't impact the behaviour and only causes noise in system call tracers. I didn't touch the windows bits of this, since I don't do windows. For the prosody side of things I've made the TLS shutdown bits resemble interface:onwritable(), and put it under a combined guard of self._tls and self.conn.shutdown. The self._tls bit is there to prevent getting stuck on this condition, and self.conn.shutdown is there to prevent the code being called by instances where the patched luasec isn't deployed. The destroy() method can be called from various places and is read by me as the "we give up" error path. To accommodate for these unexpected entrypoints I've added a single call to self.conn:shutdown() to prevent the socket being put into blocking mode. I have no expectations that there is any other use here. Same as previous, the self.conn.shutdown check is there to make sure it's not called on unpatched luasec deployments and self._tls is there to make sure we don't call shutdown() on tcp sockets. I wouldn't recommend logging of the conn:shutdown() error inside close(), since a lot of clients simply close the connection before SSL_shutdown() is done.
author Martijn van Duren <martijn@openbsd.org>
date Thu, 06 Feb 2025 15:04:38 +0000
parent 12977:74b9e05af71e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1421
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
4 --
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1421
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1421
diff changeset
6 -- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1421
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1421
diff changeset
8
1265
3f3c62e45eeb mod_iq: Error reply for IQ to non-existing session. mod_iq now handles all 'iq/full' cases
Waqas Hussain <waqas20@gmail.com>
parents: 1260
diff changeset
9
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 8728
diff changeset
10 local st = require "prosody.util.stanza";
1234
0ff02499f05c mod_message, mod_iq: A little cleanup
Waqas Hussain <waqas20@gmail.com>
parents: 1233
diff changeset
11
5370
7838acadb0fa mod_announce, mod_auth_anonymous, mod_c2s, mod_c2s, mod_component, mod_iq, mod_message, mod_presence, mod_tls: Access prosody.{hosts,bare_sessions,full_sessions} instead of the old globals
Kim Alvefur <zash@zash.se>
parents: 4966
diff changeset
12 local full_sessions = prosody.full_sessions;
1233
4c8c3d7d9c27 mod_iq: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13
3679
afdce92d07be mod_iq: Fix an extra character in previous commit...
Waqas Hussain <waqas20@gmail.com>
parents: 3678
diff changeset
14 if module:get_host_type() == "local" then
3678
ce04b8b144de mod_iq: Don't hook 'iq/full' on components.
Waqas Hussain <waqas20@gmail.com>
parents: 3673
diff changeset
15 module:hook("iq/full", function(data)
8728
41c959c5c84b Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
16 -- IQ to full JID received
3678
ce04b8b144de mod_iq: Don't hook 'iq/full' on components.
Waqas Hussain <waqas20@gmail.com>
parents: 3673
diff changeset
17 local origin, stanza = data.origin, data.stanza;
1233
4c8c3d7d9c27 mod_iq: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18
3678
ce04b8b144de mod_iq: Don't hook 'iq/full' on components.
Waqas Hussain <waqas20@gmail.com>
parents: 3673
diff changeset
19 local session = full_sessions[stanza.attr.to];
4966
073eff2853a1 mod_iq: Don't treat an iq as handled if session.send() returns false
Matthew Wild <mwild1@gmail.com>
parents: 4870
diff changeset
20 if not (session and session.send(stanza)) then
3678
ce04b8b144de mod_iq: Don't hook 'iq/full' on components.
Waqas Hussain <waqas20@gmail.com>
parents: 3673
diff changeset
21 if stanza.attr.type == "get" or stanza.attr.type == "set" then
ce04b8b144de mod_iq: Don't hook 'iq/full' on components.
Waqas Hussain <waqas20@gmail.com>
parents: 3673
diff changeset
22 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
ce04b8b144de mod_iq: Don't hook 'iq/full' on components.
Waqas Hussain <waqas20@gmail.com>
parents: 3673
diff changeset
23 end
1265
3f3c62e45eeb mod_iq: Error reply for IQ to non-existing session. mod_iq now handles all 'iq/full' cases
Waqas Hussain <waqas20@gmail.com>
parents: 1260
diff changeset
24 end
3678
ce04b8b144de mod_iq: Don't hook 'iq/full' on components.
Waqas Hussain <waqas20@gmail.com>
parents: 3673
diff changeset
25 return true;
ce04b8b144de mod_iq: Don't hook 'iq/full' on components.
Waqas Hussain <waqas20@gmail.com>
parents: 3673
diff changeset
26 end);
ce04b8b144de mod_iq: Don't hook 'iq/full' on components.
Waqas Hussain <waqas20@gmail.com>
parents: 3673
diff changeset
27 end
1233
4c8c3d7d9c27 mod_iq: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28
4c8c3d7d9c27 mod_iq: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 module:hook("iq/bare", function(data)
8728
41c959c5c84b Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
30 -- IQ to bare JID received
4760
55501fc4394b mod_iq: Remove unused import of jid.split, bare_sessions and don't unpack event.origin when it isn't used. Waqas.
Matthew Wild <mwild1@gmail.com>
parents: 3679
diff changeset
31 local stanza = data.stanza;
3657
07f0c2ef16cb mod_iq: Optimized a bit (fewer table accesses).
Waqas Hussain <waqas20@gmail.com>
parents: 3656
diff changeset
32 local type = stanza.attr.type;
1233
4c8c3d7d9c27 mod_iq: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33
4c8c3d7d9c27 mod_iq: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 -- TODO fire post processing events
3657
07f0c2ef16cb mod_iq: Optimized a bit (fewer table accesses).
Waqas Hussain <waqas20@gmail.com>
parents: 3656
diff changeset
35 if type == "get" or type == "set" then
3658
13600f0d56b5 mod_iq: Optimized a bit more (fewer table accesses).
Waqas Hussain <waqas20@gmail.com>
parents: 3657
diff changeset
36 local child = stanza.tags[1];
4870
ca39f9b4cc8e mod_iq: Use "jabber:client" when the stanza payload namespace is nil.
Waqas Hussain <waqas20@gmail.com>
parents: 4760
diff changeset
37 local xmlns = child.attr.xmlns or "jabber:client";
ca39f9b4cc8e mod_iq: Use "jabber:client" when the stanza payload namespace is nil.
Waqas Hussain <waqas20@gmail.com>
parents: 4760
diff changeset
38 local ret = module:fire_event("iq/bare/"..xmlns..":"..child.name, data);
3656
71fd1582f01b mod_iq: Extra IQ get and set sub-events are now fired: "iq-{get,set}/{host,self,bare}/xmlns:tag" (when "iq/{host,self,bare}/xmlns:tag" is unhandled).
Waqas Hussain <waqas20@gmail.com>
parents: 3647
diff changeset
39 if ret ~= nil then return ret; end
4870
ca39f9b4cc8e mod_iq: Use "jabber:client" when the stanza payload namespace is nil.
Waqas Hussain <waqas20@gmail.com>
parents: 4760
diff changeset
40 return module:fire_event("iq-"..type.."/bare/"..xmlns..":"..child.name, data);
1260
04c1fae0eb03 mod_iq: Fire sub-events for IQs directed at bare JIDs and hosts
Waqas Hussain <waqas20@gmail.com>
parents: 1234
diff changeset
41 else
3673
43b854062206 mod_iq: Don't stop event dispatch for unhandled IQ errors and results (this lets negative priority handlers intercept the events).
Waqas Hussain <waqas20@gmail.com>
parents: 3658
diff changeset
42 return module:fire_event("iq-"..type.."/bare/"..stanza.attr.id, data);
1260
04c1fae0eb03 mod_iq: Fire sub-events for IQs directed at bare JIDs and hosts
Waqas Hussain <waqas20@gmail.com>
parents: 1234
diff changeset
43 end
1233
4c8c3d7d9c27 mod_iq: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 end);
4c8c3d7d9c27 mod_iq: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45
2686
d0d38fcaade0 mod_iq: Fire sub-events for iq/self events.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
46 module:hook("iq/self", function(data)
8728
41c959c5c84b Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
47 -- IQ to self JID received
4760
55501fc4394b mod_iq: Remove unused import of jid.split, bare_sessions and don't unpack event.origin when it isn't used. Waqas.
Matthew Wild <mwild1@gmail.com>
parents: 3679
diff changeset
48 local stanza = data.stanza;
3657
07f0c2ef16cb mod_iq: Optimized a bit (fewer table accesses).
Waqas Hussain <waqas20@gmail.com>
parents: 3656
diff changeset
49 local type = stanza.attr.type;
2686
d0d38fcaade0 mod_iq: Fire sub-events for iq/self events.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
50
3657
07f0c2ef16cb mod_iq: Optimized a bit (fewer table accesses).
Waqas Hussain <waqas20@gmail.com>
parents: 3656
diff changeset
51 if type == "get" or type == "set" then
3658
13600f0d56b5 mod_iq: Optimized a bit more (fewer table accesses).
Waqas Hussain <waqas20@gmail.com>
parents: 3657
diff changeset
52 local child = stanza.tags[1];
4870
ca39f9b4cc8e mod_iq: Use "jabber:client" when the stanza payload namespace is nil.
Waqas Hussain <waqas20@gmail.com>
parents: 4760
diff changeset
53 local xmlns = child.attr.xmlns or "jabber:client";
ca39f9b4cc8e mod_iq: Use "jabber:client" when the stanza payload namespace is nil.
Waqas Hussain <waqas20@gmail.com>
parents: 4760
diff changeset
54 local ret = module:fire_event("iq/self/"..xmlns..":"..child.name, data);
3656
71fd1582f01b mod_iq: Extra IQ get and set sub-events are now fired: "iq-{get,set}/{host,self,bare}/xmlns:tag" (when "iq/{host,self,bare}/xmlns:tag" is unhandled).
Waqas Hussain <waqas20@gmail.com>
parents: 3647
diff changeset
55 if ret ~= nil then return ret; end
4870
ca39f9b4cc8e mod_iq: Use "jabber:client" when the stanza payload namespace is nil.
Waqas Hussain <waqas20@gmail.com>
parents: 4760
diff changeset
56 return module:fire_event("iq-"..type.."/self/"..xmlns..":"..child.name, data);
2686
d0d38fcaade0 mod_iq: Fire sub-events for iq/self events.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
57 else
3673
43b854062206 mod_iq: Don't stop event dispatch for unhandled IQ errors and results (this lets negative priority handlers intercept the events).
Waqas Hussain <waqas20@gmail.com>
parents: 3658
diff changeset
58 return module:fire_event("iq-"..type.."/self/"..stanza.attr.id, data);
2686
d0d38fcaade0 mod_iq: Fire sub-events for iq/self events.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
59 end
d0d38fcaade0 mod_iq: Fire sub-events for iq/self events.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
60 end);
d0d38fcaade0 mod_iq: Fire sub-events for iq/self events.
Waqas Hussain <waqas20@gmail.com>
parents: 1522
diff changeset
61
1233
4c8c3d7d9c27 mod_iq: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 module:hook("iq/host", function(data)
8728
41c959c5c84b Fix spelling throughout the codebase [codespell]
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
63 -- IQ to a local host received
4760
55501fc4394b mod_iq: Remove unused import of jid.split, bare_sessions and don't unpack event.origin when it isn't used. Waqas.
Matthew Wild <mwild1@gmail.com>
parents: 3679
diff changeset
64 local stanza = data.stanza;
3657
07f0c2ef16cb mod_iq: Optimized a bit (fewer table accesses).
Waqas Hussain <waqas20@gmail.com>
parents: 3656
diff changeset
65 local type = stanza.attr.type;
1233
4c8c3d7d9c27 mod_iq: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66
3657
07f0c2ef16cb mod_iq: Optimized a bit (fewer table accesses).
Waqas Hussain <waqas20@gmail.com>
parents: 3656
diff changeset
67 if type == "get" or type == "set" then
3658
13600f0d56b5 mod_iq: Optimized a bit more (fewer table accesses).
Waqas Hussain <waqas20@gmail.com>
parents: 3657
diff changeset
68 local child = stanza.tags[1];
4870
ca39f9b4cc8e mod_iq: Use "jabber:client" when the stanza payload namespace is nil.
Waqas Hussain <waqas20@gmail.com>
parents: 4760
diff changeset
69 local xmlns = child.attr.xmlns or "jabber:client";
ca39f9b4cc8e mod_iq: Use "jabber:client" when the stanza payload namespace is nil.
Waqas Hussain <waqas20@gmail.com>
parents: 4760
diff changeset
70 local ret = module:fire_event("iq/host/"..xmlns..":"..child.name, data);
3656
71fd1582f01b mod_iq: Extra IQ get and set sub-events are now fired: "iq-{get,set}/{host,self,bare}/xmlns:tag" (when "iq/{host,self,bare}/xmlns:tag" is unhandled).
Waqas Hussain <waqas20@gmail.com>
parents: 3647
diff changeset
71 if ret ~= nil then return ret; end
4870
ca39f9b4cc8e mod_iq: Use "jabber:client" when the stanza payload namespace is nil.
Waqas Hussain <waqas20@gmail.com>
parents: 4760
diff changeset
72 return module:fire_event("iq-"..type.."/host/"..xmlns..":"..child.name, data);
1260
04c1fae0eb03 mod_iq: Fire sub-events for IQs directed at bare JIDs and hosts
Waqas Hussain <waqas20@gmail.com>
parents: 1234
diff changeset
73 else
3673
43b854062206 mod_iq: Don't stop event dispatch for unhandled IQ errors and results (this lets negative priority handlers intercept the events).
Waqas Hussain <waqas20@gmail.com>
parents: 3658
diff changeset
74 return module:fire_event("iq-"..type.."/host/"..stanza.attr.id, data);
1260
04c1fae0eb03 mod_iq: Fire sub-events for IQs directed at bare JIDs and hosts
Waqas Hussain <waqas20@gmail.com>
parents: 1234
diff changeset
75 end
1233
4c8c3d7d9c27 mod_iq: Initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 end);