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