1313535Sngie#       $NetBSD: t_tcpip.sh,v 1.18 2016/08/13 11:22:11 christos Exp $
2272343Sngie#
3272343Sngie# Copyright (c) 2011 The NetBSD Foundation, Inc.
4272343Sngie# All rights reserved.
5272343Sngie#
6272343Sngie# Redistribution and use in source and binary forms, with or without
7272343Sngie# modification, are permitted provided that the following conditions
8272343Sngie# are met:
9272343Sngie# 1. Redistributions of source code must retain the above copyright
10272343Sngie#    notice, this list of conditions and the following disclaimer.
11272343Sngie# 2. Redistributions in binary form must reproduce the above copyright
12272343Sngie#    notice, this list of conditions and the following disclaimer in the
13272343Sngie#    documentation and/or other materials provided with the distribution.
14272343Sngie#
15272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18272343Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25272343Sngie# POSSIBILITY OF SUCH DAMAGE.
26272343Sngie#
27272343Sngie
28313535Sngierumpnetlibs="-lrumpnet -lrumpnet_net -lrumpnet_netinet6 -lrumpnet_netinet"
29313535Sngierumpnetsrv="rump_server $rumpnetlibs -lrumpdev"
30272343Sngieexport RUMP_SERVER=unix://csock
31272343Sngie
32272343Sngieatf_test_case http cleanup
33272343Sngiehttp_head()
34272343Sngie{
35272343Sngie        atf_set "descr" "Start hijacked httpd and get webpage from it"
36272343Sngie}
37272343Sngie
38272343Sngiehttp_body()
39272343Sngie{
40272343Sngie
41313535Sngie	atf_check -s exit:0 ${rumpnetsrv} ${RUMP_SERVER}
42272343Sngie
43272343Sngie	# start bozo in daemon mode
44272343Sngie	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so \
45272343Sngie	    /usr/libexec/httpd -P ./httpd.pid -b -s $(atf_get_srcdir)
46272343Sngie
47272343Sngie	atf_check -s exit:0 -o file:"$(atf_get_srcdir)/netstat.expout" \
48272343Sngie	    rump.netstat -a
49272343Sngie
50272343Sngie	# get the webpage
51272343Sngie	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so 	\
52272343Sngie	    $(atf_get_srcdir)/h_netget 127.0.0.1 80 webfile
53272343Sngie
54272343Sngie	# check that we got what we wanted
55272343Sngie	atf_check -o match:'HTTP/1.0 200 OK' cat webfile
56272343Sngie	atf_check -o match:'Content-Length: 95' cat webfile
57313498Sngie	blank_line_re="$(printf '^\r$')" # matches a line with only <CR><LF>
58272343Sngie	atf_check -o file:"$(atf_get_srcdir)/index.html" \
59313498Sngie	    sed -n "1,/${blank_line_re}/!p" webfile
60272343Sngie}
61272343Sngie
62272343Sngiehttp_cleanup()
63272343Sngie{
64272343Sngie	if [ -f httpd.pid ]; then
65272343Sngie		kill -9 "$(cat httpd.pid)"
66272343Sngie		rm -f httpd.pid
67272343Sngie	fi
68272343Sngie
69272343Sngie	rump.halt
70272343Sngie}
71272343Sngie
72272343Sngie#
73272343Sngie# Starts a SSH server and sets up the client to access it.
74272343Sngie# Authentication is allowed and done using an RSA key exclusively, which
75272343Sngie# is generated on the fly as part of the test case.
76272343Sngie# XXX: Ideally, all the tests in this test program should be able to share
77272343Sngie# the generated key, because creating it can be a very slow process on some
78272343Sngie# machines.
79272343Sngie#
80272343Sngie# XXX2: copypasted from jmmv's sshd thingamob in the psshfs test.
81272343Sngie# ideally code (and keys, like jmmv notes above) could be shared
82272343Sngie#
83272343Sngiestart_sshd() {
84272343Sngie	echo "Setting up SSH server configuration"
85272343Sngie	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
86272343Sngie	    $(atf_get_srcdir)/sshd_config.in >sshd_config || \
87272343Sngie	    atf_fail "Failed to create sshd_config"
88272343Sngie	atf_check -s ignore -o empty -e ignore \
89272343Sngie	    cp $(atf_get_srcdir)/ssh_host_key .
90272343Sngie	atf_check -s ignore -o empty -e ignore \
91272343Sngie	    cp $(atf_get_srcdir)/ssh_host_key.pub .
92272343Sngie	atf_check -s eq:0 -o empty -e empty chmod 400 ssh_host_key
93272343Sngie	atf_check -s eq:0 -o empty -e empty chmod 444 ssh_host_key.pub
94272343Sngie
95272343Sngie        env LD_PRELOAD=/usr/lib/librumphijack.so \
96272343Sngie	    /usr/sbin/sshd -e -f ./sshd_config
97272343Sngie	while [ ! -f sshd.pid ]; do
98272343Sngie		sleep 0.01
99272343Sngie	done
100272343Sngie	echo "SSH server started (pid $(cat sshd.pid))"
101272343Sngie
102272343Sngie	echo "Setting up SSH client configuration"
103272343Sngie	atf_check -s eq:0 -o empty -e empty \
104272343Sngie	    ssh-keygen -f ssh_user_key -t rsa -b 1024 -N "" -q
105272343Sngie	atf_check -s eq:0 -o empty -e empty \
106272343Sngie	    cp ssh_user_key.pub authorized_keys
107272343Sngie	echo "127.0.0.1,localhost,::1 " \
108272343Sngie	    "$(cat $(atf_get_srcdir)/ssh_host_key.pub)" >known_hosts || \
109272343Sngie	    atf_fail "Failed to create known_hosts"
110272343Sngie	atf_check -s eq:0 -o empty -e empty chmod 600 authorized_keys
111272343Sngie	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
112272343Sngie	    $(atf_get_srcdir)/ssh_config.in >ssh_config || \
113272343Sngie	    atf_fail "Failed to create ssh_config"
114272343Sngie	
115272343Sngie	echo "sshd running"
116272343Sngie}
117272343Sngie
118272343Sngieatf_test_case ssh cleanup
119272343Sngiessh_head()
120272343Sngie{
121272343Sngie        atf_set "descr" "Test that hijacked ssh/sshd works"
122272343Sngie}
123272343Sngie
124272343Sngiessh_body()
125272343Sngie{
126313498Sngie	atf_expect_fail "PR lib/50174"
127272343Sngie
128272343Sngie	atf_check -s exit:0 ${rumpnetsrv} ${RUMP_SERVER}
129272343Sngie	# make sure clients die after we nuke the server
130272343Sngie	export RUMPHIJACK_RETRYCONNECT='die'
131272343Sngie
132272343Sngie	start_sshd
133272343Sngie
134272343Sngie	# create some sort of directory for us to "ls"
135272343Sngie	mkdir testdir
136272343Sngie	cd testdir
137272343Sngie	jot 11 | xargs touch
138272343Sngie	jot 11 12 | xargs mkdir
139272343Sngie	cd ..
140272343Sngie
141272343Sngie	atf_check -s exit:0 -o save:ssh.out				\
142272343Sngie	    env LD_PRELOAD=/usr/lib/librumphijack.so			\
143272343Sngie	    ssh -T -F ssh_config 127.0.0.1 env BLOCKSIZE=512		\
144272343Sngie	    ls -li $(pwd)/testdir
145272343Sngie	atf_check -s exit:0 -o file:ssh.out env BLOCKSIZE=512 		\
146272343Sngie	    ls -li $(pwd)/testdir
147272343Sngie}
148272343Sngie
149272343Sngiessh_cleanup()
150272343Sngie{
151272343Sngie	rump.halt
152272343Sngie	# sshd dies due to RUMPHIJACK_RETRYCONNECT=1d6
153272343Sngie}
154272343Sngie
155272343Sngietest_nfs()
156272343Sngie{
157272343Sngie
158272343Sngie	magicstr='wind in my hair'
159272343Sngie	# create ffs file system we'll be serving from
160272343Sngie	atf_check -s exit:0 -o ignore newfs -F -s 10000 ffs.img
161272343Sngie
162272343Sngie	# start nfs kernel server.  this is a mouthful
163272343Sngie	export RUMP_SERVER=unix://serversock
164272343Sngie	atf_check -s exit:0 rump_server $* ${RUMP_SERVER}
165272343Sngie
166272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 create
167272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 linkstr shmbus
168272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.1.1.1
169272343Sngie
170272343Sngie	export RUMPHIJACK_RETRYCONNECT=die
171272343Sngie	export LD_PRELOAD=/usr/lib/librumphijack.so
172272343Sngie
173272343Sngie	atf_check -s exit:0 mkdir -p /rump/var/run
174272343Sngie	atf_check -s exit:0 mkdir -p /rump/var/db
175272343Sngie	atf_check -s exit:0 touch /rump/var/db/mountdtab
176272343Sngie	atf_check -s exit:0 mkdir /rump/etc
177272343Sngie	atf_check -s exit:0 mkdir /rump/export
178272343Sngie
179272343Sngie	atf_check -s exit:0 -x \
180272343Sngie	    'echo "/export -noresvport -noresvmnt 10.1.1.100" | \
181272343Sngie		dd of=/rump/etc/exports 2> /dev/null'
182272343Sngie
183313498Sngie	atf_check -s exit:0 rump.sysctl -q -w kern.module.autoload=1
184313498Sngie
185272343Sngie	atf_check -s exit:0 -e ignore mount_ffs /dk /rump/export
186272343Sngie	atf_check -s exit:0 -x "echo ${magicstr} > /rump/export/im_alive"
187272343Sngie
188272343Sngie	# start rpcbind.  we want /var/run/rpcbind.sock
189272343Sngie	export RUMPHIJACK='blanket=/var/run,socket=all' 
190272343Sngie	atf_check -s exit:0 rpcbind
191272343Sngie
192272343Sngie	# ok, then we want mountd in the similar fashion
193272343Sngie	export RUMPHIJACK='blanket=/var/run:/var/db:/export,socket=all,path=/rump,vfs=all'
194272343Sngie	atf_check -s exit:0 mountd /rump/etc/exports
195272343Sngie
196272343Sngie	# finally, le nfschuck
197272343Sngie	export RUMPHIJACK='blanket=/var/run,socket=all,vfs=all'
198272343Sngie	atf_check -s exit:0 nfsd
199272343Sngie
200272343Sngie	#
201272343Sngie	# now, time for the client server and associated madness.
202272343Sngie	#
203272343Sngie
204272343Sngie	export RUMP_SERVER=unix://clientsock
205272343Sngie	unset RUMPHIJACK
206272343Sngie	unset LD_PRELOAD
207272343Sngie
208272343Sngie	# at least the kernel server is easier
209313498Sngie	atf_check -s exit:0 rump_server -lrumpvfs -lrumpnet -lrumpdev	\
210272343Sngie	    -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif -lrumpfs_nfs\
211272343Sngie	    ${RUMP_SERVER}
212272343Sngie
213272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 create
214272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 linkstr shmbus
215272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.1.1.100
216272343Sngie
217272343Sngie	export LD_PRELOAD=/usr/lib/librumphijack.so
218272343Sngie
219272343Sngie	atf_check -s exit:0 mkdir /rump/mnt
220272343Sngie	atf_check -s exit:0 mount_nfs 10.1.1.1:/export /rump/mnt
221272343Sngie
222272343Sngie	atf_check -s exit:0 -o inline:"${magicstr}\n" cat /rump/mnt/im_alive
223272343Sngie	atf_check -s exit:0 -o match:'.*im_alive$' ls -l /rump/mnt/im_alive
224272343Sngie}
225272343Sngie
226272343Sngie
227272343Sngieatf_test_case nfs cleanup
228272343Sngienfs_head()
229272343Sngie{
230272343Sngie        atf_set "descr" "Test hijacked nfsd and mount_nfs"
231272343Sngie}
232272343Sngie
233272343Sngienfs_body()
234272343Sngie{
235272343Sngie	test_nfs -lrumpvfs -lrumpdev -lrumpnet -lrumpnet_net		\
236313498Sngie	    -lrumpnet_netinet -lrumpnet_local -lrumpnet_shmif -lrumpdev	\
237272343Sngie	    -lrumpdev_disk -lrumpfs_ffs -lrumpfs_nfs -lrumpfs_nfsserver	\
238272343Sngie	    -d key=/dk,hostpath=ffs.img,size=host
239272343Sngie}
240272343Sngie
241272343Sngienfs_cleanup()
242272343Sngie{
243272343Sngie	RUMP_SERVER=unix://serversock rump.halt 2> /dev/null
244272343Sngie	RUMP_SERVER=unix://clientsock rump.halt 2> /dev/null
245272343Sngie	:
246272343Sngie}
247272343Sngie
248272343Sngieatf_test_case nfs_autoload cleanup
249272343Sngienfs_autoload_head()
250272343Sngie{
251272343Sngie        atf_set "descr" "Test hijacked nfsd with autoload from /stand"
252272343Sngie}
253272343Sngie
254272343Sngienfs_autoload_body()
255272343Sngie{
256272343Sngie	[ `uname -m` = "i386" ] || atf_skip "test currently valid only on i386"
257272343Sngie	test_nfs -lrumpvfs -lrumpdev -lrumpnet -lrumpnet_net		\
258313498Sngie	    -lrumpnet_netinet -lrumpnet_local -lrumpnet_shmif -lrumpdev	\
259272343Sngie	    -lrumpdev_disk -d key=/dk,hostpath=ffs.img,size=host
260272343Sngie}
261272343Sngie
262272343Sngienfs_autoload_cleanup()
263272343Sngie{
264272343Sngie	nfs_cleanup
265272343Sngie}
266272343Sngie
267272343Sngieatf_init_test_cases()
268272343Sngie{
269272343Sngie	atf_add_test_case http
270272343Sngie	atf_add_test_case ssh
271272343Sngie	atf_add_test_case nfs
272272343Sngie	atf_add_test_case nfs_autoload
273272343Sngie}
274