Debian Codegraph @

tests/tftp.sh

raw
1 #!/bin/sh
2
3 # Copyright (C) 2010-2026 Free Software Foundation, Inc.
4 #
5 # This file is part of GNU Inetutils.
6 #
7 # GNU Inetutils is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or (at
10 # your option) any later version.
11 #
12 # GNU Inetutils is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see `http://www.gnu.org/licenses/'.
19
20 # Run `inetd' with `tftpd' and try to fetch a file from there using `tftp'.
21
22 # Prerequisites:
23 #
24 # * Shell: SVR4 Bourne shell, or newer.
25 #
26 # * dd(1), id(1), kill(1), mktemp(1), netstat(8), uname(1).
27 #
28 # * Accessed by launched Inetd:
29 # /etc/nsswitch.conf, /etc/passwd, /etc/protocols.
30 #
31 # OpenBSD uses /etc/services directly, not via /etc/nsswitch.conf.
32
33 #
34 # Currently implemented tests (10 or 12 in total):
35 #
36 # * Read three files in binary mode, from 127.0.0.1 and ::1,
37 # needing one, two, and multiple data packets, respectively.
38 #
39 # * Read one moderate size ascii file from 127.0.0.1 and ::1.
40 #
41 # * Reload configuration and read a small binary file twice.
42 #
43 # * (root only) Reload configuration for chrooted mode.
44 # Read one binary file with a relative name, and one ascii
45 # file with absolute location.
46 #
47 # The values of TARGET and TARGET6 replace the loopback addresses
48 # 127.0.0.1 and ::1, whenever the variables are set. However,
49 # setting the variable ADDRESSES to a list of addresses takes
50 # precedence over all other choices. The particular value "sense"
51 # tries to find all local addresses, then go ahead with these.
52 #
53 # Whenever set, VERBOSE and LOGGING, this test is performed in verbose
54 # mode, and lets `tftpd' do system logging, respectively.
55
56 . ./tools.sh
57
58 $need_dd || exit_no_dd
59 $need_mktemp || exit_no_mktemp
60 $need_netstat || exit_no_netstat
61
62 if test -z "${VERBOSE+set}"; then
63 silence=:
64 bucket='>/dev/null'
65 fi
66
67 if test -n "$VERBOSE"; then
68 set -x
69 fi
70
71 # Portability fix for SVR4
72 PWD="${PWD:-`pwd`}"
73
74 TFTP="${TFTP:-../src/tftp$EXEEXT}"
75 TFTPD="${TFTPD:-$PWD/../src/tftpd$EXEEXT}"
76 INETD="${INETD:-../src/inetd$EXEEXT}"
77 IFCONFIG="${IFCONFIG:-../ifconfig/ifconfig$EXEEXT --format=unix}"
78 IFCONFIG_SIMPLE=`expr X"$IFCONFIG" : X'\([^ ]*\)'` # Remove options
79
80 if [ ! -x $TFTP ]; then
81 echo "No TFTP client '$TFTP' present. Skipping test" >&2
82 exit 77
83 elif [ ! -x $TFTPD ]; then
84 echo "No TFTP server '$TFTPD' present. Skipping test" >&2
85 exit 77
86 elif [ ! -x $INETD ]; then
87 echo "No inetd superserver '$INETD' present. Skipping test" >&2
88 exit 77
89 elif [ ! -x $IFCONFIG_SIMPLE ]; then # Remove options
90 echo "No ifconfig '$IFCONFIG_SIMPLE' present. Skipping test" >&2
91 exit 77
92 fi
93
94 # The superserver Inetd puts constraints on any chroot
95 # when running this script, since it needs to look up
96 # some basic facts stated in the configuration file.
97 NSSWITCH=/etc/nsswitch.conf
98 PASSWD=/etc/passwd
99 PROTOCOLS=/etc/protocols
100
101 # Overrides based on systems.
102 test `uname -s` = OpenBSD && NSSWITCH=/etc/services
103
104 if test ! -r $NSSWITCH || test ! -r $PASSWD \
105 || test ! -r $PROTOCOLS; then
106 cat <<-EOT >&2
107 The use of the superserver Inetd in this script requires
108 the availability of "$NSSWITCH", "$PASSWD", and
109 "$PROTOCOLS". At least one of these is now missing.
110 Therefore skipping test.
111 EOT
112 exit 77
113 fi
114
115 AF=${AF:-inet}
116 PROTO=${PROTO:-udp}
117 USER=`func_id_user`
118
119 # Late supplementary subtest.
120 do_conf_reload=true
121 do_secure_setting=true
122
123 # Disable chrooted mode for non-root invocation.
124 test `func_id_uid` -eq 0 || do_secure_setting=false
125
126 # Random base directory at testing time.
127 TMPDIR=`$MKTEMP -d $PWD/tmp.XXXXXXXXXX` ||
128 {
129 echo 'Failed at creating test directory. Aborting.' >&2
130 exit 1
131 }
132
133 INETD_CONF="$TMPDIR/inetd.conf.tmp"
134 INETD_PID="$TMPDIR/inetd.pid.$$"
135
136 posttesting () {
137 if test -n "$TMPDIR" && test -f "$INETD_PID" \
138 && test -r "$INETD_PID" \
139 && kill -0 "`cat $INETD_PID`" >/dev/null 2>&1
140 then
141 kill "`cat $INETD_PID`" 2>/dev/null ||
142 kill -9 "`cat $INETD_PID`" 2>/dev/null
143 fi
144 test -n "$TMPDIR" && test -d "$TMPDIR" \
145 && rm -rf "$TMPDIR" $FILELIST
146 }
147
148 trap posttesting EXIT HUP INT QUIT TERM
149
150 # Use only "127.0.0.1 ::1" as default address list,
151 # but take account of TARGET and TARGET6.
152 # Other configured addresses might be set under
153 # strict filter policies, thus might block.
154 #
155 # Allow a setting "ADDRESSES=sense" to compute the
156 # available addresses and then to test them all.
157 if test "$ADDRESSES" = "sense"; then
158 ADDRESSES=`$IFCONFIG -a | $SED -e "/$AF /!d" \
159 -e "s/^.*$AF \([:.0-9]\{1,\}\) .*$/\1/g"`
160 fi
161
162 if test -z "$ADDRESSES"; then
163 test "$TEST_IPV4" = "no" || ADDRESSES="${TARGET:-127.0.0.1}"
164 test "$TEST_IPV6" = "no" ||
165 ADDRESSES="${ADDRESSES:+$ADDRESSES }${TARGET6:-::1}"
166 fi
167
168 # Work around the peculiar output of netstat(1m,solaris).
169 #
170 # locate_port proto port
171 #
172 locate_port () {
173 if [ "`uname -s`" = "SunOS" ]; then
174 $NETSTAT -na -finet -finet6 -P$1 |
175 $GREP "\.$2[^0-9]" >/dev/null 2>&1
176 else
177 $NETSTAT -na |
178 $GREP "^$1[46]\{0,2\}.*[^0-9]$2[^0-9]" >/dev/null 2>&1
179 fi
180 }
181
182 if [ "$VERBOSE" ]; then
183 "$TFTP" --version | $SED '1q'
184 "$TFTPD" --version | $SED '1q'
185 "$INETD" --version | $SED '1q'
186 "$IFCONFIG_SIMPLE" --version | $SED '1q'
187 fi
188
189 # Find an available port number. There will be some
190 # room left for a race condition, but we try to be
191 # flexible enough for running copies of this script.
192 #
193 if test -z "$PORT"; then
194 for PORT in 7777 7779 7783 7791 7807 7839 none; do
195 test $PORT = none && break
196 if locate_port $PROTO $PORT; then
197 continue
198 else
199 break
200 fi
201 done
202 if test "$PORT" = 'none'; then
203 echo 'Our port allocation failed. Skipping test.' >&2
204 exit 77
205 fi
206 fi
207
208 # Create `inetd.conf'. Note: We want $TFTPD to be an absolute file
209 # name because `inetd' chdirs to `/' in daemon mode; ditto for
210 # $INETD_CONF. Thus the dependency on file locations will be
211 # identical in daemon-mode and in debug-mode.
212 write_conf () {
213 : > "$INETD_CONF" 2>/dev/null
214
215 test "$TEST_IPV4" = "no" ||
216 cat >> "$INETD_CONF" <<-EOF
217 $PORT dgram ${PROTO}4 wait $USER $TFTPD tftpd ${LOGGING+"-l"} $TMPDIR/tftp-test
218 EOF
219
220 test "$TEST_IPV6" = "no" ||
221 cat >> "$INETD_CONF" <<-EOF
222 $PORT dgram ${PROTO}6 wait $USER $TFTPD tftpd ${LOGGING+"-l"} $TMPDIR/tftp-test
223 EOF
224 }
225
226 if test "$TEST_IPV4" = "no" && test "$TEST_IPV6" = "no"
227 then
228 echo >&2 "Inet socket test is switched off. Skipping test."
229 exit 77
230 fi
231
232 write_conf ||
233 {
234 echo 'Could not create configuration file for Inetd. Aborting.' >&2
235 exit 1
236 }
237
238 # Launch `inetd', assuming it's reachable at all $ADDRESSES.
239 # Must use '-d' consistently to prevent daemonizing, but we
240 # would like to suppress the verbose output. The variable
241 # REDIRECT is set to '2>/dev/null' in non-verbose mode.
242 #
243 test -n "$VERBOSE" || REDIRECT='2>/dev/null'
244
245 eval "$INETD -d -p'$INETD_PID' '$INETD_CONF' $REDIRECT &"
246
247 # Debug mode allows the shell to recover PID of Inetd.
248 spawned_pid=$!
249
250 sleep 2
251
252 inetd_pid="`cat $INETD_PID 2>/dev/null`" ||
253 {
254 cat <<-EOT >&2
255 Inetd did not create a PID-file. Aborting test,
256 but losing control whether an Inetd process is
257 still around.
258 EOT
259 exit 1
260 }
261
262 test -z "$VERBOSE" || echo "Launched Inetd as process $inetd_pid." >&2
263
264 # Wait somewhat for the service to settle.
265 sleep 1
266
267 # Did `inetd' really succeed in establishing a listener?
268 locate_port $PROTO $PORT
269 if test $? -ne 0; then
270 # No it did not.
271 kill -0 "$inetd_pid" >/dev/null 2>&1 && kill -9 "$inetd_pid" 2>/dev/null
272 rm -f "$INETD_PID"
273
274 echo 'First attempt at starting Inetd has failed.' >&2
275 echo 'A new attempt will follow after some delay.' >&2
276 echo 'Increasing verbosity for better backtrace.' >&2
277 sleep 7
278
279 # Select a new port, with offset and some randomness.
280 PORT=`expr $PORT + 137 + \( ${RANDOM:-$$} % 517 \)`
281 write_conf ||
282 {
283 echo 'Could not create configuration file for Inetd. Aborting.' >&2
284 exit 1
285 }
286
287 $INETD -d -p"$INETD_PID" "$INETD_CONF" &
288 spawned_pid=$!
289 sleep 2
290 inetd_pid="`cat $INETD_PID 2>/dev/null`" ||
291 {
292 cat <<-EOT >&2
293 Inetd did not create a PID-file. Aborting test,
294 but losing control whether an Inetd process is
295 still around.
296 EOT
297 kill -0 "$spawned_pid" >/dev/null 2>&1 && kill -9 "$spawned_pid" 2>/dev/null
298 exit 1
299 }
300
301 echo "Launched Inetd as process $inetd_pid." >&2
302
303 if locate_port $PROTO $PORT; then
304 : # Successful this time.
305 else
306 echo "Failed again at starting correct Inetd instance." >&2
307 kill -0 "$spawned_pid" >/dev/null 2>&1 && kill -9 "$spawned_pid" 2>/dev/null
308 exit 1
309 fi
310 fi
311
312 if [ -r /dev/urandom ]; then
313 input="/dev/urandom"
314 else
315 input="/dev/zero"
316 fi
317
318 test -d "$TMPDIR" && rm -fr "$TMPDIR/tftp-test" tftp-test-file*
319 test -d "$TMPDIR" && mkdir -p "$TMPDIR/tftp-test" \
320 || {
321 echo 'Failed at creating directory for master files. Aborting.' >&2
322 exit 1
323 }
324
325 # It is important to test data of differing sizes.
326 # These are binary files.
327 #
328 # Input format:
329 #
330 # name block-size count
331
332 FILEDATA="file-small 320 1
333 file-medium 320 2
334 tftp-test-file 1024 170"
335
336 echo "$FILEDATA" |
337 while read name bsize count; do
338 test -z "$name" && continue
339 $DD if="$input" of="$TMPDIR/tftp-test/$name" \
340 bs=$bsize count=$count 2>/dev/null
341 done
342
343 FILELIST="`echo "$FILEDATA" | $SED 's/ .*//' | tr "\n" ' '`"
344
345 # Add a file known to be ASCII encoded.
346 #
347 ASCIIFILE=asciifile.txt
348 if test -r tools.sh; then
349 cp tools.sh "$TMPDIR/tftp-test/$ASCIIFILE"
350 FILELIST="$FILELIST $ASCIIFILE"
351 fi
352
353 SUCCESSES=0
354 EFFORTS=0
355 RESULT=0
356
357 $silence echo "Looking into '`echo $ADDRESSES | tr "\n" ' '`'."
358
359 for addr in $ADDRESSES; do
360 $silence echo "trying address '$addr'..." >&2
361
362 for name in $FILELIST missing-file; do
363 test -n "$name" || continue
364 EFFORTS=`expr $EFFORTS + 1`
365 rm -f "$name"
366 test "$name" = $ASCIIFILE && type=ascii || type=binary
367 echo "$type
368 get $name" | \
369 eval "$TFTP" ${VERBOSE:+-v} "$addr" $PORT $bucket
370
371 if test "$name" != "missing-file"; then
372 cmp "$TMPDIR/tftp-test/$name" "$name" 2>/dev/null
373 result=$?
374 else
375 # No data should have arrived, but traditionally an empty
376 # file was created.
377 result=0
378 test ! -s "$name" || result=1
379 fi
380
381 if [ "$result" -ne 0 ]; then
382 # Failure.
383 test -z "$VERBOSE" || echo "Failed comparison for $addr/$name." >&2
384 RESULT=$result
385 else
386 SUCCESSES=`expr $SUCCESSES + 1`
387 test -z "$VERBOSE" || echo "Successful comparison for $addr/$name." >&2
388 fi
389 done
390
391 # Do a compound test with multiple requests.
392 # Issue one request for locally renamed file.
393 rm -f file-small _file-small_ missing-file
394 EFFORTS=`expr $EFFORTS + 1`
395
396 cat <<-EOT |
397 binary
398 get file-small
399 get missing-file
400 get file-small _file-small_
401 quit
402 EOT
403 eval $TFTP ${VERBOSE:+-v} "$addr" $PORT $bucket
404
405 if cmp "$TMPDIR/tftp-test/file-small" file-small 2>/dev/null \
406 && test ! -s missing-file \
407 && cmp "$TMPDIR/tftp-test/file-small" _file-small_ 2>/dev/null
408 then
409 SUCCESSES=`expr $SUCCESSES + 1`
410 test -z "$VERBOSE" || echo "Successful compound test." >&2
411 else
412 echo "Failure during compound test." >&2
413
414 # Investigate probable causes.
415 test -s _file-small_ ||
416 echo "Third get request failed after file known to be missing." >&2
417 { test ! -f missing-file || test -s missing-file ; } &&
418 echo "The missing file did not appear as empty." >&1
419 test -s file-small ||
420 echo "Not even the first request succeeded." >&2
421 RESULT=1
422 fi
423
424 rm -f file-small _file-small_ missing-file
425 done
426
427 # Test the ability of inetd to reload configuration:
428 #
429 # Assign a new port in the configuration file. Send SIGHUP
430 # to inetd and check whether transmission of the small
431 # file used previously still succeeds.
432 #
433 PORT=`expr $PORT + 1 + ${RANDOM:-$$} % 521`
434
435 locate_port $PROTO $PORT &&
436 {
437 # Try a second port.
438 PORT=`expr $PORT + 97 + ${RANDOM:-$$} % 479`
439 # Disable subtest if still no free port.
440 locate_port $PROTO $PORT && do_conf_reload=false
441 }
442
443 $silence echo >&2
444
445 if $do_conf_reload; then
446 $silence echo >&2 'Testing altered and reloaded configuration.'
447 write_conf ||
448 {
449 echo >&2 'Could not rewrite configuration file for Inetd. Failing.'
450 exit 1
451 }
452
453 kill -HUP $inetd_pid
454 name=`echo "$FILELIST" | $SED 's/ .*//'`
455 for addr in $ADDRESSES; do
456 EFFORTS=`expr $EFFORTS + 1`
457 test -f "$name" && rm "$name"
458 echo "binary
459 get $name" | \
460 eval "$TFTP" ${VERBOSE:+-v} "$addr" $PORT $bucket
461 cmp "$TMPDIR/tftp-test/$name" "$name" 2>/dev/null
462 result=$?
463 if test $result -ne 0; then
464 test -z "$VERBOSE" || echo >&2 "Failed comparison for $addr/$name."
465 RESULT=$result
466 else
467 SUCCESSES=`expr $SUCCESSES + 1`
468 test -z "$VERBOSE" || echo >&2 "Success at new port for $addr/$name."
469 fi
470 done
471 else
472 $silence echo >&2 'Informational: Inhibiting config reload test.'
473 fi
474
475 if $do_secure_setting; then
476 # Allow an underprivileged process owner to read files.
477 chmod g=rx,o=rx $TMPDIR
478
479 : > "$INETD_CONF"
480
481 test "$TEST_IPV4" = "no" ||
482 cat >> "$INETD_CONF" <<-EOF
483 $PORT dgram ${PROTO}4 wait $USER $TFTPD tftpd ${LOGGING+"-l"} -s $TMPDIR /tftp-test
484 EOF
485
486 test "$TEST_IPV6" = "no" ||
487 cat >> "$INETD_CONF" <<-EOF
488 $PORT dgram ${PROTO}6 wait $USER $TFTPD tftpd ${LOGGING+"-l"} -s $TMPDIR /tftp-test
489 EOF
490
491 # Let inetd reload configuration.
492 kill -HUP $inetd_pid
493
494 # Test two files for each address: file-small and asciifile.txt
495 #
496 name=`echo $FILELIST | $SED 's/ .*//'`
497
498 for addr in $ADDRESSES; do
499 rm -f "$name" "$ASCIIFILE"
500 EFFORTS=`expr $EFFORTS + 2`
501
502 cat <<-EOT |
503 binary
504 get $name
505 ascii
506 get /tftp-test/$ASCIIFILE
507 EOT
508 eval "$TFTP" ${VERBOSE:+-v} "$addr" $PORT $bucket
509
510 cmp "$TMPDIR/tftp-test/$name" "$name" 2>/dev/null
511 result=$?
512
513 if test $? -ne 0; then
514 $silence echo >&2 "Failed chrooted access to $addr:$name."
515 RESULT=$result
516 else
517 $silence echo >&2 "Success with chrooted access to $addr:$name."
518 SUCCESSES=`expr $SUCCESSES + 1`
519 fi
520
521 cmp "$TMPDIR/tftp-test/$ASCIIFILE" "$ASCIIFILE" 2>/dev/null
522 result=$?
523
524 if test $? -ne 0; then
525 $silence echo >&2 \
526 "Failed chrooted access to $addr:/tftp-test/$ASCIIFILE."
527 RESULT=$result
528 else
529 $silence echo >&2 \
530 "Success with chrooted $addr:/tftp-test/$ASCIIFILE."
531 SUCCESSES=`expr $SUCCESSES + 1`
532 fi
533 done # addr in ADDRESSES
534 else
535 $silence echo >&2 'Informational: Inhibiting chroot test.'
536 fi
537
538 # Minimal clean up. Main work in posttesting().
539 $silence echo
540 test $RESULT -eq 0 && test $SUCCESSES -eq $EFFORTS && $silence false \
541 || echo Test had $SUCCESSES successes out of $EFFORTS cases.
542
543 exit $RESULT