summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: 6f43632307b25d235ed5094457e96fe77de5b7a8 (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. read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
  124. if [ "${OK/y/Y}" != 'Y' ] ; then
  125. failure "aborting."
  126. fi
  127. break
  128. else
  129. failure "aborting."
  130. fi
  131. fi
  132. done
  133. }
  134. ########################################################################
  135. # MAIN
  136. ########################################################################
  137. # set unset default variables
  138. GNUPGHOME=${GNUPGHOME:="${HOME}/.gnupg"}
  139. KNOWN_HOSTS="${HOME}/.ssh/known_hosts"
  140. HASH_KNOWN_HOSTS="true"
  141. AUTHORIZED_KEYS="${HOME}/.ssh/authorized_keys"
  142. # unset the check keyserver variable, since that needs to have
  143. # different defaults for the different functions
  144. unset CHECK_KEYSERVER
  145. # load global config
  146. [ -r "${SYSCONFIGDIR}/monkeysphere.conf" ] \
  147. && . "${SYSCONFIGDIR}/monkeysphere.conf"
  148. # set monkeysphere home directory
  149. MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.monkeysphere"}
  150. mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
  151. # load local config
  152. [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] \
  153. && . "$MONKEYSPHERE_CONFIG"
  154. # set empty config variables with ones from the environment
  155. GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=$GNUPGHOME}
  156. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  157. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  158. # if keyserver not specified in env or conf, then look in gpg.conf
  159. if [ -z "$KEYSERVER" ] ; then
  160. if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
  161. KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
  162. fi
  163. fi
  164. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  165. KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=$KNOWN_HOSTS}
  166. HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=$HASH_KNOWN_HOSTS}
  167. AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=$AUTHORIZED_KEYS}
  168. # other variables not in config file
  169. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
  170. REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
  171. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  172. # note that only using '=' instead of ':=' tests only if the variable
  173. # in unset, not if it's "null"
  174. LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX='ms: '}
  175. # export GNUPGHOME and make sure gpg home exists with proper
  176. # permissions
  177. export GNUPGHOME
  178. mkdir -p -m 0700 "$GNUPGHOME"
  179. export LOG_LEVEL
  180. # get subcommand
  181. COMMAND="$1"
  182. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  183. shift
  184. case $COMMAND in
  185. 'update-known_hosts'|'update-known-hosts'|'k')
  186. # whether or not to check keyservers
  187. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  188. # if hosts are specified on the command line, process just
  189. # those hosts
  190. if [ "$1" ] ; then
  191. update_known_hosts "$@"
  192. # otherwise, if no hosts are specified, process every host
  193. # in the user's known_hosts file
  194. else
  195. process_known_hosts
  196. fi
  197. ;;
  198. 'update-authorized_keys'|'update-authorized-keys'|'a')
  199. # whether or not to check keyservers
  200. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  201. # process authorized_user_ids file
  202. process_authorized_user_ids "$AUTHORIZED_USER_IDS"
  203. ;;
  204. 'import-subkey'|'i')
  205. source "${MSHAREDIR}/import_subkey"
  206. import_subkey "$@"
  207. ;;
  208. 'gen-subkey'|'g')
  209. source "${MSHAREDIR}/gen_subkey"
  210. gen_subkey "$@"
  211. ;;
  212. 'ssh-proxycommand'|'p')
  213. source "${MSHAREDIR}/ssh_proxycommand"
  214. ssh_proxycommand "$@"
  215. ;;
  216. 'subkey-to-ssh-agent'|'s')
  217. source "${MSHAREDIR}/subkey_to_ssh_agent"
  218. subkey_to_ssh_agent "$@"
  219. ;;
  220. 'sshfpr'|'f')
  221. gpg_ssh_fingerprint "$@"
  222. ;;
  223. 'version'|'v')
  224. version
  225. ;;
  226. '--help'|'help'|'-h'|'h'|'?')
  227. usage
  228. ;;
  229. *)
  230. failure "Unknown command: '$COMMAND'
  231. Type '$PGRM help' for usage."
  232. ;;
  233. esac