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