xinetd_tests.sh revision 54305b281dbcad520eae016d13e85d5142574611
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        echo "" | $TELNET_COMM ::1 2>$LTPTMP/tst_xinetd.out.ipv6 1>/dev/null
323        diff -iwB $LTPTMP/tst_xinetd.out.ipv6  $LTPTMP/tst_xinetd.exp.1.ipv6 \
324            > $LTPTMP/tst_xinetd.err.ipv6 2>&1
325        RC=$?
326        if [ $RC -ne 0 ]
327        then
328            tst_res TFAIL $LTPTMP/tst_xinetd.err.ipv6 \
329                "Test #1: with telnet diabled expected out differs RC=$RC. Details:"
330            return $RC
331        fi
332    fi
333
334    echo "" | $TELNET_COMM 127.0.0.1 2>$LTPTMP/tst_xinetd.out 1>/dev/null
335    diff -iwB $LTPTMP/tst_xinetd.out  $LTPTMP/tst_xinetd.exp.1 \
336        > $LTPTMP/tst_xinetd.err 2>&1
337    RC=$?
338    if [ $RC -ne 0 ]
339    then
340        tst_res TFAIL $LTPTMP/tst_xinetd.err \
341            "Test #1: with telnet diabled expected out differs RC=$RC. Details:"
342        return $RC
343    fi
344
345    tst_resm TINFO "Test #1: restart xinetd with telnet enabled."
346    # install the xinetd config file with telnet enabled.
347    mv $LTPTMP/tst_xinetd.conf.2 /etc/xinetd.conf > $LTPTMP/tst_xinetd.err 2>&1
348    RC=$?
349    if [ $RC -ne 0 ]
350    then
351        tst_brk TBROK $LTPTMP/tst_xinetd.err NULL \
352            "Test #1: Failed installing new xinetd.conf in /etc. Details:"
353        return $RC
354    fi
355
356    tst_resm TINFO "Test #1: new xinetd.conf installed with telnet enabled."
357
358    sleep 1s
359
360    # restart services.
361    restart_daemon xinetd > $LTPTMP/tst_xinetd.out 2>&1
362    RC=$?
363    if [ $RC -ne 0 ]
364    then
365        tst_res TFAIL $LTPTMP/tst_xinetd.out \
366            "Test #1: unable to restart services with telnet enabled. Details:"
367        return $RC
368    else
369        # even if restart has a zero exit value double check for failure.
370        grep -i "fail" $LTPTMP/tst_xinetd.out > $LTPTMP/tst_xinetd.err 2>&1
371        RC=$?
372        if [ $RC -eq 0 ]
373        then
374            tst_res TFAIL $LTPTMP/tst_xinetd.err \
375                "Test #1: xinetd failed to restart. Details"
376            return $RC
377        else
378            RC=0
379            tst_resm TINFO \
380                "Test #1: xinetd re-started successfully with telnet enabled."
381        fi
382    fi
383
384    # Not checking for exit code from telnet command because telnet is
385    # not terminated by the test gracefully.
386    if [ $IPV6_ENABLED -eq 1 ]
387    then
388        echo "" | $TELNET_COMM ::1 >$LTPTMP/tst_xinetd.out.ipv6 2>&1
389        diff -iwB $LTPTMP/tst_xinetd.out.ipv6  $LTPTMP/tst_xinetd.exp.2.ipv6 \
390            > $LTPTMP/tst_xinetd.err.ipv6 2>&1
391        RC=$?
392        if [ $RC -ne 0 ]
393        then
394            tst_res TFAIL $LTPTMP/tst_xinetd.err.ipv6 \
395                "Test #1: with telnet diabled expected out differs RC=$RC. Details:"
396            return $RC
397        else
398            tst_resm TPASS \
399            "Test #1: xinetd reads the config file and starts or stops IPv6 services."
400        fi
401    fi
402
403    echo "" | $TELNET_COMM 127.0.0.1 > $LTPTMP/tst_xinetd.out 2>&1
404
405    diff -iwB $LTPTMP/tst_xinetd.out  $LTPTMP/tst_xinetd.exp.2 \
406        > $LTPTMP/tst_xinetd.err 2>&1
407    RC=$?
408    if [ $RC -ne 0 ]
409    then
410        tst_res TFAIL $LTPTMP/tst_xinetd.err \
411            "Test #1: expected output differes from actual. Details:"
412        return $RC
413    else
414        tst_resm TPASS \
415        "Test #1: xinetd reads the config file and starts or stops services."
416    fi
417
418    return $RC
419}
420
421
422# Function:    main
423#
424# Description:    - Execute all tests and report results.
425#
426# Exit:            - zero on success
427#               - non-zero on failure.
428
429init || exit $?
430
431test01 || RC=$?
432
433exit $RC
434