summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: 147c179403d993aa80d6ea6f1acf9c0e7bf389be (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. version (v) show version number
  43. help (h,?) this help
  44. EOF
  45. }
  46. # user gpg command to define common options
  47. gpg_user() {
  48. gpg --no-greeting --quiet --no-tty "$@"
  49. }
  50. # take a secret key ID and check that only zero or one ID is provided,
  51. # and that it corresponds to only a single secret key ID
  52. check_gpg_sec_key_id() {
  53. local gpgSecOut
  54. case "$#" in
  55. 0)
  56. gpgSecOut=$(gpg_user --fixed-list-mode --list-secret-keys --with-colons 2>/dev/null | egrep '^sec:')
  57. ;;
  58. 1)
  59. gpgSecOut=$(gpg_user --fixed-list-mode --list-secret-keys --with-colons "$1" | egrep '^sec:') || failure
  60. ;;
  61. *)
  62. failure "You must specify only a single primary key ID."
  63. ;;
  64. esac
  65. # check that only a single secret key was found
  66. case $(echo "$gpgSecOut" | grep -c '^sec:') in
  67. 0)
  68. failure "No secret keys found. Create an OpenPGP key with the following command:
  69. gpg --gen-key"
  70. ;;
  71. 1)
  72. echo "$gpgSecOut" | cut -d: -f5
  73. ;;
  74. *)
  75. local seckeys=$(echo "$gpgSecOut" | cut -d: -f5)
  76. failure "Multiple primary secret keys found:
  77. $seckeys
  78. Please specify which primary key to use."
  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'." 1>&2
  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. # note that only using '=' instead of ':=' tests only if the variable
  159. # in unset, not if it's "null"
  160. LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX='ms: '}
  161. # export GNUPGHOME and make sure gpg home exists with proper
  162. # permissions
  163. export GNUPGHOME
  164. mkdir -p -m 0700 "$GNUPGHOME"
  165. export LOG_LEVEL
  166. # get subcommand
  167. COMMAND="$1"
  168. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  169. shift
  170. case $COMMAND in
  171. 'update-known_hosts'|'update-known-hosts'|'k')
  172. # whether or not to check keyservers
  173. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  174. # if hosts are specified on the command line, process just
  175. # those hosts
  176. if [ "$1" ] ; then
  177. update_known_hosts "$@"
  178. # otherwise, if no hosts are specified, process every host
  179. # in the user's known_hosts file
  180. else
  181. process_known_hosts
  182. fi
  183. ;;
  184. 'update-authorized_keys'|'update-authorized-keys'|'a')
  185. # whether or not to check keyservers
  186. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  187. # process authorized_user_ids file
  188. process_authorized_user_ids "$AUTHORIZED_USER_IDS"
  189. ;;
  190. 'import-subkey'|'i')
  191. source "${MSHAREDIR}/import_subkey"
  192. import_subkey "$@"
  193. ;;
  194. 'gen-subkey'|'g')
  195. source "${MSHAREDIR}/gen_subkey"
  196. gen_subkey "$@"
  197. ;;
  198. 'ssh-proxycommand'|'p')
  199. source "${MSHAREDIR}/ssh_proxycommand"
  200. ssh_proxycommand "$@"
  201. ;;
  202. 'subkey-to-ssh-agent'|'s')
  203. source "${MSHAREDIR}/subkey_to_ssh_agent"
  204. subkey_to_ssh_agent "$@"
  205. ;;
  206. 'version'|'v')
  207. version
  208. ;;
  209. '--help'|'help'|'-h'|'h'|'?')
  210. usage
  211. ;;
  212. *)
  213. failure "Unknown command: '$COMMAND'
  214. Type '$PGRM help' for usage."
  215. ;;
  216. esac