1685e5da797613a0151c2e5f8729ef071ce1a3e4
[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     log debug "make authentication directory structure..."
17     mkdir -p "${MADATADIR}"
18     chmod 750 "${MADATADIR}"
19     chgrp "$MONKEYSPHERE_USER" "${MADATADIR}"
20     mkdir -p "${MATMPDIR}"
21     chmod 750 "${MATMPDIR}"
22     chgrp "$MONKEYSPHERE_USER" "${MATMPDIR}"
23     mkdir -p "${GNUPGHOME_CORE}"
24     chmod 700 "${GNUPGHOME_CORE}"
25     mkdir -p "${GNUPGHOME_SPHERE}"
26     chmod 700 "${GNUPGHOME_SPHERE}"
27
28     # deliberately replace the config files via truncation
29     # FIXME: should we be dumping to tmp files and then moving atomically?
30     log debug "writing core gpg.conf..."
31     cat >"${GNUPGHOME_CORE}"/gpg.conf <<EOF
32 # Monkeysphere trust core GnuPG configuration
33 # This file is maintained by the Monkeysphere software.
34 # Edits will be overwritten.
35 no-greeting
36 EOF
37
38     log debug "writing sphere gpg.conf..."
39     cat >"${GNUPGHOME_SPHERE}"/gpg.conf <<EOF
40 # Monkeysphere trust sphere GnuPG configuration
41 # This file is maintained by the Monkeysphere software.
42 # Edits will be overwritten.
43 no-greeting
44 list-options show-uid-validity
45 EOF
46
47     # make sure the monkeysphere user owns everything in the sphere
48     # gnupghome
49     log debug "fixing sphere gnupg home ownership..."
50     chown "$MONKEYSPHERE_USER:$MONKEYSPHERE_USER" "${GNUPGHOME_SPHERE}" "${GNUPGHOME_SPHERE}"/gpg.conf
51
52     # get fingerprint of core key.  this should be empty on unconfigured systems.
53     local CORE_FPR=$(core_fingerprint)
54     log debug "core fingerprint: $CORE_FPR"
55
56     if [ -z "$CORE_FPR" ] ; then
57         log info "setting up Monkeysphere authentication trust core..."
58
59         local CORE_UID=$(printf "Monkeysphere authentication trust core UID (random string: %s)" $(head -c21 </dev/urandom | base64))
60     
61         log debug "generating monkeysphere authentication trust core key ($CORE_KEYLENGTH bits)..."
62         PEM2OPENPGP_USAGE_FLAGS=certify \
63             PEM2OPENPGP_NEWKEY=$CORE_KEYLENGTH pem2openpgp "$CORE_UID" \
64             | gpg_core --import \
65             || failure "Could not import new key for Monkeysphere authentication trust core"
66
67         # get fingerprint of core key.  should definitely not be empty at this point
68         CORE_FPR=$(core_fingerprint)
69         log debug "core fingerprint: $CORE_FPR"
70         if [ -z "$CORE_FPR" ] ; then
71             failure "Failed to create Monkeysphere authentication trust core!"
72         fi
73         
74     else 
75         log verbose "Monkeysphere authentication trust core already exists."
76     fi
77
78     # export the core key to the sphere keyring
79     log debug "exporting core pub key to sphere keyring..."
80     gpg_core --export | gpg_sphere "--import"
81
82     # ensure that the authentication sphere checker has absolute ownertrust on the expected key.
83     log debug "setting ultimate owner trust on core key in gpg_sphere..."
84     printf "%s:6:\n" "$CORE_FPR" | gpg_sphere "--import-ownertrust"
85     gpg_sphere "--export-ownertrust" 2>&1 | log debug
86
87     # check the owner trust
88     log debug "checking gpg_sphere owner trust set properly..."
89     local ORIG_TRUST
90     if ORIG_TRUST=$(gpg_sphere "--export-ownertrust" | grep '^[^#]') ; then
91         if [ "${CORE_FPR}:6:" != "$ORIG_TRUST" ] ; then
92             failure "Monkeysphere authentication trust sphere should explicitly trust the core.  It does not have proper ownertrust settings."
93         fi
94     else
95         failure "Could not get monkeysphere-authentication trust guidelines."
96         # FIXME: what does this mean?  should we suggest how to fix?
97     fi
98
99     # ensure that we're using the extended trust model (1), and that
100     # our preferences are reasonable (i.e. 3 marginal OR 1 fully
101     # trusted certifications are sufficient to grant full validity.
102     log debug "checking trust model for authentication ..."
103     local TRUST_MODEL=$(gpg_sphere "--with-colons --fixed-list-mode --list-keys" \
104         | head -n1 | grep "^tru:" | cut -d: -f3,6,7)
105     log debug "sphere trust model: $TRUST_MODEL"
106     if [ "$TRUST_MODEL" != '1:3:1' ] ; then
107         failure "monkeysphere-authentication does not have the expected trust model settings."
108         # FIXME: what does this mean?  should we suggest how to fix?
109     fi
110 }