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