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