summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: 0d8f4ff8011adb8882ee6f6a4b6b47212b02723d (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 "$keyID" | 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:"
  74. for key in $(echo "$gpgSecOut" | cut -d: -f5) ; do
  75. echo " $key"
  76. done
  77. echo "Please specify which primary key to use."
  78. failure
  79. ;;
  80. esac
  81. }
  82. # check that a valid authentication subkey does not already exist
  83. check_gpg_authentication_subkey() {
  84. local keyID
  85. local IFS
  86. local line
  87. local type
  88. local validity
  89. local usage
  90. keyID="$1"
  91. # check that a valid authentication key does not already exist
  92. IFS=$'\n'
  93. for line in $(gpg_user --fixed-list-mode --list-keys --with-colons "$keyID") ; do
  94. type=$(echo "$line" | cut -d: -f1)
  95. validity=$(echo "$line" | cut -d: -f2)
  96. usage=$(echo "$line" | cut -d: -f12)
  97. # look at keys only
  98. if [ "$type" != 'pub' -a "$type" != 'sub' ] ; then
  99. continue
  100. fi
  101. # check for authentication capability
  102. if ! check_capability "$usage" 'a' ; then
  103. continue
  104. fi
  105. # if authentication key is valid, prompt to continue
  106. if [ "$validity" = 'u' ] ; then
  107. echo "A valid authentication key already exists for primary key '$keyID'."
  108. if [ "$PROMPT" = "true" ] ; then
  109. read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
  110. if [ "${OK/y/Y}" != 'Y' ] ; then
  111. failure "aborting."
  112. fi
  113. break
  114. else
  115. failure "aborting."
  116. fi
  117. fi
  118. done
  119. }
  120. ########################################################################
  121. # MAIN
  122. ########################################################################
  123. # set unset default variables
  124. GNUPGHOME=${GNUPGHOME:="${HOME}/.gnupg"}
  125. KNOWN_HOSTS="${HOME}/.ssh/known_hosts"
  126. HASH_KNOWN_HOSTS="true"
  127. AUTHORIZED_KEYS="${HOME}/.ssh/authorized_keys"
  128. # unset the check keyserver variable, since that needs to have
  129. # different defaults for the different functions
  130. unset CHECK_KEYSERVER
  131. # load global config
  132. [ -r "${SYSCONFIGDIR}/monkeysphere.conf" ] \
  133. && . "${SYSCONFIGDIR}/monkeysphere.conf"
  134. # set monkeysphere home directory
  135. MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.monkeysphere"}
  136. mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
  137. # load local config
  138. [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] \
  139. && . "$MONKEYSPHERE_CONFIG"
  140. # set empty config variables with ones from the environment
  141. GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=$GNUPGHOME}
  142. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  143. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  144. # if keyserver not specified in env or conf, then look in gpg.conf
  145. if [ -z "$KEYSERVER" ] ; then
  146. if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
  147. KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
  148. fi
  149. fi
  150. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  151. KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=$KNOWN_HOSTS}
  152. HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=$HASH_KNOWN_HOSTS}
  153. AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=$AUTHORIZED_KEYS}
  154. # other variables not in config file
  155. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
  156. REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
  157. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  158. # export GNUPGHOME and make sure gpg home exists with proper
  159. # permissions
  160. export GNUPGHOME
  161. mkdir -p -m 0700 "$GNUPGHOME"
  162. export LOG_LEVEL
  163. # get subcommand
  164. COMMAND="$1"
  165. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  166. shift
  167. case $COMMAND in
  168. 'update-known_hosts'|'update-known-hosts'|'k')
  169. # whether or not to check keyservers
  170. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  171. # if hosts are specified on the command line, process just
  172. # those hosts
  173. if [ "$1" ] ; then
  174. update_known_hosts "$@"
  175. # otherwise, if no hosts are specified, process every host
  176. # in the user's known_hosts file
  177. else
  178. process_known_hosts
  179. fi
  180. ;;
  181. 'update-authorized_keys'|'update-authorized-keys'|'a')
  182. # whether or not to check keyservers
  183. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  184. # process authorized_user_ids file
  185. process_authorized_user_ids "$AUTHORIZED_USER_IDS"
  186. ;;
  187. 'import-subkey'|'i')
  188. source "${MSHAREDIR}/import_subkey"
  189. import_subkey "$@"
  190. ;;
  191. 'gen-subkey'|'g')
  192. source "${MSHAREDIR}/gen_subkey"
  193. gen_subkey "$@"
  194. ;;
  195. 'ssh-proxycommand'|'p')
  196. source "${MSHAREDIR}/ssh_proxycommand"
  197. ssh_proxycommand "$@"
  198. ;;
  199. 'subkey-to-ssh-agent'|'s')
  200. source "${MSHAREDIR}/subkey_to_ssh_agent"
  201. subkey_to_ssh_agent "$@"
  202. ;;
  203. 'version'|'v')
  204. echo "$VERSION"
  205. ;;
  206. '--help'|'help'|'-h'|'h'|'?')
  207. usage
  208. ;;
  209. *)
  210. failure "Unknown command: '$COMMAND'
  211. Type '$PGRM help' for usage."
  212. ;;
  213. esac