summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: 992ca063ab475f5a554db0c09f787941f8807c34 (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) import existing ssh key as gpg subkey
  38. --keyfile (-f) FILE key file to import
  39. --expire (-e) EXPIRE date to expire
  40. gen-subkey (g) [KEYID] generate an authentication subkey
  41. --length (-l) BITS key length in bits (2048)
  42. --expire (-e) EXPIRE date to expire
  43. ssh-proxycommand monkeysphere ssh ProxyCommand
  44. subkey-to-ssh-agent (s) store authentication subkey in ssh-agent
  45. version (v) show version number
  46. help (h,?) this help
  47. EOF
  48. }
  49. ########################################################################
  50. # MAIN
  51. ########################################################################
  52. # set unset default variables
  53. GNUPGHOME=${GNUPGHOME:="${HOME}/.gnupg"}
  54. KNOWN_HOSTS="${HOME}/.ssh/known_hosts"
  55. HASH_KNOWN_HOSTS="true"
  56. AUTHORIZED_KEYS="${HOME}/.ssh/authorized_keys"
  57. # unset the check keyserver variable, since that needs to have
  58. # different defaults for the different functions
  59. unset CHECK_KEYSERVER
  60. # load global config
  61. [ -r "${SYSCONFIGDIR}/monkeysphere.conf" ] \
  62. && . "${SYSCONFIGDIR}/monkeysphere.conf"
  63. # set monkeysphere home directory
  64. MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.monkeysphere"}
  65. mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
  66. # load local config
  67. [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] \
  68. && . "$MONKEYSPHERE_CONFIG"
  69. # set empty config variables with ones from the environment
  70. GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=$GNUPGHOME}
  71. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  72. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  73. # if keyserver not specified in env or conf, then look in gpg.conf
  74. if [ -z "$KEYSERVER" ] ; then
  75. if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
  76. KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
  77. fi
  78. fi
  79. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  80. KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=$KNOWN_HOSTS}
  81. HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=$HASH_KNOWN_HOSTS}
  82. AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=$AUTHORIZED_KEYS}
  83. # other variables not in config file
  84. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
  85. REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
  86. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  87. # export GNUPGHOME and make sure gpg home exists with proper
  88. # permissions
  89. export GNUPGHOME
  90. mkdir -p -m 0700 "$GNUPGHOME"
  91. export LOG_LEVEL
  92. # get subcommand
  93. COMMAND="$1"
  94. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  95. shift
  96. case $COMMAND in
  97. 'update-known_hosts'|'update-known-hosts'|'k')
  98. # whether or not to check keyservers
  99. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  100. # if hosts are specified on the command line, process just
  101. # those hosts
  102. if [ "$1" ] ; then
  103. update_known_hosts "$@"
  104. RETURN="$?"
  105. # otherwise, if no hosts are specified, process every host
  106. # in the user's known_hosts file
  107. else
  108. process_known_hosts
  109. RETURN="$?"
  110. fi
  111. ;;
  112. 'update-authorized_keys'|'update-authorized-keys'|'a')
  113. # whether or not to check keyservers
  114. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  115. # process authorized_user_ids file
  116. process_authorized_user_ids "$AUTHORIZED_USER_IDS"
  117. RETURN="$?"
  118. ;;
  119. 'import-subkey'|'i')
  120. source "${MSHAREDIR}/import_subkey"
  121. import_subkey "$@"
  122. ;;
  123. 'gen-subkey'|'g')
  124. source "${MSHAREDIR}/gen_subkey"
  125. gen_subkey "$@"
  126. ;;
  127. 'ssh-proxycommand'|'p')
  128. source "${MSHAREDIR}/ssh_proxycommand"
  129. ssh_proxycommand "$@"
  130. ;;
  131. 'subkey-to-ssh-agent'|'s')
  132. source "${MSHAREDIR}/subkey_to_ssh_agent"
  133. subkey_to_ssh_agent "$@"
  134. ;;
  135. 'version'|'v')
  136. echo "$VERSION"
  137. ;;
  138. '--help'|'help'|'-h'|'h'|'?')
  139. usage
  140. ;;
  141. *)
  142. failure "Unknown command: '$COMMAND'
  143. Type '$PGRM help' for usage."
  144. ;;
  145. esac
  146. exit "$RETURN"