2
0
mirror of https://github.com/msantos/xmppipe synced 2024-11-13 13:10:32 +00:00
Commit Graph

117 Commits

Author SHA1 Message Date
Michael Santos
417176cddb tests: add some basic tests
Check the the basic functionality of xmppipe:

    # https://github.com/sstephenson/bats
    # apt-get install bats
    bats test
2017-02-11 10:35:01 -05:00
Michael Santos
7f0b5863c0 handle_stdin: use fd for nfds 2017-02-10 10:18:01 -05:00
Michael Santos
15926183a6 sandbox/seccomp: add more syscalls 2017-02-07 15:20:29 -05:00
Michael Santos
25f3441b33 README: add information about sandbox 2017-02-06 10:03:06 -05:00
Michael Santos
4a440def98 Enforce sandboxing 2017-02-05 09:18:56 -05:00
Michael Santos
2bf9415683 sandbox: enable capabilities sandbox on FreeBSD 2017-02-04 09:00:49 -05:00
Michael Santos
707d7cf19d Display enforced sandbox in verbose mode 2017-02-03 09:47:12 -05:00
Michael Santos
5917d03137 sandbox: Linux seccomp syscall filter
Add a BPF seccomp syscall filter on Linux. Not enabled by default. To
compile:

    XMPPIPE_SANDBOX=XMPPIPE_SANDBOX_SECCOMP make

The sandbox is derived from OpenSSH's seccomp sandbox by Will Drewry and
Kees Cook's tutorial on seccomp:

    http://outflux.net/teach-seccomp/
2017-02-02 10:13:33 -05:00
Michael Santos
c346c863e4 sandbox: set number of allowed fd's
The number of file descriptors enforced by setrlimit() can now be set at
compile time using a flag. The flag defaults to 0 on Linux and -1
everywhere else:

    XMPPIPE_SANDBOX=XMPPIPE_SANDBOX_RLIMIT \
    XMPPIPE_SANDBOX_RLIMIT_NOFILE=-1 \
    make

The meaning of the XMPPIPE_SANDBOX_RLIMIT_NOFILE is:

* -1 : set rlim_cur/rlim_max to the lowest allocated file desciptor

* >=0: set rlim_cur/rlim_max to this number

On some platforms, setting rlim_cur below the value of the highest
allocated fd may interfere with polling. See commit a34d5766c5 for
details.
2017-02-01 10:25:38 -05:00
Michael Santos
a34d5766c5 sandbox: basic rlimit sandbox
The rlimit sandbox disables forking processes and opening files.

The rlimit sandbox is not used by default yet. To compile it:

    XMPPIPE_SANDBOX=XMPPIPE_SANDBOX_RLIMIT make

The rlimit sandbox should work on any platform. However the interaction
of RLIMIT_NOFILE with poll(2) (and select(2)?) on some platforms (FreeBSD
but really any OS besides Linux) is problematic:

* opening a number of fd's, setting RLIMIT_NOFILE to 0, calling
  poll(2) on the fdset

  Linux: works
  FreeBSD: fails

* opening a number of fd's, setting RLIMIT_NOFILE to maxfd+1, calling
  poll(2) on the fdset

  Linux: works
  FreeBSD: works

The issue with the second option is that a library may have opened a
sequence of file descriptors then closed the lower numbered fd's:

    open() => 3
    open() => 4
    open() => 5
    close(3)
    close(4)
    maxfd = 5

RLIMIT_NOFILE would be set to 6 (stdin, stdout, stderr, 3, 4, 5) and the
sandbox would allow re-opening fd's 3 and 4.

One possible fix would be to run through the sequence of fd's before
entering the rlimit sandbox:

* test if the fd is closed
* if the fd is closed, dup2(STDIN_FILENO, fd)

Since the closed fd's are not part of the pollset, they will not be
polled and should be ignored.

Note we can't simply move maxfd to the lowest unused fd because
libstrophe maintains the fd number as internal, opaque state.

Empirically, the xmpp fd is always 3. Another option would be to abort
the process if the fd does not equal 3.
2017-01-31 08:17:02 -05:00
Michael Santos
cc665538cb sandbox: stdio mode using pledge(2) on OpenBSD 2017-01-30 10:17:54 -05:00
Michael Santos
a7d0ca7e47 Initial support for sandboxing
Prepare for sandboxing the xmppipe process by adding a function called
after all file descriptors are allocated.

The intent of the sandbox is to limit the xmppipe process to the role
of a component in a shell pipeline: reading from stdin, reading/writing
to the XMPP socket and writing to stdout. Any activity not involved with
using stdio should force the process to exit.

The sandbox function will vary based on the capabilities of the
platform. The default sandbox function does nothing.

Limitations of the sandbox:

Probably the biggest risk is in session establishment:
* the TLS handshake
* the XML parsing

The sandbox is enforced after the TLS connection is established, i.e.,
after the file descriptor for the XMPP session is allocated and so has no
effect on the TLS handshake or the initial XMPP handshake.

Possibly an initial sandbox could be setup for the connection phase
followed by a stricter sandbox for the stdio phase.
2017-01-29 09:44:12 -05:00
Michael Santos
7cf7562bb1 Update readme 2017-01-26 10:32:10 -05:00
Michael Santos
eef6074dd5 Add a LICENSE file
Uses the ISC license. License is also in the source code.
2017-01-25 07:58:34 -05:00
Michael Santos
e20bca9bd1 const'ify all the things 2017-01-18 06:31:24 -05:00
Michael Santos
550eaf4e59 Check message id has been allocated 2016-10-28 10:14:40 -04:00
Michael Santos
04c05bd5f2 xmppipe: avoid memory leak from duplicate options 2016-10-08 11:10:15 -04:00
Michael Santos
ee32002c2f ssh-over-xmpp: clean up example 2016-09-23 10:07:22 -04:00
Michael Santos
04f0641df1 Add example of terminal sharing using script(1) 2016-09-18 08:35:30 -04:00
Michael Santos
9410df9d78 bot.sh: clean up 2016-09-17 10:24:50 -04:00
Michael Santos
34efc88484 Mention tested XMPP servers 2016-09-15 10:34:39 -04:00
Michael Santos
2f2805d68a stdin: combine read error check 2016-09-09 06:57:49 -04:00
Michael Santos
16f03deff4 Fix typo 2016-09-07 10:22:58 -04:00
Michael Santos
877ecd5619 Flush stdout after print 2016-09-06 08:15:25 -04:00
Michael Santos
1e16b16c74 encoding: allow '@' and '/'
To make the JID easier to read, do not encode @ and /. Probably all the
RFC 3986 reserved characters can be passed through.
2016-09-05 09:04:14 -04:00
Michael Santos
07174101b4 encoding: remove useless lookup, sprintf 2016-09-04 09:45:20 -04:00
Michael Santos
1426be5902 Update README 2016-09-03 09:39:57 -04:00
Michael Santos
def456835b Add README 2016-09-02 12:08:11 -04:00
Michael Santos
072e8542ae alloc: log sizes on error 2016-06-27 07:08:51 -04:00
Michael Santos
7d4672a99a enomem: log function name
xmppipe occasionally crashes with an "allocation failure" message. Log
the function name for debugging.
2016-06-17 09:56:03 -04:00
Michael Santos
e96427bb36 Fix signed/unsigned integer comparison 2016-03-21 10:14:53 -04:00
Michael Santos
a7717f381e Add compiler hardening flags 2016-02-03 10:17:31 -05:00
Michael Santos
fc09ca4ff1 Be explicit with checks 2015-07-21 09:44:08 -04:00
Michael Santos
d0792db2f8 darwin/sunos: include uuid.h 2015-07-07 09:00:28 -04:00
Michael Santos
a392e836a0 Compile on BSDs
Use uuid_create(3) and uuid_to_string(3) to create the message id on
BSDs. Only tested on FreeBSD but should work on OpenBSD and NetBSD.

Add untested support for compiling on Solaris and Mac OS X:

* SmartOS has libuuid installed by default with rsyslog via pkgsrc

* Mac OS X has libuuid as part of libSystem:

http://lists.apple.com/archives/unix-porting/2009/Aug/msg00006.html
2015-07-01 09:31:44 -04:00
Michael Santos
c814208bad Remove check for negative unsigned integer O.o 2015-06-30 12:19:20 -04:00
Michael Santos
69f97cd872 Add "to" field to message
Distinguish which user received a message. Possibly useful if multiple
sessions are logged in or someone directly messages the bot.
2015-06-29 10:29:44 -04:00
Michael Santos
4310fe0e6b Exit when disconnected 2015-06-28 10:34:41 -04:00
Michael Santos
1a680daa48 example: read in larger chunks of data
Lengthen the poll delay to read in larger chunks of data. With a poll
value of 1 ms, there could be a large protocol message overhead: 1
character per XML stanza.
2015-06-15 16:55:41 -04:00
Michael Santos
5b7c7a1b64 example: tunnel SSH over XMPP
Server (system with access to the SSH and XMPP server):

 # ssh-over-xmpp server <conference> <IP address> <port>
 ssh-over-xmpp server sshxmpp 1.2.3.4 22

Client (system with access to the XMPP server):

 ssh -o ProxyCommand="ssh-over-xmpp client sshxmpp" 127.0.0.1
2015-06-13 09:13:27 -04:00
Michael Santos
633bc390cb Test string is NULL, not first character of string 2015-06-12 10:16:56 -04:00
Michael Santos
e82fc91bfb xmpp_stanza_get_name: check for NULL 2015-06-11 09:55:54 -04:00
Michael Santos
37bd93160b Reset ping failures after any response
Use a fixed ping handler rather than scheduling a ping handler per ping
request.

If the XMPP client or server is busy, many outstanding ping requests
can be waiting, resulting in a number of ping handlers being queued.
Each of the ping handlers does the same thing (resets the failure count
to 0) but any dropped pings resulted in the handlers being queued
forever.
2015-06-10 07:49:21 -04:00
Michael Santos
c3c83f9d72 Lower the default buffer size
For ejabberd and the mongooseim fork of ejabbered, 4097 bytes is around
the maximum message size before messages are dropped (the extra byte is
for libstrophe to put the terminating NULL. Otherwise, with a buffer
of 4096, the message will be broken up into 2 parts, with the second
consisting of the last byte of the message).

With base64 encoding, the message size will increase to 4097 * 4 / 3
bytes. When sending streams of binary data (cat'ing a file over XMPP,
ssh over XMPP, ...), stream management will get backlogged and messages
will disappear.

So halve the default buffer size. Is it too big for other jabber
servers? It is one of the mysteries but the "-b" option exists for those.
2015-06-03 10:08:37 -04:00
Michael Santos
1975057b6d -m -> -b
Rename the buffer size switch to something more memorable. Add a missing
break in the option processing switch statement.
2015-06-02 08:17:28 -04:00
Michael Santos
4c69352f19 stream management: flow control
Implement flow control by monitoring the number of unacked stream
requests and the number of unprocessed stanzas. If either exceeds a user
controlled limit, xmppipe will stop reading from stdin. Further stream
requests will be generated whenever the keepalive timeout is reached and
an XMPP ping is sent.

ejabberd/mongooseim will close the connection if there are about 5
unacked stream requests. xmppipe should probably do the same thing.

With these changes, binaries can be reliably (but slowly) sent over
XMPP. ssh over XMPP is also working reliably (with light testing). Because
of the increase in size due to base64 encoding, the buffer size needs
to be reduced so the encoded message is under 4k (-m 1024 is ok).
2015-06-01 09:39:22 -04:00
Michael Santos
5c14db6448 sm: request ack for every stanza
When stdin is closed with stream management enabled, xmppipe waits for
the ack to match the number of stanzas sent to ensure all the data has
been flushed to the server.

The server may not respond to every stream request and a stream request
message is generated only when a stanza is sent. Since stdin is closed,
the only traffic is the XMPP pings which are sent out every 60 seconds
by default. The result is, when stdin is closed, it may take several
minutes for xmppipe to exit.

Work around the problem by sending a stream management request for every
stanza.
2015-05-31 08:56:04 -04:00
Michael Santos
147e0d7c2b Consistenly check xmpp get ops for NULL values 2015-05-30 10:41:36 -04:00
Michael Santos
c4499fa4df example: clean up the bot script 2015-05-27 10:55:05 -04:00
Michael Santos
16ec036134 Fix format warnings 2015-05-26 07:43:45 -04:00