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