fix some authorized_keys updating bugs in ms-server, and update to use
[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 # default return code
25 ERR=0
26
27 ########################################################################
28 # FUNCTIONS
29 ########################################################################
30
31 usage() {
32 cat <<EOF
33 usage: $PGRM <subcommand> [args]
34 MonkeySphere server admin tool.
35
36 subcommands:
37   update-users (u) [USER]...            update users authorized_keys files
38   gen-key (g) [HOSTNAME]                generate gpg key for the server
39   show-fingerprint (f)                  show server's host key fingerprint
40   publish-key (p)                       publish server's host key to keyserver
41   trust-key (t) KEYID [LEVEL]           set owner trust for keyid
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     service=${SERVICE:-"ssh"}
53     userID="${service}://${hostName}"
54
55     if gpg --list-key ="$userID" > /dev/null 2>&1 ; then
56         failure "Key for '$userID' already exists"
57     fi
58
59     # set key defaults
60     KEY_TYPE=${KEY_TYPE:-"RSA"}
61     KEY_LENGTH=${KEY_LENGTH:-"2048"}
62     KEY_USAGE=${KEY_USAGE:-"auth"}
63     KEY_EXPIRE=${KEY_EXPIRE:-"0"}
64     cat <<EOF
65 Please specify how long the key should be valid.
66          0 = key does not expire
67       <n>  = key expires in n days
68       <n>w = key expires in n weeks
69       <n>m = key expires in n months
70       <n>y = key expires in n years
71 EOF
72     read -p "Key is valid for? ($KEY_EXPIRE) " KEY_EXPIRE; KEY_EXPIRE=${KEY_EXPIRE:-"0"}
73
74     # set key parameters
75     keyParameters=$(cat <<EOF
76 Key-Type: $KEY_TYPE
77 Key-Length: $KEY_LENGTH
78 Key-Usage: $KEY_USAGE
79 Name-Real: $userID
80 Expire-Date: $KEY_EXPIRE
81 EOF
82 )
83
84     # add the revoker field if requested
85     # FIXME: the "1:" below assumes that $REVOKER's key is an RSA key.  why?
86     # FIXME: why is this marked "sensitive"?  how will this signature ever
87     # be transmitted to the expected revoker?
88     if [ "$REVOKER" ] ; then
89         keyParameters="${keyParameters}"$(cat <<EOF
90
91 Revoker: 1:$REVOKER sensitive
92 EOF
93 )
94     fi
95
96     echo "The following key parameters will be used:"
97     echo "$keyParameters"
98
99     read -p "Generate key? [Y|n]: " OK; OK=${OK:=Y}
100     if [ ${OK/y/Y} != 'Y' ] ; then
101         failure "aborting."
102     fi
103
104     # add commit command
105     keyParameters="${keyParameters}"$(cat <<EOF
106
107 %commit
108 %echo done
109 EOF
110 )
111
112     log "generating server key... "
113     echo "$keyParameters" | gpg --batch --gen-key
114
115     # output the server fingerprint
116     fingerprint_server_key "=${userID}"
117
118     # find the key fingerprint of the server primary key
119     keyID=$(gpg --list-key --with-colons --with-fingerprint "=${userID}" | \
120         grep '^fpr:' | head -1 | cut -d: -f10)
121
122     # write the key to the file
123     # NOTE: assumes that the primary key is the proper key to use
124     (umask 077 && gpgsecret2ssh "$keyID" > "${MS_HOME}/ssh_host_rsa_key")
125     log "Private SSH host key output to file: ${MS_HOME}/ssh_host_rsa_key"
126 }
127
128 # gpg output key fingerprint
129 fingerprint_server_key() {
130     local ID
131
132     if [ -z "$1" ] ; then
133         ID="$1"
134     else
135         ID="=ssh://$(hostname --fqdn)"
136     fi
137
138     gpg --fingerprint --list-secret-keys "$ID"
139 }
140
141 ########################################################################
142 # MAIN
143 ########################################################################
144
145 COMMAND="$1"
146 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
147 shift
148
149 # set ms home directory
150 MS_HOME=${MS_HOME:-"$ETC"}
151
152 # load configuration file
153 MS_CONF=${MS_CONF:-"$MS_HOME"/monkeysphere-server.conf}
154 [ -e "$MS_CONF" ] && . "$MS_CONF"
155
156 # set empty config variable with defaults
157 GNUPGHOME=${GNUPGHOME:-"${MS_HOME}/gnupg"}
158 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
159 CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
160 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
161 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"%h/.config/monkeysphere/authorized_user_ids"}
162 USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
163
164 export GNUPGHOME
165
166 # make sure the monkeysphere home directory exists
167 mkdir -p "${MS_HOME}/authorized_user_ids"
168 # make sure gpg home exists with proper permissions
169 mkdir -p -m 0700 "$GNUPGHOME"
170 # make sure the authorized_keys directory exists
171 mkdir -p "${CACHE}/authorized_keys"
172
173 case $COMMAND in
174     'update-users'|'update-user'|'u')
175         if [ "$1" ] ; then
176             # get users from command line
177             unames="$@"
178         else
179             # or just look at all users if none specified
180             unames=$(getent passwd | cut -d: -f1)
181         fi
182
183         # loop over users
184         for uname in $unames ; do
185             MODE="authorized_keys"
186
187             # check all specified users exist
188             if ! getent passwd "$uname" >/dev/null ; then
189                 error "----- unknown user '$uname' -----"
190                 continue
191             fi
192
193             log "----- user: $uname -----"
194
195             # set authorized_user_ids variable, translating ssh-style
196             # path variables
197             authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
198
199             # temporary authorized_keys file
200             AUTHORIZED_KEYS=$(mktemp)
201
202             # process authorized_user_ids file
203             if [ -s "$authorizedUserIDs" ] ; then
204                 log "processing authorized_user_ids file..."
205                 process_authorized_user_ids "$authorizedUserIDs"
206             fi
207
208             # add user-controlled authorized_keys file path if specified
209             if [ "$USER_CONTROLLED_AUTHORIZED_KEYS" != '-' ] ; then
210                 userAuthorizedKeys=$(translate_ssh_variables "$uname" "$USER_CONTROLLED_AUTHORIZED_KEYS")
211                 if [ -s "$userAuthorizedKeys" ] ; then
212                     log -n "adding user's authorized_keys file... "
213                     cat "$userAuthorizedKeys" >> "$AUTHORIZED_KEYS"
214                     loge "done."
215                 fi
216             fi
217
218             # if the resulting authorized_keys file is not empty
219             if [ -s "$AUTHORIZED_KEYS" ] ; then
220                 # openssh appears to check the contents of the
221                 # authorized_keys file as the user in question, so the
222                 # file must be readable by that user at least.
223                 # FIXME: is there a better way to do this?
224                 chgrp $(getent passwd "$uname" | cut -f4 -d:) "$AUTHORIZED_KEYS"
225                 chmod g+r "$AUTHORIZED_KEYS"
226
227                 # move the temp authorized_keys file into place
228                 mv -f "$AUTHORIZED_KEYS" "${CACHE}/authorized_keys/${uname}"
229
230                 log "authorized_keys file updated."
231
232             # else destroy it
233             else
234                 rm -f "$AUTHORIZED_KEYS"
235             fi
236         done
237         ;;
238
239     'gen-key'|'g')
240         gen_key "$1"
241         ;;
242
243     'show-fingerprint'|'f')
244         fingerprint_server_key "$@"
245         ;;
246
247     'publish-key'|'p')
248         publish_server_key
249         ;;
250
251     'trust-key'|'trust-key'|'t')
252         trust_key "$@"
253         ;;
254
255     'help'|'h'|'?')
256         usage
257         ;;
258
259     *)
260         failure "Unknown command: '$COMMAND'
261 Type '$PGRM help' for usage."
262         ;;
263 esac
264
265 exit "$ERR"