Allow for specification of whether to check keyserver.
[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 ########################################################################
28 # FUNCTIONS
29 ########################################################################
30
31 usage() {
32 cat <<EOF
33 usage: $PGRM <subcommand> [args]
34 MonkeySphere client tool.
35
36 subcommands:
37   update-known_hosts (k) [HOST]...  update known_hosts file
38   update-userids (u) [USERID]...    add/update user IDs
39   remove-userids (r) [USERID]...    remove user IDs
40   update-authorized_keys (a)        update authorized_keys file
41   gen-subkey (g) KEYID              generate an 'a' capable subkey
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 --fixed-list-mode --list-keys --with-colons \
57         "$keyID" 2> /dev/null)
58
59     # return 1 if there only "tru" lines are output from gpg
60     if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
61         failure "Key ID '$keyID' not found."
62     fi
63
64     # set subkey defaults
65     SUBKEY_TYPE=${SUBKEY_TYPE:-"RSA"}
66     #SUBKEY_LENGTH=${SUBKEY_LENGTH:-"2048"}
67     SUBKEY_USAGE=${SUBKEY_USAGE:-"auth"}
68     SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
69     cat <<EOF
70 Please specify how long the key should be valid.
71          0 = key does not expire
72       <n>  = key expires in n days
73       <n>w = key expires in n weeks
74       <n>m = key expires in n months
75       <n>y = key expires in n years
76 EOF
77     read -p "Key is valid for? ($SUBKEY_EXPIRE) " SUBKEY_EXPIRE; SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
78
79     # generate the list of commands that will be passed to edit-key
80     editCommands=$(cat <<EOF
81 addkey
82 7
83 S
84 E
85 A
86 Q
87 $SUBKEY_LENGTH
88 $SUBKEY_EXPIRE
89 save
90 EOF
91 )
92
93     echo "generating subkey..."
94     echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
95 }
96
97 ########################################################################
98 # MAIN
99 ########################################################################
100
101 COMMAND="$1"
102 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
103 shift
104
105 # set ms home directory
106 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
107
108 # load configuration file
109 MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
110 [ -e "$MS_CONF" ] && . "$MS_CONF"
111
112 # set empty config variable with defaults
113 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
114 GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
115 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
116 REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"e a"}
117 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
118 USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
119 USER_KNOWN_HOSTS=${USER_KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
120 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
121
122 export GNUPGHOME
123
124 # stagging locations
125 hostKeysCacheDir="${MS_HOME}/host_keys"
126 userKeysCacheDir="${MS_HOME}/user_keys"
127 msAuthorizedKeys="${MS_HOME}/authorized_keys"
128
129 # make sure gpg home exists with proper permissions
130 mkdir -p -m 0700 "$GNUPGHOME"
131
132 # make sure the user monkeysphere home directory exists
133 mkdir -p -m 0700 "$MS_HOME"
134 mkdir -p "$hostKeysCacheDir"
135 mkdir -p "$userKeysCacheDir"
136 touch "$AUTHORIZED_USER_IDS"
137
138 case $COMMAND in
139     'update-known_hosts'|'update-known-hosts'|'k')
140         MODE='known_hosts'
141
142         # touch the known_hosts file to make sure it exists
143         # ssh-keygen complains if it doesn't exist
144         touch "$USER_KNOWN_HOSTS"
145
146         # if hosts are specified on the command line, process just
147         # those hosts
148         if [ "$1" ] ; then
149             for host ; do
150                 process_host "$host" "$hostKeysCacheDir"
151             done
152
153         # otherwise, if no hosts are specified, process every user
154         # in the user's known_hosts file
155         else
156             if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
157                 failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
158             fi
159             log "processing known_hosts file..."
160             process_known_hosts "$hostKeysCacheDir"
161         fi
162         ;;
163
164     'update-userids'|'update-userid'|'u')
165         if [ -z "$1" ] ; then
166             failure "you must specify at least one userid."
167         fi
168         for userID ; do
169             update_userid "$userID" "$userKeysCacheDir"
170         done
171         log "Run the following to update your monkeysphere authorized_keys file:"
172         log "$PGRM update-authorized_keys"
173         ;;
174
175     'remove-userids'|'remove-userid'|'r')
176         if [ -z "$1" ] ; then
177             failure "you must specify at least one userid."
178         fi
179         for userID ; do
180             remove_userid "$userID"
181         done
182         log "Run the following to update your monkeysphere authorized_keys file:"
183         log "$PGRM update-authorized_keys"
184         ;;
185
186     'update-authorized_keys'|'update-authorized-keys'|'a')
187         MODE='authorized_keys'
188
189         # fail if the authorized_user_ids file is empty
190         if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
191             failure "$AUTHORIZED_USER_IDS is empty."
192         fi
193
194         # set user-controlled authorized_keys file path
195         userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
196
197         # update authorized_keys
198         update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$userKeysCacheDir"
199         ;;
200
201     'gen-subkey'|'g')
202         keyID="$1"
203         if [ -z "$keyID" ] ; then
204             failure "You must specify the key ID of your primary key."
205         fi
206         gen_subkey "$keyID"
207         ;;
208
209     'help'|'h'|'?')
210         usage
211         ;;
212
213     *)
214         failure "Unknown command: '$COMMAND'
215 Type '$PGRM help' for usage."
216         ;;
217 esac