1272343Sngie#       $NetBSD: t_tcpip.sh,v 1.13 2014/01/03 13:18:00 pooka 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
28272343Sngierumpnetsrv='rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet'
29272343Sngieexport RUMP_SERVER=unix://csock
30272343Sngie
31272343Sngieatf_test_case http cleanup
32272343Sngiehttp_head()
33272343Sngie{
34272343Sngie        atf_set "descr" "Start hijacked httpd and get webpage from it"
35272343Sngie}
36272343Sngie
37272343Sngiehttp_body()
38272343Sngie{
39272343Sngie
40272343Sngie	atf_check -s exit:0 ${rumpnetsrv} -lrumpnet_netinet6 ${RUMP_SERVER}
41272343Sngie
42272343Sngie	# start bozo in daemon mode
43272343Sngie	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so \
44272343Sngie	    /usr/libexec/httpd -P ./httpd.pid -b -s $(atf_get_srcdir)
45272343Sngie
46272343Sngie	atf_check -s exit:0 -o file:"$(atf_get_srcdir)/netstat.expout" \
47272343Sngie	    rump.netstat -a
48272343Sngie
49272343Sngie	# get the webpage
50272343Sngie	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so 	\
51272343Sngie	    $(atf_get_srcdir)/h_netget 127.0.0.1 80 webfile
52272343Sngie
53272343Sngie	# check that we got what we wanted
54272343Sngie	atf_check -o match:'HTTP/1.0 200 OK' cat webfile
55272343Sngie	atf_check -o match:'Content-Length: 95' cat webfile
56272343Sngie	atf_check -o file:"$(atf_get_srcdir)/index.html" \
57272343Sngie	    sed -n '1,/^$/!p' webfile
58272343Sngie}
59272343Sngie
60272343Sngiehttp_cleanup()
61272343Sngie{
62272343Sngie	if [ -f httpd.pid ]; then
63272343Sngie		kill -9 "$(cat httpd.pid)"
64272343Sngie		rm -f httpd.pid
65272343Sngie	fi
66272343Sngie
67272343Sngie	rump.halt
68272343Sngie}
69272343Sngie
70272343Sngie#
71272343Sngie# Starts a SSH server and sets up the client to access it.
72272343Sngie# Authentication is allowed and done using an RSA key exclusively, which
73272343Sngie# is generated on the fly as part of the test case.
74272343Sngie# XXX: Ideally, all the tests in this test program should be able to share
75272343Sngie# the generated key, because creating it can be a very slow process on some
76272343Sngie# machines.
77272343Sngie#
78272343Sngie# XXX2: copypasted from jmmv's sshd thingamob in the psshfs test.
79272343Sngie# ideally code (and keys, like jmmv notes above) could be shared
80272343Sngie#
81272343Sngiestart_sshd() {
82272343Sngie	echo "Setting up SSH server configuration"
83272343Sngie	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
84272343Sngie	    $(atf_get_srcdir)/sshd_config.in >sshd_config || \
85272343Sngie	    atf_fail "Failed to create sshd_config"
86272343Sngie	atf_check -s ignore -o empty -e ignore \
87272343Sngie	    cp $(atf_get_srcdir)/ssh_host_key .
88272343Sngie	atf_check -s ignore -o empty -e ignore \
89272343Sngie	    cp $(atf_get_srcdir)/ssh_host_key.pub .
90272343Sngie	atf_check -s eq:0 -o empty -e empty chmod 400 ssh_host_key
91272343Sngie	atf_check -s eq:0 -o empty -e empty chmod 444 ssh_host_key.pub
92272343Sngie
93272343Sngie        env LD_PRELOAD=/usr/lib/librumphijack.so \
94272343Sngie	    /usr/sbin/sshd -e -f ./sshd_config
95272343Sngie	while [ ! -f sshd.pid ]; do
96272343Sngie		sleep 0.01
97272343Sngie	done
98272343Sngie	echo "SSH server started (pid $(cat sshd.pid))"
99272343Sngie
100272343Sngie	echo "Setting up SSH client configuration"
101272343Sngie	atf_check -s eq:0 -o empty -e empty \
102272343Sngie	    ssh-keygen -f ssh_user_key -t rsa -b 1024 -N "" -q
103272343Sngie	atf_check -s eq:0 -o empty -e empty \
104272343Sngie	    cp ssh_user_key.pub authorized_keys
105272343Sngie	echo "127.0.0.1,localhost,::1 " \
106272343Sngie	    "$(cat $(atf_get_srcdir)/ssh_host_key.pub)" >known_hosts || \
107272343Sngie	    atf_fail "Failed to create known_hosts"
108272343Sngie	atf_check -s eq:0 -o empty -e empty chmod 600 authorized_keys
109272343Sngie	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
110272343Sngie	    $(atf_get_srcdir)/ssh_config.in >ssh_config || \
111272343Sngie	    atf_fail "Failed to create ssh_config"
112272343Sngie	
113272343Sngie	echo "sshd running"
114272343Sngie}
115272343Sngie
116272343Sngieatf_test_case ssh cleanup
117272343Sngiessh_head()
118272343Sngie{
119272343Sngie        atf_set "descr" "Test that hijacked ssh/sshd works"
120272343Sngie}
121272343Sngie
122272343Sngiessh_body()
123272343Sngie{
124272343Sngie
125272343Sngie	atf_check -s exit:0 ${rumpnetsrv} ${RUMP_SERVER}
126272343Sngie	# make sure clients die after we nuke the server
127272343Sngie	export RUMPHIJACK_RETRYCONNECT='die'
128272343Sngie
129272343Sngie	start_sshd
130272343Sngie
131272343Sngie	# create some sort of directory for us to "ls"
132272343Sngie	mkdir testdir
133272343Sngie	cd testdir
134272343Sngie	jot 11 | xargs touch
135272343Sngie	jot 11 12 | xargs mkdir
136272343Sngie	cd ..
137272343Sngie
138272343Sngie	atf_check -s exit:0 -o save:ssh.out				\
139272343Sngie	    env LD_PRELOAD=/usr/lib/librumphijack.so			\
140272343Sngie	    ssh -T -F ssh_config 127.0.0.1 env BLOCKSIZE=512		\
141272343Sngie	    ls -li $(pwd)/testdir
142272343Sngie	atf_check -s exit:0 -o file:ssh.out env BLOCKSIZE=512 		\
143272343Sngie	    ls -li $(pwd)/testdir
144272343Sngie}
145272343Sngie
146272343Sngiessh_cleanup()
147272343Sngie{
148272343Sngie	rump.halt
149272343Sngie	# sshd dies due to RUMPHIJACK_RETRYCONNECT=1d6
150272343Sngie}
151272343Sngie
152272343Sngietest_nfs()
153272343Sngie{
154272343Sngie
155272343Sngie	magicstr='wind in my hair'
156272343Sngie	# create ffs file system we'll be serving from
157272343Sngie	atf_check -s exit:0 -o ignore newfs -F -s 10000 ffs.img
158272343Sngie
159272343Sngie	# start nfs kernel server.  this is a mouthful
160272343Sngie	export RUMP_SERVER=unix://serversock
161272343Sngie	atf_check -s exit:0 rump_server $* ${RUMP_SERVER}
162272343Sngie
163272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 create
164272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 linkstr shmbus
165272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.1.1.1
166272343Sngie
167272343Sngie	export RUMPHIJACK_RETRYCONNECT=die
168272343Sngie	export LD_PRELOAD=/usr/lib/librumphijack.so
169272343Sngie
170272343Sngie	atf_check -s exit:0 mkdir -p /rump/var/run
171272343Sngie	atf_check -s exit:0 mkdir -p /rump/var/db
172272343Sngie	atf_check -s exit:0 touch /rump/var/db/mountdtab
173272343Sngie	atf_check -s exit:0 mkdir /rump/etc
174272343Sngie	atf_check -s exit:0 mkdir /rump/export
175272343Sngie
176272343Sngie	atf_check -s exit:0 -x \
177272343Sngie	    'echo "/export -noresvport -noresvmnt 10.1.1.100" | \
178272343Sngie		dd of=/rump/etc/exports 2> /dev/null'
179272343Sngie
180272343Sngie	atf_check -s exit:0 -e ignore mount_ffs /dk /rump/export
181272343Sngie	atf_check -s exit:0 -x "echo ${magicstr} > /rump/export/im_alive"
182272343Sngie
183272343Sngie	# start rpcbind.  we want /var/run/rpcbind.sock
184272343Sngie	export RUMPHIJACK='blanket=/var/run,socket=all' 
185272343Sngie	atf_check -s exit:0 rpcbind
186272343Sngie
187272343Sngie	# ok, then we want mountd in the similar fashion
188272343Sngie	export RUMPHIJACK='blanket=/var/run:/var/db:/export,socket=all,path=/rump,vfs=all'
189272343Sngie	atf_check -s exit:0 mountd /rump/etc/exports
190272343Sngie
191272343Sngie	# finally, le nfschuck
192272343Sngie	export RUMPHIJACK='blanket=/var/run,socket=all,vfs=all'
193272343Sngie	atf_check -s exit:0 nfsd
194272343Sngie
195272343Sngie	#
196272343Sngie	# now, time for the client server and associated madness.
197272343Sngie	#
198272343Sngie
199272343Sngie	export RUMP_SERVER=unix://clientsock
200272343Sngie	unset RUMPHIJACK
201272343Sngie	unset LD_PRELOAD
202272343Sngie
203272343Sngie	# at least the kernel server is easier
204272343Sngie	atf_check -s exit:0 rump_server -lrumpvfs -lrumpnet		\
205272343Sngie	    -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif -lrumpfs_nfs\
206272343Sngie	    ${RUMP_SERVER}
207272343Sngie
208272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 create
209272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 linkstr shmbus
210272343Sngie	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.1.1.100
211272343Sngie
212272343Sngie	export LD_PRELOAD=/usr/lib/librumphijack.so
213272343Sngie
214272343Sngie	atf_check -s exit:0 mkdir /rump/mnt
215272343Sngie	atf_check -s exit:0 mount_nfs 10.1.1.1:/export /rump/mnt
216272343Sngie
217272343Sngie	atf_check -s exit:0 -o inline:"${magicstr}\n" cat /rump/mnt/im_alive
218272343Sngie	atf_check -s exit:0 -o match:'.*im_alive$' ls -l /rump/mnt/im_alive
219272343Sngie}
220272343Sngie
221272343Sngie
222272343Sngieatf_test_case nfs cleanup
223272343Sngienfs_head()
224272343Sngie{
225272343Sngie        atf_set "descr" "Test hijacked nfsd and mount_nfs"
226272343Sngie}
227272343Sngie
228272343Sngienfs_body()
229272343Sngie{
230272343Sngie	test_nfs -lrumpvfs -lrumpdev -lrumpnet -lrumpnet_net		\
231272343Sngie	    -lrumpnet_netinet -lrumpnet_local -lrumpnet_shmif		\
232272343Sngie	    -lrumpdev_disk -lrumpfs_ffs -lrumpfs_nfs -lrumpfs_nfsserver	\
233272343Sngie	    -d key=/dk,hostpath=ffs.img,size=host
234272343Sngie}
235272343Sngie
236272343Sngienfs_cleanup()
237272343Sngie{
238272343Sngie	RUMP_SERVER=unix://serversock rump.halt 2> /dev/null
239272343Sngie	RUMP_SERVER=unix://clientsock rump.halt 2> /dev/null
240272343Sngie	:
241272343Sngie}
242272343Sngie
243272343Sngieatf_test_case nfs_autoload cleanup
244272343Sngienfs_autoload_head()
245272343Sngie{
246272343Sngie        atf_set "descr" "Test hijacked nfsd with autoload from /stand"
247272343Sngie}
248272343Sngie
249272343Sngienfs_autoload_body()
250272343Sngie{
251272343Sngie	[ `uname -m` = "i386" ] || atf_skip "test currently valid only on i386"
252272343Sngie	test_nfs -lrumpvfs -lrumpdev -lrumpnet -lrumpnet_net		\
253272343Sngie	    -lrumpnet_netinet -lrumpnet_local -lrumpnet_shmif		\
254272343Sngie	    -lrumpdev_disk -d key=/dk,hostpath=ffs.img,size=host
255272343Sngie}
256272343Sngie
257272343Sngienfs_autoload_cleanup()
258272343Sngie{
259272343Sngie	nfs_cleanup
260272343Sngie}
261272343Sngie
262272343Sngieatf_init_test_cases()
263272343Sngie{
264272343Sngie	atf_add_test_case http
265272343Sngie	atf_add_test_case ssh
266272343Sngie	atf_add_test_case nfs
267272343Sngie	atf_add_test_case nfs_autoload
268272343Sngie}
269