1#!/bin/sh 2################################################################################ 3## ## 4## Copyright (c) International Business Machines Corp., 2001 ## 5## ## 6## This program is free software; you can redistribute it and#or modify ## 7## it under the terms of the GNU General Public License as published by ## 8## the Free Software Foundation; either version 2 of the License, or ## 9## (at your option) any later version. ## 10## ## 11## This program is distributed in the hope that it will be useful, but ## 12## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ## 13## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ## 14## for more details. ## 15## ## 16## You should have received a copy of the GNU General Public License ## 17## along with this program; if not, write to the Free Software ## 18## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ## 19## ## 20################################################################################ 21# 22# File : xinetd_tests.sh 23# 24# Description: Test Basic functionality of xinetd command. 25# Test #1: xinetd starts programs that provide Internet services. 26# 27# Author: Manoj Iyer, manjo@mail.utexas.edu 28# 29# History: Mar 04 2003 - Created - Manoj Iyer. 30# 31# Function: chk_ifexists 32# 33# Description: - Check if command required for this test exits. 34# 35# Input: - $1 - calling test case. 36# - $2 - command that needs to be checked. 37# 38# Return: - zero on success. 39# - non-zero on failure. 40chk_ifexists() 41{ 42 which $2 > $LTPTMP/tst_xinetd.err 2>&1 43 RC=$? 44 if [ $RC -ne 0 ] 45 then 46 tst_brkm TBROK NULL "$1: command $2 not found." 47 fi 48 return $RC 49} 50 51 52# Function: init 53# 54# Description: - Check if command required for this test exits. 55# - Create temporary directories required for this test. 56# - Initialize global variables. 57# 58# Return: - zero on success. 59# - non-zero on failure. 60init() 61{ 62 # Initialize global variables. 63 export TST_TOTAL=2 64 export TCID="xinetd" 65 export TST_COUNT=0 66 . daemonlib.sh 67 68 if [ -f "/usr/lib/systemd/system/telnet.socket" ]; then 69 tst_brkm TCONF NULL "xinetd doesn't manage telnet" 70 exit $? 71 fi 72 73 # Inititalize cleanup function. 74 trap "cleanup" 0 75 76 # create the temporary directory used by this testcase 77 if [ -z $TMP ] 78 then 79 LTPTMP=/tmp/tst_xinetd.$$ 80 else 81 LTPTMP=$TMP/tst_xinetd.$$ 82 fi 83 84 mkdir -p $LTPTMP > /dev/null 2>&1 85 RC=$? 86 if [ $RC -ne 0 ] 87 then 88 tst_brkm TBROK NULL "INIT: Unable to create temporary directory" 89 return $RC 90 fi 91 92 # sometimes the default telnet may be /usr/kerberos/bin/telnet 93 TELNET_COMM='/usr/bin/telnet' 94 95 # check if commands tst_*, xinetd, awk exists. 96 chk_ifexists INIT tst_resm || return $RC 97 chk_ifexists INIT xinetd || return $RC 98 chk_ifexists INIT diff || return $RC 99 chk_ifexists INIT ip || return $RC 100 chk_ifexists INIT $TELNET_COMM || return $RC 101 102 IPV6_ENABLED=0 103 ip a | grep inet6 > /dev/null 2>&1 104 if [ $? -eq 0 ] 105 then 106 IPV6_ENABLED=1 107 fi 108 109 # Create custom xinetd.conf file. 110 # tst_xinetd.conf.1 config file has telnet service disabled. 111 cat > $LTPTMP/tst_xinetd.conf.1 <<-EOF 112defaults 113{ 114 instances = 25 115 log_type = FILE /var/log/servicelog 116 log_on_success = HOST PID 117 log_on_failure = HOST 118 disabled = telnet 119} 120EOF 121RC=$? 122 123 # tst_xinetd.conf.2 config file has telnet enabled. 124 cat > $LTPTMP/tst_xinetd.conf.2 <<-EOF 125defaults 126{ 127 instances = 25 128 log_type = FILE /var/log/servicelog 129 log_on_success = HOST PID 130 log_on_failure = HOST 131 # disabled = telnet 132} 133 134service telnet 135{ 136 socket_type = stream 137 protocol = tcp 138 wait = no 139 user = root 140 server = /usr/sbin/in.telnetd 141 server_args = -n 142 no_access = 143 flags = IPv6 144} 145EOF 146RC=$? 147 148 # Create expected file with telnet disabled. 149 cat > $LTPTMP/tst_xinetd.exp.1 <<-EOF 150telnet: connect to address 127.0.0.1: Connection refused 151EOF 152RC=$? 153 154 if [ $RC -ne 0 ] 155 then 156 tst_brkm TBROK NULL \ 157 "INIT: unable to create expected file $LTPTMP/tst_xinetd.exp.1" 158 return $RC 159 fi 160 161 if [ $IPV6_ENABLED -eq 1 ] 162 then 163 cat > $LTPTMP/tst_xinetd.exp.1.ipv6 <<-EOF 164telnet: connect to address ::1: Connection refused 165EOF 166RC=$? 167 168 if [ $RC -ne 0 ] 169 then 170 tst_brkm TBROK NULL \ 171 "INIT: unable to create expected file $LTPTMP/tst_xinetd.exp.1" 172 fi 173 fi 174 175 # Create expected file with telnet enabled. 176 cat > $LTPTMP/tst_xinetd.exp.2 <<-EOF 177Trying 127.0.0.1... 178Connected to 127.0.0.1. 179Escape character is '^]'. 180Connection closed by foreign host. 181EOF 182RC=$? 183 184 if [ $RC -ne 0 ] 185 then 186 tst_brkm TBROK NULL \ 187 "INIT: unable to create expected file $LTPTMP/tst_xinetd.exp.2" 188 return $RC 189 fi 190 191 if [ $IPV6_ENABLED -eq 1 ] 192 then 193 cat > $LTPTMP/tst_xinetd.exp.2.ipv6 <<-EOF 194Trying ::1... 195Connected to ::1. 196Escape character is '^]'. 197Connection closed by foreign host. 198EOF 199RC=$? 200 201 if [ $RC -ne 0 ] 202 then 203 tst_brkm TBROK NULL \ 204 "INIT: unable to create expected file $LTPTMP/tst_xinetd.exp.2.ipv6" 205 fi 206 fi 207 208 return $RC 209} 210 211 212# Function: cleanup 213# 214# Description: - remove temporaty files and directories. 215# 216# Return: - zero on success. 217# - non-zero on failure. 218cleanup() 219{ 220 # restore the original xinetd.conf if a back up exits. 221 if [ -f /etc/xinetd.conf.orig ] 222 then 223 mv /etc/xinetd.conf.orig /etc/xinetd.conf \ 224 > $LTPTMP/tst_xinetd.err 2>&1 225 RC=$? 226 if [ $RC -ne 0 ] 227 then 228 tst_res TINFO $LTPTMP/tst_xinetd.err \ 229 "CLEANUP: failed restoring original xinetd.conf RC=$RC. Details:" 230 fi 231 232 sleep 1s 233 234 # restoring original services 235 restart_daemon xinetd > $LTPTMP/tst_xinetd.err 2>&1 236 RC=$? 237 if [ $RC -ne 0 ] 238 then 239 tst_res TINFO $LTPTMP/tst_xinetd.err \ 240 "CLEANUP: failed restoring original services RC=$RC. Details:" 241 fi 242 fi 243 244 # remove all the temporary files created by this test. 245 tst_resm TINFO "CLEAN: removing $LTPTMP" 246 rm -fr $LTPTMP 247} 248 249 250# Function: test01 251# 252# Description: - Test that xinetd reads the configuration file and starts or 253# stops services. 254# - restart xinetd with configuration file with telnet disabled. 255# - telnet to locahost should fail. 256# - restart xinetd with configuration file with telnet enabled. 257# - telnet to locahost should work. 258# 259# Return: - zero on success. 260# - non-zero on failure. 261test01() 262{ 263 TCID=xinetd01 264 TST_COUNT=1 265 nhops=0 # Number of hops required to get to host. 266 267 tst_resm TINFO "Test #1: restart xinetd with telnet disabled." 268 269 # create a backup of the original xinetd.conf file. 270 mv /etc/xinetd.conf /etc/xinetd.conf.orig > $LTPTMP/tst_xinetd.err 2>&1 271 RC=$? 272 if [ $RC -ne 0 ] 273 then 274 tst_brk TBROK $LTPTMP/tst_xinetd.err NULL \ 275 "Test #1: Failed while backing up original xinetd.conf. Details" 276 return $RC 277 fi 278 279 # install the new config file with telnet disabled. 280 mv $LTPTMP/tst_xinetd.conf.1 /etc/xinetd.conf > $LTPTMP/tst_xinetd.err 2>&1 281 RC=$? 282 if [ $RC -ne 0 ] 283 then 284 tst_brk TBROK $LTPTMP/tst_xinetd.err NULL \ 285 "Test #1: Failed installing new xinetd.conf in /etc. Details:" 286 return $RC 287 fi 288 289 tst_resm TINFO "Test #1: new xinetd.conf installed with telnet disabled." 290 291 sleep 1s 292 293 # restart xinetd to re-start the services 294 restart_daemon xinetd > $LTPTMP/tst_xinetd.out 2>&1 295 RC=$? 296 if [ $RC -ne 0 ] 297 then 298 tst_res TFAIL $LTPTMP/tst_xinetd.out \ 299 "Test #1: unable to restart service with telnet disabled. Details:" 300 return $RC 301 else 302 # even if xinetd restart has zero exit value, 303 # make certain there was no failure. 304 grep -i "fail" $LTPTMP/tst_xinetd.out > $LTPTMP/tst_xinetd.err 2>&1 305 RC=$? 306 if [ $RC -eq 0 ] 307 then 308 tst_res TFAIL $LTPTMP/tst_xinetd.err \ 309 "Test #1: xinetd failed to restart. Details" 310 return $RC 311 else 312 RC=0 313 tst_resm TINFO \ 314 "Test #1: xinetd re-started successfully with telnet disabled." 315 fi 316 fi 317 318 # Not checking for exit code from telnet command because telnet is 319 # not terminated by the test gracefully. 320 if [ $IPV6_ENABLED -eq 1 ] 321 then 322 tst_retry "echo '' | $TELNET_COMM ::1 2>$LTPTMP/tst_xinetd.out.ipv6 \ 323 1>/dev/null" 324 diff -iwB $LTPTMP/tst_xinetd.out.ipv6 $LTPTMP/tst_xinetd.exp.1.ipv6 \ 325 > $LTPTMP/tst_xinetd.err.ipv6 2>&1 326 RC=$? 327 if [ $RC -ne 0 ] 328 then 329 tst_res TFAIL $LTPTMP/tst_xinetd.err.ipv6 \ 330 "Test #1: with telnet diabled expected out differs RC=$RC. Details:" 331 return $RC 332 fi 333 fi 334 335 tst_retry "echo "" | $TELNET_COMM 127.0.0.1 2>$LTPTMP/tst_xinetd.out \ 336 1>/dev/null" 337 diff -iwB $LTPTMP/tst_xinetd.out $LTPTMP/tst_xinetd.exp.1 \ 338 > $LTPTMP/tst_xinetd.err 2>&1 339 RC=$? 340 if [ $RC -ne 0 ] 341 then 342 tst_res TFAIL $LTPTMP/tst_xinetd.err \ 343 "Test #1: with telnet diabled expected out differs RC=$RC. Details:" 344 return $RC 345 fi 346 347 tst_resm TINFO "Test #1: restart xinetd with telnet enabled." 348 # install the xinetd config file with telnet enabled. 349 mv $LTPTMP/tst_xinetd.conf.2 /etc/xinetd.conf > $LTPTMP/tst_xinetd.err 2>&1 350 RC=$? 351 if [ $RC -ne 0 ] 352 then 353 tst_brk TBROK $LTPTMP/tst_xinetd.err NULL \ 354 "Test #1: Failed installing new xinetd.conf in /etc. Details:" 355 return $RC 356 fi 357 358 tst_resm TINFO "Test #1: new xinetd.conf installed with telnet enabled." 359 360 sleep 1s 361 362 # restart services. 363 restart_daemon xinetd > $LTPTMP/tst_xinetd.out 2>&1 364 RC=$? 365 if [ $RC -ne 0 ] 366 then 367 tst_res TFAIL $LTPTMP/tst_xinetd.out \ 368 "Test #1: unable to restart services with telnet enabled. Details:" 369 return $RC 370 else 371 # even if restart has a zero exit value double check for failure. 372 grep -i "fail" $LTPTMP/tst_xinetd.out > $LTPTMP/tst_xinetd.err 2>&1 373 RC=$? 374 if [ $RC -eq 0 ] 375 then 376 tst_res TFAIL $LTPTMP/tst_xinetd.err \ 377 "Test #1: xinetd failed to restart. Details" 378 return $RC 379 else 380 RC=0 381 tst_resm TINFO \ 382 "Test #1: xinetd re-started successfully with telnet enabled." 383 fi 384 fi 385 386 # Not checking for exit code from telnet command because telnet is 387 # not terminated by the test gracefully. 388 if [ $IPV6_ENABLED -eq 1 ] 389 then 390 tst_retry "echo '' | $TELNET_COMM ::1 2>$LTPTMP/tst_xinetd.out.ipv6 2>&1" 391 diff -iwB $LTPTMP/tst_xinetd.out.ipv6 $LTPTMP/tst_xinetd.exp.2.ipv6 \ 392 > $LTPTMP/tst_xinetd.err.ipv6 2>&1 393 RC=$? 394 if [ $RC -ne 0 ] 395 then 396 tst_res TFAIL $LTPTMP/tst_xinetd.err.ipv6 \ 397 "Test #1: with telnet diabled expected out differs RC=$RC. Details:" 398 return $RC 399 else 400 tst_resm TPASS \ 401 "Test #1: xinetd reads the config file and starts or stops IPv6 services." 402 fi 403 fi 404 405 test_retry "echo '' | $TELNET_COMM 127.0.0.1 2>$LTPTMP/tst_xinetd.out 2>&1" 406 407 diff -iwB $LTPTMP/tst_xinetd.out $LTPTMP/tst_xinetd.exp.2 \ 408 > $LTPTMP/tst_xinetd.err 2>&1 409 RC=$? 410 if [ $RC -ne 0 ] 411 then 412 tst_res TFAIL $LTPTMP/tst_xinetd.err \ 413 "Test #1: expected output differes from actual. Details:" 414 return $RC 415 else 416 tst_resm TPASS \ 417 "Test #1: xinetd reads the config file and starts or stops services." 418 fi 419 420 return $RC 421} 422 423 424# Function: main 425# 426# Description: - Execute all tests and report results. 427# 428# Exit: - zero on success 429# - non-zero on failure. 430 431init || exit $? 432 433test01 || RC=$? 434 435exit $RC 436