#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 ipv_arg=$3 timeout=$4 type=$5 case "$ipv_arg" in ipv6)ipv=-6;; ipv4)ipv=-4;; default)ipv="";; esac tmpres=$(mktemp -t xmpp_probe.XXXXXXXXX) echo "$(__xmpp_probe "$serv" "$timeout" "$type"| nc $ipv -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}." ] if echo "$rep" | grep "${expt_ns}.$" >/dev/null 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 ipv=$3 timeout=$4 type=$5 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 $ipv (timeout=${timeout}s)" 3 fi stream=$(_xmpp_probe "$serv" "$port" "$ipv" "$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 $ipv" else fail "Unexpected reply from $serv:$port $ipv : $infos" fi } check_xmpp_server_client() { _check_xmpp_server "$1" "$2" "$3" "$4" "client" } check_xmpp_server_s2s() { _check_xmpp_server "$1" "$2" "$3" "$4" "server" } check_xmpp_ssl() { serv=$1 port=$2 ipv_arg=$3 if [ -z "$port" ] then port=5222 fi case "$ipv_arg" in ipv6)ipv=-6;; ipv4)ipv=-4;; default)ipv="";; esac openssl s_client $ipv -connect "$serv:$port" /dev/null 2>/dev/null rep=$? if [ "$rep" -eq 0 ] then success "Openssl successfully negociating XMPP ssl with $serv:$port $ipv_arg" else fail "Openssl failed negociating XMPP ssl with $serv:$port $ipv_arg" fi }