xref: /minix3/tests/lib/librumphijack/t_tcpip.sh (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc#       $NetBSD: t_tcpip.sh,v 1.15 2015/08/26 09:19:20 martin Exp $
211be35a1SLionel Sambuc#
311be35a1SLionel Sambuc# Copyright (c) 2011 The NetBSD Foundation, Inc.
411be35a1SLionel Sambuc# All rights reserved.
511be35a1SLionel Sambuc#
611be35a1SLionel Sambuc# Redistribution and use in source and binary forms, with or without
711be35a1SLionel Sambuc# modification, are permitted provided that the following conditions
811be35a1SLionel Sambuc# are met:
911be35a1SLionel Sambuc# 1. Redistributions of source code must retain the above copyright
1011be35a1SLionel Sambuc#    notice, this list of conditions and the following disclaimer.
1111be35a1SLionel Sambuc# 2. Redistributions in binary form must reproduce the above copyright
1211be35a1SLionel Sambuc#    notice, this list of conditions and the following disclaimer in the
1311be35a1SLionel Sambuc#    documentation and/or other materials provided with the distribution.
1411be35a1SLionel Sambuc#
1511be35a1SLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1611be35a1SLionel Sambuc# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1711be35a1SLionel Sambuc# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1811be35a1SLionel Sambuc# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1911be35a1SLionel Sambuc# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2011be35a1SLionel Sambuc# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2111be35a1SLionel Sambuc# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2211be35a1SLionel Sambuc# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2311be35a1SLionel Sambuc# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2411be35a1SLionel Sambuc# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2511be35a1SLionel Sambuc# POSSIBILITY OF SUCH DAMAGE.
2611be35a1SLionel Sambuc#
2711be35a1SLionel Sambuc
2811be35a1SLionel Sambucrumpnetsrv='rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet'
2911be35a1SLionel Sambucexport RUMP_SERVER=unix://csock
3011be35a1SLionel Sambuc
3111be35a1SLionel Sambucatf_test_case http cleanup
3211be35a1SLionel Sambuchttp_head()
3311be35a1SLionel Sambuc{
3411be35a1SLionel Sambuc        atf_set "descr" "Start hijacked httpd and get webpage from it"
3511be35a1SLionel Sambuc}
3611be35a1SLionel Sambuc
3711be35a1SLionel Sambuchttp_body()
3811be35a1SLionel Sambuc{
3911be35a1SLionel Sambuc
40*0a6a1f1dSLionel Sambuc	atf_check -s exit:0 ${rumpnetsrv} -lrumpnet_netinet6 ${RUMP_SERVER}
4111be35a1SLionel Sambuc
4211be35a1SLionel Sambuc	# start bozo in daemon mode
4311be35a1SLionel Sambuc	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so \
4411be35a1SLionel Sambuc	    /usr/libexec/httpd -P ./httpd.pid -b -s $(atf_get_srcdir)
4511be35a1SLionel Sambuc
4611be35a1SLionel Sambuc	atf_check -s exit:0 -o file:"$(atf_get_srcdir)/netstat.expout" \
4711be35a1SLionel Sambuc	    rump.netstat -a
4811be35a1SLionel Sambuc
4911be35a1SLionel Sambuc	# get the webpage
5011be35a1SLionel Sambuc	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so 	\
5111be35a1SLionel Sambuc	    $(atf_get_srcdir)/h_netget 127.0.0.1 80 webfile
5211be35a1SLionel Sambuc
5311be35a1SLionel Sambuc	# check that we got what we wanted
5411be35a1SLionel Sambuc	atf_check -o match:'HTTP/1.0 200 OK' cat webfile
5511be35a1SLionel Sambuc	atf_check -o match:'Content-Length: 95' cat webfile
56*0a6a1f1dSLionel Sambuc	blank_line_re="$(printf '^\r$')" # matches a line with only <CR><LF>
5711be35a1SLionel Sambuc	atf_check -o file:"$(atf_get_srcdir)/index.html" \
58*0a6a1f1dSLionel Sambuc	    sed -n "1,/${blank_line_re}/!p" webfile
5911be35a1SLionel Sambuc}
6011be35a1SLionel Sambuc
6111be35a1SLionel Sambuchttp_cleanup()
6211be35a1SLionel Sambuc{
6311be35a1SLionel Sambuc	if [ -f httpd.pid ]; then
6411be35a1SLionel Sambuc		kill -9 "$(cat httpd.pid)"
6511be35a1SLionel Sambuc		rm -f httpd.pid
6611be35a1SLionel Sambuc	fi
6711be35a1SLionel Sambuc
6811be35a1SLionel Sambuc	rump.halt
6911be35a1SLionel Sambuc}
7011be35a1SLionel Sambuc
7111be35a1SLionel Sambuc#
7211be35a1SLionel Sambuc# Starts a SSH server and sets up the client to access it.
7311be35a1SLionel Sambuc# Authentication is allowed and done using an RSA key exclusively, which
7411be35a1SLionel Sambuc# is generated on the fly as part of the test case.
7511be35a1SLionel Sambuc# XXX: Ideally, all the tests in this test program should be able to share
7611be35a1SLionel Sambuc# the generated key, because creating it can be a very slow process on some
7711be35a1SLionel Sambuc# machines.
7811be35a1SLionel Sambuc#
7911be35a1SLionel Sambuc# XXX2: copypasted from jmmv's sshd thingamob in the psshfs test.
8011be35a1SLionel Sambuc# ideally code (and keys, like jmmv notes above) could be shared
8111be35a1SLionel Sambuc#
8211be35a1SLionel Sambucstart_sshd() {
8311be35a1SLionel Sambuc	echo "Setting up SSH server configuration"
8411be35a1SLionel Sambuc	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
8511be35a1SLionel Sambuc	    $(atf_get_srcdir)/sshd_config.in >sshd_config || \
8611be35a1SLionel Sambuc	    atf_fail "Failed to create sshd_config"
8711be35a1SLionel Sambuc	atf_check -s ignore -o empty -e ignore \
8811be35a1SLionel Sambuc	    cp $(atf_get_srcdir)/ssh_host_key .
8911be35a1SLionel Sambuc	atf_check -s ignore -o empty -e ignore \
9011be35a1SLionel Sambuc	    cp $(atf_get_srcdir)/ssh_host_key.pub .
9111be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty chmod 400 ssh_host_key
9211be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty chmod 444 ssh_host_key.pub
9311be35a1SLionel Sambuc
9411be35a1SLionel Sambuc        env LD_PRELOAD=/usr/lib/librumphijack.so \
9511be35a1SLionel Sambuc	    /usr/sbin/sshd -e -f ./sshd_config
9611be35a1SLionel Sambuc	while [ ! -f sshd.pid ]; do
9711be35a1SLionel Sambuc		sleep 0.01
9811be35a1SLionel Sambuc	done
9911be35a1SLionel Sambuc	echo "SSH server started (pid $(cat sshd.pid))"
10011be35a1SLionel Sambuc
10111be35a1SLionel Sambuc	echo "Setting up SSH client configuration"
10211be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty \
10311be35a1SLionel Sambuc	    ssh-keygen -f ssh_user_key -t rsa -b 1024 -N "" -q
10411be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty \
10511be35a1SLionel Sambuc	    cp ssh_user_key.pub authorized_keys
10611be35a1SLionel Sambuc	echo "127.0.0.1,localhost,::1 " \
10711be35a1SLionel Sambuc	    "$(cat $(atf_get_srcdir)/ssh_host_key.pub)" >known_hosts || \
10811be35a1SLionel Sambuc	    atf_fail "Failed to create known_hosts"
10911be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty chmod 600 authorized_keys
11011be35a1SLionel Sambuc	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
11111be35a1SLionel Sambuc	    $(atf_get_srcdir)/ssh_config.in >ssh_config || \
11211be35a1SLionel Sambuc	    atf_fail "Failed to create ssh_config"
11311be35a1SLionel Sambuc
11411be35a1SLionel Sambuc	echo "sshd running"
11511be35a1SLionel Sambuc}
11611be35a1SLionel Sambuc
11711be35a1SLionel Sambucatf_test_case ssh cleanup
11811be35a1SLionel Sambucssh_head()
11911be35a1SLionel Sambuc{
12011be35a1SLionel Sambuc        atf_set "descr" "Test that hijacked ssh/sshd works"
12111be35a1SLionel Sambuc}
12211be35a1SLionel Sambuc
12311be35a1SLionel Sambucssh_body()
12411be35a1SLionel Sambuc{
125*0a6a1f1dSLionel Sambuc	atf_expect_fail "PR lib/50174"
12611be35a1SLionel Sambuc
12711be35a1SLionel Sambuc	atf_check -s exit:0 ${rumpnetsrv} ${RUMP_SERVER}
12811be35a1SLionel Sambuc	# make sure clients die after we nuke the server
12911be35a1SLionel Sambuc	export RUMPHIJACK_RETRYCONNECT='die'
13011be35a1SLionel Sambuc
13111be35a1SLionel Sambuc	start_sshd
13211be35a1SLionel Sambuc
13311be35a1SLionel Sambuc	# create some sort of directory for us to "ls"
13411be35a1SLionel Sambuc	mkdir testdir
13511be35a1SLionel Sambuc	cd testdir
13611be35a1SLionel Sambuc	jot 11 | xargs touch
13711be35a1SLionel Sambuc	jot 11 12 | xargs mkdir
13811be35a1SLionel Sambuc	cd ..
13911be35a1SLionel Sambuc
14011be35a1SLionel Sambuc	atf_check -s exit:0 -o save:ssh.out				\
14111be35a1SLionel Sambuc	    env LD_PRELOAD=/usr/lib/librumphijack.so			\
14211be35a1SLionel Sambuc	    ssh -T -F ssh_config 127.0.0.1 env BLOCKSIZE=512		\
14311be35a1SLionel Sambuc	    ls -li $(pwd)/testdir
14411be35a1SLionel Sambuc	atf_check -s exit:0 -o file:ssh.out env BLOCKSIZE=512 		\
14511be35a1SLionel Sambuc	    ls -li $(pwd)/testdir
14611be35a1SLionel Sambuc}
14711be35a1SLionel Sambuc
14811be35a1SLionel Sambucssh_cleanup()
14911be35a1SLionel Sambuc{
15011be35a1SLionel Sambuc	rump.halt
15111be35a1SLionel Sambuc	# sshd dies due to RUMPHIJACK_RETRYCONNECT=1d6
15211be35a1SLionel Sambuc}
15311be35a1SLionel Sambuc
15411be35a1SLionel Sambuctest_nfs()
15511be35a1SLionel Sambuc{
15611be35a1SLionel Sambuc
15711be35a1SLionel Sambuc	magicstr='wind in my hair'
15811be35a1SLionel Sambuc	# create ffs file system we'll be serving from
15911be35a1SLionel Sambuc	atf_check -s exit:0 -o ignore newfs -F -s 10000 ffs.img
16011be35a1SLionel Sambuc
16111be35a1SLionel Sambuc	# start nfs kernel server.  this is a mouthful
16211be35a1SLionel Sambuc	export RUMP_SERVER=unix://serversock
16311be35a1SLionel Sambuc	atf_check -s exit:0 rump_server $* ${RUMP_SERVER}
16411be35a1SLionel Sambuc
16511be35a1SLionel Sambuc	atf_check -s exit:0 rump.ifconfig shmif0 create
16611be35a1SLionel Sambuc	atf_check -s exit:0 rump.ifconfig shmif0 linkstr shmbus
16711be35a1SLionel Sambuc	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.1.1.1
16811be35a1SLionel Sambuc
16911be35a1SLionel Sambuc	export RUMPHIJACK_RETRYCONNECT=die
17011be35a1SLionel Sambuc	export LD_PRELOAD=/usr/lib/librumphijack.so
17111be35a1SLionel Sambuc
17211be35a1SLionel Sambuc	atf_check -s exit:0 mkdir -p /rump/var/run
17311be35a1SLionel Sambuc	atf_check -s exit:0 mkdir -p /rump/var/db
17411be35a1SLionel Sambuc	atf_check -s exit:0 touch /rump/var/db/mountdtab
17511be35a1SLionel Sambuc	atf_check -s exit:0 mkdir /rump/etc
17611be35a1SLionel Sambuc	atf_check -s exit:0 mkdir /rump/export
17711be35a1SLionel Sambuc
17811be35a1SLionel Sambuc	atf_check -s exit:0 -x \
17911be35a1SLionel Sambuc	    'echo "/export -noresvport -noresvmnt 10.1.1.100" | \
18011be35a1SLionel Sambuc		dd of=/rump/etc/exports 2> /dev/null'
18111be35a1SLionel Sambuc
18211be35a1SLionel Sambuc	atf_check -s exit:0 -e ignore mount_ffs /dk /rump/export
18311be35a1SLionel Sambuc	atf_check -s exit:0 -x "echo ${magicstr} > /rump/export/im_alive"
18411be35a1SLionel Sambuc
18511be35a1SLionel Sambuc	# start rpcbind.  we want /var/run/rpcbind.sock
18611be35a1SLionel Sambuc	export RUMPHIJACK='blanket=/var/run,socket=all'
18711be35a1SLionel Sambuc	atf_check -s exit:0 rpcbind
18811be35a1SLionel Sambuc
18911be35a1SLionel Sambuc	# ok, then we want mountd in the similar fashion
19011be35a1SLionel Sambuc	export RUMPHIJACK='blanket=/var/run:/var/db:/export,socket=all,path=/rump,vfs=all'
19111be35a1SLionel Sambuc	atf_check -s exit:0 mountd /rump/etc/exports
19211be35a1SLionel Sambuc
19311be35a1SLionel Sambuc	# finally, le nfschuck
19411be35a1SLionel Sambuc	export RUMPHIJACK='blanket=/var/run,socket=all,vfs=all'
19511be35a1SLionel Sambuc	atf_check -s exit:0 nfsd
19611be35a1SLionel Sambuc
19711be35a1SLionel Sambuc	#
19811be35a1SLionel Sambuc	# now, time for the client server and associated madness.
19911be35a1SLionel Sambuc	#
20011be35a1SLionel Sambuc
20111be35a1SLionel Sambuc	export RUMP_SERVER=unix://clientsock
20211be35a1SLionel Sambuc	unset RUMPHIJACK
20311be35a1SLionel Sambuc	unset LD_PRELOAD
20411be35a1SLionel Sambuc
20511be35a1SLionel Sambuc	# at least the kernel server is easier
20611be35a1SLionel Sambuc	atf_check -s exit:0 rump_server -lrumpvfs -lrumpnet		\
20711be35a1SLionel Sambuc	    -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif -lrumpfs_nfs\
20811be35a1SLionel Sambuc	    ${RUMP_SERVER}
20911be35a1SLionel Sambuc
21011be35a1SLionel Sambuc	atf_check -s exit:0 rump.ifconfig shmif0 create
21111be35a1SLionel Sambuc	atf_check -s exit:0 rump.ifconfig shmif0 linkstr shmbus
21211be35a1SLionel Sambuc	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.1.1.100
21311be35a1SLionel Sambuc
21411be35a1SLionel Sambuc	export LD_PRELOAD=/usr/lib/librumphijack.so
21511be35a1SLionel Sambuc
21611be35a1SLionel Sambuc	atf_check -s exit:0 mkdir /rump/mnt
21711be35a1SLionel Sambuc	atf_check -s exit:0 mount_nfs 10.1.1.1:/export /rump/mnt
21811be35a1SLionel Sambuc
21911be35a1SLionel Sambuc	atf_check -s exit:0 -o inline:"${magicstr}\n" cat /rump/mnt/im_alive
22011be35a1SLionel Sambuc	atf_check -s exit:0 -o match:'.*im_alive$' ls -l /rump/mnt/im_alive
22111be35a1SLionel Sambuc}
22211be35a1SLionel Sambuc
22311be35a1SLionel Sambuc
22411be35a1SLionel Sambucatf_test_case nfs cleanup
22511be35a1SLionel Sambucnfs_head()
22611be35a1SLionel Sambuc{
22711be35a1SLionel Sambuc        atf_set "descr" "Test hijacked nfsd and mount_nfs"
22811be35a1SLionel Sambuc}
22911be35a1SLionel Sambuc
23011be35a1SLionel Sambucnfs_body()
23111be35a1SLionel Sambuc{
23211be35a1SLionel Sambuc	test_nfs -lrumpvfs -lrumpdev -lrumpnet -lrumpnet_net		\
23311be35a1SLionel Sambuc	    -lrumpnet_netinet -lrumpnet_local -lrumpnet_shmif		\
23411be35a1SLionel Sambuc	    -lrumpdev_disk -lrumpfs_ffs -lrumpfs_nfs -lrumpfs_nfsserver	\
23511be35a1SLionel Sambuc	    -d key=/dk,hostpath=ffs.img,size=host
23611be35a1SLionel Sambuc}
23711be35a1SLionel Sambuc
23811be35a1SLionel Sambucnfs_cleanup()
23911be35a1SLionel Sambuc{
24011be35a1SLionel Sambuc	RUMP_SERVER=unix://serversock rump.halt 2> /dev/null
24111be35a1SLionel Sambuc	RUMP_SERVER=unix://clientsock rump.halt 2> /dev/null
24211be35a1SLionel Sambuc	:
24311be35a1SLionel Sambuc}
24411be35a1SLionel Sambuc
24511be35a1SLionel Sambucatf_test_case nfs_autoload cleanup
24611be35a1SLionel Sambucnfs_autoload_head()
24711be35a1SLionel Sambuc{
24811be35a1SLionel Sambuc        atf_set "descr" "Test hijacked nfsd with autoload from /stand"
24911be35a1SLionel Sambuc}
25011be35a1SLionel Sambuc
25111be35a1SLionel Sambucnfs_autoload_body()
25211be35a1SLionel Sambuc{
25311be35a1SLionel Sambuc	[ `uname -m` = "i386" ] || atf_skip "test currently valid only on i386"
25411be35a1SLionel Sambuc	test_nfs -lrumpvfs -lrumpdev -lrumpnet -lrumpnet_net		\
25511be35a1SLionel Sambuc	    -lrumpnet_netinet -lrumpnet_local -lrumpnet_shmif		\
25611be35a1SLionel Sambuc	    -lrumpdev_disk -d key=/dk,hostpath=ffs.img,size=host
25711be35a1SLionel Sambuc}
25811be35a1SLionel Sambuc
25911be35a1SLionel Sambucnfs_autoload_cleanup()
26011be35a1SLionel Sambuc{
26111be35a1SLionel Sambuc	nfs_cleanup
26211be35a1SLionel Sambuc}
26311be35a1SLionel Sambuc
26411be35a1SLionel Sambucatf_init_test_cases()
26511be35a1SLionel Sambuc{
26611be35a1SLionel Sambuc	atf_add_test_case http
26711be35a1SLionel Sambuc	atf_add_test_case ssh
26811be35a1SLionel Sambuc	atf_add_test_case nfs
26911be35a1SLionel Sambuc	atf_add_test_case nfs_autoload
27011be35a1SLionel Sambuc}
271