#Copyright (C) 2016,2023 Weber Yann
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 3 of the License, or
#any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see .
#
# Jabber XMPP checks
#
__xmpp_probe() {
serv=$1
timeout=$2
type=$3
payload=$(printf "%s" "\n\n")
if [ "$verbose" -gt 2 ]
then
echo "$payload" | sed -e "s/^/$(datetime) [ DEBUG] Sent : /" >&2
fi
echo "$payload"
sleep "$timeout"
}
_xmpp_probe() {
serv=$1
port=$2
timeout=$3
type=$4
tmpres=$(mktemp -t xmpp_probe.XXXXXXXXX)
echo "$(__xmpp_probe "$serv" "$timeout" "$type"| nc -q2 "$serv" "$port")" | xmllint --format - > "$tmpres"
if [ "$verbose" -gt 2 ]
then
sed -e "s/^/$(datetime) [ DEBUG] Recv : /" >&2 < "$tmpres"
fi
cat "$tmpres"
rm "$tmpres"
}
_check_xmpp_ns() {
ns1=$1
expt_ns=$2
expt_port=$3
dnsq="_xmpp-${4}._tcp.${ns1}"
rep="$(dig "$dnsq" srv +short | cut -d" " -f3,4)"
if [ "$rep" = "$expt_port ${expt_ns}." ]
then
success "$dnsq = '$rep'"
else
fail "$dnsq = '$rep' but '$expt_port ${expt_ns}.' expected"
fi
}
check_xmpp_serv_ns() {
_check_xmpp_ns "$1" "$2" "$3" "server"
}
check_xmpp_client_ns() {
_check_xmpp_ns "$1" "$2" "$3" "client"
}
_check_xmpp_server() {
serv=$1
port=$2
timeout=$3
type=$4
if [ -z "$port" ]
then
port=5222
fi
if [ -z "$timeout" ]
then
timeout=2
fi
if [ "$verbose" -gt 1 ]
then
tpe="client"
if [ "$type" = "server" ]
then
tpe="S2S"
fi
logdate INFO "$tc_name: Connecting to XMPP $serv $tpe port $port (timeout=${timeout}s)" 3
fi
stream=$(_xmpp_probe "$serv" "$port" "$timeout" "$type"| head -n2 | tail -n1)
if [ -z "$stream" ]
then
fail "Empty reply from $serv:$port"
return
fi
if [ "$type" = "client" ]
then
infos=$(echo "$stream" | sed -E 's/^<([^ ]+).* xmlns="([^"]+)".* from="([^"]+)" .*$/\1 \2 \3/')
else
infos="$(echo "$stream" | sed -E 's/^<([^ ]+).* xmlns="([^"]+)" .*$/\1 \2/') $serv"
fi
if echo "$infos" | grep "jabber:$type" >/dev/null
then
success "Successfully connected to XMPP $type $serv:$port"
else
fail "Unexpected reply from $serv:$port : $infos"
fi
}
check_xmpp_server_client() {
_check_xmpp_server "$1" "$2" "$3" "client"
}
check_xmpp_server_s2s() {
_check_xmpp_server "$1" "$2" "$3" "server"
}
check_xmpp_ssl() {
serv=$1
port=$2
if [ -z "$port" ]
then
port=5222
fi
openssl s_client -connect "$serv:$port" /dev/null 2>/dev/null
rep=$?
if [ "$rep" -eq 0 ]
then
success "Openssl successfully negociating XMPP ssl with $serv:$port"
else
fail "Openssl failed negociating XMPP ssl with $serv:$port"
fi
}