2
0
mirror of https://github.com/msantos/xmppipe synced 2024-11-16 00:12:59 +00:00
xmppipe/README.md

512 lines
11 KiB
Markdown
Raw Normal View History

2022-01-31 13:27:59 +00:00
# SYNOPSIS
xmppipe [*options*] [*jid*]
# DESCRIPTION
xmppipe - stdio over XMPP
2016-09-02 16:08:11 +00:00
2016-09-03 13:39:57 +00:00
xmppipe redirects stdin/stdout in a shell pipeline to an XMPP MUC
2022-01-31 13:27:59 +00:00
(XEP-0045) or a one to one chat:
2016-09-02 16:08:11 +00:00
2022-01-31 13:27:59 +00:00
* supports flow control using stream management (XEP-0198)
* optionally deals with overload by acting as a circuit breaker or by
discarding messages
* works with line oriented tools like grep, sed and
awk by outputting each message as a newline terminated,
[percent-encoded](https://en.wikipedia.org/wiki/Percent-encoding) string
2016-09-02 16:08:11 +00:00
2016-09-15 14:34:39 +00:00
xmppipe can be used in shell scripts to quickly write interactive bots
for monitoring systems or for sending alerts.
2022-01-31 13:27:59 +00:00
# USAGE
2016-09-02 16:08:11 +00:00
xmppipe [*options*]
XMPPIPE_USERNAME=me@example.com
XMPPIPE_PASSWORD="password"
# default name: stdout-*hostname*-*uid*
xmpipe
xmppipe muc
xmppipe muc@example.com
2016-09-02 16:08:11 +00:00
2022-01-31 13:27:59 +00:00
# REQUIREMENTS
2016-09-03 13:39:57 +00:00
* [libstrophe](https://github.com/strophe/libstrophe)
libstrophe 0.9.2 or later is required for [TLS certificate
verification](https://github.com/strophe/libstrophe/issues/100).
2022-01-31 13:27:59 +00:00
# BUILD
2016-09-03 13:39:57 +00:00
$ make
2022-01-31 13:27:59 +00:00
## Tests
2017-02-22 15:27:02 +00:00
# Install bats:
# apt-get install bats
# git clone https://github.com/sstephenson/bats.git # or from git
make test
2022-01-31 13:27:59 +00:00
# PROCESS RESTRICTIONS
2017-02-06 15:03:06 +00:00
2020-09-12 11:03:43 +00:00
xmppipe restricts process operations at 2 points:
* a permissive "init" sandbox allowing network connections to the
XMPP server
2017-02-06 15:03:06 +00:00
2018-07-14 12:43:10 +00:00
* once the connection is established, a stricter "stdio" sandbox
2020-09-12 11:03:43 +00:00
limits the process to I/O operations
2017-02-06 15:03:06 +00:00
2020-09-12 11:03:43 +00:00
The effectiveness of the process restrictions depend on which mechanism
is used. By default:
2017-02-06 15:03:06 +00:00
* Linux:
2017-02-06 15:03:06 +00:00
* init: seccomp(2)
* stdio: seccomp(2)
* OpenBSD:
* init: pledge(2)
* stdio: pledge(2)
* FreeBSD:
* init: setrlimit(2)
* stdio: setrlimit(2)/capsicum(4)
2017-02-06 15:03:06 +00:00
* other: setrlimit(2)
* init: setrlimit(2)
* stdio: setrlimit(2)
2017-02-06 15:03:06 +00:00
2020-09-12 11:03:43 +00:00
Selecting which process restrictions are enforced is done at compile
time. For example, to use the "rlimit" process restrictions:
2017-02-06 15:03:06 +00:00
2020-09-12 11:03:43 +00:00
RESTRICT_PROCESS=rlimit make
2017-02-06 15:03:06 +00:00
2020-09-12 11:03:43 +00:00
If the process restrictions are interfering with normal operation, please
open an issue. To disable all process restrictions, compile using the
"null" sandbox:
2017-02-06 15:03:06 +00:00
2020-09-12 11:03:43 +00:00
RESTRICT_PROCESS=null make
2017-02-06 15:03:06 +00:00
2022-01-31 13:27:59 +00:00
# OPTIONS
2016-09-02 16:08:11 +00:00
-u, --username *JID*
2016-09-02 16:08:11 +00:00
: XMPP username: takes precedence over environment variable
-p, --password *password*
2016-09-02 16:08:11 +00:00
: XMPP password: takes precedence over environment variable
-r, --resource *resource*
2016-09-02 16:08:11 +00:00
: XMPP resource, used as the nickname in the MUC
-S, --subject *subject*
2016-09-02 16:08:11 +00:00
: XMPP MUC subject
2020-07-31 11:41:26 +00:00
-a, --address *address[:port]*
2016-09-02 16:08:11 +00:00
: Specify the IP address and port of the XMPP server
-F, --format *text:csv*
: stdin is text (default) or colon separated values
-d, --discard
2016-09-02 16:08:11 +00:00
: Discard stdin when MUC is empty
-D, --discard-to-stdout
2016-09-02 16:08:11 +00:00
: Discard stdin and print to local stdout
-e, --ignore-eof
2016-09-02 16:08:11 +00:00
: Ignore stdin EOF
-s, --exit-when-empty
2016-09-02 16:08:11 +00:00
: Exit when MUC is empty
-x, --base64
2016-09-02 16:08:11 +00:00
: Base64 encode/decode data
-b, --buffer-size *size*
2016-09-02 16:08:11 +00:00
: Size of read buffer
-I, --interval *interval*
2016-09-07 14:22:58 +00:00
: Request stream management status every interval messages
2016-09-02 16:08:11 +00:00
2019-05-07 12:13:47 +00:00
-k, --keepalive *seconds*
2016-09-02 16:08:11 +00:00
: Periodically send a keepalive
-K, --keepalive-failures *count*
2016-09-02 16:08:11 +00:00
: Number of keepalive failures before exiting
-P, --poll-delay *ms*
2016-09-02 16:08:11 +00:00
: Poll delay
-v, --verbose
2016-09-02 16:08:11 +00:00
: Increase verbosity
2020-07-04 11:21:01 +00:00
-V, --version
: Display version
--chat
: Use one to one chat
--no-tls-verify
: Disable TLS certificate verification
2022-01-31 13:27:59 +00:00
# ENVIRONMENT VARIABLES
XMPPIPE_USERNAME
: XMPP jid
XMPPIPE_PASSWORD
: XMPP password
# DECODING PERCENT-ENCODED STRINGS
2016-09-02 16:08:11 +00:00
Using bash:
~~~ shell
decode() {
printf '%b' "${1//%/\\x}"
}
2016-09-02 16:08:11 +00:00
~~~
2022-01-31 13:27:59 +00:00
# EXAMPLES
2016-09-02 16:08:11 +00:00
2022-01-31 13:27:59 +00:00
## Shell Bot
2016-09-02 16:08:11 +00:00
An interactive XMPP bot written in the shell:
~~~ shell
#!/bin/bash
set -o errexit
set -o nounset
2016-09-02 16:08:11 +00:00
set -o pipefail
trap cleanup EXIT
2016-09-02 16:08:11 +00:00
TMPDIR=$(mktemp -d)
in="$TMPDIR/stdin"
out="$TMPDIR/stdout"
2020-08-20 10:19:10 +00:00
mkfifo "$in"
mkfifo "$out"
2016-09-02 16:08:11 +00:00
cleanup() {
2020-08-20 10:19:10 +00:00
rm -rf "$TMPDIR"
2016-09-02 16:08:11 +00:00
}
decode() {
printf '%b' "${1//%/\\x}"
2016-09-02 16:08:11 +00:00
}
bot() {
while IFS=: read -r stanza type from to body; do
case "$stanza" in
m) ;;
p)
decode "$stanza:$type:$from:$to" 1>&2
echo 1>&2
continue
;;
*) continue ;;
esac
2020-08-20 10:19:10 +00:00
USER="$(decode "${from#*/}")"
MSG="$(decode "${body}")"
case $MSG in
*"has set the subject to:"*) ;;
"sudo make me a sandwich")
echo "$USER: you're a sandwich"
;;
sudo*)
echo "I'm sorry, $USER. I'm afraid I can't do that."
;;
uptime)
uptime
;;
exit)
echo "exiting ..."
exit 0
;;
*)
echo "$MSG"
;;
esac
2020-08-20 10:19:10 +00:00
done <"$out"
2016-09-02 16:08:11 +00:00
}
2020-08-20 10:19:10 +00:00
bot >"$in" &
xmppipe "$@" <"$in" >"$out"
2016-09-02 16:08:11 +00:00
~~~
2022-01-31 13:27:59 +00:00
## Sending Notifications/Alerts
2018-07-14 12:43:10 +00:00
Start `xmppipe` attached to a pipe:
~~~ shell
mkfifo /tmp/xmpp
2018-07-14 12:43:10 +00:00
xmppipe -o groupchat <>/tmp/xmpp
~~~
2018-07-14 12:43:10 +00:00
Any data written to the pipe will be sent to the groupchat:
~~~ shell
echo "test" >/tmp/xmpp
2020-08-21 12:12:54 +00:00
df -h >/tmp/xmpp
git diff >/tmp/xmpp
~~~
2022-01-31 13:27:59 +00:00
## SSH over XMPP
2016-09-02 16:08:11 +00:00
2016-09-03 13:39:57 +00:00
See [examples/ssh-over-xmpp](https://github.com/msantos/xmppipe/blob/master/examples/ssh-over-xmpp):
2016-09-02 16:08:11 +00:00
~~~ shell
2016-09-02 16:08:11 +00:00
# Server: has access to the destination SSH server
# ssh-over-xmpp server <conference> <IP address> <port>
ssh-over-xmpp server sshxmpp 1.2.3.4 22
## Client: has access to the XMPP server
ssh -o ProxyCommand="ssh-over-xmpp client sshxmpp" 127.0.0.1
~~~
2022-01-31 13:27:59 +00:00
## Stream Events from Riemann
2016-09-03 13:39:57 +00:00
This example will stream events from a query to an XMPP MUC using
[Riemann's](https://github.com/riemann/riemann) SSE interface. The events
are written to a named pipe to avoid buffering.
~~~ shell
2016-09-03 13:39:57 +00:00
mkfifo riemann
curl -s --get --data subscribe=true \
--data-urlencode 'query=(service ~= "^example")' \
http://example.com:80/index </dev/null >riemann &
xmppipe --verbose --verbose \
--discard --subject "riemann events" muc <riemann
2016-09-03 13:39:57 +00:00
~~~
2018-05-04 14:25:52 +00:00
### Desktop Notifications
~~~ shell
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
decode() {
printf '%b' "${1//%/\\x}"
2018-05-04 14:25:52 +00:00
}
MUC=""
while getopts ":o:" opt; do
case $opt in
o) MUC="$OPTARG" ;;
*) ;;
esac
done
2018-07-18 13:49:26 +00:00
xmppipe "$@" | while IFS=: read stanza type from to body; do
case "$stanza" in
2020-08-20 10:19:10 +00:00
m) notify-send "$MUC" "$(decode "$body")" ;;
2018-05-04 14:25:52 +00:00
*) continue ;;
esac
done
~~~
### Mirror a terminal session using script(1)
* user
~~~ shell
#!/bin/bash
MUC=console
TMPDIR=$(mktemp -d)
FIFO=$TMPDIR/console
2020-08-20 10:19:10 +00:00
mkfifo "$FIFO"
stty cols 80 rows 24
2020-08-20 10:19:10 +00:00
xmppipe --resource user -x $MUC < "$FIFO" >/dev/null 2>"$TMPDIR/stderr" &
script -q -f "$FIFO"
~~~
* viewers
~~~ shell
#!/bin/bash
decode() {
printf '%b' "${1//%/\\x}"
}
stty cols 80 rows 24
xmppipe --resource viewer --base64 console |
2018-05-23 14:05:19 +00:00
while IFS=: read -r x s f t m; do
[ "$m" = "m" ] && decode "$m"
2018-04-28 12:29:28 +00:00
done
~~~
2016-09-02 16:08:11 +00:00
2022-01-31 13:27:59 +00:00
## Image Upload
Upload an image using HTTP Upload (XEP-0363) then display it inline.
See [examples/image-upload](https://github.com/msantos/xmppipe/blob/master/examples/image-upload):
~~~
image-upload -o groupchat
~~~
~~~
# file must be in the same working directory as image-upload
echo "upload::::example.png" >/tmp/image_upload/stdin
~~~
2022-01-31 13:27:59 +00:00
# FORMAT
2016-09-02 16:08:11 +00:00
Each message is terminated by a new line. Message fields are separated by
":" and percent encoded.
Colon separated values are accepted as input if the input format type
is set to csv (`--format=csv`).
2022-01-31 13:27:59 +00:00
## Presence
2016-09-02 16:08:11 +00:00
p:<available|unavailable>:<to jid>:<from jid>
Input/Output:
Both
2016-09-02 16:08:11 +00:00
Example:
p:available:test@muc.example.com/xmppipe:occupant@example.com/1234
2016-09-02 16:08:11 +00:00
2022-01-31 13:27:59 +00:00
## Message
2016-09-02 16:08:11 +00:00
2018-08-04 13:06:52 +00:00
m:<chat|groupchat|normal|headline>:<from jid>:<to jid>:<message body>
2016-09-02 16:08:11 +00:00
Input/Output:
Both
2016-09-02 16:08:11 +00:00
Example:
m:groupchat:test@muc.example.com/mobile:user1@example.com/1234:Hello
m:chat:user1@example.com/mobile:user2@example.com:Message%20goes%20here
2016-09-02 16:08:11 +00:00
2022-01-31 13:27:59 +00:00
## Inline Image
Inline images will add a hint so clients (notably
2020-08-21 12:12:54 +00:00
[Conversations](https://github.com/iNPUTmice/Conversations)) will display
the image instead of a URL.
* type, from and to are optional
* message body: the percent escaped URL
I:<chat|groupchat|normal|headline>:<from jid>:<to jid>:<url>
Input/Output:
Input only
Example:
I::::https%3A%2F%2Fhttpstatusdogs.com%2Fimg%2F500.jpg
2022-01-31 13:27:59 +00:00
## XEP-0363: HTTP Upload
HTTP uploads create an upload slot. The XMPP server will respond with
`get` and `put` URLs. The `put` URL can be used to upload the file using,
e.g., `curl`. The `get` URL is used by clients for downloading the file.
2020-08-21 12:12:54 +00:00
Note: xmppipe creates the upload slot. Another utility, such as curl,
can be used to upload the file.
The input format is:
* type, from and to are optional
* message body: percent escaped, pipe separated value
* filename
* size
* optional: MIME type
u:<chat|groupchat|normal|headline>:<from jid>:<to jid>:<filename>|<size (bytes)>[|<content-type>]
The output format is:
* type, from and to are optional
* message body: percent escaped, pipe separated value
* get URL
* put URL
U:<chat|groupchat|normal|headline>:<from jid>:<to jid>:<get URL>|<put URL>
Example:
# $ stat --format="%s" example.png
# 16698
u::::example.png%7C16698
# also specify content type
u::::example.png%7C16698%7Cimage%2Fpng
# server response: slot created
U:groupchat:upload.example.com:user@example.com/123:https%3A//example.com/upload/0b9da82fea20a78778cbeddeab0472286cc35ed1/xyEaWFVZv3sv5ay9AGH5qBU02gglZRyUeGbjQg3k/example.png%7chttps%3A//example.com/upload/0b9da82fea20a78778cbeddeab0472286cc35ed1/xyEaWFVZv3sv5ay9AGH5qBU02gglZRyUeGbjQg3k/example.png
# to upload the file
curl https://example.com/upload/0b9da82fea20a78778cbeddeab0472286cc35ed1/xyEaWFVZv3sv5ay9AGH5qBU02gglZRyUeGbjQg3k/example.png --upload-file example.png
2022-01-31 13:27:59 +00:00
# COMPATIBILITY
2016-09-15 14:34:39 +00:00
2019-07-27 14:59:49 +00:00
Testing is done with ejabberd.
Also confirmed to work with:
* ejabberd ([creep.im](https://compliance.conversations.im/server/creep.im/))
* prosody ([dismail.de](https://compliance.conversations.im/server/dismail.de/))
* openfire ([jab.im](https://compliance.conversations.im/server/jab.im/))
* tigase ([tigase.im](https://compliance.conversations.im/server/tigase.im/))
* mongooseim
2022-01-31 13:27:59 +00:00
# LICENSE
2016-09-15 14:34:39 +00:00
2022-01-31 13:27:59 +00:00
Copyright (c) 2015-2022, Michael Santos <michael.santos@gmail.com>
2017-01-26 15:32:10 +00:00
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2022-01-31 13:27:59 +00:00
# TODO
2016-09-02 16:08:11 +00:00
* support [XEP-0384: OMEMO Encryption](https://xmpp.org/extensions/xep-0384.html)
2017-01-26 15:32:10 +00:00
* support alternative input modes
2017-01-26 15:32:10 +00:00
* "raw" mode: XML input/output
* HTTP Upload
* support PUT header elements
* handle error conditions