testing: setting up the fake testuser account with an authorized_user_id.
[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 # set up a simple default monkeysphere-server.conf
89 cat <<EOF >> "$TEMPDIR"/monkeysphere-server.conf
90 AUTHORIZED_USER_IDS="$TEMPDIR/testuser/.monkeysphere/authorized_user_ids"
91 EOF
92
93 ### SERVER TESTS
94
95 # setup monkeysphere temp gnupghome directories
96 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/gnupg-host
97 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication
98 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authorized_keys
99 cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
100 primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
101 keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
102 EOF
103
104 # create a new host key
105 echo "### generating server key..."
106 # add gpg.conf with quick-random
107 echo "quick-random" >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
108 echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
109 # remove the gpg.conf
110 rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
111
112 HOSTKEYID=$( monkeysphere-server show-key | tail -n1 | cut -f3 -d\  )
113
114 # certify it with the "Admin's Key".
115 # (this would normally be done via keyservers)
116 echo "### certifying server key..."
117 monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
118 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
119
120 # FIXME: how can we test publish-key without flooding junk into the
121 # keyservers?
122
123 # add admin as identity certifier for testhost
124 echo "### adding admin as certifier..."
125 echo y | monkeysphere-server add-identity-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
126
127 # initialize base sshd_config
128 cp etc/ssh/sshd_config "$SSHD_CONFIG"
129 # write the sshd_config
130 cat <<EOF >> "$SSHD_CONFIG"
131 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
132 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
133 EOF
134
135 # launch test sshd with the new host key.
136 echo "### starting sshd..."
137 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
138 export SSHD_PID=$!
139
140 ### TESTUSER TESTS
141
142 # generate an auth subkey for the test user
143 echo "### generating key for testuser..."
144 export GNUPGHOME="$TEMPDIR"/testuser/.gnupg
145 export SSH_ASKPASS="$TEMPDIR"/testuser/.ssh/askpass
146 export MONKEYSPHERE_HOME="$TEMPDIR"/testuser/.monkeysphere
147
148 monkeysphere gen-subkey --expire 0
149
150 # add server key to testuser keychain
151 echo "### export server key to testuser..."
152 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
153
154 # teach the "server" about the testuser's key
155 echo "### export testuser key to server..."
156 gpg --export testuser | monkeysphere-server gpg-authentication-cmd --import
157 echo "### update server authorized_keys file for this testuser..."
158 monkeysphere-server update-users "$USER"
159
160 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
161 # the identity before connection.  This should work in both directions!
162 echo "### testuser connecting to sshd socket..."
163
164 ssh-agent bash -c \
165    "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost"
166
167 trap - EXIT
168 cleanup