fix typo
[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 USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
143
144 export GNUPGHOME
145
146 # make sure the monkeysphere home directory exists
147 mkdir -p "${MS_HOME}/authorized_user_ids"
148 # make sure gpg home exists with proper permissions
149 mkdir -p -m 0700 "$GNUPGHOME"
150 # make sure the authorized_keys directory exists
151 mkdir -p "${CACHE}/authorized_keys"
152
153 case $COMMAND in
154     'update-users'|'update-user'|'s')
155         if [ "$1" ] ; then
156             unames="$@"
157         else
158             unames=$(ls -1 "${MS_HOME}/authorized_user_ids")
159         fi
160
161         for uname in $unames ; do
162             MODE="authorized_keys"
163
164             log "----- user: $uname -----"
165
166             # set variables for the user
167             AUTHORIZED_USER_IDS="${MS_HOME}/authorized_user_ids/${uname}"
168             # temporary authorized_keys file
169             AUTHORIZED_KEYS="${CACHE}/authorized_keys/${uname}.tmp"
170
171             # make sure user's authorized_user_ids file exists
172             touch "$AUTHORIZED_USER_IDS"
173             # make sure the authorized_keys file exists and is clear
174             > "$AUTHORIZED_KEYS"
175
176             # skip if the user's authorized_user_ids file is empty
177             if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
178                 log "authorized_user_ids file for '$uname' is empty."
179                 continue
180             fi
181
182             # process authorized_user_ids file
183             log "processing authorized_user_ids file..."
184             process_authorized_user_ids
185
186             # add user-controlled authorized_keys file path if specified
187             if [ "$USER_CONTROLLED_AUTHORIZED_KEYS" != '-' ] ; then
188                 userHome=$(getent passwd "$uname" | cut -d: -f6)
189                 userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$userHome"}
190                 if [ -f "$userAuthorizedKeys" ] ; then
191                     log -n "adding user's authorized_keys file... "
192                     cat "$userAuthorizedKeys" >> "$AUTHORIZED_KEYS"
193                     loge "done."
194                 fi
195             fi
196
197             # move the temp authorized_keys file into place
198             mv -f "${CACHE}/authorized_keys/${uname}.tmp" "${CACHE}/authorized_keys/${uname}"
199
200             log "authorized_keys file updated."
201         done
202
203         log "----- done. -----"
204         ;;
205
206     'gen-key'|'g')
207         gen_key "$1"
208         ;;
209
210     'show-fingerprint'|'f')
211         fingerprint_server_key
212         ;;
213
214     'publish-key'|'p')
215         publish_server_key
216         ;;
217
218     'trust-keys'|'trust-key'|'t')
219         if [ -z "$1" ] ; then
220             failure "You must specify at least one key to trust."
221         fi
222
223         # process key IDs
224         for keyID ; do
225             trust_key "$keyID"
226         done
227         ;;
228
229     'update-user-userids'|'update-user-userid'|'u')
230         uname="$1"
231         shift
232         if [ -z "$uname" ] ; then
233             failure "You must specify user."
234         fi
235         if [ -z "$1" ] ; then
236             failure "You must specify at least one user ID."
237         fi
238
239         # set variables for the user
240         AUTHORIZED_USER_IDS="$MS_HOME"/authorized_user_ids/"$uname"
241
242         # make sure user's authorized_user_ids file exists
243         touch "$AUTHORIZED_USER_IDS"
244
245         # process the user IDs
246         for userID ; do
247             update_userid "$userID"
248         done
249
250         log "Run the following to update user's authorized_keys file:"
251         log "$PGRM update-users $uname"
252         ;;
253
254     'remove-user-userids'|'remove-user-userid'|'r')
255         uname="$1"
256         shift
257         if [ -z "$uname" ] ; then
258             failure "You must specify user."
259         fi
260         if [ -z "$1" ] ; then
261             failure "You must specify at least one user ID."
262         fi
263
264         # set variables for the user
265         AUTHORIZED_USER_IDS="$MS_HOME"/authorized_user_ids/"$uname"
266
267         # make sure user's authorized_user_ids file exists
268         touch "$AUTHORIZED_USER_IDS"
269
270         # process the user IDs
271         for userID ; do
272             remove_userid "$userID"
273         done
274
275         log "Run the following to update user's authorized_keys file:"
276         log "$PGRM update-users $uname"
277         ;;
278
279     'help'|'h'|'?')
280         usage
281         ;;
282
283     *)
284         failure "Unknown command: '$COMMAND'
285 Type '$PGRM help' for usage."
286         ;;
287 esac