add ability to specify key length of core secret key, so the test scripts can specify...
[monkeysphere.git] / src / share / ma / setup
1 # -*-shell-script-*-
2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
3
4 # Monkeysphere authentication setup subcommand
5 #
6 # The monkeysphere scripts are written by:
7 # Jameson Rollins <jrollins@finestructure.net>
8 # Jamie McClelland <jm@mayfirst.org>
9 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
10 #
11 # They are Copyright 2009, and are all released under the GPL,
12 # version 3 or later.
13
14 setup() {
15     # make all needed directories
16     mkdir -p "${MADATADIR}"
17     mkdir -p "${MATMPDIR}"
18     mkdir -p "${GNUPGHOME_CORE}"
19     chmod 700 "${GNUPGHOME_CORE}"
20     mkdir -p "${GNUPGHOME_SPHERE}"
21     chmod 700 "${GNUPGHOME_SPHERE}"
22     mkdir -p "${MADATADIR}"/authorized_keys
23
24     # deliberately replace the config files via truncation
25     # FIXME: should we be dumping to tmp files and then moving atomically?
26     cat >"${GNUPGHOME_CORE}"/gpg.conf <<EOF
27 # Monkeysphere trust core GnuPG configuration
28 # This file is maintained by the Monkeysphere software.
29 # Edits will be overwritten.
30 no-greeting
31 list-options show-uid-validity
32 EOF
33     
34     cat >"${GNUPGHOME_SPHERE}"/gpg.conf <<EOF
35 # Monkeysphere trust sphere GnuPG configuration
36 # This file is maintained by the Monkeysphere software.
37 # Edits will be overwritten.
38 no-greeting
39 primary-keyring ${GNUPGHOME_SPHERE}/pubring.gpg
40 list-options show-uid-validity
41 EOF
42
43     # make sure the monkeysphere user owns everything in th sphere
44     # gnupghome
45     chown -R "$MONKEYPSHER_USER" "${GNUPGHOME_SPHERE}"
46     chgrp -R "$MONKEYPSHER_USER" "${GNUPGHOME_SPHERE}"
47
48     # get fingerprint of core key.  this should be empty on unconfigured systems.
49     local CORE_FPR=$(gpg_core --with-colons --fixed-list-mode --fingerprint --list-secret-key | grep ^fpr: | cut -f10 -d: )
50
51     if [ -z "$CORE_FPR" ] ; then
52         log info "Setting up Monkeysphere authentication trust core..."
53
54         local CORE_UID=$(printf "Monkeysphere authentication trust core UID (random string: %s)" $(head -c21 </dev/urandom | base64))
55     
56         local TMPLOC=$(mktemp -d "${MATMPDIR}"/tmp.XXXXXXXXXX) || failure "Could not create temporary directory!"
57
58         # generate the key with ssh-keygen...
59         log debug "generating ssh key ($CORE_KEYLENGTH bits)..."
60         ssh-keygen -q -b "$CORE_KEYLENGTH" -t rsa -N '' -f "${TMPLOC}/authkey" || failure "Could not generate new key for Monkeysphere authentication trust core"
61         # and then translate to openpgp encoding and import
62         # FIXME: pem2openpgp currently sets the A flag and a short
63         # expiration date.  We should set the C flag and no expiration
64         # date.
65         log debug "converting ssh key to openpgp key and importing into core..."
66         < "${TMPLOC}/authkey" pem2openpgp "$CORE_UID" | gpg_core --import || failure "Could not import new key for Monkeysphere authentication trust core"
67
68         # get fingerprint of core key.  should definitely not be empty at this point
69         log debug "get core key fingerprint..."
70         CORE_FPR=$(gpg_core --with-colons --fixed-list-mode --fingerprint --list-secret-key | grep ^fpr: | cut -f10 -d: )
71         if [ -z "$CORE_FPR" ] ; then
72             failure "Failed to create Monkeysphere authentication trust core!"
73         fi
74         
75     else 
76         log verbose "This system has already set up the Monkeysphere authentication trust core."
77     fi
78
79
80     # ensure that the authentication sphere checker has absolute ownertrust on the expected key.
81     log debug "set ultimate owner trust on core key in gpg_sphere..."
82     printf "%s:6:\n" "$CORE_FPR" | gpg_sphere --import-ownertrust
83     local ORIG_TRUST
84     log debug "check gpg_sphere owner trust set properly..."
85     if ORIG_TRUST=$(gpg_sphere --export-ownertrust | grep '^[^#]') ; then
86         if [ "${CORE_FPR}:6:" != "$ORIG_TRUST" ] ; then
87             failure "Monkeysphere authentication trust sphere should explicitly trust the core.  It does not have proper ownertrust settings."
88         fi
89     else
90         failure "Could not get monkeysphere-authentication trust guidelines."
91     fi
92
93     # ensure that we're using the extended trust model (1), and that
94     # our preferences are reasonable (i.e. 3 marginal OR 1 fully
95     # trusted certifications are sufficient to grant full validity.
96     log debug "check trust level of core key..."
97     if [ "1:3:1" != $(gpg_sphere --with-colons --fixed-list-mode --list-keys | head -n1 | grep ^tru: cut -f3,6,7 -d:) ] ; then
98         failure "monkeysphere-authentication does not have the expected trust model settings."
99     fi
100 }