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