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