testing: move ProxyCommand into a simple shell script to ease invocation (shell logic...
[monkeysphere.git] / tests / basic
1 #!/usr/bin/env bash
2
3 # Tests to ensure that the monkeysphere is working
4
5 # Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
6 # Date: 2008-09-13 13:40:15-0400
7
8 # these tests might be best run under fakeroot, particularly the
9 # "server-side" tests.  Using fakeroot, they should be able to be run
10 # as a non-privileged user.
11
12 # NOTE: these tests have *not* themselves been tested yet
13 # (2008-09-13).  Please exercise with caution!
14
15 # fail on fail
16 set -e
17
18 # gpg command for test admin user
19 gpgadmin() {
20     GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
21 }
22
23 # cleanup:
24 cleanup() {
25
26     echo
27     read -p "press enter to cleanup and remove tmp:"
28
29     if ( ps $SSHD_PID >/dev/null ) ; then 
30         echo "### stopping still-running sshd..."
31         kill $SSHD_PID
32     fi
33
34     echo "### removing temp dir..."
35     rm -rf "$TEMPDIR"
36
37     wait
38 }
39
40 ## setup trap
41 trap cleanup EXIT
42
43 ## set up some variables to ensure that we're operating strictly in
44 ## the tests, not system-wide:
45
46 export TESTDIR=$(pwd)
47
48 # make temp dir
49 TEMPDIR="$TESTDIR"/tmp
50 if [ -e "$TEMPDIR" ] ; then
51     echo "tempdir '$TEMPDIR' already exists."
52     exit 1
53 fi
54 mkdir "$TEMPDIR"
55
56 # Use the local copy of executables first, instead of system ones.
57 # This should help us test without installing.
58 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
59
60 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
61 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
62 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src
63 export MONKEYSPHERE_MONKEYSPHERE_USER="$USER"
64 export MONKEYSPHERE_CHECK_KEYSERVER=false
65
66 SSHD_CONFIG="$TEMPDIR"/sshd_config
67 export SOCKET="$TEMPDIR"/ssh-socket
68
69 # copy in admin and testuser home to tmp
70 echo "### copying admin and testuser homes..."
71 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
72 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
73
74 cat <<EOF >> "$TEMPDIR"/testuser/.ssh/config
75 UserKnownHostsFile $TEMPDIR/testuser/.ssh/known_hosts
76 ProxyCommand $TEMPDIR/testuser/.ssh/proxy-command %h %p $SOCKET
77 EOF
78
79 cat <<EOF >> "$TEMPDIR"/testuser/.monkeysphere/monkeysphere.conf
80 KNOWN_HOSTS=$TEMPDIR/testuser/.ssh/known_hosts
81 EOF
82
83 ### SERVER TESTS
84
85 # setup monkeysphere temp gnupghome directories
86 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/gnupg-host
87 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication
88 cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
89 primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
90 keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
91 EOF
92
93 # create a new host key
94 echo "### generating server key..."
95 # add gpg.conf with quick-random
96 echo "quick-random" >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
97 echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
98 # remove the gpg.conf
99 rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
100
101 HOSTKEYID=$( monkeysphere-server show-key | tail -n1 | cut -f3 -d\  )
102
103 # certify it with the "Admin's Key".
104 # (this would normally be done via keyservers)
105 echo "### certifying server key..."
106 monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
107 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
108
109 # FIXME: how can we test publish-key without flooding junk into the
110 # keyservers?
111
112 # add admin as identity certifier for testhost
113 echo "### adding admin as certifier..."
114 echo y | monkeysphere-server add-identity-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
115
116 # initialize base sshd_config
117 cp etc/ssh/sshd_config "$SSHD_CONFIG"
118 # write the sshd_config
119 cat <<EOF >> "$SSHD_CONFIG"
120 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
121 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
122 EOF
123
124 # launch test sshd with the new host key.
125 echo "### starting sshd..."
126 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -d -d -d -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
127 export SSHD_PID=$!
128
129 ### TESTUSER TESTS
130
131 # generate an auth subkey for the test user
132 echo "### generating key for testuser..."
133 MONKEYSPHERE_GNUPGHOME="$TEMPDIR"/testuser/.gnupg \
134 SSH_ASKPASS=echo \
135     monkeysphere gen-subkey --expire 0
136
137 # add server key to testuser keychain
138 echo "### export server key to testuser..."
139 gpgadmin --armor --export "$HOSTKEYID" | \
140     GNUPGHOME="$TEMPDIR"/testuser/.gnupg gpg --import
141
142 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
143 # the identity before connection.  This should work in both directions!
144 echo "### testuser connecting to sshd socket..."
145 GNUPGHOME="$TEMPDIR"/testuser/.gnupg \
146 MONKEYSPHERE_HOME="$TEMPDIR"/testuser/.monkeysphere \
147  ssh -F "$TEMPDIR"/testuser/.ssh/config -v -v -v testhost