summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: cac9a02399bc086bf64dcbbb6c172f3c172fe087 (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. PGRM=$(basename $0)
  14. SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
  15. export SYSSHAREDIR
  16. . "${SYSSHAREDIR}/common" || exit 1
  17. # sharedir for host functions
  18. MSHAREDIR="${SYSSHAREDIR}/m"
  19. # UTC date in ISO 8601 format if needed
  20. DATE=$(date -u '+%FT%T')
  21. # unset some environment variables that could screw things up
  22. unset GREP_OPTIONS
  23. # default return code
  24. RETURN=0
  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. import-subkey (i) FILE [KEYID] import existing ssh key as gpg subkey
  38. gen-subkey (g) [KEYID] generate an authentication subkey
  39. --length (-l) BITS key length in bits (2048)
  40. ssh-proxycommand monkeysphere ssh ProxyCommand
  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 "$keyID" | 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. echo "Multiple primary secret keys found:" | log error
  76. echo "$gpgSecOut" | cut -d: -f5 | log error
  77. echo "Please specify which primary key to use." | log error
  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. RETURN="$?"
  176. # otherwise, if no hosts are specified, process every host
  177. # in the user's known_hosts file
  178. else
  179. process_known_hosts
  180. RETURN="$?"
  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. RETURN="$?"
  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. echo "$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
  217. exit "$RETURN"