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