Annotate

plugins/mod_admin_socket.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 13592:34da8cc10b82
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10855
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 module:set_global();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local have_unix, unix = pcall(require, "socket.unix");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
12392
5373724e08a5 mod_admin_socket: Compat for luasocket prior to unix datagram support
Kim Alvefur <zash@zash.se>
parents: 10866
diff changeset
5 if have_unix and type(unix) == "function" then
12393
6966026262f4 mod_admin_socket: Comment on LuaSocket UNIX compat code
Kim Alvefur <zash@zash.se>
parents: 12392
diff changeset
6 -- COMPAT #1717
6966026262f4 mod_admin_socket: Comment on LuaSocket UNIX compat code
Kim Alvefur <zash@zash.se>
parents: 12392
diff changeset
7 -- Before the introduction of datagram support, only the stream socket
6966026262f4 mod_admin_socket: Comment on LuaSocket UNIX compat code
Kim Alvefur <zash@zash.se>
parents: 12392
diff changeset
8 -- constructor was exported instead of a module table. Due to the lack of a
6966026262f4 mod_admin_socket: Comment on LuaSocket UNIX compat code
Kim Alvefur <zash@zash.se>
parents: 12392
diff changeset
9 -- proper release of LuaSocket, distros have settled on shipping either the
6966026262f4 mod_admin_socket: Comment on LuaSocket UNIX compat code
Kim Alvefur <zash@zash.se>
parents: 12392
diff changeset
10 -- last RC tag or some commit since then.
12852
c35afa353f8f mod_admin_socket: Fix typo in comments
Kim Alvefur <zash@zash.se>
parents: 12418
diff changeset
11 -- Here we accommodate both variants.
12392
5373724e08a5 mod_admin_socket: Compat for luasocket prior to unix datagram support
Kim Alvefur <zash@zash.se>
parents: 10866
diff changeset
12 unix = { stream = unix };
5373724e08a5 mod_admin_socket: Compat for luasocket prior to unix datagram support
Kim Alvefur <zash@zash.se>
parents: 10866
diff changeset
13 end
10855
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 if not have_unix or type(unix) ~= "table" then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 module:log_status("error", "LuaSocket unix socket support not available or incompatible, ensure it is up to date");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 return;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12888
diff changeset
19 local server = require "prosody.net.server";
10855
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12888
diff changeset
21 local adminstream = require "prosody.util.adminstream";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12888
diff changeset
22 local st = require "prosody.util.stanza";
10855
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
10866
5265f7fe11dd mod_admin_socket: Use module API meant for file paths
Kim Alvefur <zash@zash.se>
parents: 10862
diff changeset
24 local socket_path = module:get_option_path("admin_socket", "prosody.sock", "data");
10855
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local sessions = module:shared("sessions");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local function fire_admin_event(session, stanza)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local event_data = {
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 origin = session, stanza = stanza;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 };
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local event_name;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if stanza.attr.xmlns then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 event_name = "admin/"..stanza.attr.xmlns..":"..stanza.name;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 else
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 event_name = "admin/"..stanza.name;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 module:log("debug", "Firing %s", event_name);
12887
68df46926c26 mod_admin_socket: Return error on unhandled input to prevent apparent freeze
Kim Alvefur <zash@zash.se>
parents: 12418
diff changeset
39 local ret = module:fire_event(event_name, event_data);
68df46926c26 mod_admin_socket: Return error on unhandled input to prevent apparent freeze
Kim Alvefur <zash@zash.se>
parents: 12418
diff changeset
40 if ret == nil then
68df46926c26 mod_admin_socket: Return error on unhandled input to prevent apparent freeze
Kim Alvefur <zash@zash.se>
parents: 12418
diff changeset
41 session.send(st.stanza("repl-result", { type = "error" }):text("No module handled this query. Is mod_admin_shell enabled?"));
68df46926c26 mod_admin_socket: Return error on unhandled input to prevent apparent freeze
Kim Alvefur <zash@zash.se>
parents: 12418
diff changeset
42 end
68df46926c26 mod_admin_socket: Return error on unhandled input to prevent apparent freeze
Kim Alvefur <zash@zash.se>
parents: 12418
diff changeset
43 return ret;
10855
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 module:hook("server-stopping", function ()
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 for _, session in pairs(sessions) do
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 session:close("system-shutdown");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 os.remove(socket_path);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 --- Unix domain socket management
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local conn, sock;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
13592
34da8cc10b82 mod_admin_socket: Fire event on admin client disconnect
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
57 local admin_server = adminstream.server(sessions, fire_admin_event);
34da8cc10b82 mod_admin_socket: Fire event on admin client disconnect
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
58 local listeners = admin_server.listeners;
34da8cc10b82 mod_admin_socket: Fire event on admin client disconnect
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
59
34da8cc10b82 mod_admin_socket: Fire event on admin client disconnect
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
60 module:hook_object_event(admin_server.events, "disconnected", function (event)
34da8cc10b82 mod_admin_socket: Fire event on admin client disconnect
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
61 return module:fire_event("admin-disconnected", event);
34da8cc10b82 mod_admin_socket: Fire event on admin client disconnect
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
62 end);
10855
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local function accept_connection()
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 module:log("debug", "accepting...");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 local client = sock:accept();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 if not client then return; end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 server.wrapclient(client, "unix", 0, listeners, "*a");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 function module.load()
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 sock = unix.stream();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 sock:settimeout(0);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 os.remove(socket_path);
12418
dd47adf74e93 mod_admin_socket: Improve error reporting when socket can't be created (fixes #1719)
Matthew Wild <mwild1@gmail.com>
parents: 12393
diff changeset
75 local ok, err = sock:bind(socket_path);
dd47adf74e93 mod_admin_socket: Improve error reporting when socket can't be created (fixes #1719)
Matthew Wild <mwild1@gmail.com>
parents: 12393
diff changeset
76 if not ok then
dd47adf74e93 mod_admin_socket: Improve error reporting when socket can't be created (fixes #1719)
Matthew Wild <mwild1@gmail.com>
parents: 12393
diff changeset
77 module:log_status("error", "Unable to bind admin socket %s: %s", socket_path, err);
dd47adf74e93 mod_admin_socket: Improve error reporting when socket can't be created (fixes #1719)
Matthew Wild <mwild1@gmail.com>
parents: 12393
diff changeset
78 return;
dd47adf74e93 mod_admin_socket: Improve error reporting when socket can't be created (fixes #1719)
Matthew Wild <mwild1@gmail.com>
parents: 12393
diff changeset
79 end
dd47adf74e93 mod_admin_socket: Improve error reporting when socket can't be created (fixes #1719)
Matthew Wild <mwild1@gmail.com>
parents: 12393
diff changeset
80 local ok, err = sock:listen();
dd47adf74e93 mod_admin_socket: Improve error reporting when socket can't be created (fixes #1719)
Matthew Wild <mwild1@gmail.com>
parents: 12393
diff changeset
81 if not ok then
dd47adf74e93 mod_admin_socket: Improve error reporting when socket can't be created (fixes #1719)
Matthew Wild <mwild1@gmail.com>
parents: 12393
diff changeset
82 module:log_status("error", "Unable to listen on admin socket %s: %s", socket_path, err);
dd47adf74e93 mod_admin_socket: Improve error reporting when socket can't be created (fixes #1719)
Matthew Wild <mwild1@gmail.com>
parents: 12393
diff changeset
83 return;
dd47adf74e93 mod_admin_socket: Improve error reporting when socket can't be created (fixes #1719)
Matthew Wild <mwild1@gmail.com>
parents: 12393
diff changeset
84 end
10862
1cfae9e85021 mod_admin_socket: Use wrapserver if available
Kim Alvefur <zash@zash.se>
parents: 10855
diff changeset
85 if server.wrapserver then
1cfae9e85021 mod_admin_socket: Use wrapserver if available
Kim Alvefur <zash@zash.se>
parents: 10855
diff changeset
86 conn = server.wrapserver(sock, socket_path, 0, listeners);
1cfae9e85021 mod_admin_socket: Use wrapserver if available
Kim Alvefur <zash@zash.se>
parents: 10855
diff changeset
87 else
1cfae9e85021 mod_admin_socket: Use wrapserver if available
Kim Alvefur <zash@zash.se>
parents: 10855
diff changeset
88 conn = server.watchfd(sock:getfd(), accept_connection);
1cfae9e85021 mod_admin_socket: Use wrapserver if available
Kim Alvefur <zash@zash.se>
parents: 10855
diff changeset
89 end
10855
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 function module.unload()
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 if conn then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 conn:close();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 if sock then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 sock:close();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 os.remove(socket_path);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end