Merge commit 'jrollins/master'
[monkeysphere.git] / src / monkeysphere-server
1 #!/bin/bash
2
3 # monkeysphere-server: MonkeySphere server admin tool
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 #
8 # They are Copyright 2008, and are all released under the GPL, version 3
9 # or later.
10
11 ########################################################################
12 PGRM=$(basename $0)
13
14 SHARE=${MONKEYSPHERE_SHARE:="/usr/share/monkeysphere"}
15 export SHARE
16 . "${SHARE}/common" || exit 1
17
18 VARLIB="/var/lib/monkeysphere"
19 export VARLIB
20
21 # date in UTF format if needed
22 DATE=$(date -u '+%FT%T')
23
24 # unset some environment variables that could screw things up
25 unset GREP_OPTIONS
26
27 # default return code
28 RETURN=0
29
30 ########################################################################
31 # FUNCTIONS
32 ########################################################################
33
34 usage() {
35     cat <<EOF
36 usage: $PGRM <subcommand> [options] [args]
37 MonkeySphere server admin tool.
38
39 subcommands:
40  update-users (u) [USER]...          update user authorized_keys files
41
42  gen-key (g) [NAME[:PORT]]           generate gpg key for the server
43    -l|--length BITS                    key length in bits (2048)
44    -e|--expire EXPIRE                  date to expire
45    -r|--revoker FINGERPRINT            add a revoker
46  add-hostname (n+) NAME[:PORT]       add hostname user ID to server key
47  revoke-hostname (n-) NAME[:PORT]    revoke hostname user ID
48  show-key (s)                        output all server host key information
49  fingerprint (f)                     output just the key fingerprint
50  publish-key (p)                     publish server host key to keyserver
51  diagnostics (d)                     report on server monkeysphere status
52
53  add-id-certifier (c+) KEYID         import and tsign a certification key
54    -n|--domain DOMAIN                  limit ID certifications to DOMAIN
55    -t|--trust TRUST                    trust level of certifier (full)
56    -d|--depth DEPTH                    trust depth for certifier (1)
57  remove-id-certifier (c-) KEYID      remove a certification key
58  list-id-certifiers (c)              list certification keys
59
60  gpg-authentication-cmd CMD          gnupg-authentication command
61
62  -h|--help|help (h,?)                this help
63 EOF
64 }
65
66 su_monkeysphere_user() {
67     su --preserve-environment "$MONKEYSPHERE_USER" -- -c "$@"
68 }
69
70 # function to interact with the host gnupg keyring
71 gpg_host() {
72     local returnCode
73
74     GNUPGHOME="$GNUPGHOME_HOST"
75     export GNUPGHOME
76
77     # NOTE: we supress this warning because we need the monkeysphere
78     # user to be able to read the host pubring.  we realize this might
79     # be problematic, but it's the simplest solution, without too much
80     # loss of security.
81     gpg --no-permission-warning "$@"
82     returnCode="$?"
83
84     # always reset the permissions on the host pubring so that the
85     # monkeysphere user can read the trust signatures
86     chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_HOST}/pubring.gpg"
87     chmod g+r "${GNUPGHOME_HOST}/pubring.gpg"
88     
89     return "$returnCode"
90 }
91
92 # function to interact with the authentication gnupg keyring
93 # FIXME: this function requires basically accepts only a single
94 # argument because of problems with quote expansion.  this needs to be
95 # fixed/improved.
96 gpg_authentication() {
97     GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
98     export GNUPGHOME
99
100     su_monkeysphere_user "gpg $@"
101 }
102
103 # output key information
104 show_server_key() {
105     gpg_host --list-secret-keys --fingerprint
106 }
107
108 # output just key fingerprint
109 fingerprint_server_key() {
110     gpg_host --list-secret-keys --fingerprint --with-colons --fixed-list-mode | \
111         grep '^fpr:' | head -1 | cut -d: -f10
112 }
113
114 # update authorized_keys for users
115 update_users() {
116     if [ "$1" ] ; then
117         # get users from command line
118         unames="$@"
119     else
120         # or just look at all users if none specified
121         unames=$(getent passwd | cut -d: -f1)
122     fi
123
124     # set mode
125     MODE="authorized_keys"
126
127     # set gnupg home
128     GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
129
130     # check to see if the gpg trust database has been initialized
131     if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
132         failure "GNUPG trust database uninitialized.  Please see MONKEYSPHERE-SERVER(8)."
133     fi
134
135     # make sure the authorized_keys directory exists
136     mkdir -p "${VARLIB}/authorized_keys"
137
138     # loop over users
139     for uname in $unames ; do
140         # check all specified users exist
141         if ! getent passwd "$uname" >/dev/null ; then
142             log "----- unknown user '$uname' -----"
143             continue
144         fi
145
146         # set authorized_user_ids and raw authorized_keys variables,
147         # translating ssh-style path variables
148         authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
149         rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
150
151         # if neither is found, skip user
152         if [ ! -s "$authorizedUserIDs" ] ; then
153             if [ "$rawAuthorizedKeys" = '-' -o ! -s "$rawAuthorizedKeys" ] ; then
154                 continue
155             fi
156         fi
157
158         log "----- user: $uname -----"
159
160         # exit if the authorized_user_ids file is empty
161         if ! check_key_file_permissions "$uname" "$AUTHORIZED_USER_IDS" ; then
162             log "Improper permissions on authorized_user_ids file path."
163             continue
164         fi
165
166         # check permissions on the authorized_keys file path
167         if ! check_key_file_permissions "$uname" "$RAW_AUTHORIZED_KEYS" ; then
168             log "Improper permissions on authorized_keys file path path."
169             continue
170         fi
171
172         # make temporary directory
173         TMPDIR=$(mktemp -d)
174
175         # trap to delete temporary directory on exit
176         trap "rm -rf $TMPDIR" EXIT
177
178         # create temporary authorized_user_ids file
179         TMP_AUTHORIZED_USER_IDS="${TMPDIR}/authorized_user_ids"
180         touch "$TMP_AUTHORIZED_USER_IDS"
181
182         # create temporary authorized_keys file
183         AUTHORIZED_KEYS="${TMPDIR}/authorized_keys"
184         touch "$AUTHORIZED_KEYS"
185
186         # set restrictive permissions on the temporary files
187         # FIXME: is there a better way to do this?
188         chmod 0700 "$TMPDIR"
189         chmod 0600 "$AUTHORIZED_KEYS"
190         chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
191         chown -R "$MONKEYSPHERE_USER" "$TMPDIR"
192
193         # if the authorized_user_ids file exists...
194         if [ -s "$authorizedUserIDs" ] ; then
195             # copy user authorized_user_ids file to temporary
196             # location
197             cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
198
199             # export needed variables
200             export AUTHORIZED_KEYS
201             export TMP_AUTHORIZED_USER_IDS
202
203             # process authorized_user_ids file, as monkeysphere
204             # user
205             su_monkeysphere_user \
206                 ". ${SHARE}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
207             RETURN="$?"
208         fi
209
210         # add user-controlled authorized_keys file path if specified
211         if [ "$rawAuthorizedKeys" != '-' -a -s "$rawAuthorizedKeys" ] ; then
212             log -n "adding raw authorized_keys file... "
213             cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
214             loge "done."
215         fi
216
217         # openssh appears to check the contents of the
218         # authorized_keys file as the user in question, so the
219         # file must be readable by that user at least.
220         # FIXME: is there a better way to do this?
221         chown root "$AUTHORIZED_KEYS"
222         chgrp $(getent passwd "$uname" | cut -f4 -d:) "$AUTHORIZED_KEYS"
223         chmod g+r "$AUTHORIZED_KEYS"
224
225         # move the resulting authorized_keys file into place
226         mv -f "$AUTHORIZED_KEYS" "${VARLIB}/authorized_keys/${uname}"
227
228         # destroy temporary directory
229         rm -rf "$TMPDIR"
230     done
231 }
232
233 # generate server gpg key
234 gen_key() {
235     local keyType
236     local keyLength
237     local keyUsage
238     local keyExpire
239     local revoker
240     local hostName
241     local userID
242     local keyParameters
243     local fingerprint
244
245     # set default key parameter values
246     keyType="RSA"
247     keyLength="2048"
248     keyUsage="auth"
249     keyExpire=
250     revoker=
251
252     # get options
253     TEMP=$(getopt -o e:l:r -l expire:,length:,revoker: -n "$PGRM" -- "$@")
254
255     if [ $? != 0 ] ; then
256         exit 1
257     fi
258
259     # Note the quotes around `$TEMP': they are essential!
260     eval set -- "$TEMP"
261
262     while true ; do
263         case "$1" in
264             -l|--length)
265                 keyLength="$2"
266                 shift 2
267                 ;;
268             -e|--expire)
269                 keyExpire="$2"
270                 shift 2
271                 ;;
272             -r|--revoker)
273                 revoker="$2"
274                 shift 2
275                 ;;
276             --)
277                 shift
278                 ;;
279             *)
280                 break
281                 ;;
282         esac
283     done
284
285     hostName=${1:-$(hostname --fqdn)}
286     userID="ssh://${hostName}"
287
288     # check for presense of key with user ID
289     if gpg_host --list-key ="$userID" > /dev/null 2>&1 ; then
290         failure "Key for '$userID' already exists"
291     fi
292
293     # prompt about key expiration if not specified
294     if [ -z "$keyExpire" ] ; then
295         cat <<EOF
296 Please specify how long the key should be valid.
297          0 = key does not expire
298       <n>  = key expires in n days
299       <n>w = key expires in n weeks
300       <n>m = key expires in n months
301       <n>y = key expires in n years
302 EOF
303         while [ -z "$keyExpire" ] ; do
304             read -p "Key is valid for? (0) " keyExpire
305             if ! test_gpg_expire ${keyExpire:=0} ; then
306                 echo "invalid value"
307                 unset keyExpire
308             fi
309         done
310     elif ! test_gpg_expire "$keyExpire" ; then
311         failure "invalid key expiration value '$keyExpire'."
312     fi
313
314     # set key parameters
315     keyParameters=$(cat <<EOF
316 Key-Type: $keyType
317 Key-Length: $keyLength
318 Key-Usage: $keyUsage
319 Name-Real: $userID
320 Expire-Date: $keyExpire
321 EOF
322 )
323
324     # add the revoker field if specified
325     # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key.
326     # FIXME: key is marked "sensitive"?  is this appropriate?
327     if [ "$revoker" ] ; then
328         keyParameters="${keyParameters}"$(cat <<EOF
329 Revoker: 1:$revoker sensitive
330 EOF
331 )
332     fi
333
334     echo "The following key parameters will be used for the host private key:"
335     echo "$keyParameters"
336
337     read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
338     if [ ${OK/y/Y} != 'Y' ] ; then
339         failure "aborting."
340     fi
341
342     # add commit command
343     keyParameters="${keyParameters}"$(cat <<EOF
344
345 %commit
346 %echo done
347 EOF
348 )
349
350     log "generating server key..."
351     echo "$keyParameters" | gpg_host --batch --gen-key
352
353     # output the server fingerprint
354     fingerprint_server_key "=${userID}"
355
356     # find the key fingerprint of the newly generated key
357     fingerprint=$(fingerprint_server_key)
358
359     # export host ownertrust to authentication keyring
360     log "setting ultimate owner trust for server key..."
361     echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
362
363     # translate the private key to ssh format, and export to a file
364     # for sshs usage.
365     # NOTE: assumes that the primary key is the proper key to use
366     (umask 077 && \
367         gpg_host --export-secret-key "$fingerprint" | \
368         openpgp2ssh "$fingerprint" > "${VARLIB}/ssh_host_rsa_key")
369     log "Private SSH host key output to file: ${VARLIB}/ssh_host_rsa_key"
370 }
371
372 # add hostname user ID to server key
373 add_hostname() {
374     if [ -z "$1" ] ; then
375         failure "You must specify a hostname to add."
376     fi
377
378     userID="ssh://${1}"
379
380     if [ "$(gpg_host --list-key "=${userID}")" ] ; then
381         failure "Host userID '$userID' already exists."
382     fi
383
384     fingerprint=$(fingerprint_server_key)
385
386     adduidCommand=$(cat <<EOF
387 adduid
388 $userID
389
390
391 O
392 save
393 EOF
394         )
395
396     # add uid
397     echo "$adduidCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
398
399     echo "NOTE: new host userID has not been published."
400     echo "Use '$PGRM publish-key' to publish these changes."
401 }
402
403 # revoke hostname user ID to server key
404 revoke_hostname() {
405     if [ -z "$1" ] ; then
406         failure "You must specify a hostname to revoke."
407     fi
408
409     failure "Sorry, not yet implemented."
410
411     echo "NOTE: host userID revokation has not been published."
412     echo "Use '$PGRM publish-key' to publish these changes."
413 }
414
415 # publish server key to keyserver
416 publish_server_key() {
417     read -p "Really publish host key to $KEYSERVER? (y/N) " OK; OK=${OK:=N}
418     if [ ${OK/y/Y} != 'Y' ] ; then
419         failure "aborting."
420     fi
421
422     # find the key fingerprint
423     fingerprint=$(fingerprint_server_key)
424
425     # publish host key
426     gpg_authentication "--keyserver $KEYSERVER --send-keys $fingerprint"
427 }
428
429 diagnostics() {
430 #  * check on the status and validity of the key and public certificates
431     local seckey
432     local keysfound
433     local curdate
434     local warnwindow
435     local warndate
436     local create
437     local expire
438     local uid
439     local fingerprint
440     local badhostkeys
441
442     seckey=$(fingerprint_server_key)
443     keysfound=$(echo "$seckey" | grep -c ^sec:)
444     curdate=$(date +%s)
445     # warn when anything is 2 months away from expiration
446     warnwindow='2 months'
447     warndate=$(date +%s -d "$warnwindow")
448
449     echo "Checking host GPG key..."
450     if (( "$keysfound" < 1 )); then
451         echo "! No host key found."
452         echo " - Recommendation: run 'monkeysphere-server gen-key'"
453     elif (( "$keysfound" > 1 )); then
454         echo "! More than one host key found?"
455         # FIXME: recommend a way to resolve this
456     else
457         create=$(echo "$seckey" | grep ^sec: | cut -f6 -d:)
458         expire=$(echo "$seckey" | grep ^sec: | cut -f7 -d:)
459         fingerprint=$(echo "$seckey" | grep ^fpr: | head -n1 | cut -f10 -d:)
460         # check for key expiration:
461         if [ "$expire" ]; then
462             if (( "$expire"  < "$curdate" )); then
463                 echo "! Host key is expired."
464                 # FIXME: recommend a way to resolve this other than re-keying?
465             elif (( "$expire" < "$warndate" )); then
466                 echo "! Host key expires in less than $warnwindow:" $(date -d "$(( $expire - $curdate )) seconds" +%F)
467                 # FIXME: recommend a way to resolve this?
468             fi
469         fi
470
471         # and weirdnesses:
472         if [ "$create" ] && (( "$create" > "$curdate" )); then
473             echo "! Host key was created in the future(?!). Is your clock correct?"
474             echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
475         fi
476
477         # check for UserID expiration:
478         echo "$seckey" | grep ^uid: | cut -d: -f6,7,10 | \
479         while IFS=: read create expire uid ; do
480             # FIXME: should we be doing any checking on the form
481             # of the User ID?  Should we be unmangling it somehow?
482
483             if [ "$create" ] && (( "$create" > "$curdate" )); then
484                 echo "! User ID '$uid' was created in the future(?!).  Is your clock correct?"
485                 echo " - Recommendation: Check clock ($(date +%F_%T)); use NTP?"
486             fi
487             if [ "$expire" ] ; then
488                 if (( "$expire" < "$curdate" )); then
489                     echo "! User ID '$uid' is expired."
490                         # FIXME: recommend a way to resolve this
491                 elif (( "$expire" < "$warndate" )); then
492                     echo "! User ID '$uid' expires in less than $warnwindow:" $(date -d "$(( $expire - $curdate )) seconds" +%F)                
493                     # FIXME: recommend a way to resolve this
494                 fi
495             fi
496         done
497             
498 # FIXME: verify that the host key is properly published to the
499 #   keyservers (do this with the non-privileged user)
500
501 # FIXME: check that there are valid, non-expired certifying signatures
502 #   attached to the host key after fetching from the public keyserver
503 #   (do this with the non-privileged user as well)
504
505 # FIXME: propose adding a revoker to the host key if none exist (do we
506 #   have a way to do that after key generation?)
507
508         # Ensure that the ssh_host_rsa_key file is present and non-empty:
509         echo "Checking host SSH key..."
510         if [ ! -s "${VARLIB}/ssh_host_rsa_key" ] ; then
511             echo "! The host key as prepared for SSH (${VARLIB}/ssh_host_rsa_key) is missing or empty."
512         else
513             if [ $(stat -c '%a' "${VARLIB}/ssh_host_rsa_key") != 600 ] ; then
514                 echo "! Permissions seem wrong for ${VARLIB}/ssh_host_rsa_key -- should be 0600."
515             fi
516
517             # propose changes needed for sshd_config (if any)
518             if ! grep -q "^HostKey[[:space:]]\+${VARLIB}/ssh_host_rsa_key$" /etc/ssh/sshd_config; then
519                 echo "! /etc/ssh/sshd_config does not point to the monkeysphere host key (${VARLIB}/ssh_host_rsa_key)."
520                 echo " - Recommendation: add a line to /etc/ssh/sshd_config: 'HostKey ${VARLIB}/ssh_host_rsa_key'"
521             fi
522             if badhostkeys=$(grep -i '^HostKey' | grep -q -v "^HostKey[[:space:]]\+${VARLIB}/ssh_host_rsa_key$") ; then
523                 echo "! /etc/sshd_config refers to some non-monkeysphere host keys:"
524                 echo "$badhostkeys"
525                 echo " - Recommendation: remove the above HostKey lines from /etc/ssh/sshd_config"
526             fi
527         fi
528     fi
529
530 # FIXME: look at the ownership/privileges of the various keyrings,
531 #    directories housing them, etc (what should those values be?  can
532 #    we make them as minimal as possible?)
533
534 # FIXME: look to see that the ownertrust rules are set properly on the
535 #    authentication keyring
536
537 # FIXME:  make sure that at least one identity certifier exists
538
539     echo "Checking for MonkeySphere-enabled public-key authentication for users ..."
540     # Ensure that User ID authentication is enabled:
541     if ! grep -q "^AuthorizedKeysFile[[:space:]]\+${VARLIB}/authorized_keys/%u$" /etc/ssh/sshd_config; then
542         echo "! /etc/ssh/sshd_config does not point to monkeysphere authorized keys."
543         echo " - Recommendation: add a line to /etc/ssh/sshd_config: 'AuthorizedKeysFile ${VARLIB}/authorized_keys/%u'"
544     fi
545     if badauthorizedkeys=$(grep -i '^AuthorizedKeysFile' | grep -q -v "^AuthorizedKeysFile[[:space:]]\+${VARLIB}/authorized_keys/%u$") ; then
546         echo "! /etc/sshd_config refers to non-monkeysphere authorized_keys files:"
547         echo "$badauthorizedkeys"
548         echo " - Recommendation: remove the above AuthorizedKeysFile lines from /etc/ssh/sshd_config"
549     fi
550 }
551
552 # retrieve key from web of trust, import it into the host keyring, and
553 # ltsign the key in the host keyring so that it may certify other keys
554 add_certifier() {
555     local domain
556     local trust
557     local depth
558     local keyID
559     local fingerprint
560     local ltsignCommand
561     local trustval
562
563     # set default values for trust depth and domain
564     domain=
565     trust=full
566     depth=1
567
568     # get options
569     TEMP=$(getopt -o n:t:d: -l domain:,trust:,depth: -n "$PGRM" -- "$@")
570
571     if [ $? != 0 ] ; then
572         exit 1
573     fi
574
575     # Note the quotes around `$TEMP': they are essential!
576     eval set -- "$TEMP"
577
578     while true ; do
579         case "$1" in
580             -n|--domain)
581                 domain="$2"
582                 shift 2
583                 ;;
584             -t|--trust)
585                 trust="$2"
586                 shift 2
587                 ;;
588             -d|--depth)
589                 depth="$2"
590                 shift 2
591                 ;;
592             --)
593                 shift
594                 ;;
595             *)
596                 break
597                 ;;
598         esac
599     done
600
601     keyID="$1"
602     if [ -z "$keyID" ] ; then
603         failure "You must specify the key ID of a key to add."
604     fi
605     export keyID
606
607     # get the key from the key server
608     gpg_authentication "--keyserver $KEYSERVER --recv-key '$keyID'"
609
610     # get the full fingerprint of a key ID
611     fingerprint=$(gpg_authentication "--list-key --with-colons --with-fingerprint $keyID" | \
612         grep '^fpr:' | grep "$keyID" | cut -d: -f10)
613
614     echo "key found:"
615     gpg_authentication "--fingerprint $fingerprint"
616
617     echo "Are you sure you want to add this key as a certifier of"
618     read -p "users on this system? (y/N) " OK; OK=${OK:-N}
619     if [ "${OK/y/Y}" != 'Y' ] ; then
620         failure "aborting."
621     fi
622
623     # export the key to the host keyring
624     gpg_authentication "--export $keyID" | gpg_host --import
625
626     if [ "$trust" == marginal ]; then
627         trustval=1
628     elif [ "$trust" == full ]; then
629         trustval=2
630     else
631         failure "trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)"
632     fi
633
634     # ltsign command
635     # NOTE: *all* user IDs will be ltsigned
636     ltsignCommand=$(cat <<EOF
637 ltsign
638 y
639 $trustval
640 $depth
641 $domain
642 y
643 save
644 EOF
645         )
646
647     # ltsign the key
648     echo "$ltsignCommand" | gpg_host --quiet --command-fd 0 --edit-key "$fingerprint"
649
650     # update the trustdb for the authentication keyring
651     gpg_authentication "--check-trustdb"
652 }
653
654 # delete a certifiers key from the host keyring
655 remove_certifier() {
656     local keyID
657     local fingerprint
658
659     keyID="$1"
660     if [ -z "$keyID" ] ; then
661         failure "You must specify the key ID of a key to remove."
662     fi
663
664     # delete the requested key (with prompting)
665     gpg_host --delete-key "$keyID"
666
667     # update the trustdb for the authentication keyring
668     gpg_authentication "--check-trustdb"
669 }
670
671 # list the host certifiers
672 list_certifiers() {
673     gpg_host --list-keys
674 }
675
676 # issue command to gpg-authentication keyring
677 gpg_authentication_cmd() {
678     gpg_authentication "$@"
679 }
680
681 ########################################################################
682 # MAIN
683 ########################################################################
684
685 # unset variables that should be defined only in config file
686 unset KEYSERVER
687 unset AUTHORIZED_USER_IDS
688 unset RAW_AUTHORIZED_KEYS
689 unset MONKEYSPHERE_USER
690
691 # load configuration file
692 [ -e ${MONKEYSPHERE_SERVER_CONFIG:="${ETC}/monkeysphere-server.conf"} ] && . "$MONKEYSPHERE_SERVER_CONFIG"
693
694 # set empty config variable with ones from the environment, or with
695 # defaults
696 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
697 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.config/monkeysphere/authorized_user_ids"}}
698 RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
699 MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
700
701 # other variables
702 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
703 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
704 GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${VARLIB}/gnupg-host"}
705 GNUPGHOME_AUTHENTICATION=${MONKEYSPHERE_GNUPGHOME_AUTHENTICATION:="${VARLIB}/gnupg-authentication"}
706
707 # export variables needed in su invocation
708 export DATE
709 export MODE
710 export MONKEYSPHERE_USER
711 export KEYSERVER
712 export CHECK_KEYSERVER
713 export REQUIRED_USER_KEY_CAPABILITY
714 export GNUPGHOME_HOST
715 export GNUPGHOME_AUTHENTICATION
716 export GNUPGHOME
717
718 # get subcommand
719 COMMAND="$1"
720 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
721 shift
722
723 case $COMMAND in
724     'update-users'|'update-user'|'u')
725         update_users "$@"
726         ;;
727
728     'gen-key'|'g')
729         gen_key "$@"
730         ;;
731
732     'add-hostname'|'add-name'|'n+')
733         add_hostname "$@"
734         ;;
735
736     'revoke-hostname'|'revoke-name'|'n-')
737         revoke_hostname "$@"
738         ;;
739
740     'show-key'|'show'|'s')
741         show_server_key
742         ;;
743
744     'show-fingerprint'|'fingerprint'|'f')
745         fingerprint_server_key
746         ;;
747
748     'publish-key'|'publish'|'p')
749         publish_server_key
750         ;;
751
752     'diagnostics'|'d')
753         diagnostics
754         ;;
755
756     'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
757         add_certifier "$@"
758         ;;
759
760     'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
761         remove_certifier "$@"
762         ;;
763
764     'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
765         list_certifiers "$@"
766         ;;
767
768     'gpg-authentication-cmd')
769         gpg_authentication_cmd "$@"
770         ;;
771
772     '--help'|'help'|'-h'|'h'|'?')
773         usage
774         ;;
775
776     *)
777         failure "Unknown command: '$COMMAND'
778 Type '$PGRM help' for usage."
779         ;;
780 esac
781
782 exit "$RETURN"