fix CHECK_KEYSERVER variable in monkeysphere, so the default is correct for proxycomm...
[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)                   import existing ssh key as gpg subkey
49    --keyfile (-f) FILE                 key file to import
50    --expire (-e) EXPIRE                date to expire
51  gen-subkey (g) [KEYID]              generate an authentication subkey
52    --length (-l) BITS                  key length in bits (2048)
53    --expire (-e) EXPIRE                date to expire
54  ssh-proxycommand                    monkeysphere ssh ProxyCommand
55  subkey-to-ssh-agent (s)             store authentication subkey in ssh-agent
56  version (v)                         show version number
57  help (h,?)                          this help
58
59 EOF
60 }
61
62 ########################################################################
63 # MAIN
64 ########################################################################
65
66 # set unset default variables
67 GNUPGHOME=${GNUPGHOME:="${HOME}/.gnupg"}
68 KNOWN_HOSTS="${HOME}/.ssh/known_hosts"
69 HASH_KNOWN_HOSTS="true"
70 AUTHORIZED_KEYS="${HOME}/.ssh/authorized_keys"
71
72 # unset the check keyserver variable, since that needs to have
73 # different defaults for the different functions
74 unset CHECK_KEYSERVER
75
76 # load global config
77 [ -r "${SYSCONFIGDIR}/monkeysphere.conf" ] \
78     && . "${SYSCONFIGDIR}/monkeysphere.conf"
79
80 # set monkeysphere home directory
81 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.monkeysphere"}
82 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
83
84 # load local config
85 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] \
86     && . "$MONKEYSPHERE_CONFIG"
87
88 # set empty config variables with ones from the environment
89 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=$GNUPGHOME}
90 LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
91 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
92 # if keyserver not specified in env or conf, then look in gpg.conf
93 if [ -z "$KEYSERVER" ] ; then
94     if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
95         KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
96     fi
97 fi
98 PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
99 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=$KNOWN_HOSTS}
100 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=$HASH_KNOWN_HOSTS}
101 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=$AUTHORIZED_KEYS}
102
103 # other variables not in config file
104 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
105 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
106 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
107
108 # export GNUPGHOME and make sure gpg home exists with proper
109 # permissions
110 export GNUPGHOME
111 mkdir -p -m 0700 "$GNUPGHOME"
112 export LOG_LEVEL
113
114 # get subcommand
115 COMMAND="$1"
116 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
117 shift
118
119 case $COMMAND in
120     'update-known_hosts'|'update-known-hosts'|'k')
121         # whether or not to check keyservers
122         CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
123
124         # if hosts are specified on the command line, process just
125         # those hosts
126         if [ "$1" ] ; then
127             update_known_hosts "$@"
128             RETURN="$?"
129
130         # otherwise, if no hosts are specified, process every host
131         # in the user's known_hosts file
132         else
133             process_known_hosts
134             RETURN="$?"
135         fi
136         ;;
137
138     'update-authorized_keys'|'update-authorized-keys'|'a')
139         # whether or not to check keyservers
140         CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
141
142         # process authorized_user_ids file
143         process_authorized_user_ids "$AUTHORIZED_USER_IDS"
144         RETURN="$?"
145         ;;
146
147     'import-subkey'|'i')
148         source "${MSHAREDIR}/import_subkey"
149         import_subkey "$@"
150         ;;
151
152     'gen-subkey'|'g')
153         source "${MSHAREDIR}/gen_subkey"
154         gen_subkey "$@"
155         ;;
156
157     'ssh-proxycommand'|'p')
158         source "${MSHAREDIR}/ssh_proxycommand"
159         ssh_proxycommand "$@"
160         ;;
161
162     'subkey-to-ssh-agent'|'s')
163         source "${MSHAREDIR}/subkey_to_ssh_agent"
164         subkey_to_ssh_agent "$@"
165         ;;
166
167     'version'|'v')
168         echo "$VERSION"
169         ;;
170
171     '--help'|'help'|'-h'|'h'|'?')
172         usage
173         ;;
174
175     *)
176         failure "Unknown command: '$COMMAND'
177 Type '$PGRM help' for usage."
178         ;;
179 esac
180
181 exit "$RETURN"