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