Provide better (ie. more informative) return codes. Required some
[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 SHAREDIR=${SHAREDIR:-"/usr/share/monkeysphere"}
15 export SHAREDIR
16 . "${SHAREDIR}/common"
17
18 GLOBAL_CONFIG=${GLOBAL_CONFIG:-"${ETC}/monkeysphere.conf"}
19 [ -r "$GLOBAL_CONFIG" ] && . "$GLOBAL_CONFIG"
20
21 # date in UTF format if needed
22 DATE=$(date -u '+%FT%T')
23
24 # unset some environment variables that could screw things up
25 GREP_OPTIONS=
26
27 # default return code
28 ERR=0
29 export ERR
30
31 ########################################################################
32 # FUNCTIONS
33 ########################################################################
34
35 usage() {
36 cat <<EOF
37 usage: $PGRM <subcommand> [args]
38 MonkeySphere client tool.
39
40 subcommands:
41   update-known_hosts (k) [HOST]...  update known_hosts file
42   update-authorized_keys (a)        update authorized_keys file
43   gen-subkey (g) KEYID              generate an 'a' capable subkey
44   help (h,?)                        this help
45
46 EOF
47 }
48
49 # generate a subkey with the 'a' usage flags set
50 # FIXME: this needs some tweaking to clean it up
51 gen_subkey(){
52     local keyID
53     local gpgOut
54     local userID
55
56     keyID="$1"
57
58     gpgOut=$(gpg --quiet --fixed-list-mode --list-keys --with-colons \
59         "$keyID" 2> /dev/null)
60
61     # fail if there only "tru" lines are output from gpg, which
62     # indicates the key was not found.
63     if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
64         failure "Key ID '$keyID' not found."
65     fi
66
67     # fail if multiple pub lines are returned, which means the id given
68     # is not unique
69     if [ $(echo "$gpgOut" | grep '^pub:' | wc -l) -gt '1' ] ; then
70         failure "Key ID '$keyID' is not unique."
71     fi
72
73     # prompt if an authentication subkey already exists
74     if echo "$gpgOut" | egrep "^(pub|sub):" | cut -d: -f 12 | grep -q a ; then
75         echo "An authentication subkey already exists for key '$keyID'."
76         read -p "Are you sure you would like to generate another one? [y|N]: " OK; OK=${OK:N}
77         if [ "${OK/y/Y}" != 'Y' ] ; then
78             failure "aborting."
79         fi
80     fi
81
82     # set subkey defaults
83     SUBKEY_TYPE=${SUBKEY_TYPE:-"RSA"}
84     SUBKEY_LENGTH=${SUBKEY_LENGTH:-}
85     SUBKEY_USAGE=${SUBKEY_USAGE:-"auth"}
86     SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
87     cat <<EOF
88 Please specify how long the key should be valid.
89          0 = key does not expire
90       <n>  = key expires in n days
91       <n>w = key expires in n weeks
92       <n>m = key expires in n months
93       <n>y = key expires in n years
94 EOF
95     read -p "Key is valid for? ($SUBKEY_EXPIRE) " SUBKEY_EXPIRE; SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
96
97     # generate the list of commands that will be passed to edit-key
98     editCommands=$(cat <<EOF
99 addkey
100 7
101 S
102 E
103 A
104 Q
105 $SUBKEY_LENGTH
106 $SUBKEY_EXPIRE
107 save
108 EOF
109 )
110
111     log "generating subkey..."
112     echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
113     log "done."
114 }
115
116 ########################################################################
117 # MAIN
118 ########################################################################
119
120 COMMAND="$1"
121 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
122 shift
123
124 # set ms home directory
125 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
126
127 # load configuration file
128 MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
129 [ -e "$MS_CONF" ] && . "$MS_CONF"
130
131 # set empty config variable with defaults
132 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
133 GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
134 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
135 CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
136 REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"a"}
137 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
138 KNOWN_HOSTS=${KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
139 AUTHORIZED_KEYS=${AUTHORIZED_KEYS:-"${HOME}/.ssh/authorized_keys"}
140 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
141
142 export GNUPGHOME
143
144 # make sure gpg home exists with proper permissions
145 mkdir -p -m 0700 "$GNUPGHOME"
146
147 # make sure the user monkeysphere home directory exists
148 mkdir -p -m 0700 "$MS_HOME"
149 touch "$AUTHORIZED_USER_IDS"
150 touch "$AUTHORIZED_KEYS"
151
152 case $COMMAND in
153     'update-known_hosts'|'update-known-hosts'|'k')
154         MODE='known_hosts'
155
156         # touch the known_hosts file to make sure it exists
157         # ssh-keygen complains if it doesn't exist
158         touch "$KNOWN_HOSTS"
159
160         # if hosts are specified on the command line, process just
161         # those hosts
162         if [ "$1" ] ; then
163             update_known_hosts "$@" || ERR=1
164
165         # otherwise, if no hosts are specified, process every host
166         # in the user's known_hosts file
167         else
168             if [ ! -s "$KNOWN_HOSTS" ] ; then
169                 failure "known_hosts file '$KNOWN_HOSTS' is empty."
170             fi
171             log "processing known_hosts file..."
172             process_known_hosts || ERR=1
173         fi
174
175         log "known_hosts file updated."
176         ;;
177
178     'update-authorized_keys'|'update-authorized-keys'|'a')
179         MODE='authorized_keys'
180
181         # fail if the authorized_user_ids file is empty
182         if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
183             failure "$AUTHORIZED_USER_IDS is empty."
184         fi
185
186         # process authorized_user_ids file
187         log "processing authorized_user_ids file..."
188         process_authorized_user_ids "$AUTHORIZED_USER_IDS" || ERR=1
189         log "authorized_keys file updated."
190         ;;
191
192     'gen-subkey'|'g')
193         keyID="$1"
194         if [ -z "$keyID" ] ; then
195             failure "You must specify the key ID of your primary key."
196         fi
197         gen_subkey "$keyID"
198         ;;
199
200     'help'|'h'|'?')
201         usage
202         ;;
203
204     *)
205         failure "Unknown command: '$COMMAND'
206 Type '$PGRM help' for usage."
207         ;;
208 esac
209
210 exit "$ERR"