xinetd_tests.sh revision 73469b59c5f779cdfb9ecd9f2fb5e2cb262f0341
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
67    # Inititalize cleanup function.
68    trap "cleanup" 0
69
70    # create the temporary directory used by this testcase
71    if [ -z $TMP ]
72    then
73        LTPTMP=/tmp/tst_xinetd.$$
74    else
75        LTPTMP=$TMP/tst_xinetd.$$
76    fi
77
78    mkdir -p $LTPTMP > /dev/null 2>&1
79    RC=$?
80    if [ $RC -ne 0 ]
81    then
82         tst_brkm TBROK NULL "INIT: Unable to create temporary directory"
83         return $RC
84    fi
85
86    # sometimes the default telnet may be /usr/kerberos/bin/telnet
87    TELNET_COMM='/usr/bin/telnet'
88
89    # check if commands tst_*, xinetd, awk exists.
90    chk_ifexists INIT tst_resm   || return $RC
91    chk_ifexists INIT xinetd     || return $RC
92    chk_ifexists INIT diff       || return $RC
93    chk_ifexists INIT ip         || return $RC
94    chk_ifexists INIT $TELNET_COMM || return $RC
95
96    IPV6_ENABLED=0
97    ip a | grep inet6 > /dev/null 2>&1
98    if [ $? -eq 0 ]
99    then
100        IPV6_ENABLED=1
101    fi
102
103    # Create custom xinetd.conf file.
104    # tst_xinetd.conf.1 config file has telnet service disabled.
105    cat > $LTPTMP/tst_xinetd.conf.1 <<-EOF
106defaults
107{
108    instances      = 25
109    log_type       = FILE /var/log/servicelog
110    log_on_success = HOST PID
111    log_on_failure = HOST
112    disabled       = telnet
113}
114EOF
115RC=$?
116
117    # tst_xinetd.conf.2 config file has telnet enabled.
118    cat > $LTPTMP/tst_xinetd.conf.2 <<-EOF
119defaults
120{
121    instances      = 25
122    log_type       = FILE /var/log/servicelog
123    log_on_success = HOST PID
124    log_on_failure = HOST
125    # disabled       = telnet
126}
127
128service telnet
129{
130    socket_type     = stream
131    protocol        = tcp
132    wait            = no
133    user            = root
134    server          = /usr/sbin/in.telnetd
135    server_args     = -n
136    no_access       =
137    flags           = IPv6
138}
139EOF
140RC=$?
141
142    # Create expected file with telnet disabled.
143    cat > $LTPTMP/tst_xinetd.exp.1 <<-EOF
144telnet: connect to address 127.0.0.1: Connection refused
145EOF
146RC=$?
147
148    if [ $RC -ne 0 ]
149    then
150        tst_brkm TBROK  NULL \
151            "INIT: unable to create expected file $LTPTMP/tst_xinetd.exp.1"
152        return $RC
153    fi
154
155    if [ $IPV6_ENABLED -eq 1 ]
156    then
157        cat > $LTPTMP/tst_xinetd.exp.1.ipv6 <<-EOF
158telnet: connect to address ::1: Connection refused
159EOF
160RC=$?
161
162        if [ $RC -ne 0 ]
163        then
164            tst_brkm TBROK NULL \
165                "INIT: unable to create expected file $LTPTMP/tst_xinetd.exp.1"
166        fi
167    fi
168
169    # Create expected file with telnet enabled.
170    cat > $LTPTMP/tst_xinetd.exp.2 <<-EOF
171Trying 127.0.0.1...
172Connected to 127.0.0.1.
173Escape character is '^]'.
174Connection closed by foreign host.
175EOF
176RC=$?
177
178    if [ $RC -ne 0 ]
179    then
180        tst_brkm TBROK  NULL \
181            "INIT: unable to create expected file $LTPTMP/tst_xinetd.exp.2"
182        return $RC
183    fi
184
185    if [ $IPV6_ENABLED -eq 1 ]
186    then
187        cat > $LTPTMP/tst_xinetd.exp.2.ipv6 <<-EOF
188Trying ::1...
189Connected to ::1.
190Escape character is '^]'.
191Connection closed by foreign host.
192EOF
193RC=$?
194
195        if [ $RC -ne 0 ]
196        then
197            tst_brkm TBROK NULL \
198                "INIT: unable to create expected file $LTPTMP/tst_xinetd.exp.2.ipv6"
199        fi
200    fi
201
202    return $RC
203}
204
205
206# Function:     cleanup
207#
208# Description:  - remove temporaty files and directories.
209#
210# Return:       - zero on success.
211#               - non-zero on failure.
212cleanup()
213{
214    # restore the original xinetd.conf if a back up exits.
215    if [ -f /etc/xinetd.conf.orig ]
216    then
217        mv /etc/xinetd.conf.orig /etc/xinetd.conf \
218            > $LTPTMP/tst_xinetd.err 2>&1
219        RC=$?
220        if [ $RC -ne 0 ]
221        then
222            tst_res TINFO $LTPTMP/tst_xinetd.err \
223            "CLEANUP: failed restoring original xinetd.conf RC=$RC. Details:"
224        fi
225
226        sleep 1s
227
228        # restoring original services
229        /etc/init.d/xinetd restart > $LTPTMP/tst_xinetd.err 2>&1
230        RC=$?
231        if [ $RC -ne 0 ]
232        then
233            tst_res TINFO $LTPTMP/tst_xinetd.err \
234            "CLEANUP: failed restoring original services RC=$RC. Details:"
235        fi
236    fi
237
238    # remove all the temporary files created by this test.
239    tst_resm TINFO "CLEAN: removing $LTPTMP"
240    rm -fr $LTPTMP
241}
242
243
244# Function:     test01
245#
246# Description:  - Test that xinetd reads the configuration file and starts or
247#                 stops services.
248#               - restart xinetd with configuration file with telnet disabled.
249#               - telnet to locahost should fail.
250#               - restart xinetd with configuration file with telnet enabled.
251#               - telnet to locahost should work.
252#
253# Return:       - zero on success.
254#               - non-zero on failure.
255test01()
256{
257    TCID=xinetd01
258    TST_COUNT=1
259    nhops=0             # Number of hops required to get to host.
260
261    tst_resm TINFO "Test #1: restart xinetd with telnet disabled."
262
263    # create a backup of the original xinetd.conf file.
264    mv /etc/xinetd.conf /etc/xinetd.conf.orig > $LTPTMP/tst_xinetd.err 2>&1
265    RC=$?
266    if [ $RC -ne 0 ]
267    then
268        tst_brk TBROK $LTPTMP/tst_xinetd.err NULL \
269            "Test #1: Failed while backing up original xinetd.conf. Details"
270        return $RC
271    fi
272
273    # install the new config file with telnet disabled.
274    mv $LTPTMP/tst_xinetd.conf.1 /etc/xinetd.conf > $LTPTMP/tst_xinetd.err 2>&1
275    RC=$?
276    if [ $RC -ne 0 ]
277    then
278        tst_brk TBROK $LTPTMP/tst_xinetd.err NULL \
279            "Test #1: Failed installing new xinetd.conf in /etc. Details:"
280        return $RC
281    fi
282
283    tst_resm TINFO "Test #1: new xinetd.conf installed with telnet disabled."
284
285    sleep 1s
286
287    # restart xinetd to re-start the services
288    /etc/init.d/xinetd restart > $LTPTMP/tst_xinetd.out 2>&1
289    RC=$?
290    if [ $RC -ne 0 ]
291    then
292        tst_res TFAIL $LTPTMP/tst_xinetd.out \
293       "Test #1: unable to restart service with telnet disabled. Details:"
294        return $RC
295    else
296        # even if xinetd restart has zero exit value,
297        # make certain there was no failure.
298        grep -i "fail" $LTPTMP/tst_xinetd.out > $LTPTMP/tst_xinetd.err 2>&1
299        RC=$?
300        if [ $RC -eq 0 ]
301        then
302            tst_res TFAIL $LTPTMP/tst_xinetd.err \
303                "Test #1: xinetd failed to restart. Details"
304            return $RC
305        else
306            RC=0
307            tst_resm TINFO \
308                "Test #1: xinetd re-started successfully with telnet disabled."
309        fi
310    fi
311
312    # Not checking for exit code from telnet command because telnet is
313    # not terminated by the test gracefully.
314    if [ $IPV6_ENABLED -eq 1 ]
315    then
316        echo "" | $TELNET_COMM ::1 2>$LTPTMP/tst_xinetd.out.ipv6 1>/dev/null
317        diff -iwB $LTPTMP/tst_xinetd.out.ipv6  $LTPTMP/tst_xinetd.exp.1.ipv6 \
318            > $LTPTMP/tst_xinetd.err.ipv6 2>&1
319        RC=$?
320        if [ $RC -ne 0 ]
321        then
322            tst_res TFAIL $LTPTMP/tst_xinetd.err.ipv6 \
323                "Test #1: with telnet diabled expected out differs RC=$RC. Details:"
324            return $RC
325        fi
326    fi
327
328    echo "" | $TELNET_COMM 127.0.0.1 2>$LTPTMP/tst_xinetd.out 1>/dev/null
329    diff -iwB $LTPTMP/tst_xinetd.out  $LTPTMP/tst_xinetd.exp.1 \
330        > $LTPTMP/tst_xinetd.err 2>&1
331    RC=$?
332    if [ $RC -ne 0 ]
333    then
334        tst_res TFAIL $LTPTMP/tst_xinetd.err \
335            "Test #1: with telnet diabled expected out differs RC=$RC. Details:"
336        return $RC
337    fi
338
339    tst_resm TINFO "Test #1: restart xinetd with telnet enabled."
340    # install the xinetd config file with telnet enabled.
341    mv $LTPTMP/tst_xinetd.conf.2 /etc/xinetd.conf > $LTPTMP/tst_xinetd.err 2>&1
342    RC=$?
343    if [ $RC -ne 0 ]
344    then
345        tst_brk TBROK $LTPTMP/tst_xinetd.err NULL \
346            "Test #1: Failed installing new xinetd.conf in /etc. Details:"
347        return $RC
348    fi
349
350    tst_resm TINFO "Test #1: new xinetd.conf installed with telnet enabled."
351
352    sleep 1s
353
354    # restart services.
355    /etc/init.d/xinetd restart > $LTPTMP/tst_xinetd.out 2>&1
356    RC=$?
357    if [ $RC -ne 0 ]
358    then
359        tst_res TFAIL $LTPTMP/tst_xinetd.out \
360            "Test #1: unable to restart services with telnet enabled. Details:"
361        return $RC
362    else
363        # even if restart has a zero exit value double check for failure.
364        grep -i "fail" $LTPTMP/tst_xinetd.out > $LTPTMP/tst_xinetd.err 2>&1
365        RC=$?
366        if [ $RC -eq 0 ]
367        then
368            tst_res TFAIL $LTPTMP/tst_xinetd.err \
369                "Test #1: xinetd failed to restart. Details"
370            return $RC
371        else
372            RC=0
373            tst_resm TINFO \
374                "Test #1: xinetd re-started successfully with telnet enabled."
375        fi
376    fi
377
378    # Not checking for exit code from telnet command because telnet is
379    # not terminated by the test gracefully.
380    if [ $IPV6_ENABLED -eq 1 ]
381    then
382        echo "" | $TELNET_COMM ::1 >$LTPTMP/tst_xinetd.out.ipv6 2>&1
383        diff -iwB $LTPTMP/tst_xinetd.out.ipv6  $LTPTMP/tst_xinetd.exp.2.ipv6 \
384            > $LTPTMP/tst_xinetd.err.ipv6 2>&1
385        RC=$?
386        if [ $RC -ne 0 ]
387        then
388            tst_res TFAIL $LTPTMP/tst_xinetd.err.ipv6 \
389                "Test #1: with telnet diabled expected out differs RC=$RC. Details:"
390            return $RC
391        else
392            tst_resm TPASS \
393            "Test #1: xinetd reads the config file and starts or stops IPv6 services."
394        fi
395    fi
396
397    echo "" | $TELNET_COMM 127.0.0.1 > $LTPTMP/tst_xinetd.out 2>&1
398
399    diff -iwB $LTPTMP/tst_xinetd.out  $LTPTMP/tst_xinetd.exp.2 \
400        > $LTPTMP/tst_xinetd.err 2>&1
401    RC=$?
402    if [ $RC -ne 0 ]
403    then
404        tst_res TFAIL $LTPTMP/tst_xinetd.err \
405            "Test #1: expected output differes from actual. Details:"
406        return $RC
407    else
408        tst_resm TPASS \
409        "Test #1: xinetd reads the config file and starts or stops services."
410    fi
411
412    return $RC
413}
414
415
416# Function:    main
417#
418# Description:    - Execute all tests and report results.
419#
420# Exit:            - zero on success
421#               - non-zero on failure.
422
423init || exit $?
424
425test01 || RC=$?
426
427exit $RC
428