3 # monkeysphere-server: MonkeySphere server admin tool
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
8 # They are Copyright 2008, and are all released under the GPL, version 3
11 ########################################################################
14 SHAREDIR=${SHAREDIR:-"/usr/share/monkeysphere"}
16 . "${SHAREDIR}/common"
18 # date in UTF format if needed
19 DATE=$(date -u '+%FT%T')
21 # unset some environment variables that could screw things up
24 ########################################################################
26 ########################################################################
30 usage: $PGRM <subcommand> [args]
31 Monkeysphere server admin tool.
34 update-users (s) [USER]... update users authorized_keys files
35 gen-key (g) generate gpg key for the server
36 publish-key (p) publish server key to keyserver
37 trust-keys (t) KEYID... mark keyids as trusted
38 update-user-userids (u) USER UID... add/update userids for a user
44 # generate server gpg key
47 KEY_TYPE=${KEY_TYPE:-RSA}
48 KEY_LENGTH=${KEY_LENGTH:-2048}
49 KEY_USAGE=${KEY_USAGE:-encrypt,auth}
50 SERVICE=${SERVICE:-ssh}
51 HOSTNAME_FQDN=${HOSTNAME_FQDN:-$(hostname -f)}
53 USERID=${USERID:-"$SERVICE"://"$HOSTNAME_FQDN"}
56 keyParameters=$(cat <<EOF
58 Key-Length: $KEY_LENGTH
64 # add the revoker field if requested
65 if [ "$REVOKER" ] ; then
66 keyParameters="${keyParameters}"$(cat <<EOF
68 Revoker: 1:$REVOKER sensitive
73 log "The following key parameters will be used:"
76 read -p "generate key? [Y|n]: " OK; OK=${OK:=Y}
77 if [ ${OK/y/Y} != 'Y' ] ; then
81 if gpg --list-key ="$USERID" > /dev/null 2>&1 ; then
82 failure "key for '$USERID' already exists"
86 keyParameters="${keyParameters}"$(cat <<EOF
93 echo "generating server key..."
94 echo "$keyParameters" | gpg --batch --gen-key
97 # publish server key to keyserver
99 read -p "publish key to $KEYSERVER? [Y|n]: " OK; OK=${OK:=Y}
100 if [ ${OK/y/Y} != 'Y' ] ; then
104 keyID=$(gpg --list-key --with-colons ="$USERID" 2> /dev/null | grep '^pub:' | cut -d: -f5)
106 # dummy command so as not to publish fakes keys during testing
108 #gpg --send-keys --keyserver "$KEYSERVER" "$keyID"
109 echo "NOT PUBLISHED: gpg --send-keys --keyserver $KEYSERVER $keyID"
112 ########################################################################
114 ########################################################################
117 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
120 # set ms home directory
121 MS_HOME=${MS_HOME:-"$ETC"}
123 # load configuration file
124 MS_CONF=${MS_CONF:-"$MS_HOME"/monkeysphere-server.conf}
125 [ -e "$MS_CONF" ] && . "$MS_CONF"
127 # set empty config variable with defaults
128 GNUPGHOME=${GNUPGHOME:-"$MS_HOME"/gnupg}
129 KEYSERVER=${KEYSERVER:-subkeys.pgp.net}
130 REQUIRED_KEY_CAPABILITY=${REQUIRED_KEY_CAPABILITY:-"e a"}
131 USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-%h/.ssh/authorized_keys}
132 STAGING_AREA=${STAGING_AREA:-"$LIB"/stage}
136 # make sure gpg home exists with proper permissions
137 mkdir -p -m 0700 "$GNUPGHOME"
144 unames=$(ls -1 "$MS_HOME"/authorized_user_ids)
147 for uname in $unames ; do
148 MODE="authorized_keys"
150 log "----- user: $uname -----"
152 AUTHORIZED_USER_IDS="$MS_HOME"/authorized_user_ids/"$uname"
153 cacheDir="$STAGING_AREA"/"$uname"/user_keys
154 msAuthorizedKeys="$STAGING_AREA"/"$uname"/authorized_keys
156 # make sure authorized_user_ids file exists
157 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
158 log "authorized_user_ids file for '$uname' is empty or does not exist."
162 # set user-controlled authorized_keys file path
163 if [ "$USER_CONTROLLED_AUTHORIZED_KEYS" ] ; then
164 userHome=$(getent passwd "$uname" | cut -d: -f6)
165 userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$userHome"}
168 # update authorized_keys
169 update_authorized_keys "$cacheDir" "$msAuthorizedKeys" "$userAuthorizedKeys"
172 log "----- done. -----"
184 if [ -z "$1" ] ; then
185 failure "you must specify at least one key to trust."
192 'update-user-userids'|'u')
195 if [ -z "$uname" ] ; then
196 failure "you must specify user."
198 if [ -z "$1" ] ; then
199 failure "you must specify at least one userid."
201 AUTHORIZED_USER_IDS="$MS_HOME"/authorized_user_ids/"$uname"
202 userKeysCacheDir="$STAGING_AREA"/"$uname"/user_keys
204 update_userid "$userID" "$userKeysCacheDir"
213 failure "Unknown command: '$COMMAND'
214 Type 'cereal-admin help' for usage."