File: inetd.sh

package info (click to toggle)
inetutils 2%3A2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,588 kB
  • sloc: ansic: 132,363; sh: 12,498; yacc: 1,651; makefile: 725; perl: 72
file content (224 lines) | stat: -rwxr-xr-x 5,588 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/sh

# Copyright (C) 2013-2025 Free Software Foundation, Inc.
#
# This file is part of GNU Inetutils.
#
# GNU Inetutils is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# GNU Inetutils is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see `http://www.gnu.org/licenses/'.

# Test to establish functionality of inetd.
# An important part is to run in daemon mode
# and to send SIGHUP repeatedly.
#
# Written by Mats Erik Andersson.

. ./tools.sh

if test -z "${VERBOSE+set}"; then
    silence=:
fi

# Portability fix for SVR4
PWD="${PWD:-`pwd`}"
USER=${USER:-`func_id_user`}

# Prerequisites
#
$need_mktemp || exit_no_mktemp

PASSWD=/etc/passwd
PWDDB=/etc/pwd.db
PROTOCOLS=/etc/protocols

# Keep the following two tests separate for better diagnosis!
#
if test ! -r $PROTOCOLS; then
    cat <<-EOT >&2
	This test requires the availability of "$PROTOCOLS",
	a file which can not be found in the current system.
	Therefore skipping this test.
	EOT
    exit 77
fi

if test ! -r $PASSWD && test ! -r $PWDDB; then
    cat <<-EOT >&2
	This test requires availability of either "$PASSWD"
	or "$PWDDB".  The requirement can not be met in the
	current system.  Therefore skipping this test.
	EOT
    exit 77
fi

# Execution control.  Initialise early!
#
do_cleandir=false

# Select numerical target addresses.
TARGET=${TARGET:-127.0.0.1}
TARGET6=${TARGET6:-::1}

# Executable under test and helper functionality.
#
INETD=${INETD:-../src/inetd$EXEEXT}
ADDRPEEK=${ADDRPEEK:-$PWD/addrpeek$EXEEXT}
TCPGET=${TCPGET:-$PWD/tcpget$EXEEXT}

if test ! -x $ADDRPEEK; then
    echo >&2 "No executable '$ADDRPEEK' present.  Skipping test."
    exit 77
fi

if [ ! -x $INETD ]; then
    echo "Missing executable '$INETD'.  Skipping test." >&2
    exit 77
fi

if test ! -x $TCPGET; then
    echo >&2 "No executable '$TCPGET' present.  Skipping test."
    exit 77
fi

if test "$TEST_IPV4" = "no" && test "$TEST_IPV6" = "no"; then
    echo >&2 "Inet socket testing is disabled.  Skipping test."
    exit 77
fi

if test -n "$VERBOSE"; then
    set -x
    $INETD --version | $SED '1q'
fi

# The value of USER is vital to the test configuration.
$silence echo "Running test as user $USER."

# For file creation below IU_TESTDIR.
umask 0077

# Keep any external assignment of testing directory.
# Otherwise a randomisation is included.
#
: ${IU_TESTDIR:=$PWD/iu_inetd.XXXXXX}

if [ ! -d "$IU_TESTDIR" ]; then
    do_cleandir=true
    IU_TESTDIR="`$MKTEMP -d "$IU_TESTDIR" 2>/dev/null`" ||
	{
	    echo 'Failed at creating test directory.  Aborting.' >&2
	    exit 77
	}
elif expr X"$IU_TESTDIR" : X'[./]\{1,\}$' >/dev/null; then
    # Eliminating directories: all mixtures of . and / only.
    echo 'Dangerous input for test directory.  Aborting.' >&2
    exit 77
fi

# The INETD daemon uses two files in the present test.
#
CONF="$IU_TESTDIR"/inetd.conf
PID="$IU_TESTDIR"/inetd.pid

# Are we able to write in IU_TESTDIR?
# This could happen with preset IU_TESTDIR.
#
touch "$CONF" || {
    echo 'No write access in test directory.  Aborting.' >&2
    exit 1
}

# Erase the temporary directory.
#
clean_testdir () {
    if test -f "$PID" && kill -0 "`cat "$PID"`" >/dev/null 2>&1; then
	kill "`cat "$PID"`" || kill -9 "`cat "$PID"`"
    fi
    if test -z "${NOCLEAN+no}" && $do_cleandir; then
	rm -r -f "$IU_TESTDIR"
    fi
}

# Write a fresh configuration file.  Port is input parameter.
write_conf () {
    # First argument is port number.  Node is fixed.
    : > $CONF

    test "$TEST_IPV4" = "no" ||
	echo "$TARGET:$1 stream tcp4 nowait $USER $ADDRPEEK addrpeek addr" \
	    >> $CONF
    test "$TEST_IPV6" = "no" ||
	echo "$TARGET6:$1 stream tcp6 nowait $USER $ADDRPEEK addrpeek addr" \
	    >> $CONF
}

errno=0

PORT=`expr 12347 + ${RANDOM:-$$} % 521`

write_conf $PORT

# The daemon is launched only once.
#
$INETD -p$PID $CONF

# Allow for the service to settle.
sleep 2

if test ! -f $PID; then
    echo >&2 "Inetd never started: missing the PID-file."
    errno=1
else
    # Repeated SIGHUP testing, with modified port.
    for nn in 1 2 3 4 5; do
        family=

	# Check for response at chosen port.
	if test "$TEST_IPV4" != "no" && test -n "$TARGET"; then
	    $TCPGET $TARGET $PORT 2>/dev/null |
		$GREP "Your address is $TARGET." >/dev/null 2>&1 \
	    || { errno=`expr $errno + 1`; family=IPv4; }
	fi # TEST_IPV4 && TARGET

	if test "$TEST_IPV6" != "no" && test -n "$TARGET6"; then
	    $TCPGET $TARGET6 $PORT 2>/dev/null |
		$GREP "Your address is $TARGET6." >/dev/null 2>&1 \
	    || { errno=`expr $errno + 1`; family="${family:+$family }IPv6"; }
	fi # TEST_IPV6 && TARGET6

	test $errno -eq 0 ||
	    { cat >&2 <<-EOT
		*** Repetition $nn of SIGHUP test has failed. ***
		*** Offending socket family: $family ***
		Configuration file:
		##### $CONF
		`cat $CONF`
		###########
		EOT
	      break; }

	# Update with new port for next round.
	PORT=`expr $PORT + 1 + ${RANDOM:-$$} % 521`
	write_conf $PORT
	kill -HUP `cat $PID`

	# Allow for the service to settle.
	sleep 1
    done
    $silence echo "Passed `expr $nn - 1` SIGHUP rounds."
fi

test $errno -ne 0 || $silence echo 'Successful testing.'

clean_testdir

exit $errno