xinetd_tests.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # Copyright (c) 2020 Petr Vorel <pvorel@suse.cz>
  4. # Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved.
  5. # Copyright (c) International Business Machines Corp., 2001
  6. TST_SETUP="setup"
  7. TST_CLEANUP="cleanup"
  8. TST_NEEDS_CMDS="diff telnet in.telnetd xinetd"
  9. TST_NEEDS_TMPDIR=1
  10. TST_TESTFUNC="do_test"
  11. TST_CNT=2
  12. . daemonlib.sh
  13. . tst_net.sh
  14. setup()
  15. {
  16. [ -f "/usr/lib/systemd/system/telnet.socket" ] && \
  17. tst_brk TCONF "xinetd doesn't manage telnet"
  18. check_addr="127.0.0.1"
  19. ip a | grep -q inet6 && check_addr="$check_addr ::1"
  20. cat > tst_xinetd.conf.1 <<-EOF
  21. defaults
  22. {
  23. instances = 25
  24. log_type = FILE /var/log/servicelog
  25. log_on_success = HOST PID
  26. log_on_failure = HOST
  27. disabled = telnet
  28. }
  29. EOF
  30. cat > tst_xinetd.conf.2 <<-EOF
  31. defaults
  32. {
  33. instances = 25
  34. log_type = FILE /var/log/servicelog
  35. log_on_success = HOST PID
  36. log_on_failure = HOST
  37. # disabled = telnet
  38. }
  39. service telnet
  40. {
  41. socket_type = stream
  42. protocol = tcp
  43. wait = no
  44. user = root
  45. server = /usr/sbin/in.telnetd
  46. server_args = -n
  47. no_access =
  48. flags = IPv6
  49. }
  50. EOF
  51. ROD mv /etc/xinetd.conf xinetd.conf.orig
  52. }
  53. cleanup()
  54. {
  55. [ -f xinetd.conf.orig ] && \
  56. mv xinetd.conf.orig /etc/xinetd.conf
  57. restart_daemon xinetd
  58. }
  59. restart_xinetd()
  60. {
  61. tst_res TINFO "restart xinetd"
  62. restart_daemon xinetd > tst_xinetd.out 2>&1
  63. if [ $? -ne 0 ]; then
  64. cat tst_xinetd.out
  65. tst_brk TBROK "unable to restart service with telnet disabled"
  66. fi
  67. grep -qi "fail" tst_xinetd.out && \
  68. tst_brk TBROK "xinetd failed to restart"
  69. }
  70. xinetd_test()
  71. {
  72. local cnt=$1
  73. local desc="$2"
  74. local pattern="$3"
  75. local a p
  76. tst_res TINFO "install the new config file with telnet $desc"
  77. ROD mv tst_xinetd.conf.$cnt /etc/xinetd.conf
  78. restart_xinetd
  79. for a in $check_addr; do
  80. p=$(echo $pattern | sed "s/ADDR/$a/")
  81. echo '' | telnet $a 2>&1 | grep -qiE "$p"
  82. [ $? -ne 0 ] && \
  83. tst_brk TFAIL "not expected output for 'telnet $a'"
  84. done
  85. tst_res TPASS "expected output with telnet $desc"
  86. }
  87. do_test()
  88. {
  89. case $1 in
  90. 1) xinetd_test $1 "disabled" \
  91. "telnet: (connect to address ADDR|Unable to connect to remote host): Connection refused";;
  92. 2) xinetd_test $1 "enabled" \
  93. "Connection closed by foreign host";;
  94. esac
  95. }
  96. tst_run