Commit graph

13803 commits

Author SHA1 Message Date
Martijn van Duren
e7f77807c9 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.
2025-02-06 15:04:38 +00:00
Matthew Wild
0a93dccaa9 prosodyctl: Further deprecate start/stop/restart commands when installed
Despite the warning we introduced, many people continue to try using
prosodyctl to manage Prosody in the presence of systemctl (e.g. #1688).

Also, despite the warning, prosodyctl proceeded with the operation. This means
the commands could be invoked by accident, and cause a situation that is hard
to recover from (needing to manually track down stray processes).

This commit disables all the problematic commands by default, but this can
still be overridden using --force or via a config option.

We only perform this check when we believe Prosody has been "installed" for
system-wide use (i.e. running it from a source directory is still supported).
2025-02-06 14:51:31 +00:00
Matthew Wild
08d69fb759 prosodyctl: reload: Assume reload-via-socket if any arguments passed 2025-02-06 14:47:45 +00:00
Matthew Wild
24d50902bd prosodyctl: Fix luacheck warning 2025-02-06 14:46:36 +00:00
Matthew Wild
3e23058a2d prosodyctl: reload: Remove redundant require 2025-02-06 14:45:56 +00:00
Matthew Wild
68fa291829 util.prosodyctl: Add comments to explain logic and expected behaviour (#1688) 2025-02-06 14:42:18 +00:00
Matthew Wild
60460d4d22 prosodyctl: stop: Fix detection of whether Prosody is running 2025-02-06 14:35:29 +00:00
Matthew Wild
b3d9577ceb prosodyctl: reload: Fix detection of whether prosody is running
isrunning() returns two values (success, status) and we were only checking the
first one.
2025-02-06 14:34:47 +00:00
Matthew Wild
f2e87307a4 prosodyctl: reload: use admin socket to issue reload command, if available 2025-02-06 14:24:16 +00:00
Matthew Wild
1377333751 util.prosodyctl.shell: Export function to check for availability of admin socket 2025-02-06 14:23:15 +00:00
Matthew Wild
ba53fa2e21 mod_c2s: Add debug log when disconnecting all user sessions 2025-02-06 12:40:11 +00:00
Kim Alvefur
56c7957a56 mod_admin_shell: Fix column alignment in 'help roles' 2025-02-02 14:12:52 +01:00
Kim Alvefur
084774ae50 mod_admin_shell: Fix help forgetting arguments
The array:pluck() method mutates the args, replacing the table items
with the resulting strings. On later runs I assume it tries to index the
string, which returns nil, emptying the array.
2025-02-02 13:33:17 +01:00
Kim Alvefur
885164b2b1 util.startup: Drop mention of systemd from notification socket handling
This does not have to be specific to systemd
2025-02-01 20:42:59 +01:00
Rémi Bardon
9732b0f9d8 mod_tokenauth: Fix expiry lasting one second too much
Because the code was using `< now` in a lot of places, things expiring at the current second
wouldn't be marked as expired. It isn't noticeable in real-world scenarios but I wanted to
create OAuth 2.0 tokens valid for 0 second in integration tests and it wasn't possible.

By using `<=` instead of `<`, we make sure tokens don't live a single millisecond more than
what they are supposed to.
2025-02-01 20:45:28 +01:00
Matthew Wild
642c1c0ab5 Merge 0.12->trunk 2025-01-28 09:18:18 +00:00
Kim Alvefur
f45cf55bcb doap: Add XEP-0357 (mod_cloud_notify) 2025-01-27 12:23:10 +01:00
Kim Alvefur
edfe2fe98c mod_storage_sql: Don't treat Enter or Ctrl-C as yes in upgrade command
It seems to imply that No is the default, so it shouldn't continue doing
the Yes action unless you actually press Y
2025-01-23 22:32:44 +01:00
Kim Alvefur
c8134dd9a9 mod_storage_sql: Detect SQLite3 without UPSERT (or SQLCipher 3.x)
SQLCipher v3.4.1 (the version in Debian 12) is based on SQLite3 v3.15.2,
while UPSERT support was introduced in SQLite3 v3.24.0

This check was not needed before because we v3.24.0 has not been in a
version of Debian we support for a long, long time.

Note however that SQLCipher databases are not compatible across major
versions, upgrading from v3.x to v4.x requires executing a migration.
Attempts at making `prosodyctl mod_storage_sql upgrade` perform such a
migration has not been successful.

Executing the following in the `sqlcipher` tool should do the migration:

	PRAGMA key = '<key material>';
	PRAGMA cipher_migrate;
2025-01-23 19:33:05 +01:00
Kim Alvefur
654edd37bb util.sql: SQLCipher support
This enables use of encrypted databases if LuaDBI or LuaSQLite3 has been
linked against SQLCipher. Using `LD_PRELOAD` may work as well.

Requires SQLCipher >= 4.0.0 due to the use of UPSERT
2025-01-23 16:38:56 +01:00
Kim Alvefur
2d4ea8ac9b core.configmanager: Remove compat
I have updated my config now, have you? :)
2025-01-21 17:21:48 +01:00
Kim Alvefur
97884eb956 util.startup: Rename credentials path variable too 2025-01-18 13:29:59 +01:00
Kim Alvefur
9db7569abc luacheck: Declare new config globals 2025-01-18 12:33:18 +01:00
Kim Alvefur
059d6457e0 core.configmanager: Rename Secret to Credential
To match the variable name and what systemd calls them.
2025-01-18 12:31:48 +01:00
Kim Alvefur
41a360ce2a core.configmanager: Add function for getting secrets from separate files
Idea is to enable easily retrieving of secret values from files outside
of the config, e.g. via the method used by systemd credentials.

CREDENTIALS_DIRECTORY is expected to be set by the process manager
invoking Prosody, so being unset and unavailable from prosodyctl is
going to be normal and a warning is reported in that case. Care will
have to be taken to make it clear that prosodyctl check will not work
with such values. An error is thrown if the directory is unavailable
when running under Prosody.
2025-01-16 15:21:34 +01:00
Kim Alvefur
2f37c443b8 core.configmanager: Add ways to read config values from files
Inspired by something MattJ said

Allows retrieving config values from files which are expected to be
relative to the config directory, extending on the ENV_ method of
retrieving config values from outside the config file.

- FileLine retrieves the first line, stripping any trailing newline
- FileContents reads the whole file
- FileLines reads lines into an array
2025-01-16 15:05:00 +01:00
Matthew Wild
76f00fc2e3 mod_admin_shell: Remove log statement intended for development only 2025-01-16 11:35:19 +00:00
Kim Alvefur
b53c7b03aa util.bitcompat: Avoid potential noise from global metatable
If this module is require'd from a plugin on a Lua version without the
global bit32 library, it may trip a warning about accessing globals.
2025-01-15 09:55:28 +01:00
Matthew Wild
918e8f51e8 mod_pubsub: Remove duplicate create_node command
Removed the one without error handling.
2025-01-14 12:26:14 +00:00
Matthew Wild
cafbad2d01 features: Add feature for bundled mod_cloud_notify
Was supposed to be part of commit 2f38f3275a74.
2025-01-14 12:25:53 +00:00
Kim Alvefur
879cb1672f mod_authz_internal: Make host considered the parent configurable
This bestows the role specified by the 'host_user_role' setting onto
users of that host. For simplicity, only a single host can be specified.

Making it configurable allows for setups where VirtualHost and related
Components may be siblings instead of having a subdomain relationship.

For setups with many VirtualHosts sharing a single Component, the
'server_user_role' setting is more appropriate. Even more complicated
setups would have to resort to mod_firewall or similar.
2025-01-13 11:50:03 +01:00
Kim Alvefur
dc80c46504 luacheckrc: Add module.ready() 2025-01-10 12:34:29 +01:00
Kim Alvefur
dadd754f74 luacheckrc: Reorder module API methods for consistency with prosody-modules
Makes it easier to keep them in sync when the diff is smaller
2025-01-10 12:32:29 +01:00
Kim Alvefur
ea7bf939d5 util.signal: Factor out single #define that enables signalfd(2) use
Makes it easier to test by disabling this #define
2025-01-10 12:31:27 +01:00
Kim Alvefur
4919301ad4 util.signal: Implement signalfd for *BSD
The Lua hook based signal handling does not work correctly if signal
handling is setup in a coroutine. signalfd solves that in a nice way,
but is Linux-only.
2025-01-10 03:18:46 +01:00
Matthew Wild
c8d375af04 mod_cloud_notify: Merge from prosody-modules@fc521fb5ffa0
Many thanks to Thilo Molitor and Kim Alvefur for their work on this module
while it was in the community repository. It has been stable for some time, is
widely used, and provides a feature that is important to most deployments.
2025-01-09 16:49:27 +00:00
Matthew Wild
bde66f9436 modulemanager: Allow modules to specify supported Lua versions in metadata 2025-01-09 16:44:48 +00:00
Matthew Wild
f6ed03a903 modulemanager: Handle multiple digits in Lua version number
Lua has a slow release cycle, but it would be nice if Prosody doesn't break
unnecessarily with 5.10 :)
2025-01-09 16:06:53 +00:00
Matthew Wild
d0457564a3 mod_invites: Add support for invites_page option to use external invites pages
This allows Prosody to easily provide friendly invitation links, even without
setting up mod_invites_page (which is a community module). Admins can
configure it to use a third-party deployment such as https://xmpp.link or they
can deploy their own based on
https://github.com/modernxmpp/easy-xmpp-invitation

Alternatively they can just install mod_invites_page and this will all be
handled automatically by that.
2025-01-09 13:23:46 +00:00
Kim Alvefur
7288595789 prosodyctl: Hide the 'lua_paths' command from default command listing
All commands are called with a '-h' argument, but this one doesn't have
that. Since it's meant to be machine readable, hiding it seems
marginally more sensible than implementing '-h'.
2025-01-08 22:46:21 +01:00
Kim Alvefur
ede98f71e4 mod_storage_sql: Remove the word 'error' from debug messages
The word 'error' anywhere, especially in harmless debug messages, are
too often interpreted as fatal errors my some users, so best avoid that
word. These look too scary as it is, being tracebacks.
2025-01-08 22:30:30 +01:00
Matthew Wild
26eeda9ed6 prosodyctl: Add lua_paths command to print the LUA_PATH and LUA_CPATH used
This can be useful to run scripts and stuff with access to the same Lua paths
as Prosody uses.
2025-01-08 20:37:35 +00:00
Matthew Wild
d2b632dffc prosodyctl: 'about' becomes 'version -v', 'version' prints only version
The 'about' command remains for backwards compatibility, but hidden.
2025-01-08 20:35:05 +00:00
Kim Alvefur
f9731db67b mod_pubsub: Remove unused loop variable [luacheck] 2025-01-08 08:54:52 +01:00
Kim Alvefur
4569e951af util.prosodyctl.shell: Remove non-existent export [luacheck] 2025-01-08 08:53:43 +01:00
Kim Alvefur
83e27a61c8 prosodyctl: Remove unused import
Leftover after 854df134274f
2025-01-08 08:52:50 +01:00
Kim Alvefur
7ed9729e52 mod_admin_shell: Remove redundant 'mod_' prefix from debug message 2025-01-08 08:41:15 +01:00
Kim Alvefur
fe2c039c97 core.moduleapi: Include source modules when handling items
This improves consistency. Previously the 'source' field was only
provided in the original event when an item was added. It is used to
report the name of the module providing the item in a few places.

Also considered adding a new API to modulemanager returning a mapping
of items per module and then using that here.
2025-01-08 08:33:34 +01:00
Kim Alvefur
63690dd4a0 mod_admin_shell: Fix reporting origin module for commands
The field `_provided_by` comes from module:provides(), but these items
comes from moduel:add_item(), which include the originating module as a
'source' field of the event. However, this is absent when items are
retrieved at a later time than the initial event.
2025-01-08 08:11:02 +01:00
Kim Alvefur
0fe28ddb09 mod_pubsub: Quiet down check for service admin
This produced a *lot* of noise, especially listing items.
2025-01-07 23:37:41 +01:00