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}
135 # make sure gpg home exists with proper permissions
136 mkdir -p -m 0700 "$GNUPGHOME"
143 unames=$(ls -1 "$MS_HOME"/authorized_user_ids)
146 for uname in $unames ; do
147 MODE="authorized_keys"
149 log "----- user: $uname -----"
151 AUTHORIZED_USER_IDS="$MS_HOME"/authorized_user_ids/"$uname"
152 msAuthorizedKeys="$CACHE"/"$uname"/authorized_keys
153 cacheDir="$CACHE"/"$uname"/user_keys
155 # make sure authorized_user_ids file exists
156 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
157 log "authorized_user_ids file for '$uname' is empty or does not exist."
161 # set user-controlled authorized_keys file path
162 if [ "$USER_CONTROLLED_AUTHORIZED_KEYS" ] ; then
163 userHome=$(getent passwd "$uname" | cut -d: -f6)
164 userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$userHome"}
167 # update authorized_keys
168 update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$cacheDir"
171 log "----- done. -----"
183 if [ -z "$1" ] ; then
184 failure "you must specify at least one key to trust."
191 'update-user-userids'|'u')
194 if [ -z "$uname" ] ; then
195 failure "you must specify user."
197 if [ -z "$1" ] ; then
198 failure "you must specify at least one userid."
200 AUTHORIZED_USER_IDS="$MS_HOME"/authorized_user_ids/"$uname"
201 cacheDir="$CACHE"/"$uname"/user_keys
203 update_userid "$userID" "$cacheDir"
212 failure "Unknown command: '$COMMAND'
213 Type 'cereal-admin help' for usage."