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