Added server config variable to specify user authorized_user_ids file,
[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 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   gen-key (g) [HOSTNAME]                generate gpg key for the server
35   show-fingerprint (f)                  show server's host key fingerprint
36   publish-key (p)                       publish server key to keyserver
37   trust-keys (t) KEYID...               mark keyids as trusted
38
39   update-users (s) [USER]...            update users authorized_keys files
40   update-user-userids (u) USER UID...   add/update user IDs for a user
41   remove-user-userids (r) USER UID...   remove user IDs for a user
42   help (h,?)                            this help
43
44 EOF
45 }
46
47 # generate server gpg key
48 gen_key() {
49     local hostName
50
51     hostName=${1:-$(hostname --fqdn)}
52
53     # set key defaults
54     KEY_TYPE=${KEY_TYPE:-"RSA"}
55     KEY_LENGTH=${KEY_LENGTH:-"2048"}
56     KEY_USAGE=${KEY_USAGE:-"auth"}
57     cat <<EOF
58 Please specify how long the key should be valid.
59          0 = key does not expire
60       <n>  = key expires in n days
61       <n>w = key expires in n weeks
62       <n>m = key expires in n months
63       <n>y = key expires in n years
64 EOF
65     read -p "Key is valid for? ($EXPIRE) " EXPIRE; EXPIRE=${EXPIRE:-"0"}
66
67     SERVICE=${SERVICE:-"ssh"}
68     USERID=${USERID:-"$SERVICE"://"$hostName"}
69
70     # set key parameters
71     keyParameters=$(cat <<EOF
72 Key-Type: $KEY_TYPE
73 Key-Length: $KEY_LENGTH
74 Key-Usage: $KEY_USAGE
75 Name-Real: $USERID
76 Expire-Date: $EXPIRE
77 EOF
78 )
79
80     # add the revoker field if requested
81 # FIXME: the 1: below assumes that $REVOKER's key is an RSA key.  why?
82 # FIXME: why is this marked "sensitive"?  how will this signature ever
83 # be transmitted to the expected revoker?
84     if [ "$REVOKER" ] ; then
85         keyParameters="${keyParameters}"$(cat <<EOF
86
87 Revoker: 1:$REVOKER sensitive
88 EOF
89 )
90     fi
91
92     echo "The following key parameters will be used:"
93     echo "$keyParameters"
94
95     read -p "generate key? [Y|n]: " OK; OK=${OK:=Y}
96     if [ ${OK/y/Y} != 'Y' ] ; then
97         failure "aborting."
98     fi
99
100     if gpg --list-key ="$USERID" > /dev/null 2>&1 ; then
101         failure "key for '$USERID' already exists"
102     fi
103
104     # add commit command
105     keyParameters="${keyParameters}"$(cat <<EOF
106
107 %commit
108 %echo done
109 EOF
110 )
111
112     log -n "generating server key... "
113     echo "$keyParameters" | gpg --batch --gen-key
114     loge "done."
115     fingerprint_server_key
116 }
117
118 fingerprint_server_key() {
119     gpg --fingerprint --list-secret-keys =ssh://$(hostname --fqdn)
120 }
121
122 ########################################################################
123 # MAIN
124 ########################################################################
125
126 COMMAND="$1"
127 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
128 shift
129
130 # set ms home directory
131 MS_HOME=${MS_HOME:-"$ETC"}
132
133 # load configuration file
134 MS_CONF=${MS_CONF:-"$MS_HOME"/monkeysphere-server.conf}
135 [ -e "$MS_CONF" ] && . "$MS_CONF"
136
137 # set empty config variable with defaults
138 GNUPGHOME=${GNUPGHOME:-"${MS_HOME}/gnupg"}
139 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
140 CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
141 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
142 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"%h/.config/monkeysphere/authorized_user_ids"}
143 USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
144
145 export GNUPGHOME
146
147 # make sure the monkeysphere home directory exists
148 mkdir -p "${MS_HOME}/authorized_user_ids"
149 # make sure gpg home exists with proper permissions
150 mkdir -p -m 0700 "$GNUPGHOME"
151 # make sure the authorized_keys directory exists
152 mkdir -p "${CACHE}/authorized_keys"
153
154 case $COMMAND in
155     'update-users'|'update-user'|'s')
156         if [ "$1" ] ; then
157             # get users from command line
158             unames="$@"
159         else
160             # or just look at all users if none specified
161             unames=$(getent passwd | cut -d: -f1)
162         fi
163
164         # loop over users
165         for uname in $unames ; do
166             MODE="authorized_keys"
167
168             # set authorized_user_ids variable,
169             # translate ssh-style path variables
170             authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
171
172             # skip user if authorized_user_ids file does not exist
173             if [ ! -f "$authorizedUserIDs" ] ; then
174                 continue
175             fi
176
177             log "----- user: $uname -----"
178
179             # temporary authorized_keys file
180             AUTHORIZED_KEYS=$(mktemp)
181
182             # skip if the user's authorized_user_ids file is empty
183             if [ ! -s "$authorizedUserIDs" ] ; then
184                 log "authorized_user_ids file '$authorizedUserIDs' is empty."
185                 continue
186             fi
187
188             # process authorized_user_ids file
189             log "processing authorized_user_ids file..."
190             process_authorized_user_ids "$authorizedUserIDs"
191
192             # add user-controlled authorized_keys file path if specified
193             if [ "$USER_CONTROLLED_AUTHORIZED_KEYS" != '-' ] ; then
194                 userAuthorizedKeys=$(translate_ssh_variables "$uname" "$USER_CONTROLLED_AUTHORIZED_KEYS")
195                 if [ -f "$userAuthorizedKeys" ] ; then
196                     log -n "adding user's authorized_keys file... "
197                     cat "$userAuthorizedKeys" >> "$AUTHORIZED_KEYS"
198                     loge "done."
199                 fi
200             fi
201
202             # move the temp authorized_keys file into place
203             mv -f "$AUTHORIZED_KEYS" "${CACHE}/authorized_keys/${uname}"
204
205             log "authorized_keys file updated."
206         done
207
208         log "----- done. -----"
209         ;;
210
211     'gen-key'|'g')
212         gen_key "$1"
213         ;;
214
215     'show-fingerprint'|'f')
216         fingerprint_server_key
217         ;;
218
219     'publish-key'|'p')
220         publish_server_key
221         ;;
222
223     'trust-keys'|'trust-key'|'t')
224         if [ -z "$1" ] ; then
225             failure "You must specify at least one key to trust."
226         fi
227
228         # process key IDs
229         for keyID ; do
230             trust_key "$keyID"
231         done
232         ;;
233
234     'update-user-userids'|'update-user-userid'|'u')
235         uname="$1"
236         shift
237         if [ -z "$uname" ] ; then
238             failure "You must specify user."
239         fi
240         if [ -z "$1" ] ; then
241             failure "You must specify at least one user ID."
242         fi
243
244         # set authorized_user_ids variable,
245         # translate ssh-style path variables
246         authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
247
248         # make sure user's authorized_user_ids file exists
249         touch "$authorizedUserIDs"
250
251         # process the user IDs
252         for userID ; do
253             update_userid "$userID" "$authorizedUserIDs"
254         done
255
256         log "Run the following to update user's authorized_keys file:"
257         log "$PGRM update-users $uname"
258         ;;
259
260     'remove-user-userids'|'remove-user-userid'|'r')
261         uname="$1"
262         shift
263         if [ -z "$uname" ] ; then
264             failure "You must specify user."
265         fi
266         if [ -z "$1" ] ; then
267             failure "You must specify at least one user ID."
268         fi
269
270         # set authorized_user_ids variable,
271         # translate ssh-style path variables
272         authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
273
274         # make sure user's authorized_user_ids file exists
275         if [ ! -f "$authorizedUserIDs" ] ; then
276             failure "authorized_user_ids file '$authorizedUserIDs' does not exist."
277         fi
278
279         # process the user IDs
280         for userID ; do
281             remove_userid "$userID" "$authorizedUserIDs"
282         done
283
284         log "Run the following to update user's authorized_keys file:"
285         log "$PGRM update-users $uname"
286         ;;
287
288     'help'|'h'|'?')
289         usage
290         ;;
291
292     *)
293         failure "Unknown command: '$COMMAND'
294 Type '$PGRM help' for usage."
295         ;;
296 esac