The monkeysphere {import,gen}_subkey functions were not up-to-date.
[monkeysphere.git] / src / monkeysphere
1 #!/usr/bin/env bash
2
3 # monkeysphere: Monkeysphere client tool
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
9 # Micah Anderson <micah@riseup.net>
10 #
11 # They are Copyright 2008-2009, and are all released under the GPL, version 3
12 # or later.
13
14 ########################################################################
15 PGRM=$(basename $0)
16
17 SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
18 export SYSSHAREDIR
19 . "${SYSSHAREDIR}/common" || exit 1
20
21 # sharedir for host functions
22 MSHAREDIR="${SYSSHAREDIR}/m"
23
24 # UTC date in ISO 8601 format if needed
25 DATE=$(date -u '+%FT%T')
26
27 # unset some environment variables that could screw things up
28 unset GREP_OPTIONS
29
30 # default return code
31 RETURN=0
32
33 # set the file creation mask to be only owner rw
34 umask 077
35
36 ########################################################################
37 # FUNCTIONS
38 ########################################################################
39
40 usage() {
41     cat <<EOF >&2
42 usage: $PGRM <subcommand> [options] [args]
43 Monkeysphere client tool.
44
45 subcommands:
46  update-known_hosts (k) [HOST]...    update known_hosts file
47  update-authorized_keys (a)          update authorized_keys file
48  import-subkey (i) FILE [KEYID]      import existing ssh key as gpg subkey
49  gen-subkey (g) [KEYID]              generate an authentication subkey
50    --length (-l) BITS                  key length in bits (2048)
51  ssh-proxycommand                    monkeysphere ssh ProxyCommand
52  subkey-to-ssh-agent (s)             store authentication subkey in ssh-agent
53  version (v)                         show version number
54  help (h,?)                          this help
55
56 EOF
57 }
58
59 # take a secret key ID and check that only zero or one ID is provided,
60 # and that it corresponds to only a single secret key ID
61 check_gpg_sec_key_id() {
62     local gpgSecOut
63
64     case "$#" in
65         0)
66             gpgSecOut=$(gpg --quiet --fixed-list-mode --list-secret-keys --with-colons 2>/dev/null | egrep '^sec:')
67             ;;
68         1)
69             gpgSecOut=$(gpg --quiet --fixed-list-mode --list-secret-keys --with-colons "$keyID" | egrep '^sec:') || failure
70             ;;
71         *)
72             failure "You must specify only a single primary key ID."
73             ;;
74     esac
75
76     # check that only a single secret key was found
77     case $(echo "$gpgSecOut" | grep -c '^sec:') in
78         0)
79             failure "No secret keys found.  Create an OpenPGP key with the following command:
80  gpg --gen-key"
81             ;;
82         1)
83             echo "$gpgSecOut" | cut -d: -f5
84             ;;
85         *)
86             echo "Multiple primary secret keys found:" | log error
87             echo "$gpgSecOut" | cut -d: -f5 | log error
88             echo "Please specify which primary key to use." | log error
89             failure
90             ;;
91     esac
92 }
93
94 # check that a valid authentication subkey does not already exist
95 check_gpg_authentication_subkey() {
96     local keyID
97     local IFS
98     local line
99     local type
100     local validity
101     local usage
102
103     keyID="$1"
104
105     # check that a valid authentication key does not already exist
106     IFS=$'\n'
107     for line in $(gpg --quiet --fixed-list-mode --list-keys --with-colons "$keyID") ; do
108         type=$(echo "$line" | cut -d: -f1)
109         validity=$(echo "$line" | cut -d: -f2)
110         usage=$(echo "$line" | cut -d: -f12)
111
112         # look at keys only
113         if [ "$type" != 'pub' -a "$type" != 'sub' ] ; then
114             continue
115         fi
116         # check for authentication capability
117         if ! check_capability "$usage" 'a' ; then
118             continue
119         fi
120         # if authentication key is valid, prompt to continue
121         if [ "$validity" = 'u' ] ; then
122             log error "A valid authentication key already exists for primary key '$keyID'."
123             if [ "$PROMPT" = "true" ] ; then
124                 read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
125                 if [ "${OK/y/Y}" != 'Y' ] ; then
126                     failure "aborting."
127                 fi
128                 break
129             else
130                 failure "aborting."
131             fi
132         fi
133     done
134 }
135
136 ########################################################################
137 # MAIN
138 ########################################################################
139
140 # set unset default variables
141 GNUPGHOME=${GNUPGHOME:="${HOME}/.gnupg"}
142 KNOWN_HOSTS="${HOME}/.ssh/known_hosts"
143 HASH_KNOWN_HOSTS="true"
144 AUTHORIZED_KEYS="${HOME}/.ssh/authorized_keys"
145
146 # unset the check keyserver variable, since that needs to have
147 # different defaults for the different functions
148 unset CHECK_KEYSERVER
149
150 # load global config
151 [ -r "${SYSCONFIGDIR}/monkeysphere.conf" ] \
152     && . "${SYSCONFIGDIR}/monkeysphere.conf"
153
154 # set monkeysphere home directory
155 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.monkeysphere"}
156 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
157
158 # load local config
159 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] \
160     && . "$MONKEYSPHERE_CONFIG"
161
162 # set empty config variables with ones from the environment
163 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=$GNUPGHOME}
164 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
165 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
166 # if keyserver not specified in env or conf, then look in gpg.conf
167 if [ -z "$KEYSERVER" ] ; then
168     if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
169         KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
170     fi
171 fi
172 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
173 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=$KNOWN_HOSTS}
174 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=$HASH_KNOWN_HOSTS}
175 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=$AUTHORIZED_KEYS}
176
177 # other variables not in config file
178 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
179 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
180 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
181
182 # export GNUPGHOME and make sure gpg home exists with proper
183 # permissions
184 export GNUPGHOME
185 mkdir -p -m 0700 "$GNUPGHOME"
186 export LOG_LEVEL
187
188 # get subcommand
189 COMMAND="$1"
190 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
191 shift
192
193 case $COMMAND in
194     'update-known_hosts'|'update-known-hosts'|'k')
195         # whether or not to check keyservers
196         CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
197
198         # if hosts are specified on the command line, process just
199         # those hosts
200         if [ "$1" ] ; then
201             update_known_hosts "$@"
202             RETURN="$?"
203
204         # otherwise, if no hosts are specified, process every host
205         # in the user's known_hosts file
206         else
207             process_known_hosts
208             RETURN="$?"
209         fi
210         ;;
211
212     'update-authorized_keys'|'update-authorized-keys'|'a')
213         # whether or not to check keyservers
214         CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
215
216         # process authorized_user_ids file
217         process_authorized_user_ids "$AUTHORIZED_USER_IDS"
218         RETURN="$?"
219         ;;
220
221     'import-subkey'|'i')
222         source "${MSHAREDIR}/import_subkey"
223         import_subkey "$@"
224         ;;
225
226     'gen-subkey'|'g')
227         source "${MSHAREDIR}/gen_subkey"
228         gen_subkey "$@"
229         ;;
230
231     'ssh-proxycommand'|'p')
232         source "${MSHAREDIR}/ssh_proxycommand"
233         ssh_proxycommand "$@"
234         ;;
235
236     'subkey-to-ssh-agent'|'s')
237         source "${MSHAREDIR}/subkey_to_ssh_agent"
238         subkey_to_ssh_agent "$@"
239         ;;
240
241     'version'|'v')
242         echo "$VERSION"
243         ;;
244
245     '--help'|'help'|'-h'|'h'|'?')
246         usage
247         ;;
248
249     *)
250         failure "Unknown command: '$COMMAND'
251 Type '$PGRM help' for usage."
252         ;;
253 esac
254
255 exit "$RETURN"