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