Major rework to remove all caching. Everything processed straight
[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 --quiet --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     log "generating subkey..."
94     echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
95     log "done."
96 }
97
98 ########################################################################
99 # MAIN
100 ########################################################################
101
102 COMMAND="$1"
103 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
104 shift
105
106 # set ms home directory
107 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
108
109 # load configuration file
110 MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
111 [ -e "$MS_CONF" ] && . "$MS_CONF"
112
113 # set empty config variable with defaults
114 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
115 GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
116 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
117 CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
118 REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"e a"}
119 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
120 KNOWN_HOSTS=${KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
121 AUTHORIZED_KEYS=${AUTHORIZED_KEYS:-"${HOME}/.ssh/authorized_keys"}
122 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
123
124 export GNUPGHOME
125
126 # make sure gpg home exists with proper permissions
127 mkdir -p -m 0700 "$GNUPGHOME"
128
129 # make sure the user monkeysphere home directory exists
130 mkdir -p -m 0700 "$MS_HOME"
131 touch "$AUTHORIZED_USER_IDS"
132 touch "$AUTHORIZED_KEYS"
133
134 case $COMMAND in
135     'update-known_hosts'|'update-known-hosts'|'k')
136         MODE='known_hosts'
137
138         # touch the known_hosts file to make sure it exists
139         # ssh-keygen complains if it doesn't exist
140         touch "$KNOWN_HOSTS"
141
142         # if hosts are specified on the command line, process just
143         # those hosts
144         if [ "$1" ] ; then
145             for host ; do
146                 process_host_known_hosts "$host"
147             done
148             log "known_hosts file updated."
149
150         # otherwise, if no hosts are specified, process every user
151         # in the user's known_hosts file
152         else
153             if [ ! -s "$KNOWN_HOSTS" ] ; then
154                 failure "known_hosts file '$KNOWN_HOSTS' is empty."
155             fi
156             log "processing known_hosts file..."
157             process_known_hosts
158             log "known_hosts file updated."
159         fi
160         ;;
161
162     'update-userids'|'update-userid'|'u')
163         if [ -z "$1" ] ; then
164             failure "you must specify at least one userid."
165         fi
166         for userID ; do
167             update_userid "$userID"
168         done
169         log "Run the following to update your monkeysphere authorized_keys file:"
170         log "$PGRM update-authorized_keys"
171         ;;
172
173     'remove-userids'|'remove-userid'|'r')
174         if [ -z "$1" ] ; then
175             failure "you must specify at least one userid."
176         fi
177         for userID ; do
178             remove_userid "$userID"
179         done
180         log "Run the following to update your monkeysphere authorized_keys file:"
181         log "$PGRM update-authorized_keys"
182         ;;
183
184     'update-authorized_keys'|'update-authorized-keys'|'a')
185         MODE='authorized_keys'
186
187         # fail if the authorized_user_ids file is empty
188         if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
189             failure "$AUTHORIZED_USER_IDS is empty."
190         fi
191
192         # process authorized_user_ids file
193         log "processing authorized_user_ids file..."
194         process_authorized_user_ids
195         log "authorized_keys file updated."
196         ;;
197
198     'gen-subkey'|'g')
199         keyID="$1"
200         if [ -z "$keyID" ] ; then
201             failure "You must specify the key ID of your primary key."
202         fi
203         gen_subkey "$keyID"
204         ;;
205
206     'help'|'h'|'?')
207         usage
208         ;;
209
210     *)
211         failure "Unknown command: '$COMMAND'
212 Type '$PGRM help' for usage."
213         ;;
214 esac