Small sh "framework" to test some server responses
sh
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xmpp.sh 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #Copyright (C) 2016,2023 Weber Yann
  2. #
  3. #This program is free software; you can redistribute it and/or modify
  4. #it under the terms of the GNU General Public License as published by
  5. #the Free Software Foundation; either version 3 of the License, or
  6. #any later version.
  7. #
  8. #This program is distributed in the hope that it will be useful,
  9. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. #GNU General Public License for more details.
  12. #
  13. #You should have received a copy of the GNU General Public License
  14. #along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. #
  16. # Jabber XMPP checks
  17. #
  18. __xmpp_probe() {
  19. serv=$1
  20. timeout=$2
  21. type=$3
  22. payload=$(printf "%s" "<?xml version=\"1.0\"?>\n<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\" xmlns=\"jabber:$type\" to=\"${1}\" xml:lang=\"en\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\">\n")
  23. if [ "$verbose" -gt 2 ]
  24. then
  25. echo "$payload" | sed -e "s/^/$(datetime) [ DEBUG] Sent : /" >&2
  26. fi
  27. echo "$payload"
  28. sleep "$timeout"
  29. }
  30. _xmpp_probe() {
  31. serv=$1
  32. port=$2
  33. timeout=$3
  34. type=$4
  35. tmpres=$(mktemp -t xmpp_probe.XXXXXXXXX)
  36. echo "$(__xmpp_probe "$serv" "$timeout" "$type"| nc -q2 "$serv" "$port")</stream:stream>" | xmllint --format - > "$tmpres"
  37. if [ "$verbose" -gt 2 ]
  38. then
  39. sed -e "s/^/$(datetime) [ DEBUG] Recv : /" >&2 < "$tmpres"
  40. fi
  41. cat "$tmpres"
  42. rm "$tmpres"
  43. }
  44. _check_xmpp_ns() {
  45. ns1=$1
  46. expt_ns=$2
  47. expt_port=$3
  48. dnsq="_xmpp-${4}._tcp.${ns1}"
  49. rep="$(dig "$dnsq" srv +short | cut -d" " -f3,4)"
  50. if [ "$rep" = "$expt_port ${expt_ns}." ]
  51. then
  52. success "$dnsq = '$rep'"
  53. else
  54. fail "$dnsq = '$rep' but '$expt_port ${expt_ns}.' expected"
  55. fi
  56. }
  57. check_xmpp_serv_ns() {
  58. _check_xmpp_ns "$1" "$2" "$3" "server"
  59. }
  60. check_xmpp_client_ns() {
  61. _check_xmpp_ns "$1" "$2" "$3" "client"
  62. }
  63. _check_xmpp_server() {
  64. serv=$1
  65. port=$2
  66. timeout=$3
  67. type=$4
  68. if [ -z "$port" ]
  69. then
  70. port=5222
  71. fi
  72. if [ -z "$timeout" ]
  73. then
  74. timeout=2
  75. fi
  76. if [ "$verbose" -gt 1 ]
  77. then
  78. tpe="client"
  79. if [ "$type" = "server" ]
  80. then
  81. tpe="S2S"
  82. fi
  83. logdate INFO "$tc_name: Connecting to XMPP $serv $tpe port $port (timeout=${timeout}s)" 3
  84. fi
  85. stream=$(_xmpp_probe "$serv" "$port" "$timeout" "$type"| head -n2 | tail -n1)
  86. if [ -z "$stream" ]
  87. then
  88. fail "Empty reply from $serv:$port"
  89. return
  90. fi
  91. if [ "$type" = "client" ]
  92. then
  93. infos=$(echo "$stream" | sed -E 's/^<([^ ]+).* xmlns="([^"]+)".* from="([^"]+)" .*$/\1 \2 \3/')
  94. else
  95. infos="$(echo "$stream" | sed -E 's/^<([^ ]+).* xmlns="([^"]+)" .*$/\1 \2/') $serv"
  96. fi
  97. if echo "$infos" | grep "jabber:$type" >/dev/null
  98. then
  99. success "Successfully connected to XMPP $type $serv:$port"
  100. else
  101. fail "Unexpected reply from $serv:$port : $infos"
  102. fi
  103. }
  104. check_xmpp_server_client() {
  105. _check_xmpp_server "$1" "$2" "$3" "client"
  106. }
  107. check_xmpp_server_s2s() {
  108. _check_xmpp_server "$1" "$2" "$3" "server"
  109. }
  110. check_xmpp_ssl() {
  111. serv=$1
  112. port=$2
  113. if [ -z "$port" ]
  114. then
  115. port=5222
  116. fi
  117. openssl s_client -connect "$serv:$port" </dev/null -starttls xmpp >/dev/null 2>/dev/null
  118. rep=$?
  119. if [ "$rep" -eq 0 ]
  120. then
  121. success "Openssl successfully negociating XMPP ssl with $serv:$port"
  122. else
  123. fail "Openssl failed negociating XMPP ssl with $serv:$port"
  124. fi
  125. }