Merge commit 'dkg/master'
[monkeysphere.git] / src / monkeysphere-server
1 #!/bin/sh
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 SHAREDIR=${SHAREDIR:-"/usr/share/monkeysphere"}
15 export SHAREDIR
16 . "${SHAREDIR}/common"
17
18 # date in UTF format if needed
19 DATE=$(date -u '+%FT%T')
20
21 # unset some environment variables that could screw things up
22 GREP_OPTIONS=
23
24 ########################################################################
25 # FUNCTIONS
26 ########################################################################
27
28 usage() {
29 cat <<EOF
30 usage: $PGRM <subcommand> [args]
31 Monkeysphere server admin tool.
32
33 subcommands:
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
39   help (h,?)                            this help
40
41 EOF
42 }
43
44 # generate server gpg key
45 gen_key() {
46     # set key defaults
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)}
52
53     USERID=${USERID:-"$SERVICE"://"$HOSTNAME_FQDN"}
54
55     # set key parameters
56     keyParameters=$(cat <<EOF
57 Key-Type: $KEY_TYPE
58 Key-Length: $KEY_LENGTH
59 Key-Usage: $KEY_USAGE
60 Name-Real: $USERID
61 EOF
62 )
63
64     # add the revoker field if requested
65     if [ "$REVOKER" ] ; then
66         keyParameters="${keyParameters}"$(cat <<EOF
67
68 Revoker: 1:$REVOKER sensitive
69 EOF
70 )
71     fi
72
73     log "The following key parameters will be used:"
74     echo "$keyParameters"
75
76     read -p "generate key? [Y|n]: " OK; OK=${OK:=Y}
77     if [ ${OK/y/Y} != 'Y' ] ; then
78         failure "aborting."
79     fi
80
81     if gpg --list-key ="$USERID" > /dev/null 2>&1 ; then
82         failure "key for '$USERID' already exists"
83     fi
84
85     # add commit command
86     keyParameters="${keyParameters}"$(cat <<EOF
87
88 %commit
89 %echo done
90 EOF
91 )
92
93     echo "generating server key..."
94     echo "$keyParameters" | gpg --batch --gen-key
95 }
96
97 # publish server key to keyserver
98 publish_key() {
99     read -p "publish key to $KEYSERVER? [Y|n]: " OK; OK=${OK:=Y}
100     if [ ${OK/y/Y} != 'Y' ] ; then
101         failure "aborting."
102     fi
103
104     keyID=$(gpg --list-key --with-colons ="$USERID" 2> /dev/null | grep '^pub:' | cut -d: -f5)
105
106     # dummy command so as not to publish fakes keys during testing
107     # eventually:
108     #gpg --send-keys --keyserver "$KEYSERVER" "$keyID"
109     echo "NOT PUBLISHED: gpg --send-keys --keyserver $KEYSERVER $keyID"
110 }
111
112 ########################################################################
113 # MAIN
114 ########################################################################
115
116 COMMAND="$1"
117 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
118 shift
119
120 # set ms home directory
121 MS_HOME=${MS_HOME:-"$ETC"}
122
123 # load configuration file
124 MS_CONF=${MS_CONF:-"$MS_HOME"/monkeysphere-server.conf}
125 [ -e "$MS_CONF" ] && . "$MS_CONF"
126
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}
133
134 export GNUPGHOME
135
136 # make sure gpg home exists with proper permissions
137 mkdir -p -m 0700 "$GNUPGHOME"
138
139 case $COMMAND in
140     'update-users'|'s')
141         if [ "$1" ] ; then
142             unames="$@"
143         else
144             unames=$(ls -1 "$MS_HOME"/authorized_user_ids)
145         fi
146
147         for uname in $unames ; do
148             MODE="authorized_keys"
149
150             log "----- user: $uname -----"
151
152             AUTHORIZED_USER_IDS="$MS_HOME"/authorized_user_ids/"$uname"
153             cacheDir="$STAGING_AREA"/"$uname"/user_keys
154             msAuthorizedKeys="$STAGING_AREA"/"$uname"/authorized_keys
155
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."
159                 continue
160             fi
161
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"}
166             fi
167
168             # update authorized_keys
169             update_authorized_keys "$cacheDir" "$msAuthorizedKeys" "$userAuthorizedKeys"
170         done
171
172         log "----- done. -----"
173         ;;
174
175     'gen-key'|'g')
176         gen_key
177         ;;
178
179     'publish-key'|'p')
180         publish_key
181         ;;
182
183     'trust-keys'|'t')
184         if [ -z "$1" ] ; then
185             failure "you must specify at least one key to trust."
186         fi
187         for keyID ; do
188             trust_key "$keyID"
189         done
190         ;;
191
192     'update-user-userids'|'u')
193         uname="$1"
194         shift
195         if [ -z "$uname" ] ; then
196             failure "you must specify user."
197         fi
198         if [ -z "$1" ] ; then
199             failure "you must specify at least one userid."
200         fi
201         AUTHORIZED_USER_IDS="$MS_HOME"/authorized_user_ids/"$uname"
202         userKeysCacheDir="$STAGING_AREA"/"$uname"/user_keys
203         for userID ; do
204             update_userid "$userID" "$userKeysCacheDir"
205         done
206         ;;
207
208     'help'|'h'|'?')
209         usage
210         ;;
211
212     *)
213         failure "Unknown command: '$COMMAND'
214 Type 'cereal-admin help' for usage."
215         ;;
216 esac