summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: 8b7bfee49f5860e18269699fbec1325bb55824e6 (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 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. # unset variables that should be defined only in config file
  53. unset KEYSERVER
  54. unset CHECK_KEYSERVER
  55. unset KNOWN_HOSTS
  56. unset HASH_KNOWN_HOSTS
  57. unset AUTHORIZED_KEYS
  58. # load global config
  59. [ -r "${SYSCONFIGDIR}/monkeysphere.conf" ] && . "${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"} ] && . "$MONKEYSPHERE_CONFIG"
  65. # set empty config variables with ones from the environment, or from
  66. # config file, or with defaults
  67. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
  68. GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
  69. KEYSERVER=${MONKEYSPHERE_KEYSERVER:="$KEYSERVER"}
  70. # if keyserver not specified in env or monkeysphere.conf,
  71. # look in gpg.conf
  72. if [ -z "$KEYSERVER" ] ; then
  73. if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
  74. KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
  75. fi
  76. fi
  77. # if it's still not specified, use the default
  78. KEYSERVER=${KEYSERVER:="subkeys.pgp.net"}
  79. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  80. KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
  81. HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
  82. AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/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. MODE='known_hosts'
  99. # touch the known_hosts file so that the file permission check
  100. # below won't fail upon not finding the file
  101. (umask 0022 && touch "$KNOWN_HOSTS")
  102. # check permissions on the known_hosts file path
  103. check_key_file_permissions "$USER" "$KNOWN_HOSTS" || failure
  104. # if hosts are specified on the command line, process just
  105. # those hosts
  106. if [ "$1" ] ; then
  107. update_known_hosts "$@"
  108. RETURN="$?"
  109. # otherwise, if no hosts are specified, process every host
  110. # in the user's known_hosts file
  111. else
  112. # exit if the known_hosts file does not exist
  113. if [ ! -e "$KNOWN_HOSTS" ] ; then
  114. log error "known_hosts file '$KNOWN_HOSTS' does not exist."
  115. exit
  116. fi
  117. process_known_hosts
  118. RETURN="$?"
  119. fi
  120. ;;
  121. 'update-authorized_keys'|'update-authorized-keys'|'a')
  122. MODE='authorized_keys'
  123. # check permissions on the authorized_user_ids file path
  124. check_key_file_permissions "$USER" "$AUTHORIZED_USER_IDS" || failure
  125. # check permissions on the authorized_keys file path
  126. check_key_file_permissions "$USER" "$AUTHORIZED_KEYS" || failure
  127. # exit if the authorized_user_ids file is empty
  128. if [ ! -e "$AUTHORIZED_USER_IDS" ] ; then
  129. log error "authorized_user_ids file '$AUTHORIZED_USER_IDS' does not exist."
  130. exit
  131. fi
  132. # process authorized_user_ids file
  133. process_authorized_user_ids "$AUTHORIZED_USER_IDS"
  134. RETURN="$?"
  135. ;;
  136. 'import-subkey'|'i')
  137. source "${MSHAREDIR}/import_subkey"
  138. import_subkey "$@"
  139. ;;
  140. 'gen-subkey'|'g')
  141. source "${MSHAREDIR}/gen_subkey"
  142. gen_subkey "$@"
  143. ;;
  144. 'ssh-proxycommand'|'p')
  145. source "${MSHAREDIR}/ssh_proxycommand"
  146. ssh_proxycommand "$@"
  147. ;;
  148. 'subkey-to-ssh-agent'|'s')
  149. source "${MSHAREDIR}/subkey_to_ssh_agent"
  150. subkey_to_ssh_agent "$@"
  151. ;;
  152. 'version'|'v')
  153. echo "$VERSION"
  154. ;;
  155. '--help'|'help'|'-h'|'h'|'?')
  156. usage
  157. ;;
  158. *)
  159. failure "Unknown command: '$COMMAND'
  160. Type '$PGRM help' for usage."
  161. ;;
  162. esac
  163. exit "$RETURN"