summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: a65cef62273e76671bc6fe658564195551a2ad8a (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. 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 "$keyID" | 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. echo "Multiple primary secret keys found:" | log error
  75. echo "$gpgSecOut" | cut -d: -f5 | log error
  76. echo "Please specify which primary key to use." | log error
  77. failure
  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'."
  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. # export GNUPGHOME and make sure gpg home exists with proper
  158. # permissions
  159. export GNUPGHOME
  160. mkdir -p -m 0700 "$GNUPGHOME"
  161. export LOG_LEVEL
  162. # get subcommand
  163. COMMAND="$1"
  164. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  165. shift
  166. case $COMMAND in
  167. 'update-known_hosts'|'update-known-hosts'|'k')
  168. # whether or not to check keyservers
  169. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  170. # if hosts are specified on the command line, process just
  171. # those hosts
  172. if [ "$1" ] ; then
  173. update_known_hosts "$@"
  174. RETURN="$?"
  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. RETURN="$?"
  180. fi
  181. ;;
  182. 'update-authorized_keys'|'update-authorized-keys'|'a')
  183. # whether or not to check keyservers
  184. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  185. # process authorized_user_ids file
  186. process_authorized_user_ids "$AUTHORIZED_USER_IDS"
  187. RETURN="$?"
  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. echo "$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
  216. exit "$RETURN"