Update man pages, and tweak default error return code.
[monkeysphere.git] / src / monkeysphere
1 #!/bin/bash
2
3 # monkeysphere: MonkeySphere client 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 SHARE=${MONKEYSPHERE_SHARE:-"/usr/share/monkeysphere"}
15 export SHARE
16 . "${SHARE}/common" || exit 1
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 unset GREP_OPTIONS
23
24 # default return code
25 RETURN=0
26
27 ########################################################################
28 # FUNCTIONS
29 ########################################################################
30
31 usage() {
32 cat <<EOF
33 usage: $PGRM <subcommand> [options] [args]
34 MonkeySphere client tool.
35
36 subcommands:
37   update-known_hosts (k) [HOST]...  update known_hosts file
38   update-authorized_keys (a)        update authorized_keys file
39   gen-subkey (g) KEYID              generate an 'a' capable subkey
40     -l|--length BITS                  key length in bits (2048)
41     -e|--expire EXPIRE                date to expire
42   help (h,?)                        this help
43
44 EOF
45 }
46
47 # generate a subkey with the 'a' usage flags set
48 # FIXME: this needs some tweaking to clean it up
49 gen_subkey(){
50     local keyID
51     local gpgOut
52     local userID
53
54     keyID="$1"
55
56     gpgOut=$(gpg --quiet --fixed-list-mode --list-keys --with-colons \
57         "$keyID" 2> /dev/null)
58
59     # fail if there only "tru" lines are output from gpg, which
60     # indicates the key was not found.
61     if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
62         failure "Key ID '$keyID' not found."
63     fi
64
65     # fail if multiple pub lines are returned, which means the id given
66     # is not unique
67     if [ $(echo "$gpgOut" | grep '^pub:' | wc -l) -gt '1' ] ; then
68         failure "Key ID '$keyID' is not unique."
69     fi
70
71     # prompt if an authentication subkey already exists
72     if echo "$gpgOut" | egrep "^(pub|sub):" | cut -d: -f 12 | grep -q a ; then
73         echo "An authentication subkey already exists for key '$keyID'."
74         read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
75         if [ "${OK/y/Y}" != 'Y' ] ; then
76             failure "aborting."
77         fi
78     fi
79
80     # set subkey defaults
81     KEY_TYPE="RSA"
82     KEY_LENGTH=${KEY_LENGTH:-}
83     KEY_USAGE="auth"
84     # prompt about key expiration if not specified
85     if [ -z "$KEY_EXPIRE" ] ; then
86         cat <<EOF
87 Please specify how long the key should be valid.
88          0 = key does not expire
89       <n>  = key expires in n days
90       <n>w = key expires in n weeks
91       <n>m = key expires in n months
92       <n>y = key expires in n years
93 EOF
94         while [ -z "$KEY_EXPIRE" ] ; do
95             read -p "Key is valid for? (0) " KEY_EXPIRE
96             if ! test_gpg_expire ${KEY_EXPIRE:=0} ; then
97                 echo "invalid value"
98                 unset KEY_EXPIRE
99             fi
100         done
101     elif ! test_gpg_expire "$KEY_EXPIRE" ; then
102         failure "invalid key expiration value '$KEY_EXPIRE'."
103     fi
104
105     # generate the list of commands that will be passed to edit-key
106     editCommands=$(cat <<EOF
107 addkey
108 7
109 S
110 E
111 A
112 Q
113 $KEY_LENGTH
114 $KEY_EXPIRE
115 save
116 EOF
117 )
118
119     log "generating subkey..."
120     echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
121     log "done."
122 }
123
124 ########################################################################
125 # MAIN
126 ########################################################################
127
128 # unset variables that should be defined only in config file
129 unset KEYSERVER
130 unset CHECK_KEYSERVER
131 unset KNOWN_HOSTS
132 unset HASH_KNOWN_HOSTS
133 unset AUTHORIZED_KEYS
134
135 # load global config
136 [ -r "${ETC}/monkeysphere.conf" ] && . "${ETC}/monkeysphere.conf"
137
138 # set monkeysphere home directory
139 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.config/monkeysphere"}
140 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
141
142 # load local config
143 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] && . "$MONKEYSPHERE_CONFIG"
144
145 # set empty config variables with ones from the environment, or from
146 # config file, or with defaults
147 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
148 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
149 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
150 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
151 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
152 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
153
154 # other variables not in config file
155 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
156 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
157 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
158
159 # export GNUPGHOME and make sure gpg home exists with proper
160 # permissions
161 export GNUPGHOME
162 mkdir -p -m 0700 "$GNUPGHOME"
163
164 # get subcommand
165 COMMAND="$1"
166 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
167 shift
168
169 # unset option variables
170 unset KEY_LENGTH
171 unset KEY_EXPIRE
172
173 # get options for key generation and add-certifier functions
174 TEMP=$(getopt -o l:e: -l length:,expire: -n "$PGRM" -- "$@")
175
176 if [ $? != 0 ] ; then
177     usage
178     exit 1
179 fi
180
181 # Note the quotes around `$TEMP': they are essential!
182 eval set -- "$TEMP"
183
184 while true ; do
185     case "$1" in
186         -l|--length)
187             KEY_LENGTH="$2"
188             shift 2
189             ;;
190         -e|--expire)
191             KEY_EXPIRE="$2"
192             shift 2
193             ;;
194         --)
195             shift
196             ;;
197         *)
198             break
199             ;;
200     esac
201 done
202
203 case $COMMAND in
204     'update-known_hosts'|'update-known-hosts'|'k')
205         MODE='known_hosts'
206
207         # if hosts are specified on the command line, process just
208         # those hosts
209         if [ "$1" ] ; then
210             update_known_hosts "$@"
211             RETURN="$?"
212
213         # otherwise, if no hosts are specified, process every host
214         # in the user's known_hosts file
215         else
216             if [ ! -s "$KNOWN_HOSTS" ] ; then
217                 failure "known_hosts file '$KNOWN_HOSTS' is empty or does not exist."
218             fi
219
220             process_known_hosts
221             RETURN="$?"
222         fi
223         ;;
224
225     'update-authorized_keys'|'update-authorized-keys'|'a')
226         MODE='authorized_keys'
227
228         # fail if the authorized_user_ids file is empty
229         if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
230             failure "$AUTHORIZED_USER_IDS is empty or does not exist."
231         fi
232
233         # process authorized_user_ids file
234         process_authorized_user_ids "$AUTHORIZED_USER_IDS"
235         RETURN="$?"
236         ;;
237
238     'gen-subkey'|'g')
239         keyID="$1"
240         if [ -z "$keyID" ] ; then
241             failure "You must specify the key ID of your primary key."
242         fi
243         gen_subkey "$keyID"
244         ;;
245
246     'help'|'h'|'?')
247         usage
248         ;;
249
250     *)
251         failure "Unknown command: '$COMMAND'
252 Type '$PGRM help' for usage."
253         ;;
254 esac
255
256 exit "$RETURN"