summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: 0726ff94a61fdd492f6475978fec41bee711ba80 (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. set -e
  14. PGRM=$(basename $0)
  15. SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
  16. export SYSSHAREDIR
  17. . "${SYSSHAREDIR}/defaultenv"
  18. . "${SYSSHAREDIR}/common"
  19. # sharedir for host functions
  20. MSHAREDIR="${SYSSHAREDIR}/m"
  21. # UTC date in ISO 8601 format if needed
  22. DATE=$(date -u '+%FT%T')
  23. # unset some environment variables that could screw things up
  24. unset GREP_OPTIONS
  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. ssh-proxycommand HOST [PORT] monkeysphere ssh ProxyCommand
  38. --no-connect do not make TCP connection to host
  39. subkey-to-ssh-agent (s) store authentication subkey in ssh-agent
  40. sshfpr (f) KEYID output ssh fingerprint of gpg key
  41. keys-from-userid (u) USERID output valid keys for user id literal
  42. gen-subkey (g) [KEYID] generate an authentication subkey
  43. --length (-l) BITS key length in bits (2048)
  44. version (v) show version number
  45. help (h,?) this help
  46. EOF
  47. }
  48. # user gpg command to define common options
  49. gpg_user() {
  50. gpg --no-greeting --quiet --no-tty "$@"
  51. }
  52. # output the ssh fingerprint of a gpg key
  53. gpg_ssh_fingerprint() {
  54. keyid="$1"
  55. local tmpfile=$(mktemp)
  56. # trap to remove tmp file if break
  57. trap "rm -f $tmpfile" EXIT
  58. # use temporary file, since ssh-keygen won't accept keys on stdin
  59. gpg_user --export "$keyid" | openpgp2ssh "$keyid" >"$tmpfile"
  60. ssh-keygen -l -f "$tmpfile" | awk '{ print $1, $2, $4 }'
  61. # remove the tmp file
  62. trap - EXIT
  63. rm -rf "$tmpfile"
  64. }
  65. # take a secret key ID and check that only zero or one ID is provided,
  66. # and that it corresponds to only a single secret key ID
  67. check_gpg_sec_key_id() {
  68. local gpgSecOut
  69. case "$#" in
  70. 0)
  71. gpgSecOut=$(gpg_user --fixed-list-mode --list-secret-keys --with-colons 2>/dev/null | egrep '^sec:')
  72. ;;
  73. 1)
  74. gpgSecOut=$(gpg_user --fixed-list-mode --list-secret-keys --with-colons "$1" | egrep '^sec:') || failure
  75. ;;
  76. *)
  77. failure "You must specify only a single primary key ID."
  78. ;;
  79. esac
  80. # check that only a single secret key was found
  81. case $(echo "$gpgSecOut" | grep -c '^sec:') in
  82. 0)
  83. failure "No secret keys found. Create an OpenPGP key with the following command:
  84. gpg --gen-key"
  85. ;;
  86. 1)
  87. echo "$gpgSecOut" | cut -d: -f5
  88. ;;
  89. *)
  90. local seckeys=$(echo "$gpgSecOut" | cut -d: -f5)
  91. failure "Multiple primary secret keys found:
  92. $seckeys
  93. Please specify which primary key to use."
  94. ;;
  95. esac
  96. }
  97. # check that a valid authentication subkey does not already exist
  98. check_gpg_authentication_subkey() {
  99. local keyID
  100. local IFS
  101. local line
  102. local type
  103. local validity
  104. local usage
  105. keyID="$1"
  106. # check that a valid authentication key does not already exist
  107. IFS=$'\n'
  108. for line in $(gpg_user --fixed-list-mode --list-keys --with-colons "$keyID") ; do
  109. type=$(echo "$line" | cut -d: -f1)
  110. validity=$(echo "$line" | cut -d: -f2)
  111. usage=$(echo "$line" | cut -d: -f12)
  112. # look at keys only
  113. if [ "$type" != 'pub' -a "$type" != 'sub' ] ; then
  114. continue
  115. fi
  116. # check for authentication capability
  117. if ! check_capability "$usage" 'a' ; then
  118. continue
  119. fi
  120. # if authentication key is valid, prompt to continue
  121. if [ "$validity" = 'u' ] ; then
  122. echo "A valid authentication key already exists for primary key '$keyID'." 1>&2
  123. if [ "$PROMPT" = "true" ] ; then
  124. printf "Are you sure you would like to generate another one? (y/N) " >&2
  125. read OK; OK=${OK:N}
  126. if [ "${OK/y/Y}" != 'Y' ] ; then
  127. failure "aborting."
  128. fi
  129. break
  130. else
  131. failure "aborting."
  132. fi
  133. fi
  134. done
  135. }
  136. ########################################################################
  137. # MAIN
  138. ########################################################################
  139. # set unset default variables
  140. GNUPGHOME=${GNUPGHOME:="${HOME}/.gnupg"}
  141. KNOWN_HOSTS="${HOME}/.ssh/known_hosts"
  142. HASH_KNOWN_HOSTS="true"
  143. AUTHORIZED_KEYS="${HOME}/.ssh/authorized_keys"
  144. # unset the check keyserver variable, since that needs to have
  145. # different defaults for the different functions
  146. unset CHECK_KEYSERVER
  147. # load global config
  148. [ -r "${SYSCONFIGDIR}/monkeysphere.conf" ] \
  149. && . "${SYSCONFIGDIR}/monkeysphere.conf"
  150. # set monkeysphere home directory
  151. MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.monkeysphere"}
  152. mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
  153. # load local config
  154. [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] \
  155. && . "$MONKEYSPHERE_CONFIG"
  156. # set empty config variables with ones from the environment
  157. GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=$GNUPGHOME}
  158. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  159. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  160. # if keyserver not specified in env or conf, then look in gpg.conf
  161. if [ -z "$KEYSERVER" ] ; then
  162. if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
  163. KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
  164. fi
  165. fi
  166. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  167. KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=$KNOWN_HOSTS}
  168. HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=$HASH_KNOWN_HOSTS}
  169. AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=$AUTHORIZED_KEYS}
  170. STRICT_MODES=${MONKEYSPHERE_STRICT_MODES:=$STRICT_MODES}
  171. # other variables not in config file
  172. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
  173. REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
  174. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  175. # note that only using '=' instead of ':=' tests only if the variable
  176. # in unset, not if it's "null"
  177. LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX='ms: '}
  178. # export GNUPGHOME and make sure gpg home exists with proper
  179. # permissions
  180. export GNUPGHOME
  181. mkdir -p -m 0700 "$GNUPGHOME"
  182. export LOG_LEVEL
  183. export LOG_PREFIX
  184. # get subcommand
  185. COMMAND="$1"
  186. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  187. shift
  188. case $COMMAND in
  189. 'update-known_hosts'|'update-known-hosts'|'k')
  190. # whether or not to check keyservers
  191. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  192. # if hosts are specified on the command line, process just
  193. # those hosts
  194. if [ "$1" ] ; then
  195. update_known_hosts "$@"
  196. # otherwise, if no hosts are specified, process every host
  197. # in the user's known_hosts file
  198. else
  199. process_known_hosts
  200. fi
  201. ;;
  202. 'update-authorized_keys'|'update-authorized-keys'|'a')
  203. # whether or not to check keyservers
  204. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  205. # process authorized_user_ids file
  206. process_authorized_user_ids "$AUTHORIZED_USER_IDS"
  207. ;;
  208. 'import-subkey'|'i')
  209. source "${MSHAREDIR}/import_subkey"
  210. import_subkey "$@"
  211. ;;
  212. 'gen-subkey'|'g')
  213. source "${MSHAREDIR}/gen_subkey"
  214. gen_subkey "$@"
  215. ;;
  216. 'ssh-proxycommand'|'p')
  217. source "${MSHAREDIR}/ssh_proxycommand"
  218. ssh_proxycommand "$@"
  219. ;;
  220. 'subkey-to-ssh-agent'|'s')
  221. source "${MSHAREDIR}/subkey_to_ssh_agent"
  222. subkey_to_ssh_agent "$@"
  223. ;;
  224. 'sshfpr'|'f')
  225. gpg_ssh_fingerprint "$@"
  226. ;;
  227. 'keys-from-userid'|'u')
  228. keys_from_userid "$@"
  229. ;;
  230. 'version'|'v')
  231. version
  232. ;;
  233. '--help'|'help'|'-h'|'h'|'?')
  234. usage
  235. ;;
  236. *)
  237. failure "Unknown command: '$COMMAND'
  238. Type '$PGRM help' for usage."
  239. ;;
  240. esac