add ability to bypass prompting with a MONKEYSPHERE_PROMPT variable,
[monkeysphere.git] / src / monkeysphere-authentication
1 #!/usr/bin/env bash
2
3 # monkeysphere-authentication: Monkeysphere authentication admin tool
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@finestructure.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
9 # Micah Anderson <micah@riseup.net>
10 #
11 # They are Copyright 2008-2009, and are all released under the GPL,
12 # version 3 or later.
13
14 ########################################################################
15 set -e
16
17 PGRM=$(basename $0)
18
19 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
20 export SYSSHAREDIR
21 . "${SYSSHAREDIR}/common" || exit 1
22
23 SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
24 export SYSDATADIR
25
26 # sharedir for authentication functions
27 MASHAREDIR="${SYSSHAREDIR}/ma"
28
29 # datadir for authentication functions
30 MADATADIR="${SYSDATADIR}/authentication"
31
32 # temp directory to enable atomic moves of authorized_keys files
33 MATMPDIR="${MADATADIR}/tmp"
34 export MATMPDIR
35
36 # UTC date in ISO 8601 format if needed
37 DATE=$(date -u '+%FT%T')
38
39 # unset some environment variables that could screw things up
40 unset GREP_OPTIONS
41
42 # default return code
43 RETURN=0
44
45 ########################################################################
46 # FUNCTIONS
47 ########################################################################
48
49 usage() {
50     cat <<EOF >&2
51 usage: $PGRM <subcommand> [options] [args]
52 Monkeysphere authentication admin tool.
53
54 subcommands:
55  setup (s)                           setup monkeysphere user authentication
56  update-users (u) [USER]...          update user authorized_keys files
57  add-id-certifier (c+) KEYID         import and tsign a certification key
58    --domain (-n) DOMAIN                limit ID certifications to DOMAIN
59    --trust (-t) TRUST                  trust level of certifier (full)
60    --depth (-d) DEPTH                  trust depth for certifier (1)
61  remove-id-certifier (c-) KEYID      remove a certification key
62  list-id-certifiers (c)              list certification keys
63
64  version (v)                         show version number
65  help (h,?)                          this help
66
67 See ${PGRM}(8) for more info.
68 EOF
69 }
70
71 # function to interact with the gpg core keyring
72 gpg_core() {
73     GNUPGHOME="$GNUPGHOME_CORE"
74     export GNUPGHOME
75
76     gpg "$@"
77 }
78
79 # function to interact with the gpg sphere keyring
80 # FIXME: this function requires only a single argument because of
81 # problems with quote expansion.  this needs to be fixed/improved.
82 gpg_sphere() {
83     GNUPGHOME="$GNUPGHOME_SPHERE"
84     export GNUPGHOME
85
86     su_monkeysphere_user "gpg $@"
87 }
88
89 # output to stdout the core fingerprint from the gpg core secret
90 # keyring
91 core_fingerprint() {
92     log debug "determining core key fingerprint..."
93     gpg_core --quiet --list-secret-key \
94         --with-colons --fixed-list-mode --with-fingerprint \
95         | grep ^fpr: | cut -d: -f10
96 }
97
98 # fail if authentication has not been setup
99 check_no_setup() {
100     # FIXME: what is the right test to do here?
101     [ -d "$MADATADIR" ] \
102         || failure "This host appears to have not yet been set up for Monkeysphere authentication.
103 Please run 'monkeysphere-authentication setup' first."
104 }
105
106 # export signatures from core to sphere
107 gpg_core_sphere_sig_transfer() {
108     log debug "exporting core local sigs to sphere..."
109     gpg_core --export-options export-local-sigs --export | \
110         gpg_sphere "--import-options import-local-sigs --import"
111 }
112
113 ########################################################################
114 # MAIN
115 ########################################################################
116
117 # unset variables that should be defined only in config file of in
118 # MONKEYSPHERE_ variables
119 unset LOG_LEVEL
120 unset KEYSERVER
121 unset AUTHORIZED_USER_IDS
122 unset RAW_AUTHORIZED_KEYS
123 unset MONKEYSPHERE_USER
124 unset PROMPT
125
126 # load configuration file
127 [ -e ${MONKEYSPHERE_AUTHENTICATION_CONFIG:="${SYSCONFIGDIR}/monkeysphere-authentication.conf"} ] && . "$MONKEYSPHERE_AUTHENTICATION_CONFIG"
128
129 # set empty config variable with ones from the environment, or with
130 # defaults
131 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
132 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
133 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
134 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
135 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
136 PROMPT=${MONKEYSPHERE_PROMPT:=${PROMPT:="true"}}
137
138 # other variables
139 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
140 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
141 GNUPGHOME_CORE=${MONKEYSPHERE_GNUPGHOME_CORE:="${MADATADIR}/core"}
142 GNUPGHOME_SPHERE=${MONKEYSPHERE_GNUPGHOME_SPHERE:="${MADATADIR}/sphere"}
143 CORE_KEYLENGTH=${MONKEYSPHERE_CORE_KEYLENGTH:="2048"}
144
145 # export variables needed in su invocation
146 export DATE
147 export MODE
148 export LOG_LEVEL
149 export KEYSERVER
150 export MONKEYSPHERE_USER
151 export PROMPT
152 export CHECK_KEYSERVER
153 export REQUIRED_USER_KEY_CAPABILITY
154 export GNUPGHOME_CORE
155 export GNUPGHOME_SPHERE
156 export GNUPGHOME
157 export CORE_KEYLENGTH
158
159 # get subcommand
160 COMMAND="$1"
161 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
162 shift
163
164 case $COMMAND in
165     'setup'|'setup'|'s')
166         source "${MASHAREDIR}/setup"
167         setup "$@"
168         ;;
169
170     'update-users'|'update-user'|'u')
171         check_no_setup
172         source "${MASHAREDIR}/update_users"
173         update_users "$@"
174         ;;
175
176     'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
177         check_no_setup
178         source "${MASHAREDIR}/add_certifier"
179         add_certifier "$@"
180         ;;
181
182     'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
183         check_no_setup
184         source "${MASHAREDIR}/remove_certifier"
185         remove_certifier "$@"
186         ;;
187
188     'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
189         check_no_setup
190         source "${MASHAREDIR}/list_certifiers"
191         list_certifiers "$@"
192         ;;
193
194     'diagnostics'|'d')
195         source "${MASHAREDIR}/diagnostics"
196         diagnostics
197         ;;
198
199     'gpg-cmd')
200         check_no_setup
201         gpg_sphere "$@"
202         ;;
203
204     'version'|'v')
205         echo "$VERSION"
206         ;;
207
208     '--help'|'help'|'-h'|'h'|'?')
209         usage
210         ;;
211
212     *)
213         failure "Unknown command: '$COMMAND'
214 Type '$PGRM help' for usage."
215         ;;
216 esac
217
218 exit "$RETURN"