The code-path which checks whether the server requires an "xmpp-session"
was untested and therefore the connection-callback handler was triggered
twice with the state `XMPP_CONN_CONNECT`. This happened if the server
supports stream-management and requires a session.
Reported via https://github.com/profanity-im/profanity/issues/1954
Fixup of c7d410f38b
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
`rc` must be reset in order to make the `do-while` loop work in case
there's no `sockopt_cb` set.
Fixup of f3460460e9
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
Introduce a `conn_interface` to simplify the decision logic which API
we must call.
This also fixes some bugs of the previous commit.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
* Delay the notification of the library user that the connection was
successful, until SM is reported by the server as enabled.
* Clear the SM queue in case resumption failed.
* Improve some debug statements & comments.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
Instead of setting each element individually to 0 or NULL, use memset.
This also fixes the missing initialization of `conn->sm_disable`.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
In case a connection is established, but the stream negotiation has not
completed yet, a user may think that they can start sending data,
but this is not the case.
All of the following quotes originate in RFC6120.
As of Ch. 8:
> After a client and a server (or two servers) have completed stream
> negotiation, either party can send XML stanzas.
As of Ch. 7.3.1:
> The parties to a stream MUST consider resource binding as
> mandatory-to-negotiate.
As of Ch. 6.3.1:
> The parties to a stream MUST consider SASL as mandatory-to-negotiate.
As of Ch. 4.3.5:
> The initiating entity MUST NOT attempt to send XML stanzas to entities
> other than itself (i.e., the client's connected resource or any other
> authenticated resource of the client's account) or the server to which
> it is connected until stream negotiation has been completed.
> Even if the initiating entity does attempt to do so, the receiving
> entity MUST NOT accept such stanzas and MUST close the stream with a
> <not-authorized/> stream error [...]
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
Before this change we couldn't differentiate between this instance
and the one further down in the code.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
... also make it possible to pre-define `STROPHE_MESSAGE_BUFFER_SIZE` in
a custom build if someone doesn't want such a huge stack buffer.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
While the struct is still public in all versions of OpenSSL and its
forks, this may well change in the future. Prepare for that case and
use the X509_ALGOR_get0() accessor which has been available with this
signature since OpenSSL 1.1.
It has been pointed out that the wording of the license of this library is
not entirely clear.
The term "dual licensing" usually refers to a licence choice of two
licenses "LICENSE1 _or_ LICENSE2.
Instead the license of this library claimed "LICENSE1 _and_ LICENSE2".
After an internal discussion with @metajack and @pasis it was made clear
that the initial idea was to dual license the library in the usual way.
This was also made clear by jack on the ML in the past [0].
As of jack, these licensing terms originated from jquery, which also used
the 'and' version in the past and has since been corrected [1].
This patch changes the license terms to 'MIT or GPLv3' and also adds SPDX
headers [2].
[0] https://groups.google.com/g/libstrophe/c/JkFgr601JQc
[1] https://stackoverflow.com/q/2758409
[2] https://spdx.org
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
Break the linked-list of children before releasing a child.
Before this patch it was possible, when a child is cloned and stored
for longer than the lifetime of its parent, that its `next` pointer
points to invalid memory that was already free'd when the parent stanza
was released.
This issue exists already since the initial version of
`xmpp_stanza_release()`.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
This patch handles the case where the server sends its list of features,
but the `<bind>` feature is missing.
A server doing so is violating RFC6120 (c.f. [0]), but it happened in [1].
Previously we ended up in a segfault, now we terminate the connection.
Reproducing this was done with netcat and profanity:
```
shell1 $ nc -l -p 5222
profanity $ /connect foo@127.0.0.1 tls disable
-> nc receives: '<?xml version="1.0"?><stream:stream to="127.0.0.1" xml:lang="en" version="1.0" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">'
nc send: <?xml version="1.0"?>
<stream:stream from="127.0.0.1" id="foobarbaz" lang="en" version="1.0" xmlns:stream="http://etherx.jabber.org/streams">
<features xmlns="http://etherx.jabber.org/streams"><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism></mechanisms></features>
-> nc receives: '<auth mechanism="PLAIN" xmlns="urn:ietf:params:xml:ns:xmpp-sasl">XXXXXXXXXX</auth>'
nc send: <success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
-> nc receives: '<?xml version="1.0"?><stream:stream to="127.0.0.1" xml:lang="en" version="1.0" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams"><stream:stream from="127.0.0.1" id="foobarbaz" lang="en" version="1.0" xmlns:stream="http://etherx.jabber.org/streams">'
nc send: <?xml version="1.0"?>
<stream:stream from="127.0.0.1" id="foobarbaz" lang="en" version="1.0" xmlns:stream="http://etherx.jabber.org/streams">
<features xmlns="http://etherx.jabber.org/streams"/>
-> pre-patch this lead to a segfault of profanity, now the stream gets closed.
```
[0] https://datatracker.ietf.org/doc/html/rfc6120#section-7.2
[1] https://github.com/profanity-im/profanity/issues/1849
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
libstrophe uses non-blocking sockets and the connect() syscall may
return before a TCP connection is established. This doesn't allow
to catch all possible errors synchronously and some of the errors
are handled in the event handler.
In a scenario with multiple SRV records and/or multiple IP addresses
resolution, we need to repeat connection attempt on a failure.
xmpp_sock_t resolves the above problem. It keeps resolved records and
addresses to repeat connect attempt asynchronously.
The resolver maybe returned an error code but allocated an RR list. Free
this list now in the resolver instead of depending on the user to free it
even though there was an error.
This API is only library internal, but still it makes more sense like that.
This issue was discovered in CI job [0].
[0] https://github.com/strophe/libstrophe/actions/runs/3874980845/jobs/6776501943
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
If functions `xmpp_conn_set_cafile()` or `xmpp_conn_set_capath()` are
called twice they leaked the previously allocated memory.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
Instead of using the "cloning version" `xmpp_stanza_add_child()` use the
version that takes ownership, so we don't have to release directly
afterwards.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
Instead of always releasing the stanza after calling `send_stanza()`, this
is done inside the function now.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
... and forgetting to 1. clear the password cache and 2. print a message
of what exactly happened ...
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
Prevent assuming SM is enabled if the connection gets re-used without a
complete destroy&re-create of the connection object.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
When compiling on buildroot I get:
`src/tls.c:235:28: error: ‘ssize_t’ undeclared (first use in this
function); did you mean ‘size_t’? `
To have this POSIX type available we will need to include
`sys/types.h` or `unistd.h`.
Before this change the user had to provide a password callback, even if the
PKCS#12 encoded file has no or an empty password.
This changes the behavior, so we first try to open the file without a
password and only then ask the user to provide one.
This fixes#204.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
In order to be able to distinguish between user-created entries and
library-internal entries, we mark each entry who it belongs to.
This allows then later to let the user manage only the queue entries
that were created by them.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
The cache is stored per connection object and is cleared on
* entry of wrong password
* release of connection object
* successful connection
It can be configured that libstrophe retries the password entry in case
the user entered a wrong password.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
In order to be able to load password-protected key files a password
callback was added.
This also adds support for PKCS#12 containers instead of certificate+key.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
The registered callback function will be called before connect() is
called on the socket, allowing tweaking of much more than just keepalive
settings.
This deprecates xmpp_conn_set_keepalive().
Includes helper/example callback function to set default keepalive
parameters that some library users may find useful.
In order to do proper deprecation of those API's we re-introduce them
so they can be removed in the next release. This will then also lead to
an ABI bump.
Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>